CoachQualRecordService.php 1.8 KB

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