123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace App\Services;
- use App\Models\CoachUser;
- use App\Models\CoachRealRecord;
- use Illuminate\Support\Facades\DB;
- use App\Enums\TechnicianAuthStatus;
- use Slowlyo\OwlAdmin\Services\AdminService;
- /**
- * 技师实名认证
- *
- * @method CoachRealRecord getModel()
- * @method CoachRealRecord|\Illuminate\Database\Query\Builder query()
- */
- class CoachRealRecordService extends AdminService
- {
- protected string $modelName = CoachRealRecord::class;
- /**
- * 审核技师实名认证
- *
- * @param int $id 认证记录ID
- * @param int $state 审核状态 2:通过 3:驳回
- * @param string $remark 审核意见
- * @param int $adminId 审核人ID
- * @return bool
- * @throws \Exception
- */
- public function audit(int $id, int $state, string $remark, int $adminId): bool
- {
- DB::beginTransaction();
- try {
- // 获取认证记录
- $record = $this->getModel()::findOrFail($id);
- // 检查状态
- if ((int)$record->state != TechnicianAuthStatus::AUDITING->value) {
- throw new \Exception('该认证已审核');
- }
- // 更新认证记录
- $record->update([
- 'state' => $state,
- 'audit_remark' => $remark,
- 'auditor' => $adminId,
- 'audit_time' => now(),
- ]);
- // 如果审核通过,更新技师状态
- if ($state == TechnicianAuthStatus::PASSED->value) {
- CoachUser::where('id', $record->coach_id)->update([
- 'real_auth_record_id' => $record->id
- ]);
- }
- DB::commit();
- return true;
- } catch (\Exception $e) {
- DB::rollBack();
- throw $e;
- }
- }
- /**
- * 更新技师实名认证信息
- *
- * @param int $id 认证记录ID
- * @param array $data 更新的数据
- * @return bool
- * @throws \Exception
- */
- public function updateInfo(int $id, array $data): bool
- {
- DB::beginTransaction();
- try {
- // 获取认证记录
- $record = $this->getModel()::findOrFail($id);
- // 准备更新数据,确保不为 null
- $updateData = [];
- if (isset($data['id_card_front_photo'])) {
- // 确保存储的值是有效的 JSON 格式
- $updateData['id_card_front_photo'] = json_encode($data['id_card_front_photo']);
- }
- if (isset($data['id_card_back_photo'])) {
- // 确保存储的值是有效的 JSON 格式
- $updateData['id_card_back_photo'] = json_encode($data['id_card_back_photo']);
- }
- if (isset($data['id_card_hand_photo'])) {
- // 确保存储的值是有效的 JSON 格式
- $updateData['id_card_hand_photo'] = json_encode($data['id_card_hand_photo']);
- }
- // 确保更新数据不为空
- if (empty($updateData)) {
- throw new \Exception('没有提供有效的更新数据');
- }
- // 更新认证记录
- $record->update($updateData);
- DB::commit();
- return true;
- } catch (\Exception $e) {
- DB::rollBack();
- throw $e;
- }
- }
- }
|