UserBillRepository.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\user;
  12. use app\common\dao\BaseDao;
  13. use app\common\dao\user\UserBillDao;
  14. use app\common\repositories\BaseRepository;
  15. use app\common\repositories\store\product\ProductRepository;
  16. use crmeb\jobs\SendSmsJob;
  17. use think\facade\Queue;
  18. use think\Model;
  19. /**
  20. * Class UserBillRepository
  21. * @package app\common\repositories\user
  22. * @author xaboy
  23. * @day 2020-05-07
  24. * @mixin UserBillDao
  25. */
  26. class UserBillRepository extends BaseRepository
  27. {
  28. /**
  29. * @var UserBillDao
  30. */
  31. protected $dao;
  32. const TYPE_INFO = [
  33. 'brokerage/now_money' => '佣金转入余额',
  34. 'brokerage/order_one' => '获得一级推广佣金',
  35. 'brokerage/order_two' => '获得二级推广佣金',
  36. 'brokerage/refund_one' => '退还一级佣金',
  37. 'brokerage/refund_two' => '退还二级佣金',
  38. 'integral/cancel' => '退回积分',
  39. 'integral/deduction' => '购买商品',
  40. 'integral/lock' => '下单赠送积分',
  41. 'integral/refund' => '订单退款',
  42. 'integral/refund_lock' => '扣除赠送积分',
  43. 'integral/sign_integral' => '签到赠送积分',
  44. 'integral/spread' => '邀请好友',
  45. 'integral/sys_dec' => '系统减少积分',
  46. 'integral/sys_inc' => '系统增加积分',
  47. 'integral/timeout' => '积分过期',
  48. 'mer_integral/deduction' => '积分抵扣',
  49. 'mer_integral/refund' => '订单退款',
  50. 'mer_lock_money/order' => '商户佣金冻结',
  51. 'now_money/brokerage' => '佣金转入余额',
  52. 'now_money/pay_product' => '购买商品',
  53. 'now_money/presell' => '支付预售尾款',
  54. 'now_money/recharge' => '余额充值',
  55. 'now_money/sys_dec_money' => '系统减少余额',
  56. 'now_money/sys_inc_money' => '系统增加余额',
  57. ];
  58. /**
  59. * UserBillRepository constructor.
  60. * @param UserBillDao $dao
  61. */
  62. public function __construct(UserBillDao $dao)
  63. {
  64. $this->dao = $dao;
  65. }
  66. public function userList($where, $uid, $page, $limit)
  67. {
  68. $where['uid'] = $uid;
  69. $query = $this->dao->search($where)->order('create_time DESC');
  70. $count = $query->count();
  71. $list = $query->setOption('field', [])->field('bill_id,pm,title,number,balance,mark,create_time,status')->page($page, $limit)->select();
  72. return compact('count', 'list');
  73. }
  74. public function month(array $where)
  75. {
  76. $group = $this->dao->search($where)->field('FROM_UNIXTIME(unix_timestamp(create_time),"%Y-%m") as time')
  77. ->order('time DESC')->group('time')->select();
  78. $ret = [];
  79. foreach ($group as $k => $item){
  80. $ret[$k]['month'] = $item['time'];
  81. $query = $this->dao->getSearch($where)->field('title,number,create_time')->whereMonth('create_time',$item['time']);
  82. $ret[$k]['list'] = $query->order('create_time DESC')->select();
  83. }
  84. return $ret;
  85. }
  86. public function getList($where, $page, $limit)
  87. {
  88. $query = $this->dao->searchJoin($where)->order('a.create_time DESC');
  89. $count = $query->count();
  90. $list = $query->page($page, $limit)->select();
  91. return compact('count', 'list');
  92. }
  93. public function getLst($where, $page, $limit)
  94. {
  95. $query = $this->dao->search($where)->order('create_time DESC');
  96. $count = $query->count();
  97. $list = $query->page($page, $limit)->select();
  98. return compact('count', 'list');
  99. }
  100. /**
  101. * @param int $uid
  102. * @param string $category
  103. * @param string $type
  104. * @param int $pm
  105. * @param array $data
  106. * @return BaseDao|Model
  107. * @author xaboy
  108. * @day 2020-05-07
  109. */
  110. public function bill(int $uid, string $category, string $type, int $pm, array $data)
  111. {
  112. $data['category'] = $category;
  113. $data['type'] = $type;
  114. $data['uid'] = $uid;
  115. $data['pm'] = $pm;
  116. $bill = $this->dao->create($data);
  117. if($category == 'now_money'){
  118. Queue::push(SendSmsJob::class,['tempId' => 'USER_BALANCE_CHANGE','id' => $bill->bill_id]);
  119. }
  120. return $bill;
  121. }
  122. /**
  123. * @param int $uid
  124. * @param string $category
  125. * @param string $type
  126. * @param array $data
  127. * @return BaseDao|Model
  128. * @author xaboy
  129. * @day 2020-05-07
  130. */
  131. public function incBill(int $uid, string $category, string $type, array $data)
  132. {
  133. return $this->bill($uid, $category, $type, 1, $data);
  134. }
  135. /**
  136. * @param int $uid
  137. * @param string $category
  138. * @param string $type
  139. * @param array $data
  140. * @return BaseDao|Model
  141. * @author xaboy
  142. * @day 2020-05-07
  143. */
  144. public function decBill(int $uid, string $category, string $type, array $data)
  145. {
  146. return $this->bill($uid, $category, $type, 0, $data);
  147. }
  148. public function type()
  149. {
  150. $data = [];
  151. foreach (self::TYPE_INFO as $type => $title) {
  152. $data[] = compact('type', 'title');
  153. }
  154. return $data;
  155. }
  156. /**
  157. * TODO 积分日志头部统计
  158. * @return array
  159. * @author Qinii
  160. * @day 6/9/21
  161. */
  162. public function getStat($merId = 0)
  163. {
  164. if($merId){
  165. $isusd = app()->make(ProductRepository::class)->getSearch(['mer_id' => $merId])->sum('integral_total');
  166. $refund = $this->dao->search(['category' => 'mer_integral','type' => 'refund','mer_id' => $merId])->sum('number');
  167. $numb = app()->make(ProductRepository::class)->getSearch(['mer_id' => $merId])->sum('integral_price_total');
  168. return [
  169. [
  170. 'className' => 'el-icon-s-cooperation',
  171. 'count' => $isusd,
  172. 'field' => '个',
  173. 'name' => '已使用积分(分)'
  174. ],
  175. [
  176. 'className' => 'el-icon-edit',
  177. 'count' => $refund,
  178. 'field' => '次',
  179. 'name' => '退款订单返回积分(分)'
  180. ],
  181. [
  182. 'className' => 'el-icon-edit',
  183. 'count' => $numb,
  184. 'field' => '次',
  185. 'name' => '积分抵扣金额(元)'
  186. ],
  187. ];
  188. }
  189. // 总积分
  190. $integral = app()->make(UserRepository::class)->search(['status' => 1])->sum('integral');
  191. // 客户签到次数
  192. $sign = app()->make(UserSignRepository::class)->getSearch([])->count('*');
  193. // 签到送出积分
  194. $sign_integral = $this->dao->search(['type' => 'sign_integral'])->sum('number');
  195. // 使用积分
  196. $isusd = $this->dao->search(['category' => 'integral','type' => 'deduction'])->sum('number');
  197. $refund = $this->dao->search(['category' => 'mer_integral','type' => 'refund'])->sum('number');
  198. $order = $isusd - $refund;
  199. // 下单赠送积分
  200. $order_integral1 = $this->dao->search(['category' => 'integral','type' => 'lock'])->sum('number');
  201. $order_integral2 = $this->dao->search(['category' => 'integral','type' => 'refund_lock'])->sum('number');
  202. $order_integral = $order_integral1 - $order_integral2;
  203. $order_integral = $order_integral < 0 ? 0 : $order_integral;
  204. // 冻结积分
  205. $freeze_integral = $this->dao->lockIntegral();
  206. return [
  207. [
  208. 'className' => 'el-icon-s-cooperation',
  209. 'count' => $integral,
  210. 'field' => '个',
  211. 'name' => '总积分'
  212. ],
  213. [
  214. 'className' => 'el-icon-edit',
  215. 'count' => $sign,
  216. 'field' => '次',
  217. 'name' => '客户签到次数'
  218. ],
  219. [
  220. 'className' => 'el-icon-s-goods',
  221. 'count' => $sign_integral ,
  222. 'field' => '个',
  223. 'name' => '签到送出积分'
  224. ],
  225. [
  226. 'className' => 'el-icon-s-order',
  227. 'count' => $order,
  228. 'field' => '个',
  229. 'name' => '使用积分'
  230. ],
  231. [
  232. 'className' => 'el-icon-present',
  233. 'count' => $order_integral,
  234. 'field' => '个',
  235. 'name' => '下单赠送积分'
  236. ],
  237. [
  238. 'className' => 'el-icon-warning',
  239. 'count' => $freeze_integral,
  240. 'field' => '',
  241. 'name' => '冻结积分'
  242. ],
  243. ];
  244. }
  245. }