updates.py 8.9 KB

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