views.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. from rest_framework import viewsets
  2. from utils.page import MyPageNumberPagination
  3. from utils.datasolve import sumOfList, transportation_calculate
  4. from utils.md5 import Md5
  5. from rest_framework.filters import OrderingFilter
  6. from django_filters.rest_framework import DjangoFilterBackend
  7. from rest_framework.response import Response
  8. from rest_framework.exceptions import APIException
  9. from django.utils import timezone
  10. from django.db import transaction
  11. import logging
  12. from rest_framework import status
  13. from .models import DeviceModel,LocationModel,LocationGroupModel,LocationContainerLink,LocationChangeLog,ContainerDetailModel
  14. from bound.models import BoundBatchModel,BoundDetailModel,BoundListModel
  15. from .filter import DeviceFilter,LocationFilter,LocationContainerLinkFilter,LocationChangeLogFilter,LocationGroupFilter
  16. from .serializers import LocationListSerializer,LocationPostSerializer
  17. from .serializers import LocationGroupListSerializer,LocationGroupPostSerializer
  18. # 以后添加模块时,只需要在这里添加即可
  19. from rest_framework.permissions import AllowAny
  20. from container.models import ContainerListModel,ContainerDetailModel,ContainerOperationModel,TaskModel,
  21. logger = logging.getLogger(__name__)
  22. # 库位分配
  23. class LocationAllocation:
  24. @transaction.atomic
  25. def get_pallet_count_by_batch(self, container_code):
  26. """
  27. 根据托盘码查询批次下托盘总数
  28. :param container_code: 要查询的托盘码
  29. :return: 所属批次下的托盘总数
  30. """
  31. # 1. 通过托盘码获取容器详情
  32. container_detail = ContainerDetailModel.objects.filter(
  33. container_code=container_code
  34. ).select_related('bound_detail').first()
  35. if not container_detail:
  36. logger.error(f"托盘 {pallet_code} 不存在")
  37. # 2. 获取关联的批次明细
  38. bound_detail = container_detail.bound_detail
  39. if not bound_detail:
  40. logger.error(f"容器 {container_detail.container_code} 不存在")
  41. # 3. 获取父级批次
  42. batch = BoundBatchModel.objects.filter(
  43. batch_code=bound_detail.batch_code
  44. ).prefetch_related('bound_details').first()
  45. if not batch:
  46. logger.error(f"批次 {bound_detail.batch_code} 不存在")
  47. # 4. 统计批次下所有托盘数量(两种方式)
  48. # 方式一:直接统计关联明细数量(如果每个detail对应一个托盘)
  49. total_pallets = batch.bound_details.count()
  50. # 方式二:累加明细中的容器数量(如果detail包含多个容器)
  51. # total_pallets = sum(detail.container_count for detail in batch.bound_details.all())
  52. return {
  53. "batch_code": batch.batch_code,
  54. "total_pallets": total_pallets,
  55. "current_pallet": pallet_code
  56. }
  57. class locationViewSet(viewsets.ModelViewSet):
  58. """
  59. retrieve:
  60. Response a data list(get)
  61. list:
  62. Response a data list(all)
  63. create:
  64. Create a data line(post)
  65. delete:
  66. Delete a data line(delete)
  67. """
  68. # authentication_classes = [] # 禁用所有认证类
  69. # permission_classes = [AllowAny] # 允许任意访问
  70. pagination_class = MyPageNumberPagination
  71. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  72. ordering_fields = ['id', "create_time", "update_time", ]
  73. filter_class = LocationFilter
  74. def get_project(self):
  75. try:
  76. id = self.kwargs.get('pk')
  77. return id
  78. except:
  79. return None
  80. def get_queryset(self):
  81. id = self.get_project()
  82. if self.request.user:
  83. if id is None:
  84. return LocationModel.objects.filter()
  85. else:
  86. return LocationModel.objects.filter( id=id)
  87. else:
  88. return LocationModel.objects.none()
  89. def get_serializer_class(self):
  90. if self.action == 'list':
  91. return LocationListSerializer
  92. elif self.action == 'update':
  93. return LocationPostSerializer
  94. elif self.action =='retrieve':
  95. return LocationListSerializer
  96. def update(self, request, *args, **kwargs):
  97. data = self.request.data
  98. order_month = str(timezone.now().strftime('%Y%m'))
  99. data['month'] = order_month
  100. location_code = data.get('location_code')
  101. # 处理库位对象
  102. location_obj = LocationModel.objects.filter(location_code=location_code).first()
  103. if location_obj:
  104. data['id'] = location_obj.id
  105. logger.info(f"库位 {location_code} 已存在")
  106. else:
  107. logger.info(f"库位 {location_code} 不存在,创建库位对象")
  108. serializer_list = LocationPostSerializer(data=data)
  109. serializer_list.is_valid(raise_exception=True)
  110. serializer_list.save()
  111. data['id'] = serializer_list.data.get('id')
  112. return Response(data, status=status.HTTP_201_CREATED)
  113. class locationGroupViewSet(viewsets.ModelViewSet):
  114. """
  115. retrieve:
  116. Response a data list(get)
  117. list:
  118. Response a data list(all)
  119. create:
  120. Create a data line(post)
  121. delete:
  122. Delete a data line(delete)
  123. """
  124. # authentication_classes = [] # 禁用所有认证类
  125. # permission_classes = [AllowAny] # 允许任意访问
  126. pagination_class = MyPageNumberPagination
  127. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  128. ordering_fields = ['id', "create_time", "update_time", ]
  129. filter_class = LocationGroupFilter
  130. def get_project(self):
  131. try:
  132. id = self.kwargs.get('pk')
  133. return id
  134. except:
  135. return None
  136. def get_queryset(self):
  137. id = self.get_project()
  138. if self.request.user:
  139. if id is None:
  140. return LocationGroupModel.objects.filter()
  141. else:
  142. return LocationGroupModel.objects.filter(id=id)
  143. else:
  144. return LocationGroupModel.objects.none()
  145. def get_serializer_class(self):
  146. if self.action == 'list':
  147. return LocationGroupListSerializer
  148. elif self.action == 'update':
  149. return LocationGroupPostSerializer
  150. elif self.action =='retrieve':
  151. return LocationGroupListSerializer
  152. def update(self, request, *args, **kwargs):
  153. data = self.request.data
  154. order_month = str(timezone.now().strftime('%Y%m'))
  155. data['month'] = order_month
  156. group_code = data.get('group_code')
  157. # 处理库位组对象
  158. group_obj = LocationGroupModel.objects.filter(group_code=group_code).first()
  159. if group_obj:
  160. data['id'] = group_obj.id
  161. logger.info(f"库位组 {group_code} 已存在")
  162. else:
  163. logger.info(f"库位组 {group_code} 不存在,创建库位组对象")
  164. serializer_list = LocationGroupPostSerializer(data=data)
  165. serializer_list.is_valid(raise_exception=True)
  166. serializer_list.save()
  167. data['id'] = serializer_list.data.get('id')
  168. return Response(data, status=status.HTTP_201_CREATED)