<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/8/2
 * Time: 上午10:38
 */

namespace common\helpers\bonus;

use common\helpers\Cache;
use common\helpers\user\Info;
use common\models\CalcBonus;
use common\models\PerfMonth;
use common\models\Period;
use common\models\DeclarationLevel;
use common\models\DecRole;
use common\models\EmployLevel;
use common\models\User;
use common\models\UserBonus;
use common\models\UserPerf;
use Yii;
use common\models\UserInfo;
use yii\helpers\Json;

class CalcCache {
    const LIMIT = 10000;

    const REDIS_KEY_PREFIX_USER = 'calc:user_';
    const REDIS_KEY_PREFIX_USER_ACTIVE = 'calc:userActive_';
    const REDIS_KEY_PREFIX_USER_INFO = 'calc:userInfo_';
    const REDIS_KEY_PREFIX_USER_BONUS = 'calc:userBonus_';
    const REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS = 'calc:periodMonthCalcBonus_';
    const REDIS_KEY_PREFIX_USER_PERF = 'calc:userPerf_';
    const REDIS_KEY_PREFIX_SURPLUS_PERF = 'calc:spPerf_';
    const REDIS_KEY_PREFIX_NOW_PERIOD_PERF = 'calc:nowPeriodPerf_';
    const REDIS_KEY_PREFIX_NOW_STANDARD_MONTH_PERF = 'calc:nowStandardMonthPerf_';
    const REDIS_KEY_PREFIX_LAST_MONTH_PERF = 'calc:lastMonthPerf_';
    const REDIS_KEY_PREFIX_NOW_MONTH_PERF = 'calc:nowMonthPerf_';
    const REDIS_KEY_PREFIX_NOW_MONTH_SCORE = 'calc:nowMonthScore_';
    const REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS = 'calc:nowMonthLastPeriodReconsumePoints_';
    const REDIS_KEY_PREFIX_EMP_NUM_PERF = 'calc:empLevelNum_';
    const REDIS_KEY_PREFIX_BONUS = 'calc:bonus_';
    const REDIS_KEY_PREFIX_STANDARD_BONUS = 'calc:standard:bonus_';
    const REDIS_KEY_PREFIX_YC_BONUS = 'calc:yc:bonus_';
    const REDIS_KEY_PREFIX_FW_BONUS = 'calc:fw:bonus_';
    const REDIS_KEY_PREFIX_TOURISM_BONUS = 'calc:tourism:bonus_';
    const REDIS_KEY_PREFIX_VILLA_BONUS = 'calc:villa:bonus_';
    const REDIS_KEY_PREFIX_GARAGE_BONUS = 'calc:garage:bonus_';
    const REDIS_KEY_PREFIX_HAS_PERF_USER = 'calc:hasPerfUser_';
    const REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER = 'calc:hasMonthPerfUser_';
    const REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER = 'calc:hasStandardMonthPerfUser_';
    const REDIS_KEY_PREFIX_HAS_BD_USER = 'calc:hasBDUser_';
    const REDIS_KEY_PREFIX_HAS_INCOME_USER = 'calc:hasIncomeUser_';
    const REDIS_KEY_PREFIX_HAS_BONUS_USER = 'calc:hasBonusUser_';
    const REDIS_KEY_PREFIX_HAS_YC_BONUS_USER = 'calc:hasYcBonusUser_';
    const REDIS_KEY_PREFIX_HAS_FW_BONUS_USER = 'calc:hasFwBonusUser_';
    const REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER = 'calc:hasMonthBonusUser_';
    const REDIS_KEY_PREFIX_HAS_PERF_USER_POOL = 'calc:hasPerfUserPool_';
    const REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL = 'calc:hasMonthPerfUserPool_';
    const REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL = 'calc:hasStandardMonthPerfUserPool_';
    const REDIS_KEY_PREFIX_HAS_BD_USER_POOL = 'calc:hasBDUserPool_';
    const REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL = 'calc:hasIncomeUserPool_';
    const REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL = 'calc:hasBonusUserPool_';
    const REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL = 'calc:hasYcBonusUserPool_';
    const REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL = 'calc:hasFwBonusUserPool_';
    const REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL = 'calc:hasMonthBonusUserPool_';
    const REDIS_KEY_PREFIX_HAS_SCORE_USER = 'calc:hasScoreUser_';
    const REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL = 'calc:hasScoreUserPool_';
    const REDIS_KEY_PREFIX_HAS_LS_PCS_USER = 'calc:hasLSPCSUser_';
    const REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL = 'calc:hasLSPCSUserPool_';
    const REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER = 'calc:hasCFPercentPCSUser_';
    const REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL = 'calc:hasCFPercentPCSUserPool_';
    const REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER = 'calc:hasLXPercentPCSUser_';
    const REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL = 'calc:hasLXPercentPCSUserPool_';
    const REDIS_KEY_PREFIX_DEC_ROLE_CONFIG = 'calc:decRoleConfig_';
    const REDIS_KEY_PREFIX_CF_TOTAL_PERCENT = 'calc:cfTotalPercent_';
    const REDIS_KEY_PREFIX_LX_TOTAL_PERCENT = 'calc:lxTotalPercent_';
    const REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP = 'calc:userInfo:ChildOneDeep_';
    const REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA = 'calc:gxBonusDeepOneListData_';
    const REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA = 'calc:gxBonusDeepTwoListData_';
    const REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA = 'calc:ycBonusListData_';
    const REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA = 'calc:fwBonusListData_';

    //@todo
    const REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF = 'calc:repairSurplusPerf_';
    const REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER = 'calc:hasRepairPerfUser_';
    const REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL = 'calc:hasRepairPerfPool_';

    //收入的奖金类型
//    const INCOME_TG_BONUS = 'BONUS_TG';
//    const INCOME_XF_BONUS = 'BONUS_XF';
//    const INCOME_YJ_BONUS = 'BONUS_YJ';
//    const INCOME_QY_BONUS = 'BONUS_QY';
    const INCOME_QY_BONUS_BD = 'BONUS_QY_BD';
    const INCOME_QY_BONUS_FX = 'BONUS_QY_FX';
    const INCOME_BONUS_TRAVEL = 'BONUS_TRAVEL';
    const INCOME_BONUS_CAR = 'BONUS_CAR';
    const INCOME_BONUS_HOUSE = 'BONUS_HOUSE';
    const CAPPED_BONUS_QY = 'CAPPED_BONUS_QY'; // 团队奖封顶前的金额
    const INCOME_BONUS_LIST = [
//        self::INCOME_TG_BONUS,
//        self::INCOME_XF_BONUS,
//        self::INCOME_YJ_BONUS,
//        self::INCOME_QY_BONUS,
        self::INCOME_QY_BONUS_BD,
//        self::INCOME_QY_BONUS_FX,
    ];
    const NOT_SEND_BONUS_LIST = [
        self::INCOME_QY_BONUS_BD,
        self::INCOME_QY_BONUS_FX,
        self::INCOME_BONUS_TRAVEL,
        self::INCOME_BONUS_CAR,
        self::INCOME_BONUS_HOUSE,
        self::CAPPED_BONUS_QY,
    ];

