分销直推奖

/**
* 直推奖
* @ApiMethod (post)
* @ApiHeaders (name=token, type=string, required=true, description=“请求的Token”)
* @ApiParams (name=“user_id”, type=“integer”, required=“false”, description=“用户id”)
* @ApiParams (name=“money”, type=“integer”, required=“false”, description=“金额”)
*/

 
     public function test()
     {
         $user_id = $this->request->param('user_id');
         $money = $this->request->param('money');
         $money1 = $money * 0.2;
         $parent_id = Db::name('user')->where('id',$user_id)->value('parent_id');
         $parent_level = Db::name('user')->where('id',$parent_id)->value('level');

         Db::startTrans();
         try{
             //父级是会员奖励20%
         if ($parent_level != 0){
             Db::name('user')->where('id',$parent_id)->setInc('money',$money1);
              Db::name('user')->where('id',$user_id)->setDec('money',$money);
             $pparent_id = $parent_id;
         }else{
             //父级不是会员向上找等级是会员的上级奖励20%
             $path = Db::name('user')->where('id',$user_id)->value('path');
             $path = array_reverse(explode(',',$path));
             foreach ($path as $key=>$v){
                 $parent_info = Db::name('user')->where('id',$v)->find();
                 if (empty($parent_info)){
                     break;
                 }
                 if ($parent_info['level'] == 0){
                     continue;
                 }
                 Db::name('user')->where('id',$v)->setInc('money',$money1);
                 Db::name('user')->where('id',$user_id)->setDec('money',$money);
                 $pparent_id = $v;
                    break;
             }
         }
         //绩效奖50%递减直到为1
             //上上级
             $money2 = $money1 *0.5;
             $pp_path = Db::name('user')->where('id',$pparent_id)->value('path');
             $pp_path = array_reverse(explode(',',$pp_path));
             foreach ($pp_path as $key1=>$value){
                 $pparent_info = Db::name('user')->where('id',$value)->find();
                 if (empty($pp_path)){
                     break;
                 }
                 if ($pparent_info['level'] == 0){
                     continue;
                 }
                 Db::name('user')->where('id',$value)->setInc('money',$money2);
                 if ($money2 < 1){
                     break;
                 }
                 $money2 = $money2*0.5;
             }
             Db::commit();
         }catch (\Exception $e){
             Db::rollback();
         }
     }
     

分销直推奖_第1张图片

你可能感兴趣的:(分销直推奖)