InspectionService.php 8.7 KB

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