InspectService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. namespace app\common\Service;
  3. use think\Db;
  4. use app\common\enum\OrderEnum;
  5. class InspectService
  6. {
  7. /**
  8. * 获取验货选项列表
  9. * @param array $params 查询参数
  10. * @return array
  11. */
  12. public static function getInspectionItems($params = [])
  13. {
  14. $where = [];
  15. // 父级ID筛选
  16. if (isset($params['parent_id'])) {
  17. $where['parent_id'] = $params['parent_id'];
  18. }
  19. // 状态筛选
  20. if (isset($params['status']) && $params['status'] !== '') {
  21. $where['status'] = $params['status'];
  22. }
  23. // 是否必检项筛选
  24. if (isset($params['is_required']) && $params['is_required'] !== '') {
  25. $where['is_required'] = $params['is_required'];
  26. }
  27. // 名称搜索
  28. if (isset($params['name']) && $params['name'] !== '') {
  29. $where['name'] = ['like', '%' . $params['name'] . '%'];
  30. }
  31. $query = (new \app\common\model\inspection\Item())->where($where)
  32. ->field('id,name,parent_id,is_required,status,standard_desc');
  33. // 排序
  34. $order = isset($params['order']) ? $params['order'] : 'id ASC';
  35. $query->order($order);
  36. // 分页处理
  37. if (isset($params['page']) && isset($params['page_size'])) {
  38. $result = $query->paginate([
  39. 'page' => $params['page'],
  40. 'list_rows' => $params['page_size']
  41. ]);
  42. return $result;
  43. }
  44. return $query->select();
  45. }
  46. /**
  47. * 获取树形结构的验货选项
  48. * @param array $params 查询参数
  49. * @return array
  50. */
  51. public function getInspectionItemTree($params = [])
  52. {
  53. // 获取所有选项
  54. $allItems = $this->getInspectionItems($params);
  55. if (is_object($allItems)) {
  56. // 分页对象转数组
  57. $allItems = $allItems->toArray()['data'];
  58. }
  59. return $this->buildTree($allItems);
  60. }
  61. /**
  62. * 构建树形结构
  63. * @param array $items 选项数组
  64. * @param int $parentId 父级ID
  65. * @return array
  66. */
  67. private function buildTree($items, $parentId = 0)
  68. {
  69. $tree = [];
  70. foreach ($items as $item) {
  71. if ($item['parent_id'] == $parentId) {
  72. $children = $this->buildTree($items, $item['id']);
  73. if (!empty($children)) {
  74. $item['children'] = $children;
  75. }
  76. $tree[] = $item;
  77. }
  78. }
  79. return $tree;
  80. }
  81. /**
  82. * 根据ID获取验货选项详情
  83. * @param int $id 选项ID
  84. * @return array|null
  85. */
  86. public function getInspectionItemById($id)
  87. {
  88. return Db::name('inspection_item')
  89. ->where('id', $id)
  90. ->where('deletetime', 'exp', 'IS NULL')
  91. ->find();
  92. }
  93. /**
  94. * 获取必检项列表
  95. * @param array $params 查询参数
  96. * @return array
  97. */
  98. public function getRequiredItems($params = [])
  99. {
  100. $params['is_required'] = 1;
  101. return $this->getInspectionItems($params);
  102. }
  103. /**
  104. * 获取顶级选项(parent_id = 0)
  105. * @param array $params 查询参数
  106. * @return array
  107. */
  108. public function getTopLevelItems($params = [])
  109. {
  110. $params['parent_id'] = 0;
  111. return $this->getInspectionItems($params);
  112. }
  113. /**
  114. * 获取指定父级下的子选项
  115. * @param int $parentId 父级ID
  116. * @param array $params 其他查询参数
  117. * @return array
  118. */
  119. public function getChildItems($parentId, $params = [])
  120. {
  121. $params['parent_id'] = $parentId;
  122. return $this->getInspectionItems($params);
  123. }
  124. /**
  125. * 搜索验货选项
  126. * @param string $keyword 搜索关键词
  127. * @param array $params 其他查询参数
  128. * @return array
  129. */
  130. public function searchItems($keyword, $params = [])
  131. {
  132. $params['name'] = $keyword;
  133. return $this->getInspectionItems($params);
  134. }
  135. /**
  136. * 提交验货任务 接口总方法
  137. * @param array $taskData 任务数据
  138. * @param array $resultData 验货结果数据
  139. * @return array
  140. */
  141. public static function submitInspectionTask($taskData, $resultData)
  142. {
  143. Db::startTrans();
  144. try {
  145. // 1. 创建或更新验货任务
  146. $taskId = self::saveInspectionTask($taskData);
  147. if (!$taskId) {
  148. throw new \Exception('验货任务创建失败');
  149. }
  150. // 2. 保存验货结果
  151. if (!empty($resultData)) {
  152. foreach ($resultData as $result) {
  153. $result['task_id'] = $taskId;
  154. $resultId = self::saveInspectionResult($result);
  155. // 3. 保存验货照片
  156. if (!empty($result['photos'])) {
  157. self::saveInspectionPhotos($resultId, $result['photos']);
  158. }
  159. }
  160. }
  161. // 验货结果 是 通过 还是 不通过
  162. $isPass = 1;
  163. foreach ($resultData as $result) {
  164. if ($result['is_qualified'] == 0) {
  165. $isPass = 0;
  166. }
  167. }
  168. // 4. 更新任务状态为已完成
  169. self::updateTaskStatus($taskId, 2, $isPass); // 2:已完成
  170. if ($isPass) {
  171. $orderStatus = OrderEnum::STATUS_INSPECTION_PASS;
  172. } else {
  173. $orderStatus = OrderEnum::STATUS_INSPECTION_FAIL;
  174. }
  175. // 5. 更新订单状态
  176. OrderService::updateOrderStatus($taskData['order_id'], $taskData['inspector_id'], $orderStatus); //修改订单状态
  177. Db::commit();
  178. return [
  179. 'code' => 1,
  180. 'msg' => '验货任务提交成功',
  181. 'data' => ['task_id' => $taskId]
  182. ];
  183. } catch (\Exception $e) {
  184. Db::rollback();
  185. return [
  186. 'code' => 0,
  187. 'msg' => '验货任务提交失败:' . $e->getMessage(),
  188. 'data' => []
  189. ];
  190. }
  191. }
  192. /**
  193. * 保存验货任务
  194. * @param array $taskData 任务数据
  195. * @return int 任务ID
  196. */
  197. private static function saveInspectionTask($taskData)
  198. {
  199. $data = [
  200. 'order_id' => $taskData['order_id'] ?? 0,
  201. 'inspector_id' => $taskData['inspector_id'] ?? 0,
  202. 'remark' => $taskData['remark'] ?? '',
  203. 'images' => $taskData['images'] ?? '',
  204. 'start_time' => $taskData['start_time'] ?? time(),
  205. 'end_time' => $taskData['end_time'] ?? time(),
  206. 'task_status' => $taskData['task_status'] ?? 1,
  207. 'status' => $taskData['status'] ?? 1,
  208. 'updatetime' => time()
  209. ];
  210. // 如果提供了task_id,则更新,否则创建
  211. if (isset($taskData['task_id']) && $taskData['task_id'] > 0) {
  212. $result = Db::table('inspection_task')
  213. ->where('id', $taskData['task_id'])
  214. ->update($data);
  215. return $result ? $taskData['task_id'] : false;
  216. } else {
  217. $data['createtime'] = time();
  218. return Db::table('inspection_task')->insertGetId($data);
  219. }
  220. }
  221. /**
  222. * 保存验货结果
  223. * @param array $resultData 结果数据
  224. * @return int 结果ID
  225. */
  226. private static function saveInspectionResult($resultData)
  227. {
  228. $data = [
  229. 'task_id' => $resultData['task_id'],
  230. 'item_id' => $resultData['item_id'],
  231. 'name' => $resultData['name'] ?? '',
  232. 'is_qualified' => $resultData['is_qualified'] ?? 1,
  233. 'remark' => $resultData['remark'] ?? '',
  234. 'status' => $resultData['status'] ?? 1,
  235. 'createtime' => time(),
  236. 'updatetime' => time()
  237. ];
  238. return Db::table('inspection_result')->insertGetId($data);
  239. }
  240. /**
  241. * 保存验货照片
  242. * @param int $resultId 结果ID
  243. * @param array $photos 照片数组
  244. * @return bool
  245. */
  246. private static function saveInspectionPhotos($resultId, $photos)
  247. {
  248. if (empty($photos) || !is_array($photos)) {
  249. return true;
  250. }
  251. $photoData = [];
  252. foreach ($photos as $photo) {
  253. $photoData[] = [
  254. 'result_id' => $resultId,
  255. 'photo_url' => $photo['photo_url'] ?? $photo,
  256. 'upload_time' => $photo['upload_time'] ?? time(),
  257. 'status' => $photo['status'] ?? 1,
  258. 'createtime' => time(),
  259. 'updatetime' => time()
  260. ];
  261. }
  262. return Db::table('inspection_photo')->insertAll($photoData);
  263. }
  264. /**
  265. * 更新任务状态
  266. * @param int $taskId 任务ID
  267. * @param int $status 状态 1:进行中 2:已完成
  268. * @param int $isPass 是否通过
  269. * @return bool
  270. */
  271. private static function updateTaskStatus($taskId, $status,$isPass)
  272. {
  273. return Db::table('inspection_task')
  274. ->where('id', $taskId)
  275. ->update([
  276. 'task_status' => $status,
  277. 'end_time' => time(),
  278. 'updatetime' => time(),
  279. 'is_qualified' => $isPass
  280. ]);
  281. }
  282. /**
  283. * 获取验货任务详情
  284. * @param int $taskId 任务ID
  285. * @return array|null
  286. */
  287. public static function getInspectionTaskDetail($taskId)
  288. {
  289. // 获取任务基本信息
  290. $task = Db::table('inspection_task')
  291. ->where('id', $taskId)
  292. ->where('deletetime', 'exp', 'IS NULL')
  293. ->find();
  294. if (!$task) {
  295. return null;
  296. }
  297. // 获取验货结果
  298. $results = Db::table('inspection_result')
  299. ->where('task_id', $taskId)
  300. ->where('deletetime', 'exp', 'IS NULL')
  301. ->select();
  302. // 为每个结果获取照片
  303. foreach ($results as &$result) {
  304. $photos = Db::table('inspection_photo')
  305. ->where('result_id', $result['id'])
  306. ->where('deletetime', 'exp', 'IS NULL')
  307. ->select();
  308. $result['photos'] = $photos;
  309. }
  310. $task['results'] = $results;
  311. return $task;
  312. }
  313. /**
  314. * 获取验货任务列表
  315. * @param array $params 查询参数
  316. * @return array
  317. */
  318. public static function getInspectionTaskList($params = [])
  319. {
  320. $where = [];
  321. $where['deletetime'] = ['exp', 'IS NULL'];
  322. // 验货员ID筛选
  323. if (isset($params['inspector_id']) && $params['inspector_id'] > 0) {
  324. $where['inspector_id'] = $params['inspector_id'];
  325. }
  326. // 订单ID筛选
  327. if (isset($params['order_id']) && $params['order_id'] > 0) {
  328. $where['order_id'] = $params['order_id'];
  329. }
  330. // 任务状态筛选
  331. if (isset($params['task_status']) && $params['task_status'] !== '') {
  332. $where['task_status'] = $params['task_status'];
  333. }
  334. // 时间范围筛选
  335. if (isset($params['start_time']) && isset($params['end_time'])) {
  336. $where['createtime'] = ['between', [$params['start_time'], $params['end_time']]];
  337. }
  338. $query = Db::name('inspection_task')->where($where);
  339. // 排序
  340. $order = $params['order'] ?? 'createtime DESC';
  341. $query->order($order);
  342. // 分页处理
  343. if (isset($params['page']) && isset($params['page_size'])) {
  344. return $query->paginate([
  345. 'page' => $params['page'],
  346. 'list_rows' => $params['page_size']
  347. ]);
  348. }
  349. return $query->select();
  350. }
  351. }