InspectService.php 14 KB

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