User.php 11 KB

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