|
@@ -20,48 +20,60 @@ class CoachInfoRecordService extends AdminService
|
|
|
protected string $modelName = CoachInfoRecord::class;
|
|
|
|
|
|
/**
|
|
|
- * 审核技师申请
|
|
|
+ * 审核技师基本信息认证
|
|
|
*
|
|
|
- * @param int $id 申请记录ID
|
|
|
- * @param int $state 审核状态 2:通过 3:驳回
|
|
|
- * @param string $remark 审核意见
|
|
|
- * @param int $adminId 审核人ID
|
|
|
- * @return bool
|
|
|
- * @throws \Exception
|
|
|
+ * 审核流程:
|
|
|
+ * 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
|
|
|
{
|
|
|
- DB::beginTransaction();
|
|
|
- try {
|
|
|
- // 获取申请记录
|
|
|
- $record = $this->getModel()::findOrFail($id);
|
|
|
+ return DB::transaction(function () use ($id, $state, $remark, $adminId) {
|
|
|
+ // 获取认证记录及关联的技师信息
|
|
|
+ $record = $this->getModel()::with('coach')->findOrFail($id);
|
|
|
+ $coach = $record->coach;
|
|
|
|
|
|
- // 检查状态
|
|
|
- if ($record->state != TechnicianAuthStatus::AUDITING->value) {
|
|
|
- throw new \Exception('该申请已审核');
|
|
|
- }
|
|
|
+ // 检查技师信息和认证状态
|
|
|
+ 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];
|
|
|
|
|
|
- // 更新申请记录
|
|
|
- $record->update([
|
|
|
- 'state' => $state,
|
|
|
- 'audit_remark' => $remark,
|
|
|
- 'auditor' => $adminId,
|
|
|
- 'audit_time' => now(),
|
|
|
- ]);
|
|
|
+ // 首次申请通过时,激活技师状态
|
|
|
+ if (!$coach->info_record_id) {
|
|
|
+ $updateData['state'] = TechnicianStatus::ACTIVE->value;
|
|
|
+ }
|
|
|
|
|
|
- // 如果审核通过,更新技师状态
|
|
|
- if ($state == TechnicianAuthStatus::PASSED->value) {
|
|
|
- CoachUser::where('id', $record->coach_id)->update([
|
|
|
- 'state' => TechnicianStatus::ACTIVE->value,
|
|
|
- 'info_record_id' => $record->id
|
|
|
- ]);
|
|
|
+ // 更新技师信息
|
|
|
+ $coach->update($updateData);
|
|
|
}
|
|
|
|
|
|
- DB::commit();
|
|
|
return true;
|
|
|
- } catch (\Exception $e) {
|
|
|
- DB::rollBack();
|
|
|
- throw $e;
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
}
|