<?php

/**
 * Notes:用户属性管理
 * User: wsc
 * Time: 2020/6/4 15:52
 * Class attr
 */
class ModelAttr
{
    private $attrType;
    public function __construct()
    {
        $this->redis = oo::commonOprRedis('user');
        $this->attrType = oo::cfg('attr');
    }

    /**
     * Notes:新增属性
     * User: wsc
     * Time: 2020/6/4 17:14
     * @param $uid
     * @param $type
     * @param $count
     * @param string $reason
     * @param string $ext
     * @return int
     */
    public function add($uid,$type,$count,$rid,$reason=""){
        $count =    abs(intval($count));
        if($count==0){
            return 0;
        }
        $num = $this->change($uid,$type,$count);
        //写流水,成就，活动
        oo::commonOprModel('currency')->log($uid,$type,"+",$count,$num,$rid,$reason);//流水，统计
//        if($type=="money"){
//            oo::commonOprModel('member')->saveStatistics($uid,'money',$count); //个人基本统计（历史总获得金币数）
//        }
        return $num;
    }

    /**
     * Notes:减除属性
     * User: wsc
     * Time: 2020/6/4 17:14
     * @param $uid
     * @param $type
     * @param $count
     * @param $reason
     * @param string $ext
     * @return int
     */
    public function del($uid,$type,$count,$rid,$reason=''){
        $value = -abs(intval($count));
        $num = $this->change($uid,$type,$value);
        if($num==-1){
            $hcount = $this->redis->hIncrBy(okeys::userAttr($uid),$type,0);
            oo::logs()->debug3(["uid"=>$uid,"type"=>$type,"count"=>$count,"haveCount"=>$hcount,"rid"=>$rid],"delMoneyErr.txt");
            return $num;
        }
        //写流水,成就，活动
        oo::commonOprModel('currency')->log($uid,$type,"-",$count,$num,$rid,$reason);
        if($type=='spins'){
            //体力大师活动判别
            oo::commonOprModel('activitynew')->addCommonScore($uid,'spins_master',-$value);
        }
        return $num;
    }

    /**
     * Notes:获取用户属性
     * User: wsc
     * Time: 2020/6/4 17:15
     * @param $uid
     * @param $type
     * @return int|array
     */
    public function get2($uid,$type=""){
        $key = okeys::userAttr($uid);
        if($type){
            $res = $this->redis->hIncrBy($key,$type,0);
        }else{
            $res =$this->redis->hGetAll($key);
        }
        return $res;
    }

    public function get($uid,$type="",$limit=0){
        $key = okeys::userAttr($uid);
        $attr = oo::cfg('attr');
        $res = $this->redis->hGetAll($key);
        if(empty($res)){//再查一次
            $res = $this->redis->hGetAll($key);
        }
        $needRecover = [];//需要恢复的字段
        if(empty($res)){
            $needRecover = array_filter(array_column($attr,"name"));
        }else{
            if (isset($res['uid'])) unset($res['uid']);
            if(count($res)<count($attr)){
                foreach ($attr as $v){
                    if(!isset($res[$v['name']])){
                        $needRecover[] = $v['name'];
                    }
                }
            }
        }
        if(!empty($needRecover)&&$limit==0){
            oo::commonOprModel('recoverdata')->recoverAttr($uid,$needRecover);//从新拉取写缓存
            $res = $this->get($uid,$type,1);
        }else{
            if($type){
                $res = $res[$type];
            }
        }
        return $res;
    }

    /**
     * Notes:改变用户属性
     * User: wsc
     * Time: 2020/6/4 17:15
     * @param $uid
     * @param $type
     * @param $count
     * @return int
     */
    public function change($uid,$type,$count){
        $this->get($uid);
        $key = okeys::userAttr($uid);
        $num = $this->redis->hIncrBy($key,$type,$count);
        if($num<0){//不能为负数，容错回滚
            $this->redis->hIncrBy($key,$type,abs($count));
            return -1;
        }
        $this->joinAttrLand($uid);
        return $num;
    }

    public function joinAttrLand($uid){
        $key  = okeys::Landing("GAMEINFO", $uid);
        oo::commonOprRedis('Usercache')->zAdd($key,time(),$uid);
    }

}
