UserRechargeRepository.php 4.3 KB

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