极差(团队业绩)

//培育奖
function cultivate_reward($user_id,$money)
{
    //total_achievement 个人总业绩
    $path = Db::name('user')->where('id',$user_id)->value('path');
    $paths = explode(',',$path);
    rsort($paths);
    $peiyu1 = Db::name('reward_config')->where('id',6)->field('value,condition')->find();
    $peiyu2 = Db::name('reward_config')->where('id',7)->field('value,condition')->find();
    $peiyu3 = Db::name('reward_config')->where('id',8)->field('value,condition')->find();

    $total_money = $money*$peiyu3['value'];//要分的总钱数
    $num = 0;
    $num1 = 1;
    $num2 = 1;
    $num3 = 1;
    Db::startTrans();
    try{
        foreach ($paths as $k=>$v){
            $reward_money=0;
            //判断剩余
            if ($total_money<=0){
                break;
            }
            $moneys = Db::name('user')->where('find_in_set(:id,path)',['id'=>$v])->sum('total_achievement');
            $money_self = Db::name('user')->where('id',$v)->field('total_achievement')->find();
            //判断用户是否存在 不存在跳过
            if (empty($money_self)){
                continue;
            }
            $user_level = Db::name('user')->where('id',$v)->value('level');
            //判断等级
            if ($user_level==0){
                continue;
            }
            if ($moneys+$money_self['total_achievement']>=$peiyu1['condition'] && $moneys+$money_self['total_achievement']<$peiyu2['condition']){
                $reward_money = $money*$peiyu1['value'];
                if ($num1==0){
                    continue;
                }
                $num1--;
            }elseif ($moneys+$money_self['total_achievement']>=$peiyu2['condition'] && $moneys+$money_self['total_achievement']<$peiyu3['condition']){
                $reward_money = $money*$peiyu2['value'];
                if ($num2==0){
                    continue;
                }
                $num2--;
            }elseif ($moneys+$money_self['total_achievement']>=$peiyu3['condition']){
                $reward_money = $money*$peiyu3['value'];
                if ($num3==0){
                    break;
                }
                $num3--;
            }
            //判断平级
            if ($num == $reward_money){
                $num = $reward_money;//平级跳过
                continue;
            }
            //极差
            if ($num!=0){
                $reward_money = $reward_money-$num;
            }
            $total_money = $total_money-$reward_money;
            $current_info = Db::name('user')->where('id',$v)->field('commission')->find();
            Db::name('commission_log')->insert([
                'user_id'=>$v,
                'number'=>$reward_money,
                'before'=>$current_info['commission'],
                'after'=>$current_info['commission'] + $reward_money,
                'type'  =>5,//培育奖
                'create_time'=>time()
            ]);
            Db::name('user')->where('id',$v)->setInc('commission',$reward_money);
            $num += $reward_money;
        }
        Db::commit();
        return ['code'=>1,'msg'=>'成功'];
    }catch (Exception $e){
        Db::rollback();
        return ['code'=>0,'msg'=>$e->getMessage()];
    }

}

极差(团队业绩)_第1张图片

你可能感兴趣的:(极差(团队业绩))