CoachInfoRecordService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 Illuminate\Support\Facades\Auth;
  9. use Slowlyo\OwlAdmin\Services\AdminService;
  10. /**
  11. * 技师信息记录
  12. *
  13. * @method CoachInfoRecord getModel()
  14. * @method CoachInfoRecord|\Illuminate\Database\Query\Builder query()
  15. */
  16. class CoachInfoRecordService extends AdminService
  17. {
  18. protected string $modelName = CoachInfoRecord::class;
  19. /**
  20. * 审核技师基本信息认证
  21. *
  22. * 审核流程:
  23. * 1. 验证基本信息认证记录是否存在且状态为待审核
  24. * 2. 更新认证记录的审核状态、意见、审核人和时间
  25. * 3. 如果审核通过:
  26. * - 如果技师首次申请,将技师状态设为激活
  27. * - 更新技师表中的基本信息认证记录ID
  28. * 4. 使用事务确保数据一致性
  29. *
  30. * @param int $id 认证记录ID
  31. * @param int $state 审核状态(2:通过 3:驳回) {@see TechnicianAuthStatus}
  32. * @param string $remark 审核意见
  33. * @param int $adminId 审核人ID
  34. *
  35. * @return bool 审核是否成功
  36. * @throws \Exception 当记录不存在或已审核时抛出异常
  37. */
  38. public function audit(int $id, int $state, string $remark, int $adminId): bool
  39. {
  40. return DB::transaction(function () use ($id, $state, $remark, $adminId) {
  41. // 获取认证记录及关联的技师信息
  42. $record = $this->getModel()::with('coach')->findOrFail($id);
  43. $coach = $record->coach;
  44. // 检查技师信息和认证状态
  45. abort_if(!$coach, 400, '技师信息不存在');
  46. abort_if(
  47. (int)$record->state !== TechnicianAuthStatus::AUDITING->value,
  48. 400,
  49. '该申请已审核'
  50. );
  51. // 更新认证记录
  52. $record->state = $state;
  53. $record->audit_remark = $remark;
  54. $record->auditor = $adminId;
  55. $record->audit_time = now();
  56. $record->save();
  57. // 审核通过时的处理
  58. if ($state === TechnicianAuthStatus::PASSED->value) {
  59. $updateData = ['info_record_id' => $record->id];
  60. // 首次申请通过时,激活技师状态
  61. if (!$coach->info_record_id) {
  62. $updateData['state'] = TechnicianStatus::ACTIVE->value;
  63. }
  64. // 更新技师信息
  65. $coach->update($updateData);
  66. }
  67. return true;
  68. });
  69. }
  70. /**
  71. * 更新技师基本信息
  72. *
  73. * 业务流程:
  74. * 1. 验证记录是否存在
  75. * 2. 处理正装照片:
  76. * - 从数据中提取正装照片字段
  77. * - 如果有值则更新技师表
  78. * - 记录更新时间和操作人
  79. * 3. 处理基本信息:
  80. * - 过滤空值字段
  81. * - 处理生活照片JSON转换
  82. * - 更新基本信息记录
  83. * 4. 使用事务确保数据一致性
  84. *
  85. * 注意事项:
  86. * - 所有字段都是可选的
  87. * - 正装照片存储在技师表中
  88. * - 生活照片以JSON数组格式存储
  89. * - 只更新有值的字段
  90. *
  91. * @param array $data 更新数据,包含:
  92. * - id: int 记录ID
  93. * - nickname: ?string 昵称
  94. * - avatar: ?string 头像
  95. * - formal_photo: ?string 正装照片(存储在技师表)
  96. * - gender: ?int 性别(1:男 2:女)
  97. * - mobile: ?string 手机号
  98. * - birthday: ?string 出生日期
  99. * - work_years: ?int 工作年限
  100. * - intention_city: ?string 意向城市
  101. * - introduction: ?string 个人简介
  102. * - life_photos: ?array 生活照片数组
  103. *
  104. * @return array 更新结果
  105. * @throws \Exception 当记录不存在时抛出异常
  106. */
  107. public function updateInfo(array $data): array
  108. {
  109. return DB::transaction(function () use ($data) {
  110. $record = $this->getModel()::findOrFail($data['id']);
  111. $coach = $record->coach;
  112. // 提取正装照片字段
  113. $formalPhoto = $data['formal_photo'] ?? null;
  114. unset($data['formal_photo']);
  115. // 移除 id 字段
  116. unset($data['id']);
  117. // 过滤掉空值字段
  118. $updateData = array_filter($data, function ($value) {
  119. return !is_null($value);
  120. });
  121. // 如果有生活照片,转换为JSON
  122. if (isset($updateData['life_photos'])) {
  123. // 将逗号分割的字符串转换为数组格式
  124. $updateData['life_photos'] = explode(',', $updateData['life_photos']);
  125. }
  126. if (isset($updateData['avatar'])) {
  127. // 将逗号分割的字符串转换为数组格式
  128. $updateData['avatar'] = explode(',', $updateData['avatar']);
  129. }
  130. // 更新记录
  131. $record->update($updateData);
  132. // 如果有正装照片,更新技师表
  133. if ($formalPhoto !== null && $coach) {
  134. $coach->update([
  135. 'formal_photo' => $formalPhoto,
  136. 'formal_photo_updated_at' => now(),
  137. 'formal_photo_admin_id' => Auth::id(),
  138. ]);
  139. }
  140. return [
  141. 'message' => '修改成功',
  142. 'data' => $record
  143. ];
  144. });
  145. }
  146. }