UserDao.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. declare (strict_types=1);
  12. namespace app\dao\user;
  13. use app\dao\BaseDao;
  14. use app\model\user\User;
  15. /**
  16. * 用户
  17. * Class UserDao
  18. * @package app\dao\user
  19. */
  20. class UserDao extends BaseDao
  21. {
  22. protected function setModel(): string
  23. {
  24. return User::class;
  25. }
  26. /**
  27. * @param array $where
  28. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  29. */
  30. public function search(array $where = [])
  31. {
  32. return parent::search($where)->when(isset($where['label_id']) && $where['label_id'], function($query) use ($where) {
  33. $query->whereIn('uid', function ($q) use ($where) {
  34. if (is_array($where['label_id'])) {
  35. $q->name('user_label_relation')->whereIn('label_id', $where['label_id'])->field('uid')->select();
  36. } else {
  37. if (strpos($where['label_id'], ',') !== false) {
  38. $q->name('user_label_relation')->whereIn('label_id', explode(',', $where['label_id']))->field('uid')->select();
  39. } else {
  40. $q->name('user_label_relation')->where('label_id', $where['label_id'])->field('uid')->select();
  41. }
  42. }
  43. });
  44. });
  45. }
  46. /**
  47. * 是否存在
  48. * @param int $uid
  49. * @return bool
  50. */
  51. public function exist(int $uid)
  52. {
  53. return !!$this->getModel()->where('uid', $uid)->count();
  54. }
  55. /**
  56. * @param array $where
  57. * @return mixed
  58. */
  59. public function getWithTrashedCount(array $where = [], array $time = [])
  60. {
  61. return $this->getModel()->withTrashed()->where($where)->whereTime('add_time', 'between', $time)->count();
  62. }
  63. /**
  64. * 获取删除和没有删除的用户信息
  65. * @param int $uid
  66. * @param $field
  67. * @return mixed
  68. */
  69. public function getUserWithTrashedInfo(int $uid, $field = '*')
  70. {
  71. return $this->getModel()->withTrashed()->field($field)->find($uid);
  72. }
  73. /**
  74. * 获取用户列表
  75. * @param array $where
  76. * @param string $field
  77. * @param int $page
  78. * @param int $limit
  79. * @return array
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function getList(array $where, string $field = '*', int $page = 0, int $limit = 0): array
  85. {
  86. return $this->search($where)->field($field)->with(['label'])->when($page && $limit, function ($query) use ($page, $limit) {
  87. $query->page($page, $limit);
  88. })->select()->toArray();
  89. }
  90. /**
  91. * 获取特定条件的总数
  92. * @param array $where
  93. * @param bool $is_list
  94. * @return array|int
  95. */
  96. public function getCount(array $where, bool $is_list = false)
  97. {
  98. if ($is_list)
  99. return $this->getModel()->where($where)->group('uid')->fetchSql(true)->column('count(*) as user_count', 'uid');
  100. else
  101. return $this->getModel()->where($where)->count();
  102. }
  103. /**
  104. * 用户支付成功个数增加
  105. * @param int $uid
  106. * @return mixed
  107. */
  108. public function incPayCount(int $uid)
  109. {
  110. event('user.update', [$uid]);
  111. return $this->getModel()->where('uid', $uid)->inc('pay_count', 1)->update();
  112. }
  113. /**
  114. * 某个字段累加某个数值
  115. * @param string $field
  116. * @param int $num
  117. */
  118. public function incField(int $uid, string $field, int $num = 1)
  119. {
  120. event('user.update', [$uid]);
  121. return $this->getModel()->where('uid', $uid)->inc($field, $num)->update();
  122. }
  123. /**
  124. * @param $uid
  125. * @return \think\Collection
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\DbException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. */
  130. public function getUserLabel($uid, $field = '*')
  131. {
  132. return $this->search(['uid' => $uid])->field($field)->with(['label'])->select()->toArray();
  133. }
  134. /**
  135. * 获取分销用户
  136. * @param array $where
  137. * @param string $field
  138. * @param int $page
  139. * @param int $limit
  140. * @return array
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\DbException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. */
  145. public function getAgentUserList(array $where, string $field = '*', int $page = 0, int $limit = 0)
  146. {
  147. return $this->search($where)->field($field)->with([
  148. 'extract' => function ($query) {
  149. $query->field('sum(extract_price + extract_fee) as extract_count_price,count(id) as extract_count_num,uid')->where('status', '1')->group('uid');
  150. }, 'order' => function ($query) {
  151. $query->field('sum(pay_price) as order_price,count(id) as order_count,uid')->where('pid', '>=', 0)->where('paid', 1)->where('is_del', 0)->where('is_system_del', 0)->where('refund_status', 'IN', [0, 3])->group('uid');
  152. }, 'brokerage' => function ($query) {
  153. $query->field('sum(number) as brokerage_money,uid')->where('status', 1)->where('pm', 1)->group('uid');
  154. }, 'spreadCount' => function ($query) {
  155. $query->field('count(`uid`) as spread_count,spread_uid')->group('spread_uid');
  156. }, 'spreadUser' => function ($query) {
  157. $query->field('uid,phone,nickname');
  158. }, 'agentLevel' => function ($query) {
  159. $query->field('id,name');
  160. }
  161. ])->when($page && $limit, function ($query) use ($page, $limit) {
  162. $query->page($page, $limit);
  163. })->order('uid desc')->select()->toArray();
  164. }
  165. /**
  166. * 获取推广人列表
  167. * @param array $where
  168. * @param string $field
  169. * @param int $page
  170. * @param int $limit
  171. * @return array
  172. * @throws \think\db\exception\DataNotFoundException
  173. * @throws \think\db\exception\DbException
  174. * @throws \think\db\exception\ModelNotFoundException
  175. */
  176. public function getSairList(array $where, string $field = '*', int $page = 0, int $limit = 0)
  177. {
  178. return $this->search($where)->field($field)->with([
  179. 'order' => function ($query) {
  180. $query->field('sum(pay_price) as order_price,count(id) as order_count,uid')->where('paid', 1)->where('refund_status', 0)->group('uid');
  181. }, 'spreadCount' => function ($query) {
  182. $query->field('count(`uid`) as spread_count,spread_uid')->group('spread_uid');
  183. }, 'spreadUser' => function ($query) {
  184. $query->field('uid,phone,nickname');
  185. }
  186. ])->page($page, $limit)->order('uid desc')->select()->toArray();
  187. }
  188. /**
  189. * 获取推广人排行
  190. * @param array $time
  191. * @param string $field
  192. * @param int $page
  193. * @param int $limit
  194. */
  195. public function getAgentRankList(array $time, string $field = '*', int $page = 0, int $limit = 0)
  196. {
  197. return $this->getModel()->alias('t0')
  198. ->field($field)
  199. ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
  200. ->where('t1.spread_uid', '<>', 0)
  201. ->order('count desc')
  202. ->order('t0.uid desc')
  203. ->where('t1.spread_time', 'BETWEEN', $time)
  204. ->page($page, $limit)
  205. ->group('t0.uid')
  206. ->select()->toArray();
  207. }
  208. /**
  209. * 获取推广员ids
  210. * @param array $where
  211. * @return array
  212. */
  213. public function getAgentUserIds(array $where)
  214. {
  215. return $this->search($where)->column('uid');
  216. }
  217. /**
  218. * 某个条件 用户某个字段总和
  219. * @param array $where
  220. * @param string $filed
  221. * @return float
  222. */
  223. public function getWhereSumField(array $where, string $filed)
  224. {
  225. return $this->search($where)->sum($filed);
  226. }
  227. /**
  228. * 根据条件查询对应的用户信息以数组形式返回
  229. * @param array $where
  230. * @param string $field
  231. * @param string $key
  232. * @return array
  233. */
  234. public function getUserInfoArray(array $where, string $field, string $key)
  235. {
  236. return $this->search($where)->column($field, $key);
  237. }
  238. /**
  239. * 获取特定时间用户访问量
  240. * @param $time
  241. * @param $week
  242. * @return int
  243. */
  244. public function todayLastVisit($time, $week)
  245. {
  246. switch ($week) {
  247. case 1:
  248. return $this->search(['time' => $time ?: 'today', 'timeKey' => 'last_time'])->count();
  249. case 2:
  250. return $this->search(['time' => $time ?: 'week', 'timeKey' => 'last_time'])->count();
  251. }
  252. }
  253. /**
  254. * 获取特定时间用户访问量
  255. * @param $time
  256. * @return int
  257. */
  258. public function totalUserCount($time)
  259. {
  260. return $this->search(['time' => $time ?: 'today', 'timeKey' => 'add_time'])->count();
  261. }
  262. /**
  263. * 获取特定时间内用户列表
  264. * @param $starday
  265. * @param $yesterday
  266. * @return mixed
  267. */
  268. public function userList($starday, $yesterday)
  269. {
  270. return $this->getModel()->where('add_time', 'between time', [$starday, $yesterday])
  271. ->field("FROM_UNIXTIME(add_time,'%Y-%m-%d') as day,count(*) as count")
  272. ->group("FROM_UNIXTIME(add_time, '%Y%m%d')")
  273. ->order('add_time asc')
  274. ->select()->toArray();
  275. }
  276. /**
  277. * 购买量范围的用户数量
  278. * @param $status
  279. * @return int
  280. */
  281. public function userCount($status)
  282. {
  283. switch ($status) {
  284. case 1:
  285. return $this->getModel()->where('pay_count', '>', 1)->where('pay_count', '<=', 4)->count();
  286. case 2:
  287. return $this->getModel()->where('pay_count', '>', 4)->count();
  288. }
  289. }
  290. /**
  291. * 获取用户统计数据
  292. * @param $time
  293. * @param $type
  294. * @param $timeType
  295. * @return mixed
  296. */
  297. public function getTrendData($time, $type, $timeType)
  298. {
  299. return $this->getModel()->when($type != '', function ($query) use ($type) {
  300. $query->where('user_type', $type);
  301. })->where(function ($query) use ($time) {
  302. if ($time[0] == $time[1]) {
  303. $query->whereDay('add_time', $time[0]);
  304. } else {
  305. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  306. $query->whereTime('add_time', 'between', $time);
  307. }
  308. })->field("FROM_UNIXTIME(add_time,'$timeType') as days,count(uid) as num")->group('days')->select()->toArray();
  309. }
  310. /**
  311. * @param array $where
  312. * @return array
  313. * @throws \think\db\exception\DataNotFoundException
  314. * @throws \think\db\exception\DbException
  315. * @throws \think\db\exception\ModelNotFoundException
  316. */
  317. public function getUserInfoList(array $where, $field = "*"): array
  318. {
  319. return $this->search($where)->field($field)->select()->toArray();
  320. }
  321. /**
  322. * 获取用户会员数量
  323. * @param $where (time type)
  324. * @return int
  325. */
  326. public function getMemberCount($where, int $overdue_time = 0)
  327. {
  328. if (!$overdue_time) $overdue_time = time();
  329. return $this->search($where)->where('is_ever_level', 1)->whereOr(function ($qeury) use ($overdue_time) {
  330. $qeury->where('is_money_level', '>', 0)->where('overdue_time', '>', $overdue_time);
  331. })->count();
  332. }
  333. public function getOutOne(int $uid, $field = "*")
  334. {
  335. return $this->getModel()->where('uid|phone', $uid)->field($field)->find();
  336. }
  337. }