123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- namespace app\common\Service;
- use app\common\model\inspection\InspectionApplication;
- use think\Db;
- use think\Exception;
- /**
- * 验货员申请服务类
- */
- class InspectionService
- {
- /**
- * 创建验货员申请
- * @param int $userId 用户ID
- * @param array $data 申请数据
- * @return InspectionApplication
- * @throws Exception
- */
- public static function createApplication($userId, $data)
- {
- // 检查用户是否可以申请
- if (!InspectionApplication::canApply($userId)) {
- throw new Exception('您已有申请记录,请勿重复申请');
- }
- // 检查手机号是否已被使用
- if (InspectionApplication::isPhoneApplied($data['phone'])) {
- throw new Exception('该手机号已被其他用户申请');
- }
- // 补充数据
- $data['user_id'] = $userId;
- $data['apply_time'] = time();
- $data['audit_status'] = InspectionApplication::AUDIT_STATUS_PENDING;
- $data['audit_time'] = 0;
- $data['reject_reason'] = '';
- // 启动事务
- Db::startTrans();
- try {
- $application = InspectionApplication::create($data);
-
- // 提交事务
- Db::commit();
-
- return $application;
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- throw new Exception('申请提交失败:' . $e->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 Exception('申请记录不存在或无权访问');
- }
- 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 Exception('申请记录不存在或无权访问');
- }
- if (!$application->canEdit()) {
- throw new Exception('该申请状态下不允许修改');
- }
- // 检查手机号是否已被其他用户使用
- if (InspectionApplication::isPhoneApplied($data['phone'], $applicationId)) {
- throw new Exception('该手机号已被其他用户申请');
- }
- // 启动事务
- Db::startTrans();
- try {
- // 更新申请信息
- $application->allowField(true)->save($data);
-
- // 提交事务
- Db::commit();
-
- return $application;
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- throw new Exception('修改失败:' . $e->getMessage());
- }
- }
- /**
- * 检查申请资格
- * @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 Exception('申请记录不存在');
- }
- if ($application->audit_status != InspectionApplication::AUDIT_STATUS_PENDING) {
- throw new Exception('该申请已被审核,无法重复操作');
- }
- // 验证审核状态
- $validStatuses = [
- InspectionApplication::AUDIT_STATUS_PASSED,
- InspectionApplication::AUDIT_STATUS_REJECTED
- ];
-
- if (!in_array($auditStatus, $validStatuses)) {
- throw new Exception('无效的审核状态');
- }
- // 如果是驳回,必须填写驳回原因
- if ($auditStatus == InspectionApplication::AUDIT_STATUS_REJECTED && empty($rejectReason)) {
- throw new Exception('驳回申请必须填写驳回原因');
- }
- // 启动事务
- 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 Exception('审核失败:' . $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);
- }
- }
|