UserRechargeRepository.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. namespace app\common\repositories\user;
  12. use app\common\dao\user\UserRechargeDao;
  13. use app\common\model\user\UserRecharge;
  14. use app\common\repositories\BaseRepository;
  15. use crmeb\jobs\SendTemplateMessageJob;
  16. use crmeb\services\MiniProgramService;
  17. use crmeb\services\WechatService;
  18. use EasyWeChat\Support\Collection;
  19. use Exception;
  20. use think\facade\Db;
  21. use think\facade\Queue;
  22. /**
  23. * Class UserRechargeRepository
  24. * @package app\common\repositories\user
  25. * @author xaboy
  26. * @day 2020/6/2
  27. * @mixin UserRechargeDao
  28. */
  29. class UserRechargeRepository extends BaseRepository
  30. {
  31. /**
  32. * UserRechargeRepository constructor.
  33. * @param UserRechargeDao $dao
  34. */
  35. public function __construct(UserRechargeDao $dao)
  36. {
  37. $this->dao = $dao;
  38. }
  39. public function create($uid, float $price, float $givePrice, string $type)
  40. {
  41. return $this->dao->create([
  42. 'uid' => $uid,
  43. 'price' => $price,
  44. 'give_price' => $givePrice,
  45. 'recharge_type' => $type,
  46. 'paid' => 0,
  47. 'order_id' => $this->dao->createOrderId($uid)
  48. ]);
  49. }
  50. public function getList($where, $page, $limit)
  51. {
  52. $query = $this->dao->searchJoinQuery($where)->order('a.pay_time DESC,a.create_time DESC');
  53. $count = $query->count();
  54. $list = $query->page($page, $limit)->select();
  55. return compact('count', 'list');
  56. }
  57. public function priceByGive($price)
  58. {
  59. $quota = systemGroupData('user_recharge_quota');
  60. $give = 0;
  61. foreach ($quota as $item) {
  62. $min = floatval($item['price']);
  63. $_give = floatval($item['give']);
  64. if ($price > $min) $give = $_give;
  65. }
  66. return $give;
  67. }
  68. /**
  69. * 充值js支付
  70. * @param $openId
  71. * @param UserRecharge $recharge
  72. * @return array|string
  73. * @author xaboy
  74. * @day 2020/6/2
  75. */
  76. public function jsPay($openId, UserRecharge $recharge)
  77. {
  78. return MiniProgramService::create()->jsPay($openId, $recharge['order_id'], $recharge['price'], 'user_recharge', '用户充值');
  79. }
  80. /**
  81. * 微信H5支付
  82. * @param UserRecharge $recharge
  83. * @return Collection
  84. * @author xaboy
  85. * @day 2020/6/2
  86. */
  87. public function wxH5Pay(UserRecharge $recharge)
  88. {
  89. return WechatService::create()->paymentPrepare(null, $recharge['order_id'], $recharge['price'], 'user_recharge', '用户充值', '', 'MWEB');
  90. }
  91. /**
  92. * 公众号支付
  93. * @param $openId
  94. * @param UserRecharge $recharge
  95. * @return array|string
  96. * @author xaboy
  97. * @day 2020/6/2
  98. */
  99. public function wxPay($openId, UserRecharge $recharge)
  100. {
  101. return WechatService::create()->jsPay($openId, $recharge['order_id'], $recharge['price'], 'user_recharge', '用户充值');
  102. }
  103. /**
  104. * //TODO 余额充值成功
  105. *
  106. * @param $orderId
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. * @author xaboy
  111. * @day 2020/6/19
  112. */
  113. public function paySuccess($orderId)
  114. {
  115. $recharge = $this->dao->getWhere(['order_id' => $orderId]);
  116. if ($recharge->paid == 1) return;
  117. $recharge->paid = 1;
  118. $recharge->pay_time = date('Y-m-d H:i:s');
  119. Db::transaction(function () use ($recharge) {
  120. $price = bcadd($recharge->price, $recharge->give_price, 2);
  121. $mark = '成功充值余额' . floatval($recharge->price) . '元' . ($recharge->give_price > 0 ? ',赠送' . $recharge->give_price . '元' : '');
  122. app()->make(UserBillRepository::class)->incBill($recharge->user->uid, 'now_money', 'recharge', [
  123. 'link_id' => $recharge->recharge_id,
  124. 'status' => 1,
  125. 'title' => '余额充值',
  126. 'number' => $price,
  127. 'mark' => $mark,
  128. 'balance' => $recharge->user->now_money
  129. ]);
  130. $recharge->user->now_money = bcadd($recharge->user->now_money, $price, 2);
  131. $recharge->user->save();
  132. $recharge->save();
  133. });
  134. Queue::push(SendTemplateMessageJob::class,[
  135. 'tempCode' => 'ORDER_DELIVER_SUCCESS',
  136. 'id' =>$orderId
  137. ]);
  138. }
  139. }