UserOfflineRechargeRepository.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. use app\common\repositories\merchant\order\OrderMerchantRepository;
  18. /**
  19. * 用户线下充值
  20. * Class UserOfflineRechargeRepository
  21. * @package app\common\repositories\user
  22. * @author php迷途小书童
  23. * @created 2021/1/30 15:59
  24. */
  25. class UserOfflineRechargeRepository extends BaseRepository
  26. {
  27. /**
  28. * UserRechargeRepository constructor.
  29. * @param UserOfflineRechargeDao $dao
  30. */
  31. public function __construct(UserOfflineRechargeDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 线下充值成功后
  37. * @return bool
  38. * @author php迷途小书童
  39. * @created 2021/2/1 15:09
  40. */
  41. public function paySuccess($orderId, $orderSn, $uid, $mer_id, $money, $give_money, $pension, $avaiable, $pay_user_id)
  42. {
  43. if ($money <= 0) return true;
  44. Db::transaction(function () use ($money, $mer_id, $give_money, $pension, $uid, $orderId, $orderSn, $avaiable, $pay_user_id) {
  45. $userRepository = app()->make(UserRepository::class);
  46. $user = $userRepository->getOne(['uid' => $uid]);
  47. $recharge_money = bcadd($money, $give_money, 2);
  48. $recharge = $this->dao->getWhere(['uid' => $uid, 'mer_id' => $mer_id]);
  49. if (!$recharge) {
  50. $this->dao->create([
  51. 'uid' => $uid,
  52. 'mer_id' => $mer_id,
  53. 'money' => $recharge_money,
  54. 'pension' => $pension,
  55. ]);
  56. $balance = $recharge_money;
  57. } else {
  58. $recharge->money += $recharge_money;
  59. $recharge->pension += $pension;
  60. $recharge->save();
  61. $balance = $recharge->money;
  62. }
  63. $unionUsersRepository = app()->make(UnionUsersRepository::class);
  64. $unionUser = $unionUsersRepository->getWhere(['unionid' => $pay_user_id,'uid' => 0]);
  65. //更新三方注册信息UID
  66. if ($unionUser) {
  67. $recharge->money += $unionUser->balance;
  68. $recharge->pension += $unionUser->pension;
  69. $recharge->save();
  70. $unionUser->uid = $user->uid;
  71. $unionUser->save();
  72. }
  73. $price = bcadd($money, $give_money, 2);
  74. Log::info('price'.$price.'orderId'.$orderId);
  75. $mark = '线下扫码充值' . floatval($money) . '元' . ($give_money > 0 ? ',赠送' . $give_money . '元' : '');
  76. app()->make(UserBillRepository::class)->incBill($uid, 'offline_recharge_money', 'offline_recharge', [
  77. 'link_id' => $orderId,
  78. 'pm' => 1,
  79. 'status' => 1,
  80. 'title' => '线下扫码充值',
  81. 'number' => $price,
  82. 'mark' => $mark,
  83. 'balance' => $balance,
  84. 'order_sn' => $orderSn,
  85. 'mer_id' => $mer_id,
  86. ]);
  87. });
  88. }
  89. /**
  90. * 用户绑定成功后
  91. * @return bool
  92. * @author php迷途小书童
  93. * @created 2021/2/1 15:09
  94. */
  95. public function userBindSuccess($unionUser)
  96. {
  97. Db::transaction(function () use ($unionUser) {
  98. $recharge = $this->dao->getWhere(['uid' => $unionUser->uid, 'mer_id' => $unionUser->lock_mer_id]);
  99. if (!$recharge) {
  100. $this->dao->create([
  101. 'uid' => $unionUser->uid,
  102. 'mer_id' => $unionUser->lock_mer_id,
  103. 'money' => $unionUser->balance,
  104. 'give_money' => $unionUser->give_money,
  105. 'pension' => $unionUser->pension,
  106. ]);
  107. $balance = $unionUser->balance;
  108. } else {
  109. $recharge->money += $unionUser->balance;
  110. $recharge->give_money += $unionUser->give_money;
  111. $recharge->pension += $unionUser->pension;
  112. $recharge->save();
  113. $balance = $recharge->money;
  114. }
  115. $mark = '线下扫码充值' . floatval($unionUser->balance) . '元' . ($unionUser->give_money > 0 ? ',赠送' . $unionUser->give_money . '元' : '');
  116. app()->make(UserBillRepository::class)->incBill($unionUser->uid, 'offline_recharge_money', 'offline_recharge', [
  117. 'link_id' => $unionUser->id,
  118. 'pm' => 1,
  119. 'status' => 1,
  120. 'title' => '线下扫码充值',
  121. 'number' => $unionUser->give_money,
  122. 'mark' => $mark,
  123. 'balance' => $balance,
  124. 'order_sn' => $unionUser->id,
  125. 'mer_id' => $unionUser->lock_mer_id,
  126. ]);
  127. //返养老金
  128. try {
  129. $userRepository = app()->make(UserRepository::class);
  130. $user = $userRepository->getOne(['uid' => $unionUser->uid]);
  131. if ($unionUser->pension <= 0) return true;
  132. //496商户线下支付不返回养老金
  133. $orders = OrderMerchant::getDB()->where('pay_user_id', $unionUser->unionid)->where('status', 1) ->whereNotIn('mer_id', [496]) ->where('binds',0)->select();
  134. foreach ($orders as $order) {
  135. // app()->make(UserBillRepository::class)->incBill($unionUser->uid, 'now_money', 'commission', [
  136. // 'link_id' => $order->id,
  137. // 'pm' => 1,
  138. // 'status' => 1,
  139. // 'title' => '养老金',
  140. // 'number' => $order->pension,
  141. // 'mark' => '线下消费获得养老金',
  142. // 'commission_type' => 5,
  143. // 'balance' => $user->annuity,
  144. // 'order_sn' => $order->out_trade_no,
  145. // 'mer_id' => $order->mer_id,
  146. // 'source' => 1
  147. // ]);
  148. // $user->annuity += $order->pension;
  149. // $user->annuity_num += 1;
  150. if (!$user->mer_id || !$user->locktime) {
  151. Db::name('user')->where('uid', $unionUser->uid)->update(['mer_id' => $order->mer_id, 'locktime' => date('Y-m-d H:i:s')]);
  152. }
  153. // $user->save();
  154. //同步用户部分------------------------------------
  155. //同步红包
  156. // Db::name("user_sign_hongbao")->where('order_id', $order->id)->update(['uid' => $unionUser->uid]);
  157. // //同步佣金
  158. // Db::name("user_sign_hongbao")->where('order_id', $order->id)->update(['uid' => $unionUser->uid]);
  159. // //同步积分
  160. // Db::name("user_sign_score")->where('order_id', $order->id)->update(['uid' => $unionUser->uid]);
  161. // //同步复购金
  162. // Db::name("user_sign_fugou")->where('order_id', $order->id)->update(['uid' => $unionUser->uid]);
  163. //同步贡献值
  164. $gongxian = Db::name('user_sign_gongxian')->where('order_id', $order->id)->find();
  165. if($gongxian){
  166. Db::name("user_sign_gongxian")->where('order_id', $order->id)->update(['uid' => $unionUser->uid]);
  167. $user_contribute = Db::name('user')->where('order_id', $order->id)->where('uid', $unionUser->uid)->value('contribute');
  168. $user_contribute = bcadd($user_contribute, $gongxian['number'], 2);
  169. Db::name('user')->where('uid', $unionUser->uid)->update(['contribute'=>$user_contribute]);
  170. }
  171. //同步后返佣
  172. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  173. $order_data = [
  174. 'order_sn' => $order->out_trade_no,
  175. 'order_id' => $order->id,
  176. 'pay_price' => $order->money,
  177. 'uid' => $unionUser->uid,
  178. 'mer_id' => $order->mer_id
  179. ];
  180. $user = Db::name('user')->where('uid', $unionUser->uid)->find();
  181. $OrderMerchantRepository->feidacang_fenyong($user, $order_data, 0);
  182. $OrderMerchantRepository->feidacang_fenyong_mer($user, $order_data, 0);
  183. //锁客
  184. $flag = 1;
  185. $order = (object)$order;
  186. $userRepository = app()->make(UserRepository::class);
  187. $user = $userRepository->getOne(['uid' => $unionUser->uid]);
  188. $OrderMerchantRepository->doBind($user, $order, $flag);
  189. OrderMerchant::getDB()->where('pay_user_id', $unionUser->unionid)->update(['uid' => $unionUser->uid, 'binds' => 1]);
  190. }
  191. } catch (\Throwable $e) {
  192. Log::info($e->getMessage());
  193. }
  194. });
  195. }
  196. }