updates.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. from django.db import transaction
  2. from .models import *
  3. from container.models import *
  4. import logging
  5. logger = logging.getLogger(__name__)
  6. class LocationUpdates:
  7. """
  8. 库位相关更新
  9. functions:
  10. update_location_status(location_code, status): 更新库位状态
  11. link_container(location_code, container_code): 关联托盘
  12. update_batch_status(container_code, status): 更新批次状态
  13. save_allocation_plan(batch_number, solution, pressure): 保存分配方案
  14. """
  15. @transaction.atomic
  16. def link_container(location_code, container_code):
  17. try:
  18. location = LocationModel.objects.select_for_update().get(
  19. location_code=location_code
  20. )
  21. container = ContainerListModel.objects.get(
  22. container_code=container_code
  23. )
  24. link, created = LocationContainerLink.objects.update_or_create(
  25. location=location,
  26. defaults={'container': container, 'is_active': True}
  27. )
  28. return True
  29. except Exception as e:
  30. raise RuntimeError(f"关联更新失败: {str(e)}")
  31. @transaction.atomic
  32. def update_batch_status(container_code, status):
  33. try:
  34. container = ContainerListModel.objects.get(
  35. container_code=container_code
  36. )
  37. detail = ContainerDetailModel.objects.select_related('batch').filter(
  38. container=container.id,
  39. is_delete=False,
  40. status__in=[1, 2]
  41. ).all()
  42. if not detail:
  43. print(f"托盘 {container_code} 未组盘_from update_batch_status")
  44. for item in detail:
  45. item.batch.status = status
  46. item.batch.save()
  47. return True
  48. except Exception as e:
  49. raise RuntimeError(f"批次状态更新失败: {str(e)}")
  50. @transaction.atomic
  51. def save_allocation_plan(batch_number, solution, pressure):
  52. with transaction.atomic():
  53. base_location.objects.update_or_create(
  54. id=1,
  55. defaults={
  56. 'layer1_pressure': pressure[0],
  57. 'layer2_pressure': pressure[1],
  58. 'layer3_pressure': pressure[2]
  59. }
  60. )
  61. alloction_pre.objects.update_or_create(
  62. batch_number=batch_number,
  63. defaults={'layer_pre_type': solution}
  64. )
  65. @staticmethod
  66. def update_pallet_count(batch_container_count,bound_number):
  67. batch_item = BoundBatchModel.objects.filter(bound_number = bound_number).first()
  68. if not batch_item:
  69. print(f"批次号获取失败!")
  70. return None
  71. batch_item.container_number = batch_container_count
  72. batch_item.save()
  73. return True
  74. @staticmethod
  75. def update_group_status_reserved(location_group_list):
  76. """
  77. 更新库位组状态
  78. :param location_group_list: 库位组对象列表
  79. :return:
  80. """
  81. try:
  82. for location_group in location_group_list:
  83. # 1. 获取库位组
  84. if not location_group:
  85. print(f"库位组获取失败!")
  86. return False
  87. # 2. 更新库位组状态
  88. location_group_id = location_group.split('_')[1]
  89. location_group_item = LocationGroupModel.objects.filter(
  90. id=location_group_id
  91. ).first()
  92. if not location_group_item:
  93. print(f"库位组 {location_group} 不存在")
  94. return False
  95. # 3. 更新库位组状态
  96. location_group_item.status = 'reserved'
  97. location_group_item.save()
  98. return True
  99. except Exception as e:
  100. logger.error(f"更新库位组状态失败:{str(e)}")
  101. print(f"更新库位组状态失败:{str(e)}")
  102. return False
  103. def update_current_finish_task(container,task_finish_number):
  104. """
  105. 更新当前完成任务数
  106. :param container: 托盘号
  107. :param task_finish_number: 完成任务数
  108. :return: Boolean
  109. """
  110. from .queries import LocationQueries
  111. batch = LocationQueries.get_batch_info(container).get('number')
  112. if not batch:
  113. return None
  114. solution = alloction_pre.objects.filter(batch_number=batch).first()
  115. if not solution:
  116. return None
  117. solution.layer1_task_finish_number = task_finish_number[0]
  118. solution.layer2_task_finish_number = task_finish_number[1]
  119. solution.layer3_task_finish_number = task_finish_number[2]
  120. solution.save()
  121. return True
  122. @staticmethod
  123. @transaction.atomic
  124. def update_location_group_status(location_code):
  125. """
  126. 更新库位组状态
  127. :param location_code: 库位编码
  128. :return:
  129. """
  130. try:
  131. # 1. 获取库位
  132. location = LocationModel.objects.filter(
  133. location_code=location_code
  134. ).first()
  135. if not location:
  136. print(f"库位获取失败!")
  137. return False
  138. # 2. 获取库位组
  139. location_group = LocationGroupModel.objects.filter(
  140. group_code=location.location_group
  141. ).first()
  142. if not location_group:
  143. print(f"库位组获取失败!")
  144. return False
  145. current=0
  146. for location_item in location_group.location_items.all():
  147. if location_item.status != 'available':
  148. current=current + 1
  149. # 3. 更新库位组状态
  150. if current == 0:
  151. location_group.status = 'available'
  152. elif current == location_group.max_capacity:
  153. location_group.status = 'full'
  154. else:
  155. location_group.status = 'occupied'
  156. location_group.current_goods_quantity = sum(
  157. [loc.current_quantity for loc in location_group.location_items.all()]
  158. )
  159. location_group.current_quantity = current
  160. location_group.save()
  161. return True
  162. except Exception as e:
  163. logger.error(f"更新库位组状态失败:{str(e)}")
  164. print(f"更新库位组状态失败:{str(e)}")
  165. @staticmethod
  166. @transaction.atomic
  167. def update_location_group_batch(location,bound_number):
  168. """
  169. :param location: 库位对象
  170. :param
  171. :return:
  172. """
  173. try:
  174. # 1. 获取库位组
  175. location_group = LocationGroupModel.objects.filter(
  176. group_code=location.location_group
  177. ).first()
  178. if not location_group:
  179. print(f"库位组获取失败!")
  180. return False
  181. # 2. 更新库位组的批次
  182. location_group.current_batch = bound_number
  183. location_group.save()
  184. print(f"更新库位组的批次成功!")
  185. return True
  186. except Exception as e:
  187. logger.error(f"更新库位组的批次失败:{str(e)}")
  188. print(f"更新库位组的批次失败:{str(e)}")
  189. return False
  190. def update_container_detail_status(container_code,status):
  191. try:
  192. # 1. 获取托盘
  193. container = ContainerListModel.objects.filter(
  194. container_code=container_code
  195. ).first()
  196. if not container:
  197. print(f"托盘 {container_code} 不存在")
  198. return False
  199. # 2. 更新托盘状态
  200. container_detail = ContainerDetailModel.objects.filter(
  201. container=container.id,is_delete=False
  202. ).exclude(status=3).all()
  203. if not container_detail:
  204. print(f"托盘 {container_code} 未组盘_from update_container_detail_status")
  205. return True
  206. for detail in container_detail:
  207. detail.status = status
  208. detail.save()
  209. return True
  210. except Exception as e:
  211. logger.error(f"更新托盘状态失败:{str(e)}")
  212. print(f"更新托盘状态失败:{str(e)}")
  213. return False