UserDao.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-28
  7. *
  8. *
  9. */
  10. namespace app\common\dao\user;
  11. use app\common\dao\BaseDao;
  12. use app\common\enum\user\UserEnum;
  13. use app\common\model\BaseModel;
  14. use app\common\model\user\User;
  15. use app\common\repositories\user\UserRepository;
  16. use app\entity\CommonEntity;
  17. use app\entity\data\user\UserEntity;
  18. use think\db\BaseQuery;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. use think\facade\Db;
  23. use think\Model;
  24. /**
  25. * Class UserDao
  26. * @package app\common\dao\user
  27. * @author xaboy
  28. * @day 2020-04-28
  29. */
  30. class UserDao extends BaseDao
  31. {
  32. /**
  33. * @return BaseModel
  34. * @author xaboy
  35. * @day 2020-03-30
  36. */
  37. protected function getModel(): string
  38. {
  39. return User::class;
  40. }
  41. public function getUserGroupMap():array
  42. {
  43. return [
  44. 1 => '消费者',
  45. 2 => 'v客',
  46. 3 => '大v经理',
  47. 4 => '创客',
  48. 5 => '区县代理',
  49. 6 => '省级代理',
  50. 7 => '创客合伙人',
  51. ];
  52. }
  53. /**
  54. * @return string
  55. * @author xaboy
  56. * @day 2020/6/22
  57. */
  58. public function defaultPwd()
  59. {
  60. return '123456';
  61. }
  62. /**
  63. * @param array $where
  64. * @return BaseQuery
  65. * @author xaboy
  66. * @day 2020-05-07
  67. */
  68. public function search(array $where)
  69. {
  70. $get_is_show=(app()->make(UserRepository::class))->get_level_list();
  71. // ->when($where['identity'] >= 0 , function (BaseQuery $query) use ($where) {
  72. // return $query->where('User.identity', intval($where['identity']));
  73. // })
  74. if (isset($where['province']) && $where['province'] !== '') {
  75. $query = User::hasWhere('wechat', function ($query) use ($where) {
  76. $query->where('province', $where['province']);
  77. if ($where['city'] !== '') $query->where('city', $where['city']);
  78. });
  79. } else {
  80. $query = User::getDB()->alias('User');
  81. }
  82. $query->hasWhere('userRich',[],'*','left');
  83. // $query->hasWhere('areaManage',[],'*','left');
  84. $query->when(isset($where['keyword']) && $where['keyword'], function (BaseQuery $query) use ($where) {
  85. return $query->where('User.real_name|User.nickname|User.phone', 'like', '%' . $where['keyword'] . '%');
  86. })->when($where['identity']!=='' && $where['identity'] >= 0 , function (BaseQuery $query) use ($where) {
  87. return $query->where('User.identity', intval($where['identity']));
  88. })->when($get_is_show, function ($query) {
  89. return $query->where('User.is_show',1);
  90. })->when(isset($where['user_type']) && $where['user_type'] !== '', function (BaseQuery $query) use ($where) {
  91. return $query->where('User.user_type', $where['user_type']);
  92. })->when(isset($where['status']) && $where['status'] !== '', function (BaseQuery $query) use ($where) {
  93. return $query->where('User.status', intval($where['status']));
  94. })->when(isset($where['group_id']) && $where['group_id'], function (BaseQuery $query) use ($where) {
  95. return $query->where('User.group_id', intval($where['group_id']));
  96. })->when(isset($where['group_id']) && $where['group_id'], function (BaseQuery $query) use ($where) {
  97. return $query->where('User.group_id', intval($where['group_id']));
  98. })->when(isset($where['user_group']) && $where['user_group'], function (BaseQuery $query) use ($where) {
  99. return $query->where('User.user_group', intval($where['user_group']));
  100. })->when(isset($where['label_id']) && $where['label_id'] !== '', function (BaseQuery $query) use ($where) {
  101. return $query->whereRaw('CONCAT(\',\',User.label_id,\',\') LIKE \'%,' . $where['label_id'] . ',%\'');
  102. })->when(isset($where['sex']) && $where['sex'] !== '', function (BaseQuery $query) use ($where) {
  103. return $query->where('User.sex', intval($where['sex']));
  104. })->when(isset($where['is_promoter']) && $where['is_promoter'] !== '', function (BaseQuery $query) use ($where) {
  105. return $query->where('User.is_promoter', $where['is_promoter']);
  106. })->when(isset($where['nickname']) && $where['nickname'] !== '', function (BaseQuery $query) use ($where) {
  107. return $query->whereLike('User.nickname', "%{$where['nickname']}%");
  108. })->when(isset($where['account']) && $where['account'] !== '', function (BaseQuery $query) use ($where) {
  109. return $query->whereLike('User.account', "%{$where['account']}%");
  110. })->when(isset($where['spread_time']) && $where['spread_time'] !== '', function (BaseQuery $query) use ($where) {
  111. getModelTime($query, $where['spread_time'], 'User.spread_time');
  112. })->when(isset($where['date']) && $where['date'] !== '', function (BaseQuery $query) use ($where) {
  113. getModelTime($query, $where['date'], 'User.create_time');
  114. })->when(isset($where['spread_uid']) && $where['spread_uid'] !== '', function (BaseQuery $query) use ($where) {
  115. return $query->where('User.spread_uid', intval($where['spread_uid']));
  116. })->when(isset($where['spread_uids']), function (BaseQuery $query) use ($where) {
  117. return $query->whereIn('User.spread_uid', $where['spread_uids']);
  118. })->when(isset($where['is_show']) && $where['is_show'] !== '', function (BaseQuery $query) use ($where) {
  119. return $query->where('User.is_show', $where['is_show']);
  120. })->when(isset($where['pay_count']) && $where['pay_count'] !== '', function ($query) use ($where) {
  121. if($where['pay_count'] == -1 ){
  122. $query->where('User.pay_count', 0);
  123. }else{
  124. $query->where('User.pay_count', '>', $where['pay_count']);
  125. }
  126. })->when(isset($where['user_time_type']) && $where['user_time_type'] !== '' && $where['user_time'] != '', function ($query) use ($where) {
  127. list($startTime, $endTime) = explode('-', $where['user_time']);
  128. if ($where['user_time_type'] == 'visitno') {
  129. $query->whereTime("User.last_time", 'between', [$startTime, $endTime]);
  130. }
  131. if ($where['user_time_type'] == 'visit') {
  132. $query->whereTime('User.last_time', '>=', $startTime);
  133. $query->whereTime('User.last_time', '<', $endTime);
  134. }
  135. if ($where['user_time_type'] == 'add_time') {
  136. $query->whereTime('User.create_time', '>=', $startTime);
  137. $query->whereTime('User.create_time', '<', $endTime);
  138. }
  139. })->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) {
  140. $query->order('User.' . $where['sort']);
  141. })->when(isset($where['now_money']) , function (BaseQuery $query) use ($where) {
  142. if($where['now_money'] == 1 ){
  143. $query->order('User.now_money', "asc");
  144. }elseif($where['now_money'] == 2 ){
  145. $query->order('User.now_money', "desc");
  146. }
  147. }, function ($query) {
  148. $query->order('User.uid DESC');
  149. })->when((isset($where['user_rich_type']) && $where['user_rich_type']!=''), function ($query) use ($where) {
  150. if( $where['user_rich_type']=='0'){
  151. $query->where('userRich.id is null or userRich.type=0');
  152. }else {
  153. $query->where('userRich.type', $where['user_rich_type']);
  154. }
  155. })->when( (isset($where['stockholder']) && $where['stockholder']!='') , function ($query) use ($where) {
  156. $query->where('userRich.stockholder' , $where['stockholder']);
  157. })->when( (isset($where['subIdentify']) && $where['subIdentify']!='') , function ($query) use ($where) {
  158. $query->where('areaManage.type' , $where['subIdentify']);
  159. })->when(isset($where['uid']) , function ($query) use ($where) {
  160. $query->whereIn('User.uid' , $where['uid']);
  161. });
  162. $company = input('company','');
  163. $identify = input('identify','');
  164. $query->when( $company==1 , function ($query) {
  165. $query->join('enterprise_user eu','eu.uid=user.uid','left')->where('eu.mer_id',496);
  166. })->when( $company==1 && $identify , function ($query) use ($identify) {
  167. $query->where('eu.identity',$identify);
  168. })->when( $company==2 , function ($query) {
  169. //国盛小程序还未开发
  170. $query->where('user.uid',0);
  171. });
  172. return $query;
  173. }
  174. /**
  175. * @param $keyword
  176. * @return BaseQuery
  177. * @author xaboy
  178. * @day 2020/6/22
  179. */
  180. public function searchMerUser($keyword)
  181. {
  182. return User::getDB()->whereLike('nickname', "%$keyword%")->where('status', 1);
  183. }
  184. /**
  185. * @param array $ids
  186. * @param int $group_id
  187. * @return int
  188. * @throws DbException
  189. * @author xaboy
  190. * @day 2020-05-07
  191. */
  192. public function batchChangeGroupId(array $ids, int $group_id)
  193. {
  194. return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('group_id'));
  195. }
  196. /**
  197. * @param array $ids
  198. * @param array $label_id
  199. * @return int
  200. * @throws DbException
  201. * @author xaboy
  202. * @day 2020-05-07
  203. */
  204. public function batchChangeLabelId(array $ids, array $label_id)
  205. {
  206. $label_id = implode(',', $label_id);
  207. return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('label_id'));
  208. }
  209. /**
  210. * @param int $id
  211. * @return array|Model|null
  212. * @throws DataNotFoundException
  213. * @throws DbException
  214. * @throws ModelNotFoundException
  215. * @author xaboy
  216. * @day 2020-04-28
  217. */
  218. public function wechatUserIdBytUser(int $id)
  219. {
  220. return User::getDB()->where('wechat_user_id', $id)->find();
  221. }
  222. /**
  223. * @param $id
  224. * @return mixed
  225. * @author xaboy
  226. * @day 2020-04-28
  227. */
  228. public function wechatUserIdByUid($id)
  229. {
  230. return User::getDB()->where('wechat_user_id', $id)->value('uid');
  231. }
  232. /**
  233. * @param $id
  234. * @return mixed
  235. * @author xaboy
  236. * @day 2020/7/7
  237. */
  238. public function uidByWechatUserId($id)
  239. {
  240. return User::getDB()->where('uid', $id)->value('wechat_user_id');
  241. }
  242. /**
  243. * @param $account
  244. * @return array|Model|null
  245. * @throws DataNotFoundException
  246. * @throws DbException
  247. * @throws ModelNotFoundException
  248. * @author xaboy
  249. * @day 2020/6/22
  250. */
  251. public function accountByUser($account)
  252. {
  253. return User::getDB()->where('account', $account)->where('is_del', 0)->find();
  254. }
  255. public function accountLikeByUser($account)
  256. {
  257. return User::getDB()->where('account', 'like',$account)->where('is_del', 0)->select()->toArray();
  258. }
  259. public function nicknameLikeByUser($nickname)
  260. {
  261. return User::getDB()->where('nickname', 'like', $nickname)->where('is_del', 0)->select()->toArray();
  262. }
  263. /**
  264. * @param $uid
  265. * @return array
  266. * @author xaboy
  267. * @day 2020/6/22
  268. */
  269. public function getSubIds($uid)
  270. {
  271. return User::getDB()->where('spread_uid', $uid)->column('uid');
  272. }
  273. /**
  274. * @param $uid
  275. * @return int
  276. * @author xaboy
  277. * @day 2020/6/22
  278. */
  279. public function getOneLevelCount($uid)
  280. {
  281. return User::getDB()->where('spread_uid', $uid)->count();
  282. }
  283. /**
  284. * @param $uid
  285. * @return int
  286. * @author xaboy
  287. * @day 2020/6/22
  288. */
  289. public function getTwoLevelCount($uid)
  290. {
  291. $ids = $this->getSubIds($uid);
  292. return count($ids) ? User::getDB()->whereIn('spread_uid', $ids)->count() : 0;
  293. }
  294. /**
  295. * @param $ids
  296. * @return array
  297. * @author xaboy
  298. * @day 2020/6/22
  299. */
  300. public function getSubAllIds(array $ids)
  301. {
  302. return User::getDB()->whereIn('spread_uid', $ids)->column('uid');
  303. }
  304. /**
  305. * @param array $ids
  306. * @param string $field
  307. * @return \think\Collection
  308. * @throws DataNotFoundException
  309. * @throws DbException
  310. * @throws ModelNotFoundException
  311. * @author xaboy
  312. * @day 2020/6/22
  313. */
  314. public function users(array $ids, $field = '*')
  315. {
  316. return User::getDB()->whereIn('uid', $ids)->field($field)->select();
  317. }
  318. public function newUserNum($date, $where = [])
  319. {
  320. return User::getDB()->where($where)->when($date, function ($query, $date) {
  321. getModelTime($query, $date, 'create_time');
  322. })->count();
  323. }
  324. public function userOrderDetail($uid)
  325. {
  326. return User::getDB()->alias('A')
  327. ->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') . '\'')
  328. ->field('A.*,A.brokerage_price as commission, A.brokerage_price as brokerage_price, A.score as green_integral, A.score as score, A.create_time as create_time,A.uid,A.avatar,A.nickname,A.phone,A.now_money,A.pay_price,A.pay_count, sum(B.pay_price) as total_pay_price, count(B.order_id) as total_pay_count')
  329. ->where('A.uid', $uid)
  330. ->find();
  331. }
  332. public function userNumGroup($date)
  333. {
  334. return User::getDB()->when($date, function ($query, $date) {
  335. getModelTime($query, $date, 'create_time');
  336. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(uid) as new'))
  337. ->group('time')->order('time ASC')->select();
  338. }
  339. public function userNumGroupHour($date)
  340. {
  341. return User::getDB()->when($date, function ($query, $date) {
  342. getModelTime($query, $date, 'create_time');
  343. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%H\') as time, count(uid) as new'))
  344. ->group('time')->order('time ASC')->select();
  345. }
  346. public function userNumGroupYear($date)
  347. {
  348. return User::getDB()->when($date, function ($query, $date) {
  349. getModelTime($query, $date, 'create_time');
  350. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m\') as time, count(uid) as new'))
  351. ->group('time')->order('time ASC')->select();
  352. }
  353. public function beforeUserNum($date)
  354. {
  355. return User::getDB()->where('create_time', '<', $date)->count();
  356. }
  357. public function selfUserList($phone)
  358. {
  359. return User::getDB()->where('phone', $phone)->field('uid,nickname,avatar,user_type')->select();
  360. }
  361. /**
  362. * 获取测试用户 uid List
  363. * @return array
  364. */
  365. public function getTestUidList(): array
  366. {
  367. return $this::getModel()::getDB()->where('test_user', '=', 1)->column('uid');
  368. }
  369. /**
  370. * 获取删除用户 uid List
  371. * @return array
  372. */
  373. public function getDelUidList(): array
  374. {
  375. return $this::getModel()::getDB()->where('is_del', '=', 1)->column('uid');
  376. }
  377. /**
  378. * 获取删除用户 uid List
  379. * @return array
  380. */
  381. public function getInvalidUidList(): array
  382. {
  383. return $this::getModel()::getDB()->where('test_user', '=', 1)->whereOr('is_del', '=', 1)->column('uid');
  384. }
  385. /**
  386. * @param array $uidList
  387. * @return UserEntity[]
  388. */
  389. public function getUserEntityListByUidList(array $uidList): array
  390. {
  391. $entityList = [];
  392. try {
  393. $dataList = User::getDB()
  394. ->whereIn('uid', $uidList)
  395. ->where('is_del', '=', UserEnum::IS_DEL['No']['code'])
  396. ->select()
  397. ->toArray();
  398. if (!empty($dataList)) {
  399. $entityList = array_map(function ($data) {
  400. return UserEntity::newInstance($data);
  401. }, $dataList);
  402. }
  403. } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
  404. }
  405. return $entityList;
  406. }
  407. /**
  408. * 批量更新
  409. * @param $dataList
  410. * @return array
  411. */
  412. public function saveAll($dataList): array
  413. {
  414. try {
  415. return $this->getModel()::getInstance()->allowField(array_keys(reset($dataList)))->saveAll($dataList)->toArray();
  416. } catch (\Exception $e) {
  417. return [];
  418. }
  419. }
  420. /**
  421. * @param $userId
  422. * @return array
  423. * 获取直推用户ID
  424. */
  425. public function getDirectUserIds($userId)
  426. {
  427. return $this::getModel()::getDB()->where('level_id', '=', $userId)->column('uid');
  428. }
  429. /**
  430. * 通过uid获取spreadUid
  431. */
  432. public function getSpreadUidFromUid($uid)
  433. {
  434. try {
  435. return $this::getModel()::getDB()->where('uid', '=', $uid)->value('spread_uid');
  436. } catch (\Exception $e) {
  437. return [];
  438. }
  439. }
  440. /**
  441. * 根据spread查找用户信息
  442. */
  443. public function getSpreadUserFromUid($spreadUid)
  444. {
  445. try {
  446. return $this::getModel()::getDB()->where('uid', '=', $spreadUid)->find();
  447. } catch (\Exception $e) {
  448. return [];
  449. }
  450. }
  451. /**
  452. * @param $uid
  453. * @param $number
  454. * @return mixed
  455. * @author 史晨
  456. * @date 2026/2/27 15:15
  457. * 更新用户佣金
  458. */
  459. public function updateBrokeragePriceByUid($uid,$number)
  460. {
  461. return $this->getModel()::getDB()->where('uid', '=', $uid)->inc('brokerage_price',$number)->update();
  462. }
  463. public function updateWithdrawalLimitByUid($uid,$number)
  464. {
  465. return $this->getModel()::getDB()->where('uid', '=', $uid)->inc('withdrawal_limit',$number)->update();
  466. }
  467. }