123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /**
- * @Name
- * @Description
- * @Author 刘学玺
- * @Date 2024/9/18 13:59
- */
- namespace App\Http\Services\Backend\Server\Coach;
- use App\Exceptions\ApiException;
- use App\Http\Services\Service;
- use App\Models\Coach\User;
- use App\Models\Coach\Verify;
- use Illuminate\Support\Facades\DB;
- class UserService extends Service
- {
- protected array $selectColumn = ['id', 'name', 'user_id', 'nickname', 'sex', 'mobile', 'avatar', 'id_code as idCode', 'id_card as idCard', 'status', 'auth_status as authStatus'];
- protected array $appendColumn = ['created_at as createTime'];
- protected array $authColumn = ['auth_time as authTime', 'auth_text as authText'];
- public function getUserList($params)
- {
- // isset($params['nickname']) && filled($params['nickname']) && $user->whereLike('nickname', "%{$params['nickname']}%");
- // isset($params['mobile']) && filled($params['mobile']) && $user->whereLike('mobile', "%{$params['mobile']}%");
- // isset($params['status']) && filled($params['status']) && $user->where('status', $params['status']);
- // !empty($params['loginDate']) && $user->whereBetween('login_date', $params['loginDate']);
- // !empty($params['createTime']) && $user->whereBetween('created_at', $params['createTime']);
- $user = User::query();
- $rolePage = $user->with('verify', function ($query) {
- $query->select(['coach_id', 'status']);
- })->paginate($params['pageSize'], [...$this->selectColumn, ...$this->appendColumn], 'page', $params['pageNo']);
- return ['list' => $rolePage->items(), 'total' => $rolePage->total()];
- }
- public function apply(int $id)
- {
- // 查询审核信息
- $verify = Verify::query()->where('coach_id', $id)->first();
- $coach = User::query()->find($id);
- $verify = $verify? $verify->toArray():[];
- return array_merge($coach->toArray(), $verify);
- }
- public function updateApply(array $data, int $id)
- {
- $coach = User::query()->find($id);
- unset($data['id']);
- if (isset($data['status']) && in_array($data['status'], [1, 3]) && $coach->status === 0) {
- $data['verifyTime'] = time();
- }
- $coach = self::toModel([...$data], User::class);
- $coach->where('id', $id)->update($coach->getAttributes());
- }
- /**
- * @throws ApiException
- */
- public function updateReApply(array $data, int $id): void
- {
- $verify_id = Verify::query()->where('coach_id', $id)->value('id');
- empty($verify_id) && self::error();
- $data['id'] = $verify_id;
- $verify = self::toModel($data, Verify::class);
- DB::beginTransaction();
- try {
- $verify->save();
- if ($data['status'] === 1) {
- $coachData = $verify->only(['name', 'mobile', 'status', 'verify_text', 'verify_time', 'sex', 'work_img']);
- $coachData['id'] = $id;
- $coach = self::toModel($coachData, User::class);
- $coach->save();
- }
- DB::commit();
- } catch (\Exception) {
- DB::rollBack();
- self::error();
- }
- }
- public function auth(int $id)
- {
- return User::query()->select([...$this->selectColumn, ...$this->authColumn])->find($id);
- }
- /**
- * @throws ApiException
- */
- public function updateAuth(array $data, int $id): void
- {
- $isExistsCoach = User::query()->where('id', $id)->exists();
- !$isExistsCoach && self::error('数据错误');
- $data['authTime'] = time();
- $data['id'] = $id;
- self::toModel($data, User::class)->save();
- }
- }
|