views.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. from rest_framework import viewsets
  2. from utils.page import MyPageNumberPagination
  3. from utils.datasolve import sumOfList, transportation_calculate
  4. from utils.md5 import Md5
  5. from rest_framework.filters import OrderingFilter
  6. from django_filters.rest_framework import DjangoFilterBackend
  7. from rest_framework.response import Response
  8. from rest_framework.exceptions import APIException
  9. from django.utils import timezone
  10. from django.db import transaction
  11. import logging
  12. from rest_framework import status
  13. from .models import DeviceModel,LocationModel,LocationGroupModel,LocationContainerLink,LocationChangeLog,alloction_pre,base_location
  14. from bound.models import BoundBatchModel,BoundDetailModel,BoundListModel
  15. from .filter import DeviceFilter,LocationFilter,LocationContainerLinkFilter,LocationChangeLogFilter,LocationGroupFilter
  16. from .serializers import LocationListSerializer,LocationPostSerializer
  17. from .serializers import LocationGroupListSerializer,LocationGroupPostSerializer
  18. # 以后添加模块时,只需要在这里添加即可
  19. from rest_framework.permissions import AllowAny
  20. from container.models import ContainerListModel,ContainerDetailModel,ContainerOperationModel,TaskModel
  21. from django.db.models import Prefetch
  22. import copy
  23. import json
  24. from collections import defaultdict
  25. logger = logging.getLogger(__name__)
  26. # 库位分配
  27. # 入库规则函数
  28. # 逻辑根据批次下的托盘数目来找满足区间范围的库位,按照优先级排序,
  29. class locationViewSet(viewsets.ModelViewSet):
  30. """
  31. retrieve:
  32. Response a data list(get)
  33. list:
  34. Response a data list(all)
  35. create:
  36. Create a data line(post)
  37. delete:
  38. Delete a data line(delete)
  39. """
  40. # authentication_classes = [] # 禁用所有认证类
  41. # permission_classes = [AllowAny] # 允许任意访问
  42. pagination_class = MyPageNumberPagination
  43. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  44. ordering_fields = ['id', "create_time", "update_time", ]
  45. filter_class = LocationFilter
  46. def get_project(self):
  47. try:
  48. id = self.kwargs.get('pk')
  49. return id
  50. except:
  51. return None
  52. def get_queryset(self):
  53. id = self.get_project()
  54. # 预取激活的关联托盘(通过中间模型过滤)
  55. prefetch_containers = Prefetch(
  56. 'current_containers',
  57. queryset=ContainerListModel.objects.filter(
  58. locationcontainerlink__is_active=True # 确保中间模型字段名正确
  59. ).distinct(),
  60. to_attr='active_containers' # 将结果存储到模型的 active_containers 属性
  61. )
  62. if self.request.user:
  63. if id is None:
  64. return LocationModel.objects.prefetch_related(prefetch_containers).all()
  65. else:
  66. return LocationModel.objects.prefetch_related(prefetch_containers).filter(id=id)
  67. else:
  68. return LocationModel.objects.none()
  69. def get_serializer_class(self):
  70. if self.action == 'list':
  71. return LocationListSerializer
  72. elif self.action == 'update':
  73. return LocationPostSerializer
  74. elif self.action =='retrieve':
  75. return LocationListSerializer
  76. def update(self, request, *args, **kwargs):
  77. qs = self.get_object()
  78. data = self.request.data
  79. location_code = data.get('location_code')
  80. # 处理库位对象
  81. location_obj = LocationModel.objects.filter(location_code=location_code).first()
  82. if not location_obj:
  83. logger.info(f"库位 {location_code} 不存在")
  84. return Response(
  85. {'code': '400', 'message': '库位不存在', 'data': None},
  86. status=status.HTTP_400_BAD_REQUEST
  87. )
  88. else:
  89. data['id'] = location_obj.id
  90. logger.info(f"库位 {location_code} 已存在")
  91. serializer = self.get_serializer(qs, data=data)
  92. serializer.is_valid(raise_exception=True)
  93. serializer.save()
  94. headers = self.get_success_headers(serializer.data)
  95. self.handle_group_location_status(location_code,location_obj.location_group)
  96. return Response(serializer.data, status=200, headers=headers)
  97. def handle_group_location_status(self,location_code,location_group):
  98. """
  99. 处理库位组和库位的关联关系
  100. :param location_code: 库位编码
  101. :param location_group: 库位组编码
  102. :return:
  103. """
  104. # 1. 获取库位空闲状态的库位数目
  105. location_obj_number = LocationModel.objects.filter(
  106. location_group=location_group,
  107. status='available'
  108. ).all().count()
  109. # 2. 获取库位组对象
  110. logger.info(f"库位组 {location_group} 下的库位数目:{location_obj_number}")
  111. # 1. 获取库位和库位组的关联关系
  112. location_group_obj = LocationGroupModel.objects.filter(
  113. group_code=location_group
  114. ).first()
  115. if not location_group_obj:
  116. logger.info(f"库位组 {location_group} 不存在")
  117. return None
  118. else:
  119. if location_obj_number == 0:
  120. # 库位组库位已满,更新库位组状态为full
  121. location_group_obj.status = 'full'
  122. location_group_obj.save()
  123. elif location_obj_number < location_group_obj.max_capacity:
  124. location_group_obj.status = 'occupied'
  125. location_group_obj.save()
  126. else:
  127. location_group_obj.status = 'available'
  128. location_group_obj.save()
  129. class locationGroupViewSet(viewsets.ModelViewSet):
  130. """
  131. retrieve:
  132. Response a data list(get)
  133. list:
  134. Response a data list(all)
  135. create:
  136. Create a data line(post)
  137. delete:
  138. Delete a data line(delete)
  139. """
  140. # authentication_classes = [] # 禁用所有认证类
  141. # permission_classes = [AllowAny] # 允许任意访问
  142. pagination_class = MyPageNumberPagination
  143. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  144. ordering_fields = ['id', "create_time", "update_time", ]
  145. filter_class = LocationGroupFilter
  146. def get_project(self):
  147. try:
  148. id = self.kwargs.get('pk')
  149. return id
  150. except:
  151. return None
  152. def get_queryset(self):
  153. id = self.get_project()
  154. if self.request.user:
  155. if id is None:
  156. return LocationGroupModel.objects.filter()
  157. else:
  158. return LocationGroupModel.objects.filter(id=id)
  159. else:
  160. return LocationGroupModel.objects.none()
  161. def get_serializer_class(self):
  162. if self.action == 'list':
  163. return LocationGroupListSerializer
  164. elif self.action == 'update':
  165. return LocationGroupPostSerializer
  166. elif self.action =='retrieve':
  167. return LocationGroupListSerializer
  168. def update(self, request, *args, **kwargs):
  169. data = self.request.data
  170. order_month = str(timezone.now().strftime('%Y%m'))
  171. data['month'] = order_month
  172. group_code = data.get('group_code')
  173. # 处理库位组对象
  174. group_obj = LocationGroupModel.objects.filter(group_code=group_code).first()
  175. if group_obj:
  176. data['id'] = group_obj.id
  177. logger.info(f"库位组 {group_code} 已存在")
  178. else:
  179. logger.info(f"库位组 {group_code} 不存在,创建库位组对象")
  180. serializer_list = LocationGroupPostSerializer(data=data)
  181. serializer_list.is_valid(raise_exception=True)
  182. serializer_list.save()
  183. data['id'] = serializer_list.data.get('id')
  184. return Response(data, status=status.HTTP_201_CREATED)
  185. class LocationAllocation:
  186. # 入库规则函数
  187. # fun:get_pallet_count_by_batch: 根据托盘码查询批次下托盘总数
  188. # fun:get_left_locationGroup_number_by_type: 获取每层库位组剩余数量
  189. # fun:get_location_type: 根据托盘数目获取库位类型
  190. # fun:update_location_container_link: 更新库位和托盘的关联关系
  191. # fun:update_location_group_batch: 更新库位组的批次
  192. # fun:update_batch_status: 更新批次状态yes/no
  193. # fun:update_location_status: 更新库位状态和
  194. # fun:up
  195. # fun:get_batch_status: 获取批次状态
  196. # fun:get_batch: 获取批次
  197. # fun:get_location_list_remainder: 获取可用库位的c_number列表
  198. # fun
  199. # fun:get_location_by_type_remainder: 根据库位类型获取库位
  200. # fun:get_location_by_type: 第一次入库,根据库位类型获取库位
  201. # fun:get_location_by_status: 根据库位状态获取库位
  202. @transaction.atomic
  203. def get_pallet_count_by_batch(self, container_code):
  204. """
  205. 根据托盘码查询批次下托盘总数
  206. :param container_code: 要查询的托盘码
  207. :return: 所属批次下的托盘总数
  208. """
  209. # 1. 通过托盘码获取容器详情
  210. container = ContainerListModel.objects.filter(
  211. container_code=container_code
  212. ).first()
  213. if not container:
  214. logger.error(f"托盘 {container_code} 不存在")
  215. return None
  216. # 2. 获取关联的批次明细
  217. container_detail = ContainerDetailModel.objects.filter(
  218. container=container.id
  219. ).exclude(status = 3).first()
  220. if not container_detail:
  221. logger.error(f"托盘 {container_code} 未组盘")
  222. return None
  223. batch_container = ContainerDetailModel.objects.filter(
  224. batch = container_detail.batch.id,
  225. status = 1
  226. ).all()
  227. # 统计批次下的不同托盘 item.contianer_id
  228. batch_container_count = 0
  229. container_ids = []
  230. for item in batch_container:
  231. if item.container_id not in container_ids:
  232. batch_container_count = batch_container_count + 1
  233. container_ids.append(item.container_id)
  234. batch_item = BoundBatchModel.objects.filter( bound_number = container_detail.batch.bound_number).first()
  235. if not batch_item:
  236. print(f"批次号获取失败!")
  237. return None
  238. batch_item.container_number = batch_container_count
  239. batch_item.save()
  240. return batch_container_count
  241. def get_left_locationGroup_number_by_type(self):
  242. """
  243. 获取每层库位组剩余数量
  244. :return:
  245. """
  246. try:
  247. # 定义库位组和层号
  248. group = ['T1', 'T2', 'S4', 'T4', 'T5']
  249. layer = [1, 2, 3]
  250. # 初始化结果列表,包含三个空字典对应三个层
  251. left_number = [{} for _ in layer]
  252. for item in group:
  253. for idx, layer_num in enumerate(layer):
  254. # 检查库位组是否存在(不考虑状态)
  255. exists = LocationGroupModel.objects.filter(
  256. group_type=item,
  257. layer=layer_num
  258. ).exists()
  259. if not exists:
  260. print(f"库位组 {item}_{layer_num} 不存在")
  261. left_number[idx][item] = 0
  262. else:
  263. # 统计可用状态的库位组数量
  264. count = LocationGroupModel.objects.filter(
  265. group_type=item,
  266. layer=layer_num,
  267. status='available'
  268. ).count()
  269. left_number[idx][item] = count
  270. return left_number
  271. except Exception as e:
  272. logger.error(f"获取库位组剩余数量失败:{str(e)}")
  273. print(f"获取库位组剩余数量失败:{str(e)}")
  274. return None
  275. @transaction.atomic
  276. def update_location_container_link(self,location_code,container_code):
  277. """
  278. 更新库位和托盘的关联关系
  279. :param location_code: 库位编码
  280. :param container_code: 托盘编码
  281. :return:
  282. """
  283. try:
  284. # 1. 获取库位和托盘的关联关系
  285. location = LocationModel.objects.filter(
  286. location_code=location_code
  287. ).first()
  288. container = ContainerListModel.objects.filter(
  289. container_code=container_code
  290. ).first()
  291. # 2. 如果库位和托盘的关联关系不存在,创建新的关联关系
  292. if not LocationContainerLink.objects.filter(location=location).exists():
  293. location_container_link = LocationContainerLink(
  294. location=location,
  295. container=container
  296. )
  297. location_container_link.save()
  298. print(f"更新库位和托盘的关联关系成功!")
  299. return True
  300. # 3. 更新库位和托盘的关联关系
  301. else:
  302. LocationContainerLink.objects.filter(location=location).update(location=location, container=container)
  303. print(f"更新库位和托盘的关联关系成功!")
  304. return True
  305. except Exception as e:
  306. logger.error(f"更新库位和托盘的关联关系失败:{str(e)}")
  307. print(f"更新库位和托盘的关联关系失败:{str(e)}")
  308. return False
  309. def update_container_detail_status(self,container_code,status):
  310. try:
  311. # 1. 获取托盘
  312. container = ContainerListModel.objects.filter(
  313. container_code=container_code
  314. ).first()
  315. if not container:
  316. print(f"托盘 {container_code} 不存在")
  317. return False
  318. # 2. 更新托盘状态
  319. container_detail = ContainerDetailModel.objects.filter(
  320. container=container.id
  321. ).exclude(status=3).first()
  322. if not container_detail:
  323. print(f"托盘 {container_code} 未组盘_from update_container_detail_status")
  324. return False
  325. container_detail.status = status
  326. container_detail.save()
  327. print(f"更新托盘状态成功!")
  328. return True
  329. except Exception as e:
  330. logger.error(f"更新托盘状态失败:{str(e)}")
  331. print(f"更新托盘状态失败:{str(e)}")
  332. return False
  333. def update_location_group_batch(self,location,container_code):
  334. """
  335. :param location: 库位对象
  336. :param container_code: 托盘码
  337. :return:
  338. """
  339. try:
  340. # 1. 获取库位组
  341. location_group = LocationGroupModel.objects.filter(
  342. group_code=location.location_group
  343. ).first()
  344. if not location_group:
  345. print(f"库位组获取失败!")
  346. return False
  347. # 2. 更新库位组的批次
  348. bound_number=self.get_batch(container_code)
  349. if not bound_number:
  350. print(f"批次号获取失败!")
  351. return False
  352. location_group.current_batch = bound_number
  353. location_group.save()
  354. print(f"更新库位组的批次成功!")
  355. return True
  356. except Exception as e:
  357. logger.error(f"更新库位组的批次失败:{str(e)}")
  358. print(f"更新库位组的批次失败:{str(e)}")
  359. return False
  360. def update_location_status(self,location_code,status):
  361. """
  362. 更新库位状态
  363. :param location_code: 库位编码
  364. :param status: 库位状态
  365. :return:
  366. """
  367. try:
  368. # 1. 获取库位
  369. location = LocationModel.objects.filter(
  370. location_code=location_code
  371. ).first()
  372. if not location:
  373. print(f"库位获取失败!")
  374. return False
  375. # 2. 更新库位状态
  376. location.status = status
  377. location.save()
  378. print(f"更新库位状态成功!")
  379. return True
  380. except Exception as e:
  381. logger.error(f"更新库位状态失败:{str(e)}")
  382. print(f"更新库位状态失败:{str(e)}")
  383. return False
  384. def update_group_status_reserved(self,location_group_list):
  385. """
  386. 更新库位组状态
  387. :param location_group_list: 库位组对象列表
  388. :return:
  389. """
  390. try:
  391. for location_group in location_group_list:
  392. # 1. 获取库位组
  393. if not location_group:
  394. print(f"库位组获取失败!")
  395. return False
  396. # 2. 更新库位组状态
  397. location_group_id = location_group.split('_')[1]
  398. location_group_item = LocationGroupModel.objects.filter(
  399. id=location_group_id
  400. ).first()
  401. if not location_group_item:
  402. print(f"库位组 {location_group} 不存在")
  403. return False
  404. # 3. 更新库位组状态
  405. location_group_item.status = 'reserved'
  406. location_group_item.save()
  407. return True
  408. except Exception as e:
  409. logger.error(f"更新库位组状态失败:{str(e)}")
  410. print(f"更新库位组状态失败:{str(e)}")
  411. return False
  412. @transaction.atomic
  413. def update_location_group_status(self, location_code):
  414. """
  415. 更新库位组状态
  416. :param location_code: 库位编码
  417. :return:
  418. """
  419. try:
  420. # 1. 获取库位
  421. location = LocationModel.objects.filter(
  422. location_code=location_code
  423. ).first()
  424. if not location:
  425. print(f"库位获取失败!")
  426. return False
  427. # 2. 获取库位组
  428. location_group = LocationGroupModel.objects.filter(
  429. group_code=location.location_group
  430. ).first()
  431. if not location_group:
  432. print(f"库位组获取失败!")
  433. return False
  434. current=0
  435. for location_item in location_group.location_items.all():
  436. if location_item.status != 'available':
  437. current=current + 1
  438. # 3. 更新库位组状态
  439. if current == 0:
  440. location_group.status = 'available'
  441. elif current == location_group.max_capacity:
  442. location_group.status = 'full'
  443. else:
  444. location_group.status = 'occupied'
  445. location_group.current_goods_quantity = sum(
  446. [loc.current_quantity for loc in location_group.location_items.all()]
  447. )
  448. location_group.current_quantity = current
  449. location_group.save()
  450. print(f"更新库位组状态成功!")
  451. return True
  452. except Exception as e:
  453. logger.error(f"更新库位组状态失败:{str(e)}")
  454. print(f"更新库位组状态失败:{str(e)}")
  455. def update_batch_status(self,container_code,status):
  456. """
  457. 更新批次状态
  458. :param batch_id: 批次id
  459. :param status: 批次状态
  460. :return:
  461. """
  462. try:
  463. # 1. 通过托盘码获取托盘详情
  464. container = ContainerListModel.objects.filter(
  465. container_code=container_code
  466. ).first()
  467. if not container:
  468. logger.error(f"托盘 {container_code} 不存在")
  469. print(f"托盘 {container_code} 不存在")
  470. return None
  471. # 2. 获取关联的批次明细
  472. container_detail = ContainerDetailModel.objects.filter(
  473. container=container.id
  474. ).exclude(status=3).first()
  475. if not container_detail:
  476. print (f"托盘 {container_code} 未组盘")
  477. logger.error(f"托盘 {container_code} 未组盘_from update_batch_status")
  478. return None
  479. # 3. 更新批次状态
  480. batch = container_detail.batch
  481. batch.status = status
  482. batch.save()
  483. print(f"更新批次状态成功!")
  484. return True
  485. except Exception as e:
  486. logger.error(f"更新批次状态失败:{str(e)}")
  487. print(f"更新批次状态失败:{str(e)}")
  488. return False
  489. def get_batch_status(self,container_code):
  490. """
  491. 获取批次状态
  492. :param container_code: 托盘码
  493. :return: 批次状态
  494. """
  495. # 1. 通过托盘码获取容器详情
  496. container = ContainerListModel.objects.filter(
  497. container_code=container_code
  498. ).first()
  499. if not container:
  500. logger.error(f"托盘 {container_code} 不存在")
  501. print(f"托盘 {container_code} 不存在")
  502. return None
  503. # 2. 获取关联的批次明细
  504. container_detail = ContainerDetailModel.objects.filter(
  505. container=container.id
  506. ).exclude(status=3).first()
  507. if not container_detail:
  508. print (f"托盘 {container_code} 未组盘")
  509. logger.error(f"托盘 {container_code} 未组盘_from get_batch_status")
  510. return None
  511. batch_status = container_detail.batch.status
  512. return batch_status
  513. def get_batch(self,container_code):
  514. """
  515. 获取批次
  516. :param container_code: 托盘码
  517. :return: 批次
  518. """
  519. # 1. 通过托盘码获取容器详情
  520. container = ContainerListModel.objects.filter(
  521. container_code=container_code
  522. ).first()
  523. if not container:
  524. logger.error(f"托盘 {container_code} 不存在")
  525. print(f"托盘 {container_code} 不存在")
  526. return None
  527. # 2. 获取关联的批次明细
  528. container_detail = ContainerDetailModel.objects.filter(
  529. container=container.id
  530. ).exclude(status=3).first()
  531. if not container_detail:
  532. print (f"托盘 {container_code} 未组盘")
  533. logger.error(f"托盘 {container_code} 未组盘_from get_batch")
  534. return None
  535. batch = container_detail.batch.bound_number
  536. return batch
  537. @transaction.atomic
  538. def get_location_list_remainder(self, location_group_list,container_code):
  539. """
  540. 获取可用库位的c_number列表
  541. :param location_list: 库位对象列表
  542. :return: 可用库位编号列表
  543. """
  544. if not location_group_list:
  545. return None
  546. min_c_number=1000
  547. min_c_number_index=1000
  548. current_task = self.get_current_finish_task(container_code)
  549. print(f"[1]当前已完成任务: {current_task}")
  550. # 按压力排序
  551. sorted_pressure = sorted(
  552. [(0, current_task[0]), (1, current_task[1]), (2, current_task[2])],
  553. key=lambda x: (-x[1], x[0])
  554. )
  555. # 交换第一和第二个元素的位置
  556. sorted_pressure[0], sorted_pressure[1] = sorted_pressure[1], sorted_pressure[0]
  557. print(f"[2]任务排序: {sorted_pressure}")
  558. print(f"[3]当前选择:{sorted_pressure[0][0]+1}")
  559. location_type_dict = json.loads(self.divide_solution_by_layer(location_group_list))
  560. # print(f"库位类型分配方案: {location_type_dict}")
  561. for layer, _ in sorted_pressure:
  562. if not location_type_dict.get(str(layer+1)):
  563. continue
  564. # print(f"当前层: {layer+1}")
  565. # print(f"当前层库位组: {location_type_dict[str(layer+1)].keys()}")
  566. for group_id in location_type_dict[str(layer+1)].keys():
  567. location_group = LocationGroupModel.objects.filter(
  568. id=group_id,
  569. ).first()
  570. if not location_group:
  571. continue
  572. location_list = location_group.location_items.filter(
  573. status='available'
  574. ).all().order_by('c_number')
  575. if not location_list:
  576. print(f"当前层库位组 {location_group.group_code} 可用库位: None")
  577. continue
  578. # 提取所有库位的 c_number
  579. c_numbers = [loc.c_number for loc in location_list]
  580. print(f"当前层库位组 {location_group.group_code} 可用库位: {c_numbers}")
  581. # 更新任务完成数目
  582. current_task[layer] = current_task[layer] + 1
  583. self.update_current_finish_task(container_code,current_task)
  584. return location_list[0]
  585. @transaction.atomic
  586. def get_location_type(self, container_code):
  587. """
  588. 智能库位分配核心算法
  589. :param container_code: 托盘码
  590. :return: 库位类型分配方案
  591. """
  592. try:
  593. batch = self.get_batch(container_code)
  594. if not batch:
  595. logger.error("批次信息获取失败")
  596. return None
  597. # 检查已有分配方案
  598. existing_solution = alloction_pre.objects.filter(batch_number=batch).first()
  599. if existing_solution:
  600. return existing_solution.layer_pre_type
  601. # 获取关键参数
  602. total_pallets = self.get_pallet_count_by_batch(container_code)
  603. layer_capacity = self.get_left_locationGroup_number_by_type()
  604. current_pressure = self.get_current_pressure()
  605. # 测试参数
  606. # total_pallets = 30
  607. # layer_capacity = [{'T1': 29, 'T2': 14, 'S4': 10, 'T4': 27, 'T5': 27}, {'T1': 0, 'T2': 0, 'S4': 0, 'T4': 0, 'T5': 21}, {'T1': 29, 'T2': 14, 'S4': 10, 'T4': 27, 'T5': 27}]
  608. # current_pressure = [1,0,0]
  609. print(f"[1]托盘数目: {total_pallets}")
  610. print(f"[2]层容量: {layer_capacity}")
  611. # print(f"[3]当前压力: {current_pressure}")
  612. # 定义库位容量表
  613. LOCATION_CAPACITY = {'T1':1, 'T2':2, 'T4':4, 'S4':4, 'T5':5}
  614. def allocate(remain, path, pressure,real_pressure,layer_capacity_state, depth=0):
  615. # 终止条件
  616. if remain <= 0:
  617. return [path,real_pressure]
  618. # 深拷贝当前层容量状态
  619. new_layer_capacity = copy.deepcopy(layer_capacity_state)
  620. # print(f"[2]当前剩余: {new_layer_capacity}")
  621. # 压力平衡系数
  622. balance_factor = 1.0 - (0.1 * min(depth, 5))
  623. # 层选择策略
  624. print (f"[3]当前压力: {pressure}")
  625. layer_priority = sorted(
  626. [(0, pressure[0]), (1, pressure[1]), (2, pressure[2])],
  627. key=lambda x: (x[1] * balance_factor, x[0])
  628. )
  629. for layer, _ in layer_priority:
  630. # 生成候选库位类型(按效率和容量排序)
  631. # 排序键函数 :
  632. # min(x[1], remain) 计算当前库位类型的容量 c 和剩余数量 remain 中的较小值。
  633. # -min(x[1], remain) 和 -x[1] 都使用了负号,这意味着排序是按降序进行的。
  634. # 首先按 -min(x[1], remain) 排序,即优先选择容量与剩余数量更接近的库位类型。
  635. # 如果有多个库位类型的容量与剩余数量相同,则按 -x[1] 排序,即优先选择容量更大的库位类型。
  636. print(f"[4]当前层: {layer+1}, 剩余: {remain}, 容量状态: {new_layer_capacity[layer]}")
  637. candidates = sorted(
  638. [(t, c) for t, c in LOCATION_CAPACITY.items()
  639. if new_layer_capacity[layer].get(t,0) > 0],
  640. key=lambda x: (abs(x[1]-remain), -x[1])
  641. )
  642. print(f"[4]候选库位类型: {candidates}")
  643. for loc_type, cap in candidates:
  644. # 更新容量状态
  645. updated_capacity = copy.deepcopy(new_layer_capacity)
  646. updated_capacity[layer][loc_type] -= 1 # 占用一个库位组
  647. # 允许适度空间浪费(当剩余<2时)
  648. # effective_cap = min(cap, remain) if (cap - remain) < 2 else cap
  649. effective_cap = min(cap, remain)
  650. if effective_cap <= remain:
  651. new_remain = remain - effective_cap
  652. new_pressure = pressure.copy()
  653. for i in range(0, 3):
  654. new_pressure[i] -=1 if new_pressure[i] > 0 else 0
  655. new_pressure[layer] += effective_cap # 按实际存放数计算压力,此时别的楼层压力可能降下来了
  656. real_pressure[layer] += effective_cap # 实际压力
  657. result = allocate(
  658. new_remain,
  659. path + [f"{layer+1}_{loc_type}"],
  660. new_pressure,
  661. real_pressure,
  662. updated_capacity,
  663. depth + 1
  664. )
  665. if result:
  666. print (f"[5]分配方案: {result}")
  667. return result
  668. return None
  669. # 执行分配
  670. allocation = allocate(total_pallets, [], [current_pressure[0], current_pressure[1],current_pressure[2]],[current_pressure[0], current_pressure[1],current_pressure[2]], layer_capacity)
  671. if not allocation:
  672. logger.error("无法生成有效分配方案")
  673. return None
  674. # 保存分配方案
  675. allocation_json = self.divide_solution_by_layer(allocation[0])
  676. print(f"[6]分配方案: {allocation_json}")
  677. solution = alloction_pre(
  678. batch_number=batch,
  679. layer_pre_type =allocation_json
  680. )
  681. solution_pressure, created = base_location.objects.get_or_create(
  682. id=1,
  683. defaults={
  684. 'layer1_pressure': 0,
  685. 'layer2_pressure': 0,
  686. 'layer3_pressure': 0
  687. }
  688. )
  689. solution_pressure.layer1_pressure = allocation[1][0]
  690. solution_pressure.layer2_pressure = allocation[1][1]
  691. solution_pressure.layer3_pressure = allocation[1][2]
  692. solution.save()
  693. solution_pressure.save()
  694. return [loc.split('_')[1] for loc in allocation[0]]
  695. except Exception as e:
  696. logger.error(f"分配算法异常:{str(e)}")
  697. return None
  698. def divide_solution_by_layer(self, data):
  699. # 统计所有存在的层级
  700. layer_counts = defaultdict(lambda: defaultdict(int))
  701. existing_layers = set()
  702. for item in data:
  703. # 分割层级和类型
  704. try:
  705. layer, loc_type = item.split('_')
  706. layer_num = int(layer)
  707. existing_layers.add(layer_num)
  708. layer_counts[layer_num][loc_type] += 1
  709. except (ValueError, IndexError):
  710. continue # 跳过无效格式的数据
  711. # 确定最大层级(至少包含1层)
  712. max_layer = max(existing_layers) if existing_layers else 1
  713. # 构建包含所有层级的最终结果
  714. final_result = {}
  715. for layer in range(1, max_layer + 1):
  716. final_result[str(layer)] = dict(layer_counts.get(layer, {}))
  717. return json.dumps(final_result, indent=2)
  718. def get_current_pressure(self):
  719. """获取实时工作压力"""
  720. last_solution = base_location.objects.order_by('-id').first()
  721. if not last_solution:
  722. base_location.objects.create(
  723. layer1_pressure=0,
  724. layer2_pressure=0,
  725. layer3_pressure=0,
  726. ).save()
  727. return [
  728. last_solution.layer1_pressure if last_solution else 0,
  729. last_solution.layer2_pressure if last_solution else 0,
  730. last_solution.layer3_pressure if last_solution else 0,
  731. ]
  732. def get_current_finish_task(self,container):
  733. batch = self.get_batch(container)
  734. if not batch:
  735. return None
  736. solution = alloction_pre.objects.filter(batch_number=batch).first()
  737. if not solution:
  738. return None
  739. return [solution.layer1_task_finish_number,solution.layer2_task_finish_number,solution.layer3_task_finish_number]
  740. def update_current_finish_task(self,container,task_finish_number):
  741. batch = self.get_batch(container)
  742. if not batch:
  743. return None
  744. solution = alloction_pre.objects.filter(batch_number=batch).first()
  745. if not solution:
  746. return None
  747. solution.layer1_task_finish_number = task_finish_number[0]
  748. solution.layer2_task_finish_number = task_finish_number[1]
  749. solution.layer3_task_finish_number = task_finish_number[2]
  750. solution.save()
  751. return True
  752. @transaction.atomic
  753. def get_location_by_type(self, location_type_list, start_location, container_code):
  754. """
  755. 根据库位类型获取库位,先根据工作压力找出最空闲的层,看下这层有没有工作,如果有,就从里面找,如果没有,则跳过该层,继续找下一层
  756. :param location_type_list: 库位分配方案
  757. {
  758. "1": {
  759. "S4": 1
  760. },
  761. "2": {},
  762. "3": {
  763. "T5": 1
  764. }
  765. }
  766. :param start_location: 起始位置(决定优先级排序方式)
  767. :return: 符合条件的库位列表
  768. """
  769. locations = []
  770. # 检查已有分配方案
  771. existing_solution = alloction_pre.objects.filter(batch_number=self.get_batch(container_code)).first()
  772. if existing_solution.layer_solution_type:
  773. print(f"[0]已有库位分配方案:{existing_solution.layer_solution_type}")
  774. return existing_solution.layer_solution_type
  775. for layer, location_type_dict in location_type_list.items():
  776. if not location_type_dict:
  777. continue
  778. # 获取库位类型列表
  779. location_type = list(location_type_dict.keys())
  780. demand_number = sum(location_type_dict.values())
  781. print (f"[1]层{layer} 需求数量: {demand_number}, 库位: {location_type}")
  782. location_groups = LocationGroupModel.objects.filter(
  783. group_type__in=location_type,
  784. layer=layer,
  785. status='available'
  786. )
  787. if not location_groups:
  788. print(f"层{layer} 无库位")
  789. # 根据起始位置选择排序字段
  790. if start_location == '203':
  791. ordered_groups = location_groups.order_by('left_priority')
  792. elif start_location == '103':
  793. ordered_groups = location_groups.order_by('right_priority')
  794. else:
  795. ordered_groups = location_groups.none()
  796. number = 0
  797. for location_group in ordered_groups:
  798. if number >= demand_number:
  799. break
  800. locations.append(f"{layer}_{location_group.id}")
  801. number += 1
  802. existing_solution.layer_solution_type = locations
  803. existing_solution.save()
  804. print(f"[2]分配方案: {locations}")
  805. return locations if locations else None
  806. @transaction.atomic
  807. def get_location_by_status(self,container_code,start_location):
  808. """
  809. 根据库位状态获取库位
  810. :param location_type: 库位类型
  811. :param start_location: 起始库位 if in1 优先考虑left_priority, if in2 优先考虑right_priority 就是获取库位组列表之后进行排序
  812. :return: 库位列表
  813. """
  814. # 1. 获取批次状态 1 为已组盘 2 为部分入库 3 为全部入库
  815. status = self.get_batch_status(container_code)
  816. #
  817. if status == 1:
  818. # 2. 获取库位组
  819. print(f"[1]第一次入库")
  820. # 重新获取最新数据
  821. self.get_location_type(container_code)
  822. location_type_list = json.loads(alloction_pre.objects.filter(batch_number=self.get_batch(container_code)).first().layer_pre_type)
  823. location_list = self.get_location_by_type(location_type_list,start_location,container_code)
  824. # 预定这些库组
  825. self.update_group_status_reserved(location_list)
  826. location_min_value = self.get_location_list_remainder(location_list,container_code)
  827. print(f"库位安排到第{location_min_value.c_number}个库位:{location_min_value}")
  828. # if not location_list[location_min_index]:
  829. # # 库位已满,返回None
  830. # return None
  831. # else:
  832. # return location_list[location_min_index]
  833. return location_min_value
  834. elif status == 2:
  835. # 3. 获取部分入库库位
  836. print (f"部分入库")
  837. location_list = alloction_pre.objects.filter(batch_number=self.get_batch(container_code)).first().layer_solution_type
  838. location_min_value = self.get_location_list_remainder(location_list,container_code)
  839. print(f"库位安排到第{location_min_value.c_number}个库位:{location_min_value}")
  840. return location_min_value
  841. def release_location(self, location_code):
  842. """释放库位并更新关联数据"""
  843. try:
  844. location = LocationModel.objects.get(location_code=location_code)
  845. links = LocationContainerLink.objects.get(location=location, is_active=True)
  846. print(f"释放库位: {location_code}, 关联容器: {links.container_id}")
  847. # 解除关联并标记为非活跃
  848. links.is_active = False
  849. links.save()
  850. return True
  851. except Exception as e:
  852. logger.error(f"释放库位失败: {str(e)}")
  853. return False