quota_input.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. # compilation_unit_price=dto.compilation_unit_price,
  95. # compilation_total_price=dto.compilation_total_price,
  96. # unit_weight=dto.unit_weight,
  97. # total_weight=dto.total_weight,
  98. # labor_cost=dto.labor_cost,
  99. # compilation_labor_cost=dto.compilation_labor_cost,
  100. # material_cost=dto.material_cost,
  101. # compilation_material_cost=dto.compilation_material_cost,
  102. # deduct_material_cost=dto.deduct_material_cost,
  103. # compilation_deduct_material_cost=dto.compilation_deduct_material_cost,
  104. # mechanical_cost=dto.mechanical_cost,
  105. # compilation_mechanical_cost=dto.compilation_mechanical_cost,
  106. # equipment_cost=dto.equipment_cost,
  107. # compilation_equipment_cost=dto.compilation_equipment_cost,
  108. # transport_cost=dto.transport_cost,
  109. # compilation_transport_cost=dto.compilation_transport_cost,
  110. # quota_workday=dto.quota_workday,
  111. # total_workday=dto.total_workday,
  112. # workday_salary=dto.workday_salary,
  113. # compilation_workday_salary=dto.compilation_workday_salary,
  114. # quota_mechanical_workday=dto.quota_mechanical_workday,
  115. # total_mechanical_workday=dto.total_mechanical_workday,
  116. # mechanical_workday_salary=dto.mechanical_workday_salary,
  117. # compilation_mechanical_workday_salary=dto.compilation_mechanical_workday_salary,
  118. compiler=dto.compiler,
  119. modify_date=datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
  120. # quota_consumption=dto.quota_consumption,0
  121. # basic_quota=dto.basic_quota,
  122. # quota_comprehensive_unit_price=dto.quota_comprehensive_unit_price,
  123. # quota_comprehensive_total_price=dto.quota_comprehensive_total_price
  124. )
  125. db_session.add(model)
  126. db_session.flush()
  127. return QuotaInputDto.from_model(model)
  128. def update_quota(
  129. self, project_id: str, dto: QuotaInputDto
  130. ) -> Optional[QuotaInputDto]:
  131. """更新定额输入
  132. Args:
  133. project_id:
  134. dto: 定额输入DTO
  135. Returns:
  136. QuotaInputDto or None
  137. """
  138. self._database = project_id
  139. with db_helper.sqlserver_session(self._database) as db_session:
  140. model = (
  141. db_session.query(QuotaInputModel)
  142. .filter(QuotaInputModel.quota_id == dto.quota_id)
  143. .first()
  144. )
  145. if model is None:
  146. return None
  147. model.budget_id = dto.budget_id
  148. model.item_id = dto.item_id
  149. model.quota_code = dto.quota_code
  150. # model.sequence_number = dto.sequence_number
  151. model.project_name = dto.project_name
  152. model.unit = dto.unit
  153. model.project_quantity = dto.project_quantity
  154. # model.project_quantity_input = dto.project_quantity_input
  155. # model.quota_adjustment = dto.quota_adjustment
  156. # model.unit_price = dto.unit_price
  157. # model.compilation_unit_price = dto.compilation_unit_price
  158. # model.total_price = dto.total_price
  159. # model.compilation_total_price = dto.compilation_total_price
  160. # model.unit_weight = dto.unit_weight
  161. # model.total_weight = dto.total_weight
  162. # model.labor_cost = dto.labor_cost
  163. # model.compilation_labor_cost = dto.compilation_labor_cost
  164. # model.material_cost = dto.material_cost
  165. # model.compilation_material_cost = dto.compilation_material_cost
  166. # model.deduct_material_cost = dto.deduct_material_cost
  167. # model.compilation_deduct_material_cost = dto.compilation_deduct_material_cost
  168. # model.mechanical_cost = dto.mechanical_cost
  169. # model.compilation_mechanical_cost = dto.compilation_mechanical_cost
  170. # model.equipment_cost = dto.equipment_cost
  171. # model.compilation_equipment_cost = dto.compilation_equipment_cost
  172. # model.transport_cost = dto.transport_cost
  173. # model.compilation_transport_cost = dto.compilation_transport_cost
  174. # model.quota_workday = dto.quota_workday
  175. # model.total_workday = dto.total_workday
  176. # model.workday_salary = dto.workday_salary
  177. # model.compilation_workday_salary = dto.compilation_workday_salary
  178. # model.quota_mechanical_workday = dto.quota_mechanical_workday
  179. # model.total_mechanical_workday = dto.total_mechanical_workday
  180. # model.mechanical_workday_salary = dto.mechanical_workday_salary
  181. # model.compilation_mechanical_workday_salary = dto.compilation_mechanical_workday_salary
  182. # model.compiler = dto.compiler
  183. model.modify_date = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
  184. # model.quota_consumption = dto.quota_consumption
  185. # model.basic_quota = dto.basic_quota
  186. # model.quota_comprehensive_unit_price = dto.quota_comprehensive_unit_price
  187. # model.quota_comprehensive_total_price = dto.quota_comprehensive_total_price
  188. db_session.merge(model)
  189. return QuotaInputDto.from_model(model)
  190. def delete_quota(self, quota_id: int) -> bool:
  191. """删除定额输入
  192. Args:
  193. quota_id: 定额序号
  194. Returns:
  195. bool
  196. """
  197. with db_helper.sqlserver_session(self._database) as db_session:
  198. model = (
  199. db_session.query(QuotaInputModel)
  200. .filter(QuotaInputModel.quota_id == quota_id)
  201. .first()
  202. )
  203. if model is None:
  204. return False
  205. db_session.delete(model)
  206. return True