UserDao.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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\model\BaseModel;
  13. use app\common\model\user\User;
  14. use app\common\repositories\user\UserRepository;
  15. use think\db\BaseQuery;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\facade\Db;
  20. use think\Model;
  21. /**
  22. * Class UserDao
  23. * @package app\common\dao\user
  24. * @author xaboy
  25. * @day 2020-04-28
  26. */
  27. class UserDao extends BaseDao
  28. {
  29. /**
  30. * @return BaseModel
  31. * @author xaboy
  32. * @day 2020-03-30
  33. */
  34. protected function getModel(): string
  35. {
  36. return User::class;
  37. }
  38. public function getUserGroupMap():array
  39. {
  40. return [
  41. 1 => '消费者',
  42. 2 => 'v客',
  43. 3 => '大v经理',
  44. 4 => '创客',
  45. 5 => '区县代理',
  46. 6 => '省级代理',
  47. 7 => '创客合伙人',
  48. ];
  49. }
  50. /**
  51. * @return string
  52. * @author xaboy
  53. * @day 2020/6/22
  54. */
  55. public function defaultPwd()
  56. {
  57. return '123456';
  58. }
  59. /**
  60. * @param array $where
  61. * @return BaseQuery
  62. * @author xaboy
  63. * @day 2020-05-07
  64. */
  65. public function search(array $where)
  66. {
  67. $get_is_show=(app()->make(UserRepository::class))->get_level_list();
  68. // ->when($where['identity'] >= 0 , function (BaseQuery $query) use ($where) {
  69. // return $query->where('User.identity', intval($where['identity']));
  70. // })
  71. if (isset($where['province']) && $where['province'] !== '') {
  72. $query = User::hasWhere('wechat', function ($query) use ($where) {
  73. $query->where('province', $where['province']);
  74. if ($where['city'] !== '') $query->where('city', $where['city']);
  75. });
  76. } else {
  77. $query = User::getDB()->alias('User');
  78. }
  79. $query->hasWhere('userRich',[],'*','left');
  80. // $query->hasWhere('areaManage',[],'*','left');
  81. $query->when(isset($where['keyword']) && $where['keyword'], function (BaseQuery $query) use ($where) {
  82. return $query->where('User.real_name|User.nickname|User.phone', 'like', '%' . $where['keyword'] . '%');
  83. })->when($where['identity']!=='' && $where['identity'] >= 0 , function (BaseQuery $query) use ($where) {
  84. return $query->where('User.identity', intval($where['identity']));
  85. })->when($get_is_show, function ($query) {
  86. return $query->where('User.is_show',1);
  87. })->when(isset($where['user_type']) && $where['user_type'] !== '', function (BaseQuery $query) use ($where) {
  88. return $query->where('User.user_type', $where['user_type']);
  89. })->when(isset($where['status']) && $where['status'] !== '', function (BaseQuery $query) use ($where) {
  90. return $query->where('User.status', intval($where['status']));
  91. })->when(isset($where['group_id']) && $where['group_id'], function (BaseQuery $query) use ($where) {
  92. return $query->where('User.group_id', intval($where['group_id']));
  93. })->when(isset($where['group_id']) && $where['group_id'], function (BaseQuery $query) use ($where) {
  94. return $query->where('User.group_id', intval($where['group_id']));
  95. })->when(isset($where['user_group']) && $where['user_group'], function (BaseQuery $query) use ($where) {
  96. return $query->where('User.user_group', intval($where['user_group']));
  97. })->when(isset($where['label_id']) && $where['label_id'] !== '', function (BaseQuery $query) use ($where) {
  98. return $query->whereRaw('CONCAT(\',\',User.label_id,\',\') LIKE \'%,' . $where['label_id'] . ',%\'');
  99. })->when(isset($where['sex']) && $where['sex'] !== '', function (BaseQuery $query) use ($where) {
  100. return $query->where('User.sex', intval($where['sex']));
  101. })->when(isset($where['is_promoter']) && $where['is_promoter'] !== '', function (BaseQuery $query) use ($where) {
  102. return $query->where('User.is_promoter', $where['is_promoter']);
  103. })->when(isset($where['nickname']) && $where['nickname'] !== '', function (BaseQuery $query) use ($where) {
  104. return $query->whereLike('User.nickname', "%{$where['nickname']}%");
  105. })->when(isset($where['account']) && $where['account'] !== '', function (BaseQuery $query) use ($where) {
  106. return $query->whereLike('User.account', "%{$where['account']}%");
  107. })->when(isset($where['spread_time']) && $where['spread_time'] !== '', function (BaseQuery $query) use ($where) {
  108. getModelTime($query, $where['spread_time'], 'User.spread_time');
  109. })->when(isset($where['date']) && $where['date'] !== '', function (BaseQuery $query) use ($where) {
  110. getModelTime($query, $where['date'], 'User.create_time');
  111. })->when(isset($where['spread_uid']) && $where['spread_uid'] !== '', function (BaseQuery $query) use ($where) {
  112. return $query->where('User.spread_uid', intval($where['spread_uid']));
  113. })->when(isset($where['spread_uids']), function (BaseQuery $query) use ($where) {
  114. return $query->whereIn('User.spread_uid', $where['spread_uids']);
  115. })->when(isset($where['is_show']) && $where['is_show'] !== '', function (BaseQuery $query) use ($where) {
  116. return $query->where('User.is_show', $where['is_show']);
  117. })->when(isset($where['pay_count']) && $where['pay_count'] !== '', function ($query) use ($where) {
  118. if($where['pay_count'] == -1 ){
  119. $query->where('User.pay_count', 0);
  120. }else{
  121. $query->where('User.pay_count', '>', $where['pay_count']);
  122. }
  123. })->when(isset($where['user_time_type']) && $where['user_time_type'] !== '' && $where['user_time'] != '', function ($query) use ($where) {
  124. list($startTime, $endTime) = explode('-', $where['user_time']);
  125. if ($where['user_time_type'] == 'visitno') {
  126. $query->whereTime("User.last_time", 'between', [$startTime, $endTime]);
  127. }
  128. if ($where['user_time_type'] == 'visit') {
  129. $query->whereTime('User.last_time', '>=', $startTime);
  130. $query->whereTime('User.last_time', '<', $endTime);
  131. }
  132. if ($where['user_time_type'] == 'add_time') {
  133. $query->whereTime('User.create_time', '>=', $startTime);
  134. $query->whereTime('User.create_time', '<', $endTime);
  135. }
  136. })->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) {
  137. $query->order('User.' . $where['sort']);
  138. })->when(isset($where['now_money']) , function (BaseQuery $query) use ($where) {
  139. if($where['now_money'] == 1 ){
  140. $query->order('User.now_money', "asc");
  141. }elseif($where['now_money'] == 2 ){
  142. $query->order('User.now_money', "desc");
  143. }
  144. }, function ($query) {
  145. $query->order('User.uid DESC');
  146. })->when((isset($where['user_rich_type']) && $where['user_rich_type']!=''), function ($query) use ($where) {
  147. if( $where['user_rich_type']=='0'){
  148. $query->where('userRich.id is null or userRich.type=0');
  149. }else {
  150. $query->where('userRich.type', $where['user_rich_type']);
  151. }
  152. })->when( (isset($where['stockholder']) && $where['stockholder']!='') , function ($query) use ($where) {
  153. $query->where('userRich.stockholder' , $where['stockholder']);
  154. })->when( (isset($where['subIdentify']) && $where['subIdentify']!='') , function ($query) use ($where) {
  155. $query->where('areaManage.type' , $where['subIdentify']);
  156. })->when(isset($where['uid']) , function ($query) use ($where) {
  157. $query->whereIn('User.uid' , $where['uid']);
  158. });
  159. $company = input('company','');
  160. $identify = input('identify','');
  161. $query->when( $company==1 , function ($query) {
  162. $query->join('enterprise_user eu','eu.uid=user.uid','left')->where('eu.mer_id',496);
  163. })->when( $company==1 && $identify , function ($query) use ($identify) {
  164. $query->where('eu.identity',$identify);
  165. })->when( $company==2 , function ($query) {
  166. //国盛小程序还未开发
  167. $query->where('user.uid',0);
  168. });
  169. return $query;
  170. }
  171. /**
  172. * @param $keyword
  173. * @return BaseQuery
  174. * @author xaboy
  175. * @day 2020/6/22
  176. */
  177. public function searchMerUser($keyword)
  178. {
  179. return User::getDB()->whereLike('nickname', "%$keyword%")->where('status', 1);
  180. }
  181. /**
  182. * @param array $ids
  183. * @param int $group_id
  184. * @return int
  185. * @throws DbException
  186. * @author xaboy
  187. * @day 2020-05-07
  188. */
  189. public function batchChangeGroupId(array $ids, int $group_id)
  190. {
  191. return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('group_id'));
  192. }
  193. /**
  194. * @param array $ids
  195. * @param array $label_id
  196. * @return int
  197. * @throws DbException
  198. * @author xaboy
  199. * @day 2020-05-07
  200. */
  201. public function batchChangeLabelId(array $ids, array $label_id)
  202. {
  203. $label_id = implode(',', $label_id);
  204. return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('label_id'));
  205. }
  206. /**
  207. * @param int $id
  208. * @return array|Model|null
  209. * @throws DataNotFoundException
  210. * @throws DbException
  211. * @throws ModelNotFoundException
  212. * @author xaboy
  213. * @day 2020-04-28
  214. */
  215. public function wechatUserIdBytUser(int $id)
  216. {
  217. return User::getDB()->where('wechat_user_id', $id)->find();
  218. }
  219. /**
  220. * @param $id
  221. * @return mixed
  222. * @author xaboy
  223. * @day 2020-04-28
  224. */
  225. public function wechatUserIdByUid($id)
  226. {
  227. return User::getDB()->where('wechat_user_id', $id)->value('uid');
  228. }
  229. /**
  230. * @param $id
  231. * @return mixed
  232. * @author xaboy
  233. * @day 2020/7/7
  234. */
  235. public function uidByWechatUserId($id)
  236. {
  237. return User::getDB()->where('uid', $id)->value('wechat_user_id');
  238. }
  239. /**
  240. * @param $account
  241. * @return array|Model|null
  242. * @throws DataNotFoundException
  243. * @throws DbException
  244. * @throws ModelNotFoundException
  245. * @author xaboy
  246. * @day 2020/6/22
  247. */
  248. public function accountByUser($account)
  249. {
  250. return User::getDB()->where('account', $account)->where('is_del', 0)->find();
  251. }
  252. /**
  253. * @param $uid
  254. * @return array
  255. * @author xaboy
  256. * @day 2020/6/22
  257. */
  258. public function getSubIds($uid)
  259. {
  260. return User::getDB()->where('spread_uid', $uid)->column('uid');
  261. }
  262. /**
  263. * @param $uid
  264. * @return int
  265. * @author xaboy
  266. * @day 2020/6/22
  267. */
  268. public function getOneLevelCount($uid)
  269. {
  270. return User::getDB()->where('spread_uid', $uid)->count();
  271. }
  272. /**
  273. * @param $uid
  274. * @return int
  275. * @author xaboy
  276. * @day 2020/6/22
  277. */
  278. public function getTwoLevelCount($uid)
  279. {
  280. $ids = $this->getSubIds($uid);
  281. return count($ids) ? User::getDB()->whereIn('spread_uid', $ids)->count() : 0;
  282. }
  283. /**
  284. * @param $ids
  285. * @return array
  286. * @author xaboy
  287. * @day 2020/6/22
  288. */
  289. public function getSubAllIds(array $ids)
  290. {
  291. return User::getDB()->whereIn('spread_uid', $ids)->column('uid');
  292. }
  293. /**
  294. * @param array $ids
  295. * @param string $field
  296. * @return \think\Collection
  297. * @throws DataNotFoundException
  298. * @throws DbException
  299. * @throws ModelNotFoundException
  300. * @author xaboy
  301. * @day 2020/6/22
  302. */
  303. public function users(array $ids, $field = '*')
  304. {
  305. return User::getDB()->whereIn('uid', $ids)->field($field)->select();
  306. }
  307. public function newUserNum($date, $where = [])
  308. {
  309. return User::getDB()->where($where)->when($date, function ($query, $date) {
  310. getModelTime($query, $date, 'create_time');
  311. })->count();
  312. }
  313. public function userOrderDetail($uid)
  314. {
  315. return User::getDB()->alias('A')
  316. ->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') . '\'')
  317. ->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')
  318. ->where('A.uid', $uid)
  319. ->find();
  320. }
  321. public function userNumGroup($date)
  322. {
  323. return User::getDB()->when($date, function ($query, $date) {
  324. getModelTime($query, $date, 'create_time');
  325. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(uid) as new'))
  326. ->group('time')->order('time ASC')->select();
  327. }
  328. public function userNumGroupHour($date)
  329. {
  330. return User::getDB()->when($date, function ($query, $date) {
  331. getModelTime($query, $date, 'create_time');
  332. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%H\') as time, count(uid) as new'))
  333. ->group('time')->order('time ASC')->select();
  334. }
  335. public function userNumGroupYear($date)
  336. {
  337. return User::getDB()->when($date, function ($query, $date) {
  338. getModelTime($query, $date, 'create_time');
  339. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m\') as time, count(uid) as new'))
  340. ->group('time')->order('time ASC')->select();
  341. }
  342. public function beforeUserNum($date)
  343. {
  344. return User::getDB()->where('create_time', '<', $date)->count();
  345. }
  346. public function selfUserList($phone)
  347. {
  348. return User::getDB()->where('phone', $phone)->field('uid,nickname,avatar,user_type')->select();
  349. }
  350. }