|
@@ -0,0 +1,289 @@
|
|
|
+from sqlalchemy import and_, or_
|
|
|
+from datetime import datetime
|
|
|
+from typing import Optional
|
|
|
+
|
|
|
+import tools.db_helper as db_helper
|
|
|
+from core.dtos import ProjectQuotaDto
|
|
|
+from core.models import ProjectQuotaModel
|
|
|
+from core.user_session import UserSession
|
|
|
+
|
|
|
+class ProjectQuotaStore:
|
|
|
+ def __init__(self):
|
|
|
+ # self._database= None
|
|
|
+ self._database = 'Iwb_RailwayCosting'
|
|
|
+ self._current_user = None
|
|
|
+
|
|
|
+ @property
|
|
|
+ def current_user(self):
|
|
|
+ if self._current_user is None:
|
|
|
+ self._current_user = UserSession.get_current_user()
|
|
|
+ return self._current_user
|
|
|
+
|
|
|
+ def get_quotas_paginated(
|
|
|
+ self,
|
|
|
+ budget_id: int,
|
|
|
+ project_id: str,
|
|
|
+ item_code: str,
|
|
|
+ page: int = 1,
|
|
|
+ page_size: int = 10,
|
|
|
+ keyword: Optional[str] = None,
|
|
|
+ process_status: Optional[int] = None,
|
|
|
+ send_status: Optional[int] = None,
|
|
|
+ ):
|
|
|
+ """分页查询定额列表
|
|
|
+
|
|
|
+ Args:
|
|
|
+ page: 页码,从1开始
|
|
|
+ page_size: 每页数量
|
|
|
+ project_id: 项目编号
|
|
|
+ budget_id: 概算序号
|
|
|
+ item_code: 条目序号
|
|
|
+ keyword: 关键字
|
|
|
+ process_status: 处理状态
|
|
|
+ send_status: 发送状态
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Tuple[total_count, quotas]
|
|
|
+ """
|
|
|
+ # with db_helper.mysql_query_session(self._database) as db_session:
|
|
|
+ with db_helper.sqlserver_query_session(self._database) as db_session:
|
|
|
+ query = db_session.query(ProjectQuotaModel)
|
|
|
+
|
|
|
+ # 构建查询条件
|
|
|
+ conditions = [
|
|
|
+ ProjectQuotaModel.is_del == 0,
|
|
|
+ ProjectQuotaModel.project_id == project_id,
|
|
|
+ ProjectQuotaModel.budget_id == budget_id,
|
|
|
+ ProjectQuotaModel.item_code.like(f"{item_code}%")
|
|
|
+ ]
|
|
|
+
|
|
|
+ if process_status is not None:
|
|
|
+ conditions.append(ProjectQuotaModel.process_status == process_status)
|
|
|
+ if send_status is not None:
|
|
|
+ conditions.append(ProjectQuotaModel.send_status == send_status)
|
|
|
+ if keyword:
|
|
|
+ conditions.append(or_(
|
|
|
+ ProjectQuotaModel.quota_code.like(f"%{keyword}%"),
|
|
|
+ ProjectQuotaModel.project_name.like(f"%{keyword}%"),
|
|
|
+ ))
|
|
|
+ query = query.filter(and_(*conditions))
|
|
|
+
|
|
|
+ # 计算总数
|
|
|
+ total_count = query.count()
|
|
|
+
|
|
|
+ # 分页
|
|
|
+ query = query.offset((page - 1) * page_size).limit(page_size)
|
|
|
+
|
|
|
+ quotas = query.all()
|
|
|
+
|
|
|
+ return {
|
|
|
+ 'total': total_count,
|
|
|
+ 'data': quotas
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ def get_quotas_by_task_id(self,task_id:int, with_quota_code:bool=False):
|
|
|
+ # with db_helper.sqlserver_query_session(self._database) as db_session:
|
|
|
+ with db_helper.mysql_query_session(self._database) as db_session:
|
|
|
+ query = db_session.query(ProjectQuotaModel).filter(
|
|
|
+ and_(
|
|
|
+ ProjectQuotaModel.task_id == task_id,
|
|
|
+ ProjectQuotaModel.is_del == 0
|
|
|
+ )
|
|
|
+ )
|
|
|
+ if with_quota_code:
|
|
|
+ query = query.filter(and_(ProjectQuotaModel.quota_code!=None,ProjectQuotaModel.quota_code!='') )
|
|
|
+ quotas = query.all()
|
|
|
+ return quotas
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ def get_quota(self, quota_id: int) -> Optional[ProjectQuotaModel]:
|
|
|
+ """根据ID查询定额
|
|
|
+
|
|
|
+ Args:
|
|
|
+ quota_id: 定额ID
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Optional[ProjectQuotaDto]
|
|
|
+ """
|
|
|
+ # with db_helper.mysql_query_session(self._database) as db_session:
|
|
|
+ with db_helper.sqlserver_query_session(self._database) as db_session:
|
|
|
+ quota = db_session.query(ProjectQuotaModel).filter(
|
|
|
+ and_(
|
|
|
+ ProjectQuotaModel.id == quota_id,
|
|
|
+ ProjectQuotaModel.is_del == 0
|
|
|
+ )
|
|
|
+ ).first()
|
|
|
+ return quota
|
|
|
+ def get_quota_dto(self, quota_id: int) -> Optional[ProjectQuotaDto]:
|
|
|
+ """根据ID查询定额
|
|
|
+
|
|
|
+ Args:
|
|
|
+ quota_id: 定额ID
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Optional[ProjectQuotaDto]
|
|
|
+ """
|
|
|
+ quota = self.get_quota(quota_id)
|
|
|
+
|
|
|
+ return ProjectQuotaDto.from_model(quota) if quota else None
|
|
|
+
|
|
|
+ def create_quota(self, quota_dto: ProjectQuotaDto) -> ProjectQuotaDto:
|
|
|
+ """创建定额
|
|
|
+
|
|
|
+ Args:
|
|
|
+ quota_dto: 定额DTO
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ ProjectQuotaDto
|
|
|
+ """
|
|
|
+ # with db_helper.mysql_session(self._database) as db_session:
|
|
|
+ with db_helper.sqlserver_session(self._database) as db_session:
|
|
|
+ quota = ProjectQuotaModel(
|
|
|
+ project_id=quota_dto.project_id,
|
|
|
+ budget_id=quota_dto.budget_id,
|
|
|
+ item_id=quota_dto.item_id,
|
|
|
+ item_code=quota_dto.item_code,
|
|
|
+ quota_code=quota_dto.quota_code,
|
|
|
+ project_name=quota_dto.project_name,
|
|
|
+ unit=quota_dto.unit,
|
|
|
+ project_quantity=quota_dto.project_quantity,
|
|
|
+ project_quantity_input=quota_dto.project_quantity_input,
|
|
|
+ quota_adjustment=quota_dto.quota_adjustment,
|
|
|
+ unit_price=quota_dto.unit_price,
|
|
|
+ total_price=quota_dto.total_price,
|
|
|
+ unit_weight=quota_dto.unit_weight,
|
|
|
+ total_weight=quota_dto.total_weight,
|
|
|
+ labor_cost=quota_dto.labor_cost,
|
|
|
+ process_status=quota_dto.process_status,
|
|
|
+ process_time=quota_dto.process_time,
|
|
|
+ process_error=quota_dto.process_error,
|
|
|
+ send_status=quota_dto.send_status,
|
|
|
+ send_time=quota_dto.send_time,
|
|
|
+ send_error=quota_dto.send_error,
|
|
|
+ created_by=quota_dto.created_by or self.current_user.username,
|
|
|
+ created_at=datetime.now(),
|
|
|
+ )
|
|
|
+
|
|
|
+ db_session.add(quota)
|
|
|
+ db_session.flush()
|
|
|
+ return ProjectQuotaDto.from_model(quota)
|
|
|
+
|
|
|
+ def update_quota(self, quota_dto: ProjectQuotaDto) -> Optional[ProjectQuotaDto]:
|
|
|
+ """更新定额
|
|
|
+
|
|
|
+ Args:
|
|
|
+ quota_dto: 定额DTO
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Optional[ProjectQuotaDto]
|
|
|
+ """
|
|
|
+ quota = self.get_quota(quota_dto.id)
|
|
|
+
|
|
|
+ if not quota:
|
|
|
+ return None
|
|
|
+ # with db_helper.mysql_session(self._database) as db_session:
|
|
|
+ with db_helper.sqlserver_session(self._database) as db_session:
|
|
|
+ quota.quota_code = quota_dto.quota_code
|
|
|
+ quota.project_name = quota_dto.project_name
|
|
|
+ quota.unit = quota_dto.unit
|
|
|
+ quota.project_quantity = quota_dto.project_quantity
|
|
|
+ quota.project_quantity_input = quota_dto.project_quantity_input
|
|
|
+ quota.quota_adjustment = quota_dto.quota_adjustment
|
|
|
+ quota.unit_price = quota_dto.unit_price
|
|
|
+ quota.total_price = quota_dto.total_price
|
|
|
+ quota.unit_weight = quota_dto.unit_weight
|
|
|
+ quota.total_weight = quota_dto.total_weight
|
|
|
+ quota.labor_cost = quota_dto.labor_cost
|
|
|
+ quota.updated_by = self.current_user.username
|
|
|
+ quota.updated_at = datetime.now()
|
|
|
+
|
|
|
+ quota = db_session.merge(quota)
|
|
|
+ return ProjectQuotaDto.from_model(quota)
|
|
|
+
|
|
|
+ def delete_quota(self, quota_id: int) -> bool:
|
|
|
+ """删除定额
|
|
|
+
|
|
|
+ Args:
|
|
|
+ quota_id: 定额ID
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ bool
|
|
|
+ """
|
|
|
+
|
|
|
+ quota = self.get_quota(quota_id)
|
|
|
+ if not quota:
|
|
|
+ return False
|
|
|
+
|
|
|
+ # with db_helper.mysql_session(self._database) as db_session:
|
|
|
+ with db_helper.sqlserver_session(self._database) as db_session:
|
|
|
+ quota.is_del = 1
|
|
|
+ quota.deleted_by = self.current_user.username
|
|
|
+ quota.deleted_at = datetime.now()
|
|
|
+ quota = db_session.merge(quota)
|
|
|
+ return True
|
|
|
+
|
|
|
+ def update_process_status(self,quota_id:int, status:int, err:str = None):
|
|
|
+ """
|
|
|
+ 更新项目定额的流程状态
|
|
|
+ Args:
|
|
|
+ quota_id: 定额ID
|
|
|
+ status: 流程状态
|
|
|
+ err:
|
|
|
+ Returns:
|
|
|
+ bool
|
|
|
+ """
|
|
|
+ quota = self.get_quota(quota_id)
|
|
|
+ if not quota:
|
|
|
+ return False
|
|
|
+ # with db_helper.mysql_session(self._database) as db_session:
|
|
|
+ with db_helper.sqlserver_session(self._database) as db_session:
|
|
|
+ quota.process_status = status
|
|
|
+ quota.process_error = err
|
|
|
+ quota.process_time = datetime.now()
|
|
|
+ quota = db_session.merge(quota)
|
|
|
+ return True
|
|
|
+
|
|
|
+ def update_send_status(self,quota_id:int, status:int, err:str = None) -> bool:
|
|
|
+ """
|
|
|
+ 更新发送状态
|
|
|
+ Args:
|
|
|
+ quota_id: int
|
|
|
+ status: int
|
|
|
+ err: str
|
|
|
+ Returns:
|
|
|
+ bool
|
|
|
+ """
|
|
|
+ quota = self.get_quota(quota_id)
|
|
|
+ if not quota:
|
|
|
+ return False
|
|
|
+ # with db_helper.mysql_session(self._database) as db_session:
|
|
|
+ with db_helper.sqlserver_session(self._database) as db_session:
|
|
|
+ quota.send_status = status
|
|
|
+ quota.send_error = err
|
|
|
+ quota.send_time = datetime.now()
|
|
|
+ quota = db_session.merge(quota)
|
|
|
+ return True
|
|
|
+
|
|
|
+ def update_quota_code(self, quota_dto: ProjectQuotaDto):
|
|
|
+ """更新定额编号
|
|
|
+ Args:
|
|
|
+ quota_dto: 定额DTO
|
|
|
+ Returns:
|
|
|
+ bool
|
|
|
+ """
|
|
|
+ quota = self.get_quota(quota_dto.id)
|
|
|
+ if not quota:
|
|
|
+ return False
|
|
|
+ # with db_helper.mysql_session(self._database) as db_session:
|
|
|
+ with db_helper.sqlserver_session(self._database) as db_session:
|
|
|
+ quota.quota_code = quota_dto.quota_code
|
|
|
+ quota.unit = quota_dto.unit
|
|
|
+ quota.unit_price = quota_dto.unit_price
|
|
|
+ quota.total_price = quota_dto.total_price
|
|
|
+
|
|
|
+ quota.updated_by = self.current_user.username
|
|
|
+ quota.updated_at = datetime.now()
|
|
|
+ db_session.merge(quota)
|
|
|
+ return True
|