| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace App\Services;
- use App\DeliveryAppointment;
- use App\DeliveryAppointmentCar;
- use App\Traits\ServiceAppAop;
- use App\ReceivingTask;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use Ramsey\Uuid\Uuid;
- class ReceivingTaskService
- {
- use ServiceAppAop;
- protected $modelClass = ReceivingTask::class;
- private $itemService;
- public function __construct()
- {
- $this->itemService =app(ReceivingTaskItemService::class);
- }
- public function endTask($receivingTaskNumber)
- {
- ReceivingTask::query()->where('number', $receivingTaskNumber)->update(['status' => '延时完结']);
- }
- public function executionTask($receivingTaskNumber)
- {
- ReceivingTask::query()->where('number', $receivingTaskNumber)->update(['status' => '进行中']);
- }
- public function createReceivingTask(DeliveryAppointmentCar $delivery_appointment_car, $params): ReceivingTask
- {
- // 生成入库任务号
- $task_number = $this->buildTaskNumber();
- $params['number'] = $task_number;
- $params['delivery_appointment_car_id'] = $delivery_appointment_car->id; // 预约号
- $receivingTask = new ReceivingTask($params);
- DB::transaction(function () use ($delivery_appointment_car, $params, &$receivingTask) {
- $receivingTask->save();
- if ($receivingTask->id) {
- try {
- $this->itemService->createItems($receivingTask,$params['asn_nos'] ?? []);
- $this->saveImage($receivingTask, $params['driving_license_image']);
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- }
- }
- });
- 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;
- }
- public function buildTaskNumber(): string
- {
- $year_month_day = Carbon::now()->format("Ymd");
- $current = Carbon::now()->format("Y-m-d");
- $count = ReceivingTask::query()
- ->where('created_at', '>=', $current . ' 00:00:00')
- ->where('created_at', '<=', $current . ' 23.59.59')
- ->count();
- $count++;
- $count = sprintf('%03s', $count);
- return "SH$year_month_day" . $count;
- }
- /**
- * 完结昨天未完成的工单
- */
- public function endReceivingTask(){
- $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')
- ->update(['status'=>'完成']);
- }
- }
|