=', 1]; } else { $where[] = ['b.state', '=', 0]; } // $list = Billions::alias('b')->where($where) // ->leftjoin('order o', 'o.id = b.order_id') // ->leftjoin('goods g', 'g.id = b.goods_id') // ->leftjoin('shop s', 's.id = b.shop_id') // ->field('b.*, o.order_sn, s.name') // ->page($page,$size)->select(); $list = Billions::alias('b')->where($where) ->page($page,$size)->select(); foreach ($list as &$v) { if ($v['type'] == 1) { $v->order_sn = Order::where('id', $v->order_id)->value('order_sn'); $v->name = Shop::where('id',$v->shop_id)->value('name'); } else if ($v['type'] == 2) { $v->order_sn = RechargeOrder::where('id', $v->order_id)->value('order_sn'); } else if ($v['type'] == 3) { $v->order_sn = UserBillionsRechargeBalance::where('id', $v->order_id)->value('order_sn'); } } $count = Billions::alias('b')->where($where)->count(); // 消费订单笔数 $historyOrder = Billions::where('user_id', $user_id)->count(); // 商家最高补贴 $highSubsidy = Billions::where('user_id', $user_id)->sum('order_price', false); $more = is_more($count, $page, $size); //是否有下一页 $data = [ 'list' => $list, 'page_no' => $page, 'page_size' => $size, 'count' => $count, 'more' => $more, 'historyOrder' => $historyOrder, 'highSubsidy' => $highSubsidy ]; return $data; } // 获取排名 public static function getRanking($billions_id){ $where[] = ['billions_id', '<=', $billions_id]; $where[] = ['state', '=', 0]; return Billions::where($where)->count(); } // 提现进行记录 public static function withdrawRecord($id, $user_id){ Db::startTrans(); try { $billions = Billions::where([ 'billions_id' => $id, 'state' => 0 ])->find(); if ($billions) { Billions::where([ 'billions_id' => $id, 'user_id' => $user_id, ])->update(['state' => 1]); // 查看用户是否有过余额添加记录 $subsidy = SubsidyWithdrawal::where(['user_id' => $user_id])->find(); if ($subsidy) { $subsidy_id = $subsidy->id; SubsidyWithdrawal::where(['user_id' => $user_id])->inc('total_amount', $billions->subsidy_price)->update(); } else { $subsidy_id = SubsidyWithdrawal::insert([ 'user_id' => $user_id, 'total_amount' => $billions->subsidy_price ]); } // 增加余额记录日志 SubsidyWithdrawalRecord::insert([ 'subsidy_withdrawal_id' => $subsidy_id, 'user_id' => $user_id, 'billions_id' => $id, 'type' => 1, 'way' => 0, 'money' => $billions->subsidy_price, 'create_time' => date('Y-m-d H:i:s') ]); } Db::commit(); return true; } catch (Exception $e) { Db::rollback(); return false; } } public static function getSubsidyWithdraw($user_id, $type) { $data['type'] = array(); if ($type == 'weixin') { $data['type'] = array( 0 => [ 'name' => '微信零钱', 'value' => 1, ], ); } else if ($type == 'balance') { $data['type'] = array( 0 => [ 'name' => '东龙汇余额账户', 'value' => 3, ], ); } // $data['type'] = array( // 0 => [ // 'name' => '微信零钱', // 'value' => 1, // ], // 1 => [ // 'name' => '银行卡', // 'value' => 5, // ], // 2 => [ // 'name' => '东龙汇余额账户', // 'value' => 3, // ] // ); // 手续费 $data['commission'] = self::COMMISSION . '%'; // 提现规则 $data['rule'] = '提现规则'; // 最小提现金额 $data['min_withdraw'] = self::MINIMUM_WITHDRAWAL; $subsidy = SubsidyWithdrawal::where(['user_id' => $user_id])->find(); if (empty($subsidy)) { return []; } $data['able_withdraw'] = $subsidy->total_amount; return $data; } public static function balanceapply($user_id, $post) { Db::startTrans(); try { // // 1. 减少余额-实际要等零钱到账之后才能对余额进行操作 // SubsidyWithdrawal::where(['user_id' => $user_id])->dec('total_amount', $post['money'])->update(); // // // 2.先记录到日志下 $commission_money = $post['money'] * (BillionsLogic::COMMISSION / 100); // 需要扣除手续费的金额 $take_money = $post['money'] - ($post['money'] * (BillionsLogic::COMMISSION / 100)); // 实际到手的金额 // SubsidyWithdrawalRecord::insert([ // 'subsidy_withdrawal_id' => 0, // 'user_id' => $user_id, // 'billions_id' => 0, // 'type' => 2, // 'commission_money' => $commission_money, // 'commission' => BillionsLogic::COMMISSION . '%', // 'take_money' => $take_money, // 'way' => $post['type'], // 'money' => $post['money'], // 'create_time' => date('Y-m-d H:i:s') // ]); // 提现到微信零钱 if ($post['type'] == 1) { $withdraw['sn'] = self::createSn(); $withdraw['left_money'] = $take_money; $withdraw['batch_no'] = self::createSn('SJZZ', 7); dump($withdraw); die; // //微信零钱接口: 商家转账到零钱 // WechatMerchantTransferLogic::transfer($withdraw); } die; // 提现到平台余额 if ($post['type'] == 3) { $user = User::find($user_id); $user->user_money = ['inc', $take_money]; $user->save(); return [ 'code' => 1, 'msg' => '提现至钱包余额成功' ]; } Db::commit(); } catch (Exception $e) { Db::rollback(); throw new Exception($e->getMessage()); } } // 创建订单号 private static function createSn($prefix = '', $length = 11) { $suffix = ''; for ($i = 0; $i < $length; $i++) { if (empty($pool)) { $suffix .= rand(0, 9); } else { $suffix .= $pool[array_rand($pool)]; } } return $prefix . date('YmdHis') . $suffix; } public static function applyBillionsBalance($user_id, $post) { $money = $post['money']; $check = self::checkBalance($user_id, $post['money']); if ($check['code'] == -1) { return $check; } $result= Db::name('user_billions_recharge_balance')->insertGetId(array( 'order_sn' => createSn('user_billions_recharge_balance', 'order_sn'), 'user_id' => $user_id, 'recharge_money' => $money, 'recharge_time' => date('Y-m-d H:i:s') )); // 充值到本地余额 $order_sn = createSn('user_billions_recharge_balance', 'order_sn'); $id = Db::name('user_billions_recharge_balance')->insertGetId(array( 'order_sn' => $order_sn, 'user_id' => $user_id, 'recharge_money' => $money, 'recharge_time' => date('Y-m-d H:i:s') )); // 扣除返补的金额 Db::name('subsidy_withdrawal')->where('user_id', $user_id)->dec('total_amount', $money)->update(); // 增加本地余额 Db::name('user')->where('id', $user_id)->inc('user_money', $money)->update(); // 记录到日志 Db::name('recharge_order')->insert([ 'order_sn' => $order_sn, 'order_source' => 1, 'pay_way' => 5, 'pay_status' => 1, 'pay_time' => time(), 'user_id' => $user_id, 'template_id' => 0, 'order_amount' => $money, 'give_money' => 0, 'give_integral' => 0, 'give_growth' => 0, 'create_time' => time() ]); $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'] = $money; // 改为充值金额 $data['user_id'] = $user_id; $data['goods_id'] = 0; // 充值接口不需要该参数 $data['shop_id'] = 0; // 充值接口不需要该参数 $data['goods_num'] = 1; // 充值接口设置为1 $data['order_id'] = $id; $data['dtime'] = date("Y-m-d H:i:s"); $data['type'] = 3; // 类型是充值 $data['subsidy_price'] = round($money * $user_subsidy / 100,5); $res = Db::name("user_billions")->save($data); $billions_msg = Db::name("user_billions")->where("state",0)->select()->toArray(); $subsidy = $money * $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($money * $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_balance, //todo 改为充值类型 'source_id' => $id, //todo 改为充值订单主键 'source_sn' => $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' => $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_balance' => 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($money * $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_balance, //todo 改为充值类型 'source_id' => $id, //todo 改为充值订单主键 'source_sn' => $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' => $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() ]); } } return ['code' => 1, 'msg' => '充值成功']; } private static function checkBalance($user_id, $money) { if ($money <= 0) { return [ 'code' => -1, 'msg' => '提现金额需要大于0' ]; } $able_withdraw = SubsidyWithdrawal::where('user_id', $user_id)->value('total_amount'); // 1.如果提现金额大于余额 if ($money > $able_withdraw) { return [ 'code' => -1, 'msg' => '可提现金额不足' ]; } // 2.最低提现金额 $min_withdraw = BillionsLogic::MINIMUM_WITHDRAWAL; if ($money < $min_withdraw) { return [ 'code' => -1, 'msg' => '最低提现' . $min_withdraw . '元' ]; } // 3.最高提现金额 $max_withdraw = BillionsLogic::MAXIMUM_WITHDRAWAL; if ($money > $max_withdraw) { return [ 'code' => -1, 'msg' => '最高提现' . $max_withdraw . '元' ]; } // 4.如果提现金额在扣除手续费之后,小于最低提现金额则提现失败 // $left_money = $money - ($money * (BillionsLogic::COMMISSION / 100)); // if ($left_money < $min_withdraw) { // return [ // 'code' => -1, // 'msg' => '扣除手续费后提现金额不能小于' . $min_withdraw . '元' // ]; // } return ['code' => 1, 'msg' => '']; } }