CoachInfoRecordService.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Services;
  3. use App\Models\CoachUser;
  4. use App\Enums\TechnicianStatus;
  5. use App\Models\CoachInfoRecord;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Enums\TechnicianAuthStatus;
  8. use Slowlyo\OwlAdmin\Services\AdminService;
  9. /**
  10. * 技师信息记录
  11. *
  12. * @method CoachInfoRecord getModel()
  13. * @method CoachInfoRecord|\Illuminate\Database\Query\Builder query()
  14. */
  15. class CoachInfoRecordService extends AdminService
  16. {
  17. protected string $modelName = CoachInfoRecord::class;
  18. /**
  19. * 审核技师申请
  20. *
  21. * @param int $id 申请记录ID
  22. * @param int $state 审核状态 2:通过 3:驳回
  23. * @param string $remark 审核意见
  24. * @param int $adminId 审核人ID
  25. * @return bool
  26. * @throws \Exception
  27. */
  28. public function audit(int $id, int $state, string $remark, int $adminId): bool
  29. {
  30. DB::beginTransaction();
  31. try {
  32. // 获取申请记录
  33. $record = $this->getModel()::findOrFail($id);
  34. // 检查状态
  35. if ($record->state != TechnicianAuthStatus::AUDITING->value) {
  36. throw new \Exception('该申请已审核');
  37. }
  38. // 更新申请记录
  39. $record->update([
  40. 'state' => $state,
  41. 'audit_remark' => $remark,
  42. 'auditor' => $adminId,
  43. 'audit_time' => now(),
  44. ]);
  45. // 如果审核通过,更新技师状态
  46. if ($state == TechnicianAuthStatus::PASSED->value) {
  47. CoachUser::where('id', $record->coach_id)->update([
  48. 'state' => TechnicianStatus::ACTIVE->value,
  49. 'info_record_id' => $record->id
  50. ]);
  51. }
  52. DB::commit();
  53. return true;
  54. } catch (\Exception $e) {
  55. DB::rollBack();
  56. throw $e;
  57. }
  58. }
  59. }