级差奖

**

  • 级差奖
    *@ApiMethod (POST)
  • $price 价格 $user_id 价格
    */
 function _range($price,$user_id)
{
    $user = Db::name('user')->where('id',$user_id)->find();
    $path = Db::name('user')->where('id',$user['id'])->value('path');
    $path = array_reverse(explode(',',$path));
    $rate = [
        '1'=>Db::name('config')->where('id',78)->value('value'),
        '2'=>Db::name('config')->where('id',79)->value('value'),
        '3'=>Db::name('config')->where('id',80)->value('value'),
        '4'=>Db::name('config')->where('id',81)->value('value'),
        '5'=>Db::name('config')->where('id',82)->value('value')
    ];
    $first = 0;//第一次拿奖励
    $user_level = $user['four_level'];
    Db::startTrans();
    try {
        foreach ($path as $k=>$v){
            $parent_level = Db::name('user')->where('id',$path[$k])->value('four_level');
            $money = Db::name('user')->where('id',$path[$k])->value('money');
          
            if ($parent_level > $user_level){
                if ($first == 0){
                    $rate_1 =  $rate[$parent_level];
                }else{
                    $rate_1 = $rate[$parent_level] - $rate[$user_level];
                }
                $reward = $price * $rate_1;
                Db::name('user')->where('id',$path[$k])->setInc('money',$reward);
                $money_log = [
                    'user_id'=>$path[$k],
                    'number'=>$reward,
                    'before'=>$money,
                    'after'=>$money + $reward,
                    'type'=>'range_01',
                    'create_time'=>time()
                ];
                Db::name('money_log')->insert($money_log);

                $user_level = $parent_level;
                $first = 1;
            }
        }
        Db::commit();
        // $this->success('成功');
    } catch (\Exception $e) {
        Db::rollback();
        // $this->error($e->getMessage());
    }
}

级差奖_第1张图片

你可能感兴趣的:(级差奖)