UserDao.php 10 KB

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