    const FROM_MEANS_BD = 'BD';
    const FROM_MEANS_FX = 'FX';

    /**
     * 结算奖金时要清空的缓存
     * @param $periodNum
     */
    public static function clearCalcBonusCache($periodNum) {
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_STANDARD_BONUS . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum);
    }

    /**
     * 清网络缓存
     */
    public static function clearNetCache(){
        \Yii::$app->redis->del(Cache::USER_NETWORK_PARENTS);
        \Yii::$app->redis->del(Cache::USER_RELATION_PARENTS);
    }

    /**
     * 清空所有临时计算用到的缓存
     * @param $periodNum
     */
    public static function clearAll($periodNum) {
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_INFO . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_BONUS . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_SURPLUS_PERF . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_PERF . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_STANDARD_MONTH_PERF . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_MONTH_SCORE . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_EMP_NUM_PERF . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_STANDARD_BONUS . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_SCORE_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_DEC_ROLE_CONFIG . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum);
    }

    /**
     * 会员信息加入缓存
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return bool
     */
    public static function addUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        // 修复USER_INFO表里面已经没有PERIOD_NUM了
        $allData = UserInfo::findUseDbCalc()->from(UserInfo::tableName(). 'AS UI')->select('UI.USER_ID,U.STATUS')->leftJoin(User::tableName() . 'AS U', 'U.ID=UI.USER_ID')->where('1=1')->orderBy('UI.RELATION_DEEP DESC,UI.ID DESC')->offset($offset)->limit($limit)->asArray()->all();
        if ($allData) {
            foreach ($allData as $data) {
                // 会员ID加入缓存
                $cacheKey = self::REDIS_KEY_PREFIX_USER . $periodNum;
                $value = $data['USER_ID'];
                Yii::$app->redis->rpush($cacheKey, $value);
                if( $data['STATUS'] == 1 ) {
                    $activeCacheKey = self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum;
                    Yii::$app->redis->rpush($activeCacheKey, $value);
                    unset($activeCacheKey);
                }
                unset($data, $cacheKey, $value);
            }
            unset($allData);
            return self::addUsers($periodNum, $offset + $limit, $limit);
        }
        unset($allData);
        return true;
    }

    /**
     * 获取会员从缓存中
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_USER . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 获取激活会员从缓存中
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getActiveUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 加入有业绩的会员
     * @param $userId
     * @param $periodNum
     */
    public static function addHasPerfUsers($userId, $periodNum) {
        // 先从已存在的会员池里面获取
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum, $userId, 1);
            Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum, $userId);
        }
        unset($userId, $periodNum, $isset);
    }

    /**
     * 获取有业绩的会员
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasPerfUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 加入有月业绩的会员
     * @param $userId
     * @param $periodNum
     */
    public static function addHasMonthPerfUsers($userId, $periodNum) {
        // 先从已存在的会员池里面获取
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum, $userId, 1);
            Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum, $userId);
        }
        unset($userId, $periodNum, $isset);
    }

    /**
     * 获取有业绩的会员
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasMonthPerfUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 加入有达标业绩的会员
     * @param $userId
     * @param $periodNum
     */
    public static function addHasStandardMonthPerfUsers($userId, $periodNum) {
        // 先从已存在的会员池里面获取
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL . $periodNum, $userId, 1);
            Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER . $periodNum, $userId);
        }
        unset($userId, $periodNum, $isset);
    }

    /**
     * 获取有达标业绩的会员
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasStandardMonthPerfUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER . $periodNum, $offset, ($offset + $limit - 1));
    }


    /**
     * 加入被报单的会员
     * @param $userId
     * @param $periodNum
     * @param $saveData
     */
    public static function addHasBDUsers($userId, $periodNum, $saveData) {
        // 先从已存在的会员池里面获取
        $data = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId);
        if (!$data) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId, Json::encode($saveData));
            Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum, $userId);
        }
        unset($userId, $periodNum, $saveData, $isset);
    }

    /**
     * 获取被报单的会员信息
     * @param $userId
     * @param $periodNum
     * @return array
     */
    public static function getBDUsersInfo($userId, $periodNum) {
        $data = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId);
        if( !$data ) return [];

        return Json::decode($data, true);
    }

    /**
     * 获取被报单的会员
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasBDUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 加入有收入的会员
     * @param $userId
     * @param $periodNum
     */
    public static function addHasIncomeUsers($userId, $periodNum) {
        // 先从已存在的会员池里面获取
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum, $userId, 1);
            Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum, $userId);
        }

        unset($userId, $periodNum, $isset);
    }

    /**
     * 获取有收入的会员
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasIncomeUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 加入有奖金的会员
     * @param $userId
     * @param $periodNum
     */
    public static function addHasBonusUsers($userId, $periodNum) {
        // 先从已存在的会员池里面获取
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum, $userId, 1);
            Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum, $userId);
        }

        unset($userId, $periodNum, $isset);
    }

    /**
     * 获取有奖金的会员
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 加入荣衔奖的会员
     * @param $userId
     * @param $periodNum
     */
    public static function addHasYcBonusUsers($userId, $periodNum) {
        // 先从已存在的会员池里面获取
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum, $userId, 1);
            Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum, $userId);
        }

        unset($userId, $periodNum, $isset);
    }

    /**
     * 保存荣衔奖奖金
     * @param $userId
     * @param $periodNum
     * @param $empBonus
     * @param array $fromData
     */
    public static function saveYCBonusList($userId, $periodNum, $empBonus, $fromData=[]) {
        $userYcBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum, $userId);

        if( $userYcBonusData ) {
            $ycBonusData = Json::decode($userYcBonusData, true);
            $ycBonusData['empBonus'] += $empBonus;
//            $ycBonusData['fromData'][] = $fromData;
        }else {
            $ycBonusData = [
                'empBonus' => $empBonus,
//                'fromData' => [
//                    $fromData
//                ]
            ];
        }
        unset($userYcBonusData);

        Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum, $userId, Json::encode($ycBonusData));
        unset($ycBonusData, $userId, $periodNum, $empBonus, $fromData);
    }

    /**
     * 返回荣衔奖信息
     * @param $userId
     * @param $periodNum
     * @return array
     */
    public static function getYCBonusList($userId, $periodNum) {
        $userYcBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum, $userId);

        return $userYcBonusData ? Json::decode($userYcBonusData, true) : [];
    }

    /**
     * 获取有荣衔奖的会员
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasYcBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 获取有服务奖的会员
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasFwBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
    }


    /**
     * 加入服务奖的会员
     * @param $userId
     * @param $periodNum
     */
    public static function addHasFwBonusUsers($userId, $periodNum) {
        // 先从已存在的会员池里面获取
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum, $userId, 1);
            Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum, $userId);
        }

        unset($userId, $periodNum, $isset);
    }

    /**
     * 获取有月奖的会员
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasMonthBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
    }


    /**
     * 加入月奖的会员
     * @param $userId
     * @param $periodNum
     */
    public static function addHasMonthBonusUsers($userId, $periodNum) {
        // 先从已存在的会员池里面获取
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum, $userId, 1);
            Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum, $userId);
        }

        unset($userId, $periodNum, $isset);
    }

    /**
     * 保存荣衔奖奖金
     * @param $userId
     * @param $periodNum
     * @param $fwBonus
     * @param array $fromData
     */
    public static function saveFwBonusList($userId, $periodNum, $fwBonus, $fromData=[]) {
        $userFwBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId);

        if( $userFwBonusData ) {
            $fwBonusData = Json::decode($userFwBonusData, true);
            $fwBonusData['fwBonus'] += $fwBonus;
//            $ycBonusData['fromData'][] = $fromData;
        }else {
            $fwBonusData = [
                'fwBonus' => $fwBonus,
//                'fromData' => [
//                    $fromData
//                ]
            ];
        }
        unset($userFwBonusData);

        Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId, Json::encode($fwBonusData));
        unset($fwBonusData, $userId, $periodNum, $empBonus, $fromData);
    }

    /**
     * 返回服务奖信息
     * @param $userId
     * @param $periodNum
     * @return array
     */
    public static function getFwBonusList($userId, $periodNum) {
        $userYcBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId);

        return $userYcBonusData ? Json::decode($userYcBonusData, true) : [];
    }

    /**
     * 加入有复销业绩的会员列表
     * @param $userId
     * @param $periodNum
     */
    public static function addHasScoreUsers($userId, $periodNum) {
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL . $periodNum, $userId, 1);
            $key = self::REDIS_KEY_PREFIX_HAS_SCORE_USER . $periodNum;
            Yii::$app->redis->rpush($key, $userId);
        }
    }

    /**
     * 获取有复销业绩的会员列表
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasScoreUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_SCORE_USER . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 加入有零售个人消费的会员列表
     * @param $userId
     * @param $periodNum
     */
    public static function addHasLSPCSUsers($userId, $periodNum) {
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL . $periodNum, $userId, 1);
            $key = self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER . $periodNum;
            $value = $userId;
            Yii::$app->redis->rpush($key, $value);
        }
    }

    /**
     * 加入有零售消费的会员列表
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasLSPCSUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 有车房补贴比例的人
     * @param $userId
     * @param $periodNum
     */
    public static function addHasCFPercentUsers($userId, $periodNum) {
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL . $periodNum, $userId, 1);
            $key = self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER . $periodNum;
            $value = $userId;
            Yii::$app->redis->rpush($key, $value);
        }
    }

    /**
     * 获取有车房补贴的人
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasCFPercentUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 有领袖分红的人
     * @param $userId
     * @param $periodNum
     */
    public static function addHasLXPercentUsers($userId, $periodNum) {
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL . $periodNum, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL . $periodNum, $userId, 1);
            $key = self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER . $periodNum;
            $value = $userId;
            Yii::$app->redis->rpush($key, $value);
        }
    }

    /**
     * 获取有领袖分红的人
     * @param $periodNum
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasLXPercentUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER . $periodNum, $offset, ($offset + $limit - 1));
    }

    /**
     * 通过创建时间获取指定长度的用户列表
     * @param $userId
     * @param $limit
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getUserListByCreatedAtFromDb($userId, $limit)
    {
        //查找这个人的CREATED_AT
        $user = User::findUseDbCalc()->select('ID,CREATED_AT')->where('ID=:ID', ['ID'=>$userId])->asArray()->one();
        if( !$user ) return [];

        return User::findUseDbCalc()->select('ID,CREATED_AT')->where('ID<:ID AND CREATED_AT<=:CREATED_AT', [
            'ID'=>$userId,
            'CREATED_AT' => $user['CREATED_AT']
        ])->orderBy('CREATED_AT DESC,ID DESC')->limit($limit)->asArray()->all();
    }

    /**
     * 通过创建时间获取指定长度的用户列表
     * @param $userId
     * @param $limit
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAfterUserListByCreatedAtFromDb($userId, $limit)
    {
        $cacheKey = Cache::USER_CREATED_AT_LIST . $limit;
        $value = Yii::$app->redis->hGet($cacheKey, $userId);
        if( $value ) {
            unset($cacheKey);
            return json_decode($value, true);
        }
        unset($value);

        $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('USER_ID>:USER_ID', [
            'USER_ID'=>$userId,
        ])->orderBy('USER_ID ASC')->limit($limit)->asArray()->all();
        if ( $limit == count($list) ) {
            Yii::$app->redis->hSet($cacheKey, $userId, json_encode($list));
        }

        unset($cacheKey);
        return $list;
    }

    /**
     * 获取会员信息从缓存
     * @param $userId
     * @param $periodNum
     * @return array
     * @throws \Exception
     */
    public static function getUserInfo($userId, $periodNum) {
        $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
        $data = Yii::$app->redis->hget($key, $userId);
        if (!$data) {
            $userInfo = UserInfo::findUseDbCalc()->select('USER_ID,USER_NAME,ZC_AMOUNT,ZC_PV,CON_UID,REC_UID,CON_NUM,REC_NUM,NETWORK_DEEP,RELATION_DEEP,STORE_TYPE,UPDATED_AT,SYSTEM_ID')->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
            if( !$userInfo ) {
                throw new \Exception('用户信息表数据不存在,userId:'.$userId);
            }
            // 在获取商城库的会员的级别信息和是否报单中心的会员
            $userShopInfo = User::find()->select('REAL_NAME,LAST_DEC_LV,DEC_LV,EMP_LV,IS_DEC,DEC_ID,DEC_ROLE_ID,STATUS,PERIOD_AT,MOBILE,CREATED_AT,SUB_COM_ID,PROVINCE,CITY,COUNTY,DEC_PROVINCE,DEC_CITY,DEC_COUNTY,IS_DIRECT_SELLER,SUB_COM_LEADER')->where('ID=:ID', [':ID' => $userId])->asArray()->one();
            if ( !$userShopInfo ) {
                throw new \Exception('用户表数据不存在,userId:'.$userId);
            }
            $userInfo['REAL_NAME'] = $userShopInfo['REAL_NAME'];
            //先用last_dec_lv
            $userInfo['DEC_LV'] = $userShopInfo['LAST_DEC_LV'];
            $userInfo['EMP_LV'] = $userShopInfo['EMP_LV'];
            $userInfo['IS_DEC'] = $userShopInfo['IS_DEC'];
            $userInfo['DEC_ID'] = $userShopInfo['DEC_ID'];
            $userInfo['DEC_ROLE_ID'] = $userShopInfo['DEC_ROLE_ID'];
            $userInfo['DEC_PROVINCE'] = $userShopInfo['DEC_PROVINCE'];
            $userInfo['DEC_CITY'] = $userShopInfo['DEC_CITY'];
            $userInfo['DEC_COUNTY'] = $userShopInfo['DEC_COUNTY'];
            $userInfo['STATUS'] = $userShopInfo['STATUS'];
            $userInfo['PERIOD_NUM'] = $userShopInfo['PERIOD_AT'];
            $userInfo['MOBILE'] = $userShopInfo['MOBILE'];
            $userInfo['CREATED_AT'] = $userShopInfo['CREATED_AT'];
            $userInfo['SUB_COM_ID'] = $userShopInfo['SUB_COM_ID'];
            $userInfo['PROVINCE'] = $userShopInfo['PROVINCE'];
            $userInfo['CITY'] = $userShopInfo['CITY'];
            $userInfo['COUNTY'] = $userShopInfo['COUNTY'];
            $userInfo['IS_DIRECT_SELLER'] = $userShopInfo['IS_DIRECT_SELLER'];
            $userInfo['SUB_COM_LEADER'] = $userShopInfo['SUB_COM_LEADER'];
            $userInfo['REC_USER_NAME'] = isset($userInfo['REC_UID']) && $userInfo['REC_UID'] ? Info::getUserNameByUserId($userInfo['REC_UID']) : '';
            $userInfo['REC_REAL_NAME'] = isset($userInfo['REC_UID']) && $userInfo['REC_UID'] ? Info::getUserRealNameByUserId($userInfo['REC_UID']) : '';
            $userInfo['CON_USER_NAME'] = isset($userInfo['CON_UID']) && $userInfo['CON_UID'] ? Info::getUserNameByUserId($userInfo['CON_UID']) : '';
            $userInfo['CON_REAL_NAME'] = isset($userInfo['CON_UID']) && $userInfo['CON_UID'] ? Info::getUserRealNameByUserId($userInfo['CON_UID']) : '';

            unset($userShopInfo);

            if (!$userInfo['DEC_LV']) {
                $userInfo['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
            }
            if (!$userInfo['EMP_LV']) {
                $userInfo['EMP_LV'] = EmployLevel::getDefaultLevelId();
            }
            $data = Json::encode($userInfo);
            Yii::$app->redis->hset($key, $userId, $data);
            unset($userInfo, $key, $userId, $periodNum);
        }
        return $data ? Json::decode($data, true) : [];
    }

    public static function setUserInfo($userId, $periodNum, $userInfo) {
        $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
        $data = Json::encode($userInfo);
        Yii::$app->redis->hset($key, $userId, $data);
        unset($userId, $key, $data, $userInfo, $periodNum);
        return true;
    }

    /**
     * 用户奖金信息
     * @param $userId
     * @param $periodNum
     * @return array|mixed|null|\yii\db\ActiveRecord
     */
    public static function LastUserBonus($userId, $periodNum) {

        $cacheKey = self::REDIS_KEY_PREFIX_USER_BONUS . $periodNum;
        $field = $userId;
        $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
        if ($cacheValue) {
            $value = Json::decode($cacheValue, true);
        } else {
            $value = UserBonus::findUseDbCalc()->select('USER_ID,BONUS_TOTAL')->where('USER_ID=:USER_ID', [
                'USER_ID'=>$userId
            ])->asArray()->one();

            \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
        }
        return $value;
    }

    /**
     * 用户奖金信息
     * @param $userId
     * @param $periodNum
     * @param $yearMonth
     * @return array|mixed|null|\yii\db\ActiveRecord
     */
    public static function lastPeriodMonthCalcBonus($userId, $periodNum, $yearMonth) {

        $cacheKey = self::REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS . $periodNum;
        $field = $userId;
        $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
        if ($cacheValue) {
            $value = Json::decode($cacheValue, true);
        } else {
            $value = CalcBonus::findUseDbCalc()->select('SUM(ORI_BONUS_QY) AS ORI_BONUS_QY_SUM,SUM(ORI_BONUS_VIP) AS ORI_BONUS_VIP_SUM')->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [
                'USER_ID'=>$userId,
                'CALC_MONTH'=>$yearMonth
            ])->asArray()->one();

            \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
        }
        return $value;
    }


    /**
     * 本月往期的复消积分的数据
     * @param $userId
     * @param $periodNum
     * @param $calcYearMonth
     * @return array|mixed|null|\yii\db\ActiveRecord
     * @throws \yii\db\Exception
     */
    public static function monthLastPeriodReconsumePoints($userId, $periodNum, $calcYearMonth) {
        $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum;
        $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
        if ($cacheValue) {
            $value = Json::decode($cacheValue, true);
        } else {
            $value = CalcBonus::findUseDbCalc()->select(["SUM(RECONSUME_POINTS) as RECONSUME_POINTS_SUM"])->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [':USER_ID' => $userId, ':CALC_MONTH'=>$calcYearMonth])->asArray()->one();
            \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
        }
        return $value;
    }

    /**
     * 获取结算时这一期的报单中心级别的配置
     * @param $periodNum
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getDecRoleConfig($periodNum) {
        $key = self::REDIS_KEY_PREFIX_DEC_ROLE_CONFIG . $periodNum;
        Yii::$app->redis->del($key);
        $data = Yii::$app->redis->get($key);
        if (!$data) {
            $data = DecRole::find()->where('1=1')->indexBy('ID')->asArray()->all();
            $data = Json::encode($data);
            Yii::$app->redis->set($key, $data);
        }
        return $data ? Json::decode($data) : [];
    }

    /**
     * 上一期结余业绩
     * @param $userId
     * @param $periodNum
     * @return array|mixed|\yii\db\ActiveRecord|null
     * @throws \yii\db\Exception
     */
    public static function surplusPerf($userId, $periodNum) {
//        $cacheKey = self::REDIS_KEY_PREFIX_SURPLUS_PERF . $periodNum;
//        $field = $userId;
//        $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
//        if ($cacheValue) {
//            $value = Json::decode($cacheValue);
//        } else {
//            $value = UserPerf::getPeriodSurplusPerf($userId);
//            \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
//        }
//        return $value;
        $userPerf = self::userPerf($userId, $periodNum);
        return [
            'SURPLUS_1L' => $userPerf['SURPLUS_1L'],
            'SURPLUS_2L' => $userPerf['SURPLUS_2L'],
            'SURPLUS_3L' => $userPerf['SURPLUS_3L'],
            'SURPLUS_4L' => $userPerf['SURPLUS_4L'],
            'SURPLUS_5L' => $userPerf['SURPLUS_5L'],
            'SURPLUS_1L_ZC' => $userPerf['SURPLUS_1L_ZC'],
            'SURPLUS_2L_ZC' => $userPerf['SURPLUS_2L_ZC'],
            'SURPLUS_3L_ZC' => $userPerf['SURPLUS_3L_ZC'],
            'SURPLUS_4L_ZC' => $userPerf['SURPLUS_4L_ZC'],
            'SURPLUS_5L_ZC' => $userPerf['SURPLUS_5L_ZC'],
            'SURPLUS_1L_FX' => $userPerf['SURPLUS_1L_FX'],
            'SURPLUS_2L_FX' => $userPerf['SURPLUS_2L_FX'],
            'SURPLUS_3L_FX' => $userPerf['SURPLUS_3L_FX'],
            'SURPLUS_4L_FX' => $userPerf['SURPLUS_4L_FX'],
            'SURPLUS_5L_FX' => $userPerf['SURPLUS_5L_FX'],
            'VIP_SURPLUS_1L_ZC' => $userPerf['VIP_SURPLUS_1L_ZC'],
            'VIP_SURPLUS_2L_ZC' => $userPerf['VIP_SURPLUS_2L_ZC'],
            'VIP_SURPLUS_3L_ZC' => $userPerf['VIP_SURPLUS_3L_ZC'],
            'VIP_SURPLUS_4L_ZC' => $userPerf['VIP_SURPLUS_4L_ZC'],
            'VIP_SURPLUS_5L_ZC' => $userPerf['VIP_SURPLUS_5L_ZC'],
            'SURPLUS_LS' => 0,
        ];
    }

    /**
     * 获取repairSurplusPerf
     * @param $userId
     * @return int[]|mixed|null
     */
    public static function getRepairSurplusPerf($userId) {
        $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
        $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
        if ($cacheValue) {
            $value = Json::decode($cacheValue);
        }else {
            $value = [
                'SURPLUS_1L' => 0,
                'SURPLUS_2L' => 0,
                'SURPLUS_3L' => 0,
                'SURPLUS_4L' => 0,
                'SURPLUS_5L' => 0,
            ];
        }

        return $value;
    }

    /**
     * 累加repairSurplusPerf
     * @param $userId
     * @param $surplusPerf
     * @return int[]|mixed|null
     */
    public static function setRepairSurplusPerf($userId, $surplusPerf) {
        $surplusPerfList = self::getRepairSurplusPerf($userId);
        foreach ($surplusPerfList as $key => $perf) {
            $thisPerf = $surplusPerf[$key] ?? 0;
            $surplusPerfList[$key] = $perf + $thisPerf;

            unset($key, $perf, $thisPerf);
        }

        $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
        \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($surplusPerfList));
        self::addHasRepairPerfUsers($userId);

        unset($userId, $cacheKey, $surplusPerf);

        return $surplusPerfList;
    }

    /**
     * 加入有往期结余业绩的会员
     * @param $userId
     */
    public static function addHasRepairPerfUsers($userId) {
        // 先从已存在的会员池里面获取
        $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId);
        if (!$isset) {
            Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId, 1);
            Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $userId);
        }
        unset($userId, $isset);
    }

    /**
     * 获取有往期结余业绩的会员
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public static function getHasRepairPerfUsers($offset = 0, $limit = self::LIMIT) {
        return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $offset, ($offset + $limit - 1));
    }

    /**
     * 修正业绩缓存清空
     */
    public static function clearRepairAllCache() {
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER);
        Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL);
    }

    /**
     * 上一期结余业绩
     * @param $userId
     * @param $periodNum
     * @return array|mixed|\yii\db\ActiveRecord|null
     * @throws \yii\db\Exception
     */
    public static function userPerf($userId, $periodNum) {
        $cacheKey = self::REDIS_KEY_PREFIX_USER_PERF . $periodNum;
        $field = $userId;
        $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
        if ($cacheValue) {
            $value = Json::decode($cacheValue);
        } else {
            $value = UserPerf::getOneUserPerf($userId);
            \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
        }
        return $value;
    }

    /**
     * 当前期数的的业绩
     * @param $userId
     * @param $periodNum
     * @param null $perf
     * @return array|mixed
     */
    public static function nowPeriodPerf($userId, $periodNum, $perf = null) {
        $cacheKey = self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum;
        $field = $userId;
        $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
        if ($cacheValue) {
            $value = Json::decode($cacheValue);
        } else {
            $value = [
                'FX_AMOUNT_CASH' => 0,
                'PV_PCS' => 0,
                'PV_PSS' => 0,//本期的团队业绩
                'PV_PCS_ZC' => 0,//注册
                'PV_PCS_YH' => 0,
                'PV_PCS_ZG' => 0,//增购
                'PV_PCS_LS' => 0,
                'PV_PCS_FX' => 0,
                'PV_PCS_FX_CASH' => 0,//现金复消
                'PV_PCS_FX_POINT' => 0,//积分复消
                'PV_1L' => 0,
                'PV_1L_TOUCH' => 0,
                'PV_1L_ZC' => 0,
                'PV_1L_YH' => 0,
                'PV_1L_ZG' => 0,
                'PV_1L_LS' => 0,
                'PV_1L_FX' => 0,
                'PV_2L' => 0,
                'PV_2L_TOUCH' => 0,
                'PV_2L_ZC' => 0,
                'PV_2L_YH' => 0,
                'PV_2L_ZG' => 0,
                'PV_2L_LS' => 0,
                'PV_2L_FX' => 0,
                'PV_3L' => 0,
                'PV_3L_TOUCH' => 0,
                'PV_3L_ZC' => 0,
                'PV_3L_YH' => 0,
                'PV_3L_ZG' => 0,
                'PV_3L_LS' => 0,
                'PV_3L_FX' => 0,
                'PV_4L' => 0,
                'PV_4L_TOUCH' => 0,
                'PV_4L_ZC' => 0,
                'PV_4L_YH' => 0,
                'PV_4L_ZG' => 0,
                'PV_4L_LS' => 0,
                'PV_4L_FX' => 0,
                'PV_5L' => 0,
                'PV_5L_TOUCH' => 0,
                'PV_5L_ZC' => 0,
                'PV_5L_YH' => 0,
                'PV_5L_ZG' => 0,
                'PV_5L_LS' => 0,
                'PV_5L_FX' => 0,
                'PV_LS_TOUCH' => 0,
                'SURPLUS_1L' => 0,
                'SURPLUS_2L' => 0,
                'SURPLUS_3L' => 0,
                'SURPLUS_4L' => 0,
                'SURPLUS_5L' => 0,
                'SURPLUS_1L_ZC' => 0,
                'SURPLUS_2L_ZC' => 0,
                'SURPLUS_3L_ZC' => 0,
                'SURPLUS_4L_ZC' => 0,
                'SURPLUS_5L_ZC' => 0,
                'SURPLUS_1L_FX' => 0,
                'SURPLUS_2L_FX' => 0,
                'SURPLUS_3L_FX' => 0,
                'SURPLUS_4L_FX' => 0,
                'SURPLUS_5L_FX' => 0,
                'SURPLUS_LS' => 0,
            ];
        }
        if ($perf !== null) {
            foreach ($perf as $key => $pv) {
                if (strpos($key, 'SURPLUS') !== false) {
                    $value[$key] = $pv;
                } else {
                    $value[$key] = $pv + $value[$key];
                }
            }
            Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
        }
        return $value;
    }

    /**
     * 上个月的业绩
     * @param $userId
     * @param $periodNum
     * @return array|mixed|null|\yii\db\ActiveRecord
     */
    public static function lastMonthPerf($userId, $periodNum) {
        $period = Period::instance();
        $last = $period->getLastMonth($periodNum);
        $lastYearMonth = $last['yearMonth'];
        $cacheKey = self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum;
        $field = $userId;
        $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
        if ($cacheValue) {
            $value = Json::decode($cacheValue);
        } else {
            $value = PerfMonth::getMonthPerf($lastYearMonth, $userId);
            \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
        }
        return $value;
    }

    /**
     * 当前月的业绩
     * @param $userId
     * @param $periodNum
     * @param $perf
     * @return array|mixed
     */
    public static function nowMonthPerf($userId, $periodNum, $perf = null) {
        $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum;
        $field = $userId;
        $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
        if ($cacheValue) {
            $value = Json::decode($cacheValue);
        } else {
            $baseInfo = self::getUserInfo($userId, $periodNum);
            $value = [
                'USER_ID' => $userId,
                'FX_AMOUNT_CASH' => 0,
                'PV_PCS' => 0,
                'PV_PCS_FX' => 0,
                'PV_PSS' => 0,
                'PV_1L' => 0,
                'PV_2L' => 0,
                'PV_3L' => 0,
                'PV_4L' => 0,
                'PV_5L' => 0,
                'VIP_PV_1L_ZC' => 0,
                'VIP_PV_2L_ZC' => 0,
                'VIP_PV_3L_ZC' => 0,
                'VIP_PV_4L_ZC' => 0,
                'VIP_PV_5L_ZC' => 0,
                'PV_1L_TOTAL' => 0,
                'PV_2L_TOTAL' => 0,
                'PV_3L_TOTAL' => 0,
                'PV_4L_TOTAL' => 0,
                'PV_5L_TOTAL' => 0,
                'PV_PSS_TOTAL' => 0,
                'DEC_LEVEL' => $baseInfo['DEC_LV'],
                'EMP_LEVEL' => EmployLevel::getDefaultLevelId(),
                'CF_PERCENT' => 0,
                'LX_PERCENT' => 0,
                'FX_STATUS' => PerfMonth::NEXT_MONTH_FX_FALSE,
            ];
        }
        if ($perf !== null) {
            foreach ($perf as $key => $item) {
                $value[$key] = $item;
            }
        }
        Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
        return $value;
    }

    /**
     * 当前期数的的业绩
     * @param $userId
     * @param $periodNum
     * @param null $perf
     * @return array|mixed
     */
    public static function nowStandardMonthPerf($userId, $periodNum, $perf = null) {
        $cacheKey = self::REDIS_KEY_PREFIX_NOW_STANDARD_MONTH_PERF . $periodNum;
        $field = $userId;
        $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
        if ($cacheValue) {
            $value = Json::decode($cacheValue);
        } else {
            $value = [
                'AMOUNT_PCS' => 0,
                'AMOUNT_PSS' => 0
            ];
        }
        if ($perf !== null) {
            foreach ($perf as $key => $pv) {
                $value[$key] = $pv + $value[$key];
            }
            Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
        }
        return $value;
    }

    /**
     * 当前月的积分
     * @param $userId
     * @param $periodNum
     * @param $perf
     * @return array|mixed
     */
    public static function nowMonthScore($userId, $periodNum, $perf = null) {
        $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_SCORE . $periodNum;
        $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
        if ($cacheValue) {
            $value = Json::decode($cacheValue);
        } else {
            $value = [
                'USER_ID' => $userId,
                'BASE_SCORE' => 0,
                'LEVEL_SCORE' => 0,
                'UPGRADE_SCORE' => 0,
//                'TOTAL_SCORE' => 0,
            ];
        }
        unset($cacheValue);
        if ($perf !== null) {
            foreach ($perf as $key => $item) {
                $value[$key] = $item;
//                $value['TOTAL_SCORE'] += $item;
                unset($key, $item);
            }
            Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
        }

        unset($userId, $periodNum, $perf, $cacheKey);
        return $value;
    }

    /**
     * 获取直推的所有子会员
     * @param $userId
     * @param $periodNum
     * @return array
     */
    public static function getChildrenOneDeepFromRedis($userId, $periodNum){
        $key = self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum;
        $data = Yii::$app->redis->hget($key, $userId);
        if(!$data){
            $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('REC_UID=:REC_UID', [
                'REC_UID' => $userId
            ])->asArray()->all();
            $data = Json::encode($list);
            unset($list);
            Yii::$app->redis->hset($key, $userId, $data);
        }
        unset($key, $userId, $periodNum);
        return $data ? Json::decode($data, true) : [];
    }

    /**
     * 每个区有多少个什么级别的数量
     * @param $userId
     * @param $periodNum
     * @param null $levelNumArr
     * @return array|mixed
     */
    public static function hasEmpLevelNum($userId,  $periodNum, $levelNumArr = null) {
        $key = self::REDIS_KEY_PREFIX_EMP_NUM_PERF . $periodNum;
        $field = $userId;
        $cacheValue = Yii::$app->redis->hget($key, $field);

        if ($cacheValue) {
            $value = Json::decode($cacheValue);
        } else {
            $allEmpLevel = EmployLevel::getFromCache();
            $value = [];
            $relationList = self::getChildrenOneDeepFromRedis($userId, $periodNum);
            foreach ($relationList as $relationData) {
                foreach ($allEmpLevel as $level) {
                    // 从月度业绩表中找到所有下级的各个部门的数量
                    $value[$relationData['USER_ID']][$level['ID']] = 0;
                }
            }
        }
        if ($levelNumArr != null) {
            foreach ($levelNumArr as $departUserId => $item) {
                foreach ($item as $levelID => $num) {
                    if( isset($value[$departUserId][$levelID]) ) {
                        $value[$departUserId][$levelID] += $num;
                    }else {
                        $value[$departUserId][$levelID] = $num;
                    }
                }
            }
        }
        Yii::$app->redis->hset($key, $field, Json::encode($value));

        if ($levelNumArr == null) return $value;
    }

    /**
     * 会员是否本月注册
     * @param $userId
     * @param $periodNum
     * @return bool
     * @throws \yii\db\Exception
     */
    public static function isMonthJoinFromRedis($userId, $periodNum) {
        $period = Period::instance();
        $calcYearMonth = $period->getYearMonth($periodNum);
        $userInfo = self::getUserInfo($userId, $periodNum);
        $addYearMonth = $period->getYearMonth($userInfo['PERIOD_NUM']);
        //当月注册
        if ($addYearMonth == $calcYearMonth) return true;
        //上月注册
        $calcLastYearMonth = $period->getLastMonth($periodNum);
        if ($addYearMonth == $calcLastYearMonth['yearMonth']) return true;
        // 未来注册
        if ($addYearMonth > $calcYearMonth) return true;
        return false;
    }

    /**
     * 荣衔奖最大比例
     * @param $userId
     * @param $periodNum
     * @param int $percent
     * @return float|int
     */
    public static function ycMaxBonusPercent($userId, $periodNum, $percent=0) {
        $cacheKey = self::REDIS_KEY_PREFIX_YC_BONUS . $periodNum;
        $value = Yii::$app->redis->hget($cacheKey, $userId);
        $maxPercent = floatval($value)>0 ? floatval($value) : 0;
        if ( $percent > 0 && $percent > $maxPercent ) {
            $maxPercent = $percent;
            unset($periodNum, $percent);
            Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);

            return $maxPercent;
        }

        unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
        return $maxPercent;
    }

    /**
     * 服务奖最大比例
     * @param $userId
     * @param $periodNum
     * @param int $percent
     * @return float|int
     */
    public static function fwMaxBonusPercent($userId, $periodNum, $percent=0) {
        $cacheKey = self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum;
        $value = Yii::$app->redis->hget($cacheKey, $userId);
        $maxPercent = floatval($value)>0 ? floatval($value) : 0;
        if ( $percent > 0 && $percent > $maxPercent ) {
            $maxPercent = $percent;
            unset($periodNum, $percent);
            Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);

            return $maxPercent;
        }

        unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
        return $maxPercent;
    }

    /**
     * 奖金缓存
     * @param $userId
     * @param $periodNum
     * @param string $bonusType
     * @param float $oriBonus
     * @param array $deductData
     * @return array|mixed
     */
    public static function bonus($userId, $periodNum, $bonusType = null, $oriBonus = 0.00, $deductData=[], $fromMeans='') {
        $cacheKey = self::REDIS_KEY_PREFIX_BONUS . $periodNum;
        $value = [
            'BONUS_BD' => 0,
            'BONUS_TG' => 0,
            'BONUS_XF' => 0,
            'BONUS_YJ' => 0,
            'BONUS_GX' => 0,
            'BONUS_GL' => 0,
            'BONUS_QY' => 0,
            'BONUS_YC' => 0,
            'BONUS_VIP' => 0,
            'BONUS_BS' => 0,
            'BONUS_QUARTER' => 0,
            'BONUS_YC_EXTRA' => 0,
            'ORI_BONUS_BD' => 0,
            'ORI_BONUS_BS' => 0,
            'BONUS_BS_MNT' => 0,
            'BONUS_BS_ABBR' => 0,
            'ORI_BONUS_BS_MNT' => 0,
            'ORI_BONUS_BS_ABBR' => 0,
            'ORI_BONUS_QUARTER' => 0,
            'ORI_BONUS_TG' => 0,
            'ORI_BONUS_XF' => 0,
            'ORI_BONUS_YJ' => 0,
            'ORI_BONUS_YJ_BD' => 0,
            'ORI_BONUS_YJ_FX' => 0,
            'ORI_BONUS_GX' => 0,
            'ORI_BONUS_GL' => 0,
            'ORI_BONUS_GL_BD' => 0,
            'ORI_BONUS_GL_FX' => 0,
            'ORI_BONUS_QY' => 0,
            'ORI_BONUS_QY_BD' => 0,
            'ORI_BONUS_QY_FX' => 0,
            'ORI_BONUS_YC' => 0,
            'ORI_BONUS_VIP' => 0,
            'ORI_BONUS_YC_EXTRA' => 0,
//            'INCOME_TG_TOTAL' => 0,
//            'INCOME_YJ_TOTAL' => 0,
//            'INCOME_QY_TOTAL' => 0,
            'INCOME_TOTAL' => 0,
            'BONUS_TOTAL' => 0,
            'RECONSUME_POINTS' => 0,
            'MANAGE_TAX' => 0,
            'ORI_CAPPED_BONUS_QY' => 0,// 团队奖,封顶前金额

            //没有用到的
            'BONUS_FX' => 0,
            'BONUS_LS' => 0,
            'BONUS_CF' => 0,
            'BONUS_LX' => 0,
            'BONUS_HB' => 0,
            'BONUS_BT' => 0,
            'BONUS_BT_PROD' => 0,
            'BONUS_BT_TOOL' => 0,
            'DEDUCT_ZR' => 0,
            'BONUS_FL' => 0,
        ];
        // 从 redis 中获取当前的结果
        $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
        if ($cacheValue) {
            $cacheValue = Json::decode($cacheValue);
            $value = $cacheValue;
        }
        unset($cacheValue);
        if ($oriBonus > 0) {
            $oriBonusType = sprintf('ORI_%s', $bonusType);
            $value[$oriBonusType] += $oriBonus;
            if( $fromMeans !== '' ) {
                $oriBonusMeansType = sprintf('ORI_%s_%s', $bonusType, $fromMeans);
                $value[$oriBonusMeansType] += $oriBonus;
            }

            if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
                if( $deductData ) {
                    $value[$bonusType] += $deductData['surplus'];
                    $value['RECONSUME_POINTS'] += $deductData['reConsumePoints'];
                    $value['MANAGE_TAX'] += $deductData['manageTax'];
                }else {
                    $value[$bonusType] += $oriBonus;
                }
                $value['BONUS_TOTAL'] += $oriBonus;
            }

            if( in_array($bonusType, self::INCOME_BONUS_LIST) ) {
                self::addHasIncomeUsers($userId, $periodNum);
//                if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
//                    $value['INCOME_TOTAL'] += $oriBonus;
//                }
            }

            Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
            unset($oriBonusType);
            self::addHasBonusUsers($userId, $periodNum);
        }
        unset($userId, $periodNum, $bonusType, $oriBonus, $deductData, $cacheKey);
        return $value;
    }

    public static function standardBonus($userId, $periodNum, $bonus=0.00) {
        $cacheKey = self::REDIS_KEY_PREFIX_STANDARD_BONUS . $periodNum;

        $value = 0.00;
        if( $bonus > 0 ) {
            Yii::$app->redis->hset($cacheKey, $userId, $bonus);
            $value = $bonus;
            //加入有奖金的会员中
            self::addHasBonusUsers($userId, $periodNum);
        }else {
            $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
            if ($cacheValue) {
                $value = $cacheValue;
            }
            unset($cacheValue);
        }

        unset($cacheKey, $userId, $periodNum, $bonus);
        return $value;
    }

    public static function tourismBonus($userId, $periodNum, $bonus = 0.00) {
        $cacheKey = self::REDIS_KEY_PREFIX_TOURISM_BONUS . $periodNum;

        $value = 0.00;
        if( $bonus > 0 ) {
            Yii::$app->redis->hset($cacheKey, $userId, $bonus);
            $value = $bonus;
            //加入有奖金的会员中
            self::addHasBonusUsers($userId, $periodNum);
        }else {
            $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
            if ($cacheValue) {
                $value = $cacheValue;
            }
            unset($cacheValue);
        }

        unset($cacheKey, $userId, $periodNum, $bonus);
        return $value;
    }

    public static function villaBonus($userId, $periodNum, $bonus = 0.00) {
        $cacheKey = self::REDIS_KEY_PREFIX_VILLA_BONUS . $periodNum;

        $value = 0.00;
        if( $bonus > 0 ) {
            Yii::$app->redis->hset($cacheKey, $userId, $bonus);
            $value = $bonus;
            //加入有奖金的会员中
            self::addHasBonusUsers($userId, $periodNum);
        }else {
            $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
            if ($cacheValue) {
                $value = $cacheValue;
            }
            unset($cacheValue);
        }

        unset($cacheKey, $userId, $periodNum, $bonus);
        return $value;
    }

    public static function garageBonus($userId, $periodNum, $bonus = 0.00) {
        $cacheKey = self::REDIS_KEY_PREFIX_GARAGE_BONUS . $periodNum;

        $value = 0.00;
        if( $bonus > 0 ) {
            Yii::$app->redis->hset($cacheKey, $userId, $bonus);
            $value = $bonus;
            //加入有奖金的会员中
            self::addHasBonusUsers($userId, $periodNum);
        }else {
            $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
            if ($cacheValue) {
                $value = $cacheValue;
            }
            unset($cacheValue);
        }

        unset($cacheKey, $userId, $periodNum, $bonus);
        return $value;
    }

    /**
     * 设置车房总系数
     * @param $periodNum
     * @param $value
     */
    public static function setCFTotalPercent($periodNum, $value) {
        Yii::$app->redis->set(self::REDIS_KEY_PREFIX_CF_TOTAL_PERCENT . $periodNum, $value);
    }

    /**
     * 获得车房总系数
     * @param $periodNum
     * @return mixed
     */
    public static function getCFTotalPercent($periodNum) {
        return Yii::$app->redis->get(self::REDIS_KEY_PREFIX_CF_TOTAL_PERCENT . $periodNum);
    }

    /**
     * 设置领袖总系数
     * @param $periodNum
     * @param $value
     */
    public static function setLXTotalPercent($periodNum, $value) {
        Yii::$app->redis->set(self::REDIS_KEY_PREFIX_LX_TOTAL_PERCENT . $periodNum, $value);
    }

    /**
     * 获得领袖总系数
     * @param $periodNum
     * @return mixed
     */
    public static function getLXTotalPercent($periodNum) {
        return Yii::$app->redis->get(self::REDIS_KEY_PREFIX_LX_TOTAL_PERCENT . $periodNum);
    }


    /**
     * 添加上2代共享奖关系
     * @param $bonusUserId
     * @param $fromUserId
     * @param $periodNum
     * @param $validDeep
     * @return  boolean
     */
    public static function addShareBonusOneRelation($bonusUserId, $fromUserId, $periodNum, $validDeep) {
        if ($validDeep == 1) {
            $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum;
        }else {
            $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum;
        }
        $fromUserDataJson = Yii::$app->redis->hget($cacheKey, $fromUserId);

        $fromUserData = [];
        if( $fromUserDataJson ) {
            $fromUserData = Json::decode($fromUserDataJson, true);
        }
        unset($fromUserDataJson);

        $fromUserData[] = $bonusUserId;

        Yii::$app->redis->hset($cacheKey, $fromUserId, Json::encode($fromUserData));

        unset($fromUserData, $bonusUserId, $fromUserId, $periodNum);

        return true;
    }

    /**
     * 获取上2代共享奖关系
     * @param $fromUserId
     * @param $periodNum
     * @param $validDeep
     * @return array
     */
    public static function getShareBonusOneRelation($fromUserId, $periodNum, $validDeep) {
        if ($validDeep == 1) {
            $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum;
        }else {
            $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum;
        }
        $fromUserDataJson = Yii::$app->redis->hget($cacheKey, $fromUserId);

        $bonusUserData = [];
        if( $fromUserDataJson ) {
            $bonusUserData = Json::decode($fromUserDataJson, true);
        }
        unset($fromUserDataJson);

        return $bonusUserData;
    }
}