InspectionService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace app\common\Service;
  3. use app\common\model\inspection\InspectionApplication;
  4. use think\Db;
  5. use think\Exception;
  6. use app\common\exception\BusinessException;
  7. /**
  8. * 验货员申请服务类
  9. */
  10. class InspectionService
  11. {
  12. /**
  13. * 创建验货员申请
  14. * @param int $userId 用户ID
  15. * @param array $data 申请数据
  16. * @return InspectionApplication
  17. * @throws Exception
  18. */
  19. public static function createApplication($userId, $data)
  20. {
  21. // 检查用户是否可以申请
  22. if (!InspectionApplication::canApply($userId)) {
  23. throw new Exception('您已有申请记录,请勿重复申请');
  24. }
  25. // 检查手机号是否已被使用
  26. if (InspectionApplication::isPhoneApplied($data['phone'])) {
  27. throw new Exception('该手机号已被其他用户申请');
  28. }
  29. // 补充数据
  30. $data['user_id'] = $userId;
  31. $data['apply_time'] = time();
  32. $data['audit_status'] = InspectionApplication::AUDIT_STATUS_PENDING;
  33. $data['audit_time'] = 0;
  34. $data['reject_reason'] = '';
  35. // 启动事务
  36. Db::startTrans();
  37. try {
  38. $application = InspectionApplication::create($data);
  39. // 提交事务
  40. Db::commit();
  41. return $application;
  42. } catch (Exception $e) {
  43. // 回滚事务
  44. Db::rollback();
  45. throw new BusinessException('申请提交失败:' . $e->getMessage());
  46. }
  47. }
  48. /**
  49. * 获取申请详情
  50. * @param int $applicationId 申请ID
  51. * @param int $userId 用户ID
  52. * @return InspectionApplication|null
  53. * @throws Exception
  54. */
  55. public static function getApplicationDetail($applicationId = 0, $userId = 0)
  56. {
  57. $application = InspectionApplication::where(function($query) use ($applicationId,$userId){
  58. if($applicationId){
  59. $query->where('inspection_application.id', $applicationId);
  60. }
  61. if($userId){
  62. $query->where('inspection_application.user_id', $userId);
  63. }
  64. })
  65. ->find();
  66. // if (!$application) {
  67. // throw new BusinessException('申请记录不存在或无权访问');
  68. // }
  69. return $application;
  70. }
  71. /**
  72. * 获取用户申请列表
  73. * @param int $userId 用户ID
  74. * @return \think\Collection
  75. */
  76. public static function getApplicationList($userId)
  77. {
  78. return InspectionApplication::where('inspection_application.user_id', $userId)
  79. ->order('inspection_application.id', 'desc')
  80. ->select();
  81. }
  82. /**
  83. * 修改申请信息
  84. * @param int $applicationId 申请ID
  85. * @param int $userId 用户ID
  86. * @param array $data 修改数据
  87. * @return InspectionApplication
  88. * @throws Exception
  89. */
  90. public static function updateApplication($applicationId, $userId, $data)
  91. {
  92. $application = InspectionApplication::where('inspection_application.id', $applicationId)
  93. ->where('inspection_application.user_id', $userId)
  94. ->find();
  95. if (!$application) {
  96. throw new BusinessException('申请记录不存在或无权访问');
  97. }
  98. if (!$application->canEdit()) {
  99. throw new BusinessException('该申请状态下不允许修改');
  100. }
  101. // 检查手机号是否已被其他用户使用
  102. if (InspectionApplication::isPhoneApplied($data['phone'], $applicationId)) {
  103. throw new BusinessException('该手机号已被其他用户申请');
  104. }
  105. // 更新申请信息
  106. $application->allowField(true)->save($data);
  107. return $application;
  108. }
  109. /**
  110. * 检查申请资格
  111. * @param int $userId 用户ID
  112. * @return array
  113. */
  114. public static function checkEligibility($userId)
  115. {
  116. $application = InspectionApplication::where('inspection_application.user_id', $userId)
  117. ->order('inspection_application.id', 'desc')
  118. ->find();
  119. if (!$application) {
  120. return [
  121. 'can_apply' => true,
  122. 'status' => 'no_application',
  123. 'message' => '您还未申请验货员,可以提交申请'
  124. ];
  125. }
  126. switch ($application->audit_status) {
  127. case InspectionApplication::AUDIT_STATUS_PENDING:
  128. return [
  129. 'can_apply' => false,
  130. 'status' => 'pending',
  131. 'message' => '您的申请正在审核中,请耐心等待',
  132. 'application' => $application
  133. ];
  134. case InspectionApplication::AUDIT_STATUS_PASSED:
  135. return [
  136. 'can_apply' => false,
  137. 'status' => 'passed',
  138. 'message' => '您已是认证验货员',
  139. 'application' => $application
  140. ];
  141. case InspectionApplication::AUDIT_STATUS_REJECTED:
  142. return [
  143. 'can_apply' => true,
  144. 'status' => 'rejected',
  145. 'message' => '您的申请被驳回,可以重新申请',
  146. 'reject_reason' => $application->reject_reason,
  147. 'application' => $application
  148. ];
  149. default:
  150. return [
  151. 'can_apply' => true,
  152. 'status' => 'unknown',
  153. 'message' => '状态异常,可以重新申请'
  154. ];
  155. }
  156. }
  157. /**
  158. * 管理员审核申请
  159. * @param int $applicationId 申请ID
  160. * @param int $auditStatus 审核状态
  161. * @param string $rejectReason 驳回原因(可选)
  162. * @return bool
  163. * @throws Exception
  164. */
  165. public static function auditApplication($applicationId, $auditStatus, $rejectReason = '')
  166. {
  167. $application = InspectionApplication::get($applicationId);
  168. if (!$application) {
  169. throw new BusinessException('申请记录不存在');
  170. }
  171. if ($application->audit_status != InspectionApplication::AUDIT_STATUS_PENDING) {
  172. throw new BusinessException('该申请已被审核,无法重复操作');
  173. }
  174. // 验证审核状态
  175. $validStatuses = [
  176. InspectionApplication::AUDIT_STATUS_PASSED,
  177. InspectionApplication::AUDIT_STATUS_REJECTED
  178. ];
  179. if (!in_array($auditStatus, $validStatuses)) {
  180. throw new BusinessException('无效的审核状态');
  181. }
  182. // 如果是驳回,必须填写驳回原因
  183. if ($auditStatus == InspectionApplication::AUDIT_STATUS_REJECTED && empty($rejectReason)) {
  184. throw new BusinessException('驳回申请必须填写驳回原因');
  185. }
  186. // 启动事务
  187. Db::startTrans();
  188. try {
  189. $application->audit_status = $auditStatus;
  190. $application->audit_time = time();
  191. $application->reject_reason = $rejectReason;
  192. $application->save();
  193. // 提交事务
  194. Db::commit();
  195. return true;
  196. } catch (Exception $e) {
  197. // 回滚事务
  198. Db::rollback();
  199. throw new BusinessException('审核失败:' . $e->getMessage());
  200. }
  201. }
  202. /**
  203. * 获取管理员申请列表(分页)
  204. * @param array $param 查询参数
  205. * @return \think\Paginator
  206. */
  207. public static function getAdminApplicationList($param = [])
  208. {
  209. $where = [];
  210. // 审核状态筛选
  211. if (isset($param['audit_status']) && $param['audit_status'] !== '') {
  212. $where['inspection_application.audit_status'] = $param['audit_status'];
  213. }
  214. // 关键词搜索(姓名、手机号)
  215. if (!empty($param['keywords'])) {
  216. $where['inspection_application.name|inspection_application.phone'] = ['like', '%' . $param['keywords'] . '%'];
  217. }
  218. // 时间范围筛选
  219. if (!empty($param['start_time']) && !empty($param['end_time'])) {
  220. $where['inspection_application.apply_time'] = ['between', [strtotime($param['start_time']), strtotime($param['end_time'])]];
  221. }
  222. $pageSize = $param['pageSize'] ?? 20;
  223. return InspectionApplication::where($where)
  224. ->with(['user'])
  225. ->order('inspection_application.id', 'desc')
  226. ->paginate($pageSize);
  227. }
  228. }