dlh_test_server/app/common/logic/Billions.php
2024-12-06 23:00:52 +08:00

361 lines
19 KiB
PHP

<?php
namespace app\common\logic;
use app\common\model\AccountLog;
use app\common\model\distribution\Distribution;
use app\common\model\distribution\DistributionOrderGoods;
use app\common\model\user\User;
use app\common\model\user\UserDistribution;
use think\facade\Db;
class Billions
{
public static function billions($order, $user_id,$type="",$lstore_id="")
{
$user_subsidy = 2; // 自我补贴
$all_user_subsidy = 20;
$user_start_num = 1; // 补贴前几位数量
$user_start_subsidy = 30; //补贴前几位比例 全用户比例中扣除
$user_end_subsidy = 70; //补贴剩余部分比例
$user_first_leader = 30; // 推广者排队补贴
$first_leader = 2; //分销推广者补贴
$store_subsidy = 1; // 店铺推广者补贴 如没有补贴给店铺拥有者
$donglonghui = 2; //东龙汇服务费
$other_leader = 3; //分公司收入 如没有则并入东龙汇
$shop_com = 70; // 商家获取收入
$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'] = $order['type']; // 类型是充值
$data['subsidy_price'] = round($order['order_amount'] * $user_subsidy / 100,8);
$data['withdrawal_amount'] = $data['subsidy_price'];
$res = Db::name("user_billions")->insertGetId($data);
$subsidy = $order['order_amount'] * $all_user_subsidy / 100;
if($is_d != null){
$ufl = round($subsidy*$user_first_leader/100,8);
$subsidy_ufl = round($subsidy - $ufl,8);
$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;
}
$total_amount = round($v['order_price']-$v['available_amount'],8);
$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'] = $total_amount;
$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'],
'money' =>$total_amount
);
sendBillionsSubsidySMS($send);
}
if(!$first_save_record){
return -5;
}
}else{
$d['user_id'] = $v['user_id'];
$total_amount = round($v['order_price']-$v['available_amount'],8);
$d['total_amount'] = $total_amount;
$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'] = $total_amount ;
$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'],
'money' => $total_amount
);
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'],8);
}else{
$subsidy_price_sum = round($v['subsidy_price']+$ufl,8);
$datd['subsidy_price'] = $subsidy_price_sum;
$datd['withdrawal_amount'] = round($v['withdrawal_amount']+$ufl,8);
Db::name("user_billions")->where("billions_id",$v['billions_id'])->update($datd);
// Db::name("user_billions")->where("billions_id",$v['billions_id'])->inc("subsidy_price",$ufl)->update();
$ufl = 0;
}
}
}
$subsidy = round($subsidy_ufl + $ufl,8);
}
self::atcl($order,$store_subsidy,$type,$user,$first_leader,$other_leader);
}
$billions_count = Db::name("user_billions")->where("state",0)->count();
$data_surplus['billions_id'] = $res;
$data_surplus['subsidy_all_price'] = $subsidy;
$data_surplus['user_start_num'] = $user_start_num;
$data_surplus['user_start_subsidy'] = $user_start_subsidy;
$data_surplus['user_start_price'] = round($subsidy*$user_start_subsidy/100,8);
$data_surplus['user_start_avg_price'] = round($data_surplus['user_start_price']/$user_start_num,8);
$data_surplus['residue_price'] = $subsidy-$data_surplus['user_start_price'];
if($billions_count>$user_start_num){
$data_surplus['user_price'] = round(($subsidy-$data_surplus['user_start_price'])/($billions_count-$user_start_num),8);
}
$data_surplus['dtime'] = date("Y-m-d H:i:s");
$rest = Db::name("user_billions_surplus")->save($data_surplus);
return $rest;
}
public static function atcl($order,$store_subsidy,$type,$user,$first_leader,$other_leader){
if(isset($order['lstore_id'])){
$local_msg = Db::name("local_store")
->where("lstore_id",$order['lstore_id'])
->where("is_delete",0)
->where("store_state",1)
->find();
if($local_msg != null){
$local_first_leader = Db::name("user")
->where("id",$local_msg['user_id'])
->where("del",0)
->where("disable",0)
->find();
if($local_first_leader != null){
if($local_first_leader['first_leader']!=0){
$is_local_d = Distribution::where('user_id', $local_first_leader['first_leader'] )->where('is_distribution', 1 )->find();
if($is_local_d!=null){
$local_earnings = round($order['order_amount']*$store_subsidy/100,2);
User::where('id', $local_first_leader['first_leader'])
->inc('earnings', $local_earnings)->update();
$local_earnings_buy = AccountLog::where('user_id', $local_first_leader['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' => $local_first_leader['first_leader'],
'source_type' => AccountLog::local_inc_earnings, //todo 改为充值类型
'source_id' => 0, //todo 改为充值订单主键
'source_sn' => 0, // todo 改为充值订单编号
'change_amount' => $local_earnings,
'left_amount' => $local_earnings_buy + $local_earnings,
'change_type' => 1,
'remark' => '推广本地商家收款佣金增加'
]);
}
}
}
}
}
$remark = $remark1 =$remark2= "";
$source_type = $source_type1 =$source_type2= 0;
if($type == "balance_rechange"){
$remark = "用户充值佣金";
$source_type = AccountLog::recharge_inc_earnings;
$remark1 = "无极充值佣金";
$source_type1 = AccountLog::electrodeless_rechange_inc_earnings;
$remark2 = "无极公司充值佣金";
$source_type2 = AccountLog::company_electrodeless_rechange_inc_earnings;
}
if($type == "local_pay"){
$remark = "本地生活用户佣金";
$source_type = AccountLog::local_user_inc_earnings;
$remark1 = "无极本地生活用户佣金";
$source_type1 = AccountLog::electrodeless_local_inc_earnings;
$remark2 = "无极公司本地生活用户佣金";
$source_type2 = AccountLog::company_electrodeless_local_inc_earnings;
}
if($type == "billions_rechange"){
$remark = "返补充值佣金";
$source_type = AccountLog::billions_rechange_inc_earnings;
$remark1 = "无极返补充值佣金";
$source_type1 = AccountLog::electrodeless_billions_rechange_inc_earnings;
$remark2 = "无极公司返补充值佣金";
$source_type2 = AccountLog::company_electrodeless_billions_rechange_inc_earnings;
}
if($type == "pay_goods"){
$remark = "消费佣金";
$source_type = AccountLog::pay_money;
$remark1 = "无极消费佣金";
$source_type1 = AccountLog::electrodeless_pay_money;
$remark2 = "无极公司消费佣金";
$source_type2 = AccountLog::company_electrodeless_pay_money;
}
if($type == "distribution"){
$remark = "分销推广会员佣金";
$source_type = AccountLog::distribution_user_earnings;
$remark1 = "无极分销推广会员佣金";
$source_type1 = AccountLog::distribution_user_electrodeless_earnings;
$remark2 = "无极公司分销推广会员佣金";
$source_type2 = AccountLog::distribution_user_company_electrodeless_earnings;
}
if($type == "billions_use"){
$remark = "提前返补消费佣金";
$source_type = AccountLog::billion_user_earnings;
$remark1 = "无极提前返补消费佣金";
$source_type1 = AccountLog::billion_user_electrodeless_earnings;
$remark2 = "无极公司提前返补消费佣金";
$source_type2 = AccountLog::billion_user_company_electrodeless_earnings;
}
if($user['first_leader']>0){
$first_leader_percent = round($order['order_amount'] * $first_leader / 100, 8);
// 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' => $source_type, //todo 改为充值类型
'source_id' => 0, //todo 改为充值订单主键
'source_sn' => 0, // todo 改为充值订单编号
'change_amount' => $first_leader_percent,
'left_amount' => $earnings_buy + $first_leader_percent,
'change_type' => 1,
'remark' => $remark
]);
DistributionOrderGoods::create([
'sn' => $sn,
'user_id' => $user['first_leader'],
'ratio' => $first_leader,
'order_id' => 0, //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()
]);
}
// 1%给运营人员
$divide_second_user_msg = Db::name("user")->where("id",$user['divide_second_user_id'])->find();
$regional_state = 0;
if($divide_second_user_msg != null){
$regional_state = $user['regional_state'];
}
$performance_person = 0;
$performance = 0;
if($regional_state == 1){
$performance = $user['regional_percent'];
$performance_person = round($order['order_amount'] * $performance / 100, 8);
// Order::where('id', $order['id'])
// ->inc('distribution_money',$performance_person)->update();
// $shopDistribution_msg = UserShopDistribution::where('shop_id', $value['shop_id'])
// ->find();
User::where('id', $user['divide_second_user_id'])
->inc('earnings', $performance_person)->update();
$earnings_buy = AccountLog::where('user_id', $user['divide_second_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_second_user_id'],
'source_type' => $source_type1,
'source_id' => 0,
'source_sn' => 0,
'change_amount' => $performance_person,
'left_amount' => $earnings_buy + $performance_person,
'change_type' => 1,
'remark' => $remark1
]);
DistributionOrderGoods::create([
'sn' => $sn,
'user_id' => $user['divide_second_user_id'],
'ratio' => $performance,
'order_id' => 0,
'order_goods_id' => 0,
'goods_num' => 1,
'money' => $performance_person,
'status' => 2,
'create_time' => time(),
'shop_id' => 0,
'settlement_time' => time()
]);
}
// 3%对公司
$role_user_msg = Db::name("user")->where("id",$user['divide_role_user_id'])->find();
$state = 0;
if($role_user_msg != null){
$state = $user['divide_state'];
}
if($user['divide_role_user_id'] > 0&&$state>0 ){
$divide_percent = round($order['order_amount'] * ($other_leader-$performance) / 100, 8);
// 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' => $source_type2, //todo 改为充值类型
'source_id' => 0, //todo 改为充值订单主键
'source_sn' => $order['order_sn'], // todo 改为充值订单编号
'change_amount' => $divide_percent,
'left_amount' => $earnings_buy + $divide_percent,
'change_type' => 1,
'remark' => $remark2
]);
DistributionOrderGoods::create([
'sn' => $sn,
'user_id' => $user['divide_role_user_id'],
'ratio' => $other_leader-$performance,
'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()
]);
}
return true;
}
}