UserBillDao.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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, int $limit)
  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 $time
  75. * @param string $timeType
  76. * @param string $field
  77. * @param string $str
  78. * @return mixed
  79. */
  80. public function getUserRefundPriceList(array $time, string $timeType, string $str, string $field = 'add_time')
  81. {
  82. return $this->getModel()->where('type', 'pay_product_refund')->where(function ($query) use ($time, $field) {
  83. if ($time[0] == $time[1]) {
  84. $query->whereDay($field, $time[0]);
  85. } else {
  86. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  87. $query->whereTime($field, 'between', $time);
  88. }
  89. })->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num,GROUP_CONCAT(link_id) as link_ids")->group('days')->select()->toArray();
  90. }
  91. /**
  92. * 获取某个条件总条数
  93. * @param array $where
  94. */
  95. public function getBillCount(array $where)
  96. {
  97. return $this->getModel()->where($where)->count();
  98. }
  99. /**
  100. * 获取某些条件的bill总数
  101. * @param array $where
  102. * @return mixed
  103. */
  104. public function getBillSumColumn(array $where)
  105. {
  106. if (isset($where['uid']) && is_array($where['uid'])) {
  107. return $this->search($where)->group('uid')->column('sum(number) as num', 'uid');
  108. } else
  109. return $this->search($where)->sum('number');
  110. }
  111. /**
  112. *
  113. * @param array $where
  114. * @param string $filed
  115. * @return mixed
  116. */
  117. public function getType(array $where, string $filed = 'title,type')
  118. {
  119. return $this->search($where)->distinct(true)->field($filed)->group('type')->select();
  120. }
  121. /**
  122. * 获取签到用户数量
  123. * @param array $where
  124. * @return mixed
  125. */
  126. public function getUserSignPoint(array $where)
  127. {
  128. return $this->search($where)->count();
  129. }
  130. /**
  131. * 修改收货状态
  132. * @param int $uid
  133. * @param int $id
  134. * @return \crmeb\basic\BaseModel
  135. */
  136. public function takeUpdate(int $uid, int $id)
  137. {
  138. return $this->getModel()->where('uid', $uid)->where('link_id', $id)->where('type', 'pay_money')->update(['take' => 1]);
  139. }
  140. /**
  141. * @param array $where
  142. * @return array
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\DbException
  145. * @throws \think\db\exception\ModelNotFoundException
  146. */
  147. public function getUserBillList(array $where)
  148. {
  149. return $this->search($where)->select()->toArray();
  150. }
  151. /**
  152. * 获取佣金排行
  153. * @param array $where
  154. * @param int $page
  155. * @param int $limit
  156. * @return array
  157. * @throws \think\db\exception\DataNotFoundException
  158. * @throws \think\db\exception\DbException
  159. * @throws \think\db\exception\ModelNotFoundException
  160. */
  161. public function brokerageRankList(array $where, int $page, int $limit)
  162. {
  163. return $this->search($where)->field('uid,SUM(IF(pm=1,`number`,-`number`)) as brokerage_price')->with(['user' => function ($query) {
  164. $query->field('uid,avatar,nickname');
  165. }])->order('brokerage_price desc')->group('uid')->page($page, $limit)->select()->toArray();
  166. }
  167. /**
  168. * 时间分组
  169. * @param array $where
  170. * @param string $filed
  171. * @param string $group
  172. * @param int $page
  173. * @param int $limit
  174. * @return mixed
  175. */
  176. public function getUserBillListByGroup(array $where, string $filed, string $group, int $page, int $limit)
  177. {
  178. return $this->search($where)->field($filed)->where('number', '>', 0)->order('add_time desc')->group($group)->page($page, $limit)->select()->toArray();
  179. }
  180. /**
  181. * @param array $where
  182. * @param int $page
  183. * @param int $limit
  184. * @return array
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\DbException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. */
  189. public function getBalanceRecord(array $where, int $page, int $limit)
  190. {
  191. return $this->search($where)->order('add_time desc')->page($page, $limit)->select()->toArray();
  192. }
  193. /**
  194. * 计算某个条件下订单内商品总数
  195. * @param $where
  196. * @return float|int
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. */
  201. public function getTotalSum(array $where)
  202. {
  203. $list = $this->search($where)->with('order')->select()->toArray();
  204. if (count($list)) {
  205. $sum = 0;
  206. foreach ($list as $item) {
  207. $sum += $item['total_num'];
  208. }
  209. return $sum;
  210. } else {
  211. return 0;
  212. }
  213. }
  214. /**
  215. * 获取某个字段总和
  216. * @param array $where
  217. * @param string $field
  218. * @return float
  219. */
  220. public function getWhereSumField(array $where, string $field)
  221. {
  222. return $this->search($where)
  223. ->when(isset($where['timeKey']), function ($query) use ($where) {
  224. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  225. })
  226. ->sum($field);
  227. }
  228. /**根据某字段分组查询
  229. * @param array $where
  230. * @param string $field
  231. * @param string $group
  232. * @return mixed
  233. */
  234. public function getGroupField(array $where, string $field, string $group)
  235. {
  236. return $this->search($where)
  237. ->when(isset($where['timeKey']), function ($query) use ($where, $field, $group) {
  238. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  239. if ($where['timeKey']['days'] == 1) {
  240. $timeUinx = "%H";
  241. } elseif ($where['timeKey']['days'] == 30) {
  242. $timeUinx = "%Y-%m-%d";
  243. } elseif ($where['timeKey']['days'] == 365) {
  244. $timeUinx = "%Y-%m";
  245. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  246. $timeUinx = "%Y-%m-%d";
  247. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  248. $timeUinx = "%Y-%m";
  249. }
  250. $query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  251. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  252. })
  253. ->order('add_time ASC')->select()->toArray();
  254. }
  255. /**
  256. * 获取退款佣金
  257. * @return mixed
  258. */
  259. public function getRefundBrokerage()
  260. {
  261. return $this->getModel()->whereIn('type', ['brokerage', 'brokerage_user'])
  262. ->where('category', 'now_money')
  263. ->where('pm', 0)
  264. ->group('uid')
  265. ->column('sum(number) as sum_number', 'uid');
  266. }
  267. }