| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace App\Services;
- use App\Order;
- use App\OrderIssue;
- use App\OrderIssueType;
- use App\Traits\ServiceAppAop;
- use App\WorkOrder;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\Auth;
- class WorkOrderService
- {
- use ServiceAppAop;
- protected $modelClass = WorkOrder::class;
- /**
- * 订单拦截 工单
- * @param $workOrderType
- * @param $uniquelyTags
- * @param int $status
- * @param int $grad
- * @return array
- */
- public function interceptOrder($workOrderType, $uniquelyTags, int $grad = 1, int $status = 1): array
- {
- app("OrderService")->syncOrderByCodes($uniquelyTags); // 同步订单
- $creator_id = Auth::user()['id']; // 创建人
- if (is_string($uniquelyTags)) $uniquelyTags = [$uniquelyTags];
- if (!$uniquelyTags) return ['success' => false, 'message' => '订单号为空'];
- $orders = Order::query()->whereIn('code', $uniquelyTags)->get();
- $created_at = Carbon::now();
- foreach ($uniquelyTags as $uniquelyTag) {
- $order = $orders->where('code', $uniquelyTag)->first();
- $params[] = [
- 'creator_id' => $creator_id,
- 'work_order_type_id' => $workOrderType->id,
- 'grad' => $grad,
- 'remark' => "{$workOrderType->name}:{$uniquelyTag}",
- 'outer_table_name' => $workOrderType->table_name ?? 'orders',
- 'order_id' => $order->id ?? '',
- 'uniquely_tag' => $uniquelyTag,
- 'status' => $status,
- 'created_at' => $created_at,
- ];
- }
- if (isset($params)) WorkOrder::query()->insert($params);
- return ['success' => true];
- }
- /**
- * 工单审核
- * @param $wordOrder
- * @return array
- */
- public function review($wordOrder): array
- {
- $wordOrder->update(['reviewer_id' => Auth::user()['id'], 'review_at' => Carbon::now(), 'status' => 2,]);
- $wordOrder = WorkOrder::query()->with(['type', 'creator', 'order' => function ($query) {
- /** @var $query Builder */
- $query->with('packages', 'issue','logistic');
- }, 'reviewer'])->find($wordOrder['id']);
- return ['success' => true, 'data' => $wordOrder];
- }
- /**
- * 拦截工单
- * 生成问题件
- * @param $work_orders
- * @return array|bool[]
- */
- public function createOrderIssue($work_orders): array
- {
- $order_ids = $work_orders->map(function ($item) {
- return $item->order_id;
- });
- $order_issues = OrderIssue::query()->with('order')->whereIn('order_id', $order_ids)->get();
- $codes = $order_issues->map(function ($item) {
- return $item->order->code;
- })->toArray();
- if (count($codes) > 0)
- return ['success' => false, 'message' => '对应【' . join(',', $codes) . '】订单已有问题件'];
- $orders = Order::query()->select('code')->whereIn('id', $order_ids)->get();
- $order_headers = app(OracleDOCOrderHeaderService::class)->getQuery()->whereIn('orderNo', $orders)->get(); // 获取
- $order_issue_type = OrderIssueType::query()->where('name', '拦截')->first(); // 问题件类型
- $result = app('OrderIssueService')->createOrderIssueByWmsOrder($order_headers, $order_issue_type->id, '拦截工单');
- if (!$result) return ['success' => false, 'message' => '生成对应问题件失败'];
- $work_orders = WorkOrder::query()->with(['type', 'creator', 'order' => function ($query) {
- /** @var $query Builder */
- $query->with('packages', 'issue','logistic');
- }, 'reviewer'])
- ->where('outer_table_name', 'orders')
- ->whereIn('order_id', $order_ids)
- ->get();
- return ['success' => true, 'data' => $work_orders];
- }
- /**
- * 按 指定工单类型 和 唯一标识 确认工单是否存在
- * @param $work_order_type_id
- * @param $uniquely_tag
- * @return array|false[]
- */
- public function exists($work_order_type_id, $uniquely_tag): array
- {
- $query = WorkOrder::query()->where('work_order_type_id', $work_order_type_id);
- if (is_array($uniquely_tag)) $query->whereIn('uniquely_tag', $uniquely_tag);
- if (is_string($uniquely_tag)) $query->where('uniquely_tag', $uniquely_tag);
- $items = $query->get();
- if (count($items) > 0) {
- $exists_nos = $items->map(function ($item) {
- return $item->uniquely_tag;
- });
- return ['success' => true, 'data' => $exists_nos];
- }
- return ['success' => false];
- }
- }
|