chunk(5, function ($orders) use ($app) { foreach($orders as $order){ $result = $app->order->queryByOutTradeNumber($order['order_sn']); dump($result); // 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'] : ''; // dump($update_data); // $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']); // } } }); die; } 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(); $first_leader_user = Db::name("user")->where("id", $user['first_leader'])->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) { $send = array( 'user_id' => $v['user_id'], 'mobile' => $first_leader_user['mobile'], 'nickname' => $first_leader_user['nickname'], 'money' => $v['order_price'], ); sendBillionsSubsidySMS($send); } 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) { $send = array( 'user_id' => $v['user_id'], 'mobile' => $first_leader_user['mobile'], 'nickname' => $first_leader_user['nickname'], 'money' => $v['order_price'], ); sendBillionsSubsidySMS($send); } 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) { $send = array( 'user_id' => $v['user_id'], 'mobile' => $first_leader_user['mobile'], 'nickname' => $first_leader_user['nickname'], 'money' => $v['order_price'], ); sendBillionsSubsidySMS($send); } 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) { $send = array( 'user_id' => $v['user_id'], 'mobile' => $first_leader_user['mobile'], 'nickname' => $first_leader_user['nickname'], 'money' => $v['order_price'], ); sendBillionsSubsidySMS($send); } 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) { $send = array( 'user_id' => $v['user_id'], 'mobile' => $first_leader_user['mobile'], 'nickname' => $first_leader_user['nickname'], 'money' => $v['order_price'], ); sendBillionsSubsidySMS($send); } 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) { $send = array( 'user_id' => $v['user_id'], 'mobile' => $first_leader_user['mobile'], 'nickname' => $first_leader_user['nickname'], 'money' => $v['order_price'], ); sendBillionsSubsidySMS($send); } 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() ]); } } } }