UserBillDao.php 10 KB

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