UserBillDao.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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\UserBill;
  15. /**
  16. * 积分&经验
  17. * Class UserBilldao
  18. * @package app\dao\user
  19. */
  20. class UserBilldao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return UserBill::class;
  29. }
  30. /**
  31. * 获取某些条件总数
  32. * @param array $where
  33. * @return int
  34. */
  35. public function getCount(array $where, $search = false)
  36. {
  37. if ($search) {
  38. return $this->search($where)->count();
  39. } else
  40. return $this->getModel()->where($where)->count();
  41. }
  42. /**
  43. * 获取列表
  44. * @param array $where
  45. * @param string $field
  46. * @param int $page
  47. * @param int $limit
  48. * @param array $typeWhere
  49. * @return array
  50. */
  51. public function getList(array $where, string $field = '*', int $page = 0, int $limit = 0, array $typeWhere = [])
  52. {
  53. return $this->search($where)->when(count($typeWhere) > 0, function ($query) use ($typeWhere) {
  54. $query->where($typeWhere);
  55. })->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
  56. $query->page($page, $limit);
  57. })->order('id desc')->select()->toArray();
  58. }
  59. /**
  60. * 获取列表
  61. * @param array $where
  62. * @param string $field
  63. * @param int $page
  64. * @param int $limit
  65. * @return array
  66. */
  67. public function getBillList(array $where, string $field = '*', int $page = 0, int $limit = 0)
  68. {
  69. return $this->search($where)->field($field)->with([
  70. 'user' => function ($query) {
  71. $query->field('uid,nickname');
  72. }])->when($page && $limit, function ($query) use ($page, $limit) {
  73. $query->page($page, $limit);
  74. })->order('id desc')->select()->toArray();
  75. }
  76. /**
  77. * 获取某个条件总数
  78. * @param array $where
  79. */
  80. public function getBillSum(array $where)
  81. {
  82. return $this->search($where)->sum('number');
  83. }
  84. /**
  85. * 获取某个条件总条数
  86. * @param array $where
  87. */
  88. public function getBillCount(array $where)
  89. {
  90. return $this->getModel()->where($where)->count();
  91. }
  92. /**
  93. * 获取某些条件的bill总数
  94. * @param array $where
  95. * @return mixed
  96. */
  97. public function getBillSumColumn(array $where)
  98. {
  99. if (isset($where['uid']) && is_array($where['uid'])) {
  100. return $this->search($where)->group('uid')->column('sum(number) as num', 'uid');
  101. } else
  102. return $this->search($where)->sum('number');
  103. }
  104. /**
  105. * 获取类型
  106. * @param array $where
  107. * @param string $filed
  108. * @return mixed
  109. */
  110. public function getType(array $where, string $filed = 'title,type')
  111. {
  112. return $this->search($where)->distinct(true)->field($filed)->group('type')->select();
  113. }
  114. /**
  115. * 获取签到用户数量
  116. * @param array $where
  117. * @return mixed
  118. */
  119. public function getUserSignPoint(array $where)
  120. {
  121. return $this->search($where)->count();
  122. }
  123. /**
  124. * @param array $where
  125. * @return array
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\DbException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. */
  130. public function getUserBillList(array $where)
  131. {
  132. return $this->search($where)->select()->toArray();
  133. }
  134. /**
  135. * 获取佣金排行
  136. * @param array $where
  137. * @param int $page
  138. * @param int $limit
  139. * @return array
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\DbException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. */
  144. public function brokerageRankList(array $where, int $page = 0, int $limit = 0)
  145. {
  146. return $this->search($where)->field('uid,SUM(IF(pm=1,`number`,-`number`)) as brokerage_price')->with(['user' => function ($query) {
  147. $query->field('uid,avatar,nickname');
  148. }])->order('brokerage_price desc')->group('uid')->when($page && $limit, function ($query) use ($page, $limit) {
  149. $query->page($page, $limit);
  150. })->select()->toArray();
  151. }
  152. /**
  153. * 时间分组
  154. * @param array $where
  155. * @param string $filed
  156. * @param string $group
  157. * @param int $page
  158. * @param int $limit
  159. * @return mixed
  160. */
  161. public function getUserBillListByGroup(array $where, string $filed, string $group, int $page, int $limit)
  162. {
  163. return $this->search($where)->field($filed)->where('number', '>', 0)->order('add_time desc')->group($group)->page($page, $limit)->select()->toArray();
  164. }
  165. /**
  166. * @param array $where
  167. * @param int $page
  168. * @param int $limit
  169. * @return array
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\DbException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. */
  174. public function getBalanceRecord(array $where, int $page, int $limit)
  175. {
  176. return $this->search($where)->order('add_time desc')->page($page, $limit)->select()->toArray();
  177. }
  178. /**
  179. * 计算某个条件下订单内商品总数
  180. * @param $where
  181. * @return float|int
  182. * @throws \think\db\exception\DataNotFoundException
  183. * @throws \think\db\exception\DbException
  184. * @throws \think\db\exception\ModelNotFoundException
  185. */
  186. public function getTotalSum(array $where)
  187. {
  188. $list = $this->search($where)->with('order')->select()->toArray();
  189. if (count($list)) {
  190. $sum = 0;
  191. foreach ($list as $item) {
  192. $sum += $item['total_num'];
  193. }
  194. return $sum;
  195. } else {
  196. return 0;
  197. }
  198. }
  199. /**
  200. * 获取某个字段总和
  201. * @param array $where
  202. * @param string $field
  203. * @return float
  204. */
  205. public function getWhereSumField(array $where, string $field)
  206. {
  207. return $this->search($where)
  208. ->when(isset($where['timeKey']), function ($query) use ($where) {
  209. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  210. })
  211. ->sum($field);
  212. }
  213. /**
  214. * 根据某字段分组查询
  215. * @param array $where
  216. * @param string $field
  217. * @param string $group
  218. * @return array
  219. * @throws \think\db\exception\DataNotFoundException
  220. * @throws \think\db\exception\DbException
  221. * @throws \think\db\exception\ModelNotFoundException
  222. */
  223. public function getGroupField(array $where, string $field, string $group)
  224. {
  225. return $this->search($where)
  226. ->when(isset($where['timeKey']), function ($query) use ($where, $field, $group) {
  227. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  228. if ($where['timeKey']['days'] == 1) {
  229. $timeUinx = "%H";
  230. } elseif ($where['timeKey']['days'] == 30) {
  231. $timeUinx = "%Y-%m-%d";
  232. } elseif ($where['timeKey']['days'] == 365) {
  233. $timeUinx = "%Y-%m";
  234. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  235. $timeUinx = "%Y-%m-%d";
  236. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  237. $timeUinx = "%Y-%m";
  238. } else {
  239. $timeUinx = "%Y-%m";
  240. }
  241. $query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  242. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  243. })
  244. ->order('add_time ASC')->select()->toArray();
  245. }
  246. /**
  247. * 积分趋势
  248. * @param $time
  249. * @param $timeType
  250. * @param $field
  251. * @param $str
  252. * @return mixed
  253. */
  254. public function getPointTrend($time, $timeType, $field, $str, $orderStatus = '')
  255. {
  256. return $this->getModel()->where(function ($query) use ($field, $orderStatus) {
  257. $query->where('category', 'integral');
  258. if ($orderStatus == 'add') {
  259. $query->where('pm', 1);
  260. } elseif ($orderStatus == 'sub') {
  261. $query->where('pm', 0);
  262. }
  263. })->where(function ($query) use ($time, $field) {
  264. if ($time[0] == $time[1]) {
  265. $query->whereDay($field, $time[0]);
  266. } else {
  267. $query->whereTime($field, 'between', $time);
  268. }
  269. })->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  270. }
  271. }