updates.py 9.2 KB

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