location_allocation.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from django.db import transaction
  2. import sys
  3. import os
  4. # 将当前脚本所在目录(bin)添加到 Python 路径
  5. current_dir = os.path.dirname(os.path.abspath(__file__))
  6. sys.path.append(current_dir)
  7. from .services import AllocationService
  8. from .exceptions import AllocationError
  9. from .models import *
  10. class LocationAllocation:
  11. """
  12. 库位分配
  13. functions:
  14. process(container_code, start_point): 处理托盘分配
  15. params: container_code: 托盘编号
  16. params: start_point: 起始库位
  17. return: dict
  18. release(location_code): 释放库位
  19. params: location_code: 库位编号
  20. return: bool
  21. """
  22. def __init__(self):
  23. self.service = AllocationService
  24. def process(self, container_code, start_point='203'):
  25. try:
  26. with transaction.atomic():
  27. return self.service.allocate(container_code, start_point)
  28. except Exception as e:
  29. raise AllocationError(f"分配失败: {str(e)}")
  30. def release(self, location_code):
  31. try:
  32. location = LocationModel.objects.get(location_code=location_code)
  33. link = LocationContainerLink.objects.get(
  34. location=location,
  35. is_active=True
  36. )
  37. link.is_active = False
  38. link.save()
  39. return True
  40. except Exception as e:
  41. raise AllocationError(f"释放失败: {str(e)}")