UserService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2024/9/18 13:59
  7. */
  8. namespace App\Http\Services\Backend\Server\Coach;
  9. use App\Exceptions\ApiException;
  10. use App\Http\Services\Service;
  11. use App\Models\Coach\User;
  12. use App\Models\Coach\Verify;
  13. use Illuminate\Support\Facades\DB;
  14. class UserService extends Service
  15. {
  16. protected array $selectColumn = ['id', 'name', 'user_id', 'nickname', 'sex', 'mobile', 'avatar', 'id_code as idCode', 'id_card as idCard', 'status', 'auth_status as authStatus'];
  17. protected array $appendColumn = ['created_at as createTime'];
  18. protected array $authColumn = ['auth_time as authTime', 'auth_text as authText'];
  19. public function getUserList($params)
  20. {
  21. // isset($params['nickname']) && filled($params['nickname']) && $user->whereLike('nickname', "%{$params['nickname']}%");
  22. // isset($params['mobile']) && filled($params['mobile']) && $user->whereLike('mobile', "%{$params['mobile']}%");
  23. // isset($params['status']) && filled($params['status']) && $user->where('status', $params['status']);
  24. // !empty($params['loginDate']) && $user->whereBetween('login_date', $params['loginDate']);
  25. // !empty($params['createTime']) && $user->whereBetween('created_at', $params['createTime']);
  26. $user = User::query();
  27. $rolePage = $user->with('verify', function ($query) {
  28. $query->select(['coach_id', 'status']);
  29. })->paginate($params['pageSize'], [...$this->selectColumn, ...$this->appendColumn], 'page', $params['pageNo']);
  30. return ['list' => $rolePage->items(), 'total' => $rolePage->total()];
  31. }
  32. public function apply(int $id)
  33. {
  34. // 查询审核信息
  35. $verify = Verify::query()->where('coach_id', $id)->first();
  36. $coach = User::query()->find($id);
  37. $verify = $verify? $verify->toArray():[];
  38. return array_merge($coach->toArray(), $verify);
  39. }
  40. public function updateApply(array $data, int $id)
  41. {
  42. $coach = User::query()->find($id);
  43. unset($data['id']);
  44. if (isset($data['status']) && in_array($data['status'], [1, 3]) && $coach->status === 0) {
  45. $data['verifyTime'] = time();
  46. }
  47. $coach = self::toModel([...$data], User::class);
  48. $coach->where('id', $id)->update($coach->getAttributes());
  49. }
  50. /**
  51. * @throws ApiException
  52. */
  53. public function updateReApply(array $data, int $id): void
  54. {
  55. $verify_id = Verify::query()->where('coach_id', $id)->value('id');
  56. empty($verify_id) && self::error();
  57. $data['id'] = $verify_id;
  58. $verify = self::toModel($data, Verify::class);
  59. DB::beginTransaction();
  60. try {
  61. $verify->save();
  62. if ($data['status'] === 1) {
  63. $coachData = $verify->only(['name', 'mobile', 'status', 'verify_text', 'verify_time', 'sex', 'work_img']);
  64. $coachData['id'] = $id;
  65. $coach = self::toModel($coachData, User::class);
  66. $coach->save();
  67. }
  68. DB::commit();
  69. } catch (\Exception) {
  70. DB::rollBack();
  71. self::error();
  72. }
  73. }
  74. public function auth(int $id)
  75. {
  76. return User::query()->select([...$this->selectColumn, ...$this->authColumn])->find($id);
  77. }
  78. /**
  79. * @throws ApiException
  80. */
  81. public function updateAuth(array $data, int $id): void
  82. {
  83. $isExistsCoach = User::query()->where('id', $id)->exists();
  84. !$isExistsCoach && self::error('数据错误');
  85. $data['authTime'] = time();
  86. $data['id'] = $id;
  87. self::toModel($data, User::class)->save();
  88. }
  89. }