TaskController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\validate\sys_manage\TaskValidate;
  4. use app\controller\Curd;
  5. use app\model\Appointment;
  6. use app\model\OrderSheet;
  7. use app\model\SysCategory;
  8. use app\model\Task;
  9. use app\model\TaskProcess;
  10. use support\Db;
  11. use support\Request;
  12. use support\Response;
  13. use Tinywan\Jwt\JwtToken;
  14. class TaskController extends Curd
  15. {
  16. public function __construct()
  17. {
  18. $this->model = new Task();
  19. $this->validate = true;
  20. $this->validateClass = new TaskValidate();
  21. }
  22. /**
  23. * @Desc 列表查询
  24. * @Author Gorden
  25. * @Date 2024/4/7 15:52
  26. *
  27. * @param Request $request
  28. * @return Response
  29. * @throws \support\exception\BusinessException
  30. */
  31. public function select(Request $request): Response
  32. {
  33. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  34. $order = $request->get('order', 'desc');
  35. $field = $field ?? 'task_addtimes';
  36. $query = $this->doSelect($where, $field, $order);
  37. return $this->doFormat($query, $format, $limit);
  38. }
  39. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  40. {
  41. $model = $this->model->with([
  42. 'member' => function ($query) {
  43. $query->select('member_id', 'member_mobile');
  44. },
  45. 'user' => function ($query) {
  46. $query->select('user_name', 'user_login_name', 'user_id');
  47. },
  48. 'cert'
  49. ]);
  50. foreach ($where as $column => $value) {
  51. if (is_array($value)) {
  52. if ($value[0] === 'like' || $value[0] === 'not like') {
  53. $model = $model->where($column, $value[0], "%$value[1]%");
  54. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  55. $model = $model->where($column, $value[0], $value[1]);
  56. } elseif ($value[0] == 'in' && !empty($value[1])) {
  57. $valArr = $value[1];
  58. if (is_string($value[1])) {
  59. $valArr = explode(",", trim($value[1]));
  60. }
  61. $model = $model->whereIn($column, $valArr);
  62. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  63. $valArr = $value[1];
  64. if (is_string($value[1])) {
  65. $valArr = explode(",", trim($value[1]));
  66. }
  67. $model = $model->whereNotIn($column, $valArr);
  68. } elseif ($value[0] == 'null') {
  69. $model = $model->whereNull($column);
  70. } elseif ($value[0] == 'not null') {
  71. $model = $model->whereNotNull($column);
  72. } elseif ($value[0] !== '' || $value[1] !== '') {
  73. $model = $model->whereBetween($column, $value);
  74. }
  75. } else {
  76. $model = $model->where($column, $value);
  77. }
  78. }
  79. if ($field) {
  80. $model = $model->orderBy($field, $order);
  81. }
  82. return $model;
  83. }
  84. /**
  85. * @Desc 工单详情
  86. * @Author Gorden
  87. * @Date 2024/4/7 16:15
  88. *
  89. * @param Request $request
  90. * @return Response
  91. */
  92. public function info(Request $request): Response
  93. {
  94. if ($this->validate && !$this->validateClass->scene('info')->check(['task_id' => $request->get('id')])) {
  95. return json_fail($this->validateClass->getError());
  96. }
  97. $data = $this->model->with([
  98. 'member' => function ($query) {
  99. $query->select('member_id', 'member_mobile');
  100. },
  101. 'user' => function ($query) {
  102. $query->select('user_name', 'user_login_name', 'user_id');
  103. },
  104. 'memberInfo',
  105. 'cert'
  106. ])->where('task_id', $request->get('id'))
  107. ->first();
  108. if (!empty($data)) {
  109. $data = $data->toArray();
  110. }
  111. // 查订单详情
  112. if (!empty($data['join_task_order_id'])) {
  113. $order = OrderSheet::with([
  114. 'goods' => function ($query) {
  115. $query->select('goods_id', 'goods_name', 'goods_market_price', 'goods_sales_price', 'goods_status', 'join_goods_category_id', 'goods_cover');
  116. },
  117. 'order' => function ($query) {
  118. $query->select('order_id', 'order_name', 'order_amount_total', 'order_amount_pay', 'order_category', 'order_status_system', 'order_status_payment', 'order_status_storage', 'order_addtimes');
  119. },
  120. 'running'
  121. ])->where('join_sheet_order_id', $data['join_task_order_id'])
  122. ->first();
  123. if ($order->goods) {
  124. $order->goods->goods_cover = getenv('STORAGE_DOMAIN') . $order->goods->goods_cover;
  125. if (!empty($order->goods->join_goods_category_id)) {
  126. $order->goods->join_goods_category_id = SysCategory::where('category_id', $order->goods->join_goods_category_id)->value('category_name');
  127. }
  128. $data['order'] = $order ?? null;
  129. }
  130. }
  131. // 查预约详情
  132. if (!empty($data['join_task_appointment_id'])) {
  133. $appointment = Appointment::with([
  134. 'goods' => function ($query) {
  135. $query->select('goods_id', 'goods_name', 'goods_market_price', 'goods_sales_price', 'goods_status', 'join_goods_category_id', 'goods_cover');
  136. },
  137. 'member' => function ($query) {
  138. $query->select('member_id', 'member_mobile');
  139. },
  140. 'memberInfo',
  141. 'cert',
  142. 'running'
  143. ])->where('appointment_id', $data['join_task_appointment_id'])
  144. ->first();
  145. if ($appointment) {
  146. if (!empty($appointment->goods->join_goods_category_id)) {
  147. $appointment->goods->join_goods_category_id = SysCategory::where('category_id', $appointment->goods->join_goods_category_id)->value('category_name');
  148. }
  149. $appointment->goods->goods_cover = getenv('STORAGE_DOMAIN') . $appointment->goods->goods_cover;
  150. $data['appointment'] = $appointment ?? null;
  151. }
  152. }
  153. // 查询工单处理记录
  154. $process = TaskProcess::with('user')
  155. ->where('join_process_task_id',$request->get('id'))
  156. ->get()
  157. ->toArray();
  158. $data['process'] = $process;
  159. return json_success('', $data);
  160. }
  161. /**
  162. * @Desc 处理工单
  163. * @Author Gorden
  164. * @Date 2024/4/8 10:30
  165. *
  166. * @param Request $request
  167. * @return Response
  168. */
  169. public function process(Request $request)
  170. {
  171. $params = $request->post();
  172. $params['task_id'] = $request->post('id');
  173. if ($this->validate && !$this->validateClass->scene('process')->check($params)) {
  174. return json_fail($this->validateClass->getError());
  175. }
  176. Db::beginTransaction();
  177. try {
  178. // 更新工单状态
  179. Task::where('task_id', $params['task_id'])->update(['task_status' => $params['task_status']]);
  180. // 创建处理记录
  181. $process = new TaskProcess();
  182. $process->join_process_task_id = $params['task_id'];
  183. $process->join_task_process_user_id = JwtToken::getCurrentId();
  184. $process->task_process_status = $params['task_process_status'];
  185. $process->task_process_category = $params['task_process_category'] ?? null;
  186. $process->task_process_begin_datetimes = date('Y-m-d H:i:s', strtotime($params['task_process_datetimes'][0])) ?? null;
  187. $process->task_process_end_datetimes = date('Y-m-d H:i:s', strtotime($params['task_process_datetimes'][1])) ?? null;
  188. $process->task_process_json = $params['task_process_json'] ?? null;
  189. $process->task_process_config_json = $params['task_process_config_json'] ?? null;
  190. $process->task_process_notify_json = $params['task_process_notify_json'] ?? null;
  191. $process->task_process_remark = $params['task_process_remark'] ?? null;
  192. $process->task_process_addtimes = time();
  193. $process->save();
  194. // 写日志
  195. _syslog("工单状态变更", "工单状态变更为:" . $params['task_status']);
  196. _syslog("工单处理", "处理状态:" . $process->task_process_status . "; 处理分类:" . $process->task_process_category ?? '无');
  197. Db::commit();
  198. return json_success('success');
  199. } catch (\Exception $e) {
  200. dump($e->getMessage());
  201. return json_fail('工单处理失败');
  202. }
  203. }
  204. /**
  205. * @Desc 操作日志
  206. * @Author Gorden
  207. * @Date 2024/4/8 11:04
  208. *
  209. * @param Request $request
  210. * @return Response
  211. */
  212. public function processLog(Request $request)
  213. {
  214. if ($this->validate && !$this->validateClass->scene('info')->check(['task_id' => $request->get('id')])) {
  215. return json_fail($this->validateClass->getError());
  216. }
  217. $taskId = $request->get('id');
  218. $log = TaskProcess::with('user')
  219. ->where('join_process_task_id', $taskId)
  220. ->get()
  221. ->toArray();
  222. foreach ($log as &$item) {
  223. $item['datetimes'] = $item['task_process_begin_datetimes'] . ' ~ ' . $item['task_process_end_datetimes'];
  224. }
  225. return json_success('', $log);
  226. }
  227. }