<?php

namespace common\models;

use common\helpers\Cache;
use common\helpers\Tool;
use common\helpers\user\Info;
use Yii;
use yii\data\Pagination;
use yii\helpers\Json;
use common\libs\logging\operate\valueType\Config as ValueTypeConfig;

/**
 * This is the model class for table "{{%USER}}".
 *
 * @property string $ID
 * @property string $USER_NAME 帐号
 * @property string $PASSWORD_HASH 登录密码
 * @property string $PAY_PASSWORD 支付密码
 * @property int $NATION 民族
 * @property string $REAL_NAME 姓名
 * @property string $ID_CARD 身份证号
 * @property int $ID_TYPE 证件类型
 * @property string $MOBILE 手机号
 * @property string $ADDRESS 身份证地址
 * @property string $ID_IMAGE 证件图片
 * @property string $OPEN_BANK 开户行
 * @property string $BANK_ADDRESS 银行地址
 * @property string $BANK_NO 银行卡号
 * @property int $BANK_PROVINCE 银行省份
 * @property int $BANK_CITY 银行城市
 * @property int $BANK_COUNTY 银行县区
 * @property string $SPOUSE_NAME 配偶姓名
 * @property string $SPOUSE_IDCARD 配偶证件号
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 更新时间
 * @property int $STATUS 状态
 * @property int $DEC_CLOSED 是否关闭报单功能
 * @property int $DEC_CLOSED_AT 关闭时间
 * @property string $DEC_LV 报单级别
 * @property string $EMP_LV 聘级
 * @property int $PROVINCE 省份
 * @property int $CITY 城市
 * @property int $COUNTY 县区
 * @property string $TEL 座机
 * @property string $SUB_COM_ID 子公司ID
 * @property string $AVATAR 头像
 * @property int $DELETED 是否删除
 * @property int $DELETED_AT 删除时间
 * @property int $IS_DEC 是否为报单中心
 * @property int $IS_ATLAS 是否显示图谱
 * @property int $IS_RECHARGE 是否显示充值
 * @property string $DEC_ID 报单中心ID
 * @property string $BIRTHDAY 生日
 * @property string $DEC_ROLE_ID 报单中心级别
 * @property int $PERIOD_AT 期数
 * @property int $DEC_PROVINCE 报单中心所属的省份
 * @property int $DEC_CITY 报单中心所属的城市
 * @property int $DEC_COUNTY 报单中心所属的县区
 * @property int $IS_UNION 是否为点位合作
 * @property int $STATUS_AT 状态更改时间
 * @property int $VERIFIED 是否认证
 * @property int $VERIFIED_AT 认证时间
 * @property int $ALLOW_LOGIN 允许登录
 * @property int $NOT_OPERATING 不运作
 * @property int $REG_FROM 注册类型
 * @property string $ID_CARD_PREFIX 身份证前缀
 * @property string $SEX 性别
 * @property string $DEC_ACCOUNT_OLD 老系统中的报单中心编号
 * @property int $BANK_UPDATED_AT 银行信息更新时间
 * @property int $IS_DIRECT_SELLER 是否为直销员
 * @property int $DEC_LV_UPDATED_AT 报单级别更新时间
 * @property int $DEC_LV_UPDATED_PERIOD 报单级别更新期数
 * @property string $DEC_ADDRESS 报单中心详细地址
 * @property string $DEC_PHONE 报单中心电话
 * @property string $GUARANTOR 担保人编号
 * @property string $GUARANTOR_NAME 担保人姓名
 * @property int $PART_FUNC_CLOSED 部分功能开启
 * @property int $LAST_DEC_LV_UPDATED_PERIOD 上次报单级别更新期数
 * @property string $USER_CREATOR 创建人
 * @property string $USER_UPDATER 修改人
 * @property int $LAST_DEC_LV_UPDATED_AT 上次报单级别更新时间
 * @property int $DEC_CREATED_AT 成为报单中心时间
 * @property string $PART_FUNC_CLOSED_REMARK 部分功能关闭原因
 * @property int $DEC_CREATED_PERIOD 成为报单中心期数
 * @property int $PASSWORD_CHANGED 是否修改过密码
 * @property int $SUB_COM_LEADER 是否为分公司领导
 * @property string $ZG_UPGRADE_PV 增购升级PV
 * @property string $APP_CLIENT_ID APP设备ID
 * @property int $READ_AGREEMENT 已读协议
 * @property string $LAST_DEC_LV 上次的报单级别
 * @property string $BONUS_APP_CLIENT_ID 结算APP设备ID
 * @property int $IS_FIRST_OPEN 首次开通
 * @property int $IS_MODIFY_PASSWORD 是否修改密码
 * @property int $IS_STUDIO 是否是工作室
 * @property int $IS_STUDIO_CODE 工作室编号
 * @property int $IS_STUDIO_DATE 工作室时间
 */
