where($where) ->field('id,name,parent_id,is_required,status,standard_desc'); // 排序 $order = isset($params['order']) ? $params['order'] : 'id ASC'; $query->order($order); // 分页处理 if (isset($params['page']) && isset($params['page_size'])) { $result = $query->paginate([ 'page' => $params['page'], 'list_rows' => $params['page_size'] ]); return $result; } return $query->select(); } /** * 获取树形结构的验货选项 * @param array $params 查询参数 * @return array */ public function getInspectionItemTree($params = []) { // 获取所有选项 $allItems = $this->getInspectionItems($params); if (is_object($allItems)) { // 分页对象转数组 $allItems = $allItems->toArray()['data']; } return $this->buildTree($allItems); } /** * 构建树形结构 * @param array $items 选项数组 * @param int $parentId 父级ID * @return array */ private function buildTree($items, $parentId = 0) { $tree = []; foreach ($items as $item) { if ($item['parent_id'] == $parentId) { $children = $this->buildTree($items, $item['id']); if (!empty($children)) { $item['children'] = $children; } $tree[] = $item; } } return $tree; } /** * 根据ID获取验货选项详情 * @param int $id 选项ID * @return array|null */ public function getInspectionItemById($id) { return Db::name('inspection_item') ->where('id', $id) ->where('deletetime', 'exp', 'IS NULL') ->find(); } /** * 获取必检项列表 * @param array $params 查询参数 * @return array */ public function getRequiredItems($params = []) { $params['is_required'] = 1; return $this->getInspectionItems($params); } /** * 获取顶级选项(parent_id = 0) * @param array $params 查询参数 * @return array */ public function getTopLevelItems($params = []) { $params['parent_id'] = 0; return $this->getInspectionItems($params); } /** * 获取指定父级下的子选项 * @param int $parentId 父级ID * @param array $params 其他查询参数 * @return array */ public function getChildItems($parentId, $params = []) { $params['parent_id'] = $parentId; return $this->getInspectionItems($params); } /** * 搜索验货选项 * @param string $keyword 搜索关键词 * @param array $params 其他查询参数 * @return array */ public function searchItems($keyword, $params = []) { $params['name'] = $keyword; return $this->getInspectionItems($params); } /** * 提交验货任务 接口总方法 * @param array $taskData 任务数据 * @param array $resultData 验货结果数据 * @return array */ public static function submitInspectionTask($taskData, $resultData) { Db::startTrans(); try { // 0. 验证订单和订单商品是否存在 $orderId = $taskData['order_id'] ?? 0; $orderGoodsId = $taskData['order_goods_id'] ?? 0; $orderData = OrderService::validateOrderAndOrderGoods($orderId, $orderGoodsId); if (!$orderData) { throw new BusinessException('订单或订单商品不存在'); } // 0.1 验证是否重复提交验货任务(如果不是更新任务的话) if (empty($taskData['task_id'])) { $existingTask = self::checkDuplicateInspectionTask($orderId, $orderGoodsId); if ($existingTask) { throw new BusinessException('该订单商品已存在验货任务,不能重复提交'); } } // 1. 创建或更新验货任务 $taskId = self::saveInspectionTask($taskData); if (!$taskId) { throw new BusinessException('验货任务创建失败'); } // 2. 保存验货结果 if (!empty($resultData)) { foreach ($resultData as $result) { $result['task_id'] = $taskId; $resultId = self::saveInspectionResult($result); // 3. 保存验货照片 if (!empty($result['photos'])) { self::saveInspectionPhotos($resultId, $result['photos']); } } } // 验货结果 是 通过 还是 不通过 $isPass = 1; foreach ($resultData as $result) { if ($result['is_qualified'] == 0) { $isPass = 0; break; } } // 4. 更新任务状态为已完成 self::updateTaskStatus($taskId, 2, $isPass); // 2:已完成 // 5. 更新订单商品的验收状态 $inspectStatus = $isPass ? 1 : 2; // 1:验收通过 2:验收不通过 OrderService::updateOrderGoodsInspectStatus($taskData['order_goods_id'], $inspectStatus); // 6. 检查订单是否所有商品都验收完成,决定是否更新订单状态 OrderService::updateOrderStatusByInspectResult($taskData['order_id'], $taskData['inspector_id'] ?? 0); Db::commit(); return [1, '验货任务提交成功', ['task_id' => $taskId]]; } catch (\Exception $e) { Db::rollback(); return [0, '验货任务提交失败:' . $e->getMessage(), []]; } } /** * 保存验货任务 * @param array $taskData 任务数据 * @return int 任务ID */ private static function saveInspectionTask($taskData) { $data = [ 'order_id' => $taskData['order_id'] ?? 0, 'order_goods_id' => $taskData['order_goods_id'] ?? 0, 'inspector_id' => $taskData['inspector_id'] ?? 0, 'remark' => $taskData['remark'] ?? '', 'images' => $taskData['images'] ?? '', 'start_time' => $taskData['start_time'] ?? time(), 'end_time' => $taskData['end_time'] ?? time(), 'task_status' => $taskData['task_status'] ?? 1, 'status' => $taskData['status'] ?? 1, 'updatetime' => time() ]; // 如果提供了task_id,则更新,否则创建 if (isset($taskData['task_id']) && $taskData['task_id'] > 0) { $result = Db::table('inspection_task') ->where('id', $taskData['task_id']) ->update($data); return $result ? $taskData['task_id'] : false; } else { $data['createtime'] = time(); return Db::table('inspection_task')->insertGetId($data); } } /** * 保存验货结果 * @param array $resultData 结果数据 * @return int 结果ID */ private static function saveInspectionResult($resultData) { $data = [ 'task_id' => $resultData['task_id'], 'item_id' => $resultData['item_id'], 'name' => $resultData['name'] ?? '', 'is_qualified' => $resultData['is_qualified'] ?? 1, 'remark' => $resultData['remark'] ?? '', 'status' => $resultData['status'] ?? 1, 'createtime' => time(), 'updatetime' => time() ]; return Db::table('inspection_result')->insertGetId($data); } /** * 保存验货照片 * @param int $resultId 结果ID * @param array $photos 照片数组 * @return bool */ private static function saveInspectionPhotos($resultId, $photos) { if (empty($photos) || !is_array($photos)) { return true; } $photoData = []; foreach ($photos as $photo) { $photoData[] = [ 'result_id' => $resultId, 'photo_url' => $photo['photo_url'] ?? $photo, 'upload_time' => $photo['upload_time'] ?? time(), 'status' => $photo['status'] ?? 1, 'createtime' => time(), 'updatetime' => time() ]; } return Db::table('inspection_photo')->insertAll($photoData); } /** * 更新任务状态 * @param int $taskId 任务ID * @param int $status 状态 1:进行中 2:已完成 * @param int $isPass 是否通过 * @return bool */ private static function updateTaskStatus($taskId, $status,$isPass) { return Db::table('inspection_task') ->where('id', $taskId) ->update([ 'task_status' => $status, 'end_time' => time(), 'updatetime' => time(), 'is_qualified' => $isPass ]); } /** * 获取验货任务详情 * @param int $taskId 任务ID * @return array|null */ public static function getInspectionTaskDetail($taskId) { // 获取任务基本信息 $task = Db::table('inspection_task') ->where('id', $taskId) ->where('deletetime', 'exp', 'IS NULL') ->find(); if (!$task) { return null; } // 获取验货结果 $results = Db::table('inspection_result') ->where('task_id', $taskId) ->where('deletetime', 'exp', 'IS NULL') ->select(); // 为每个结果获取照片 foreach ($results as &$result) { $photos = Db::table('inspection_photo') ->where('result_id', $result['id']) ->where('deletetime', 'exp', 'IS NULL') ->select(); $result['photos'] = $photos; } $task['results'] = $results; return $task; } /** * 获取验货任务列表 * @param array $params 查询参数 * @return array */ public static function getInspectionTaskList($params = []) { $where = []; $where['deletetime'] = ['exp', 'IS NULL']; // 验货员ID筛选 if (isset($params['inspector_id']) && $params['inspector_id'] > 0) { $where['inspector_id'] = $params['inspector_id']; } // 订单ID筛选 if (isset($params['order_id']) && $params['order_id'] > 0) { $where['order_id'] = $params['order_id']; } // 任务状态筛选 if (isset($params['task_status']) && $params['task_status'] !== '') { $where['task_status'] = $params['task_status']; } // 时间范围筛选 if (isset($params['start_time']) && isset($params['end_time'])) { $where['createtime'] = ['between', [$params['start_time'], $params['end_time']]]; } $query = Db::name('inspection_task')->where($where); // 排序 $order = $params['order'] ?? 'createtime DESC'; $query->order($order); // 分页处理 if (isset($params['page']) && isset($params['page_size'])) { return $query->paginate([ 'page' => $params['page'], 'list_rows' => $params['page_size'] ]); } return $query->select(); } /** * 检查是否重复提交验货任务 * @param int $orderId 订单ID * @param int $orderGoodsId 订单商品ID * @return array|null 返回已存在的任务信息,不存在返回null */ private static function checkDuplicateInspectionTask($orderId, $orderGoodsId) { return Db::table('inspection_task') ->where('order_id', $orderId) ->where('order_goods_id', $orderGoodsId) ->where('deletetime', 'exp', 'IS NULL') ->find(); } }