| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Services;
- use App\DeliveryAppointmentCar;
- use App\Traits\ServiceAppAop;
- use App\ReceivingTask;
- use App\Utils\IdCreate;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Ramsey\Uuid\Uuid;
- class ReceivingTaskService
- {
- use ServiceAppAop;
- protected $modelClass = ReceivingTask::class;
- private $itemService;
- public function __construct(ReceivingTaskItemService $itemService)
- {
- $this->itemService = $itemService;
- }
- // 完结工单
- public function endTask($receivingTaskNumber)
- {
- ReceivingTask::query()->where('number', $receivingTaskNumber)->update(['status' => '完成', 'end_at' => now()]);
- }
- // 延时完成
- public function overtimeToComplete($receivingTaskNumber)
- {
- ReceivingTask::query()->where('number', $receivingTaskNumber)->update(['status' => '延时完结']);
- }
- // 标记任务进行中
- public function executionTask($receivingTaskNumber)
- {
- ReceivingTask::query()->where('number', $receivingTaskNumber)->update(['status' => '进行中']);
- }
- /**
- * 生成入库任务号
- * @param DeliveryAppointmentCar $deliveryAppointmentCar
- * @param $params
- * @return ReceivingTask
- */
- public function createReceivingTask(DeliveryAppointmentCar $deliveryAppointmentCar, $params): ReceivingTask
- {
- // 生成入库任务号
- $task_number = $this->buildTaskNumber();
- $params['number'] = $task_number;
- $params['delivery_appointment_car_id'] = $deliveryAppointmentCar->id; // 预约号
- $params['user_id'] = Auth::id();
- $receivingTask = new ReceivingTask($params);
- DB::transaction(function () use ($deliveryAppointmentCar, $params, &$receivingTask) {
- $receivingTask->save();
- if ($receivingTask->id) {
- $ans_number_string = $deliveryAppointmentCar->deliveryAppointment->asn_number ?? '';
- $ans_numbers = array_filter(preg_split('/[,, ]+/is', $ans_number_string));
- $ans_numbers = array_unique(array_merge($ans_numbers, $params['asn_nos'] ?? []));
- $this->itemService->createItems($receivingTask, $ans_numbers);
- $this->saveImage($receivingTask, $params['driving_license_image']);
- }
- });
- return $receivingTask;
- }
- // 保存图片
- public function saveImage(ReceivingTask $receivingTask, $image): bool
- {
- if (!$this->checkImage($image)) return false;
- $tmpFile = $image->getRealPath();
- $fileSuffix = $image->getClientOriginalExtension();
- $dirPath = $this->getStorageDirPath();
- $fileName = date('ymd') . '-' . Uuid::uuid1();
- $pathName = $dirPath . $fileName . '.' . $fileSuffix;
- $result = move_uploaded_file($tmpFile, $pathName);
- if (!$result) return false;
- $receivingTask->file()->create(
- ['url' => '/files/receivingTask/' . $fileName, 'type' => $fileSuffix, 'table_name' => $receivingTask->getTable()]
- );
- return true;
- }
- public function checkImage($image): bool
- {
- $tmpFile = $image->getRealPath();
- $fileSuffix = $image->getClientOriginalExtension();
- if (!is_uploaded_file($tmpFile)) return false;
- if ($image->getSize() > 5 * 1024 * 1024) return false;
- if (!in_array($fileSuffix, ['gif', 'image', 'jpeg', 'jpg', 'png', 'svg'])) return false;
- return true;
- }
- public function getStorageDirPath(): string
- {
- $path = ['app', 'public', 'files', 'receivingTask'];
- $path = join(DIRECTORY_SEPARATOR, $path);
- $dirPath = storage_path($path);
- if (!file_exists($dirPath)) {
- mkdir($dirPath);
- }
- return $dirPath . DIRECTORY_SEPARATOR;
- }
- /**
- * 雪花算法生成任务号
- * @return string
- */
- public function buildTaskNumber(): string
- {
- return 'SH' . IdCreate::createOnlyId();
- }
- /**
- * 对昨天未完成的单进行超时完成
- */
- public function overtimeToCompleteTask()
- {
- $yesterday = Carbon::now()->subDays(1)->format("Y-m-d");
- ReceivingTask::query()
- ->where('created_at', '>=', $yesterday . ' 00:00:00')
- ->where('created_at', '<=', $yesterday . ' 23:59:59')
- ->whereNull('end_at')
- ->whereIn('status', ['创建', '进行中'])
- ->update(['status' => '超时完成']);
- }
- }
|