class User extends \common\components\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%USER}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['USER_NAME', 'PASSWORD_HASH', 'PAY_PASSWORD', 'REAL_NAME', 'ID_CARD', 'AVATAR'], 'required'],
            [['NATION', 'ID_TYPE', 'BANK_PROVINCE', 'BANK_CITY', 'BANK_COUNTY', 'CREATED_AT', 'UPDATED_AT', 'STATUS', 'DEC_CLOSED', 'DEC_CLOSED_AT', 'PROVINCE', 'CITY', 'COUNTY', 'DELETED', 'DELETED_AT', 'IS_DEC', 'IS_ATLAS', 'IS_RECHARGE','IS_STUDIO',  'PERIOD_AT', 'DEC_PROVINCE', 'DEC_CITY', 'DEC_COUNTY', 'IS_UNION', 'STATUS_AT', 'VERIFIED', 'VERIFIED_AT', 'ALLOW_LOGIN', 'NOT_OPERATING', 'REG_FROM', 'BANK_UPDATED_AT', 'IS_DIRECT_SELLER', 'DEC_LV_UPDATED_AT', 'DEC_LV_UPDATED_PERIOD', 'PART_FUNC_CLOSED', 'LAST_DEC_LV_UPDATED_PERIOD', 'LAST_DEC_LV_UPDATED_AT', 'DEC_CREATED_AT', 'DEC_CREATED_PERIOD', 'PASSWORD_CHANGED', 'SUB_COM_LEADER', 'READ_AGREEMENT', 'IS_FIRST_OPEN', 'IS_MODIFY_PASSWORD'], 'integer'],
            [['ZG_UPGRADE_PV'], 'number'],
            [['ID', 'OPEN_BANK', 'BANK_NO', 'DEC_LV', 'EMP_LV', 'SUB_COM_ID', 'DEC_ID', 'DEC_ROLE_ID', 'ID_CARD_PREFIX', 'SEX', 'DEC_PHONE', 'GUARANTOR', 'USER_CREATOR', 'USER_UPDATER', 'LAST_DEC_LV'], 'string', 'max' => 32],
            [['USER_NAME', 'SPOUSE_NAME', 'TEL'], 'string', 'max' => 16],
            [['PASSWORD_HASH', 'PAY_PASSWORD', 'ID_IMAGE', 'BANK_ADDRESS', 'AVATAR'], 'string', 'max' => 255],
            [['REAL_NAME', 'APP_CLIENT_ID', 'BONUS_APP_CLIENT_ID'], 'string', 'max' => 128],
            [['ID_CARD', 'SPOUSE_IDCARD'], 'string','min' => 18, 'max' => 18],
            [['MOBILE'], 'string','min' => 11, 'max' => 11],
            [['ADDRESS', 'DEC_ADDRESS'], 'string', 'max' => 2000],
            [['DEC_ACCOUNT_OLD', 'GUARANTOR_NAME'], 'string', 'max' => 64],
            [['PART_FUNC_CLOSED_REMARK'], 'string', 'max' => 4000],
            [['USER_NAME'], 'unique'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_NAME' => '帐号',
            'PASSWORD_HASH' => '登录密码',
            'PAY_PASSWORD' => '支付密码',
            'NATION' => '民族',
            'REAL_NAME' => '姓名',
            'ID_CARD' => '身份证号',
            'ID_TYPE' => '证件类型',
            'MOBILE' => '手机号',
            'ADDRESS' => '身份证地址',
            'ID_IMAGE' => '证件图片',
            'OPEN_BANK' => '开户行',
            'BANK_ADDRESS' => '银行地址',
            'BANK_NO' => '银行卡号',
            'BANK_PROVINCE' => '银行省份',
            'BANK_CITY' => '银行城市',
            'BANK_COUNTY' => '银行县区',
            'SPOUSE_NAME' => '配偶姓名',
            'SPOUSE_IDCARD' => '配偶证件号',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
            'STATUS' => '状态',
            'DEC_CLOSED' => '是否关闭报单功能',
            'DEC_CLOSED_AT' => '关闭时间',
            'DEC_LV' => '报单级别',
            'EMP_LV' => '聘级',
            'PROVINCE' => '省份',
            'CITY' => '城市',
            'COUNTY' => '县区',
            'TEL' => '座机',
            'SUB_COM_ID' => '子公司ID',
            'AVATAR' => '头像',
            'DELETED' => '是否删除',
            'DELETED_AT' => '删除时间',
            'IS_DEC' => '是否为报单中心',
            'IS_ATLAS' => '是否显示图谱',
            'IS_RECHARGE' => '是否显示充值',
            'DEC_ID' => '报单中心ID',
            'BIRTHDAY' => '生日',
            'DEC_ROLE_ID' => '报单中心级别',
            'PERIOD_AT' => '期数',
            'DEC_PROVINCE' => '报单中心所属的省份',
            'DEC_CITY' => '报单中心所属的城市',
            'DEC_COUNTY' => '报单中心所属的县区',
            'IS_UNION' => '是否为点位合作',
            'STATUS_AT' => '状态更改时间',
            'VERIFIED' => '是否认证',
            'VERIFIED_AT' => '认证时间',
            'ALLOW_LOGIN' => '允许登录',
            'NOT_OPERATING' => '不运作',
            'REG_FROM' => '注册类型',
            'ID_CARD_PREFIX' => '身份证前缀',
            'SEX' => '性别',
            'DEC_ACCOUNT_OLD' => '老系统中的报单中心编号',
            'BANK_UPDATED_AT' => '银行信息更新时间',
            'IS_DIRECT_SELLER' => '是否为直销员',
            'DEC_LV_UPDATED_AT' => '报单级别更新时间',
            'DEC_LV_UPDATED_PERIOD' => '报单级别更新期数',
            'DEC_ADDRESS' => '报单中心详细地址',
            'DEC_PHONE' => '报单中心电话',
            'GUARANTOR' => '担保人编号',
            'GUARANTOR_NAME' => '担保人姓名',
            'PART_FUNC_CLOSED' => '部分功能开启',
            'LAST_DEC_LV_UPDATED_PERIOD' => '上次报单级别更新期数',
            'USER_CREATOR' => '创建人',
            'USER_UPDATER' => '修改人',
            'LAST_DEC_LV_UPDATED_AT' => '上次报单级别更新时间',
            'DEC_CREATED_AT' => '成为报单中心时间',
            'PART_FUNC_CLOSED_REMARK' => '部分功能关闭原因',
            'DEC_CREATED_PERIOD' => '成为报单中心期数',
            'PASSWORD_CHANGED' => '是否修改过密码',
            'SUB_COM_LEADER' => '是否为分公司领导',
            'ZG_UPGRADE_PV' => '增购升级PV',
            'APP_CLIENT_ID' => 'APP设备ID',
            'READ_AGREEMENT' => '已读协议',
            'LAST_DEC_LV' => '上次的报单级别',
            'BONUS_APP_CLIENT_ID' => '结算APP设备ID',
            'IS_FIRST_OPEN' => '首次开通',
            'IS_MODIFY_PASSWORD' => '是否修改密码',
            'IS_STUDIO' => '是否是工作室',
            'IS_STUDIO_CODE' => '工作室编号',
            'IS_STUDIO_DATE' => '工作室日期',
        ];
    }

    public function getUserInfo()
    {
        return $this->hasOne(UserInfo::class, ['USER_ID' => 'ID']);
    }

    public static function checkUser($userName){
        return self::find()->select('ID')->where('USER_NAME=:USER_NAME', [':USER_NAME'=>$userName])->asArray()->one();
    }

    /**
     * 获取会员基本信息
     * @param $userId
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getBaseInfo($userId){
        return static::findUseSlaves()->where('ID=:ID', [':ID'=>$userId])->asArray()->one();
    }

    /**
     * 获取用户基本信息从redis里面
     * @param $userId
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getBaseInfoFromRedis($userId){
        $userInfo = Yii::$app->redis->hget(Cache::USER_INFO_KEY, $userId);
        if(!$userInfo){
            $userInfo = static::find()->select('ID,USER_NAME,NATION,REAL_NAME,ID_CARD,ID_TYPE,MOBILE,ADDRESS,STATUS,DEC_LV,LAST_DEC_LV,EMP_LV,DEC_CLOSED,AVATAR,OPEN_BANK,BANK_ADDRESS,BANK_NO,VERIFIED,IS_UNION,IS_DEC,IS_ATLAS,IS_RECHARGE,IS_STUDIO,DEC_ROLE_ID,PROVINCE,CITY,COUNTY')->where('ID=:ID', [':ID'=>$userId])->asArray()->one();
            Yii::$app->redis->hset(Cache::USER_INFO_KEY, $userId, Json::encode($userInfo));
        } else {
            $userInfo = Json::decode($userInfo);
        }
        return $userInfo;
    }

    /**
     * 更新缓存
     * @param $userId
     * @return mixed
     */
    public static function updateBaseInfoToRedis($userId){
        $userInfo = static::find()->select('ID,USER_NAME,NATION,REAL_NAME,ID_CARD,ID_TYPE,MOBILE,ADDRESS,STATUS,DEC_LV,LAST_DEC_LV,EMP_LV,DEC_CLOSED,AVATAR,OPEN_BANK,BANK_ADDRESS,BANK_NO,VERIFIED,IS_UNION,IS_DEC,IS_ATLAS,IS_RECHARGE,IS_STUDIO,DEC_ROLE_ID,PROVINCE,CITY,COUNTY')->where('ID=:ID', [':ID'=>$userId])->asArray()->one();
        return Yii::$app->redis->hset(Cache::USER_INFO_KEY, $userId, Json::encode($userInfo));
    }

    /**
     * @param $userId
     * 删除用户信息缓存
     */
    public static function deleteBaseInfoFromRedis($userId) {
        Yii::$app->redis->hdel(Cache::USER_INFO_KEY, $userId);
    }

    /**
     * 批量删除用户
     * @return void
     */
    public static function deleteAllBaseInfoFromRedis() {
        Yii::$app->redis->del(Cache::USER_INFO_KEY);
    }

    /**
     * 获取会员的部分信息并对敏感信息加密
     * @param $userId
     * @return array
     */
    public static function getEnCodeInfo($userId){
        $userInfo = self::getBaseInfoFromRedis($userId);
        return [
            'ID' => $userId,
            'USER_NAME' => $userInfo['USER_NAME'],
            'NATION' => \Yii::$app->params['nation'][$userInfo['NATION']]['name']??'',
            'NATION_ID' => \Yii::$app->params['nation'][$userInfo['NATION']]['id']??'',
            'REAL_NAME' => $userInfo['REAL_NAME'],
            'ID_CARD' => $userInfo['ID_CARD'],
//            'ID_CARD' => Tool::hideIdCard($userInfo['ID_CARD']),
            'MOBILE' => $userInfo['MOBILE'],
//            'MOBILE' => Tool::hideMobile($userInfo['MOBILE']),
            'AVATAR' => $userInfo['AVATAR'],
            'VERIFIED' => $userInfo['VERIFIED'],
            'IS_UNION' => $userInfo['IS_UNION'],
            'IS_DEC' => $userInfo['IS_DEC'],
            'IS_ATLAS' => $userInfo['IS_ATLAS'],
            'IS_RECHARGE' => $userInfo['IS_RECHARGE'],
            'IS_STUDIO' => $userInfo['IS_STUDIO'],
            'DEC_ROLE_ID' => $userInfo['DEC_ROLE_ID'],
            'OPEN_BANK' => $userInfo['OPEN_BANK'],
            'BANK_ADDRESS' => $userInfo['BANK_ADDRESS'],
            'DEC_LV' => $userInfo['DEC_LV'],
            'EMP_LV' => $userInfo['EMP_LV'],
            'PROVINCE' => $userInfo['PROVINCE'],
            'CITY' => $userInfo['CITY'],
            'COUNTY' => $userInfo['COUNTY'],
            'BANK_NO' => $userInfo['BANK_NO'],
//            'BANK_NO' => Tool::hideBankNo($userInfo['BANK_NO']) ,
        ];
    }

    /**
     * 验证密码
     * @param $password
     * @return bool
     */
    public function validatePassword($password) {
        if( !$this->PASSWORD_HASH ) return false;

        return password_verify($password, $this->PASSWORD_HASH);
    }

    /**
     * verificationPassword
     *
     * @param string $password password to validate
     * @return bool if password provided is valid for current user
     */
    public function verificationPassword($password,$scenario,$model)
    {
        // 判断密码长度不能少于8位
        if (strlen($password) < 8){
            return [ 'code' => 1000, 'type' => $scenario , 'message' => '登录密码不能小于8位' ];
        }

        // 判断密码中数字、大写字母、小写字母、特殊字符至少三种
        $symbol = '`~!@#$^&*()=|{}\'\":;\',\\[\\].<>\/?~！@#￥……&*（）——|{}【】‘；：”。，、？';
        // $requiredRules = '/^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)(?!['.$symbol.']+$)[\da-zA-z'.$symbol.']{8,}$/';   // 这是两种的正则
        $requiredRules = '/^(?![\da-z]+$)(?![\dA-Z]+$)(?![\d'.$symbol.']+$)(?![a-zA-Z]+$)(?![a-z'.$symbol.']+$)(?![A-Z'.$symbol.']+$)[\da-zA-z'.$symbol.']{8,}$/';
        $requiredHandleResults = preg_match($requiredRules,$password);
        if (!$requiredHandleResults){
            return [ 'code' => 1000, 'type' => $scenario , 'message' => '登录密码中需要包含数字、大写字母、小写字母、特殊字符至少三种' ];
        }

        // 判断有三位以上重复性
        if(preg_match('/(\w)*(\w)\1{2}(\w)*/',$password)) {
            return [ 'code' => 1000, 'type' => $scenario , 'message' => '登录密码中数字、大写字母、小写字母不能连续重复三个以上' ];
        }

        // 判断密码中不能出现登录账号
        if (strpos($password, $model->USER_NAME) !== false){
            return [ 'code' => 1000, 'type' => $scenario , 'message' => '登录密码中不允许出现会员登录账号的信息' ];
        }

        // 判断密码中不能出现会员的手机号
        if ($model->MOBILE){
            if (strpos($password, $model->MOBILE) !== false){
                return [ 'code' => 1000, 'type' => $scenario , 'message' => '登录密码中不允许出现会员的联系方式' ];
            }
        }

        // 判断密码中不能出现会员的生日
        if ($model->ID_CARD && strlen($model->ID_CARD) == 18){
            $idCard = substr($model->ID_CARD,6,8);
            if (strpos($password, $idCard) !== false){
                return [ 'code' => 1000, 'type' => $scenario , 'message' => '登录密码中不允许出现会员的生日' ];
            }
        }

        return [ 'code' => 200, 'type' => $scenario , 'message' => '验证通过了' ];
    }

    /**
     * 验证支付密码
     * @param $payPassword
     * @return bool
     */
    public function validatePasswordPay($payPassword) {
        if( !$this->PAY_PASSWORD ) return false;

        return password_verify($payPassword, $this->PAY_PASSWORD);
    }

    /**
     * 检验支付密码
     * @param $userId
     * @param $password
     * @return bool
     */
    public static function validatePayPassword($userId, $password){
        $oneUser = static::find()->select('PAY_PASSWORD')->where('ID=:ID', [':ID'=>$userId])->asArray()->one();
        if($oneUser){
            return password_verify($password, $oneUser['PAY_PASSWORD']);
        } else {
            return false;
        }
    }


    /**
     *  判断用户是否是观察期
     *  观望期：自加入算起2个月
     *  例子:2022年5月14日加入， 观望期至2022年7月31日。
     *  true为是观察期用户  false为不是观察期用户
     * @param $addAt 用户的加入时间
     * @param $month 观察期月份限制 系统初始化为2个月
     */
    public static function checkIsObserve($addAt, $month) {
        if (empty($month)) {
            // 如果0 代表全站全额升级
            return false;
        }
        $appendMonth = date("Y-m", strtotime("first day of +$month month", $addAt)); // 指定月份
        $lastDay = date("t", strtotime($appendMonth));  // 获取指定月的最后一天
        $eTime = strtotime($appendMonth.'-'.$lastDay);

        return time() < $eTime ? true : false;
    }

    // 获取用户报单PV总和
    public static function sumDevPvByUserId($userId) {
        $decOrderPv = DecOrder::find()
        ->select('SUM(DEC_PV) AS PV_SUM')
        ->where('TO_USER_ID=:TO_USER_ID AND IS_DEL=0',
            [
                'TO_USER_ID'=>$userId,

            ]
        )
        ->asArray()
        ->one();
        $decOrderPv = isset($decOrderPv['PV_SUM']) ? $decOrderPv['PV_SUM'] : 0;
        $orderDecPv = OrderDec::find()
        ->select('SUM(PAY_PV) AS PV_SUM')
        ->where('USER_ID=:USER_ID AND IS_DELETE=0',
            [
                'USER_ID'=>$userId,

            ]
        )
        ->asArray()
        ->one();
        $orderDecPv = isset($orderDecPv['PV_SUM']) ? $orderDecPv['PV_SUM'] : 0;
        // 还得加上用户在老系统中的所有报单PV之和
        $originPv = OriginDecPv::find()
        ->select('SUM(DEC_PV) AS PV_SUM')
        ->where('USER_ID=:USER_ID',
            [
                'USER_ID'=>$userId,

            ]
        )
        ->asArray()
        ->one();
        $originPv = isset($originPv['PV_SUM']) ? $originPv['PV_SUM'] : 0;
        $total = $orderDecPv + $decOrderPv + $originPv;

        return $total;
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'ID' => 'ID',
            'USER_NAME' => '帐号',
            'NATION' => [
                'label' => '民族',
                'type' => function($data){
                    $value = $data['value'];
                    return \Yii::$app->params['nation'][$value]['name']??'';
                },
            ],
            'REAL_NAME' => '姓名',
            'ID_CARD' => '身份证号',
            'ID_TYPE' => [
                'label' => '证件类型',
                'type' => function($data){
                    return '身份证';
                },
            ],
            'MOBILE' => '手机号',
            'ADDRESS' => '身份证地址',
            'ID_IMAGE' => '证件图片',
            'OPEN_BANK' => '开户行',
            'BANK_ADDRESS' => '银行地址',
            'BANK_NO' => '银行卡号',
            'BANK_PROVINCE' => [
                'label' => '银行省份',
                'type' => ValueTypeConfig::REGION_TYPE
            ],
            'BANK_CITY' => [
                'label' => '银行城市',
                'type' => ValueTypeConfig::REGION_TYPE
            ],
            'BANK_COUNTY' => [
                'label' => '银行县区',
                'type' => ValueTypeConfig::REGION_TYPE
            ],
            'SPOUSE_NAME' => '配偶姓名',
            'SPOUSE_IDCARD' => '配偶证件号',
            'STATUS' => [
                'label' => '状态',
                'type' => ValueTypeConfig::USER_STATUS_TYPE
            ],
            'DEC_CLOSED' => [
                'label' => '是否关闭报单功能',
                'type' => ValueTypeConfig::YES_NO_TYPE,
            ],
            'DEC_CLOSED_AT' => [
                'label' => '关闭报单时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
            'DEC_LV' => [
                'label' => '报单级别',
                'type' => ValueTypeConfig::DEC_LV_TYPE,
            ],
            'EMP_LV' => [
                'label' => '聘级',
                'type' => ValueTypeConfig::EMP_LV_TYPE,
            ],
            'LAST_DEC_LV' => [
                'label' => '上次的报单级别',
                'type' => ValueTypeConfig::DEC_LV_TYPE,
            ],
            'PROVINCE' => [
                'label' => '省份',
                'type' => ValueTypeConfig::REGION_TYPE
            ],
            'CITY' => [
                'label' => '城市',
                'type' => ValueTypeConfig::REGION_TYPE
            ],
            'COUNTY' => [
                'label' => '县区',
                'type' => ValueTypeConfig::REGION_TYPE
            ],
            'TEL' => '备用手机号码',
            'SUB_COM_ID' => '分公司ID',
            'TRANSFER_PROP' => '转帐比例',
            'IS_DEC' => [
                'label' => '是否为报单中心',
                'type' => ValueTypeConfig::YES_NO_TYPE,
            ],
            'DEC_ROLE_ID' => [
                'label' => '报单中心级别',
                'type' => ValueTypeConfig::DEC_ROLE_ID_TYPE,
            ],
            'IS_UNION' => [
                'label' => '是否为点位合作',
                'type' => ValueTypeConfig::YES_NO_TYPE,
            ],
            'STATUS_AT' => '状态修改时间',
            'ALLOW_LOGIN' => [
                'label' => '是否允许商城登录',
                'type' => ValueTypeConfig::YES_NO_TYPE,
            ],
            'NOT_OPERATING' => [
                'label' => '不运作',
                'type' => ValueTypeConfig::YES_NO_TYPE,
            ],
            'BIRTHDAY' => '生日',
            'SEX' => '性别',
        ];
    }

    /**
     * 获取列表
     * @param string $condition
     * @param array $params
     * @param array $argv
     * @return array
     */
    public static function lists($condition = '', $params = [], $argv = []) {
        self::prepare($condition, $params, $argv, true);
        unset($condition, $params, $argv);

        $countQuery = clone self::$query;
        $simpleQuery = clone self::$query;

        if(!$simpleQuery->params){
            $simpleQuery->join = [];
            $count = $simpleQuery->count(self::$argv['count']);
        } else {
            $count = $countQuery->count(self::$argv['count']); // 得到总数
        }
//        $count = $countQuery->count(self::$argv['count']); // 得到总数
        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->setPageSize(self::$argv['pageSize']);
        if(self::$argv['page'] !== null){
            $pagination->setPage(self::$argv['page']);
        }
        self::$query->offset($pagination->offset)->limit($pagination->limit);
        if(self::$argv['asArray']){
            self::$query->asArray();
        }
        $lists = self::$query->all();

        self::$query = null;
        unset($countQuery);
        $startNum = $pagination->page * $pagination->pageSize + 1;
        return [
            'list' => $lists ? $lists : [],
            'pagination' => $pagination,
            // 'sql'=self::$rawSql,
            'currentPage'=>$pagination->page,
            'totalPages'=>$pagination->pageCount,
            'startNum' => $startNum,
            'totalCount' => $pagination->totalCount,
            'pageSize' => $pagination->pageSize,
        ];
    }
}
