UserBillDao.php 8.8 KB

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