960 lines
42 KiB
PHP
Executable File
960 lines
42 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\api\logic;
|
|
|
|
use app\common\basics\Logic;
|
|
use app\common\model\AccountLog;
|
|
use app\common\model\billions\Billions;
|
|
use app\common\model\distribution\Distribution;
|
|
use app\common\model\distribution\DistributionOrderGoods;
|
|
use app\common\model\user\UserDistribution;
|
|
use app\common\model\subsidy_withdrawal\SubsidyWithdrawal;
|
|
use app\common\model\subsidy_withdrawal\SubsidyWithdrawalRecord;
|
|
use app\common\model\UserBillionsRechargeBalance;
|
|
use app\common\model\user\User;
|
|
use app\common\model\order\Order;
|
|
use app\common\model\RechargeOrder;
|
|
use app\common\model\shop\Shop;
|
|
use think\Exception;
|
|
use think\facade\Db;
|
|
use app\common\enum\PayEnum;
|
|
|
|
class BillionsLogic extends Logic
|
|
{
|
|
const COMMISSION = 10; // 手续费
|
|
const MINIMUM_WITHDRAWAL = 1; // 最低提现
|
|
const MIN_RECHARGE = 0.1; // 最低充值金额
|
|
const MAXIMUM_WITHDRAWAL = 500; // 最高提现
|
|
|
|
// 获取数据
|
|
public static function lists($page, $size, $state, $user_id)
|
|
{
|
|
$where[] = ['b.user_id', '=', $user_id];
|
|
if ($state == 1) {
|
|
$where[] = ['b.state', '>=', 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();
|
|
|
|
if ($state == 1) {
|
|
$list = Billions::alias('b')->where($where)->order('billions_id', 'desc')
|
|
->page($page,$size)->select();
|
|
} else {
|
|
$list = Billions::alias('b')->where($where)
|
|
->page($page,$size)->select();
|
|
}
|
|
|
|
$ids = [];
|
|
foreach ($list as &$v) {
|
|
$ids[] = $v['billions_id'];
|
|
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');
|
|
}
|
|
}
|
|
|
|
|
|
Billions::whereIn('billions_id', $ids)->where(['user_id' => $user_id])->update(['is_read' => 1]);
|
|
|
|
$count = Billions::alias('b')->where($where)->count();
|
|
|
|
// 商家最高补贴
|
|
$highSubsidy = Billions::where(['user_id' => $user_id])->sum('order_price', false);
|
|
|
|
// 已完成订单笔数
|
|
$doneOrder = Billions::where([
|
|
['user_id', '=', $user_id],
|
|
['state', '>', 0]
|
|
])->count();
|
|
|
|
// 正在排队的数量
|
|
$queuesNumber = Billions::where(['user_id' => $user_id, 'state' => 0])->count();
|
|
|
|
// 排队中的返补金额
|
|
$queuesMoney = Billions::where(['user_id' => $user_id, 'state' => 0])->sum('subsidy_price', false);
|
|
|
|
// 已返补到的金额
|
|
$getSubsidy = Billions::where(array(
|
|
['user_id', '=', $user_id],
|
|
['state', '>', 0]
|
|
))->sum('subsidy_price', false);
|
|
|
|
$more = is_more($count, $page, $size); //是否有下一页
|
|
|
|
$data = [
|
|
'list' => $list,
|
|
'page_no' => $page,
|
|
'page_size' => $size,
|
|
'count' => $count,
|
|
'more' => $more,
|
|
'doneOrder' => $doneOrder,
|
|
'highSubsidy' => $highSubsidy,
|
|
'getSubsidy' => $getSubsidy,
|
|
'queuesNumber' => $queuesNumber,
|
|
'queuesMoney' => $queuesMoney,
|
|
'unreadBillions' => self::getUnreadCount($user_id)
|
|
];
|
|
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)
|
|
{
|
|
// 手续费
|
|
$data['commission'] = self::COMMISSION . '%';
|
|
|
|
// 最小充值金额
|
|
$data['min_recharge'] = self::MIN_RECHARGE;
|
|
// 最小提现金额
|
|
$data['min_withdraw'] = self::MINIMUM_WITHDRAWAL;
|
|
|
|
// 充值金额规则
|
|
$data['recharge_rule'] =
|
|
' <p>
|
|
<b>充值到本地余额能参与消费返补,余额提现参考余额规则</b>
|
|
</p>
|
|
';
|
|
|
|
// 提现金额规则
|
|
$data['withdrawl_rule'] =
|
|
' <p>
|
|
<b>提现规则</b>
|
|
</p>
|
|
<p>提现手续费 10%</p>
|
|
';
|
|
|
|
$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, $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 = 1; // 补贴前几位数量
|
|
$user_start_subsidy = 40; //补贴前几位比例 全用户比例中扣除
|
|
$user_end_subsidy = 60; //补贴剩余部分比例
|
|
$user_first_leader = 40; // 推广者排队补贴
|
|
$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,8);
|
|
$res = Db::name("user_billions")->save($data);
|
|
$subsidy = $money * $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();
|
|
// var_dump($first_billions_msg);die;
|
|
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) {
|
|
$user = Db::name('user')->field('nickname, mobile')->where('id', $v['user_id'])->find();
|
|
$send = array(
|
|
'user_id' => $v['user_id'],
|
|
'mobile' => $user['mobile'],
|
|
'nickname' => $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) {
|
|
$user = Db::name('user')->field('nickname, mobile')->where('id', $v['user_id'])->find();
|
|
$send = array(
|
|
'user_id' => $v['user_id'],
|
|
'mobile' => $user['mobile'],
|
|
'nickname' => $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'],8);
|
|
}else{
|
|
$subsidy_price_sum = round($v['subsidy_price']+$ufl,8);
|
|
Db::name("user_billions")->where("billions_id",$v['billions_id'])->update(['subsidy_price'=>$subsidy_price_sum]);
|
|
// Db::name("user_billions")->where("billions_id",$v['billions_id'])->inc("subsidy_price",$ufl)->update();
|
|
$ufl = 0;
|
|
}
|
|
}
|
|
|
|
}
|
|
$subsidy = round($subsidy_ufl + $ufl,8);
|
|
}
|
|
}
|
|
// $first_msg = Db::name("user_billions")->where("user_id",$user['first_leader'])->where("state",0)->select()->toArray();
|
|
$billions_msg = Db::name("user_billions")->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, 8);
|
|
$last = round($start_subsidy - $average * ($user_start_num - 1), 8);
|
|
for ($i = 0; $i < $user_start_num; $i++) {
|
|
if ($i + 1 == $user_start_num) {
|
|
$arr[$i] = $last;
|
|
} else {
|
|
$arr[$i] = $average;
|
|
}
|
|
}
|
|
// var_dump($subsidy);
|
|
// var_dump($start_subsidy);
|
|
// var_dump($count-$user_start_num);
|
|
|
|
// 剩余人数补贴
|
|
$end_average = round(($subsidy - $start_subsidy)/ ($count-$user_start_num), 8);
|
|
// var_dump($end_average);
|
|
$end_last = round($subsidy - $start_subsidy - $end_average * ($count-$user_start_num - 1), 8);
|
|
// var_dump($end_last);
|
|
for ($y = 0; $y < $count-$user_start_num; $y++) {
|
|
if ($y + 1 == $count-$user_start_num) {
|
|
$arr1[$y] = $end_last;
|
|
} else {
|
|
$arr1[$y] = $end_average;
|
|
}
|
|
}
|
|
}else{
|
|
$user_start_num = $count;
|
|
// 数量不超出前几位补贴用户,按全部金额计算
|
|
$average = round($subsidy/$count, 8);
|
|
$last = round($subsidy - $average * ($count - 1), 8);
|
|
for ($i = 0; $i < $count; $i++) {
|
|
if ($i + 1 == $count) {
|
|
$arr[$i] = $last;
|
|
} else {
|
|
$arr[$i] = $average;
|
|
}
|
|
}
|
|
}
|
|
// print_r("<pre>");
|
|
// var_dump($arr);
|
|
// var_dump($arr1);die;
|
|
$billion_arr = $billion_arr_id = "";
|
|
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) {
|
|
$user = Db::name('user')->field('nickname, mobile')->where('id', $v['user_id'])->find();
|
|
$send = array(
|
|
'user_id' => $v['user_id'],
|
|
'mobile' => $user['mobile'],
|
|
'nickname' => $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) {
|
|
$user = Db::name('user')->field('nickname, mobile')->where('id', $v['user_id'])->find();
|
|
$send = array(
|
|
'user_id' => $v['user_id'],
|
|
'mobile' => $user['mobile'],
|
|
'nickname' => $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;
|
|
if($residue_count != 0){
|
|
// 剩余金额
|
|
$residue_price = round($arr[$k]+$v['subsidy_price']-$v['order_price'],8);
|
|
foreach($arr as $k1=>$v1){
|
|
if($k1 > $k){
|
|
$residue_price+=$arr[$k1];
|
|
}
|
|
}
|
|
|
|
$average = round($residue_price/$residue_count,8);
|
|
$last = round($residue_price - $average*($residue_count-1),8);
|
|
foreach($arr as $k1=>$v1){
|
|
if($k1+1 == $user_start_num){
|
|
$arr[$k1] = $last;
|
|
}else{
|
|
if($k1 > $k){
|
|
$arr[$k1] = $average;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}else{
|
|
// $subp = round($v['subsidy_price']+$arr[$k],6);
|
|
// $billion_arr.=" when ".$v['billions_id']." then '".$subp."'";
|
|
// $billion_arr_id.=$v['billions_id'].",";
|
|
// $dataSet[] = [
|
|
// 'billions_id' => $v['billions_id'], // 假设每个模型数据都有一个ID字段
|
|
// 'subsidy_price' => round($v['subsidy_price']+$arr[$k],6) // 其他字段数据
|
|
// ];
|
|
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) {
|
|
$user = Db::name('user')->field('nickname, mobile')->where('id', $v['user_id'])->find();
|
|
$send = array(
|
|
'user_id' => $v['user_id'],
|
|
'mobile' => $user['mobile'],
|
|
'nickname' => $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) {
|
|
$user = Db::name('user')->field('nickname, mobile')->where('id', $v['user_id'])->find();
|
|
$send = array(
|
|
'user_id' => $v['user_id'],
|
|
'mobile' => $user['mobile'],
|
|
'nickname' => $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;
|
|
if($residue_count!=0){
|
|
// 剩余金额
|
|
$residue_price = round($arr1[$k-$user_start_num]+$v['subsidy_price']-$v['order_price'],8);
|
|
foreach($arr1 as $k1=>$v1){
|
|
if($k1 > $k - $user_start_num){
|
|
$residue_price+=$arr1[$k1];
|
|
}
|
|
}
|
|
$average = round($residue_price/$residue_count,8);
|
|
$last = round($residue_price - $average*($residue_count-1),8);
|
|
foreach($arr1 as $k1=>$v1){
|
|
if($k1+1 == $count-$user_start_num){
|
|
$arr1[$k1] = $last;
|
|
}else{
|
|
if($k1 > $k){
|
|
$arr[$k1] = $average;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}else{
|
|
// $subp = round($v['subsidy_price']+$arr1[$k-$user_start_num],6);
|
|
// $billion_arr.=" when ".$v['billions_id']." then '".$subp."'";
|
|
// $billion_arr_id.=$v['billions_id'].",";
|
|
// $dataSet[] = [
|
|
// 'billions_id' => $v['billions_id'], // 假设每个模型数据都有一个ID字段
|
|
// 'subsidy_price' => round($v['subsidy_price']+$arr1[$k-$user_start_num],6) // 其他字段数据
|
|
// ];
|
|
Db::name("user_billions")->where("billions_id",$v['billions_id'])->inc("subsidy_price",$arr1[$k-$user_start_num])->update();
|
|
}
|
|
}
|
|
}
|
|
// $billion_arr_id = explode(",",$billion_arr_id);
|
|
// array_pop($billion_arr_id);
|
|
// // print_r("<pre>");
|
|
// // var_dump($billion_arr);
|
|
// // var_dump($billion_arr_id);die;
|
|
// $billion_arr_id = implode(",",$billion_arr_id);
|
|
// $sql = "UPDATE `ls_user_billions`
|
|
// SET subsidy_price = CASE billions_id
|
|
// $billion_arr
|
|
// END
|
|
// WHERE billions_id IN ($billion_arr_id)";
|
|
// $query = Db::query($sql);
|
|
|
|
// Db::name("billions")->updateAll($dataSet,"billions_id");
|
|
if($is_d!=null){
|
|
if($user['first_leader']>0){
|
|
$first_leader_percent = round($money * $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' => 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()
|
|
]);
|
|
}
|
|
// 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 = $divide_second_user_msg['regional_state'];
|
|
}
|
|
$performance_person = 0;
|
|
$performance = 0;
|
|
if($regional_state == 1){
|
|
$performance = $user['regional_percent'];
|
|
$performance_person = round($money * $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' => AccountLog::billions_inc_earnings,
|
|
'source_id' => 0,
|
|
'source_sn' => 0,
|
|
'change_amount' => $performance_person,
|
|
'left_amount' => $earnings_buy + $performance_person,
|
|
'change_type' => 1,
|
|
'remark' => '百亿返补销售人员佣金增加'
|
|
]);
|
|
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 = $role_user_msg['divide_state'];
|
|
}
|
|
if($user['divide_role_user_id'] > 0&&$state>0 ){
|
|
$divide_percent = round($money * ($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' => 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-$performance,
|
|
'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_recharge = BillionsLogic::MIN_RECHARGE;
|
|
if ($money < $min_recharge) {
|
|
return [
|
|
'code' => -1,
|
|
'msg' => '最低充值' . $min_recharge . '元'
|
|
];
|
|
}
|
|
|
|
// 4.如果提现金额在扣除手续费之后,小于最低提现金额则提现失败
|
|
// $left_money = $money - ($money * (BillionsLogic::COMMISSION / 100));
|
|
// if ($left_money < $min_withdraw) {
|
|
// return [
|
|
// 'code' => -1,
|
|
// 'msg' => '扣除手续费后提现金额不能小于' . $min_withdraw . '元'
|
|
// ];
|
|
// }
|
|
return ['code' => 1, 'msg' => ''];
|
|
}
|
|
|
|
/**
|
|
* @notes 百亿充值记录
|
|
*/
|
|
public static function rechargeRecord($get)
|
|
{
|
|
$list = RechargeOrder::field('order_sn, order_amount, create_time')
|
|
->where([
|
|
'user_id' => $get['user_id'],
|
|
'pay_status' => PayEnum::ISPAID, // 已支付的
|
|
'pay_way' => 5 // 百亿返补充值
|
|
])
|
|
->order('create_time', 'desc')
|
|
->page($get['page_no'], $get['page_size'])
|
|
->select()
|
|
->toArray();
|
|
|
|
$count = RechargeOrder::where([
|
|
'user_id' => $get['user_id'],
|
|
'pay_status' => PayEnum::ISPAID,
|
|
'pay_way' => 5 // 百亿返补充值
|
|
])->count();
|
|
|
|
foreach($list as &$item) {
|
|
$item['desc'] = '充值到余额¥'. clearZero($item['order_amount']);
|
|
}
|
|
|
|
$result = [
|
|
'count' => $count,
|
|
'lists' => $list,
|
|
'more' => is_more($count, $get['page_no'], $get['page_size']),
|
|
'page_no' => $get['page_no'],
|
|
'page_size' => $get['page_size']
|
|
];
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @notes 百亿提现微信记录
|
|
*/
|
|
public static function withdrawalRecord($get)
|
|
{
|
|
$list = SubsidyWithdrawalRecord::field('commission, take_money, money, sn, create_time')
|
|
->where([
|
|
'user_id' => $get['user_id'],
|
|
'way' => 1
|
|
])
|
|
->order('create_time', 'desc')
|
|
->page($get['page_no'], $get['page_size'])
|
|
->select()
|
|
->toArray();
|
|
|
|
$count = SubsidyWithdrawalRecord::where([
|
|
'user_id' => $get['user_id'],
|
|
'way' => 1
|
|
])->count();
|
|
|
|
foreach($list as &$item) {
|
|
$item['desc'] = '提现微信¥' . number_format($item['money'], 2) . ', 手续费: ' . $item['commission'] . '%, 实际到账 ¥' . $item['take_money'];
|
|
}
|
|
|
|
$result = [
|
|
'count' => $count,
|
|
'lists' => $list,
|
|
'more' => is_more($count, $get['page_no'], $get['page_size']),
|
|
'page_no' => $get['page_no'],
|
|
'page_size' => $get['page_size']
|
|
];
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 获取返补已满但是未读状态的数量
|
|
public static function getUnreadCount($user_id)
|
|
{
|
|
$where[] = ['user_id', '=', $user_id];
|
|
$where[] = ['state', '>', 0];
|
|
$where[] = ['is_read', '=', 0];
|
|
return Billions::where($where)->count();
|
|
}
|
|
} |