UserBillDao.php 8.8 KB

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