getMessage()); } } /** * 获取申请详情 * @param int $applicationId 申请ID * @param int $userId 用户ID * @return InspectionApplication|null * @throws Exception */ public static function getApplicationDetail($applicationId = 0, $userId = 0) { $application = InspectionApplication::where(function($query) use ($applicationId,$userId){ if($applicationId){ $query->where('inspection_application.id', $applicationId); } if($userId){ $query->where('inspection_application.user_id', $userId); } }) ->find(); // if (!$application) { // throw new BusinessException('申请记录不存在或无权访问'); // } return $application; } /** * 获取用户申请列表 * @param int $userId 用户ID * @return \think\Collection */ public static function getApplicationList($userId) { return InspectionApplication::where('inspection_application.user_id', $userId) ->order('inspection_application.id', 'desc') ->select(); } /** * 修改申请信息 * @param int $applicationId 申请ID * @param int $userId 用户ID * @param array $data 修改数据 * @return InspectionApplication * @throws Exception */ public static function updateApplication($applicationId, $userId, $data) { $application = InspectionApplication::where('inspection_application.id', $applicationId) ->where('inspection_application.user_id', $userId) ->find(); if (!$application) { throw new BusinessException('申请记录不存在或无权访问'); } if (!$application->canEdit()) { throw new BusinessException('该申请状态下不允许修改'); } // 检查手机号是否已被其他用户使用 if (InspectionApplication::isPhoneApplied($data['phone'], $applicationId)) { throw new BusinessException('该手机号已被其他用户申请'); } // 更新申请信息 $application->allowField(true)->save($data); return $application; } /** * 检查申请资格 * @param int $userId 用户ID * @return array */ public static function checkEligibility($userId) { $application = InspectionApplication::where('inspection_application.user_id', $userId) ->order('inspection_application.id', 'desc') ->find(); if (!$application) { return [ 'can_apply' => true, 'status' => 'no_application', 'message' => '您还未申请验货员,可以提交申请' ]; } switch ($application->audit_status) { case InspectionApplication::AUDIT_STATUS_PENDING: return [ 'can_apply' => false, 'status' => 'pending', 'message' => '您的申请正在审核中,请耐心等待', 'application' => $application ]; case InspectionApplication::AUDIT_STATUS_PASSED: return [ 'can_apply' => false, 'status' => 'passed', 'message' => '您已是认证验货员', 'application' => $application ]; case InspectionApplication::AUDIT_STATUS_REJECTED: return [ 'can_apply' => true, 'status' => 'rejected', 'message' => '您的申请被驳回,可以重新申请', 'reject_reason' => $application->reject_reason, 'application' => $application ]; default: return [ 'can_apply' => true, 'status' => 'unknown', 'message' => '状态异常,可以重新申请' ]; } } /** * 管理员审核申请 * @param int $applicationId 申请ID * @param int $auditStatus 审核状态 * @param string $rejectReason 驳回原因(可选) * @return bool * @throws Exception */ public static function auditApplication($applicationId, $auditStatus, $rejectReason = '') { $application = InspectionApplication::get($applicationId); if (!$application) { throw new BusinessException('申请记录不存在'); } if ($application->audit_status != InspectionApplication::AUDIT_STATUS_PENDING) { throw new BusinessException('该申请已被审核,无法重复操作'); } // 验证审核状态 $validStatuses = [ InspectionApplication::AUDIT_STATUS_PASSED, InspectionApplication::AUDIT_STATUS_REJECTED ]; if (!in_array($auditStatus, $validStatuses)) { throw new BusinessException('无效的审核状态'); } // 如果是驳回,必须填写驳回原因 if ($auditStatus == InspectionApplication::AUDIT_STATUS_REJECTED && empty($rejectReason)) { throw new BusinessException('驳回申请必须填写驳回原因'); } // 启动事务 Db::startTrans(); try { $application->audit_status = $auditStatus; $application->audit_time = time(); $application->reject_reason = $rejectReason; $application->save(); // 提交事务 Db::commit(); return true; } catch (Exception $e) { // 回滚事务 Db::rollback(); throw new BusinessException('审核失败:' . $e->getMessage()); } } /** * 获取管理员申请列表(分页) * @param array $param 查询参数 * @return \think\Paginator */ public static function getAdminApplicationList($param = []) { $where = []; // 审核状态筛选 if (isset($param['audit_status']) && $param['audit_status'] !== '') { $where['inspection_application.audit_status'] = $param['audit_status']; } // 关键词搜索(姓名、手机号) if (!empty($param['keywords'])) { $where['inspection_application.name|inspection_application.phone'] = ['like', '%' . $param['keywords'] . '%']; } // 时间范围筛选 if (!empty($param['start_time']) && !empty($param['end_time'])) { $where['inspection_application.apply_time'] = ['between', [strtotime($param['start_time']), strtotime($param['end_time'])]]; } $pageSize = $param['pageSize'] ?? 20; return InspectionApplication::where($where) ->with(['user']) ->order('inspection_application.id', 'desc') ->paginate($pageSize); } }