AllwinUnmarketReward.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 红包
  7. */
  8. namespace app\allwin\model;
  9. use app\allwin\model\AllwinUnmarket;
  10. use app\allwin\model\AllwinUnmarketOrder;
  11. use app\common\facade\WechatPay;
  12. use app\common\model\SystemMemberMiniapp;
  13. use think\Model;
  14. use util\Util;
  15. class AllwinUnmarketReward extends Model{
  16. protected $pk = 'id';
  17. //和主用户表绑定关系
  18. public function user(){
  19. return $this->hasOne('app\common\model\SystemUser','id','uid');
  20. }
  21. //活动主表
  22. public function unmarket(){
  23. return $this->hasOne('AllwinUnmarket','id','union_id');
  24. }
  25. //添加或编辑
  26. public static function edit(array $param){
  27. $rel = self::where(['uid' => $param['uid'],'union_id' => $param['union_id']])->find();
  28. if(isset($rel)){
  29. $rel->money = ['inc',money($param['money'])];
  30. $rel->update_time = time();
  31. return $rel->save();
  32. }else{
  33. $data = [
  34. 'member_miniapp_id' => trim($param['member_miniapp_id']),
  35. 'uid' => trim($param['uid']),
  36. 'union_id' => trim($param['union_id']),
  37. 'money' => trim(money($param['money'])),
  38. 'times' => time(),
  39. ];
  40. return self::insert($data);
  41. }
  42. }
  43. /**红包奖励与付款给客户 */
  44. public static function redRacket($order){
  45. $redpack_amount = 0;
  46. if($order->share_uid > 0){
  47. $market = AllwinUnmarket::where(['id' => $order->share_id])->field('id,uid,member_miniapp_id,title,share_text,price,bonus_types,bonus_user_types,share_ratio,share_ratio_user')->find();
  48. if(!$market->isEmpty()){
  49. $share_ratio = 0;
  50. if($market->bonus_user_types){
  51. //判断奖励用户是否下单
  52. $is_order = AllwinUnmarketOrder::where(['share_id' => $order->share_id,'uid' => $order->share_uid,'state' => 1])->count();
  53. if($market->bonus_user_types == 1){
  54. $share_ratio = $is_order > 0 ? $market->share_ratio_user : 1;
  55. }else{
  56. $share_ratio = $is_order > 0 ? $market->share_ratio_user : $market->share_ratio;
  57. }
  58. }else{
  59. $share_ratio = $market->share_ratio;
  60. }
  61. //开始计算奖励金额
  62. $amount = 0;
  63. if($share_ratio >= 1){
  64. if($market->bonus_types){
  65. $amount = $share_ratio;
  66. }else{
  67. $money = Util::priceCalculate($order['price'],'*',$order->price*($share_ratio/100));
  68. $money = $money >= 1 ? $money : 1;
  69. $num = intval($money/2);
  70. $num = $num > 0 ? $num : 1;
  71. $amount = pocket($money,$num,1);
  72. }
  73. }
  74. if($amount > 0){
  75. $wechat = model('SystemUser')->where(['id' => $order->share_uid])->field('official_uid,invite_code')->find();
  76. if($wechat->isEmpty()){
  77. return $redpack_amount;
  78. }
  79. $web = SystemMemberMiniapp::where(['id' => $market->member_miniapp_id])->find();
  80. $payment = WechatPay::doPay($market->member_miniapp_id,true);
  81. $redpackData = [
  82. 'mch_billno' => $wechat->invite_code.order_no(),
  83. 'send_name' => $web->appname,
  84. 're_openid' => $wechat->official_uid,
  85. 'total_num' => 1,
  86. 'total_amount' => $amount*100,
  87. 'wishing' => '继续~努力完成更多任务',
  88. 'act_name' => $web->appname.'联盟活动',
  89. 'remark' => $market->share_text,
  90. ];
  91. $result = $payment->redpack->sendNormal($redpackData);
  92. if ($result['return_code'] === 'SUCCESS') {
  93. if ($result['result_code'] === 'SUCCESS') {
  94. $redpack_amount = $amount;
  95. self::money($market->member_miniapp_id,$market->id,$order->share_uid,$redpack_amount); //增加钱
  96. }
  97. }
  98. }
  99. }
  100. }
  101. return $redpack_amount;
  102. }
  103. /**
  104. * 应付款增加
  105. * @param integer $miniapp_id
  106. * @param integer $uid
  107. * @param integer $money (元)
  108. * @return void
  109. */
  110. public static function money(int $miniapp_id,int $union_id,int $uid,float $money){
  111. $info = self::where(['uid' => $uid,'union_id' => $union_id])->find();
  112. $money = money($money);
  113. if(empty($info)){
  114. $data['member_miniapp_id'] = $miniapp_id;
  115. $data['union_id'] = $union_id;
  116. $data['uid'] = $uid;
  117. $data['money'] = $money;
  118. $data['number'] = 1;
  119. $data['update_time'] = time();
  120. return self::insert($data);
  121. }else{
  122. $info->number = ['inc',1];
  123. $info->money = ['inc',$money];
  124. $info->update_time = time();
  125. return $info->save();
  126. }
  127. }
  128. }