WorkOrderController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Filters\WorkOrderFilter;
  4. use App\Http\Resources\WarningLogResource;
  5. use App\Http\Resources\WorkOrderResource;
  6. use App\Models\AdminMerchant;
  7. use App\Models\AdminUser;
  8. use App\Models\AdminUserArea;
  9. use App\Models\Area;
  10. use App\Models\Bike;
  11. use App\Models\WarningLog;
  12. use App\Models\WorkOrder;
  13. use App\Utils\Admin;
  14. use Carbon\Carbon;
  15. use Illuminate\Http\Request;
  16. use App\Http\Controllers\Controller;
  17. use Illuminate\Support\Facades\Log;
  18. /**
  19. * Class WorkOrderController
  20. * @package App\Http\Controllers\Admin
  21. */
  22. class WorkOrderController extends Controller
  23. {
  24. /**
  25. * index 工单列表
  26. *
  27. * @param Request $request
  28. * @param WorkOrderFilter $filter
  29. * @return \Illuminate\Http\JsonResponse *@author Fx
  30. *
  31. */
  32. public function index(Request $request, WorkOrderFilter $filter)
  33. {
  34. $admin_id = Admin::user()->id;
  35. $workOrder = WorkOrder::query()
  36. ->filter($filter)
  37. ->where(AdminMerchant::getMerchantWhere())
  38. ->orderByDesc('id');
  39. //->orderByDesc('times');
  40. if (!Admin::isAdministrator()) {
  41. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  42. if (count($area_ids) !== 0) {
  43. $workOrder = $workOrder->whereIn('area_id', $area_ids);
  44. } else {
  45. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  46. $area_id = $area_id->area_id ?? 0;
  47. $workOrder = $workOrder->where('area_id', $area_id);
  48. }
  49. }
  50. $workOrder = $request->get('all') ? $workOrder->get() : $workOrder->paginate();
  51. return $this->ok(WorkOrderResource::collection($workOrder));
  52. }
  53. /**
  54. * workOrderType 工单类型
  55. *
  56. * @return \Illuminate\Http\JsonResponse *@author Fx
  57. *
  58. */
  59. public function workOrderType()
  60. {
  61. $type = WorkOrder::$typeMaps;
  62. $data = [];
  63. foreach ($type as $k => $v) {
  64. $arr = [];
  65. $arr['id'] = $k;
  66. $arr['name'] = $v;
  67. $data[] = $arr;
  68. }
  69. return $this->ok($data);
  70. }
  71. /**
  72. * addWorkOrder 添加工单
  73. *
  74. * @param Request $request
  75. * @return \Illuminate\Http\JsonResponse *@author Fx
  76. *
  77. */
  78. public function addWorkOrder(Request $request)
  79. {
  80. $reason = $request->get('reason') ?? '';
  81. $bike_no = $request->get('bike_no') ?? '';
  82. $type = $request->has('type');
  83. if (empty($reason) || empty($bike_no) || !$type) return $this->error('参数错误');
  84. $type = $request->get('type');
  85. $bike = Bike::where('bike_no', $bike_no)->first();
  86. if (empty($bike)) {
  87. Log::error('找不到车辆相关信息,车辆编号为' . $bike_no);
  88. return $this->error('找不到车辆相关信息');
  89. }
  90. $data = [
  91. 'work_no' => WorkOrder::makeWorkNo(),
  92. 'bike_no' => $bike_no,
  93. 'bike_id' => $bike->id,
  94. 'source' => WorkOrder::SOURCE_ADMIN,
  95. 'planned' => WorkOrder::PLANNED_STATUS_MEET,
  96. 'area_id' => $bike->put_area_id,
  97. 'type' => $type,
  98. 'reason' => $reason,
  99. 'type_name' => WorkOrder::$typeMaps[$type],
  100. 'insert_time' => Carbon::now(),
  101. 'merchant_id' => AdminMerchant::putMerchantId(),
  102. ];
  103. // 验证是否存在数据
  104. $verify = [
  105. 'type' => $type,
  106. 'bike_id' => $bike->id,
  107. 'status' => WorkOrder::STATUS_NO,
  108. ];
  109. $workOrder = WorkOrder::firstOrCreate($verify, $data);
  110. if ($workOrder->wasRecentlyCreated) {
  111. // 新用户处理
  112. return $this->ok('添加成功');
  113. } else {
  114. return $this->error('已经存在该类型工单,无需重复添加');
  115. }
  116. }
  117. /**
  118. * mulOverWorkOrder 批量设为已完成
  119. *
  120. * @param Request $request
  121. * @return \Illuminate\Http\JsonResponse *@author Fx
  122. *
  123. */
  124. public function mulOverWorkOrder(Request $request)
  125. {
  126. $workOrderIds = $request->get('ids') ?? [];
  127. if (empty($workOrderIds)) return $this->error('参数错误');
  128. $admin_id = Admin::user()->id;
  129. try {
  130. WorkOrder::whereIn('id', $workOrderIds)->where('status', WorkOrder::STATUS_NO)->update([
  131. 'planned' => WorkOrder::PLANNED_STATUS_OVER,
  132. 'status' => WorkOrder::STATUS_OK,
  133. 'fix_start_time' => Carbon::now(),
  134. 'worker_id' => $admin_id,
  135. 'work_over_id' => $admin_id,
  136. 'fix_end_time' => Carbon::now(),
  137. ]);
  138. } catch (\Exception $exception) {
  139. Log::error($exception->getMessage());
  140. return $this->error('操作失败,请联系管理员');
  141. }
  142. return $this->ok('操作成功');
  143. }
  144. /**
  145. * mulAssignWorkOrder 批量指派
  146. *
  147. * @param Request $request
  148. * @return \Illuminate\Http\JsonResponse *@author Fx
  149. *
  150. */
  151. public function mulAssignWorkOrder(Request $request)
  152. {
  153. $workOrderIds = $request->get('ids') ?? [];
  154. $worker_id = $request->get('worker_id') ?? '';
  155. if (empty($workOrderIds) || empty($worker_id)) return $this->error('参数错误');
  156. $admin_id = Admin::user()->id;
  157. try {
  158. WorkOrder::whereIn('id', $workOrderIds)->where('status', WorkOrder::STATUS_NO)->where('planned', WorkOrder::PLANNED_STATUS_MEET)->update([
  159. 'planned' => WorkOrder::PLANNED_STATUS_WORK,
  160. 'fix_start_time' => Carbon::now(),
  161. 'worker_id' => $worker_id,
  162. 'work_over_id' => $admin_id,
  163. ]);
  164. } catch (\Exception $exception) {
  165. Log::error($exception->getMessage());
  166. return $this->error('操作失败,请联系管理员');
  167. }
  168. return $this->ok('操作成功');
  169. }
  170. /**
  171. * getWarningByWorkOrderId 根据工单id获取警告日志
  172. *
  173. * @param Request $request
  174. * @return \Illuminate\Http\JsonResponse
  175. * @author Fx
  176. *
  177. */
  178. public function getWarningByWorkOrderId(Request $request)
  179. {
  180. $workOrderId = $request->get('id');
  181. $is_limit_type = $request->get('is_limit_type', 1);
  182. $workOrder = WorkOrder::find($workOrderId);
  183. if (empty($workOrder)) return $this->error('找不到工单');
  184. $warning = WarningLog::query()
  185. ->where('bike_no', $workOrder->bike_no);
  186. if ((bool)$is_limit_type) {
  187. // Log::info($is_limit_type);
  188. $warning = $warning
  189. ->where('type', $workOrder->warning_type);
  190. }
  191. $warning = $warning
  192. ->where('created_at', '>', Carbon::parse($workOrder->created_at)->subMinutes(5))
  193. ->orderByDesc('id')
  194. ->limit(300)
  195. ->get();
  196. return $this->ok(WarningLogResource::collection($warning));
  197. }
  198. }