UserDao.php 10 KB

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