UserOfflineRechargeRepository.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\merchant\order\OrderMerchantDao;
  12. use app\common\dao\user\UserOfflineRechargeDao;
  13. use app\common\model\merchant\order\OrderMerchant;
  14. use app\common\repositories\BaseRepository;
  15. use think\facade\Db;
  16. use think\facade\Log;
  17. /**
  18. * 用户线下充值
  19. * Class UserOfflineRechargeRepository
  20. * @package app\common\repositories\user
  21. * @author php迷途小书童
  22. * @created 2021/1/30 15:59
  23. */
  24. class UserOfflineRechargeRepository extends BaseRepository
  25. {
  26. /**
  27. * UserRechargeRepository constructor.
  28. * @param UserOfflineRechargeDao $dao
  29. */
  30. public function __construct(UserOfflineRechargeDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 线下充值成功后
  36. * @return bool
  37. * @author php迷途小书童
  38. * @created 2021/2/1 15:09
  39. */
  40. public function paySuccess($orderId, $orderSn, $uid, $mer_id, $money, $give_money, $pension, $avaiable, $pay_user_id)
  41. {
  42. if ($money <= 0) return true;
  43. Db::transaction(function () use ($money, $mer_id, $give_money, $pension, $uid, $orderId, $orderSn, $avaiable, $pay_user_id) {
  44. $userRepository = app()->make(UserRepository::class);
  45. $user = $userRepository->getOne(['uid' => $uid]);
  46. $recharge_money = bcadd($money, $give_money, 2);
  47. $recharge = $this->dao->getWhere(['uid' => $uid, 'mer_id' => $mer_id]);
  48. if (!$recharge) {
  49. $this->dao->create([
  50. 'uid' => $uid,
  51. 'mer_id' => $mer_id,
  52. 'money' => $recharge_money,
  53. 'pension' => $pension,
  54. ]);
  55. $balance = $recharge_money;
  56. } else {
  57. $recharge->money += $recharge_money;
  58. $recharge->pension += $pension;
  59. $recharge->save();
  60. $balance = $recharge->money;
  61. }
  62. $unionUsersRepository = app()->make(UnionUsersRepository::class);
  63. $unionUser = $unionUsersRepository->getWhere(['unionid' => $pay_user_id,'uid' => 0]);
  64. //更新三方注册信息UID
  65. if ($unionUser) {
  66. $recharge->money += $unionUser->balance;
  67. $recharge->pension += $unionUser->pension;
  68. $recharge->save();
  69. $unionUser->uid = $user->uid;
  70. $unionUser->save();
  71. }
  72. $price = bcadd($money, $give_money, 2);
  73. Log::info('price'.$price.'orderId'.$orderId);
  74. $mark = '线下扫码充值' . floatval($money) . '元' . ($give_money > 0 ? ',赠送' . $give_money . '元' : '');
  75. app()->make(UserBillRepository::class)->incBill($uid, 'offline_recharge_money', 'offline_recharge', [
  76. 'link_id' => $orderId,
  77. 'pm' => 1,
  78. 'status' => 1,
  79. 'title' => '线下扫码充值',
  80. 'number' => $price,
  81. 'mark' => $mark,
  82. 'balance' => $balance,
  83. 'order_sn' => $orderSn,
  84. 'mer_id' => $mer_id,
  85. ]);
  86. });
  87. }
  88. /**
  89. * 用户绑定成功后
  90. * @return bool
  91. * @author php迷途小书童
  92. * @created 2021/2/1 15:09
  93. */
  94. public function userBindSuccess($unionUser)
  95. {
  96. Db::transaction(function () use ($unionUser) {
  97. $recharge = $this->dao->getWhere(['uid' => $unionUser->uid, 'mer_id' => $unionUser->lock_mer_id]);
  98. if (!$recharge) {
  99. $this->dao->create([
  100. 'uid' => $unionUser->uid,
  101. 'mer_id' => $unionUser->lock_mer_id,
  102. 'money' => $unionUser->balance,
  103. 'give_money' => $unionUser->give_money,
  104. 'pension' => $unionUser->pension,
  105. ]);
  106. $balance = $unionUser->balance;
  107. } else {
  108. $recharge->money += $unionUser->balance;
  109. $recharge->give_money += $unionUser->give_money;
  110. $recharge->pension += $unionUser->pension;
  111. $recharge->save();
  112. $balance = $recharge->money;
  113. }
  114. $mark = '线下扫码充值' . floatval($unionUser->balance) . '元' . ($unionUser->give_money > 0 ? ',赠送' . $unionUser->give_money . '元' : '');
  115. app()->make(UserBillRepository::class)->incBill($unionUser->uid, 'offline_recharge_money', 'offline_recharge', [
  116. 'link_id' => $unionUser->id,
  117. 'pm' => 1,
  118. 'status' => 1,
  119. 'title' => '线下扫码充值',
  120. 'number' => $unionUser->give_money,
  121. 'mark' => $mark,
  122. 'balance' => $balance,
  123. 'order_sn' => $unionUser->id,
  124. 'mer_id' => $unionUser->lock_mer_id,
  125. ]);
  126. //返养老金
  127. try {
  128. $userRepository = app()->make(UserRepository::class);
  129. $user = $userRepository->getOne(['uid' => $unionUser->uid]);
  130. if ($unionUser->pension <= 0) return true;
  131. //496商户线下支付不返回养老金
  132. $orders = OrderMerchant::getDB()->where('pay_user_id', $unionUser->unionid)->where('status', 1) ->whereNotIn('mer_id', [496]) ->where('binds',0)->select();
  133. foreach ($orders as $order) {
  134. app()->make(UserBillRepository::class)->incBill($unionUser->uid, 'now_money', 'commission', [
  135. 'link_id' => $order->id,
  136. 'pm' => 1,
  137. 'status' => 1,
  138. 'title' => '养老金',
  139. 'number' => $order->pension,
  140. 'mark' => '线下消费获得养老金',
  141. 'commission_type' => 5,
  142. 'balance' => $user->annuity,
  143. 'order_sn' => $order->out_trade_no,
  144. 'mer_id' => $order->mer_id,
  145. 'source' => 1
  146. ]);
  147. $user->annuity += $order->pension;
  148. $user->annuity_num += 1;
  149. $user->save();
  150. OrderMerchant::getDB()->where('pay_user_id', $unionUser->unionid)->update(['uid' => $unionUser->uid, 'binds' => 1]);
  151. }
  152. } catch (\Throwable $e) {
  153. Log::info($e->getMessage());
  154. }
  155. });
  156. }
  157. }