123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- <?php
- namespace app\common\Service;
- use think\Db;
- use app\common\enum\OrderEnum;
- use app\common\Service\OrderService;
- use app\common\exception\BusinessException;
- use app\common\model\inspection\TypeItem;
- use app\common\model\inspection\Item;
- class InspectService
- {
- /**
- * 获取验货选项列表
- * @param array $params 查询参数
- * @return array
- */
- public static function getInspectionItems($params = [])
- {
- $where = [];
-
- // 父级ID筛选
- if (isset($params['parent_id'])) {
- $where['parent_id'] = $params['parent_id'];
- }
-
- // 状态筛选
- if (isset($params['status']) && $params['status'] !== '') {
- $where['status'] = $params['status'];
- }
-
- // 是否必检项筛选
- if (isset($params['is_required']) && $params['is_required'] !== '') {
- $where['is_required'] = $params['is_required'];
- }
-
- // 名称搜索
- if (isset($params['name']) && $params['name'] !== '') {
- $where['name'] = ['like', '%' . $params['name'] . '%'];
- }
- if (isset($params['inspection_type_id']) && $params['inspection_type_id'] !== '') {
- // 通过关联表查询ID
- $typeItem = TypeItem::where('type_id', $params['inspection_type_id'])->select();
- $inspectIds = array_column( collection($typeItem)->toArray(), 'item_id');
- $where['id'] = ['in', $inspectIds];
- }
-
- $query = (new Item())->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,$inspectUid = 0)
- {
- 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,$inspectUid);
-
- // 6. 检查订单是否所有商品都验收完成,决定是否更新订单状态
- OrderService::updateOrderStatusByInspectResult($taskData['order_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();
- }
- }
|