quota_input.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. from sqlalchemy import and_, or_
  2. from datetime import datetime
  3. from typing import Optional
  4. import tools.db_helper as db_helper
  5. from core.dtos import QuotaInputDto
  6. from core.models import QuotaInputModel
  7. from core.user_session import UserSession
  8. class QuotaInputStore:
  9. def __init__(self):
  10. self._database = None
  11. self._current_user = None
  12. @property
  13. def current_user(self):
  14. if self._current_user is None:
  15. self._current_user = UserSession.get_current_user()
  16. return self._current_user
  17. def get_quotas_paginated(
  18. self,
  19. project_id: str,
  20. budget_id: int,
  21. item_id: int,
  22. page: int = 1,
  23. page_size: int = 10,
  24. keyword: Optional[str] = None,
  25. ):
  26. """分页查询定额输入列表
  27. Args:
  28. page: 页码,从1开始
  29. page_size: 每页数量
  30. project_id: 总概算序号
  31. budget_id: 总概算序号
  32. item_id: 条目序号
  33. keyword: 关键字
  34. Returns:
  35. """
  36. self._database = project_id
  37. with db_helper.sqlserver_query_session(self._database) as db_session:
  38. query = db_session.query(QuotaInputModel)
  39. # 构建查询条件
  40. conditions = [
  41. QuotaInputModel.budget_id == budget_id,
  42. QuotaInputModel.item_id == item_id,
  43. ]
  44. if keyword:
  45. conditions.append(
  46. or_(
  47. QuotaInputModel.quota_code.like(f"%{keyword}%"),
  48. QuotaInputModel.project_name.like(f"%{keyword}%"),
  49. )
  50. )
  51. query = query.filter(and_(*conditions))
  52. # 获取总数
  53. total_count = query.count()
  54. # 分页
  55. query = query.offset((page - 1) * page_size).limit(page_size)
  56. # 转换为DTO
  57. quotas = [QuotaInputDto.from_model(model) for model in query.all()]
  58. return {"total": total_count, "data": quotas}
  59. def get_quota(self, project_id: str, budget_id: int, item_id: int, quota_code: str):
  60. self._database = project_id
  61. with db_helper.sqlserver_query_session(self._database) as db_session:
  62. model = (
  63. db_session.query(QuotaInputModel)
  64. .filter(
  65. QuotaInputModel.budget_id == budget_id,
  66. QuotaInputModel.item_id == item_id,
  67. QuotaInputModel.quota_code == quota_code,
  68. )
  69. .first()
  70. )
  71. if model is None:
  72. return None
  73. return QuotaInputDto.from_model(model)
  74. def create_quota(self, project_id: str, dto: QuotaInputDto) -> QuotaInputDto:
  75. """创建定额输入
  76. Args:
  77. project_id
  78. dto: 定额输入DTO
  79. Returns:
  80. QuotaInputDto
  81. """
  82. self._database = project_id
  83. with db_helper.sqlserver_session(self._database) as db_session:
  84. model = QuotaInputModel(
  85. budget_id=dto.budget_id,
  86. item_id=dto.item_id,
  87. quota_code=dto.quota_code,
  88. sequence_number=dto.sequence_number,
  89. project_name=dto.project_name,
  90. unit=dto.unit,
  91. project_quantity=dto.project_quantity,
  92. project_quantity_input=dto.project_quantity_input,
  93. quota_adjustment=dto.quota_adjustment,
  94. unit_price=dto.unit_price,
  95. # compilation_unit_price=dto.compilation_unit_price,
  96. # compilation_total_price=dto.compilation_total_price,
  97. unit_weight=dto.unit_weight,
  98. # total_weight=dto.total_weight,
  99. # labor_cost=dto.labor_cost,
  100. # compilation_labor_cost=dto.compilation_labor_cost,
  101. # material_cost=dto.material_cost,
  102. # compilation_material_cost=dto.compilation_material_cost,
  103. # deduct_material_cost=dto.deduct_material_cost,
  104. # compilation_deduct_material_cost=dto.compilation_deduct_material_cost,
  105. # mechanical_cost=dto.mechanical_cost,
  106. # compilation_mechanical_cost=dto.compilation_mechanical_cost,
  107. # equipment_cost=dto.equipment_cost,
  108. # compilation_equipment_cost=dto.compilation_equipment_cost,
  109. # transport_cost=dto.transport_cost,
  110. # compilation_transport_cost=dto.compilation_transport_cost,
  111. # quota_workday=dto.quota_workday,
  112. # total_workday=dto.total_workday,
  113. # workday_salary=dto.workday_salary,
  114. # compilation_workday_salary=dto.compilation_workday_salary,
  115. # quota_mechanical_workday=dto.quota_mechanical_workday,
  116. # total_mechanical_workday=dto.total_mechanical_workday,
  117. # mechanical_workday_salary=dto.mechanical_workday_salary,
  118. # compilation_mechanical_workday_salary=dto.compilation_mechanical_workday_salary,
  119. compiler=dto.compiler,
  120. modify_date=datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
  121. # quota_consumption=dto.quota_consumption,0
  122. # basic_quota=dto.basic_quota,
  123. # quota_comprehensive_unit_price=dto.quota_comprehensive_unit_price,
  124. # quota_comprehensive_total_price=dto.quota_comprehensive_total_price
  125. )
  126. db_session.add(model)
  127. db_session.flush()
  128. return QuotaInputDto.from_model(model)
  129. def update_quota(
  130. self, project_id: str, dto: QuotaInputDto
  131. ) -> Optional[QuotaInputDto]:
  132. """更新定额输入
  133. Args:
  134. project_id:
  135. dto: 定额输入DTO
  136. Returns:
  137. QuotaInputDto or None
  138. """
  139. self._database = project_id
  140. with db_helper.sqlserver_session(self._database) as db_session:
  141. model = (
  142. db_session.query(QuotaInputModel)
  143. .filter(QuotaInputModel.quota_id == dto.quota_id)
  144. .first()
  145. )
  146. if model is None:
  147. return None
  148. model.budget_id = dto.budget_id
  149. model.item_id = dto.item_id
  150. model.quota_code = dto.quota_code
  151. model.sequence_number = dto.sequence_number
  152. model.project_name = dto.project_name
  153. model.unit = dto.unit
  154. model.project_quantity = dto.project_quantity
  155. model.project_quantity_input = str(dto.project_quantity)
  156. model.quota_adjustment = dto.quota_adjustment
  157. model.unit_price = dto.unit_price
  158. # model.compilation_unit_price = dto.compilation_unit_price
  159. # model.total_price = dto.total_price
  160. # model.compilation_total_price = dto.compilation_total_price
  161. model.unit_weight = dto.unit_weight
  162. # model.total_weight = dto.total_weight
  163. # model.labor_cost = dto.labor_cost
  164. # model.compilation_labor_cost = dto.compilation_labor_cost
  165. # model.material_cost = dto.material_cost
  166. # model.compilation_material_cost = dto.compilation_material_cost
  167. # model.deduct_material_cost = dto.deduct_material_cost
  168. # model.compilation_deduct_material_cost = dto.compilation_deduct_material_cost
  169. # model.mechanical_cost = dto.mechanical_cost
  170. # model.compilation_mechanical_cost = dto.compilation_mechanical_cost
  171. # model.equipment_cost = dto.equipment_cost
  172. # model.compilation_equipment_cost = dto.compilation_equipment_cost
  173. # model.transport_cost = dto.transport_cost
  174. # model.compilation_transport_cost = dto.compilation_transport_cost
  175. # model.quota_workday = dto.quota_workday
  176. # model.total_workday = dto.total_workday
  177. # model.workday_salary = dto.workday_salary
  178. # model.compilation_workday_salary = dto.compilation_workday_salary
  179. # model.quota_mechanical_workday = dto.quota_mechanical_workday
  180. # model.total_mechanical_workday = dto.total_mechanical_workday
  181. # model.mechanical_workday_salary = dto.mechanical_workday_salary
  182. # model.compilation_mechanical_workday_salary = dto.compilation_mechanical_workday_salary
  183. # model.compiler = dto.compiler
  184. model.modify_date = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
  185. # model.quota_consumption = dto.quota_consumption
  186. # model.basic_quota = dto.basic_quota
  187. # model.quota_comprehensive_unit_price = dto.quota_comprehensive_unit_price
  188. # model.quota_comprehensive_total_price = dto.quota_comprehensive_total_price
  189. db_session.merge(model)
  190. return QuotaInputDto.from_model(model)
  191. def delete_quota(self, quota_id: int) -> bool:
  192. """删除定额输入
  193. Args:
  194. quota_id: 定额序号
  195. Returns:
  196. bool
  197. """
  198. with db_helper.sqlserver_session(self._database) as db_session:
  199. model = (
  200. db_session.query(QuotaInputModel)
  201. .filter(QuotaInputModel.quota_id == quota_id)
  202. .first()
  203. )
  204. if model is None:
  205. return False
  206. db_session.delete(model)
  207. return True