views.py 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524
  1. from wsgiref import headers
  2. from rest_framework.viewsets import ViewSet
  3. from rest_framework import viewsets
  4. from utils.page import MyPageNumberPagination
  5. from django.db.models import Prefetch
  6. from rest_framework.filters import OrderingFilter
  7. from django_filters.rest_framework import DjangoFilterBackend
  8. from rest_framework.response import Response
  9. from django.db.models import F, Case, When
  10. from django.db.models import OuterRef, Subquery
  11. from django.utils import timezone
  12. import requests
  13. from django.db import transaction
  14. import logging
  15. from rest_framework import status
  16. from .models import ContainerListModel,ContainerDetailModel,ContainerOperationModel,ContainerWCSModel,TaskModel,out_batch_detail,ContainerDetailLogModel,batchLogModel
  17. from bound.models import BoundDetailModel,BoundListModel,OutBoundDetailModel
  18. from bin.views import LocationAllocation,base_location
  19. from bin.models import LocationModel,LocationContainerLink,LocationGroupModel
  20. from bound.models import BoundBatchModel,OutBatchModel,BatchLogModel
  21. from .serializers import ContainerDetailGetSerializer,ContainerDetailPostSerializer,ContainerDetailSimpleGetSerializer
  22. from .serializers import ContainerListGetSerializer,ContainerListPostSerializer
  23. from .serializers import ContainerOperationGetSerializer,ContainerOperationPostSerializer
  24. from .serializers import TaskGetSerializer,TaskPostSerializer
  25. from .serializers import WCSTaskGetSerializer
  26. from .serializers import OutBoundFullDetailSerializer,OutBoundDetailSerializer
  27. from .serializers import ContainerDetailLogSerializer
  28. from .serializers import batchLogModelSerializer
  29. from .filter import ContainerDetailFilter,ContainerListFilter,ContainerOperationFilter,TaskFilter,WCSTaskFilter,ContainerDetailLogFilter,batchLogFilter
  30. from rest_framework.permissions import AllowAny
  31. import threading
  32. from django.db import close_old_connections
  33. from bin.services import AllocationService
  34. from collections import defaultdict
  35. from django.db.models import Sum
  36. from staff.models import ListModel as StaffListModel
  37. logger = logging.getLogger(__name__)
  38. # 托盘流水汇总批次流水
  39. class batchLogModelViewSet(viewsets.ModelViewSet):
  40. """
  41. retrieve:
  42. Response a data list(get)
  43. list:
  44. Response a data list(all)
  45. create:
  46. Create a data line(post)
  47. delete:
  48. Delete a data line(delete)
  49. """
  50. pagination_class = MyPageNumberPagination
  51. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  52. ordering_fields = ['id', "create_time", "update_time", ]
  53. filter_class = batchLogFilter
  54. def get_project(self):
  55. try:
  56. id = self.kwargs.get('pk')
  57. return id
  58. except:
  59. return None
  60. def get_queryset(self):
  61. id = self.get_project()
  62. if self.request.user:
  63. if id is None:
  64. return batchLogModel.objects.filter()
  65. else:
  66. return batchLogModel.objects.filter(id=id)
  67. else:
  68. return batchLogModel.objects.none()
  69. def get_serializer_class(self):
  70. if self.action in ['list', 'destroy','retrieve']:
  71. return batchLogModelSerializer
  72. else:
  73. return self.http_method_not_allowed(request=self.request)
  74. def create(self, request, *args, **kwargs):
  75. data = self.request.data
  76. return Response(data, status=200, headers=headers)
  77. def update(self, request, pk):
  78. qs = self.get_object()
  79. data = self.request.data
  80. serializer = self.get_serializer(qs, data=data)
  81. serializer.is_valid(raise_exception=True)
  82. serializer.save()
  83. headers = self.get_success_headers(serializer.data)
  84. return Response(serializer.data, status=200, headers=headers)
  85. def get_container_operation_log(self,request):
  86. batchlog_id = self.request.query_params.get('batchlog_id')
  87. batch_obj = batchLogModel.objects.get(id=batchlog_id)
  88. container_operation_log = batch_obj.detail_logs.all()
  89. serializer = ContainerDetailLogSerializer(container_operation_log, many=True)
  90. return Response(serializer.data, status=200)
  91. # 进出库log查看
  92. class ContainerDetailLogModelViewSet(viewsets.ModelViewSet):
  93. """
  94. retrieve:
  95. Response a data list(get)
  96. list:
  97. Response a data list(all)
  98. create:
  99. Create a data line(post)
  100. delete:
  101. Delete a data line(delete)
  102. """
  103. # authentication_classes = [] # 禁用所有认证类
  104. # permission_classes = [AllowAny] # 允许任意访问
  105. pagination_class = MyPageNumberPagination
  106. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  107. ordering_fields = ['id', "create_time", "update_time", ]
  108. filter_class = ContainerDetailLogFilter
  109. def get_project(self):
  110. try:
  111. id = self.kwargs.get('pk')
  112. return id
  113. except:
  114. return None
  115. def get_queryset(self):
  116. id = self.get_project()
  117. if self.request.user:
  118. if id is None:
  119. return ContainerDetailLogModel.objects.filter()
  120. else:
  121. return ContainerDetailLogModel.objects.filter(id=id)
  122. else:
  123. return ContainerDetailLogModel.objects.none()
  124. def get_serializer_class(self):
  125. if self.action in ['list', 'destroy','retrieve']:
  126. return ContainerDetailLogSerializer
  127. else:
  128. return self.http_method_not_allowed(request=self.request)
  129. def create(self, request, *args, **kwargs):
  130. data = self.request.data
  131. return Response(data, status=200, headers=headers)
  132. def update(self, request, pk):
  133. qs = self.get_object()
  134. data = self.request.data
  135. serializer = self.get_serializer(qs, data=data)
  136. serializer.is_valid(raise_exception=True)
  137. serializer.save()
  138. headers = self.get_success_headers(serializer.data)
  139. return Response(serializer.data, status=200, headers=headers)
  140. # 托盘列表视图
  141. class ContainerListViewSet(viewsets.ModelViewSet):
  142. """
  143. retrieve:
  144. Response a data list(get)
  145. list:
  146. Response a data list(all)
  147. create:
  148. Create a data line(post)
  149. delete:
  150. Delete a data line(delete)
  151. """
  152. # authentication_classes = [] # 禁用所有认证类
  153. # permission_classes = [AllowAny] # 允许任意访问
  154. pagination_class = MyPageNumberPagination
  155. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  156. ordering_fields = ['id', "create_time", "update_time", ]
  157. filter_class = ContainerListFilter
  158. def get_project(self):
  159. try:
  160. id = self.kwargs.get('pk')
  161. return id
  162. except:
  163. return None
  164. def get_queryset(self):
  165. id = self.get_project()
  166. if self.request.user:
  167. if id is None:
  168. return ContainerListModel.objects.filter()
  169. else:
  170. return ContainerListModel.objects.filter(id=id)
  171. else:
  172. return ContainerListModel.objects.none()
  173. def get_serializer_class(self):
  174. if self.action in ['list', 'destroy','retrieve']:
  175. return ContainerListGetSerializer
  176. elif self.action in ['create', 'update']:
  177. return ContainerListPostSerializer
  178. else:
  179. return self.http_method_not_allowed(request=self.request)
  180. def create(self, request, *args, **kwargs):
  181. # 创建托盘:托盘码五位数字(唯一),当前库位,目标库位,状态,最后操作时间
  182. container_all = ContainerListModel.objects.all().order_by('container_code')
  183. if container_all.count() == 0:
  184. container_code = 12345
  185. else:
  186. container_code = container_all.last().container_code + 1
  187. container_obj = ContainerListModel.objects.create(
  188. container_code=container_code,
  189. current_location='N/A',
  190. target_location='N/A',
  191. status=0,
  192. last_operation=timezone.now()
  193. )
  194. serializer = ContainerListGetSerializer(container_obj)
  195. headers = self.get_success_headers(serializer.data)
  196. return Response(serializer.data, status=201, headers=headers)
  197. def update(self, request, pk):
  198. qs = self.get_object()
  199. data = self.request.data
  200. serializer = self.get_serializer(qs, data=data)
  201. serializer.is_valid(raise_exception=True)
  202. serializer.save()
  203. headers = self.get_success_headers(serializer.data)
  204. return Response(serializer.data, status=200, headers=headers)
  205. # wcs任务视图
  206. class WCSTaskViewSet(viewsets.ModelViewSet):
  207. """
  208. retrieve:
  209. Response a data list(get)
  210. list:
  211. Response a data list(all)
  212. create:
  213. Create a data line(post)
  214. delete:
  215. Delete a data line(delete)
  216. """
  217. # authentication_classes = [] # 禁用所有认证类
  218. # permission_classes = [AllowAny] # 允许任意访问
  219. pagination_class = MyPageNumberPagination
  220. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  221. ordering_fields = ['-id', "-create_time", "update_time", ]
  222. filter_class = WCSTaskFilter
  223. def get_project(self):
  224. try:
  225. id = self.kwargs.get('pk')
  226. return id
  227. except:
  228. return None
  229. def get_queryset(self):
  230. id = self.get_project()
  231. if self.request.user:
  232. if id is None:
  233. return ContainerWCSModel.objects.filter()
  234. else:
  235. return ContainerWCSModel.objects.filter(id=id)
  236. else:
  237. return ContainerWCSModel.objects.none()
  238. def get_serializer_class(self):
  239. if self.action in ['list', 'destroy','retrieve']:
  240. return WCSTaskGetSerializer
  241. else:
  242. return self.http_method_not_allowed(request=self.request)
  243. def send_task_to_wcs(self, request, *args, **kwargs):
  244. data = self.request.data
  245. task_id = data.get('taskid')
  246. logger.info(f"请求任务:{task_id}")
  247. data_return = {}
  248. try:
  249. task_obj = ContainerWCSModel.objects.get(id=task_id)
  250. if not task_obj:
  251. data_return = {
  252. 'code': '400',
  253. 'message': '任务不存在',
  254. 'data': data
  255. }
  256. return Response(data_return, status=status.HTTP_400_BAD_REQUEST)
  257. if task_obj.working == 1:
  258. OutboundService.send_task_to_wcs(task_obj)
  259. data_return = {
  260. 'code': '200',
  261. 'message': '任务已在执行中,再次下发',
  262. 'data': data
  263. }
  264. else:
  265. data_return = {
  266. 'code': '200',
  267. 'message': '任务已执行完成',
  268. 'data': data
  269. }
  270. except ContainerWCSModel.DoesNotExist:
  271. data_return = {
  272. 'code': '404',
  273. 'message': '任务不存在',
  274. 'data': data
  275. }
  276. return Response(data_return, status=status.HTTP_200_OK)
  277. # 入库任务视图
  278. class TaskViewSet(viewsets.ModelViewSet):
  279. """
  280. retrieve:
  281. Response a data list(get)
  282. list:
  283. Response a data list(all)
  284. create:
  285. Create a data line(post)
  286. delete:
  287. Delete a data line(delete)
  288. """
  289. pagination_class = MyPageNumberPagination
  290. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  291. ordering_fields = ['id', "create_time", "update_time", ]
  292. filter_class = TaskFilter
  293. def get_project(self):
  294. try:
  295. id = self.kwargs.get('pk')
  296. return id
  297. except:
  298. return None
  299. def get_queryset(self):
  300. id = self.get_project()
  301. if self.request.user:
  302. if id is None:
  303. return TaskModel.objects.filter()
  304. else:
  305. return TaskModel.objects.filter(id=id)
  306. else:
  307. return TaskModel.objects.none()
  308. def get_serializer_class(self):
  309. if self.action in ['list', 'destroy','retrieve']:
  310. return TaskGetSerializer
  311. elif self.action in ['create', 'update']:
  312. return TaskPostSerializer
  313. else:
  314. return self.http_method_not_allowed(request=self.request)
  315. def create(self, request, *args, **kwargs):
  316. data = self.request.data
  317. return Response(data, status=200, headers=headers)
  318. def update(self, request, pk):
  319. qs = self.get_object()
  320. data = self.request.data
  321. serializer = self.get_serializer(qs, data=data)
  322. serializer.is_valid(raise_exception=True)
  323. serializer.save()
  324. headers = self.get_success_headers(serializer.data)
  325. return Response(serializer.data, status=200, headers=headers)
  326. # 任务回滚
  327. class TaskRollbackMixin:
  328. @transaction.atomic
  329. def rollback_task(self, request, task_id, *args, **kwargs):
  330. """
  331. 撤销入库任务并回滚相关状态
  332. """
  333. try:
  334. # 获取任务实例并锁定数据库记录
  335. task = ContainerWCSModel.objects.select_for_update().get(taskid=task_id)
  336. container_code = task.container
  337. target_location = task.target_location
  338. batch = task.batch
  339. # 初始化库位分配器
  340. allocator = LocationAllocation()
  341. # ==================== 库位状态回滚 ====================
  342. # 解析目标库位信息(格式:仓库代码-行-列-层)
  343. try:
  344. warehouse_code, row, col, layer = target_location.split('-')
  345. location = LocationModel.objects.get(
  346. warehouse_code=warehouse_code,
  347. row=int(row),
  348. col=int(col),
  349. layer=int(layer)
  350. )
  351. # 回滚库位状态到可用状态
  352. allocator.update_location_status(location.location_code, 'available')
  353. # 更新库位组状态(需要根据实际逻辑实现)
  354. allocator.update_location_group_status(location.location_code)
  355. # 解除库位与托盘的关联
  356. allocator.update_location_container_link(location.location_code, None)
  357. # 清除库位组的批次关联
  358. allocator.update_location_group_batch(location, None)
  359. except (ValueError, LocationModel.DoesNotExist) as e:
  360. logger.error(f"库位解析失败: {str(e)}")
  361. raise Exception("关联库位信息无效")
  362. # ==================== 批次状态回滚 ====================
  363. if batch:
  364. # 将批次状态恢复为未处理状态(假设原状态为1)
  365. allocator.update_batch_status(batch.bound_number, '1')
  366. # ==================== 托盘状态回滚 ====================
  367. container_obj = ContainerListModel.objects.get(container_code=container_code)
  368. # 恢复托盘详细状态为初始状态(假设原状态为1)
  369. allocator.update_container_detail_status(container_code, 1)
  370. # 恢复托盘的目标位置为当前所在位置
  371. container_obj.target_location = task.current_location
  372. container_obj.save()
  373. # ==================== 删除任务记录 ====================
  374. task.delete()
  375. # ==================== 其他关联清理 ====================
  376. # 如果有其他关联数据(如inport_update_task的操作),在此处添加清理逻辑
  377. return Response(
  378. {'code': '200', 'message': '任务回滚成功', 'data': None},
  379. status=status.HTTP_200_OK
  380. )
  381. except ContainerWCSModel.DoesNotExist:
  382. logger.warning(f"任务不存在: {task_id}")
  383. return Response(
  384. {'code': '404', 'message': '任务不存在', 'data': None},
  385. status=status.HTTP_404_NOT_FOUND
  386. )
  387. except Exception as e:
  388. logger.error(f"任务回滚失败: {str(e)}", exc_info=True)
  389. return Response(
  390. {'code': '500', 'message': '服务器内部错误', 'data': None},
  391. status=status.HTTP_500_INTERNAL_SERVER_ERROR
  392. )
  393. # 入库任务下发
  394. class ContainerWCSViewSet(viewsets.ModelViewSet):
  395. """
  396. retrieve:
  397. Response a data list(get)
  398. list:
  399. Response a data list(all)
  400. create:
  401. Create a data line(post)
  402. delete:
  403. Delete a data line(delete)
  404. """
  405. authentication_classes = [] # 禁用所有认证类
  406. permission_classes = [AllowAny] # 允许任意访问
  407. def generate_move_task(self, request, *args, **kwargs):
  408. data = self.request.data
  409. container = data.get('container_code')
  410. start_location = data.get('start_location')
  411. target_location = data.get('target_location')
  412. logger.info(f"请求托盘:{container},起始位置:{start_location},目标位置:{target_location}")
  413. data_return = {}
  414. try:
  415. container_obj = ContainerListModel.objects.filter(container_code=container).first()
  416. if not container_obj:
  417. data_return = {
  418. 'code': '400',
  419. 'message': '托盘编码不存在',
  420. 'data': data
  421. }
  422. return Response(data_return, status=status.HTTP_400_BAD_REQUEST)
  423. # 检查是否已在目标位置
  424. if target_location == str(container_obj.target_location) and target_location!= '203' and target_location!= '103':
  425. logger.info(f"托盘 {container} 已在目标位置")
  426. data_return = {
  427. 'code': '200',
  428. 'message': '当前位置已是目标位置',
  429. 'data': data
  430. }
  431. else:
  432. # 生成任务
  433. current_task = ContainerWCSModel.objects.filter(
  434. container=container,
  435. tasktype='inbound',
  436. working = 1,
  437. ).exclude(status=300).first()
  438. if current_task:
  439. data_return = {
  440. 'code': '200',
  441. 'message': '任务已存在,重新下发',
  442. 'data': current_task.to_dict()
  443. }
  444. else:
  445. # todo: 这里的入库操作记录里面的记录的数量不对
  446. location_min_value,allocation_target_location, batch_info = AllocationService._move_allocation(start_location, target_location,container)
  447. batch_id = batch_info['number']
  448. if batch_info['class'] == 2:
  449. self.generate_task_no_batch(container, start_location, allocation_target_location,batch_id,location_min_value.c_number)
  450. self.generate_container_operate_no_batch(container_obj, batch_id, allocation_target_location)
  451. elif batch_info['class'] == 3:
  452. self.generate_task_no_batch(container, start_location, allocation_target_location,batch_id,location_min_value.c_number)
  453. self.generate_move_container_operate(container_obj, allocation_target_location)
  454. else:
  455. self.generate_task(container, start_location, allocation_target_location,batch_id,location_min_value.c_number) # 生成任务
  456. self.generate_move_container_operate(container_obj, allocation_target_location)
  457. current_task = ContainerWCSModel.objects.get(
  458. container=container,
  459. tasktype='inbound',
  460. working=1,
  461. )
  462. OutboundService.send_task_to_wcs(current_task)
  463. data_return = {
  464. 'code': '200',
  465. 'message': '任务下发成功',
  466. 'data': current_task.to_dict()
  467. }
  468. container_obj.target_location = allocation_target_location
  469. container_obj.save()
  470. if batch_info['class'] == 1 or batch_info['class'] == 3:
  471. self.inport_update_task(current_task.id, container_obj.id)
  472. http_status = status.HTTP_200_OK if data_return['code'] == '200' else status.HTTP_400_BAD_REQUEST
  473. return Response(data_return, status=http_status)
  474. except Exception as e:
  475. logger.error(f"处理请求时发生错误: {str(e)}", exc_info=True)
  476. return Response(
  477. {'code': '500', 'message': '服务器内部错误', 'data': None},
  478. status=status.HTTP_500_INTERNAL_SERVER_ERROR
  479. )
  480. def get_container_wcs(self, request, *args, **kwargs):
  481. data = self.request.data
  482. container = data.get('container_number')
  483. current_location = data.get('current_location')
  484. logger.info(f"请求托盘:{container},请求位置:{current_location}")
  485. data_return = {}
  486. try:
  487. container_obj = ContainerListModel.objects.filter(container_code=container).first()
  488. if not container_obj:
  489. data_return = {
  490. 'code': '400',
  491. 'message': '托盘编码不存在',
  492. 'data': data
  493. }
  494. return Response(data_return, status=status.HTTP_400_BAD_REQUEST)
  495. # 更新托盘数据(部分更新)
  496. serializer = ContainerListPostSerializer(
  497. container_obj,
  498. data=data,
  499. partial=True # 允许部分字段更新
  500. )
  501. serializer.is_valid(raise_exception=True)
  502. serializer.save()
  503. # 检查是否已在目标位置
  504. if current_location == str(container_obj.target_location) and current_location!= '203' and current_location!= '103':
  505. logger.info(f"托盘 {container} 已在目标位置")
  506. data_return = {
  507. 'code': '200',
  508. 'message': '当前位置已是目标位置',
  509. 'data': data
  510. }
  511. else:
  512. current_task = ContainerWCSModel.objects.filter(
  513. container=container,
  514. tasktype='inbound',
  515. working = 1,
  516. ).exclude(status=300).first()
  517. if current_task:
  518. data_return = {
  519. 'code': '200',
  520. 'message': '任务已存在,重新下发',
  521. 'data': current_task.to_dict()
  522. }
  523. else:
  524. # todo: 这里的入库操作记录里面的记录的数量不对
  525. location_min_value,allocation_target_location, batch_info = AllocationService.allocate(container, current_location)
  526. batch_id = batch_info['number']
  527. if batch_info['class'] == 2:
  528. self.generate_task_no_batch(container, current_location, allocation_target_location,batch_id,location_min_value.c_number)
  529. self.generate_container_operate_no_batch(container_obj, batch_id, allocation_target_location)
  530. elif batch_info['class'] == 3:
  531. self.generate_task_no_batch(container, current_location, allocation_target_location,batch_id,location_min_value.c_number)
  532. self.generate_container_operate(container_obj, allocation_target_location)
  533. else:
  534. self.generate_task(container, current_location, allocation_target_location,batch_id,location_min_value.c_number) # 生成任务
  535. self.generate_container_operate(container_obj, allocation_target_location)
  536. current_task = ContainerWCSModel.objects.get(
  537. container=container,
  538. tasktype='inbound',
  539. working=1,
  540. )
  541. data_return = {
  542. 'code': '200',
  543. 'message': '任务下发成功',
  544. 'data': current_task.to_dict()
  545. }
  546. container_obj.target_location = allocation_target_location
  547. container_obj.save()
  548. if batch_info['class'] == 1 or batch_info['class'] == 3:
  549. self.inport_update_task(current_task.id, container_obj.id)
  550. http_status = status.HTTP_200_OK if data_return['code'] == '200' else status.HTTP_400_BAD_REQUEST
  551. return Response(data_return, status=http_status)
  552. except Exception as e:
  553. logger.error(f"处理请求时发生错误: {str(e)}", exc_info=True)
  554. return Response(
  555. {'code': '500', 'message': '服务器内部错误', 'data': None},
  556. status=status.HTTP_500_INTERNAL_SERVER_ERROR
  557. )
  558. @transaction.atomic
  559. # def generate_container_operate(self, container_obj, bound_number,allocation_target_location):
  560. def generate_container_operate(self, container_obj, allocation_target_location):
  561. # 获取容器中所有有效的批次明细(排除已删除和状态3的)
  562. container_detaillist = ContainerDetailModel.objects.filter(
  563. container=container_obj,
  564. is_delete=False
  565. ).exclude(status=3)
  566. # 优化查询 - 预取批次对象
  567. container_detaillist = container_detaillist.select_related('batch')
  568. # 创建批次数量字典
  569. batch_totals = {}
  570. for detail in container_detaillist:
  571. batch_id = detail.batch_id
  572. if batch_id not in batch_totals:
  573. batch_totals[batch_id] = {
  574. 'obj': detail.batch, # 批次对象
  575. 'total_qty': 0
  576. }
  577. batch_totals[batch_id]['total_qty'] += detail.goods_qty
  578. # 当前月份(单次计算多次使用)
  579. current_month = int(timezone.now().strftime("%Y%m"))
  580. current_time = timezone.now()
  581. current_location = container_obj.current_location
  582. # 为每个批次创建操作记录
  583. for batch_id, data in batch_totals.items():
  584. batch_obj = data['obj']
  585. goods_qty = data['total_qty']
  586. ContainerOperationModel.objects.create(
  587. month=current_month,
  588. container=container_obj,
  589. goods_code=batch_obj.goods_code,
  590. goods_desc=batch_obj.goods_desc,
  591. operation_type="inbound",
  592. batch_id=batch_obj.id,
  593. goods_qty=goods_qty,
  594. goods_weight=goods_qty,
  595. from_location=current_location,
  596. to_location=allocation_target_location,
  597. timestamp=current_time,
  598. operator="WMS",
  599. memo=f"WCS入库: 批次: {batch_obj.bound_number}, 数量: {goods_qty}" # 使用实际容器中的数量
  600. )
  601. @transaction.atomic
  602. def generate_move_container_operate(self, container_obj, allocation_target_location):
  603. # 获取容器中所有有效的批次明细
  604. container_detaillist = ContainerDetailModel.objects.filter(
  605. container=container_obj,
  606. is_delete=False
  607. ).exclude(status=3)
  608. # 优化查询 - 预取批次对象
  609. container_detaillist = container_detaillist.select_related('batch')
  610. # 创建批次数量字典
  611. batch_totals = {}
  612. for detail in container_detaillist:
  613. batch_id = detail.batch_id
  614. if batch_id not in batch_totals:
  615. batch_totals[batch_id] = {
  616. 'obj': detail.batch, # 批次对象
  617. 'total_qty': 0
  618. }
  619. batch_totals[batch_id]['total_qty'] += detail.goods_qty
  620. # 获取当前时间和位置信息
  621. current_month = int(timezone.now().strftime("%Y%m"))
  622. current_time = timezone.now()
  623. current_location = container_obj.current_location
  624. # 为每个批次创建操作记录
  625. for batch_id, data in batch_totals.items():
  626. batch_obj = data['obj']
  627. goods_qty = data['total_qty']
  628. ContainerOperationModel.objects.create(
  629. month=current_month,
  630. container=container_obj,
  631. goods_code=batch_obj.goods_code,
  632. goods_desc=batch_obj.goods_desc,
  633. operation_type="adjust", # 操作类型改为移动
  634. batch_id=batch_obj.id,
  635. goods_qty=goods_qty,
  636. goods_weight=goods_qty,
  637. from_location=current_location,
  638. to_location=allocation_target_location,
  639. timestamp=current_time,
  640. operator="WMS",
  641. memo=f"托盘移动: 批次: {batch_obj.bound_number}, 数量: {goods_qty}"
  642. )
  643. @transaction.atomic
  644. def generate_move_container_operate_no_batch(self, container_obj, bound_number,allocation_target_location):
  645. ContainerOperationModel.objects.create(
  646. month = int(timezone.now().strftime("%Y%m")),
  647. container = container_obj,
  648. goods_code = 'container',
  649. goods_desc = '托盘组',
  650. operation_type ="adjust",
  651. goods_qty = 1,
  652. goods_weight = 0,
  653. from_location = container_obj.current_location,
  654. to_location= allocation_target_location,
  655. timestamp=timezone.now(),
  656. memo=f"托盘组移库:从{container_obj.current_location}移库到{allocation_target_location}"
  657. )
  658. @transaction.atomic
  659. def generate_container_operate_no_batch(self, container_obj, bound_number,allocation_target_location):
  660. ContainerOperationModel.objects.create(
  661. month = int(timezone.now().strftime("%Y%m")),
  662. container = container_obj,
  663. goods_code = 'container',
  664. goods_desc = '托盘组',
  665. operation_type ="inbound",
  666. goods_qty = 1,
  667. goods_weight = 0,
  668. from_location = container_obj.current_location,
  669. to_location= allocation_target_location,
  670. timestamp=timezone.now(),
  671. memo=f"WCS入库: 批次: {bound_number}, 数量: 1"
  672. )
  673. @transaction.atomic
  674. def generate_task(self, container, current_location, target_location,batch_id,location_c_number):
  675. batch = BoundBatchModel.objects.filter(bound_number=batch_id).first()
  676. batch_detail = BoundDetailModel.objects.filter(bound_batch=batch).first()
  677. if not batch:
  678. logger.error(f"批次号 {batch_id} 不存在")
  679. return False
  680. data_tosave = {
  681. 'container': container,
  682. 'batch': batch,
  683. 'batch_number': batch_id,
  684. 'batch_out': None,
  685. 'bound_list': batch_detail.bound_list,
  686. 'sequence': 1,
  687. 'order_number' :location_c_number,
  688. 'priority': 1,
  689. 'current_location': current_location,
  690. 'month': timezone.now().strftime('%Y%m'),
  691. 'target_location': target_location,
  692. 'tasktype': 'inbound',
  693. 'status': 103,
  694. 'is_delete': False
  695. }
  696. # 生成唯一递增的 taskid
  697. last_task = ContainerWCSModel.objects.filter(
  698. month=data_tosave['month'],
  699. ).order_by('-tasknumber').first()
  700. if last_task:
  701. number_id = last_task.tasknumber + 1
  702. new_id = f"{number_id:05d}"
  703. else:
  704. new_id = "00001"
  705. number_id = f"{data_tosave['month']}{new_id}"
  706. data_tosave['taskid'] = f"inbound-{data_tosave['month']}-{new_id}"
  707. logger.info(f"生成入库任务: {data_tosave['taskid']}")
  708. # 每月生成唯一递增的 taskNumber
  709. data_tosave['tasknumber'] = number_id
  710. ContainerWCSModel.objects.create(**data_tosave)
  711. def generate_task_no_batch(self, container, current_location, target_location,batch_id,location_c_number):
  712. data_tosave = {
  713. 'container': container,
  714. 'batch': None,
  715. 'batch_number': batch_id,
  716. 'batch_out': None,
  717. 'bound_list': None,
  718. 'sequence': 1,
  719. 'order_number' :location_c_number,
  720. 'priority': 1,
  721. 'current_location': current_location,
  722. 'month': timezone.now().strftime('%Y%m'),
  723. 'target_location': target_location,
  724. 'tasktype': 'inbound',
  725. 'status': 103,
  726. 'is_delete': False
  727. }
  728. # 生成唯一递增的 taskid
  729. last_task = ContainerWCSModel.objects.filter(
  730. month=data_tosave['month'],
  731. ).order_by('-tasknumber').first()
  732. if last_task:
  733. number_id = last_task.tasknumber + 1
  734. new_id = f"{number_id:05d}"
  735. else:
  736. new_id = "00001"
  737. number_id = f"{data_tosave['month']}{new_id}"
  738. data_tosave['taskid'] = f"inbound-{data_tosave['month']}-{new_id}"
  739. logger.info(f"生成入库任务: {data_tosave['taskid']}")
  740. # 每月生成唯一递增的 taskNumber
  741. data_tosave['tasknumber'] = number_id
  742. ContainerWCSModel.objects.create(**data_tosave)
  743. def update_container_wcs(self, request, *args, **kwargs):
  744. data = self.request.data
  745. logger.info(f"请求托盘:{data.get('container_number')}, 请求位置:{data.get('current_location')}, 任务号:{data.get('taskNumber')}")
  746. try:
  747. # 前置校验
  748. container_obj, error_response = self.validate_container(data)
  749. if error_response:
  750. return error_response
  751. # 更新托盘数据
  752. if not self.update_container_data(container_obj, data):
  753. return Response(
  754. {'code': '400', 'message': '数据更新失败', 'data': data},
  755. status=status.HTTP_400_BAD_REQUEST
  756. )
  757. # 处理位置逻辑
  758. task = ContainerWCSModel.objects.filter(
  759. container=container_obj.container_code,
  760. tasktype='inbound'
  761. ).first()
  762. if self.is_already_at_target(container_obj, data.get('current_location')):
  763. return self.handle_target_reached(container_obj, data)
  764. elif task:
  765. data_return = {
  766. 'code': '200',
  767. 'message': '任务已存在,重新下发',
  768. 'data': task.to_dict()
  769. }
  770. return Response(data_return, status=status.HTTP_200_OK)
  771. else:
  772. return self.handle_new_allocation(container_obj, data)
  773. except Exception as e:
  774. logger.error(f"处理请求时发生错误: {str(e)}", exc_info=True)
  775. return Response({'code': '500', 'message': '服务器内部错误', 'data': None},
  776. status=status.HTTP_500_INTERNAL_SERVER_ERROR)
  777. # ---------- 辅助函数 ----------
  778. def validate_container(self, data):
  779. """验证托盘是否存在"""
  780. container = data.get('container_number')
  781. container_obj = ContainerListModel.objects.filter(container_code=container).first()
  782. if not container_obj:
  783. return None, Response({
  784. 'code': '400',
  785. 'message': '托盘编码不存在',
  786. 'data': data
  787. }, status=status.HTTP_400_BAD_REQUEST)
  788. return container_obj, None
  789. def update_container_data(self, container_obj, data):
  790. """更新托盘数据"""
  791. serializer = ContainerListPostSerializer(
  792. container_obj,
  793. data=data,
  794. partial=True
  795. )
  796. if serializer.is_valid():
  797. serializer.save()
  798. return True
  799. return False
  800. def is_already_at_target(self, container_obj, current_location):
  801. """检查是否已在目标位置"""
  802. print (current_location)
  803. print (str(container_obj.target_location))
  804. return current_location == str(container_obj.target_location)
  805. def handle_target_reached(self, container_obj, data):
  806. """处理已到达目标位置的逻辑"""
  807. logger.info(f"托盘 {container_obj.container_code} 已在目标位置")
  808. task = self.get_task_by_tasknumber(data)
  809. self.update_pressure_values(task, container_obj)
  810. # if task.working == 1:
  811. # alloca = LocationAllocation()
  812. # alloca.update_batch_goods_in_location_qty(container_obj.container_code, 1)
  813. task = self.process_task_completion(data)
  814. if not task:
  815. return Response({'code': '400', 'message': '任务不存在', 'data': data},
  816. status=status.HTTP_400_BAD_REQUEST)
  817. if task and task.tasktype == 'inbound':
  818. self.update_storage_system(container_obj)
  819. if task and task.tasktype == 'outbound' and task.status == 300:
  820. success = self.handle_outbound_completion(container_obj, task)
  821. if not success:
  822. return Response({'code': '500', 'message': '出库状态更新失败', 'data': None},
  823. status=status.HTTP_500_INTERNAL_SERVER_ERROR)
  824. OutboundService.process_next_task()
  825. if task and task.tasktype == 'check' and task.status == 300:
  826. success = self.handle_outbound_completion(container_obj, task)
  827. if not success:
  828. return Response({'code': '500', 'message': '出库状态更新失败', 'data': None},
  829. status=status.HTTP_500_INTERNAL_SERVER_ERROR)
  830. OutboundService.process_next_task()
  831. return Response({
  832. 'code': '200',
  833. 'message': '当前位置已是目标位置',
  834. 'data': data
  835. }, status=status.HTTP_200_OK)
  836. def get_task_by_tasknumber(self, data):
  837. taskNumber = data.get('taskNumber') + 20000000000
  838. task = ContainerWCSModel.objects.filter(tasknumber=taskNumber).first()
  839. if task:
  840. return task
  841. else:
  842. return None
  843. def process_task_completion(self, data):
  844. """处理任务完成状态"""
  845. taskNumber = data.get('taskNumber') + 20000000000
  846. task = ContainerWCSModel.objects.filter(tasknumber=taskNumber).first()
  847. if task:
  848. task.status = 300
  849. task.message = '任务已完成'
  850. task.working = 0
  851. task.save()
  852. return task
  853. def update_pressure_values(self, task, container_obj):
  854. """更新压力值计算"""
  855. if task :
  856. base_location_obj = base_location.objects.get(id=1)
  857. layer = int(container_obj.target_location.split('-')[-1])
  858. pressure_field = f"layer{layer}_pressure"
  859. logger.info(f"更新压力值,压力字段:{pressure_field}")
  860. current_pressure = getattr(base_location_obj, pressure_field, 0)
  861. updated_pressure = max(current_pressure - task.working, 0)
  862. setattr(base_location_obj, pressure_field, updated_pressure)
  863. base_location_obj.save()
  864. def update_storage_system(self, container_obj):
  865. """更新仓储系统状态"""
  866. allocator = LocationAllocation()
  867. location_code = self.get_location_code(container_obj.target_location)
  868. # 链式更新操作
  869. update_operations = [
  870. (allocator.update_location_status, location_code, 'occupied'),
  871. (allocator.update_location_container_link, location_code, container_obj.container_code),
  872. (allocator.update_container_detail_status, container_obj.container_code, 2)
  873. ]
  874. for func, *args in update_operations:
  875. if not func(*args):
  876. logger.error(f"操作失败: {func.__name__}")
  877. return False
  878. return True
  879. def get_location_code(self, target_location):
  880. """从目标位置解析获取位置编码"""
  881. parts = target_location.split('-')
  882. coordinate = f"{int(parts[1])}-{int(parts[2])}-{int(parts[3])}"
  883. return LocationModel.objects.filter(coordinate=coordinate).first().location_code
  884. def handle_new_allocation(self, container_obj, data):
  885. """处理新库位分配逻辑"""
  886. allocator = LocationAllocation()
  887. container_code = container_obj.container_code
  888. # 获取并验证库位分配
  889. location = allocator.get_location_by_status(container_code, data.get('current_location'))
  890. if not location or not self.perform_initial_allocation(allocator, location, container_code):
  891. return Response({'code': '400', 'message': '库位分配失败', 'data': data},
  892. status=status.HTTP_400_BAD_REQUEST)
  893. # 生成目标位置并更新托盘
  894. target_location = self.generate_target_location(location)
  895. container_obj.target_location = target_location
  896. container_obj.save()
  897. # 创建任务并返回响应
  898. task = self.create_inbound_task(container_code, data, target_location, location)
  899. return Response({
  900. 'code': '200',
  901. 'message': '任务下发成功',
  902. 'data': task.to_dict()
  903. }, status=status.HTTP_200_OK)
  904. def perform_initial_allocation(self, allocator, location, container_code):
  905. """执行初始库位分配操作"""
  906. operations = [
  907. (allocator.update_location_status, location.location_code, 'reserved'),
  908. (allocator.update_location_group_status, location.location_code),
  909. (allocator.update_batch_status, container_code, '2'),
  910. (allocator.update_location_group_batch, location, container_code),
  911. (allocator.update_location_container_link, location.location_code, container_code),
  912. (allocator.update_container_detail_status, container_code, 2)
  913. ]
  914. for func, *args in operations:
  915. if not func(*args):
  916. logger.error(f"分配操作失败: {func.__name__}")
  917. return False
  918. return True
  919. def generate_target_location(self, location):
  920. """生成目标位置字符串"""
  921. return (
  922. f"{location.warehouse_code}-"
  923. f"{int(location.row):02d}-"
  924. f"{int(location.col):02d}-"
  925. f"{int(location.layer):02d}"
  926. )
  927. def create_inbound_task(self, container_code, data, target_location, location):
  928. """创建入库任务"""
  929. batch_id = LocationAllocation().get_batch(container_code)
  930. self.generate_task(
  931. container_code,
  932. data.get('current_location'),
  933. target_location,
  934. batch_id,
  935. location.c_number
  936. )
  937. task = ContainerWCSModel.objects.get(container=container_code, tasktype='inbound')
  938. self.inport_update_task(task.id, container_code)
  939. return task
  940. @transaction.atomic
  941. def inport_update_task(self, wcs_id,container_id):
  942. try:
  943. task_obj = ContainerWCSModel.objects.filter(id=wcs_id).first()
  944. if task_obj:
  945. container_detail_obj = ContainerDetailModel.objects.filter(container=container_id,is_delete=False).all()
  946. if container_detail_obj:
  947. for detail in container_detail_obj:
  948. # 保存到数据库
  949. batch = BoundDetailModel.objects.filter(bound_batch_id=detail.batch.id).first()
  950. TaskModel.objects.create(
  951. task_wcs = task_obj,
  952. container_detail = detail,
  953. batch_detail = batch
  954. )
  955. logger.info(f"入库任务 {wcs_id} 已更新")
  956. else:
  957. logger.info(f"入库任务 {container_id} 批次不存在")
  958. else:
  959. logger.info(f"入库任务 {wcs_id} 不存在")
  960. except Exception as e:
  961. logger.error(f"处理入库任务时发生错误: {str(e)}", exc_info=True)
  962. return Response(
  963. {'code': '500', 'message': '服务器内部错误', 'data': None},
  964. status=status.HTTP_500_INTERNAL_SERVER_ERROR
  965. )
  966. def release_location(self,request):
  967. """释放库位"""
  968. """添加权限管理"""
  969. token = self.request.META.get('HTTP_TOKEN')
  970. if not token:
  971. return Response({'code': '401', 'message': '请登录', 'data': None},
  972. status=status.HTTP_200_OK)
  973. user = StaffListModel.objects.filter(openid=token).first()
  974. if not user:
  975. return Response({'code': '401', 'message': '请登录', 'data': None},
  976. status=status.HTTP_200_OK)
  977. if user.staff_type not in ['Supervisor', 'Manager','Admin']:
  978. return Response({'code': '401', 'message': '无权限', 'data': None},
  979. status=status.HTTP_200_OK)
  980. try:
  981. location_release = request.data.get('location_release')
  982. location_row = location_release.split('-')[1]
  983. location_col = location_release.split('-')[2]
  984. location_layer = location_release.split('-')[3]
  985. location= LocationModel.objects.filter(row=location_row, col=location_col, layer=location_layer).first()
  986. location_code = location.location_code
  987. allocator = LocationAllocation()
  988. with transaction.atomic():
  989. if not allocator.release_location(location_code):
  990. raise Exception("解除库位关联失败")
  991. if not allocator.update_location_status(location_code, 'available'):
  992. raise Exception("库位状态更新失败")
  993. if not allocator.update_location_group_status(location_code):
  994. raise Exception("库位组状态更新失败")
  995. except Exception as e:
  996. logger.error(f"解除库位关联失败: {str(e)}")
  997. return Response({'code': '500', 'message': e, 'data': None},
  998. status=status.HTTP_200_OK)
  999. return Response({'code': '200', 'message': '解除库位关联成功', 'data': None},
  1000. status=status.HTTP_200_OK)
  1001. def handle_outbound_completion(self, container_obj, task):
  1002. """处理出库完成后的库位释放和状态更新"""
  1003. try:
  1004. allocator = LocationAllocation()
  1005. location_task = task.current_location
  1006. if location_task == '103' or location_task == '203':
  1007. return True
  1008. else:
  1009. location_row = location_task.split('-')[1]
  1010. location_col = location_task.split('-')[2]
  1011. location_layer = location_task.split('-')[3]
  1012. location= LocationModel.objects.filter(row=location_row, col=location_col, layer=location_layer).first()
  1013. location_code = location.location_code
  1014. # 事务确保原子性
  1015. with transaction.atomic():
  1016. # 解除库位与托盘的关联
  1017. if not allocator.release_location(location_code):
  1018. raise Exception("解除库位关联失败")
  1019. # 更新库位状态为可用
  1020. if not allocator.update_location_status(location_code, 'available'):
  1021. raise Exception("库位状态更新失败")
  1022. # 更新库位组的统计信息
  1023. self.handle_group_location_status(location_code, location.location_group)
  1024. # 更新托盘状态为已出库(假设状态3表示已出库)
  1025. container_obj.status = 3
  1026. container_obj.save()
  1027. return True
  1028. except Exception as e:
  1029. logger.error(f"出库完成处理失败: {str(e)}")
  1030. return False
  1031. def handle_group_location_status(self,location_code,location_group):
  1032. """
  1033. 处理库位组和库位的关联关系
  1034. :param location_code: 库位编码
  1035. :param location_group: 库位组编码
  1036. :return:
  1037. """
  1038. # 1. 获取库位空闲状态的库位数目
  1039. location_obj_number = LocationModel.objects.filter(
  1040. location_group=location_group,
  1041. status='available'
  1042. ).all().count()
  1043. # 2. 获取库位组对象
  1044. logger.info(f"库位组 {location_group} 下的库位数目:{location_obj_number}")
  1045. # 1. 获取库位和库位组的关联关系
  1046. location_group_obj = LocationGroupModel.objects.filter(
  1047. group_code=location_group
  1048. ).first()
  1049. if not location_group_obj:
  1050. logger.info(f"库位组 {location_group} 不存在")
  1051. return None
  1052. else:
  1053. if location_obj_number == 0:
  1054. # 库位组库位已满,更新库位组状态为full
  1055. location_group_obj.status = 'full'
  1056. location_group_obj.save()
  1057. elif location_obj_number < location_group_obj.max_capacity:
  1058. location_group_obj.status = 'occupied'
  1059. location_group_obj.save()
  1060. else:
  1061. location_group_obj.status = 'available'
  1062. location_group_obj.current_batch = ''
  1063. location_group_obj.current_goods_code = ''
  1064. location_group_obj.save()
  1065. # PDA组盘入库 将扫描到的托盘编码和批次信息保存到数据库
  1066. # 1. 先查询托盘对象,如果不存在,则创建托盘对象
  1067. # 2. 循环处理每个批次,查询批次对象,
  1068. # 3. 更新批次数据(根据业务规则)
  1069. # 4. 保存到数据库
  1070. # 5. 保存操作记录到数据库
  1071. class ContainerDetailViewSet(viewsets.ModelViewSet):
  1072. """
  1073. retrieve:
  1074. Response a data list(get)
  1075. list:
  1076. Response a data list(all)
  1077. create:
  1078. Create a data line(post)
  1079. delete:
  1080. Delete a data line(delete)
  1081. """
  1082. # authentication_classes = [] # 禁用所有认证类
  1083. # permission_classes = [AllowAny] # 允许任意访问
  1084. pagination_class = MyPageNumberPagination
  1085. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  1086. ordering_fields = ['id', "create_time", "update_time", ]
  1087. filter_class = ContainerDetailFilter
  1088. def get_project(self):
  1089. try:
  1090. id = self.kwargs.get('pk')
  1091. return id
  1092. except:
  1093. return None
  1094. def get_queryset(self):
  1095. id = self.get_project()
  1096. if self.request.user:
  1097. if id is None:
  1098. return ContainerDetailModel.objects.filter( is_delete=False)
  1099. else:
  1100. return ContainerDetailModel.objects.filter( id=id, is_delete=False)
  1101. else:
  1102. return ContainerDetailModel.objects.none()
  1103. def get_serializer_class(self):
  1104. if self.action in ['list', 'destroy','retrieve']:
  1105. return ContainerDetailGetSerializer
  1106. elif self.action in ['create', 'update']:
  1107. return ContainerDetailPostSerializer
  1108. else:
  1109. return self.http_method_not_allowed(request=self.request)
  1110. def create(self, request, *args, **kwargs):
  1111. data = self.request.data
  1112. from .container_operate import ContainerService
  1113. ContainerService.create_container_operation(data,logger=logger)
  1114. # 将处理后的数据返回(或根据业务需求保存到数据库)
  1115. res_data={
  1116. "code": "200",
  1117. "msg": "Success Create",
  1118. "data": data
  1119. }
  1120. return Response(res_data, status=200)
  1121. def update(self, request, pk):
  1122. qs = self.get_object()
  1123. data = self.request.data
  1124. serializer = self.get_serializer(qs, data=data)
  1125. serializer.is_valid(raise_exception=True)
  1126. serializer.save()
  1127. headers = self.get_success_headers(serializer.data)
  1128. return Response(serializer.data, status=200, headers=headers)
  1129. def destroy(self, request, pk):
  1130. qs = self.get_object()
  1131. qs.is_delete = True
  1132. qs.save()
  1133. return Response({'code': 200,'message': '删除成功', 'data': None}, status=200)
  1134. def containerdetail_list(self, request):
  1135. """
  1136. 获取托盘详情列表
  1137. """
  1138. try:
  1139. container_id = request.query_params.get('container')
  1140. if not container_id:
  1141. return Response(
  1142. {'code': 400, 'message': '缺少托盘ID参数', 'data': None},
  1143. status=status.HTTP_400_BAD_REQUEST
  1144. )
  1145. # 获取托盘对象
  1146. try:
  1147. container = ContainerListModel.objects.get(id=container_id)
  1148. except ContainerListModel.DoesNotExist:
  1149. return Response(
  1150. {'code': 404, 'message': '指定托盘不存在', 'data': None},
  1151. status=status.HTTP_404_NOT_FOUND
  1152. )
  1153. # 查询关联批次明细(排除状态0和3)
  1154. details = ContainerDetailModel.objects.filter(
  1155. container=container,is_delete=False
  1156. ).exclude(
  1157. status__in=[0, 3]
  1158. ).select_related('batch')
  1159. details_serializer = ContainerDetailSimpleGetSerializer(details, many=True)
  1160. return Response(
  1161. {'code': 200, 'message': 'Success', 'data': details_serializer.data},
  1162. status=status.HTTP_200_OK
  1163. )
  1164. except Exception as e:
  1165. return Response(
  1166. {'code': 500, 'message': f'服务器错误: {str(e)}', 'data': None},
  1167. status=status.HTTP_500_INTERNAL_SERVER_ERROR
  1168. )
  1169. def locationdetail_list(self, request):
  1170. """
  1171. 获取库位所处托盘的信息(按批次号 + 数量分组)
  1172. 新增批次总量统计功能
  1173. """
  1174. try:
  1175. container_id = request.query_params.get('container')
  1176. if not container_id:
  1177. return Response(
  1178. {'code': 400, 'message': '缺少托盘ID参数', 'data': None},
  1179. status=status.HTTP_400_BAD_REQUEST
  1180. )
  1181. # 获取托盘对象
  1182. try:
  1183. container = ContainerListModel.objects.get(id=container_id)
  1184. except ContainerListModel.DoesNotExist:
  1185. return Response(
  1186. {'code': 404, 'message': '指定托盘不存在', 'data': None},
  1187. status=status.HTTP_404_NOT_FOUND
  1188. )
  1189. # 查询关联批次明细(排除状态0和3)
  1190. details = ContainerDetailModel.objects.filter(
  1191. container=container,is_delete=False
  1192. ).exclude(
  1193. status__in=[0, 3]
  1194. ).select_related('batch')
  1195. if not details.exists():
  1196. return Response(
  1197. {'code': 404, 'message': '未找到有效批次数据', 'data': None},
  1198. status=status.HTTP_404_NOT_FOUND
  1199. )
  1200. # 按批次号 + 数量分组统计
  1201. batch_dict = {}
  1202. batch_qty_dict = defaultdict(int) # 使用默认字典自动初始化
  1203. for detail in details:
  1204. if not detail.batch:
  1205. continue
  1206. bound_number = detail.batch.bound_number
  1207. goods_qty = detail.goods_qty - detail.goods_out_qty # 剔除出库数量
  1208. # 组合键:批次号 + 当前数量
  1209. batch_key = (bound_number, goods_qty)
  1210. batch_qty_dict[bound_number] += goods_qty # 自动处理键初始化
  1211. # 分组统计
  1212. if batch_key not in batch_dict:
  1213. batch_obj = BoundBatchModel.objects.filter( bound_number=bound_number).first()
  1214. batch_dict[batch_key] = {
  1215. "goods_code": detail.goods_code,
  1216. "goods_desc": detail.goods_desc,
  1217. "goods_qty": goods_qty,
  1218. "goods_class": detail.goods_class,
  1219. "goods_package": batch_obj.goods_package,
  1220. "batch_total_qty": batch_obj.goods_qty,
  1221. "batch_total_in_qty": batch_obj.goods_in_qty - batch_obj.goods_out_qty,
  1222. "status": detail.status,
  1223. "group_qty": 1,
  1224. "create_time": detail.create_time,
  1225. }
  1226. else:
  1227. batch_dict[batch_key]["group_qty"] += 1
  1228. # 重构数据结构
  1229. results = []
  1230. for (bound_number, qty), data in batch_dict.items():
  1231. results.append({
  1232. **data,
  1233. "bound_number": bound_number,
  1234. "current_qty": qty,
  1235. "total_batch_qty": batch_qty_dict[bound_number] # 添加批次总量
  1236. })
  1237. batch_totals =[]
  1238. for bound_number, qty in batch_qty_dict.items():
  1239. batch_totals.append({
  1240. "bound_number": bound_number,
  1241. "total_batch_qty": qty
  1242. })
  1243. return Response(
  1244. {
  1245. "code": 200,
  1246. "message": "Success",
  1247. "data": {
  1248. "container_code": container.container_code,
  1249. "count": len(results),
  1250. "results": results,
  1251. "batch_totals": batch_totals # 可选:单独返回批次总量
  1252. }
  1253. },
  1254. status=status.HTTP_200_OK
  1255. )
  1256. except Exception as e:
  1257. return Response(
  1258. {'code': 500, 'message': f'服务器错误: {str(e)}', 'data': None},
  1259. status=status.HTTP_500_INTERNAL_SERVER_ERROR
  1260. )
  1261. def pdadetail_list(self, request):
  1262. """
  1263. 获取PDA组盘入库的托盘详情列表
  1264. """
  1265. try:
  1266. container_code = request.query_params.get('container_code')
  1267. if not container_code:
  1268. return Response(
  1269. {'code': 400, 'message': '缺少托盘编码参数', 'data': None},
  1270. status=status.HTTP_200_OK
  1271. )
  1272. # 获取托盘对象
  1273. try:
  1274. container = ContainerListModel.objects.get(container_code=container_code)
  1275. except ContainerListModel.DoesNotExist:
  1276. return Response(
  1277. {'code': 404, 'message': '指定托盘不存在', 'data': None},
  1278. status=status.HTTP_200_OK
  1279. )
  1280. # 查询关联批次明细(排除状态0和3)
  1281. details = ContainerDetailModel.objects.filter(
  1282. container=container,is_delete=False
  1283. ).exclude(
  1284. status__in=[0, 3]
  1285. ).select_related('batch')
  1286. if not details.exists():
  1287. return Response(
  1288. {'code': 404, 'message': '未找到有效批次数据', 'data': None},
  1289. status=status.HTTP_200_OK
  1290. )
  1291. # 按批次号 + 数量分组统计
  1292. batch_dict = {}
  1293. batch_qty_dict = defaultdict(int) # 使用默认字典自动初始化
  1294. for detail in details:
  1295. if not detail.batch:
  1296. continue
  1297. bound_number = detail.batch.bound_number
  1298. goods_qty = detail.goods_qty - detail.goods_out_qty # 剔除出库数量
  1299. # 组合键:批次号 + 当前数量
  1300. batch_key = (bound_number, goods_qty)
  1301. batch_qty_dict[bound_number] += goods_qty # 自动处理键初始化
  1302. # 分组统计
  1303. if batch_key not in batch_dict:
  1304. batch_obj = BoundBatchModel.objects.filter( bound_number=bound_number).first()
  1305. batch_dict[batch_key] = {
  1306. "goods_code": detail.goods_code,
  1307. "goods_desc": detail.goods_desc,
  1308. "goods_qty": goods_qty,
  1309. "goods_class": detail.goods_class,
  1310. "goods_package": batch_obj.goods_package,
  1311. "batch_total_qty": batch_obj.goods_qty,
  1312. "batch_total_in_qty": batch_obj.goods_in_qty - batch_obj.goods_out_qty,
  1313. "status": detail.status,
  1314. "group_qty": 1,
  1315. "create_time": detail.create_time,
  1316. }
  1317. else:
  1318. batch_dict[batch_key]["group_qty"] += 1
  1319. # 重构数据结构
  1320. results = []
  1321. for (bound_number, qty), data in batch_dict.items():
  1322. results.append({
  1323. **data,
  1324. "bound_number": bound_number,
  1325. "current_qty": qty,
  1326. "total_batch_qty": batch_qty_dict[bound_number] # 添加批次总量
  1327. })
  1328. batch_totals =[]
  1329. for bound_number, qty in batch_qty_dict.items():
  1330. batch_totals.append({
  1331. "bound_number": bound_number,
  1332. "total_batch_qty": qty
  1333. })
  1334. return Response(
  1335. {
  1336. "code": 200,
  1337. "message": "Success",
  1338. "data": {
  1339. "count": len(results),
  1340. "results": results,
  1341. "batch_totals": batch_totals # 可选:单独返回批次总量
  1342. }
  1343. },
  1344. status=status.HTTP_200_OK
  1345. )
  1346. except Exception as e:
  1347. return Response(
  1348. {'code': 500, 'message': f'服务器错误: {str(e)}', 'data': None},
  1349. status=status.HTTP_200_OK
  1350. )
  1351. # 托盘操作历史记录
  1352. class ContainerOperateViewSet(viewsets.ModelViewSet):
  1353. """
  1354. retrieve:
  1355. Response a data list(get)
  1356. list:
  1357. Response a data list(all)
  1358. create:
  1359. Create a data line(post)
  1360. delete:
  1361. Delete a data line(delete)
  1362. """
  1363. # authentication_classes = [] # 禁用所有认证类
  1364. # permission_classes = [AllowAny] # 允许任意访问
  1365. pagination_class = MyPageNumberPagination
  1366. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  1367. ordering_fields = ['id', "timestamp" ]
  1368. filter_class = ContainerOperationFilter
  1369. def get_project(self):
  1370. try:
  1371. id = self.kwargs.get('pk')
  1372. return id
  1373. except:
  1374. return None
  1375. def get_queryset(self):
  1376. id = self.get_project()
  1377. if self.request.user:
  1378. if id is None:
  1379. return ContainerOperationModel.objects.filter( is_delete=False)
  1380. else:
  1381. return ContainerOperationModel.objects.filter( id=id, is_delete=False)
  1382. else:
  1383. return ContainerOperationModel.objects.none()
  1384. def get_serializer_class(self):
  1385. if self.action in ['list', 'destroy','retrieve']:
  1386. return ContainerOperationGetSerializer
  1387. elif self.action in ['create', 'update']:
  1388. return ContainerOperationPostSerializer
  1389. else:
  1390. return self.http_method_not_allowed(request=self.request)
  1391. def create(self, request, *args, **kwargs):
  1392. data = self.request.data
  1393. serializer = self.get_serializer(data=data)
  1394. serializer.is_valid(raise_exception=True)
  1395. serializer.save()
  1396. headers = self.get_success_headers(serializer.data)
  1397. return Response(serializer.data, status=200, headers=headers)
  1398. def update(self, request, pk):
  1399. qs = self.get_object()
  1400. data = self.request.data
  1401. serializer = self.get_serializer(qs, data=data)
  1402. serializer.is_valid(raise_exception=True)
  1403. serializer.save()
  1404. headers = self.get_success_headers(serializer.data)
  1405. return Response(serializer.data, status=200, headers=headers)
  1406. # 出库任务生成
  1407. class OutboundService:
  1408. @staticmethod
  1409. def generate_task_id():
  1410. """生成唯一任务ID(格式: outbound-年月-顺序号)"""
  1411. month = timezone.now().strftime("%Y%m")
  1412. last_task = ContainerWCSModel.objects.filter(
  1413. tasktype='outbound',
  1414. month=int(month)
  1415. ).order_by('-sequence').first()
  1416. sequence = last_task.sequence + 1 if last_task else 1
  1417. return f"outbound-{month}-{sequence:05d}"
  1418. @staticmethod
  1419. def send_task_to_wcs(task):
  1420. """异步发送任务到WCS(非阻塞版本)"""
  1421. # 提取任务关键数据用于线程(避免直接传递ORM对象)
  1422. task_data = {
  1423. 'task_id': task.pk, # 使用主键而不是对象
  1424. 'send_data': {
  1425. "code":'200',
  1426. "message": task.message,
  1427. "data":{
  1428. "taskid": task.taskid,
  1429. "container": task.container,
  1430. "current_location": task.current_location,
  1431. "target_location": task.target_location,
  1432. "tasktype": task.tasktype,
  1433. "month": task.month,
  1434. "message": task.message,
  1435. "status": task.status,
  1436. "taskNumber": task.tasknumber-20000000000,
  1437. "order_number":task.order_number,
  1438. "sequence":task.sequence
  1439. }
  1440. }
  1441. }
  1442. # 创建并启动线程
  1443. thread = threading.Thread(
  1444. target=OutboundService._async_send_handler,
  1445. kwargs=task_data,
  1446. daemon=True # 守护线程(主程序退出时自动终止)
  1447. )
  1448. thread.start()
  1449. return True # 立即返回表示已开始处理
  1450. @staticmethod
  1451. def _async_send_handler(task_id, send_data):
  1452. """异步处理的实际工作函数"""
  1453. try:
  1454. # 每个线程需要独立的数据库连接
  1455. close_old_connections()
  1456. # 重新获取任务对象(确保使用最新数据)
  1457. task = ContainerWCSModel.objects.get(pk=task_id)
  1458. # 发送第一个请求(不处理结果)
  1459. requests.post(
  1460. "http://127.0.0.1:8008/container/batch/",
  1461. json=send_data,
  1462. timeout=10
  1463. )
  1464. # 发送关键请求
  1465. response = requests.post(
  1466. "http://192.168.18.200:1616/wcs/WebApi/getOutTask",
  1467. json=send_data,
  1468. timeout=10
  1469. )
  1470. # 处理响应
  1471. if response.status_code == 200:
  1472. task.status = 200
  1473. task.save()
  1474. logger.info(f"任务 {task.taskid} 已发送")
  1475. else:
  1476. logger.error(f"WCS返回错误: {response.text}")
  1477. except Exception as e:
  1478. logger.error(f"发送失败: {str(e)}")
  1479. finally:
  1480. close_old_connections() # 清理数据库连接
  1481. @staticmethod
  1482. def create_initial_tasks(container_list,bound_list_id):
  1483. """生成初始任务队列"""
  1484. with transaction.atomic():
  1485. current_WCS = ContainerWCSModel.objects.filter(tasktype='outbound',bound_list_id = bound_list_id).first()
  1486. if current_WCS:
  1487. logger.error(f"当前{bound_list_id}已有出库任务")
  1488. return False
  1489. tasks = []
  1490. start_sequence = ContainerWCSModel.objects.filter(tasktype='outbound').count() + 1
  1491. tasknumber = ContainerWCSModel.objects.filter().count()
  1492. tasknumber_index = 1
  1493. for index, container in enumerate(container_list, start=start_sequence):
  1494. container_obj = ContainerListModel.objects.filter(id =container['container_number']).first()
  1495. if container_obj.current_location != container_obj.target_location:
  1496. logger.error(f"托盘 {container_obj.container_code} 未到达目的地,不生成任务")
  1497. return False
  1498. OutBoundDetail_obj = OutBoundDetailModel.objects.filter(bound_list=bound_list_id,bound_batch_number_id=container['batch_id']).first()
  1499. if not OutBoundDetail_obj:
  1500. logger.error(f"批次 {container['batch_id']} 不存在")
  1501. return False
  1502. month = int(timezone.now().strftime("%Y%m"))
  1503. task = ContainerWCSModel(
  1504. taskid=OutboundService.generate_task_id(),
  1505. batch = OutBoundDetail_obj.bound_batch_number,
  1506. batch_out = OutBoundDetail_obj.bound_batch,
  1507. bound_list = OutBoundDetail_obj.bound_list,
  1508. sequence=index,
  1509. order_number = container['c_number'],
  1510. priority=100,
  1511. tasknumber = month*100000+tasknumber_index+tasknumber,
  1512. container=container_obj.container_code,
  1513. current_location=container_obj.current_location,
  1514. target_location="103",
  1515. tasktype="outbound",
  1516. month=int(timezone.now().strftime("%Y%m")),
  1517. message="等待出库",
  1518. status=100,
  1519. )
  1520. tasknumber_index += 1
  1521. tasks.append(task)
  1522. container_obj = ContainerListModel.objects.filter(container_code=task.container).first()
  1523. container_obj.target_location = task.target_location
  1524. container_obj.save()
  1525. ContainerWCSModel.objects.bulk_create(tasks)
  1526. logger.info(f"已创建 {len(tasks)} 个初始任务")
  1527. @staticmethod
  1528. def create_initial_check_tasks(container_list,batch_id):
  1529. """生成初始任务队列"""
  1530. with transaction.atomic():
  1531. current_WCS = ContainerWCSModel.objects.filter(tasktype='check',batch_id = batch_id,is_delete=False,working=1).first()
  1532. if current_WCS:
  1533. logger.error(f"当前{batch_id}已有检查任务")
  1534. return False
  1535. tasks = []
  1536. start_sequence = ContainerWCSModel.objects.filter(tasktype='check').count() + 1
  1537. tasknumber = ContainerWCSModel.objects.filter().count()
  1538. tasknumber_index = 1
  1539. for index, container in enumerate(container_list, start=start_sequence):
  1540. container_obj = ContainerListModel.objects.filter(id =container['container_number']).first()
  1541. if container_obj.current_location != container_obj.target_location:
  1542. logger.error(f"托盘 {container_obj.container_code} 未到达目的地,不生成任务")
  1543. return False
  1544. batch_obj = BoundBatchModel.objects.filter(id =batch_id).first()
  1545. month = int(timezone.now().strftime("%Y%m"))
  1546. task = ContainerWCSModel(
  1547. taskid=OutboundService.generate_task_id(),
  1548. batch = batch_obj,
  1549. batch_out = None,
  1550. bound_list = None,
  1551. sequence=index,
  1552. order_number = container['c_number'],
  1553. priority=100,
  1554. tasknumber = month*100000+tasknumber_index+tasknumber,
  1555. container=container_obj.container_code,
  1556. current_location=container_obj.current_location,
  1557. target_location="103",
  1558. tasktype="check",
  1559. month=int(timezone.now().strftime("%Y%m")),
  1560. message="等待出库",
  1561. status=100,
  1562. )
  1563. tasknumber_index += 1
  1564. tasks.append(task)
  1565. container_obj = ContainerListModel.objects.filter(container_code=task.container).first()
  1566. container_obj.target_location = task.target_location
  1567. container_obj.save()
  1568. ContainerWCSModel.objects.bulk_create(tasks)
  1569. logger.info(f"已创建 {len(tasks)} 个初始任务")
  1570. @staticmethod
  1571. def insert_new_tasks(new_tasks):
  1572. """动态插入新任务并重新排序"""
  1573. with transaction.atomic():
  1574. pending_tasks = list(ContainerWCSModel.objects.select_for_update().filter(status=100))
  1575. # 插入新任务
  1576. for new_task_data in new_tasks:
  1577. new_task = ContainerWCSModel(
  1578. taskid=OutboundService.generate_task_id(),
  1579. priority=new_task_data.get('priority', 100),
  1580. container=new_task_data['container'],
  1581. current_location=new_task_data['current_location'],
  1582. target_location=new_task_data.get('target_location', 'OUT01'),
  1583. tasktype="outbound",
  1584. month=int(timezone.now().strftime("%Y%m")),
  1585. message="等待出库",
  1586. status=100,
  1587. )
  1588. # 找到插入位置
  1589. insert_pos = 0
  1590. for i, task in enumerate(pending_tasks):
  1591. if new_task.priority < task.priority:
  1592. insert_pos = i
  1593. break
  1594. else:
  1595. insert_pos = len(pending_tasks)
  1596. pending_tasks.insert(insert_pos, new_task)
  1597. # 重新分配顺序号
  1598. for i, task in enumerate(pending_tasks, start=1):
  1599. task.sequence = i
  1600. if task.pk is None:
  1601. task.save()
  1602. else:
  1603. task.save(update_fields=['sequence'])
  1604. logger.info(f"已插入 {len(new_tasks)} 个新任务")
  1605. @staticmethod
  1606. def process_next_task():
  1607. """处理下一个任务"""
  1608. next_task = ContainerWCSModel.objects.filter(status=100,working=1,is_delete=False).order_by('sequence').first()
  1609. if not next_task:
  1610. logger.info("没有待处理任务")
  1611. return
  1612. location = next_task.current_location
  1613. if location =='103' or location =='203':
  1614. logger.info (f"需要跳过该任务")
  1615. next_task.status = 200
  1616. next_task.working = 0
  1617. next_task.save()
  1618. OutboundService.process_next_task()
  1619. return
  1620. allocator = LocationAllocation()
  1621. OutboundService.perform_initial_allocation(allocator, next_task.current_location)
  1622. OutboundService.send_task_to_wcs(next_task)
  1623. @staticmethod
  1624. def process_current_task(task_id):
  1625. """发送指定任务"""
  1626. try:
  1627. task = ContainerWCSModel.objects.get(taskid=task_id)
  1628. allocator = LocationAllocation()
  1629. OutboundService.perform_initial_allocation(allocator, task.current_location)
  1630. OutboundService.send_task_to_wcs(task)
  1631. except Exception as e:
  1632. logger.error(f"任务处理失败: {str(e)}")
  1633. def perform_initial_allocation(allocator, location):
  1634. """执行初始库位分配操作"""
  1635. location_row = location.split('-')[1]
  1636. location_col = location.split('-')[2]
  1637. location_layer = location.split('-')[3]
  1638. location_code = LocationModel.objects.filter(row=location_row, col=location_col, layer=location_layer).first().location_code
  1639. if not location_code:
  1640. logger.error(f"未找到库位: {location}")
  1641. operations = [
  1642. (allocator.update_location_status,location_code, 'reserved'),
  1643. (allocator.update_location_group_status,location_code)
  1644. ]
  1645. for func, *args in operations:
  1646. if not func(*args):
  1647. logger.error(f"分配操作失败: {func.__name__}")
  1648. return False
  1649. return True
  1650. # 出库任务下发
  1651. class OutTaskViewSet(ViewSet):
  1652. """
  1653. # fun:get_out_task:下发出库任务
  1654. # fun:get_batch_count_by_boundlist:获取出库申请下的批次数量
  1655. # fun:generate_location_by_demand:根据出库需求生成出库任务
  1656. """
  1657. # authentication_classes = [] # 禁用所有认证类
  1658. # permission_classes = [AllowAny] # 允许任意访问
  1659. def post(self, request):
  1660. try:
  1661. data = self.request.data
  1662. logger.info(f"收到 出库 推送数据: {data}")
  1663. # 从请求中获取 bound_list_id
  1664. bound_list_id = data.get('bound_list_id')
  1665. current_WCS = ContainerWCSModel.objects.filter(tasktype='outbound',bound_list_id = bound_list_id,is_delete=False).first()
  1666. if current_WCS:
  1667. logger.info(f"当前{bound_list_id}已有出库任务{current_WCS.taskid}")
  1668. if current_WCS.working == 1:
  1669. OutboundService.process_current_task(current_WCS.taskid)
  1670. return Response({"code": 200, "msg": f"下发任务{ current_WCS.taskid }到WCS成功"}, status=200)
  1671. else :
  1672. return Response({"code": 200, "msg": f"当前任务{current_WCS.taskid}正在处理中"}, status=200)
  1673. # 获取关联的出库批次
  1674. out_batches = OutBatchModel.objects.filter(
  1675. bound_list_id=bound_list_id,
  1676. is_delete=False
  1677. ).select_related('batch_number')
  1678. if not out_batches.exists():
  1679. return Response({"code": 404, "msg": "未找到相关出库批次"}, status=404)
  1680. # 构建批次需求字典
  1681. batch_demand = {}
  1682. for ob in out_batches:
  1683. if ob.batch_number_id not in batch_demand:
  1684. batch_demand[ob.batch_number_id] = {
  1685. 'required': ob.goods_out_qty,
  1686. 'allocated': ob.goods_qty,
  1687. 'remaining': ob.goods_qty - ob.goods_out_qty
  1688. }
  1689. else:
  1690. batch_demand[ob.batch_number_id]['required'] += ob.goods_out_qty
  1691. batch_demand[ob.batch_number_id]['allocated'] += ob.goods_qty
  1692. batch_demand[ob.batch_number_id]['remaining'] += (ob.goods_out_qty - ob.goods_qty)
  1693. # 生成出库任务
  1694. generate_result = self.generate_location_by_demand(
  1695. batch_demand=batch_demand,
  1696. bound_list_id=bound_list_id
  1697. )
  1698. if generate_result['code'] != '200':
  1699. return Response(generate_result, status=500)
  1700. # 创建并处理出库任务
  1701. container_list = generate_result['data']
  1702. # 2. 生成初始任务
  1703. OutboundService.create_initial_tasks(container_list,bound_list_id)
  1704. # 3. 立即发送第一个任务
  1705. OutboundService.process_next_task()
  1706. return Response({"code": 200, "msg": "下发任务成功"}, status=200)
  1707. except Exception as e:
  1708. logger.error(f"任务生成失败: {str(e)}")
  1709. return Response({"code": 200, "msg": str(e)}, status=200)
  1710. def post_check(self, request):
  1711. try:
  1712. data = self.request.data
  1713. logger.info(f"收到 出库抽检 推送数据: {data}")
  1714. # 从请求中获取 batch_id
  1715. batch_id = data.get('batch_id')
  1716. container_demand = int(data.get('container_demand'))
  1717. if not batch_id or not container_demand:
  1718. return Response({"code": "400", "msg": "缺少抽检数目或批次号"}, status=200)
  1719. current_WCS = ContainerWCSModel.objects.filter(batch=batch_id,tasktype='check',is_delete=False,working=1).first()
  1720. if current_WCS:
  1721. logger.info(f"当前{batch_id}已有出库抽检任务{current_WCS.taskid}")
  1722. if current_WCS.working == 1:
  1723. OutboundService.process_current_task(current_WCS.taskid)
  1724. return Response({"code": 200, "msg": f"下发任务{ current_WCS.taskid }到WCS成功"}, status=200)
  1725. else :
  1726. return Response({"code": 200, "msg": f"当前任务{current_WCS.taskid}正在处理中"}, status=200)
  1727. # 获取批次号
  1728. generate_result = self.generate_location_by_check(batch_id,container_demand)
  1729. if generate_result['code'] != '200':
  1730. return Response(generate_result, status=200)
  1731. # 创建并处理出库任务
  1732. container_list = generate_result['data']
  1733. # 3. 立即发送第一个任务
  1734. OutboundService.create_initial_check_tasks(container_list,batch_id)
  1735. OutboundService.process_next_task()
  1736. return Response({"code": 200, "msg": "下发任务成功"}, status=200)
  1737. except Exception as e:
  1738. logger.error(f"任务生成失败: {str(e)}")
  1739. return Response({"code": 200, "msg": str(e)}, status=200)
  1740. def get_batch_count_by_boundlist(self,bound_list_id):
  1741. try:
  1742. bound_list_obj_all = OutBoundDetailModel.objects.filter(bound_list=bound_list_id).all()
  1743. if bound_list_obj_all:
  1744. batch_count_dict = {}
  1745. # 统计批次数量(创建哈希表,去重)
  1746. for batch in bound_list_obj_all:
  1747. if batch.bound_batch_number_id not in batch_count_dict:
  1748. batch_count_dict[batch.bound_batch_number_id] = batch.bound_batch.goods_out_qty
  1749. else:
  1750. batch_count_dict[batch.bound_batch_number_id] += batch.bound_batch.goods_out_qty
  1751. return batch_count_dict
  1752. else:
  1753. logger.error(f"查询批次数量失败: {bound_list_id} 不存在")
  1754. return {}
  1755. except Exception as e:
  1756. logger.error(f"查询批次数量失败: {str(e)}")
  1757. return {}
  1758. def get_location_by_status_and_batch(self,status,bound_id):
  1759. try:
  1760. container_obj = ContainerDetailModel.objects.filter(batch=bound_id,status=status,is_delete=False).all()
  1761. if container_obj:
  1762. container_dict = {}
  1763. # 统计托盘数量(创建哈希表,去重)
  1764. for obj in container_obj:
  1765. if obj.container_id not in container_dict:
  1766. container_dict[obj.container_id] = obj.goods_qty
  1767. else:
  1768. container_dict[obj.container_id] += obj.goods_qty
  1769. return container_dict
  1770. else:
  1771. logger.error(f"查询{status}状态的批次数量失败: {bound_id} 不存在")
  1772. return {}
  1773. except Exception as e:
  1774. logger.error(f"查询{status}状态的批次数量失败: {str(e)}")
  1775. return {}
  1776. def get_order_by_batch(self,container_list,bound_id):
  1777. try:
  1778. container_dict = {}
  1779. for container in container_list:
  1780. location_container = LocationContainerLink.objects.filter(container_id=container,is_active=True).first()
  1781. if location_container:
  1782. location_c_number = location_container.location.c_number
  1783. if container not in container_dict:
  1784. container_dict[container] = {
  1785. "container_number":container,
  1786. "location_c_number":location_c_number,
  1787. "location_id ":location_container.location.id,
  1788. "location_type":location_container.location.location_type,
  1789. "batch_id":bound_id,
  1790. }
  1791. if len(container_dict.keys()) == len(container_list):
  1792. return container_dict
  1793. else:
  1794. logger.error(f"查询批次数量失败: {container_list} 不存在")
  1795. return {}
  1796. except Exception as e:
  1797. logger.error(f"查询批次数量失败: {str(e)}")
  1798. return {}
  1799. except Exception as e:
  1800. logger.error(f"查询{status}状态的批次数量失败: {str(e)}")
  1801. return {}
  1802. def get_container_allocation(self, batch_id):
  1803. """兼容所有数据库的去重方案"""
  1804. # 获取唯一托盘ID列表
  1805. container_ids = (
  1806. ContainerDetailModel.objects
  1807. .filter(batch_id=batch_id, status=2,is_delete=False)
  1808. .values_list('container_id', flat=True)
  1809. .distinct()
  1810. )
  1811. # 获取每个托盘的最新明细(按id倒序)
  1812. return (
  1813. ContainerDetailModel.objects
  1814. .filter(container_id__in=container_ids,batch_id=batch_id, status=2,is_delete=False)
  1815. .select_related('container')
  1816. .prefetch_related(
  1817. Prefetch('container__location_links',
  1818. queryset=LocationContainerLink.objects.select_related('location'),
  1819. to_attr='active_location')
  1820. )
  1821. .order_by('container_id', '-id')
  1822. )
  1823. def generate_location_by_check(self,batch_id,container_demand):
  1824. '''
  1825. 根据抽检托盘数目,把相应的库位给到出库任务
  1826. '''
  1827. try:
  1828. return_data = []
  1829. # 获取已去重的托盘列表
  1830. container_qs = self.get_container_allocation(batch_id)
  1831. # 构建托盘信息字典(自动去重)
  1832. container_map = {}
  1833. for cd in container_qs:
  1834. if cd.container_id in container_map:
  1835. continue
  1836. # 获取有效库位信息
  1837. active_location = next(
  1838. (link.location for link in cd.container.active_location
  1839. if link.is_active),
  1840. None
  1841. )
  1842. container_map[cd.container_id] = {
  1843. 'detail': cd,
  1844. 'container': cd.container,
  1845. 'location': active_location
  1846. }
  1847. # 转换为排序列表
  1848. container_list = list(container_map.values())
  1849. sorted_containers = sorted(
  1850. container_list,
  1851. key=lambda x: (
  1852. self._get_goods_class_priority(x['detail'].goods_class),
  1853. -(x['location'].c_number if x['location'] else 0),
  1854. # -(x['location'].layer if x['location'] else 0),
  1855. # x['location'].row if x['location'] else 0,
  1856. # x['location'].col if x['location'] else 0
  1857. )
  1858. )
  1859. for item in sorted_containers:
  1860. if container_demand <= 0:
  1861. break
  1862. # 获取可分配数量
  1863. # 记录分配信息
  1864. allocate_container = {
  1865. "container_number": item['container'].id,
  1866. "batch_id": batch_id,
  1867. "location_code": item['location'].location_code if item['location'] else 'N/A',
  1868. "allocate_qty": 0,
  1869. "c_number": item['location'].c_number if item['location'] else 0
  1870. }
  1871. return_data.append(allocate_container)
  1872. container_demand -= 1
  1873. # 降重 return_data,以container_number为key
  1874. return_data = list({v['container_number']: v for v in return_data}.values())
  1875. # 排序
  1876. return_data = sorted(return_data, key=lambda x: -x['c_number'])
  1877. return {"code": "200", "msg": "Success", "data": return_data}
  1878. except Exception as e:
  1879. logger.error(f"出库任务生成失败: {str(e)}", exc_info=True)
  1880. return {"code": "500", "msg": str(e)}
  1881. def generate_location_by_demand(self, batch_demand, bound_list_id):
  1882. try:
  1883. return_data = []
  1884. for batch_id, demand in batch_demand.items():
  1885. # 获取已去重的托盘列表
  1886. container_qs = self.get_container_allocation(batch_id)
  1887. # 构建托盘信息字典(自动去重)
  1888. container_map = {}
  1889. for cd in container_qs:
  1890. if cd.container_id in container_map:
  1891. container_map[cd.container_id]['goods_qty'] += cd.goods_qty - cd.goods_out_qty
  1892. continue
  1893. # 获取有效库位信息
  1894. active_location = next(
  1895. (link.location for link in cd.container.active_location
  1896. if link.is_active),
  1897. None
  1898. )
  1899. container_map[cd.container_id] = {
  1900. 'detail': cd,
  1901. 'goods_qty': cd.goods_qty - cd.goods_out_qty,
  1902. 'container': cd.container,
  1903. 'location': active_location
  1904. }
  1905. # 转换为排序列表
  1906. container_list = list(container_map.values())
  1907. # 多维度排序(优化性能版)
  1908. sorted_containers = sorted(
  1909. container_list,
  1910. key=lambda x: (
  1911. self._get_goods_class_priority(x['detail'].goods_class),
  1912. -(x['location'].c_number if x['location'] else 0),
  1913. # -(x['location'].layer if x['location'] else 0),
  1914. # x['location'].row if x['location'] else 0,
  1915. # x['location'].col if x['location'] else 0
  1916. )
  1917. )
  1918. # 分配逻辑
  1919. required = demand['required']
  1920. for item in sorted_containers:
  1921. if required <= 0:
  1922. break
  1923. # 获取可分配数量
  1924. allocatable = item['goods_qty']
  1925. allocate_qty = min(required, allocatable)
  1926. # 记录分配信息
  1927. allocate_container = {
  1928. "container_number": item['container'].id,
  1929. "batch_id": batch_id,
  1930. "location_code": item['location'].location_code if item['location'] else 'N/A',
  1931. "allocate_qty": allocate_qty,
  1932. "c_number": item['location'].c_number if item['location'] else 0
  1933. }
  1934. return_data.append(allocate_container)
  1935. required -= allocate_qty
  1936. # 更新数据库状态(需要事务处理)
  1937. self._update_allocation_status(allocate_container, allocate_qty,bound_list_id)
  1938. # 降重 return_data,以container_number为key
  1939. return_data = list({v['container_number']: v for v in return_data}.values())
  1940. # 排序
  1941. return_data = sorted(return_data, key=lambda x: -x['c_number'])
  1942. return {"code": "200", "msg": "Success", "data": return_data}
  1943. except Exception as e:
  1944. logger.error(f"出库任务生成失败: {str(e)}", exc_info=True)
  1945. return {"code": "500", "msg": str(e)}
  1946. def _get_goods_class_priority(self, goods_class):
  1947. """货物类型优先级权重"""
  1948. return {
  1949. 3: 0, # 散盘最高
  1950. 1: 1, # 成品次之
  1951. 2: 2 # 空盘最低
  1952. }.get(goods_class, 99)
  1953. @transaction.atomic
  1954. def _update_allocation_status(self, allocate_container, allocate_qty,bound_list_id):
  1955. """事务化更新分配状态"""
  1956. try:
  1957. # 更新托盘明细
  1958. container_detail_all = ContainerDetailModel.objects.filter(
  1959. container_id=allocate_container['container_number'],
  1960. batch_id=allocate_container['batch_id'],
  1961. is_delete=False
  1962. ).all()
  1963. left_qty = 0
  1964. for cd in container_detail_all:
  1965. if left_qty - allocate_qty >= 0:
  1966. break
  1967. add_qty = min(allocate_qty-left_qty, cd.goods_qty - cd.goods_out_qty)
  1968. if add_qty == 0:
  1969. continue
  1970. left_qty += add_qty
  1971. last_out_qty = cd.goods_out_qty
  1972. cd.goods_out_qty += add_qty
  1973. print(f"{left_qty/25} 更新托盘 {cd.container.container_code} 批次 {cd.batch_id} 出库数量: {cd.goods_out_qty}")
  1974. cd.save()
  1975. out_batch_detail.objects.create(
  1976. out_bound_id=bound_list_id,
  1977. container_id=cd.container_id,
  1978. container_detail_id=cd.id,
  1979. out_goods_qty=add_qty,
  1980. last_out_goods_qty = last_out_qty,
  1981. working = 1,
  1982. is_delete = False
  1983. )
  1984. return True
  1985. except Exception as e:
  1986. logger.error(f"状态更新失败: {str(e)}")
  1987. return False
  1988. def create_or_update_container_operation(self,container_obj,batch_id,bound_id,to_location,goods_qty,goods_weight):
  1989. try:
  1990. container_operation_obj = ContainerOperationModel.objects.filter(container=container_obj,batch_id=batch_id,bound_id=bound_id,operation_type="outbound").first()
  1991. if container_operation_obj:
  1992. logger.info(f"[0]查询出库任务: {container_operation_obj.operation_type} ")
  1993. logger.info(f"更新出库任务: {container_obj.container_code} 批次 {batch_id} 出库需求: {bound_id} 数量: {goods_qty} 重量: {goods_weight}")
  1994. container_operation_obj.to_location = to_location
  1995. container_operation_obj.goods_qty = goods_qty
  1996. container_operation_obj.goods_weight = goods_weight
  1997. container_operation_obj.save()
  1998. else:
  1999. logger.info(f"创建出库任务: {container_obj.container_code} 批次 {batch_id} 出库需求: {bound_id} 数量: {goods_qty} 重量: {goods_weight}")
  2000. batch = BoundBatchModel.objects.filter(id=batch_id).first()
  2001. if not batch:
  2002. return {"code": "500", "msg": f"批次 {batch_id} 不存在"}
  2003. ContainerOperationModel.objects.create(
  2004. month = int(timezone.now().strftime("%Y%m")),
  2005. container = container_obj,
  2006. goods_code = batch.goods_code,
  2007. goods_desc = batch.goods_desc,
  2008. operation_type ="outbound",
  2009. batch_id = batch_id,
  2010. bound_id = bound_id,
  2011. goods_qty = goods_qty,
  2012. goods_weight = goods_weight,
  2013. from_location = container_obj.current_location,
  2014. to_location= to_location,
  2015. timestamp=timezone.now(),
  2016. operator="WMS",
  2017. memo=f"出库需求: {bound_id}, 批次: {batch_id}, 数量: {goods_qty}"
  2018. )
  2019. return {"code": "200", "msg": "Success"}
  2020. except Exception as e:
  2021. return {"code": "500", "msg": str(e)}
  2022. def update_container_detail_out_qty(self,container_obj,batch_id):
  2023. try:
  2024. logger.info(f"[1]更新托盘出库数量: {container_obj.container_code} 批次 {batch_id} ")
  2025. container_operation_obj = ContainerOperationModel.objects.filter(container=container_obj,batch_id=batch_id,operation_type="outbound").all()
  2026. if not container_operation_obj:
  2027. logger.error(f"[1]批次 {batch_id} 托盘 {container_obj.container_code} 无出库任务")
  2028. return {"code": "500", "msg": f"批次 {batch_id} 托盘 {container_obj.container_code} 无出库任务"}
  2029. container_detail_obj = ContainerDetailModel.objects.filter(container=container_obj,batch_id=batch_id,status=2,is_delete=False).first()
  2030. if not container_detail_obj:
  2031. logger.error(f"[1]批次 {batch_id} 托盘 {container_obj.container_code} 无批次信息")
  2032. return {"code": "500", "msg": f"批次 {batch_id} 托盘 {container_obj.container_code} 无批次信息"}
  2033. out_qty = 0
  2034. for obj in container_operation_obj:
  2035. out_qty += obj.goods_qty
  2036. if out_qty >= container_detail_obj.goods_qty:
  2037. out_qty = container_detail_obj.goods_qty
  2038. container_detail_obj.status = 3
  2039. break
  2040. if out_qty == 0:
  2041. logger.error(f"[1]批次 {batch_id} 托盘 {container_obj.container_code} 无出库数量")
  2042. return {"code": "500", "msg": f"批次 {batch_id} 托盘 {container_obj.container_code} 无出库数量"}
  2043. container_detail_obj.goods_out_qty = out_qty
  2044. container_detail_obj.save()
  2045. return {"code": "200", "msg": "Success"}
  2046. except Exception as e:
  2047. return {"code": "500", "msg": str(e)}
  2048. # 出库任务监测
  2049. class BatchViewSet(viewsets.ModelViewSet):
  2050. authentication_classes = [] # 禁用所有认证类
  2051. permission_classes = [AllowAny] # 允许任意访问
  2052. def wcs_post(self, request, *args, **kwargs):
  2053. data = self.request.data
  2054. logger.info(f"收到 WMS 推送数据: {data}")
  2055. return Response({"code": "200", "msg": "Success"}, status=200)
  2056. # views.py
  2057. class OutDetailViewSet(viewsets.ModelViewSet):
  2058. pagination_class = MyPageNumberPagination
  2059. serializer_class = OutBoundDetailSerializer
  2060. def get_project(self):
  2061. try:
  2062. id = self.kwargs.get('pk')
  2063. return id
  2064. except:
  2065. return None
  2066. def get_queryset(self):
  2067. """根据不同的action调整查询集"""
  2068. if self.action == 'list':
  2069. # 获取每个out_bound的最新一条记录
  2070. # 子查询,用于获取每个out_bound对应的最新out_batch_detail记录
  2071. subquery = out_batch_detail.objects.filter(
  2072. out_bound=OuterRef('out_bound')
  2073. ).order_by('-id')
  2074. # 返回最新的out_batch_detail记录,通过子查询的结果进行过滤
  2075. return out_batch_detail.objects.filter(
  2076. id=Subquery(subquery.values('id')[:1])
  2077. )
  2078. return out_batch_detail.objects.all()
  2079. def retrieve(self, request, *args, **kwargs):
  2080. """重写retrieve方法返回关联集合"""
  2081. qs = self.get_project()
  2082. queryset = self.filter_queryset(
  2083. out_batch_detail.objects.filter(out_bound = qs)
  2084. )
  2085. # 分页处理
  2086. # page = self.paginate_queryset(queryset)
  2087. # if queryset is not None:
  2088. # serializer = self.get_serializer(queryset, many=True)
  2089. # return self.get_paginated_response(serializer.data)
  2090. serializer = self.get_serializer(queryset, many=True)
  2091. return Response(serializer.data)
  2092. def get_serializer_class(self):
  2093. """根据action切换序列化器"""
  2094. if self.action == 'retrieve':
  2095. return OutBoundFullDetailSerializer
  2096. return super().get_serializer_class()
  2097. def get_out_batch_detail(self, request):
  2098. """获取某个托盘的出库明细"""
  2099. try:
  2100. container_code = request.query_params.get('container_code')
  2101. container_obj = ContainerListModel.objects.filter(container_code=container_code).first()
  2102. if not container_obj:
  2103. return Response({"code": "500", "message": f"托盘 {container_code} 不存在"}, status=status.HTTP_200_OK)
  2104. out_batch_detail_all = out_batch_detail.objects.filter(container=container_obj,working=1,is_delete=False).all()
  2105. if not out_batch_detail_all:
  2106. return Response({"code": "500", "message": f"托盘 {container_code} 无出库明细"}, status=status.HTTP_200_OK)
  2107. serializer = OutBoundFullDetailSerializer(out_batch_detail_all, many=True)
  2108. return Response({"code": "200", "message": "Success", "data": serializer.data}, status=status.HTTP_200_OK)
  2109. except Exception as e:
  2110. return Response({"code": "500", "message": str(e)}, status=status.HTTP_200_OK)
  2111. def confirm_out_batch_detail(self, request):
  2112. """确认出库"""
  2113. try:
  2114. container_code = request.query_params.get('container_code')
  2115. container_obj = ContainerListModel.objects.filter(container_code=container_code).first()
  2116. if not container_obj:
  2117. return Response({"code": "500", "message": f"托盘 {container_code} 不存在"}, status=status.HTTP_200_OK)
  2118. out_batch_detail_all = out_batch_detail.objects.filter(container=container_obj,working=1,is_delete=False).all()
  2119. if not out_batch_detail_all:
  2120. return Response({"code": "500", "message": f"托盘 {container_code} 无出库明细"}, status=status.HTTP_200_OK)
  2121. for obj in out_batch_detail_all:
  2122. obj.working = 0
  2123. obj.save()
  2124. BatchLogModel.objects.create(
  2125. batch_id = obj.container_detail.batch,
  2126. log_type = 1,
  2127. log_date = timezone.now(),
  2128. goods_code = obj.container_detail.batch.goods_code,
  2129. goods_desc = obj.container_detail.batch.goods_desc,
  2130. goods_qty = obj.out_goods_qty,
  2131. log_content = f"出库托盘 {container_code} 批次 {obj.container_detail.batch.id} 数量 {obj.out_goods_qty}",
  2132. creater = "WMS",
  2133. openid = "WMS"
  2134. )
  2135. return Response({"code": "200", "message": "出库成功"}, status=status.HTTP_200_OK)
  2136. except Exception as e:
  2137. return Response({"code": "500", "message": str(e)}, status=status.HTTP_200_OK)
  2138. def cancel_out_batch_detail(self, request):
  2139. """取消出库"""
  2140. try:
  2141. container_code = request.query_params.get('container_code')
  2142. container_obj = ContainerListModel.objects.filter(container_code=container_code).first()
  2143. if not container_obj:
  2144. return Response({"code": "500", "message": f"托盘 {container_code} 不存在"}, status=status.HTTP_200_OK)
  2145. out_batch_detail_all = out_batch_detail.objects.filter(container=container_obj,working=1,is_delete=False).order_by('-id').all()
  2146. if not out_batch_detail_all:
  2147. return Response({"code": "500", "message": f"托盘 {container_code} 无出库明细"}, status=status.HTTP_200_OK)
  2148. for obj in out_batch_detail_all:
  2149. obj.container_detail.goods_out_qty = obj.last_out_goods_qty
  2150. # print(f"取消出库托盘 {container_code} 批次 {obj.container_detail.batch.id},详情 {obj.container_detail.id} 变化前数量 {obj.container_detail.goods_out_qty} 变化后数量 {obj.last_out_goods_qty}")
  2151. obj.container_detail.save()
  2152. obj.is_delete = True
  2153. obj.working = 0
  2154. obj.save()
  2155. return Response({"code": "200", "message": "出库取消成功"}, status=status.HTTP_200_OK)
  2156. except Exception as e:
  2157. return Response({"code": "500", "message": str(e)}, status=status.HTTP_200_OK)
  2158. def get_contianer_detail(self, request):
  2159. try:
  2160. container_code = request.query_params.get('container_code')
  2161. container_obj = ContainerListModel.objects.filter(container_code=container_code).first()
  2162. if not container_obj:
  2163. return Response({"code": "500", "message": f"托盘 {container_code} 不存在"}, status=status.HTTP_200_OK)
  2164. container_detail_all = ContainerDetailModel.objects.filter(container=container_obj,is_delete=False).all().exclude(status__in=[3])
  2165. return_data=[]
  2166. for obj in container_detail_all:
  2167. return_data.append({
  2168. "id": obj.id,
  2169. "batch": obj.batch.bound_number,
  2170. "goods_code": obj.goods_code,
  2171. "goods_desc": obj.goods_desc,
  2172. "goods_qty" :obj.goods_qty,
  2173. "out_goods_qty": obj.goods_out_qty
  2174. })
  2175. return Response({"code": "200", "message": "Success", "data": return_data}, status=status.HTTP_200_OK)
  2176. except Exception as e:
  2177. return Response({"code": "500", "message": str(e)}, status=status.HTTP_200_OK)
  2178. def change_container_out_qty(self, request):
  2179. try:
  2180. container_code = request.data.get('container_code')
  2181. container_obj = ContainerListModel.objects.filter(container_code=container_code).first()
  2182. if not container_obj:
  2183. return Response({"code": "500", "message": f"托盘 {container_code} 不存在"}, status=status.HTTP_200_OK)
  2184. logger.info(f"change_container_out_qty: {request.data}")
  2185. for container_detail_id, out_qty in request.data.get('detail_list').items():
  2186. container_detail_obj = ContainerDetailModel.objects.filter(id=container_detail_id,is_delete=False).first()
  2187. if not container_detail_obj:
  2188. continue
  2189. from decimal import Decimal
  2190. out_qty = Decimal(out_qty)
  2191. container_detail_obj.goods_out_qty += out_qty
  2192. container_detail_obj.save()
  2193. return Response({"code": "200", "message": "Success"}, status=status.HTTP_200_OK)
  2194. except Exception as e:
  2195. return Response({"code": "500", "message": str(e)}, status=status.HTTP_200_OK)