UserDao.php 12 KB

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