WorkOrderService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Services;
  3. use App\Order;
  4. use App\OrderIssue;
  5. use App\OrderIssueType;
  6. use App\Traits\ServiceAppAop;
  7. use App\WorkOrder;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Support\Carbon;
  10. use Illuminate\Support\Facades\Auth;
  11. class WorkOrderService
  12. {
  13. use ServiceAppAop;
  14. protected $modelClass = WorkOrder::class;
  15. /**
  16. * 订单拦截 工单
  17. * @param $workOrderType
  18. * @param $uniquelyTags
  19. * @param int $status
  20. * @param int $grad
  21. * @return array
  22. */
  23. public function interceptOrder($workOrderType, $uniquelyTags, int $grad = 1, int $status = 1): array
  24. {
  25. app("OrderService")->syncOrderByCodes($uniquelyTags); // 同步订单
  26. $creator_id = Auth::user()['id']; // 创建人
  27. if (is_string($uniquelyTags)) $uniquelyTags = [$uniquelyTags];
  28. if (!$uniquelyTags) return ['success' => false, 'message' => '订单号为空'];
  29. $orders = Order::query()->whereIn('code', $uniquelyTags)->get();
  30. $created_at = Carbon::now();
  31. foreach ($uniquelyTags as $uniquelyTag) {
  32. $order = $orders->where('code', $uniquelyTag)->first();
  33. $params[] = [
  34. 'creator_id' => $creator_id,
  35. 'work_order_type_id' => $workOrderType->id,
  36. 'grad' => $grad,
  37. 'remark' => "{$workOrderType->name}:{$uniquelyTag}",
  38. 'outer_table_name' => $workOrderType->table_name ?? 'orders',
  39. 'order_id' => $order->id ?? '',
  40. 'uniquely_tag' => $uniquelyTag,
  41. 'status' => $status,
  42. 'created_at' => $created_at,
  43. ];
  44. }
  45. if (isset($params)) WorkOrder::query()->insert($params);
  46. return ['success' => true];
  47. }
  48. /**
  49. * 工单审核
  50. * @param $wordOrder
  51. * @return array
  52. */
  53. public function review($wordOrder): array
  54. {
  55. $wordOrder->update(['reviewer_id' => Auth::user()['id'], 'review_at' => Carbon::now(), 'status' => 2,]);
  56. $wordOrder = WorkOrder::query()->with(['type', 'creator', 'order' => function ($query) {
  57. /** @var $query Builder */
  58. $query->with('packages', 'issue','logistic');
  59. }, 'reviewer'])->find($wordOrder['id']);
  60. return ['success' => true, 'data' => $wordOrder];
  61. }
  62. /**
  63. * 拦截工单
  64. * 生成问题件
  65. * @param $work_orders
  66. * @return array|bool[]
  67. */
  68. public function createOrderIssue($work_orders): array
  69. {
  70. $order_ids = $work_orders->map(function ($item) {
  71. return $item->order_id;
  72. });
  73. $order_issues = OrderIssue::query()->with('order')->whereIn('order_id', $order_ids)->get();
  74. $codes = $order_issues->map(function ($item) {
  75. return $item->order->code;
  76. })->toArray();
  77. if (count($codes) > 0)
  78. return ['success' => false, 'message' => '对应【' . join(',', $codes) . '】订单已有问题件'];
  79. $orders = Order::query()->select('code')->whereIn('id', $order_ids)->get();
  80. $order_headers = app(OracleDOCOrderHeaderService::class)->getQuery()->whereIn('orderNo', $orders)->get(); // 获取
  81. $order_issue_type = OrderIssueType::query()->where('name', '拦截')->first(); // 问题件类型
  82. $result = app('OrderIssueService')->createOrderIssueByWmsOrder($order_headers, $order_issue_type->id, '拦截工单');
  83. if (!$result) return ['success' => false, 'message' => '生成对应问题件失败'];
  84. $work_orders = WorkOrder::query()->with(['type', 'creator', 'order' => function ($query) {
  85. /** @var $query Builder */
  86. $query->with('packages', 'issue','logistic');
  87. }, 'reviewer'])
  88. ->where('outer_table_name', 'orders')
  89. ->whereIn('order_id', $order_ids)
  90. ->get();
  91. return ['success' => true, 'data' => $work_orders];
  92. }
  93. /**
  94. * 按 指定工单类型 和 唯一标识 确认工单是否存在
  95. * @param $work_order_type_id
  96. * @param $uniquely_tag
  97. * @return array|false[]
  98. */
  99. public function exists($work_order_type_id, $uniquely_tag): array
  100. {
  101. $query = WorkOrder::query()->where('work_order_type_id', $work_order_type_id);
  102. if (is_array($uniquely_tag)) $query->whereIn('uniquely_tag', $uniquely_tag);
  103. if (is_string($uniquely_tag)) $query->where('uniquely_tag', $uniquely_tag);
  104. $items = $query->get();
  105. if (count($items) > 0) {
  106. $exists_nos = $items->map(function ($item) {
  107. return $item->uniquely_tag;
  108. });
  109. return ['success' => true, 'data' => $exists_nos];
  110. }
  111. return ['success' => false];
  112. }
  113. }