ReceivingTaskService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Services;
  3. use App\DeliveryAppointment;
  4. use App\DeliveryAppointmentCar;
  5. use App\Traits\ServiceAppAop;
  6. use App\ReceivingTask;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\DB;
  9. use Ramsey\Uuid\Uuid;
  10. class ReceivingTaskService
  11. {
  12. use ServiceAppAop;
  13. protected $modelClass = ReceivingTask::class;
  14. private $itemService;
  15. public function __construct()
  16. {
  17. $this->itemService =app(ReceivingTaskItemService::class);
  18. }
  19. public function endTask($receivingTaskNumber)
  20. {
  21. ReceivingTask::query()->where('number', $receivingTaskNumber)->update(['status' => '延时完结']);
  22. }
  23. public function executionTask($receivingTaskNumber)
  24. {
  25. ReceivingTask::query()->where('number', $receivingTaskNumber)->update(['status' => '进行中']);
  26. }
  27. public function createReceivingTask(DeliveryAppointmentCar $delivery_appointment_car, $params): ReceivingTask
  28. {
  29. // 生成入库任务号
  30. $task_number = $this->buildTaskNumber();
  31. $params['number'] = $task_number;
  32. $params['delivery_appointment_car_id'] = $delivery_appointment_car->id; // 预约号
  33. $receivingTask = new ReceivingTask($params);
  34. DB::transaction(function () use ($delivery_appointment_car, $params, &$receivingTask) {
  35. $receivingTask->save();
  36. if ($receivingTask->id) {
  37. try {
  38. $this->itemService->createItems($receivingTask,$params['asn_nos'] ?? []);
  39. $this->saveImage($receivingTask, $params['driving_license_image']);
  40. DB::commit();
  41. } catch (\Exception $e) {
  42. DB::rollBack();
  43. }
  44. }
  45. });
  46. return $receivingTask;
  47. }
  48. public function saveImage(ReceivingTask $receivingTask, $image): bool
  49. {
  50. if (!$this->checkImage($image)) return false;
  51. $tmpFile = $image->getRealPath();
  52. $fileSuffix = $image->getClientOriginalExtension();
  53. $dirPath = $this->getStorageDirPath();
  54. $fileName = date('ymd') . '-' . Uuid::uuid1();
  55. $pathName = $dirPath . $fileName . '.' . $fileSuffix;
  56. $result = move_uploaded_file($tmpFile, $pathName);
  57. if (!$result) return false;
  58. $receivingTask->file()->create(
  59. ['url' => '/files/receivingTask/' . $fileName, 'type' => $fileSuffix, 'table_name' => $receivingTask->getTable()]
  60. );
  61. return true;
  62. }
  63. public function checkImage($image): bool
  64. {
  65. $tmpFile = $image->getRealPath();
  66. $fileSuffix = $image->getClientOriginalExtension();
  67. if (!is_uploaded_file($tmpFile)) return false;
  68. if ($image->getSize() > 5 * 1024 * 1024) return false;
  69. if (!in_array($fileSuffix, ['gif', 'image', 'jpeg', 'jpg', 'png', 'svg'])) return false;
  70. return true;
  71. }
  72. public function getStorageDirPath(): string
  73. {
  74. $path = ['app', 'public', 'files', 'receivingTask'];
  75. $path = join(DIRECTORY_SEPARATOR, $path);
  76. $dirPath = storage_path($path);
  77. if (!file_exists($dirPath)) {
  78. mkdir($dirPath);
  79. }
  80. return $dirPath . DIRECTORY_SEPARATOR;
  81. }
  82. public function buildTaskNumber(): string
  83. {
  84. $year_month_day = Carbon::now()->format("Ymd");
  85. $current = Carbon::now()->format("Y-m-d");
  86. $count = ReceivingTask::query()
  87. ->where('created_at', '>=', $current . ' 00:00:00')
  88. ->where('created_at', '<=', $current . ' 23.59.59')
  89. ->count();
  90. $count++;
  91. $count = sprintf('%03s', $count);
  92. return "SH$year_month_day" . $count;
  93. }
  94. /**
  95. * 完结昨天未完成的工单
  96. */
  97. public function endReceivingTask(){
  98. $yesterday = Carbon::now()->subDays(1)->format("Y-m-d");
  99. ReceivingTask::query()
  100. ->where('created_at', '>=', $yesterday . ' 00:00:00')
  101. ->where('created_at', '<=', $yesterday . ' 23.59.59')
  102. ->update(['status'=>'完成']);
  103. }
  104. }