User - 副本.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace app\admin\model;
  3. use app\admin\model\UserAuth;
  4. use support\Model;
  5. /**
  6. * 悦享家用户信息模型
  7. * Class Users
  8. * @package app\admin\model
  9. */
  10. class User extends Model
  11. {
  12. const UPDATED_AT = 'user_update_time';
  13. const REAL_NO = '0';
  14. const REAL_YES = '1';
  15. const REAL_STATUS = [
  16. self::REAL_NO => '未认证',
  17. self::REAL_YES => '已认证'
  18. ];
  19. const PACKAGE_NO = '0';
  20. const PACKAGE_YES = '1';
  21. const PACKAGE_STATUS = [
  22. self::PACKAGE_NO => '未购买',
  23. self::PACKAGE_YES => '已购买'
  24. ];
  25. const TOURIST = '0';
  26. const ORDINARY = '1';
  27. const FORMAL = '2';
  28. const INSIDE = '3';
  29. const USER_CATEGORY = [
  30. self::TOURIST => '游客会员',
  31. self::ORDINARY => '普通会员',
  32. self::FORMAL => '正式会员',
  33. self::INSIDE => '内部会员'
  34. ];
  35. const NO_OWNER = '0';
  36. const PROSPECTIVE_OWNER = '1';
  37. const YES_OWNER = '2';
  38. const USER_OWNER = [
  39. self::NO_OWNER => '非业主',
  40. self::PROSPECTIVE_OWNER => '准业主',
  41. self::YES_OWNER => '业主'
  42. ];
  43. protected $dateFormat = 'U';
  44. /**
  45. * The table associated with the model.
  46. *
  47. * @var string
  48. */
  49. protected $table = 'user';
  50. /**
  51. * The primary key associated with the table.
  52. *
  53. * @var string
  54. */
  55. protected $primaryKey = 'user_id';
  56. /**
  57. * Notes:MembersAuth关联模型
  58. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  59. * User: yym
  60. * Date: 2022/8/4
  61. */
  62. public function member_auth_list()
  63. {
  64. return $this->hasMany(UserAuth::class, 'auth_user_id', 'user_id');
  65. }
  66. /**
  67. * Notes:关联未审核信息
  68. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  69. * User: yym
  70. * Date: 2022/8/4
  71. */
  72. public function member_auth()
  73. {
  74. return $this->hasOne(UserAuth::class, 'auth_user_id', 'user_id')->whereIn('auth_status', [UserAuth::WAIT, UserAuth::RETURN, UserAuth::CANCEL]);
  75. }
  76. /**
  77. * Notes:一对多关联会员权益包
  78. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  79. * User: yym
  80. * Date: 2022/8/5
  81. */
  82. public function members_package()
  83. {
  84. return $this->hasMany(UserPackageLog::class, 'log_user_id', 'user_id')->whereIn('log_package_status', [UserPackageLog::NORMAL]);
  85. }
  86. /**
  87. * Notes:Users插入会员
  88. * @param int $mobile
  89. * @return int
  90. * User: yym
  91. * Date: 2022/7/25
  92. */
  93. public static function insertUsers(int $mobile): int
  94. {
  95. $insert = array(
  96. 'user_name' => $mobile,
  97. 'user_category' => 'MEMBER',
  98. 'user_addtimes' => time()
  99. );
  100. return static::insertGetId($insert);
  101. }
  102. /**
  103. * Notes:获取会员信息
  104. * @param int $user_id
  105. * @return object|null
  106. * User: yym
  107. * Date: 2022/7/26
  108. */
  109. public static function getUserInfo(int $user_id)
  110. {
  111. return static::where(['user.user_id' => $user_id])
  112. ->select('user.*')
  113. ->with(['member_auth_list', 'member_auth', 'members_package' => function($query){
  114. $query->join('package', 'package_id', '=', 'user_package_log.log_package_id');
  115. }])
  116. ->first();
  117. }
  118. /**
  119. * Notes:获取会员列表
  120. * @param string $keywords
  121. * @param int $category
  122. * @param int $status
  123. * @param int $page
  124. * @param int $limit
  125. * @return array
  126. * User: yym
  127. * Date: 2022/8/4
  128. */
  129. public static function getUserList(string $keywords, int $category, int $status, int $page, int $limit)
  130. {
  131. $list = static::where('user.user_id', '>', 0)
  132. ->select('user.*')
  133. ->with(['member_auth_list', 'member_auth', 'members_package' => function($query){
  134. $query->select('user_package_log.*', 'package.*', 'package_price as package_money')->join('package', 'package_id', '=', 'user_package_log.log_package_id');
  135. }])
  136. ->when($keywords != '', function ($query) use ($keywords){
  137. $query->where(function ($query1) use ($keywords){
  138. $query1->where('user.user_account', 'like', '%' . $keywords . '%')
  139. ->orWhere('user.user_phone', 'like', '%' . $keywords . '%')
  140. ->orWhere('user.user_real_name', 'like', '%' . $keywords . '%');
  141. });
  142. })
  143. ->when($category !== '', function ($query) use ($category){
  144. $query->where('user.user_type', $category);
  145. })
  146. ->when($status != '', function ($query) use ($status){
  147. $ids = UserAuth::getListIds($status);
  148. $member_ids = array(0);
  149. if(!empty($ids))
  150. {
  151. $member_ids = array_unique(array_column($ids, 'auth_user_id'));
  152. }
  153. $query->whereIn('user.user_id', $member_ids);
  154. })
  155. ->orderBy('user.user_create_time', 'desc')
  156. ->forPage($page, $limit)
  157. ->get()
  158. ->toArray();
  159. $count = static::where('user.user_id', '>', 0)
  160. ->when($keywords != '', function ($query) use ($keywords){
  161. $query->where(function ($query1) use ($keywords){
  162. $query1->where('user.user_account', 'like', '%' . $keywords . '%')
  163. ->orWhere('user.user_phone', 'like', '%' . $keywords . '%')
  164. ->orWhere('user.user_real_name', 'like', '%' . $keywords . '%');
  165. });
  166. })
  167. ->when($category !== '', function ($query) use ($category){
  168. $query->where('user.user_type', $category);
  169. })
  170. ->when($status != '', function ($query) use ($status){
  171. $ids = UserAuth::getListIds($status);
  172. $member_ids = array(0);
  173. if(!empty($ids))
  174. {
  175. $member_ids = array_unique(array_column($ids, 'auth_user_id'));
  176. }
  177. $query->whereIn('user.user_id', $member_ids);
  178. })
  179. ->count();
  180. return [$list, $count];
  181. }
  182. /**
  183. * Notes:根据条件搜索会员列表
  184. * @param string $keywords
  185. * @param string $field
  186. * @return array
  187. * User: yym
  188. * Date: 2022/8/12
  189. */
  190. public static function getUserListKeywords(string $keywords, string $field)
  191. {
  192. return static::select($field)
  193. ->leftJoin('members', 'member_id', '=', 'users.user_id')
  194. ->where('members.member_name', 'like', '%' . $keywords . '%')
  195. ->orWhere('members.member_mobile', 'like', '%' . $keywords . '%')
  196. ->orWhere('users.user_name', 'like', '%' . $keywords . '%')
  197. ->get()
  198. ->toArray();
  199. }
  200. /**
  201. * Notes:根据主键更新会员信息
  202. * @param int $member_id
  203. * @param array $update
  204. * @return false|int
  205. * User: yym
  206. * Date: 2022/7/28
  207. */
  208. public static function updateMember(int $member_id, array $update)
  209. {
  210. if(empty($update) || $member_id <= 0)
  211. {
  212. return false;
  213. }
  214. return static::where(['user_id' => $member_id])->update($update);
  215. }
  216. /**
  217. * Notes:获取用户信息
  218. * @param int $member_id
  219. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
  220. * User: yym
  221. * Date: 2022/9/14
  222. */
  223. public static function getMemberInfo(int $member_id)
  224. {
  225. return static::where(['user_id' => $member_id])->first();
  226. }
  227. /**
  228. * Notes:用户账号唯一判断
  229. * @param int $user_account
  230. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
  231. * User: ZQ
  232. * Date: 2022/11/21
  233. */
  234. public static function getUserAccount($user_account)
  235. {
  236. return static::where(['user_account' => $user_account])->first();
  237. }
  238. /**
  239. * Notes:用户身份证号唯一判断
  240. * @param int $user_account
  241. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
  242. * User: ZQ
  243. * Date: 2022/11/21
  244. */
  245. public static function getUserCard($user_card_id)
  246. {
  247. return static::where(['user_card_id' => $user_card_id])->first();
  248. }
  249. }