ReceivingTaskService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Services;
  3. use App\DeliveryAppointmentCar;
  4. use App\Traits\ServiceAppAop;
  5. use App\ReceivingTask;
  6. use App\Utils\IdCreate;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\DB;
  10. use Ramsey\Uuid\Uuid;
  11. class ReceivingTaskService
  12. {
  13. use ServiceAppAop;
  14. protected $modelClass = ReceivingTask::class;
  15. private $itemService;
  16. public function __construct(ReceivingTaskItemService $itemService)
  17. {
  18. $this->itemService = $itemService;
  19. }
  20. // 完结工单
  21. public function endTask($receivingTaskNumber)
  22. {
  23. ReceivingTask::query()->where('number', $receivingTaskNumber)->update(['status' => '完成', 'end_at' => now()]);
  24. }
  25. // 延时完成
  26. public function overtimeToComplete($receivingTaskNumber)
  27. {
  28. ReceivingTask::query()->where('number', $receivingTaskNumber)->update(['status' => '延时完结']);
  29. }
  30. // 标记任务进行中
  31. public function executionTask($receivingTaskNumber)
  32. {
  33. ReceivingTask::query()->where('number', $receivingTaskNumber)->update(['status' => '进行中']);
  34. }
  35. /**
  36. * 生成入库任务号
  37. * @param DeliveryAppointmentCar $deliveryAppointmentCar
  38. * @param $params
  39. * @return ReceivingTask
  40. */
  41. public function createReceivingTask(DeliveryAppointmentCar $deliveryAppointmentCar, $params): ReceivingTask
  42. {
  43. // 生成入库任务号
  44. $task_number = $this->buildTaskNumber();
  45. $params['number'] = $task_number;
  46. $params['delivery_appointment_car_id'] = $deliveryAppointmentCar->id; // 预约号
  47. $params['user_id'] = Auth::id();
  48. $receivingTask = new ReceivingTask($params);
  49. DB::transaction(function () use ($deliveryAppointmentCar, $params, &$receivingTask) {
  50. $receivingTask->save();
  51. if ($receivingTask->id) {
  52. $ans_number_string = $deliveryAppointmentCar->deliveryAppointment->asn_number ?? '';
  53. $ans_numbers = array_filter(preg_split('/[,, ]+/is', $ans_number_string));
  54. $ans_numbers = array_unique(array_merge($ans_numbers, $params['asn_nos'] ?? []));
  55. $this->itemService->createItems($receivingTask, $ans_numbers);
  56. $this->saveImage($receivingTask, $params['driving_license_image']);
  57. }
  58. });
  59. return $receivingTask;
  60. }
  61. // 保存图片
  62. public function saveImage(ReceivingTask $receivingTask, $image): bool
  63. {
  64. if (!$this->checkImage($image)) return false;
  65. $tmpFile = $image->getRealPath();
  66. $fileSuffix = $image->getClientOriginalExtension();
  67. $dirPath = $this->getStorageDirPath();
  68. $fileName = date('ymd') . '-' . Uuid::uuid1();
  69. $pathName = $dirPath . $fileName . '.' . $fileSuffix;
  70. $result = move_uploaded_file($tmpFile, $pathName);
  71. if (!$result) return false;
  72. $receivingTask->file()->create(
  73. ['url' => '/files/receivingTask/' . $fileName, 'type' => $fileSuffix, 'table_name' => $receivingTask->getTable()]
  74. );
  75. return true;
  76. }
  77. public function checkImage($image): bool
  78. {
  79. $tmpFile = $image->getRealPath();
  80. $fileSuffix = $image->getClientOriginalExtension();
  81. if (!is_uploaded_file($tmpFile)) return false;
  82. if ($image->getSize() > 5 * 1024 * 1024) return false;
  83. if (!in_array($fileSuffix, ['gif', 'image', 'jpeg', 'jpg', 'png', 'svg'])) return false;
  84. return true;
  85. }
  86. public function getStorageDirPath(): string
  87. {
  88. $path = ['app', 'public', 'files', 'receivingTask'];
  89. $path = join(DIRECTORY_SEPARATOR, $path);
  90. $dirPath = storage_path($path);
  91. if (!file_exists($dirPath)) {
  92. mkdir($dirPath);
  93. }
  94. return $dirPath . DIRECTORY_SEPARATOR;
  95. }
  96. /**
  97. * 雪花算法生成任务号
  98. * @return string
  99. */
  100. public function buildTaskNumber(): string
  101. {
  102. return 'SH' . IdCreate::createOnlyId();
  103. }
  104. /**
  105. * 对昨天未完成的单进行超时完成
  106. */
  107. public function overtimeToCompleteTask()
  108. {
  109. $yesterday = Carbon::now()->subDays(1)->format("Y-m-d");
  110. ReceivingTask::query()
  111. ->where('created_at', '>=', $yesterday . ' 00:00:00')
  112. ->where('created_at', '<=', $yesterday . ' 23:59:59')
  113. ->whereNull('end_at')
  114. ->whereIn('status', ['创建', '进行中'])
  115. ->update(['status' => '超时完成']);
  116. }
  117. }