<?php
namespace app\admin\model\system;
use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;

class SystemLevelEquity extends BaseModel
{
    use ModelTrait;
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'system_level_equity';
    /**
     * 权益类型
     * type 记录在数据库中用来区分任务
     * name 任务名 (任务名中的{$num}会自动替换成设置的数字 + 单位)
     * max_number 最大设定数值 0为不限定
     * min_number 最小设定数值
     * unit 单位
     * @var array
     */
    protected static $EquityType=[
        [
            'equity_type'=>'code_stock_right_bonus',
            'title'=>'全民分红利润{$num}%',
            'real_title'=>'全民分红',
            'max_number'=>100,
            'min_number'=>0,
        ],
        [
            'equity_type'=>'give_stock_right_bonus',
            'title'=>'赠送全民分红利润{$num}%',
            'real_title'=>'赠送全民分红',
            'max_number'=>100,
            'min_number'=>0,
        ],
        [
            'equity_type'=>'all_stock_right_bonus',
            'title'=>'公司利润分红{$num}%',
            'real_title'=>'公司利润分红',
            'max_number'=>100,
            'min_number'=>0,
        ],
        [
            'equity_type'=>'recommend_bonus',
            'title'=>'高级合伙人推荐奖{$num}%',
            'real_title'=>'高级合伙人推荐奖',
            'max_number'=>100,
            'min_number'=>0,
        ],
        [
            'equity_type'=>'code_recommend_bonus',
            'title'=>'推荐总代推荐奖{$num}%',
            'real_title'=>'推荐总代推荐奖',
            'max_number'=>100,
            'min_number'=>0,
        ],
        [
        'equity_type'=>'senior_stock_right_bonus',
        'title'=>'高级合伙人公司利润分红{$num}%',
        'real_title'=>'高级合伙人公司分红',
        'max_number'=>100,
        'min_number'=>0,
        ],
    ];
    public function profile()
    {
        return $this->hasOne('SystemUserLevel','level_id','id')->field('name');
    }

    /**
     * 获取所有权益
     * @return mixed
     */
    public static function getEquityTypeAll()
    {
        return self::$EquityType;
    }
    /**
     * 获取某个任务
     * @param $type
     * @return mixed
     */
    public static function getEquityType($type)
    {
        foreach (self::$EquityType as $item){
            if($item['equity_type']==$type) return $item;
        }
    }
    /**
     * 设置权益名
     * @param $type
     * @param $num
     * @return mixed
     */
    public static function setEquityName($type,$num)
    {
        $systemType=self::getEquityType($type);
        return str_replace('{$num}',$num.$systemType['unit'],$systemType['name']);
    }
    /**
     * 获取等级会员权益列表
     * @param $level_id
     * @param $page
     * @param $limit
     * @return array
     */
    public static function getEquityList($level_id,$page,$limit)
    {
        $data=self::where('levelid',$level_id)->order('sort desc,addtime desc')->page($page,$limit)->select();
        $data=count($data) ? $data->toArray() : [];
        foreach ($data as &$item){
            $item['level_name']=SystemUserLevel::where('id',$item['levelid'])->value('name');
            $item['o_level_name']=SystemUserLevel::where('id',$item['target_levelid'])->value('name');
        }
        $count=self::where('levelid',$level_id)->count();
        return compact('data','count');
    }

}
