UserDao.php 10 KB

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