UserDao.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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 '12345678';
  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->when(isset($where['keyword']) && $where['keyword'], function (BaseQuery $query) use ($where) {
  65. return $query->where('User.uid|User.real_name|User.nickname|User.phone', 'like', '%' . $where['keyword'] . '%');
  66. })->when(isset($where['user_type']) && $where['user_type'] !== '', function (BaseQuery $query) use ($where) {
  67. return $query->where('User.user_type', $where['user_type']);
  68. })->when(isset($where['status']) && $where['status'] !== '', function (BaseQuery $query) use ($where) {
  69. return $query->where('User.status', intval($where['status']));
  70. })->when(isset($where['group_id']) && $where['group_id'], function (BaseQuery $query) use ($where) {
  71. return $query->where('User.group_id', intval($where['group_id']));
  72. })->when(isset($where['label_id']) && $where['label_id'] !== '', function (BaseQuery $query) use ($where) {
  73. return $query->whereRaw('CONCAT(\',\',User.label_id,\',\') LIKE \'%,' . $where['label_id'] . ',%\'');
  74. })->when(isset($where['sex']) && $where['sex'] !== '', function (BaseQuery $query) use ($where) {
  75. return $query->where('User.sex', intval($where['sex']));
  76. })->when(isset($where['is_promoter']) && $where['is_promoter'] !== '', function (BaseQuery $query) use ($where) {
  77. return $query->where('User.is_promoter', $where['is_promoter']);
  78. })->when(isset($where['nickname']) && $where['nickname'] !== '', function (BaseQuery $query) use ($where) {
  79. return $query->whereLike('User.nickname', "%{$where['nickname']}%");
  80. })->when(isset($where['spread_time']) && $where['spread_time'] !== '', function (BaseQuery $query) use ($where) {
  81. getModelTime($query, $where['spread_time'], 'User.spread_time');
  82. })->when(isset($where['date']) && $where['date'] !== '', function (BaseQuery $query) use ($where) {
  83. getModelTime($query, $where['date'], 'User.create_time');
  84. })->when(isset($where['promoter_date']) && $where['promoter_date'] !== '', function (BaseQuery $query) use ($where) {
  85. getModelTime($query, $where['promoter_date'], 'User.promoter_time');
  86. })->when(isset($where['spread_uid']) && $where['spread_uid'] !== '', function (BaseQuery $query) use ($where) {
  87. return $query->where('User.spread_uid', intval($where['spread_uid']));
  88. })->when(isset($where['spread_uids']), function (BaseQuery $query) use ($where) {
  89. return $query->whereIn('User.spread_uid', $where['spread_uids']);
  90. })->when(isset($where['uids']), function (BaseQuery $query) use ($where) {
  91. return $query->whereIn('User.uid', $where['uids']);
  92. })->when(isset($where['pay_count']) && $where['pay_count'] !== '', function ($query) use ($where) {
  93. if ($where['pay_count'] == -1) {
  94. $query->where('User.pay_count', 0);
  95. } else {
  96. $query->where('User.pay_count', '>', $where['pay_count']);
  97. }
  98. })->when(isset($where['user_time_type']) && $where['user_time_type'] !== '' && $where['user_time'] != '', function ($query) use ($where) {
  99. if ($where['user_time_type'] == 'visit') {
  100. getModelTime($query, $where['user_time'], 'User.last_time');
  101. }
  102. if ($where['user_time_type'] == 'add_time') {
  103. getModelTime($query, $where['user_time'], 'User.create_time');
  104. }
  105. })->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) {
  106. $query->order('User.' . $where['sort']);
  107. }, function ($query) {
  108. $query->order('User.uid DESC');
  109. });
  110. return $query;
  111. }
  112. /**
  113. * @param $keyword
  114. * @return BaseQuery
  115. * @author xaboy
  116. * @day 2020/6/22
  117. */
  118. public function searchMerUser($keyword)
  119. {
  120. return User::getDB()->whereLike('nickname', "%$keyword%")->where('status', 1);
  121. }
  122. /**
  123. * @param array $ids
  124. * @param int $group_id
  125. * @return int
  126. * @throws DbException
  127. * @author xaboy
  128. * @day 2020-05-07
  129. */
  130. public function batchChangeGroupId(array $ids, int $group_id)
  131. {
  132. return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('group_id'));
  133. }
  134. /**
  135. * @param array $ids
  136. * @param array $label_id
  137. * @return int
  138. * @throws DbException
  139. * @author xaboy
  140. * @day 2020-05-07
  141. */
  142. public function batchChangeLabelId(array $ids, array $label_id)
  143. {
  144. $label_id = implode(',', $label_id);
  145. return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('label_id'));
  146. }
  147. /**
  148. * @param int $id
  149. * @return array|Model|null
  150. * @throws DataNotFoundException
  151. * @throws DbException
  152. * @throws ModelNotFoundException
  153. * @author xaboy
  154. * @day 2020-04-28
  155. */
  156. public function wechatUserIdBytUser(int $id)
  157. {
  158. return User::getDB()->where('wechat_user_id', $id)->find();
  159. }
  160. /**
  161. * @param $id
  162. * @return mixed
  163. * @author xaboy
  164. * @day 2020-04-28
  165. */
  166. public function wechatUserIdByUid($id)
  167. {
  168. return User::getDB()->where('wechat_user_id', $id)->value('uid');
  169. }
  170. /**
  171. * @param $id
  172. * @return mixed
  173. * @author xaboy
  174. * @day 2020/7/7
  175. */
  176. public function uidByWechatUserId($id)
  177. {
  178. return User::getDB()->where('uid', $id)->value('wechat_user_id');
  179. }
  180. /**
  181. * @param $account
  182. * @return array|Model|null
  183. * @throws DataNotFoundException
  184. * @throws DbException
  185. * @throws ModelNotFoundException
  186. * @author xaboy
  187. * @day 2020/6/22
  188. */
  189. public function accountByUser($account)
  190. {
  191. return User::getDB()->where('account', $account)->find();
  192. }
  193. /**
  194. * @param $uid
  195. * @return array
  196. * @author xaboy
  197. * @day 2020/6/22
  198. */
  199. public function getSubIds($uid)
  200. {
  201. return User::getDB()->where('spread_uid', $uid)->column('uid');
  202. }
  203. /**
  204. * @param $uid
  205. * @return int
  206. * @author xaboy
  207. * @day 2020/6/22
  208. */
  209. public function getOneLevelCount($uid)
  210. {
  211. return User::getDB()->where('spread_uid', $uid)->count();
  212. }
  213. /**
  214. * @param $uid
  215. * @return int
  216. * @author xaboy
  217. * @day 2020/6/22
  218. */
  219. public function getTwoLevelCount($uid)
  220. {
  221. $ids = $this->getSubIds($uid);
  222. return count($ids) ? User::getDB()->whereIn('spread_uid', $ids)->count() : 0;
  223. }
  224. /**
  225. * @param $ids
  226. * @return array
  227. * @author xaboy
  228. * @day 2020/6/22
  229. */
  230. public function getSubAllIds(array $ids)
  231. {
  232. return User::getDB()->whereIn('spread_uid', $ids)->column('uid');
  233. }
  234. /**
  235. * @param array $ids
  236. * @param string $field
  237. * @return Collection
  238. * @throws DataNotFoundException
  239. * @throws DbException
  240. * @throws ModelNotFoundException
  241. * @author xaboy
  242. * @day 2020/6/22
  243. */
  244. public function users(array $ids, $field = '*')
  245. {
  246. return User::getDB()->whereIn('uid', $ids)->field($field)->select();
  247. }
  248. public function newUserNum($date)
  249. {
  250. return User::getDB()->when($date, function ($query, $date) {
  251. getModelTime($query, $date, 'create_time');
  252. })->count();
  253. }
  254. public function userOrderDetail($uid)
  255. {
  256. return User::getDB()->alias('A')
  257. ->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') . '\'')
  258. ->join('PresellOrder C', 'C.order_id = B.order_id and C.paid = 1', 'LEFT')
  259. ->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')
  260. ->where('A.uid', $uid)
  261. ->find();
  262. }
  263. public function userNumGroup($date)
  264. {
  265. return User::getDB()->when($date, function ($query, $date) {
  266. getModelTime($query, $date, 'create_time');
  267. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(uid) as new'))
  268. ->group('time')->order('time ASC')->select();
  269. }
  270. public function idsByPayCount(array $ids)
  271. {
  272. return User::getDB()->whereIn('uid', $ids)->column('pay_count', 'uid');
  273. }
  274. public function beforeUserNum($date)
  275. {
  276. return User::getDB()->where('create_time', '<', $date)->count();
  277. }
  278. public function selfUserList($phone)
  279. {
  280. return User::getDB()->where('phone', $phone)->field('uid,nickname,avatar,user_type')->select();
  281. }
  282. public function initSpreadLimitDay(int $day)
  283. {
  284. return User::getDB()->where('spread_uid', '>', 0)->update(['spread_limit' => date('Y-m-d H:i:s', strtotime("+ $day day"))]);
  285. }
  286. public function clearSpreadLimitDay()
  287. {
  288. return User::getDB()->where('spread_uid', '>', 0)->update(['spread_limit' => null]);
  289. }
  290. public function updateSpreadLimitDay(int $day)
  291. {
  292. User::getDB()->where('spread_uid', '>', 0)->whereNull('spread_limit')->update(['spread_limit' => date('Y-m-d H:i:s', strtotime("+ $day day"))]);
  293. return User::getDB()->where('spread_uid', '>', 0)->whereNotNull('spread_limit')->update(['spread_limit' => Db::raw('TIMESTAMPADD(DAY, ' . $day . ', `spread_limit`)')]);
  294. }
  295. public function syncSpreadStatus()
  296. {
  297. 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]);
  298. }
  299. }