WorkOrderService.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace App\Services;
  3. use App\OrderIssue;
  4. use App\OrderIssueType;
  5. use App\OrderPackage;
  6. use App\Traits\ServiceAppAop;
  7. use App\WorkOrder;
  8. use App\WorkOrderDetail;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\Gate;
  11. class WorkOrderService
  12. {
  13. use ServiceAppAop;
  14. protected $modelClass = WorkOrder::class;
  15. /**
  16. * @var WorkOrderLogService $logService
  17. * @var WorkOrderImageService $imageService
  18. * @var WorkOrderCommoditiesService $commoditiesService
  19. * @var WorkOrderDetailService $detailService
  20. * @var OrderIssueTypeService $issueTypeService
  21. * @var OrderService $orderService
  22. */
  23. private $logService;
  24. private $imageService;
  25. private $commoditiesService;
  26. private $detailService;
  27. private $issueTypeService;
  28. private $orderService;
  29. public function __construct()
  30. {
  31. $this->logService = app(WorkOrderLogService::class);
  32. $this->imageService = app(WorkOrderImageService::class);
  33. $this->commoditiesService = app(WorkOrderCommoditiesService::class);
  34. $this->detailService = app(WorkOrderDetailService::class);
  35. $this->issueTypeService = app(OrderIssueTypeService::class);
  36. $this->orderService = app(OrderService::class);
  37. }
  38. public function createOrResetWorkOrder($order, $issueType, $remark, $process_progress = '商家创建')
  39. {
  40. $user = Auth::user();
  41. $workOrder = WorkOrder::query()->where('order_id', $order->id)->orderByDesc('created_at')->first();
  42. if ($workOrder) {
  43. $this->detailService->undoneTagsByWorkOrder($workOrder);
  44. $workOrder->update([
  45. 'remark' => $remark,
  46. 'order_issue_type_id' => $issueType->id,
  47. 'creator_id' => $user['id'] ?? '',
  48. 'status' => 0,
  49. 'work_order_status' => 1,
  50. 'process_progress' => $process_progress,
  51. 'type' => $process_progress,
  52. 'last_handler_id' => $user['id'] ?? '',
  53. 'created_at' => now(),
  54. ]);
  55. return $workOrder;
  56. }
  57. return WorkOrder::query()->create([
  58. 'order_id' => $order->id,
  59. 'logistic_id' => $order->logistic_id ?? '',
  60. 'owner_id' => $order->owner_id ?? '',
  61. 'creator_id' => $user['id'] ?? '',
  62. 'remark' => $remark,
  63. 'outer_table_name' => 'orders',
  64. 'order_issue_type_id' => $issueType->id,
  65. 'uniquely_tag' => $order->code,
  66. 'status' => 0,
  67. 'work_order_status' => 1,
  68. 'process_progress' => $process_progress,
  69. 'type' => $process_progress,
  70. 'last_handler_id' => $user['id'] ?? '',
  71. ]);
  72. }
  73. public function createAndNotification($order, $orderIssueType, $remark, $process_progress = '商家创建'): WorkOrder
  74. {
  75. /** @var WorkOrder $workOrder */
  76. $workOrder = $this->createOrResetWorkOrder($order, $orderIssueType, $remark, $process_progress);
  77. $workOrder->notification();
  78. return $workOrder;
  79. }
  80. /**
  81. * @param WorkOrder $workOrder
  82. * @param WorkOrderDetail $detail
  83. */
  84. public function end(WorkOrder $workOrder, WorkOrderDetail $detail)
  85. {
  86. $this->detailService->endDetail($detail);
  87. $workOrder->clearWorkOrderStatus();
  88. $this->logService->createLog($detail, '完结', '完结工单');
  89. $workOrder->changeStatus('完成');
  90. }
  91. /**
  92. * 商家完结工单
  93. * @param WorkOrderDetail $detail
  94. */
  95. public function ownerEndWorkOrderDetail(WorkOrderDetail $detail)
  96. {
  97. $this->logService->createLog($detail, '完结', '货主完结');
  98. $detail->change('完成','完成','待货主完结');
  99. $detail->workOrder->change('完成','完成','待货主完结');
  100. $detail->workOrder->clearWorkOrderStatus(); // 清除创建标记
  101. $this->detailService->endDetail($detail); // 标记为处理过
  102. }
  103. /**
  104. * @param WorkOrder $workOrder
  105. * @param WorkOrderDetail $detail
  106. */
  107. public function logisticEnd(WorkOrder $workOrder, WorkOrderDetail $detail)
  108. {
  109. $this->logService->createLog($detail, '完结', '承运商完结工单');
  110. $this->detailService->endDetail($detail);
  111. $workOrder->clearWorkOrderStatus();
  112. $workOrder->changeStatus('完成');
  113. }
  114. public function find($id): WorkOrder
  115. {
  116. /** @var WorkOrder $item */
  117. $item = WorkOrder::query()->where('id', $id)->first();
  118. return $item;
  119. }
  120. public function getDefaultWith($id)
  121. {
  122. return WorkOrder::query()->defaultWith()->find($id);
  123. }
  124. /**
  125. * 生成问题件
  126. * @param $work_orders
  127. * @return array
  128. */
  129. public function buildOrderIssue($work_orders): array
  130. {
  131. foreach ($work_orders as $work_order) {
  132. $inner_params[] = [
  133. 'order_id' => $work_order->order_id, 'order_issue_type_id' => $work_order->order_issue_type_id, 'result_explain' => $work_order->remark,
  134. ];
  135. }
  136. if (!isset($inner_params)) return ['success' => false, 'message' => '创建问题件失败'];
  137. try {
  138. return app('OrderIssueService')->buildOrderIssue($inner_params);
  139. } catch (\Exception $e) {
  140. return ['success' => false, 'message' => '刷新页面后重试'];
  141. }
  142. }
  143. /**
  144. * 按 指定工单类型 和 唯一标识 确认工单是否存在
  145. * @param $work_order_type_id
  146. * @param $uniquely_tag
  147. * @return array|false[]
  148. */
  149. public function exists($work_order_type_id, $uniquely_tag): array
  150. {
  151. $query = WorkOrder::query()->where('work_order_type_id', $work_order_type_id);
  152. if (is_array($uniquely_tag)) $query->whereIn('uniquely_tag', $uniquely_tag);
  153. if (is_string($uniquely_tag)) $query->where('uniquely_tag', $uniquely_tag);
  154. $items = $query->get();
  155. if (count($items) > 0) {
  156. $exists_nos = $items->map(function ($item) {
  157. return $item->uniquely_tag;
  158. });
  159. return ['success' => true, 'data' => $exists_nos];
  160. }
  161. return ['success' => false];
  162. }
  163. /**
  164. * 标记已有 问题件 的工单
  165. * @param $workOrders
  166. */
  167. public function tags(&$workOrders)
  168. {
  169. $order_ids = $workOrders->map(function ($item) {
  170. return $item->order_id;
  171. });
  172. $order_issues = OrderIssue::query()->whereIn('order_id', $order_ids)->get();
  173. foreach ($order_issues as $order_issue) {
  174. $codes[$order_issue->order_id] = true;
  175. }
  176. if (!isset($codes)) return;
  177. foreach ($workOrders as &$workOrder) {
  178. if (array_key_exists($workOrder->order_id, $codes)) $workOrder->is_issue_order = true;
  179. else $workOrder->is_issue_order = false;
  180. }
  181. }
  182. /**
  183. * 判断是否拦截工单 称重
  184. * @param $logistic_number
  185. * @return bool
  186. */
  187. public function isIntercept($logistic_number): bool
  188. {
  189. $package_query = OrderPackage::query()->select('order_id')->where('logistic_number', $logistic_number);
  190. $order_issue_query = OrderIssueType::query()->select('id')->where('name', '拦截');
  191. return WorkOrder::query()->whereIn('order_id', $package_query)->whereIn('order_issue_type_id', $order_issue_query)->exists();
  192. }
  193. /**
  194. * 标记工单
  195. * @param $orders
  196. */
  197. public function tagWorkOrder(&$orders)
  198. {
  199. $orderNos = data_get($orders, '*.orderno');
  200. $workOrders = WorkOrder::query()->with('order')->whereIn('order_id', function ($query) use ($orderNos) {
  201. $query->from('orders')->select('id')->whereIn('code', $orderNos);
  202. })->get();
  203. $tags = [];
  204. $workOrders->each(function ($workOrder) use (&$tags) {
  205. $order_code = $workOrder->order->code ?? null;
  206. if ($order_code) $tags[$order_code] = true;
  207. });
  208. foreach ($orders as &$order) {
  209. if (array_key_exists($order->orderno, $tags)) $order->is_work_order = true;
  210. else $order->is_work_order = false;
  211. }
  212. }
  213. /**
  214. * 校验工单是否存在
  215. * @param $nos
  216. * @return mixed
  217. */
  218. public function checkWorkOrder($nos)
  219. {
  220. return WorkOrder::query()->defaultWith()->whereIn('order_id', function ($query) use ($nos) {
  221. $query->from('orders')->selectRaw('id');
  222. if (is_array($nos))
  223. $query->whereIn('code', $nos);
  224. else {
  225. $query->where('code', $nos);
  226. }
  227. })->get();
  228. }
  229. /**
  230. * 同步订单
  231. * @param $order_no
  232. * @return mixed
  233. */
  234. public function syncOrder($order_no)
  235. {
  236. return $this->orderService->first(['code' => $order_no]);
  237. }
  238. /**
  239. * 承运商标记在处理
  240. * @param WorkOrderDetail $detail
  241. */
  242. public function logisticHandlerTag(WorkOrderDetail $detail)
  243. {
  244. $detail->logisticTagHandle();
  245. }
  246. }