updates.py 8.3 KB

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