添加百亿返补到充值余额

This commit is contained in:
wangxiaowei 2024-07-09 16:55:59 +08:00
parent 359a6fb86d
commit 6331e4288e
5 changed files with 471 additions and 26 deletions

@ -39,7 +39,8 @@ class Billions extends Api
*/
public function config()
{
$data = BillionsLogic::getSubsidyWithdraw($this->user_id);
$type = $this->request->get('type');
$data = BillionsLogic::getSubsidyWithdraw($this->user_id, $type);
return JsonServer::success('', $data);
}
@ -57,4 +58,16 @@ class Billions extends Api
// $id = WithdrawLogic::balanceapply($this->user_id, $post);
// return JsonServer::success('申请成功', ['id' => $id]);
}
/**
* @notes 百亿返补充值到余额
*/
public function rechargeBalance()
{
$post = $this->request->post();
$result = BillionsLogic::applyBillionsBalance($this->user_id, $post);
if ($result['code'] == -1) {
return JsonServer::error('操作失败');
}
}
}

@ -10,9 +10,16 @@ use app\common\logic\AccountLogLogic;
use app\common\model\AccountLog;
use app\common\model\BalanceWithdrawApply;
use app\common\model\billions\Billions;
use app\common\model\distribution\Distribution;
use app\common\model\distribution\DistributionOrderGoods;
use app\common\model\subsidy_withdrawal\SubsidyWithdrawal;
use app\common\model\subsidy_withdrawal\SubsidyWithdrawalRecord;
use app\common\model\user\User;
use app\common\model\order\Order;
use app\common\model\RechargeOrder;
use app\common\model\shop\Shop;
use app\common\model\user\UserDistribution;
use app\common\model\UserBillionsRechargeBalance;
use app\common\model\WithdrawApply;
use app\common\server\ConfigServer;
use think\Exception;
@ -34,19 +41,28 @@ class BillionsLogic extends Logic
$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)
->join('order o', 'o.id = b.order_id')
->join('goods g', 'g.id = b.goods_id')
->join('shop s', 's.id = b.shop_id')
->field('b.*, o.order_sn, s.name')
->page($page,$size)->select();
$count = Billions::alias('b')->where($where)
->alias('b')
->join('order o', 'o.id = b.order_id')
->join('goods g', 'g.id = b.goods_id')
->join('shop s', 's.id = b.shop_id')
->count();
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();
@ -121,22 +137,38 @@ class BillionsLogic extends Logic
}
}
public static function getSubsidyWithdraw($user_id)
public static function getSubsidyWithdraw($user_id, $type)
{
$data['type'] = array(
0 => [
'name' => '微信零钱',
'value' => 1,
],
$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,
// ],
1 => [
'name' => '东龙汇余额账户',
'value' => 1,
]
);
// 2 => [
// 'name' => '东龙汇余额账户',
// 'value' => 3,
// ]
// );
// 手续费
$data['commission'] = self::COMMISSION . '%';
// 提现规则
@ -145,6 +177,9 @@ class BillionsLogic extends Logic
$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;
@ -154,7 +189,7 @@ class BillionsLogic extends Logic
{
Db::startTrans();
try {
// // 1. 减少余额
// // 1. 减少余额-实际要等零钱到账之后才能对余额进行操作
// SubsidyWithdrawal::where(['user_id' => $user_id])->dec('total_amount', $post['money'])->update();
//
// // 2.先记录到日志下
@ -213,6 +248,392 @@ class BillionsLogic extends Logic
$suffix .= $pool[array_rand($pool)];
}
}
return $prefix . date('YmdHis') . $suffix;
return $prefix . date('YmdHis') . $suffix;
}
public static function applyBillionsBalance($user_id, $post)
{
$money = $post['money'];
$check = self::checkBalance($user_id, $money);
if ($check['code'] == -1) {
return $check;
}
// 充值到本地余额
$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();
$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;
// $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){
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;
}
}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;
}
$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;
}
// 剩余数量
$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();
}
}
}
$is_d = Distribution::where('user_id', $user['first_leader'])->where('is_distribution', 1 )->find();
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()
]);
}
}
}
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' => ''];
}
}

@ -686,8 +686,8 @@ class PayNotifyLogic
// 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();
// 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'])

@ -55,6 +55,7 @@ class AccountLog extends Models
const shop_reduce_wallet = 405; //商户钱包金额减少
const billions_inc_earnings = 406;//百亿返补佣金增加
const recharge_billions_inc_earnings = 407;//充值百亿返补佣金增加
const recharge_billions_inc_balance = 408;//充值百亿返补本地余额增加
const money_change = [ //余额变动类型
self::admin_add_money,self::admin_reduce_money,self::recharge_money,self::balance_pay_order,self::cancel_order_refund,self::after_sale_refund
, self::withdraw_to_balance,self::user_transfer_inc_balance, self::user_transfer_dec_balance, self::integral_order_dec_balance, self::integral_order_inc_balance

@ -0,0 +1,10 @@
<?php
namespace app\common\model;
use app\common\basics\Models;
class UserBillionsRechargeBalance extends Models
{
protected $table = 'ls_user_recharge_balance';
}