UserBillRepository.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace app\common\repositories\user;
  3. use app\common\dao\BaseDao;
  4. use app\common\dao\user\UserBillDao;
  5. use app\common\repositories\BaseRepository;
  6. use app\common\repositories\store\order\StoreOrderRepository;
  7. use app\common\repositories\store\order\StoreRefundOrderRepository;
  8. use app\common\repositories\store\product\ProductRepository;
  9. use ln\jobs\SendTemplateMessageJob;
  10. use think\facade\Queue;
  11. use think\Model;
  12. /**
  13. * Class UserBillRepository
  14. * @package app\common\repositories\user
  15. * @author zfy
  16. * @day 2020-05-07
  17. * @mixin UserBillDao
  18. */
  19. class UserBillRepository extends BaseRepository
  20. {
  21. /**
  22. * @var UserBillDao
  23. */
  24. protected $dao;
  25. const TYPE_INFO = [
  26. 'presell' => '支付预售尾款',
  27. 'pay_product' => '购买商品',
  28. 'order_one' => '获得一级推广佣金',
  29. 'order_two' => '获得二级推广佣金',
  30. 'refund_one' => '退回一级推广佣金',
  31. 'refund_two' => '退回二级推广佣金',
  32. 'refund' => '商品退款',
  33. 'recharge' => '余额充值',
  34. 'sys_inc_money' => '系统增加余额',
  35. 'sys_dec_money' => '系统减少余额',
  36. 'brokerage' => '佣金转入余额',
  37. 'sign_integral' => '签到积分',
  38. ];
  39. /**
  40. * UserBillRepository constructor.
  41. * @param UserBillDao $dao
  42. */
  43. public function __construct(UserBillDao $dao)
  44. {
  45. $this->dao = $dao;
  46. }
  47. public function userList($where, $uid, $page, $limit)
  48. {
  49. $where['uid'] = $uid;
  50. $query = $this->dao->search($where)->order('create_time DESC');
  51. $count = $query->count();
  52. $list = $query->setOption('field', [])->field('bill_id,pm,title,number,balance,mark,create_time,status')->page($page, $limit)->select();
  53. return compact('count', 'list');
  54. }
  55. public function getList($where, $page, $limit)
  56. {
  57. $query = $this->dao->searchJoin($where)->order('a.create_time DESC');
  58. $count = $query->count();
  59. $list = $query->page($page, $limit)->select();
  60. return compact('count', 'list');
  61. }
  62. /**
  63. * @param int $uid
  64. * @param string $category
  65. * @param string $type
  66. * @param int $pm
  67. * @param array $data
  68. * @return BaseDao|Model
  69. * @author zfy
  70. * @day 2020-05-07
  71. */
  72. public function bill(int $uid, string $category, string $type, int $pm, array $data)
  73. {
  74. $data['category'] = $category;
  75. $data['type'] = $type;
  76. $data['uid'] = $uid;
  77. $data['pm'] = $pm;
  78. $bill = $this->dao->create($data);
  79. if($category == 'now_money'){
  80. $tempData = ['tempCode' => 'USER_BALANCE_CHANGE','id' => $bill->bill_id];
  81. Queue::push(SendTemplateMessageJob::class,$tempData);
  82. }
  83. return $bill;
  84. }
  85. /**
  86. * @param int $uid
  87. * @param string $category
  88. * @param string $type
  89. * @param array $data
  90. * @return BaseDao|Model
  91. * @author zfy
  92. * @day 2020-05-07
  93. */
  94. public function incBill(int $uid, string $category, string $type, array $data)
  95. {
  96. return $this->bill($uid, $category, $type, 1, $data);
  97. }
  98. /**
  99. * @param int $uid
  100. * @param string $category
  101. * @param string $type
  102. * @param array $data
  103. * @return BaseDao|Model
  104. * @author zfy
  105. * @day 2020-05-07
  106. */
  107. public function decBill(int $uid, string $category, string $type, array $data)
  108. {
  109. return $this->bill($uid, $category, $type, 0, $data);
  110. }
  111. public function type()
  112. {
  113. $data = [];
  114. foreach (self::TYPE_INFO as $type => $title) {
  115. $data[] = compact('type', 'title');
  116. }
  117. return $data;
  118. }
  119. /**
  120. * TODO 积分日志头部统计
  121. * @return array
  122. * @author Qinii
  123. * @day 6/9/21
  124. */
  125. public function getStat($merId = 0)
  126. {
  127. if($merId){
  128. $isusd = app()->make(ProductRepository::class)->getSearch(['mer_id' => $merId])->sum('integral_total');
  129. $refund = $this->dao->search(['category' => 'mer_integral','type' => 'refund','mer_id' => $merId])->sum('number');
  130. $numb = app()->make(ProductRepository::class)->getSearch(['mer_id' => $merId])->sum('integral_price_total');
  131. return [
  132. [
  133. 'className' => 'el-icon-s-cooperation',
  134. 'count' => $isusd,
  135. 'field' => '个',
  136. 'name' => '已使用积分(分)'
  137. ],
  138. [
  139. 'className' => 'el-icon-edit',
  140. 'count' => $refund,
  141. 'field' => '次',
  142. 'name' => '退款订单返回积分(分)'
  143. ],
  144. [
  145. 'className' => 'el-icon-edit',
  146. 'count' => $numb,
  147. 'field' => '次',
  148. 'name' => '积分抵扣金额(元)'
  149. ],
  150. ];
  151. }
  152. // 总积分
  153. $integral = app()->make(UserRepository::class)->search(['status' => 1])->sum('integral');
  154. // 客户签到次数
  155. $sign = app()->make(UserSignRepository::class)->getSearch([])->count('*');
  156. // 签到送出积分
  157. $sign_integral = $this->dao->search(['type' => 'sign_integral'])->sum('number');
  158. // 使用积分
  159. $isusd = $this->dao->search(['category' => 'integral','type' => 'deduction'])->sum('number');
  160. $refund = $this->dao->search(['category' => 'mer_integral','type' => 'refund'])->sum('number');
  161. $order = $isusd - $refund;
  162. // 下单赠送积分
  163. $order_integral1 = $this->dao->search(['category' => 'integral','type' => 'lock'])->sum('number');
  164. $order_integral2 = $this->dao->search(['category' => 'integral','type' => 'refund_lock'])->sum('number');
  165. $order_integral = $order_integral1 - $order_integral2;
  166. $order_integral = $order_integral < 0 ? 0 : $order_integral;
  167. // 冻结积分
  168. $freeze_integral = $this->dao->lockIntegral();
  169. return [
  170. [
  171. 'className' => 'el-icon-s-cooperation',
  172. 'count' => $integral,
  173. 'field' => '个',
  174. 'name' => '总积分'
  175. ],
  176. [
  177. 'className' => 'el-icon-edit',
  178. 'count' => $sign,
  179. 'field' => '次',
  180. 'name' => '客户签到次数'
  181. ],
  182. [
  183. 'className' => 'el-icon-s-goods',
  184. 'count' => $sign_integral ,
  185. 'field' => '个',
  186. 'name' => '签到送出积分'
  187. ],
  188. [
  189. 'className' => 'el-icon-s-order',
  190. 'count' => $order,
  191. 'field' => '个',
  192. 'name' => '使用积分'
  193. ],
  194. [
  195. 'className' => 'el-icon-present',
  196. 'count' => $order_integral,
  197. 'field' => '个',
  198. 'name' => '下单赠送积分'
  199. ],
  200. [
  201. 'className' => 'el-icon-warning',
  202. 'count' => $freeze_integral,
  203. 'field' => '',
  204. 'name' => '冻结积分'
  205. ],
  206. ];
  207. }
  208. }