UserRechargeRepository.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\common\repositories\user;
  3. use app\common\dao\user\UserRechargeDao;
  4. use app\common\model\user\User;
  5. use app\common\model\user\UserRecharge;
  6. use app\common\repositories\BaseRepository;
  7. use ln\jobs\SendTemplateMessageJob;
  8. use ln\services\MiniProgramService;
  9. use ln\services\PayService;
  10. use ln\services\WechatService;
  11. use EasyWeChat\Support\Collection;
  12. use Exception;
  13. use think\facade\Db;
  14. use think\facade\Queue;
  15. /**
  16. * Class UserRechargeRepository
  17. * @package app\common\repositories\user
  18. * @author zfy
  19. * @day 2020/6/2
  20. * @mixin UserRechargeDao
  21. */
  22. class UserRechargeRepository extends BaseRepository
  23. {
  24. /**
  25. * UserRechargeRepository constructor.
  26. * @param UserRechargeDao $dao
  27. */
  28. public function __construct(UserRechargeDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. public function create($uid, float $price, float $givePrice, string $type)
  33. {
  34. return $this->dao->create([
  35. 'uid' => $uid,
  36. 'price' => $price,
  37. 'give_price' => $givePrice,
  38. 'recharge_type' => $type,
  39. 'paid' => 0,
  40. 'order_id' => $this->dao->createOrderId($uid)
  41. ]);
  42. }
  43. public function getList($where, $page, $limit)
  44. {
  45. $query = $this->dao->searchJoinQuery($where)->order('a.pay_time DESC,a.create_time DESC');
  46. $count = $query->count();
  47. $list = $query->page($page, $limit)->select();
  48. return compact('count', 'list');
  49. }
  50. public function priceByGive($price)
  51. {
  52. $quota = systemGroupData('user_recharge_quota');
  53. $give = 0;
  54. foreach ($quota as $item) {
  55. $min = floatval($item['price']);
  56. $_give = floatval($item['give']);
  57. if ($price > $min) $give = $_give;
  58. }
  59. return $give;
  60. }
  61. /**
  62. * @param string $type
  63. * @param User $user
  64. * @param UserRecharge $recharge
  65. * @param string $return_url
  66. * @return mixed
  67. * @author zfy
  68. * @day 2020/10/22
  69. */
  70. public function pay(string $type, User $user, UserRecharge $recharge, $return_url = '', $isApp = false)
  71. {
  72. if (in_array($type, ['weixin', 'alipay'], true) && $isApp) {
  73. $type .= 'App';
  74. }
  75. $service = new PayService($type, $recharge->getPayParams($type === 'alipay' ? $return_url : ''));
  76. $config = $service->pay($user);
  77. return $config + ['recharge_id' => $recharge['recharge_id'], 'type' => $type];
  78. }
  79. /**
  80. * //TODO 余额充值成功
  81. *
  82. * @param $orderId
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. * @author zfy
  87. * @day 2020/6/19
  88. */
  89. public function paySuccess($orderId)
  90. {
  91. $recharge = $this->dao->getWhere(['order_id' => $orderId]);
  92. if ($recharge->paid == 1) return;
  93. $recharge->paid = 1;
  94. $recharge->pay_time = date('Y-m-d H:i:s');
  95. Db::transaction(function () use ($recharge) {
  96. $price = bcadd($recharge->price, $recharge->give_price, 2);
  97. $mark = '成功充值余额' . floatval($recharge->price) . '元' . ($recharge->give_price > 0 ? ',赠送' . $recharge->give_price . '元' : '');
  98. app()->make(UserBillRepository::class)->incBill($recharge->user->uid, 'now_money', 'recharge', [
  99. 'link_id' => $recharge->recharge_id,
  100. 'status' => 1,
  101. 'title' => '余额充值',
  102. 'number' => $price,
  103. 'mark' => $mark,
  104. 'balance' => $recharge->user->now_money
  105. ]);
  106. $recharge->user->now_money = bcadd($recharge->user->now_money, $price, 2);
  107. $recharge->user->save();
  108. $recharge->save();
  109. });
  110. Queue::push(SendTemplateMessageJob::class,[
  111. 'tempCode' => 'ORDER_DELIVER_SUCCESS',
  112. 'id' =>$orderId
  113. ]);
  114. }
  115. }