diff --git a/app/admin/controller/Login.php b/app/admin/controller/Login.php index 8cf7ee2d..f03ee707 100644 --- a/app/admin/controller/Login.php +++ b/app/admin/controller/Login.php @@ -24,7 +24,15 @@ namespace app\admin\controller; use app\admin\logic\LoginLogic; use app\admin\validate\LoginValidate; use app\common\basics\AdminBase; +use app\common\enum\PayEnum; +use app\common\logic\AccountLogLogic; +use app\common\model\AccountLog; +use app\common\model\billions\Billions; +use app\common\model\user\User; use app\common\server\JsonServer; +use app\common\server\WeChatServer; +use EasyWeChat\Factory; +use app\common\model\RechargeOrder; class Login extends AdminBase { @@ -36,6 +44,48 @@ class Login extends AdminBase */ public function login() { +// $highSubsidy = Billions::where()->sum('order_price', false); +// dump($highSubsidy); +// // 查询订单状态为待支付的订单 +// $config = WeChatServer::getPayConfigBySource(1)['config']; +// $app = Factory::payment($config); +// +// RechargeOrder::where('pay_status', PayEnum::UNPAID)->chunk(5, function ($orders) use ($app) { +// foreach($orders as $order){ +// $result = $app->order->queryByOutTradeNumber($order['order_sn'] . 'JSAPI1'); +//// if (isset($result['trade_state']) && $result['trade_state'] === 'SUCCESS') { +//// $update_data['pay_time'] = time(); +//// $update_data['pay_status'] = PayEnum::ISPAID; +//// $update_data['transaction_id'] = isset($result['transaction_id']) ? $result['transaction_id'] : ''; +//// $order->where('order_sn', $order->order_sn)->update($update_data); +//// +//// $user = User::find($order['user_id']); +//// $total_money = $order['order_amount'] + $order['give_money']; +//// $total_integral = $order['give_integral']; +//// $user->user_money = ['inc', $total_money]; +//// $user->user_integral = ['inc', $total_integral]; +//// $user->user_growth = ['inc', $order['give_growth']]; +//// $user->total_recharge_amount = ['inc', $total_money]; +//// $user->save(); +//// +//// //记录余额 +//// $total_money > 0 && AccountLogLogic::AccountRecord($user->id, $total_money, 1, AccountLog::recharge_money, '', $order['id'], $order['order_sn']); +//// //记录积分 +//// $total_integral > 0 && AccountLogLogic::AccountRecord($user->id, $total_integral, 1, AccountLog::recharge_give_integral); +//// //记录成长值 +//// $order['give_growth'] > 0 && AccountLogLogic::AccountRecord($user->id, $order['give_growth'], 1, AccountLog::recharge_give_growth); +//// } +// } +// }); +// die; +// +// $order = '202407091027463249JSAPI1'; +// $result = $app->order->queryByOutTradeNumber($order); +// if ($result['trade_state'] === 'SUCCESS') { +// } +// dump($config); +// dump($app->order->queryByOutTradeNumber($order)); +// die; if ($this->request->isAjax()) { $post = request()->post(); (new LoginValidate())->goCheck(); diff --git a/app/api/logic/BillionsLogic.php b/app/api/logic/BillionsLogic.php index cce94d5c..132a9fc0 100644 --- a/app/api/logic/BillionsLogic.php +++ b/app/api/logic/BillionsLogic.php @@ -71,6 +71,12 @@ class BillionsLogic extends Logic // 商家最高补贴 $highSubsidy = Billions::where('user_id', $user_id)->sum('order_price', false); + // 已返补到的金额 + $get_subsidy = Billions::where(array( + ['user_id', '=', $user_id], + ['state', '>', 0] + ))->sum('subsidy_price', false); + $more = is_more($count, $page, $size); //是否有下一页 $data = [ diff --git a/app/common/command/WechatMiNiCheckPayOrder.php b/app/common/command/WechatMiNiCheckPayOrder.php new file mode 100644 index 00000000..7ebbcfdc --- /dev/null +++ b/app/common/command/WechatMiNiCheckPayOrder.php @@ -0,0 +1,449 @@ +setName('wechat_mini_check_pay_order') + ->setDescription('微信订单状态查询'); + } + + protected function execute(Input $input, Output $output) + { + $config = WeChatServer::getPayConfigBySource(1)['config']; + $app = Factory::payment($config); + + RechargeOrder::where('pay_status', PayEnum::UNPAID)->chunk(5, function ($orders) use ($app) { + foreach($orders as $order){ + Log::write('zhifu:' .$order['order_sn']); +// $result = $app->order->queryByOutTradeNumber($order['order_sn'] . $this->prefix); +// if (isset($result['trade_state']) && $result['trade_state'] === 'SUCCESS') { +// $update_data['pay_time'] = time(); +// $update_data['pay_status'] = PayEnum::ISPAID; +// $update_data['transaction_id'] = isset($result['transaction_id']) ? $result['transaction_id'] : ''; +// $order->where('order_sn', $order->order_sn)->update($update_data); +// +// $user = User::find($order['user_id']); +// $total_money = $order['order_amount'] + $order['give_money']; +// $total_integral = $order['give_integral']; +// $user->user_money = ['inc', $total_money]; +// $user->user_integral = ['inc', $total_integral]; +// $user->user_growth = ['inc', $order['give_growth']]; +// $user->total_recharge_amount = ['inc', $total_money]; +// $user->save(); +// +// //记录余额 +// $total_money > 0 && AccountLogLogic::AccountRecord($user->id, $total_money, 1, AccountLog::recharge_money, '', $order['id'], $order['order_sn']); +// //记录积分 +// $total_integral > 0 && AccountLogLogic::AccountRecord($user->id, $total_integral, 1, AccountLog::recharge_give_integral); +// //记录成长值 +// $order['give_growth'] > 0 && AccountLogLogic::AccountRecord($user->id, $order['give_growth'], 1, AccountLog::recharge_give_growth); +// +// // 余额充值参与百亿补贴 +// self::recharge_billions($order, $order['user_id']); +// } + } + }); + } + + private static function recharge_billions($order, $user_id) + { + $user_subsidy = 2; // 自我补贴 + $all_user_subsidy = 20; //全用户获取的比例 + $user_start_num = 5; // 补贴前几位数量 + $user_start_subsidy = 30; //补贴前几位比例 全用户比例中扣除 + $user_end_subsidy = 70; //补贴剩余部分比例 + $user_first_leader = 30; // 推广者排队补贴 + $first_leader = 5; //分销推广者补贴 + $store_subsidy = 5; // 店铺推广者补贴 如没有补贴给店铺拥有者 + $donglonghui = 5; //东龙汇服务费 + $other_leader = 3; //分公司收入 如没有则并入东龙汇 + $shop_com = 60; // 商家获取收入 + $user = Db::name("user")->where(['id' => $user_id])->find(); + $is_d = Distribution::where('user_id', $user['first_leader'] )->where('is_distribution', 1 )->find(); + $data['order_price'] = $order['order_amount']; // 改为充值金额 + $data['user_id'] = $user_id; + $data['goods_id'] = 0; // 充值接口不需要该参数 + $data['shop_id'] = 0; // 充值接口不需要该参数 + $data['goods_num'] = 1; // 充值接口设置为1 + $data['order_id'] = $order['id']; + $data['dtime'] = date("Y-m-d H:i:s"); + $data['type'] = 2; // 类型是充值 + $data['subsidy_price'] = round($order['order_amount'] * $user_subsidy / 100,5); + $res = Db::name("user_billions")->save($data); + $billions_msg = Db::name("user_billions")->where("state",0)->select()->toArray(); + $subsidy = $order['order_amount'] * $all_user_subsidy / 100; + + if($is_d != null){ + $ufl = round($subsidy*$user_first_leader/100,5); + $subsidy_ufl = round($subsidy - $ufl,5); + $first_billions_msg = Db::name("user_billions")->where("user_id",$user['first_leader'])->where("state",0)->select()->toArray(); + if(count($first_billions_msg) > 0){ + foreach($first_billions_msg as $k=>$v){ + if($ufl > 0){ + if($v['order_price'] <= $ufl+$v['subsidy_price']){ + // 查询用户总余额账户 + $first_user_all_account = Db::name("subsidy_withdrawal")->where("user_id",$v['user_id'])->find(); + if($first_user_all_account != null){ + // 判断账户是否可用 + if($first_user_all_account['state'] == 0 || $first_user_all_account['state'] == 2){ + return -2; + } + // 更新主表 + $first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->where("user_id",$v['user_id'])->inc("total_amount",$v['order_price'])->update(); + if(!$first_update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $first_user_all_account['id']; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $first_save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$first_save_record){ + return -5; + } + }else{ + $d['user_id'] = $v['user_id']; + $d['total_amount'] = $v['order_price']; + $first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d); + if(!$first_update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $first_update_subsidy_withdrawal; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $first_save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$first_save_record){ + return -5; + } + } + // 更新排队状态 + $first_update_user_billions = Db::name("user_billions")->where("billions_id",$v['billions_id'])->update(['subsidy_price'=>$v['order_price'],"state"=>2]); + if(!$first_update_user_billions){ + return -3; + } + // 剩余金额 + $ufl = round($ufl+$v['subsidy_price']-$v['order_price'],5); + }else{ + Db::name("user_billions")->where("billions_id",$v['billions_id'])->inc("subsidy_price",$ufl)->update(); + $ufl = 0; + } + } + + } + $subsidy = round($subsidy_ufl + $ufl,5); + } + } +// $first_msg = Db::name("user_billions")->where("user_id",$user['first_leader'])->where("state",0)->select()->toArray(); + $count = count($billions_msg); + $arr = $arr1 = array(); + + // 数量超出前几位补贴用户 + if($count > $user_start_num) { + $start_subsidy = $subsidy * $user_start_subsidy / 100; + $average = round($start_subsidy / $user_start_num, 5); + $last = round($start_subsidy - $average * ($user_start_num - 1), 5); + for ($i = 0; $i < $user_start_num; $i++) { + if ($i + 1 == $user_start_num) { + $arr[$i] = $last; + } else { + $arr[$i] = $average; + } + } + // 剩余人数补贴 + $end_average = round(($subsidy - $start_subsidy)/ ($count-$user_start_num), 5); + $end_last = round($subsidy - $start_subsidy - $end_average * ($count-$user_start_num - 1), 5); + for ($y = 0; $y < $count-$user_start_num; $y++) { + if ($y + 1 == $count-$user_start_num) { + $arr1[$y] = $end_average; + } else { + $arr1[$y] = $end_last; + } + } + }else{ + $user_start_num = $count; + // 数量不超出前几位补贴用户,按全部金额计算 + $average = round($subsidy/$count, 5); + $last = round($subsidy - $average * ($count - 1), 5); + for ($i = 0; $i < $count; $i++) { + if ($i + 1 == $count) { + $arr[$i] = $last; + } else { + $arr[$i] = $average; + } + } + } + foreach($billions_msg as $k=>$v){ + if($k + 1 <= $user_start_num){ + // var_dump($v['order_price']); + // var_dump($arr[$k]); + // var_dump($v['subsidy_price']); + if($v['order_price'] <= $arr[$k] + $v['subsidy_price']){ + // 查询用户总余额账户 + $user_all_account = Db::name("subsidy_withdrawal")->where("user_id", $v['user_id'])->find(); + if($user_all_account != null){ + // 判断账户是否可用 + if($user_all_account['state'] == 0 || $user_all_account['state'] == 2){ + return -2; + } + // 更新主表 + $update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->where("user_id", $v['user_id'])->inc("total_amount", $v['order_price'])->update(); + if(!$update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $user_all_account['id']; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$save_record){ + return -5; + } + }else{ + $d['user_id'] = $v['user_id']; + $d['total_amount'] = $v['order_price']; + $update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d); + if(!$update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $update_subsidy_withdrawal; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['is_recharge'] = 1; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$save_record){ + return -5; + } + } + + // 更新排队状态 + $update_user_billions = Db::name("user_billions")->where("billions_id", $v['billions_id'])->update(['subsidy_price' => $v['order_price'], "state" => 2]); + if(!$update_user_billions){ + return -3; + } + + // 剩余数量 + $residue_count = $user_start_num - $k - 1; + // 剩余金额 + $residue_price = round($arr[$k]+$v['subsidy_price']-$v['order_price'],5); + foreach($arr as $k1=>$v1){ + if($k1 > $k){ + $residue_price+=$arr[$k1]; + } + } + $average = round($residue_price/$residue_count,5); + $last = round($residue_price - $average*($residue_count-1),5); + foreach($arr as $k1=>$v1){ + if($k1+1 == $user_start_num){ + $arr[$k1] = $last; + }else{ + if($k1 > $k){ + $arr[$k1] = $average; + } + } + } + + }else{ + Db::name("user_billions")->where("billions_id", $v['billions_id'])->inc("subsidy_price", $arr[$k])->update(); + } + }else{ + if($v['order_price'] <= $arr1[$k - $user_start_num] + $v['subsidy_price']){ + // 查询用户总余额账户 + $user_all_account = Db::name("subsidy_withdrawal")->where("user_id", $v['user_id'])->find(); + if($user_all_account == null){ + $d['user_id'] = $v['user_id']; + $d['total_amount'] = $v['order_price']; + $update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d); + if(!$update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $update_subsidy_withdrawal; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['is_recharge'] = 1; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$save_record){ + return -5; + } + }else{ + // 判断账户是否可用 + if($user_all_account['state'] == 0 || $user_all_account['state'] == 2){ + return -2; + } + + // 更新主表 + $update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->where("user_id",$v['user_id'])->inc("total_amount",$v['order_price'])->update(); + if(!$update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $user_all_account['id']; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['is_recharge'] = 1; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$save_record){ + return -5; + } + } + // 更新排队状态 + $update_user_billions = Db::name("user_billions")->where("billions_id",$v['billions_id'])->update(['subsidy_price'=>$v['order_price'],"state"=>2]); + if(!$update_user_billions){ + return -3; + } + + // 剩余数量 + $residue_count = $count-$user_start_num - $k+$user_start_num - 1; + // 剩余金额 + $residue_price = round($arr1[$k-$user_start_num]+$v['subsidy_price']-$v['order_price'],5); + foreach($arr1 as $k1=>$v1){ + if($k1 > $k - $user_start_num){ + $residue_price+=$arr1[$k1]; + } + } + $average = round($residue_price/$residue_count,5); + $last = round($residue_price - $average*($residue_count-1),5); + foreach($arr1 as $k1=>$v1){ + if($k1+1 == $count-$user_start_num){ + $arr1[$k1] = $last; + }else{ + if($k1 > $k){ + $arr[$k1] = $average; + } + } + } + }else{ + Db::name("user_billions")->where("billions_id",$v['billions_id'])->inc("subsidy_price",$arr1[$k-$user_start_num])->update(); + } + } + } + if($is_d!=null){ + if($user['first_leader']>0){ + $first_leader_percent = round($order['order_amount'] * $first_leader / 100, 5); +// Order::where('id', $order['id']) +// ->inc('distribution_money',$first_leader_percent)->update(); + UserDistribution::where('user_id', $user['first_leader']) + ->inc('order_amount', $first_leader_percent)->update(); + UserDistribution::where('user_id', $user['first_leader']) + ->inc('order_num')->update(); + User::where('id', $user['first_leader']) + ->inc('earnings', $first_leader_percent)->update(); + $earnings_buy = AccountLog::where('user_id', $user['first_leader'])->order("create_time desc")->limit(1)->value('left_amount'); + $sn = createSn('account_log', 'log_sn', '', 4); + AccountLog::create([ + 'log_sn' => $sn, + 'user_id' => $user['first_leader'], + 'source_type' => AccountLog::recharge_billions_inc_earnings, //todo 改为充值类型 + 'source_id' => $order['id'], //todo 改为充值订单主键 + 'source_sn' => $order['order_sn'], // todo 改为充值订单编号 + 'change_amount' => $first_leader_percent, + 'left_amount' => $earnings_buy + $first_leader_percent, + 'change_type' => 1, + 'remark' => '百亿返补佣金增加' + ]); + DistributionOrderGoods::create([ + 'sn' => $sn, + 'user_id' => $user['first_leader'], + 'ratio' => $first_leader, + 'order_id' => $order['id'], //todo 改为充值订单主键 + 'order_goods_id' => 0, //todo 因为是充值所以改为0 + 'goods_num' => 1, // todo 因为是充值所以改为1 + 'money' => $first_leader_percent, + 'status' => 2, + 'create_time' => time(), + 'shop_id' => 0, // todo 因为是充值所以改为默认0 + 'is_recharge' => 1, // todo 因为是充值所以改为1 + 'settlement_time' => time() + ]); + } + + $role_user_msg = Db::name("user")->where("id",$user['divide_role_user_id'])->find(); + $state = 0; + if($role_user_msg != null){ + $state = $role_user_msg['divide_state']; + } + // 3%对公司 + if($user['divide_role_user_id'] > 0&&$state>0 ){ + $divide_percent = round($order['order_amount'] * $other_leader / 100, 5); +// Order::where('id', $order['id']) +// ->inc('distribution_money',$divide_percent)->update(); +// $shopDistribution_msg = UserShopDistribution::where('shop_id', $value['shop_id']) +// ->find(); + User::where('id', $user['divide_role_user_id']) + ->inc('earnings', $divide_percent)->update(); + $earnings_buy = AccountLog::where('user_id', $user['divide_role_user_id'])->order("create_time desc")->limit(1)->value('left_amount'); + $sn = createSn('account_log', 'log_sn', '', 4); + AccountLog::create([ + 'log_sn' => $sn, + 'user_id' => $user['divide_role_user_id'], + 'source_type' => AccountLog::recharge_billions_inc_earnings, //todo 改为充值类型 + 'source_id' => $order['id'], //todo 改为充值订单主键 + 'source_sn' => $order['order_sn'], // todo 改为充值订单编号 + 'change_amount' => $divide_percent, + 'left_amount' => $earnings_buy + $divide_percent, + 'change_type' => 1, + 'remark' => '充值百亿返补佣金增加' + ]); + DistributionOrderGoods::create([ + 'sn' => $sn, + 'user_id' => $user['divide_role_user_id'], + 'ratio' => $other_leader, + 'order_id' => $order['id'], //todo 改为充值订单主键 + 'order_goods_id' => 0, //todo 因为是充值所以改为0 + 'goods_num' => 1, // todo 因为是充值所以改为1 + 'money' => $divide_percent, + 'status' => 2, + 'create_time' => time(), + 'shop_id' => 0, // todo 因为是充值所以改为默认0 + 'is_recharge' => 1, // todo 因为是充值所以改为1 + 'settlement_time' => time() + ]); + } + } + } +} \ No newline at end of file diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php index 815b4a33..7fad8edd 100644 --- a/app/common/logic/PayNotifyLogic.php +++ b/app/common/logic/PayNotifyLogic.php @@ -332,32 +332,35 @@ class PayNotifyLogic */ private static function recharge($order_sn, $extra = []) { + /* 会有定时任务检索订单状态,所以在时间上可能会前后不一致,因此需要做个过滤 */ $new = time(); $recharge_order = new RechargeOrder(); $order = $recharge_order->where(['order_sn' => $order_sn])->find(); - $update_data['pay_time'] = $new; - $update_data['pay_status'] = PayEnum::ISPAID; - if (isset($extra['transaction_id'])) { - $update_data['transaction_id'] = $extra['transaction_id']; - } - $recharge_order->where(['id' => $order['id']])->update($update_data); - $user = User::find($order['user_id']); - $total_money = $order['order_amount'] + $order['give_money']; - $total_integral = $order['give_integral']; - $user->user_money = ['inc', $total_money]; - $user->user_integral = ['inc', $total_integral]; - $user->user_growth = ['inc', $order['give_growth']]; - $user->total_recharge_amount = ['inc', $total_money]; - $user->save(); - //记录余额 - $total_money > 0 && AccountLogLogic::AccountRecord($user->id, $total_money, 1, AccountLog::recharge_money, '', $order['id'], $order_sn); - //记录积分 - $total_integral > 0 && AccountLogLogic::AccountRecord($user->id, $total_integral, 1, AccountLog::recharge_give_integral); - //记录成长值 - $order['give_growth'] > 0 && AccountLogLogic::AccountRecord($user->id, $order['give_growth'], 1, AccountLog::recharge_give_growth); + if ($order['pay_status'] != PayEnum::ISPAID) { + $update_data['pay_time'] = $new; + $update_data['pay_status'] = PayEnum::ISPAID; + if (isset($extra['transaction_id'])) { + $update_data['transaction_id'] = $extra['transaction_id']; + } + $recharge_order->where(['id' => $order['id']])->update($update_data); + $user = User::find($order['user_id']); + $total_money = $order['order_amount'] + $order['give_money']; + $total_integral = $order['give_integral']; + $user->user_money = ['inc', $total_money]; + $user->user_integral = ['inc', $total_integral]; + $user->user_growth = ['inc', $order['give_growth']]; + $user->total_recharge_amount = ['inc', $total_money]; + $user->save(); + //记录余额 + $total_money > 0 && AccountLogLogic::AccountRecord($user->id, $total_money, 1, AccountLog::recharge_money, '', $order['id'], $order_sn); + //记录积分 + $total_integral > 0 && AccountLogLogic::AccountRecord($user->id, $total_integral, 1, AccountLog::recharge_give_integral); + //记录成长值 + $order['give_growth'] > 0 && AccountLogLogic::AccountRecord($user->id, $order['give_growth'], 1, AccountLog::recharge_give_growth); - // 余额充值参与百亿补贴 - self::recharge_billions($order, $order['user_id']); + // 余额充值参与百亿补贴 + self::recharge_billions($order, $order['user_id']); + } } private static function recharge_billions($order, $user_id) @@ -789,7 +792,6 @@ class PayNotifyLogic '', $order['id'], $order['order_sn'] ); } - } diff --git a/app/common/server/WeChatServer.php b/app/common/server/WeChatServer.php index 14b69c8d..5cd0d8f7 100644 --- a/app/common/server/WeChatServer.php +++ b/app/common/server/WeChatServer.php @@ -118,6 +118,7 @@ class WeChatServer switch ($order_source) { case Client_::mnp: $notify_url = self::getUrl('pay/notifyMnp'); + dump($notify_url);die; break; case Client_::oa: case Client_::h5: diff --git a/app/index/controller/WechatMiNiCheckPayOrder.php b/app/index/controller/WechatMiNiCheckPayOrder.php new file mode 100644 index 00000000..f066777d --- /dev/null +++ b/app/index/controller/WechatMiNiCheckPayOrder.php @@ -0,0 +1,437 @@ +chunk(5, function ($orders) use ($app) { + foreach($orders as $order){ + $result = $app->order->queryByOutTradeNumber($order['order_sn'] . $this->prefix); + if (isset($result['trade_state']) && $result['trade_state'] === 'SUCCESS') { + $update_data['pay_time'] = time(); + $update_data['pay_status'] = PayEnum::ISPAID; + $update_data['transaction_id'] = isset($result['transaction_id']) ? $result['transaction_id'] : ''; + $order->where('order_sn', $order->order_sn)->update($update_data); + + $user = User::find($order['user_id']); + $total_money = $order['order_amount'] + $order['give_money']; + $total_integral = $order['give_integral']; + $user->user_money = ['inc', $total_money]; + $user->user_integral = ['inc', $total_integral]; + $user->user_growth = ['inc', $order['give_growth']]; + $user->total_recharge_amount = ['inc', $total_money]; + $user->save(); + + //记录余额 + $total_money > 0 && AccountLogLogic::AccountRecord($user->id, $total_money, 1, AccountLog::recharge_money, '', $order['id'], $order['order_sn']); + //记录积分 + $total_integral > 0 && AccountLogLogic::AccountRecord($user->id, $total_integral, 1, AccountLog::recharge_give_integral); + //记录成长值 + $order['give_growth'] > 0 && AccountLogLogic::AccountRecord($user->id, $order['give_growth'], 1, AccountLog::recharge_give_growth); + + // 余额充值参与百亿补贴 + self::recharge_billions($order, $order['user_id']); + } + } + }); + } + + private static function recharge_billions($order, $user_id) + { + $user_subsidy = 2; // 自我补贴 + $all_user_subsidy = 20; //全用户获取的比例 + $user_start_num = 5; // 补贴前几位数量 + $user_start_subsidy = 30; //补贴前几位比例 全用户比例中扣除 + $user_end_subsidy = 70; //补贴剩余部分比例 + $user_first_leader = 30; // 推广者排队补贴 + $first_leader = 5; //分销推广者补贴 + $store_subsidy = 5; // 店铺推广者补贴 如没有补贴给店铺拥有者 + $donglonghui = 5; //东龙汇服务费 + $other_leader = 3; //分公司收入 如没有则并入东龙汇 + $shop_com = 60; // 商家获取收入 + $user = Db::name("user")->where(['id' => $user_id])->find(); + $is_d = Distribution::where('user_id', $user['first_leader'] )->where('is_distribution', 1 )->find(); + $data['order_price'] = $order['order_amount']; // 改为充值金额 + $data['user_id'] = $user_id; + $data['goods_id'] = 0; // 充值接口不需要该参数 + $data['shop_id'] = 0; // 充值接口不需要该参数 + $data['goods_num'] = 1; // 充值接口设置为1 + $data['order_id'] = $order['id']; + $data['dtime'] = date("Y-m-d H:i:s"); + $data['type'] = 2; // 类型是充值 + $data['subsidy_price'] = round($order['order_amount'] * $user_subsidy / 100,5); + $res = Db::name("user_billions")->save($data); + $billions_msg = Db::name("user_billions")->where("state",0)->select()->toArray(); + $subsidy = $order['order_amount'] * $all_user_subsidy / 100; + + if($is_d != null){ + $ufl = round($subsidy*$user_first_leader/100,5); + $subsidy_ufl = round($subsidy - $ufl,5); + $first_billions_msg = Db::name("user_billions")->where("user_id",$user['first_leader'])->where("state",0)->select()->toArray(); + if(count($first_billions_msg) > 0){ + foreach($first_billions_msg as $k=>$v){ + if($ufl > 0){ + if($v['order_price'] <= $ufl+$v['subsidy_price']){ + // 查询用户总余额账户 + $first_user_all_account = Db::name("subsidy_withdrawal")->where("user_id",$v['user_id'])->find(); + if($first_user_all_account != null){ + // 判断账户是否可用 + if($first_user_all_account['state'] == 0 || $first_user_all_account['state'] == 2){ + return -2; + } + // 更新主表 + $first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->where("user_id",$v['user_id'])->inc("total_amount",$v['order_price'])->update(); + if(!$first_update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $first_user_all_account['id']; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $first_save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$first_save_record){ + return -5; + } + }else{ + $d['user_id'] = $v['user_id']; + $d['total_amount'] = $v['order_price']; + $first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d); + if(!$first_update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $first_update_subsidy_withdrawal; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $first_save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$first_save_record){ + return -5; + } + } + // 更新排队状态 + $first_update_user_billions = Db::name("user_billions")->where("billions_id",$v['billions_id'])->update(['subsidy_price'=>$v['order_price'],"state"=>2]); + if(!$first_update_user_billions){ + return -3; + } + // 剩余金额 + $ufl = round($ufl+$v['subsidy_price']-$v['order_price'],5); + }else{ + Db::name("user_billions")->where("billions_id",$v['billions_id'])->inc("subsidy_price",$ufl)->update(); + $ufl = 0; + } + } + + } + $subsidy = round($subsidy_ufl + $ufl,5); + } + } +// $first_msg = Db::name("user_billions")->where("user_id",$user['first_leader'])->where("state",0)->select()->toArray(); + $count = count($billions_msg); + $arr = $arr1 = array(); + + // 数量超出前几位补贴用户 + if($count > $user_start_num) { + $start_subsidy = $subsidy * $user_start_subsidy / 100; + $average = round($start_subsidy / $user_start_num, 5); + $last = round($start_subsidy - $average * ($user_start_num - 1), 5); + for ($i = 0; $i < $user_start_num; $i++) { + if ($i + 1 == $user_start_num) { + $arr[$i] = $last; + } else { + $arr[$i] = $average; + } + } + // 剩余人数补贴 + $end_average = round(($subsidy - $start_subsidy)/ ($count-$user_start_num), 5); + $end_last = round($subsidy - $start_subsidy - $end_average * ($count-$user_start_num - 1), 5); + for ($y = 0; $y < $count-$user_start_num; $y++) { + if ($y + 1 == $count-$user_start_num) { + $arr1[$y] = $end_average; + } else { + $arr1[$y] = $end_last; + } + } + }else{ + $user_start_num = $count; + // 数量不超出前几位补贴用户,按全部金额计算 + $average = round($subsidy/$count, 5); + $last = round($subsidy - $average * ($count - 1), 5); + for ($i = 0; $i < $count; $i++) { + if ($i + 1 == $count) { + $arr[$i] = $last; + } else { + $arr[$i] = $average; + } + } + } + foreach($billions_msg as $k=>$v){ + if($k + 1 <= $user_start_num){ + // var_dump($v['order_price']); + // var_dump($arr[$k]); + // var_dump($v['subsidy_price']); + if($v['order_price'] <= $arr[$k] + $v['subsidy_price']){ + // 查询用户总余额账户 + $user_all_account = Db::name("subsidy_withdrawal")->where("user_id", $v['user_id'])->find(); + if($user_all_account != null){ + // 判断账户是否可用 + if($user_all_account['state'] == 0 || $user_all_account['state'] == 2){ + return -2; + } + // 更新主表 + $update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->where("user_id", $v['user_id'])->inc("total_amount", $v['order_price'])->update(); + if(!$update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $user_all_account['id']; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$save_record){ + return -5; + } + }else{ + $d['user_id'] = $v['user_id']; + $d['total_amount'] = $v['order_price']; + $update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d); + if(!$update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $update_subsidy_withdrawal; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['is_recharge'] = 1; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$save_record){ + return -5; + } + } + + // 更新排队状态 + $update_user_billions = Db::name("user_billions")->where("billions_id", $v['billions_id'])->update(['subsidy_price' => $v['order_price'], "state" => 2]); + if(!$update_user_billions){ + return -3; + } + + // 剩余数量 + $residue_count = $user_start_num - $k - 1; + // 剩余金额 + $residue_price = round($arr[$k]+$v['subsidy_price']-$v['order_price'],5); + foreach($arr as $k1=>$v1){ + if($k1 > $k){ + $residue_price+=$arr[$k1]; + } + } + $average = round($residue_price/$residue_count,5); + $last = round($residue_price - $average*($residue_count-1),5); + foreach($arr as $k1=>$v1){ + if($k1+1 == $user_start_num){ + $arr[$k1] = $last; + }else{ + if($k1 > $k){ + $arr[$k1] = $average; + } + } + } + + }else{ + Db::name("user_billions")->where("billions_id", $v['billions_id'])->inc("subsidy_price", $arr[$k])->update(); + } + }else{ + if($v['order_price'] <= $arr1[$k - $user_start_num] + $v['subsidy_price']){ + // 查询用户总余额账户 + $user_all_account = Db::name("subsidy_withdrawal")->where("user_id", $v['user_id'])->find(); + if($user_all_account == null){ + $d['user_id'] = $v['user_id']; + $d['total_amount'] = $v['order_price']; + $update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d); + if(!$update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $update_subsidy_withdrawal; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['is_recharge'] = 1; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$save_record){ + return -5; + } + }else{ + // 判断账户是否可用 + if($user_all_account['state'] == 0 || $user_all_account['state'] == 2){ + return -2; + } + + // 更新主表 + $update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->where("user_id",$v['user_id'])->inc("total_amount",$v['order_price'])->update(); + if(!$update_subsidy_withdrawal){ + return -4; + } + $dat['subsidy_withdrawal_id'] = $user_all_account['id']; + $dat['user_id'] = $v['user_id']; + $dat['billions_id'] = $v['billions_id']; + $dat['type'] = 1; + $dat['money'] = $v['order_price']; + $dat['is_recharge'] = 1; + $dat['create_time'] = date("Y-m-d H:i:s"); + // 插入提现记录 + $save_record = Db::name("subsidy_withdrawal_record")->save($dat); + if(!$save_record){ + return -5; + } + } + // 更新排队状态 + $update_user_billions = Db::name("user_billions")->where("billions_id",$v['billions_id'])->update(['subsidy_price'=>$v['order_price'],"state"=>2]); + if(!$update_user_billions){ + return -3; + } + + // 剩余数量 + $residue_count = $count-$user_start_num - $k+$user_start_num - 1; + // 剩余金额 + $residue_price = round($arr1[$k-$user_start_num]+$v['subsidy_price']-$v['order_price'],5); + foreach($arr1 as $k1=>$v1){ + if($k1 > $k - $user_start_num){ + $residue_price+=$arr1[$k1]; + } + } + $average = round($residue_price/$residue_count,5); + $last = round($residue_price - $average*($residue_count-1),5); + foreach($arr1 as $k1=>$v1){ + if($k1+1 == $count-$user_start_num){ + $arr1[$k1] = $last; + }else{ + if($k1 > $k){ + $arr[$k1] = $average; + } + } + } + }else{ + Db::name("user_billions")->where("billions_id",$v['billions_id'])->inc("subsidy_price",$arr1[$k-$user_start_num])->update(); + } + } + } + if($is_d!=null){ + if($user['first_leader']>0){ + $first_leader_percent = round($order['order_amount'] * $first_leader / 100, 5); +// Order::where('id', $order['id']) +// ->inc('distribution_money',$first_leader_percent)->update(); + UserDistribution::where('user_id', $user['first_leader']) + ->inc('order_amount', $first_leader_percent)->update(); + UserDistribution::where('user_id', $user['first_leader']) + ->inc('order_num')->update(); + User::where('id', $user['first_leader']) + ->inc('earnings', $first_leader_percent)->update(); + $earnings_buy = AccountLog::where('user_id', $user['first_leader'])->order("create_time desc")->limit(1)->value('left_amount'); + $sn = createSn('account_log', 'log_sn', '', 4); + AccountLog::create([ + 'log_sn' => $sn, + 'user_id' => $user['first_leader'], + 'source_type' => AccountLog::recharge_billions_inc_earnings, //todo 改为充值类型 + 'source_id' => $order['id'], //todo 改为充值订单主键 + 'source_sn' => $order['order_sn'], // todo 改为充值订单编号 + 'change_amount' => $first_leader_percent, + 'left_amount' => $earnings_buy + $first_leader_percent, + 'change_type' => 1, + 'remark' => '百亿返补佣金增加' + ]); + DistributionOrderGoods::create([ + 'sn' => $sn, + 'user_id' => $user['first_leader'], + 'ratio' => $first_leader, + 'order_id' => $order['id'], //todo 改为充值订单主键 + 'order_goods_id' => 0, //todo 因为是充值所以改为0 + 'goods_num' => 1, // todo 因为是充值所以改为1 + 'money' => $first_leader_percent, + 'status' => 2, + 'create_time' => time(), + 'shop_id' => 0, // todo 因为是充值所以改为默认0 + 'is_recharge' => 1, // todo 因为是充值所以改为1 + 'settlement_time' => time() + ]); + } + + $role_user_msg = Db::name("user")->where("id",$user['divide_role_user_id'])->find(); + $state = 0; + if($role_user_msg != null){ + $state = $role_user_msg['divide_state']; + } + // 3%对公司 + if($user['divide_role_user_id'] > 0&&$state>0 ){ + $divide_percent = round($order['order_amount'] * $other_leader / 100, 5); +// Order::where('id', $order['id']) +// ->inc('distribution_money',$divide_percent)->update(); +// $shopDistribution_msg = UserShopDistribution::where('shop_id', $value['shop_id']) +// ->find(); + User::where('id', $user['divide_role_user_id']) + ->inc('earnings', $divide_percent)->update(); + $earnings_buy = AccountLog::where('user_id', $user['divide_role_user_id'])->order("create_time desc")->limit(1)->value('left_amount'); + $sn = createSn('account_log', 'log_sn', '', 4); + AccountLog::create([ + 'log_sn' => $sn, + 'user_id' => $user['divide_role_user_id'], + 'source_type' => AccountLog::recharge_billions_inc_earnings, //todo 改为充值类型 + 'source_id' => $order['id'], //todo 改为充值订单主键 + 'source_sn' => $order['order_sn'], // todo 改为充值订单编号 + 'change_amount' => $divide_percent, + 'left_amount' => $earnings_buy + $divide_percent, + 'change_type' => 1, + 'remark' => '充值百亿返补佣金增加' + ]); + DistributionOrderGoods::create([ + 'sn' => $sn, + 'user_id' => $user['divide_role_user_id'], + 'ratio' => $other_leader, + 'order_id' => $order['id'], //todo 改为充值订单主键 + 'order_goods_id' => 0, //todo 因为是充值所以改为0 + 'goods_num' => 1, // todo 因为是充值所以改为1 + 'money' => $divide_percent, + 'status' => 2, + 'create_time' => time(), + 'shop_id' => 0, // todo 因为是充值所以改为默认0 + 'is_recharge' => 1, // todo 因为是充值所以改为1 + 'settlement_time' => time() + ]); + } + } + } +} \ No newline at end of file diff --git a/config/console.php b/config/console.php index 84eb38be..ec61b1a1 100644 --- a/config/console.php +++ b/config/console.php @@ -17,5 +17,6 @@ return [ 'wechat_merchant_transfer' => 'app\common\command\WechatMerchantTransfer', //商家转账到零钱查询 'wechat_live_room' => 'app\common\command\WechatLiveRoom', //更新直播间状态 'wechat_live_goods' => 'app\common\command\WechatLiveGoods', //更新直播商品状态 + 'wechat_mini_check_pay_order' => 'app\common\command\WechatMiNiCheckPayOrder', //更新微信支付订单状态 ], ];