InspectionService.php 8.5 KB

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