WorkOrderService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. namespace App\Services;
  3. use App\OrderIssue;
  4. use App\OrderIssueRejectedBill;
  5. use App\OrderIssueType;
  6. use App\OrderPackage;
  7. use App\Traits\ServiceAppAop;
  8. use App\WorkOrder;
  9. use App\WorkOrderDetail;
  10. use Illuminate\Support\Facades\Auth;
  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. $this->logService->createLog($detail, '完结', '完结工单');
  88. $workOrder->changeStatus('完成');
  89. $workOrder->update([
  90. 'owner_tag' => 0,
  91. 'logistic_tag' => 0,
  92. 'bao_shi_tag' => 0,
  93. ]);
  94. }
  95. /**
  96. * 商家完结工单
  97. * @param WorkOrderDetail $detail
  98. */
  99. public function ownerEndWorkOrderDetail(WorkOrderDetail $detail)
  100. {
  101. $this->logService->createLog($detail, '完结', '货主完结');
  102. $detail->change('完成', '完成', '待货主完结');
  103. $detail->workOrder->change('完成', '完成', '待货主完结');
  104. $detail->workOrder->update([
  105. 'owner_tag' => 0,
  106. 'logistic_tag' => 0,
  107. 'bao_shi_tag' => 0,
  108. ]);
  109. $this->detailService->endDetail($detail); // 标记为处理过
  110. }
  111. /**
  112. * 商家批量完结工单
  113. * @param $details
  114. */
  115. public function ownerBatchEndWorkOrderDetails($details){
  116. foreach ($details as $detail){
  117. $this->ownerEndWorkOrderDetail($detail);
  118. }
  119. }
  120. public function find($id): WorkOrder
  121. {
  122. /** @var WorkOrder $item */
  123. $item = WorkOrder::query()->where('id', $id)->first();
  124. return $item;
  125. }
  126. public function getDefaultWith($id)
  127. {
  128. return WorkOrder::query()->defaultWith()->find($id);
  129. }
  130. /**
  131. * 生成问题件
  132. * @param $work_orders
  133. * @return array
  134. */
  135. public function buildOrderIssue($work_orders): array
  136. {
  137. foreach ($work_orders as $work_order) {
  138. $inner_params[] = [
  139. 'order_id' => $work_order->order_id, 'order_issue_type_id' => $work_order->order_issue_type_id, 'result_explain' => $work_order->remark,
  140. ];
  141. }
  142. if (!isset($inner_params)) return ['success' => false, 'message' => '创建问题件失败'];
  143. try {
  144. return app('OrderIssueService')->buildOrderIssue($inner_params);
  145. } catch (\Exception $e) {
  146. return ['success' => false, 'message' => '刷新页面后重试'];
  147. }
  148. }
  149. /**
  150. * 按 指定工单类型 和 唯一标识 确认工单是否存在
  151. * @param $work_order_type_id
  152. * @param $uniquely_tag
  153. * @return array|false[]
  154. */
  155. public function exists($work_order_type_id, $uniquely_tag): array
  156. {
  157. $query = WorkOrder::query()->where('work_order_type_id', $work_order_type_id);
  158. if (is_array($uniquely_tag)) $query->whereIn('uniquely_tag', $uniquely_tag);
  159. if (is_string($uniquely_tag)) $query->where('uniquely_tag', $uniquely_tag);
  160. $items = $query->get();
  161. if (count($items) > 0) {
  162. $exists_nos = $items->map(function ($item) {
  163. return $item->uniquely_tag;
  164. });
  165. return ['success' => true, 'data' => $exists_nos];
  166. }
  167. return ['success' => false];
  168. }
  169. /**
  170. * 标记已有 问题件 的工单
  171. * @param $workOrders
  172. */
  173. public function tags(&$workOrders)
  174. {
  175. $order_ids = $workOrders->map(function ($item) {
  176. return $item->order_id;
  177. });
  178. $order_issues = OrderIssue::query()->whereIn('order_id', $order_ids)->get();
  179. foreach ($order_issues as $order_issue) {
  180. $codes[$order_issue->order_id] = true;
  181. }
  182. if (!isset($codes)) return;
  183. foreach ($workOrders as &$workOrder) {
  184. if (array_key_exists($workOrder->order_id, $codes)) $workOrder->is_issue_order = true;
  185. else $workOrder->is_issue_order = false;
  186. }
  187. }
  188. /**
  189. * 判断是否拦截工单 称重
  190. * @param $logistic_number
  191. * @return bool
  192. */
  193. public function isIntercept($logistic_number): bool
  194. {
  195. $package_query = OrderPackage::query()->select('order_id')->where('logistic_number', $logistic_number);
  196. $order_issue_query = OrderIssueType::query()->select('id')->where('name', '拦截');
  197. return WorkOrder::query()->whereIn('order_id', $package_query)->whereIn('order_issue_type_id', $order_issue_query)->exists();
  198. }
  199. /**
  200. * 标记工单
  201. * @param $orders
  202. */
  203. public function tagWorkOrder(&$orders)
  204. {
  205. $orderNos = data_get($orders, '*.orderno');
  206. $workOrders = WorkOrder::query()->with('order')->whereIn('order_id', function ($query) use ($orderNos) {
  207. $query->from('orders')->select('id')->whereIn('code', $orderNos);
  208. })->get();
  209. $tags = [];
  210. $workOrders->each(function ($workOrder) use (&$tags) {
  211. $order_code = $workOrder->order->code ?? null;
  212. if ($order_code) $tags[$order_code] = true;
  213. });
  214. foreach ($orders as &$order) {
  215. if (array_key_exists($order->orderno, $tags)) $order->is_work_order = true;
  216. else $order->is_work_order = false;
  217. }
  218. }
  219. /**
  220. * 校验工单是否存在
  221. * @param $nos
  222. * @return mixed
  223. */
  224. public function checkWorkOrder($nos)
  225. {
  226. return WorkOrder::query()->defaultWith()->whereIn('order_id', function ($query) use ($nos) {
  227. $query->from('orders')->selectRaw('id');
  228. if (is_array($nos))
  229. $query->whereIn('code', $nos);
  230. else {
  231. $query->where('code', $nos);
  232. }
  233. })->get();
  234. }
  235. /**
  236. * 同步订单
  237. * @param $order_no
  238. * @return mixed
  239. */
  240. public function syncOrder($order_no)
  241. {
  242. return $this->orderService->first(['code' => $order_no]);
  243. }
  244. /**
  245. * 承运商标记在处理
  246. * @param WorkOrderDetail $detail
  247. */
  248. public function logisticHandlerTag(WorkOrderDetail $detail)
  249. {
  250. $detail->logisticTagHandle();
  251. app(WorkOrderLogService::class)->createLog($detail,'处理','处理中');
  252. $last_status = $detail->last_status;
  253. $detail->change('承运商处理','承运商处理中',$last_status);
  254. $detail->workOrder->change('承运商处理','承运商处理中',$last_status);
  255. if ($detail->workOrder->work_order_status === '1'){
  256. $detail->workOrder->update([
  257. 'work_order_status' => 0,
  258. 'owner_tag' => 2,
  259. 'bao_shi_tag' => 0,
  260. 'logistic_tag' => 0,
  261. ]);
  262. }
  263. }
  264. public function syncWorkOrder($rejectedBill)
  265. {
  266. /** @var OrderIssue $order_issue */
  267. $orderIssueRejectedBill = OrderIssueRejectedBill::query()->where('logistic_number_return',$rejectedBill['logistic_number_return'])->first();
  268. $order_issue = OrderIssue::query()->find($orderIssueRejectedBill->order_issue_id);
  269. if (!$order_issue || $order_issue->rejecting_status !== '全部退回') return ;
  270. /** @var WorkOrderInterceptService $workOrderInterceptService */
  271. $workOrderInterceptService = app(WorkOrderInterceptService::class);
  272. /** @var WorkOrder $workOrder */
  273. $workOrder = WorkOrder::query()->where('order_id',$order_issue->order_id)->orderByDesc('created_at')->first();
  274. if (!$workOrder)return ;
  275. $workOrderInterceptService->autoReviewIntercept($workOrder);
  276. }
  277. /**
  278. * 每日工单定时任务timingTask
  279. */
  280. public function timingTask(){
  281. $this->updateBaoShiHandlerTask();
  282. $this->updateLogisticHandlerTask();
  283. }
  284. private function updateLogisticHandlerTask(){
  285. // 将当日承运商处理过 且 状态为`无`的 标记为滞留状态 `滞`
  286. WorkOrder::query()->where('status',3)
  287. ->where('logistic_tag',1)
  288. ->update(['logistic_tag' => 2]);
  289. }
  290. private function updateBaoShiHandlerTask(){
  291. // 将当日宝时处理过 且 状态为`无`的 标记为滞留状态 `滞`
  292. WorkOrder::query()->whereIn('status',[1,4])
  293. ->where('bao_shi_tag',1)
  294. ->update(['bao_shi_tag' => 2]);
  295. }
  296. }