project_quota.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. from flask import Blueprint, request
  2. from core.dtos import ProjectQuotaDto
  3. from core.user_session import Permission
  4. from core.api import ResponseBase, TableResponse
  5. from services import ProjectQuotaService
  6. project_quota_api = Blueprint("project_quota_api", __name__)
  7. quota_service = ProjectQuotaService()
  8. @project_quota_api.route(
  9. "/list/<int:budget_id>/<project_id>/<item_code>", methods=["POST"]
  10. )
  11. @Permission.authorize
  12. def get_page_list(budget_id: int, project_id: str, item_code: str):
  13. try:
  14. data = request.get_json()
  15. page = int(data.get("pageNum", 1))
  16. per_page = int(data.get("pageSize", 10))
  17. keyword = data.get("keyword")
  18. send_status = int(data.get("send_status")) if data.get("send_status") else None
  19. data, count = quota_service.get_quotas_paginated(
  20. budget_id, project_id, item_code, page, per_page, keyword, send_status
  21. )
  22. return TableResponse.success(data, count)
  23. except Exception as e:
  24. return ResponseBase.error(f"获取定额条目列表失败:{str(e)}")
  25. @project_quota_api.route(
  26. "/list/task/<int:task_id>/<int:budget_id>/<project_id>/<item_code>",
  27. methods=["POST"],
  28. )
  29. @Permission.authorize
  30. def get_quotas_by_task_paginated(
  31. task_id: int, budget_id: int, project_id: str, item_code: str
  32. ):
  33. try:
  34. data = request.get_json()
  35. page = int(data.get("pageNum", 1))
  36. per_page = int(data.get("pageSize", 10))
  37. keyword = data.get("keyword")
  38. send_status = int(data.get("send_status")) if data.get("send_status") else None
  39. data, count = quota_service.get_quotas_by_task_paginated(
  40. task_id,
  41. budget_id,
  42. project_id,
  43. item_code,
  44. page,
  45. per_page,
  46. keyword,
  47. send_status,
  48. )
  49. return TableResponse.success(data, count)
  50. except Exception as e:
  51. return ResponseBase.error(f"获取任务定额条目列表失败:{str(e)}")
  52. @project_quota_api.route("/get/<int:quota_id>", methods=["POST"])
  53. @Permission.authorize
  54. def get_quota(quota_id: int):
  55. try:
  56. data = quota_service.get_quota_dto(quota_id)
  57. return ResponseBase.success(data.to_dict())
  58. except Exception as e:
  59. return ResponseBase.error(f"获取定额条目失败:{str(e)}")
  60. @project_quota_api.route("/save", methods=["POST"])
  61. @Permission.authorize
  62. def save_quota():
  63. try:
  64. data = request.get_json()
  65. quota_dto = ProjectQuotaDto(**data)
  66. quota_dto = quota_service.save_quota(quota_dto)
  67. run_now = data.get("run_now", "false") == "true"
  68. if run_now:
  69. is_cover = data.get("is_cover", "false") == "true"
  70. quota_service.start_send(quota_dto.id, is_cover)
  71. return ResponseBase.success(quota_dto.to_dict())
  72. except Exception as e:
  73. return ResponseBase.error(f"保存定额条目失败:{str(e)}")
  74. @project_quota_api.route("/edit", methods=["POST"])
  75. @Permission.authorize
  76. def edit_by_key():
  77. try:
  78. data = request.get_json()
  79. id = data.get("id")
  80. if not id:
  81. return ResponseBase.error(f"未能获取到定额条目ID")
  82. key = data.get("key")
  83. value = data.get("val")
  84. msg = quota_service.edit_by_key(id, key, value)
  85. if msg:
  86. return ResponseBase.error(msg)
  87. return ResponseBase.success()
  88. except Exception as e:
  89. return ResponseBase.error(f"编辑定额条目失败:{str(e)}")
  90. @project_quota_api.route("/edit_quota_code_batch", methods=["POST"])
  91. @Permission.authorize
  92. def edit_quota_code_batch():
  93. try:
  94. data = request.get_json()
  95. ids = data.get("ids", "")
  96. quota_code = data.get("quota_code", "")
  97. msg = quota_service.edit_quota_code_batch(ids, quota_code)
  98. if msg:
  99. return ResponseBase.error(msg)
  100. return ResponseBase.success()
  101. except Exception as e:
  102. return ResponseBase.error(f"批量修改定额编号失败:{str(e)}")
  103. @project_quota_api.route("/delete/<int:quota_id>", methods=["POST"])
  104. @Permission.authorize
  105. def delete_quota(quota_id: int):
  106. try:
  107. quota_service.delete_quota(quota_id)
  108. return ResponseBase.success()
  109. except Exception as e:
  110. return ResponseBase.error(f"删除定额条目失败:{str(e)}")
  111. @project_quota_api.route("/start_send", methods=["POST"])
  112. @Permission.authorize
  113. def start_send():
  114. try:
  115. data = request.get_json()
  116. ids = data.get("ids", "")
  117. is_cover = data.get("is_cover", "false") == "true"
  118. msg = quota_service.start_send_by_ids(ids, is_cover)
  119. if msg:
  120. return ResponseBase.error(msg)
  121. return ResponseBase.success()
  122. except Exception as e:
  123. return ResponseBase.error(f"启动定额条目失败:{str(e)}")
  124. @project_quota_api.route("/save_change_chapter/<project_id>", methods=["POST"])
  125. @Permission.authorize
  126. def save_change_chapter(project_id: str):
  127. try:
  128. data = request.get_json()
  129. ids = data.get("ids", "")
  130. new_id = data.get("new_id", "")
  131. msg = quota_service.save_change_chapter(project_id, ids, new_id)
  132. if msg:
  133. return ResponseBase.error(msg)
  134. return ResponseBase.success()
  135. except Exception as e:
  136. return ResponseBase.error(f"变更章节失败:{str(e)}")