UserDao.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\user;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\user\User;
  15. use think\Collection;
  16. use think\db\BaseQuery;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\facade\Db;
  21. use think\Model;
  22. /**
  23. * Class UserDao
  24. * @package app\common\dao\user
  25. * @author xaboy
  26. * @day 2020-04-28
  27. */
  28. class UserDao extends BaseDao
  29. {
  30. /**
  31. * @return BaseModel
  32. * @author xaboy
  33. * @day 2020-03-30
  34. */
  35. protected function getModel(): string
  36. {
  37. return User::class;
  38. }
  39. /**
  40. * @return string
  41. * @author xaboy
  42. * @day 2020/6/22
  43. */
  44. public function defaultPwd()
  45. {
  46. return substr(md5(time() . random_int(10, 99)), 0, 8);
  47. }
  48. /**
  49. * @param array $where
  50. * @return BaseQuery
  51. * @author xaboy
  52. * @day 2020-05-07
  53. */
  54. public function search(array $where)
  55. {
  56. if (isset($where['province']) && $where['province'] !== '') {
  57. $query = User::hasWhere('wechat', function ($query) use ($where) {
  58. $query->where('province', $where['province']);
  59. if ($where['city'] !== '') $query->where('city', $where['city']);
  60. });
  61. } else {
  62. $query = User::getDB()->alias('User');
  63. }
  64. $query->whereNull('User.cancel_time')
  65. ->when(isset($where['keyword']) && $where['keyword'], function (BaseQuery $query) use ($where) {
  66. return $query->where('User.uid|User.real_name|User.nickname|User.phone', 'like', '%' . $where['keyword'] . '%');
  67. })->when(isset($where['user_type']) && $where['user_type'] !== '', function (BaseQuery $query) use ($where) {
  68. return $query->where('User.user_type', $where['user_type']);
  69. })->when(isset($where['uid']) && $where['uid'] !== '', function (BaseQuery $query) use ($where) {
  70. return $query->where('User.uid', $where['uid']);
  71. })->when(isset($where['status']) && $where['status'] !== '', function (BaseQuery $query) use ($where) {
  72. return $query->where('User.status', intval($where['status']));
  73. })->when(isset($where['group_id']) && $where['group_id'], function (BaseQuery $query) use ($where) {
  74. return $query->where('User.group_id', intval($where['group_id']));
  75. })->when(isset($where['brokerage_level']) && $where['brokerage_level'], function (BaseQuery $query) use ($where) {
  76. return $query->where('User.brokerage_level', intval($where['brokerage_level']));
  77. })->when(isset($where['label_id']) && $where['label_id'] !== '', function (BaseQuery $query) use ($where) {
  78. return $query->whereRaw('CONCAT(\',\',User.label_id,\',\') LIKE \'%,' . $where['label_id'] . ',%\'');
  79. })->when(isset($where['sex']) && $where['sex'] !== '', function (BaseQuery $query) use ($where) {
  80. return $query->where('User.sex', intval($where['sex']));
  81. })->when(isset($where['is_promoter']) && $where['is_promoter'] !== '', function (BaseQuery $query) use ($where) {
  82. return $query->where('User.is_promoter', $where['is_promoter']);
  83. })->when(isset($where['phone']) && $where['phone'] !== '', function (BaseQuery $query) use ($where) {
  84. return $query->whereLike('User.phone', "%{$where['phone']}%");
  85. })->when(isset($where['nickname']) && $where['nickname'] !== '', function (BaseQuery $query) use ($where) {
  86. return $query->whereLike('User.nickname', "%{$where['nickname']}%");
  87. })->when(isset($where['spread_time']) && $where['spread_time'] !== '', function (BaseQuery $query) use ($where) {
  88. getModelTime($query, $where['spread_time'], 'User.spread_time');
  89. })->when(isset($where['date']) && $where['date'] !== '', function (BaseQuery $query) use ($where) {
  90. getModelTime($query, $where['date'], 'User.create_time');
  91. })->when(isset($where['promoter_date']) && $where['promoter_date'] !== '', function (BaseQuery $query) use ($where) {
  92. getModelTime($query, $where['promoter_date'], 'User.promoter_time');
  93. })->when(isset($where['spread_uid']) && $where['spread_uid'] !== '', function (BaseQuery $query) use ($where) {
  94. return $query->where('User.spread_uid', intval($where['spread_uid']));
  95. })->when(isset($where['spread_uids']), function (BaseQuery $query) use ($where) {
  96. return $query->whereIn('User.spread_uid', $where['spread_uids']);
  97. })->when(isset($where['uids']), function (BaseQuery $query) use ($where) {
  98. return $query->whereIn('User.uid', $where['uids']);
  99. })->when(isset($where['pay_count']) && $where['pay_count'] !== '', function ($query) use ($where) {
  100. if ($where['pay_count'] == -1) {
  101. $query->where('User.pay_count', 0);
  102. } else {
  103. $query->where('User.pay_count', '>', $where['pay_count']);
  104. }
  105. })->when(isset($where['user_time_type']) && $where['user_time_type'] !== '' && $where['user_time'] != '', function ($query) use ($where) {
  106. if ($where['user_time_type'] == 'visit') {
  107. getModelTime($query, $where['user_time'], 'User.last_time');
  108. }
  109. if ($where['user_time_type'] == 'add_time') {
  110. getModelTime($query, $where['user_time'], 'User.create_time');
  111. }
  112. })->when(isset($where['sort']) && in_array($where['sort'], ['pay_count ASC', 'pay_count DESC', 'pay_price DESC', 'pay_price ASC', 'spread_count ASC', 'spread_count DESC']), function (BaseQuery $query) use ($where) {
  113. $query->order('User.' . $where['sort']);
  114. }, function ($query) {
  115. $query->order('User.uid DESC');
  116. })->when(isset($where['is_svip']) && $where['is_svip'] !== '', function (BaseQuery $query) use ($where) {
  117. return $query->where('User.is_svip','>',0);
  118. })->when(isset($where['svip_type']) && $where['svip_type'] !== '', function (BaseQuery $query) use ($where) {
  119. return $query->where('User.is_svip',$where['svip_type']);
  120. });
  121. return $query;
  122. }
  123. /**
  124. * @param $keyword
  125. * @return BaseQuery
  126. * @author xaboy
  127. * @day 2020/6/22
  128. */
  129. public function searchMerUser($keyword)
  130. {
  131. return User::getDB()->whereLike('nickname', "%$keyword%")->whereNull('cancel_time')->where('status', 1);
  132. }
  133. /**
  134. * @param array $ids
  135. * @param int $group_id
  136. * @return int
  137. * @throws DbException
  138. * @author xaboy
  139. * @day 2020-05-07
  140. */
  141. public function batchChangeGroupId(array $ids, int $group_id)
  142. {
  143. return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('group_id'));
  144. }
  145. /**
  146. * @param array $ids
  147. * @param array $label_id
  148. * @return int
  149. * @throws DbException
  150. * @author xaboy
  151. * @day 2020-05-07
  152. */
  153. public function batchChangeLabelId(array $ids, array $label_id)
  154. {
  155. $label_id = implode(',', $label_id);
  156. return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('label_id'));
  157. }
  158. /**
  159. * @param int $id
  160. * @return array|Model|null
  161. * @throws DataNotFoundException
  162. * @throws DbException
  163. * @throws ModelNotFoundException
  164. * @author xaboy
  165. * @day 2020-04-28
  166. */
  167. public function wechatUserIdBytUser(int $id)
  168. {
  169. return User::getDB()->where('wechat_user_id', $id)->find();
  170. }
  171. /**
  172. * @param $id
  173. * @return mixed
  174. * @author xaboy
  175. * @day 2020-04-28
  176. */
  177. public function wechatUserIdByUid($id)
  178. {
  179. return User::getDB()->where('wechat_user_id', $id)->value('uid');
  180. }
  181. /**
  182. * @param $id
  183. * @return mixed
  184. * @author xaboy
  185. * @day 2020/7/7
  186. */
  187. public function uidByWechatUserId($id)
  188. {
  189. return User::getDB()->where('uid', $id)->value('wechat_user_id');
  190. }
  191. /**
  192. * @param $account
  193. * @return array|Model|null
  194. * @throws DataNotFoundException
  195. * @throws DbException
  196. * @throws ModelNotFoundException
  197. * @author xaboy
  198. * @day 2020/6/22
  199. */
  200. public function accountByUser($account)
  201. {
  202. return User::getDB()->where('account', $account)->find();
  203. }
  204. /**
  205. * @param $uid
  206. * @return array
  207. * @author xaboy
  208. * @day 2020/6/22
  209. */
  210. public function getSubIds($uid)
  211. {
  212. return User::getDB()->where('spread_uid', $uid)->column('uid');
  213. }
  214. /**
  215. * @param $uid
  216. * @return int
  217. * @author xaboy
  218. * @day 2020/6/22
  219. */
  220. public function getOneLevelCount($uid)
  221. {
  222. return User::getDB()->where('spread_uid', $uid)->count();
  223. }
  224. /**
  225. * @param $uid
  226. * @return int
  227. * @author xaboy
  228. * @day 2020/6/22
  229. */
  230. public function getTwoLevelCount($uid)
  231. {
  232. $ids = $this->getSubIds($uid);
  233. return count($ids) ? User::getDB()->whereIn('spread_uid', $ids)->count() : 0;
  234. }
  235. /**
  236. * @param $ids
  237. * @return array
  238. * @author xaboy
  239. * @day 2020/6/22
  240. */
  241. public function getSubAllIds(array $ids)
  242. {
  243. return User::getDB()->whereIn('spread_uid', $ids)->column('uid');
  244. }
  245. /**
  246. * @param array $ids
  247. * @param string $field
  248. * @return Collection
  249. * @throws DataNotFoundException
  250. * @throws DbException
  251. * @throws ModelNotFoundException
  252. * @author xaboy
  253. * @day 2020/6/22
  254. */
  255. public function users(array $ids, $field = '*')
  256. {
  257. return User::getDB()->whereIn('uid', $ids)->field($field)->select();
  258. }
  259. public function newUserNum($date)
  260. {
  261. return User::getDB()->when($date, function ($query, $date) {
  262. getModelTime($query, $date, 'create_time');
  263. })->count();
  264. }
  265. public function userOrderDetail($uid)
  266. {
  267. return User::getDB()->alias('A')
  268. ->join('StoreOrder B', 'A.uid = B.uid and B.paid = 1 and B.pay_time between \'' . date('Y/m/d', strtotime('first day of')) . ' 00:00:00\' and \'' . date('Y/m/d H:i:s') . '\'')
  269. ->join('PresellOrder C', 'C.order_id = B.order_id and C.paid = 1', 'LEFT')
  270. ->field('A.uid,A.avatar,A.nickname,A.now_money,A.pay_price,A.pay_count, sum(B.pay_price + IFNULL(C.pay_price,0)) as total_pay_price, count(B.order_id) as total_pay_count,is_svip,svip_endtime,svip_save_money')
  271. ->where('A.uid', $uid)
  272. ->find();
  273. }
  274. public function userNumGroup($date)
  275. {
  276. return User::getDB()->when($date, function ($query, $date) {
  277. getModelTime($query, $date, 'create_time');
  278. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(uid) as new'))
  279. ->group('time')->order('time ASC')->select();
  280. }
  281. public function idsByPayCount(array $ids)
  282. {
  283. return User::getDB()->whereIn('uid', $ids)->column('pay_count', 'uid');
  284. }
  285. public function beforeUserNum($date)
  286. {
  287. return User::getDB()->where('create_time', '<', $date)->count();
  288. }
  289. public function selfUserList($phone)
  290. {
  291. return User::getDB()->where('phone', $phone)->field('uid,nickname,avatar,user_type')->select();
  292. }
  293. public function initSpreadLimitDay(int $day)
  294. {
  295. return User::getDB()->where('spread_uid', '>', 0)->update(['spread_limit' => date('Y-m-d H:i:s', strtotime("+ $day day"))]);
  296. }
  297. public function clearSpreadLimitDay()
  298. {
  299. return User::getDB()->where('spread_uid', '>', 0)->update(['spread_limit' => null]);
  300. }
  301. public function updateSpreadLimitDay(int $day)
  302. {
  303. User::getDB()->where('spread_uid', '>', 0)->whereNull('spread_limit')->update(['spread_limit' => date('Y-m-d H:i:s', strtotime("+ $day day"))]);
  304. return User::getDB()->where('spread_uid', '>', 0)->whereNotNull('spread_limit')->update(['spread_limit' => Db::raw('TIMESTAMPADD(DAY, ' . $day . ', `spread_limit`)')]);
  305. }
  306. public function syncSpreadStatus()
  307. {
  308. return User::getDB()->where('spread_uid', '>', 0)->whereNotNull('spread_limit')->where('spread_limit', '<=', date('Y-m-d H:i:s'))->update(['spread_time' => null, 'spread_uid' => 0, 'spread_limit' => null]);
  309. }
  310. public function incSpreadCount($uid)
  311. {
  312. User::getDB()->where('uid', $uid)->update([
  313. 'spread_count' => Db::raw('spread_count + 1')
  314. ]);
  315. }
  316. public function decSpreadCount($uid)
  317. {
  318. User::getDB()->where('uid', $uid)->update([
  319. 'spread_count' => Db::raw('spread_count - 1')
  320. ]);
  321. }
  322. }