MemberRepository.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\Member;
  4. use App\Models\MemberInfo;
  5. use App\Models\Resume;
  6. use App\Validators\Rules\MobileRule;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Hash;
  9. use Illuminate\Support\Facades\Schema;
  10. use Prettus\Repository\Eloquent\BaseRepository;
  11. use Prettus\Repository\Criteria\RequestCriteria;
  12. /**
  13. * Class MemberRepositoryEloquent.
  14. *
  15. * @package namespace App\Repositories;
  16. */
  17. class MemberRepository extends BaseRepository
  18. {
  19. /**
  20. * Specify Model class name
  21. *
  22. * @return string
  23. */
  24. public function model()
  25. {
  26. return Member::class;
  27. }
  28. /**
  29. * Boot up the repository, pushing criteria
  30. */
  31. public function boot()
  32. {
  33. $this->pushCriteria(app(RequestCriteria::class));
  34. }
  35. /**
  36. * @param $account string
  37. * @return null|Member
  38. */
  39. public function getMemberByAccount($account)
  40. {
  41. if (validator_check($account, 'email')) {
  42. return $this->model->where('email', $account)->first();
  43. }
  44. if (validator_check($account, new MobileRule())) {
  45. return $this->model->where('mobile', $account)->first();
  46. }
  47. if (isCreditNo($account)) {
  48. $member_info = MemberInfo::where('id_card', $account)->first();
  49. if (!$member_info) {
  50. return null;
  51. }
  52. return $member_info->members;
  53. }
  54. return $this->model->where('username', $account)->first();
  55. }
  56. /**
  57. * @param $id
  58. * @return object
  59. */
  60. public function getMemberById($id)
  61. {
  62. return $this->model->find($id);
  63. }
  64. public function updatePasswordById($password, $id)
  65. {
  66. return $this->model->where(['id'=>$id])->update(['password'=>$password]);
  67. }
  68. public function getFields($id, $field = ['*'])
  69. {
  70. return $this->model->where('id', $id)->select($field)->first();
  71. }
  72. public function updateInfo($id, $date)
  73. {
  74. return $this->model->where('id', $id)->update($date);
  75. }
  76. public function updateLoginStatus($member)
  77. {
  78. $member->last_login_ip=ip2long(request()->ip);
  79. $member->last_login_time=time();
  80. $member->save();
  81. }
  82. /**
  83. * 检查字段惟一
  84. * @param $key
  85. * @param $value
  86. * @param int $id 大于0表示排除自身
  87. * @return bool
  88. */
  89. public function checkUniaue($key, $value, $id = 0)
  90. {
  91. if (!Schema::hasColumn($this->model->getTable(), $key)) {
  92. return true;
  93. }
  94. if ($this->model->withTrashed()->where($key, $value)
  95. ->when($id>0, function ($query) use ($id) {
  96. return $query->where('id', '<>', $id);
  97. })->first()) {
  98. return false;
  99. }
  100. return true;
  101. }
  102. /**
  103. * 重置密码
  104. * @param $type :mobile, email
  105. * @param $value :对应type
  106. * @param $password
  107. */
  108. public function resetPassword($type, $value, $password)
  109. {
  110. $this->model->where($type, $value)->update(['password'=>Hash::make($password)]);
  111. return $this->model->where($type, $value)->first();
  112. }
  113. public function getLastPersonCount($where)
  114. {
  115. return $this->model->where($where)->where('created_at', '>=', date('Y-m-01 00:00:00', strtotime('-1 month')))->where('created_at', '<=', date("Y-m-d 23:59:59", strtotime(-date('d').'day')))->count();
  116. }
  117. public function getNextPersonCount($where)
  118. {
  119. return $this->model->where($where)->where('created_at', '>=', date('Y-m-01 00:00:00', strtotime(date("Y-m-d"))))->where('created_at', '<=', date('Y-m-d 23:59:59', strtotime(date('Y-m-01', strtotime(date("Y-m-d")))." +1 month -1 day")))->count();
  120. }
  121. public function getPersonCount($where)
  122. {
  123. return $this->model->where($where)->count();
  124. }
  125. public function getMemberNumByTime($where, $time_condition)
  126. {
  127. return $this->model->where($where)->where(function ($query) use ($time_condition) {
  128. if ($time_condition) {
  129. $query->Where([['last_login_time', '>=',strtotime($time_condition[0])],['last_login_time', '<=',strtotime($time_condition[1])]])->orWhere([['updated_at', '>=',strtotime($time_condition[0])],['updated_at', '<=',strtotime($time_condition[1])]]);
  130. }
  131. })->count();
  132. }
  133. public function getJobSeekersByGroup($where, $where_str, $fields, $group_by)
  134. {
  135. return DB::table(Resume::getTableName().' as r')
  136. ->select(DB::raw($fields))
  137. ->leftjoin(MemberInfo::getTableName().' as mi', 'mi.uid', '=', 'r.uid')
  138. ->leftjoin(Member::getTableName().' as m', 'm.id', '=', 'mi.uid')
  139. ->where($where)
  140. ->whereRaw($where_str)
  141. ->groupBy($group_by)
  142. ->get();
  143. }
  144. }