123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace App\Services;
- use App\Models\CoachUser;
- use App\Enums\TechnicianStatus;
- use App\Models\CoachInfoRecord;
- use Illuminate\Support\Facades\DB;
- use App\Enums\TechnicianAuthStatus;
- use Illuminate\Support\Facades\Auth;
- use Slowlyo\OwlAdmin\Services\AdminService;
- /**
- * 技师信息记录
- *
- * @method CoachInfoRecord getModel()
- * @method CoachInfoRecord|\Illuminate\Database\Query\Builder query()
- */
- class CoachInfoRecordService extends AdminService
- {
- protected string $modelName = CoachInfoRecord::class;
- /**
- * 审核技师基本信息认证
- *
- * 审核流程:
- * 1. 验证基本信息认证记录是否存在且状态为待审核
- * 2. 更新认证记录的审核状态、意见、审核人和时间
- * 3. 如果审核通过:
- * - 如果技师首次申请,将技师状态设为激活
- * - 更新技师表中的基本信息认证记录ID
- * 4. 使用事务确保数据一致性
- *
- * @param int $id 认证记录ID
- * @param int $state 审核状态(2:通过 3:驳回) {@see TechnicianAuthStatus}
- * @param string $remark 审核意见
- * @param int $adminId 审核人ID
- *
- * @return bool 审核是否成功
- * @throws \Exception 当记录不存在或已审核时抛出异常
- */
- public function audit(int $id, int $state, string $remark, int $adminId): bool
- {
- return DB::transaction(function () use ($id, $state, $remark, $adminId) {
- // 获取认证记录及关联的技师信息
- $record = $this->getModel()::with('coach')->findOrFail($id);
- $coach = $record->coach;
- // 检查技师信息和认证状态
- abort_if(!$coach, 400, '技师信息不存在');
- abort_if(
- (int)$record->state !== TechnicianAuthStatus::AUDITING->value,
- 400,
- '该申请已审核'
- );
- // 更新认证记录
- $record->state = $state;
- $record->audit_remark = $remark;
- $record->auditor = $adminId;
- $record->audit_time = now();
- $record->save();
- // 审核通过时的处理
- if ($state === TechnicianAuthStatus::PASSED->value) {
- $updateData = ['info_record_id' => $record->id];
- // 首次申请通过时,激活技师状态
- if (!$coach->info_record_id) {
- $updateData['state'] = TechnicianStatus::ACTIVE->value;
- }
- // 更新技师信息
- $coach->update($updateData);
- }
- return true;
- });
- }
- /**
- * 更新技师基本信息
- *
- * 业务流程:
- * 1. 验证记录是否存在
- * 2. 处理正装照片:
- * - 从数据中提取正装照片字段
- * - 如果有值则更新技师表
- * - 记录更新时间和操作人
- * 3. 处理基本信息:
- * - 过滤空值字段
- * - 处理生活照片JSON转换
- * - 更新基本信息记录
- * 4. 使用事务确保数据一致性
- *
- * 注意事项:
- * - 所有字段都是可选的
- * - 正装照片存储在技师表中
- * - 生活照片以JSON数组格式存储
- * - 只更新有值的字段
- *
- * @param array $data 更新数据,包含:
- * - id: int 记录ID
- * - nickname: ?string 昵称
- * - avatar: ?string 头像
- * - formal_photo: ?string 正装照片(存储在技师表)
- * - gender: ?int 性别(1:男 2:女)
- * - mobile: ?string 手机号
- * - birthday: ?string 出生日期
- * - work_years: ?int 工作年限
- * - intention_city: ?string 意向城市
- * - introduction: ?string 个人简介
- * - life_photos: ?array 生活照片数组
- *
- * @return array 更新结果
- * @throws \Exception 当记录不存在时抛出异常
- */
- public function updateInfo(array $data): array
- {
- return DB::transaction(function () use ($data) {
- $record = $this->getModel()::findOrFail($data['id']);
- $coach = $record->coach;
- // 提取正装照片字段
- $formalPhoto = $data['formal_photo'] ?? null;
- unset($data['formal_photo']);
- // 移除 id 字段
- unset($data['id']);
- // 过滤掉空值字段
- $updateData = array_filter($data, function ($value) {
- return !is_null($value);
- });
- // 如果有生活照片,转换为JSON
- if (isset($updateData['life_photos'])) {
- // 将逗号分割的字符串转换为数组格式
- $updateData['life_photos'] = explode(',', $updateData['life_photos']);
- }
- if (isset($updateData['avatar'])) {
- // 将逗号分割的字符串转换为数组格式
- $updateData['avatar'] = explode(',', $updateData['avatar']);
- }
- // 更新记录
- $record->update($updateData);
- // 如果有正装照片,更新技师表
- if ($formalPhoto !== null && $coach) {
- $coach->update([
- 'formal_photo' => $formalPhoto,
- 'formal_photo_updated_at' => now(),
- 'formal_photo_admin_id' => Auth::id(),
- ]);
- }
- return [
- 'message' => '修改成功',
- 'data' => $record
- ];
- });
- }
- }
|