CoachRealRecordService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Services;
  3. use App\Models\CoachUser;
  4. use App\Models\CoachRealRecord;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Enums\TechnicianAuthStatus;
  7. use Slowlyo\OwlAdmin\Services\AdminService;
  8. /**
  9. * 技师实名认证
  10. *
  11. * @method CoachRealRecord getModel()
  12. * @method CoachRealRecord|\Illuminate\Database\Query\Builder query()
  13. */
  14. class CoachRealRecordService extends AdminService
  15. {
  16. protected string $modelName = CoachRealRecord::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): 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. 'real_auth_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. /**
  58. * 更新技师实名认证信息
  59. *
  60. * @param int $id 认证记录ID
  61. * @param array $data 更新的数据
  62. * @return bool
  63. * @throws \Exception
  64. */
  65. public function updateInfo(int $id, array $data): bool
  66. {
  67. DB::beginTransaction();
  68. try {
  69. // 获取认证记录
  70. $record = $this->getModel()::findOrFail($id);
  71. // 准备更新数据,确保不为 null
  72. $updateData = [];
  73. if (isset($data['id_card_front_photo'])) {
  74. // 确保存储的值是有效的 JSON 格式
  75. $updateData['id_card_front_photo'] = json_encode($data['id_card_front_photo']);
  76. }
  77. if (isset($data['id_card_back_photo'])) {
  78. // 确保存储的值是有效的 JSON 格式
  79. $updateData['id_card_back_photo'] = json_encode($data['id_card_back_photo']);
  80. }
  81. if (isset($data['id_card_hand_photo'])) {
  82. // 确保存储的值是有效的 JSON 格式
  83. $updateData['id_card_hand_photo'] = json_encode($data['id_card_hand_photo']);
  84. }
  85. // 确保更新数据不为空
  86. if (empty($updateData)) {
  87. throw new \Exception('没有提供有效的更新数据');
  88. }
  89. // 更新认证记录
  90. $record->update($updateData);
  91. DB::commit();
  92. return true;
  93. } catch (\Exception $e) {
  94. DB::rollBack();
  95. throw $e;
  96. }
  97. }
  98. }