<?php

namespace App\Models;
use DB;

/**
 *
 */
class NagentGrade extends BaseModel {

	protected $table = 'nagent_grade';
	public $timestamps = false;
	//获取等级列表信息
	function getList($where='',$list=10){
        //DB::connection()->enableQueryLog();
	    $data=$this;
	    if(!empty($where) && count($where)){
	        $data = $data->where($where);
        }
        $data=$data->orderBy('sort','desc')->paginate($list);
        
	    if(!$data){
	        return -8040411022;//没有数据
        }
        return $data->toArray();
    }

    //获取代理金额设置
    function getRatio(){
        $data=$this->get();
        if(!$data){
            return -8005144322;
        }
        return $data->toArray();
    }

    //更新比例
    function updateRatio($data,$id){
        $res=$this->where('id',$id)->update($data);
        if(!$res){
            return -8005344322;
        }
        return 1;
    }

    //获取全部等级列表
    function getAllGrade($where=''){
        $data=$this;
        if(!empty($where) && count($where)){
            $data->where($where);
        }
        $data=$data->orderBy('grade_code','asc')->get();
        if(!$data){
            return -8040411022;//没有数据
        }
        return $data->toArray();
    }
    //获取代理默认提成比例
    protected function getGradeRatio($money){
        //DB::connection()->enableQueryLog();
        $ratio=$this->select('water_ratio','profit_ratio')->where('first_amount','<',$money)->orderBy('first_amount','desc')->first();
        //$queries = DB::getQueryLog();
        //print_r($queries);
        if(!$ratio){
            return -8060010022;//没有默认提成比例
        }
        $ratio=$ratio->toArray();
        //没有修改内容
        return $ratio;
        /*if($type==1){
            return $ratio['profit_ratio'];
        }else{
            return $ratio['water_ratio'];
        }*/
    }

    //获取代理提成比例
    protected function getGrades(){
        $grades = \App\Models\NagentGrade::select('grade_name','grade_code','water_ratio','first_amount')->orderBy('first_amount','desc')->get();
        $grades = !$grades?array():$grades->toArray();
        return $grades;
    }
    
    //获取代理对应额度等级
    protected function getGradeBymoney($money,$grade){
        foreach ($grade as $k => $v) {
            if($money>=$v['first_amount'])return $v;
        }
        return $v;
    }

    //代理名称获取
    function getGradeInfo($value,$num){
        $fild=$this->getFild($num);
        $data=$this->where($fild,$value)->first();
        if(!$data){
            return -8040411022;//没有数据
        }
        return $data->toArray();
    }
    //获取字段
    function getFild($num){
        $fild=array(
          '1'=>'id',
          '2'=>'grade_name',
          '3'=>'grade_code',
        );
        return $fild[$num];
    }

}



