UserDao.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * 获取用户列表
  28. * @param array $where
  29. * @param string $field
  30. * @param int $page
  31. * @param int $limit
  32. * @return array
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function getList(array $where, string $field = '*', int $page, int $limit): array
  38. {
  39. return $this->search($where)->field($field)->with(['label'])->page($page, $limit)->select()->toArray();
  40. }
  41. /**
  42. * 获取特定条件的总数
  43. * @param array $where
  44. * @param bool $is_list
  45. * @return array|int
  46. */
  47. public function getCount(array $where, bool $is_list = false)
  48. {
  49. if ($is_list)
  50. return $this->getModel()->where($where)->group('uid')->fetchSql(true)->column('count(*) as user_count', 'uid');
  51. else
  52. return $this->getModel()->where($where)->count();
  53. }
  54. /**
  55. * 用户支付成功个数增加
  56. * @param int $uid
  57. * @return mixed
  58. */
  59. public function incPayCount(int $uid)
  60. {
  61. return $this->getModel()->where('uid', $uid)->inc('pay_count', 1)->update();
  62. }
  63. /**
  64. * 某个字段累加某个数值
  65. * @param string $field
  66. * @param int $num
  67. */
  68. public function incField(int $uid, string $field, int $num = 1)
  69. {
  70. return $this->getModel()->where('uid', $uid)->inc($field, $num)->update();
  71. }
  72. /**
  73. * @param $uid
  74. * @return \think\Collection
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function getUserLabel($uid, $field = '*')
  80. {
  81. return $this->search(['uid' => $uid])->field($field)->with(['label'])->select()->toArray();
  82. }
  83. /**
  84. * 获取分销用户
  85. * @param array $where
  86. * @param string $field
  87. * @param int $page
  88. * @param int $limit
  89. * @return array
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. public function getAgentUserList(array $where, string $field = '*', int $page, int $limit)
  95. {
  96. return $this->search($where)->field($field)->with([
  97. 'extract' => function ($query) {
  98. $query->field('sum(extract_price) as extract_count_price,count(id) as extract_count_num,uid')->where('status', '1')->group('uid');
  99. }, 'order' => function ($query) {
  100. $query->field('sum(pay_price) as order_price,count(id) as order_count,uid')->where('paid', 1)->where('refund_status', 0)->group('uid');
  101. }, 'bill' => function ($query) {
  102. $query->field('sum(number) as brokerage_money,uid')->where('category', 'now_money')->where('type', 'brokerage')->where('status', 1)->where('pm', 1)->group('uid');
  103. }, 'spreadCount' => function ($query) {
  104. $query->field('count(*) as spread_count,spread_uid')->group('spread_uid');
  105. }, 'spreadUser' => function ($query) {
  106. $query->field('uid,phone,nickname');
  107. }
  108. ])->when($page && $limit, function ($query) use ($page, $limit) {
  109. $query->page($page, $limit);
  110. })->order('uid desc')->select()->toArray();
  111. }
  112. /**
  113. * 获取推广人列表
  114. * @param array $where
  115. * @param string $field
  116. * @param int $page
  117. * @param int $limit
  118. * @return array
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. */
  123. public function getSairList(array $where, string $field = '*', int $page, int $limit)
  124. {
  125. return $this->search($where)->field($field)->with([
  126. 'order' => function ($query) {
  127. $query->field('sum(pay_price) as order_price,count(id) as order_count,uid')->where('paid', 1)->where('refund_status', 0)->group('uid');
  128. }, 'spreadCount' => function ($query) {
  129. $query->field('count(*) as spread_count,spread_uid')->group('spread_uid');
  130. }, 'spreadUser' => function ($query) {
  131. $query->field('uid,phone,nickname');
  132. }
  133. ])->page($page, $limit)->order('uid desc')->select()->toArray();
  134. }
  135. /**
  136. * 获取推广人排行
  137. * @param array $time
  138. * @param string $field
  139. * @param int $page
  140. * @param int $limit
  141. */
  142. public function getAgentRankList(array $time, string $field = '*', int $page, int $limit)
  143. {
  144. return $this->getModel()->alias('t0')
  145. ->field($field)
  146. ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
  147. ->where('t1.spread_uid', '<>', 0)
  148. ->order('count desc')
  149. ->order('t0.uid desc')
  150. ->where('t1.spread_time', 'BETWEEN', $time)
  151. ->page($page, $limit)
  152. ->group('t0.uid')
  153. ->select()->toArray();
  154. }
  155. /**
  156. * 获取推广员ids
  157. * @param array $where
  158. * @return array
  159. */
  160. public function getAgentUserIds(array $where)
  161. {
  162. return $this->search($where)->column('uid');
  163. }
  164. /**
  165. * 某个条件 用户某个字段总和
  166. * @param array $where
  167. * @param string $filed
  168. * @return float
  169. */
  170. public function getWhereSumField(array $where, string $filed)
  171. {
  172. return $this->search($where)->sum($filed);
  173. }
  174. /**
  175. * 根据条件查询对应的用户信息以数组形式返回
  176. * @param array $where
  177. * @param string $field
  178. * @param string $key
  179. * @return array
  180. */
  181. public function getUserInfoArray(array $where, string $field, string $key)
  182. {
  183. return $this->search($where)->column($field, $key);
  184. }
  185. /**
  186. * 获取特定时间用户访问量
  187. * @param $time
  188. * @param $week
  189. * @return int
  190. */
  191. public function todayLastVisit($time, $week)
  192. {
  193. switch ($week) {
  194. case 1:
  195. return $this->search(['time' => $time ?: 'today', 'timeKey' => 'last_time'])->count();
  196. case 2:
  197. return $this->search(['time' => $time ?: 'week', 'timeKey' => 'last_time'])->count();
  198. }
  199. }
  200. /**
  201. * 获取特定时间用户访问量
  202. * @param $time
  203. * @param $week
  204. * @return int
  205. */
  206. public function todayAddVisit($time, $week)
  207. {
  208. switch ($week) {
  209. case 1:
  210. return $this->search(['time' => $time ?: 'today', 'timeKey' => 'add_time'])->count();
  211. case 2:
  212. return $this->search(['time' => $time ?: 'week', 'timeKey' => 'add_time'])->count();
  213. }
  214. }
  215. /**
  216. * 获取特定时间内用户列表
  217. * @param $starday
  218. * @param $yesterday
  219. * @return mixed
  220. */
  221. public function userList($starday, $yesterday)
  222. {
  223. return $this->getModel()->where('add_time', 'between time', [$starday, $yesterday])
  224. ->field("FROM_UNIXTIME(add_time,'%m-%e') as day,count(*) as count")
  225. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  226. ->order('add_time asc')
  227. ->select()->toArray();
  228. }
  229. /**
  230. * 购买量范围的用户数量
  231. * @param $status
  232. * @return int
  233. */
  234. public function userCount($status)
  235. {
  236. switch ($status) {
  237. case 1:
  238. return $this->getModel()->where('pay_count', '>', 0)->where('pay_count', '<', 4)->count();
  239. case 2:
  240. return $this->getModel()->where('pay_count', '>', 4)->count();
  241. }
  242. }
  243. /**
  244. * 获取用户统计数据
  245. * @param $time
  246. * @param $type
  247. * @param $timeType
  248. * @return mixed
  249. */
  250. public function getTrendData($time, $type, $timeType)
  251. {
  252. return $this->getModel()->when($type != '', function ($query) use ($type) {
  253. $query->where('user_type', $type);
  254. })->where(function ($query) use ($time) {
  255. if ($time[0] == $time[1]) {
  256. $query->whereDay('add_time', $time[0]);
  257. } else {
  258. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  259. $query->whereTime('add_time', 'between', $time);
  260. }
  261. })->field("FROM_UNIXTIME(add_time,'$timeType') as days,count(uid) as num")->group('days')->select()->toArray();
  262. }
  263. /**
  264. * @param array $where
  265. * @return array
  266. * @throws \think\db\exception\DataNotFoundException
  267. * @throws \think\db\exception\DbException
  268. * @throws \think\db\exception\ModelNotFoundException
  269. */
  270. public function getUserInfoList(array $where, $field = "*"): array
  271. {
  272. return $this->search($where)->field($field)->select()->toArray();
  273. }
  274. /**
  275. * 获取用户会员数量
  276. * @param $where (time type)
  277. * @return int
  278. */
  279. public function getMemberCount($where, int $overdue_time = 0)
  280. {
  281. if (!$overdue_time) $overdue_time = time();
  282. return $this->search($where)->where('is_ever_level', 1)->whereOr(function ($qeury) use ($overdue_time) {
  283. $qeury->where('is_money_level', '>', 0)->where('overdue_time', '>', $overdue_time);
  284. })->count();
  285. }
  286. }