UserDao.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. ->where('t0.is_del', 0)
  156. ->page($page, $limit)
  157. ->group('t0.uid')
  158. ->select()->toArray();
  159. }
  160. /**
  161. * 获取推广员ids
  162. * @param array $where
  163. * @return array
  164. */
  165. public function getAgentUserIds(array $where)
  166. {
  167. return $this->search($where)->column('uid');
  168. }
  169. /**
  170. * 某个条件 用户某个字段总和
  171. * @param array $where
  172. * @param string $filed
  173. * @return float
  174. */
  175. public function getWhereSumField(array $where, string $filed)
  176. {
  177. return $this->search($where)->sum($filed);
  178. }
  179. /**
  180. * 根据条件查询对应的用户信息以数组形式返回
  181. * @param array $where
  182. * @param string $field
  183. * @param string $key
  184. * @return array
  185. */
  186. public function getUserInfoArray(array $where, string $field, string $key)
  187. {
  188. return $this->search($where)->column($field, $key);
  189. }
  190. /**
  191. * 获取特定时间用户访问量
  192. * @param $time
  193. * @param $week
  194. * @return int
  195. */
  196. public function todayLastVisit($time, $week)
  197. {
  198. switch ($week) {
  199. case 1:
  200. return $this->search(['time' => $time ?: 'today', 'timeKey' => 'last_time'])->count();
  201. case 2:
  202. return $this->search(['time' => $time ?: 'week', 'timeKey' => 'last_time'])->count();
  203. }
  204. }
  205. /**
  206. * 获取特定时间用户访问量
  207. * @param $time
  208. * @param $week
  209. * @return int
  210. */
  211. public function todayAddVisit($time, $week)
  212. {
  213. switch ($week) {
  214. case 1:
  215. return $this->search(['time' => $time ?: 'today', 'timeKey' => 'add_time'])->count();
  216. case 2:
  217. return $this->search(['time' => $time ?: 'week', 'timeKey' => 'add_time'])->count();
  218. }
  219. }
  220. /**
  221. * 获取特定时间内用户列表
  222. * @param $starday
  223. * @param $yesterday
  224. * @return mixed
  225. */
  226. public function userList($starday, $yesterday)
  227. {
  228. return $this->getModel()
  229. ->whereBetweenTime('add_time', $starday, $yesterday)
  230. ->field("FROM_UNIXTIME(add_time,'%m-%e') as day,count(*) as count")
  231. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  232. ->order('add_time asc')->select()->toArray();
  233. }
  234. /**
  235. * 购买量范围的用户数量
  236. * @param $status
  237. * @return int
  238. */
  239. public function userCount($status)
  240. {
  241. switch ($status) {
  242. case 1:
  243. return $this->getModel()->where('pay_count', '>', 1)->where('pay_count', '<=', 4)->count();
  244. case 2:
  245. return $this->getModel()->where('pay_count', '>', 4)->count();
  246. }
  247. }
  248. /**
  249. * 获取用户统计数据
  250. * @param $time
  251. * @param $type
  252. * @param $timeType
  253. * @return mixed
  254. */
  255. public function getTrendData($time, $type, $timeType)
  256. {
  257. return $this->getModel()->when($type != '', function ($query) use ($type) {
  258. $query->where('user_type', $type);
  259. })->where(function ($query) use ($time) {
  260. if ($time[0] == $time[1]) {
  261. $query->whereDay('add_time', $time[0]);
  262. } else {
  263. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  264. $query->whereTime('add_time', 'between', $time);
  265. }
  266. })->field("FROM_UNIXTIME(add_time,'$timeType') as days,count(uid) as num")->group('days')->select()->toArray();
  267. }
  268. /**
  269. * @param array $where
  270. * @return array
  271. * @throws \think\db\exception\DataNotFoundException
  272. * @throws \think\db\exception\DbException
  273. * @throws \think\db\exception\ModelNotFoundException
  274. */
  275. public function getUserInfoList(array $where, $field = "*"): array
  276. {
  277. return $this->search($where)->field($field)->select()->toArray();
  278. }
  279. /**
  280. * 获取用户会员数量
  281. * @param $where (time type)
  282. * @return int
  283. */
  284. public function getMemberCount($where, int $overdue_time = 0)
  285. {
  286. if (!$overdue_time) $overdue_time = time();
  287. return $this->search($where)->where('is_ever_level', 1)->whereOr(function ($qeury) use ($overdue_time) {
  288. $qeury->where('is_money_level', '>', 0)->where('overdue_time', '>', $overdue_time);
  289. })->count();
  290. }
  291. }