UserDao.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 '12345678';
  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. });
  157. $company = input('company','');
  158. $identify = input('identify','');
  159. $query->when( $company==1 , function ($query) {
  160. $query->join('enterprise_user eu','eu.uid=user.uid','left')->where('eu.mer_id',496);
  161. })->when( $company==1 && $identify , function ($query) use ($identify) {
  162. $query->where('eu.identity',$identify);
  163. })->when( $company==2 , function ($query) {
  164. //国盛小程序还未开发
  165. $query->where('user.uid',0);
  166. });
  167. return $query;
  168. }
  169. /**
  170. * @param $keyword
  171. * @return BaseQuery
  172. * @author xaboy
  173. * @day 2020/6/22
  174. */
  175. public function searchMerUser($keyword)
  176. {
  177. return User::getDB()->whereLike('nickname', "%$keyword%")->where('status', 1);
  178. }
  179. /**
  180. * @param array $ids
  181. * @param int $group_id
  182. * @return int
  183. * @throws DbException
  184. * @author xaboy
  185. * @day 2020-05-07
  186. */
  187. public function batchChangeGroupId(array $ids, int $group_id)
  188. {
  189. return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('group_id'));
  190. }
  191. /**
  192. * @param array $ids
  193. * @param array $label_id
  194. * @return int
  195. * @throws DbException
  196. * @author xaboy
  197. * @day 2020-05-07
  198. */
  199. public function batchChangeLabelId(array $ids, array $label_id)
  200. {
  201. $label_id = implode(',', $label_id);
  202. return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('label_id'));
  203. }
  204. /**
  205. * @param int $id
  206. * @return array|Model|null
  207. * @throws DataNotFoundException
  208. * @throws DbException
  209. * @throws ModelNotFoundException
  210. * @author xaboy
  211. * @day 2020-04-28
  212. */
  213. public function wechatUserIdBytUser(int $id)
  214. {
  215. return User::getDB()->where('wechat_user_id', $id)->find();
  216. }
  217. /**
  218. * @param $id
  219. * @return mixed
  220. * @author xaboy
  221. * @day 2020-04-28
  222. */
  223. public function wechatUserIdByUid($id)
  224. {
  225. return User::getDB()->where('wechat_user_id', $id)->value('uid');
  226. }
  227. /**
  228. * @param $id
  229. * @return mixed
  230. * @author xaboy
  231. * @day 2020/7/7
  232. */
  233. public function uidByWechatUserId($id)
  234. {
  235. return User::getDB()->where('uid', $id)->value('wechat_user_id');
  236. }
  237. /**
  238. * @param $account
  239. * @return array|Model|null
  240. * @throws DataNotFoundException
  241. * @throws DbException
  242. * @throws ModelNotFoundException
  243. * @author xaboy
  244. * @day 2020/6/22
  245. */
  246. public function accountByUser($account)
  247. {
  248. return User::getDB()->where('account', $account)->find();
  249. }
  250. /**
  251. * @param $uid
  252. * @return array
  253. * @author xaboy
  254. * @day 2020/6/22
  255. */
  256. public function getSubIds($uid)
  257. {
  258. return User::getDB()->where('spread_uid', $uid)->column('uid');
  259. }
  260. /**
  261. * @param $uid
  262. * @return int
  263. * @author xaboy
  264. * @day 2020/6/22
  265. */
  266. public function getOneLevelCount($uid)
  267. {
  268. return User::getDB()->where('spread_uid', $uid)->count();
  269. }
  270. /**
  271. * @param $uid
  272. * @return int
  273. * @author xaboy
  274. * @day 2020/6/22
  275. */
  276. public function getTwoLevelCount($uid)
  277. {
  278. $ids = $this->getSubIds($uid);
  279. return count($ids) ? User::getDB()->whereIn('spread_uid', $ids)->count() : 0;
  280. }
  281. /**
  282. * @param $ids
  283. * @return array
  284. * @author xaboy
  285. * @day 2020/6/22
  286. */
  287. public function getSubAllIds(array $ids)
  288. {
  289. return User::getDB()->whereIn('spread_uid', $ids)->column('uid');
  290. }
  291. /**
  292. * @param array $ids
  293. * @param string $field
  294. * @return \think\Collection
  295. * @throws DataNotFoundException
  296. * @throws DbException
  297. * @throws ModelNotFoundException
  298. * @author xaboy
  299. * @day 2020/6/22
  300. */
  301. public function users(array $ids, $field = '*')
  302. {
  303. return User::getDB()->whereIn('uid', $ids)->field($field)->select();
  304. }
  305. public function newUserNum($date, $where = [])
  306. {
  307. return User::getDB()->where($where)->when($date, function ($query, $date) {
  308. getModelTime($query, $date, 'create_time');
  309. })->count();
  310. }
  311. public function userOrderDetail($uid)
  312. {
  313. return User::getDB()->alias('A')
  314. ->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') . '\'')
  315. ->field('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')
  316. ->where('A.uid', $uid)
  317. ->find();
  318. }
  319. public function userNumGroup($date)
  320. {
  321. return User::getDB()->when($date, function ($query, $date) {
  322. getModelTime($query, $date, 'create_time');
  323. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(uid) as new'))
  324. ->group('time')->order('time ASC')->select();
  325. }
  326. public function userNumGroupHour($date)
  327. {
  328. return User::getDB()->when($date, function ($query, $date) {
  329. getModelTime($query, $date, 'create_time');
  330. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%H\') as time, count(uid) as new'))
  331. ->group('time')->order('time ASC')->select();
  332. }
  333. public function userNumGroupYear($date)
  334. {
  335. return User::getDB()->when($date, function ($query, $date) {
  336. getModelTime($query, $date, 'create_time');
  337. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m\') as time, count(uid) as new'))
  338. ->group('time')->order('time ASC')->select();
  339. }
  340. public function beforeUserNum($date)
  341. {
  342. return User::getDB()->where('create_time', '<', $date)->count();
  343. }
  344. public function selfUserList($phone)
  345. {
  346. return User::getDB()->where('phone', $phone)->field('uid,nickname,avatar,user_type')->select();
  347. }
  348. }