<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/10/30
 * Time: 下午3:19
 */

namespace common\helpers\user;

use common\helpers\Cache;
use common\helpers\Date;
use common\models\forms\UserStatusForm;
use common\models\PerfMonth;
use common\models\Period;
use common\models\UserWallet;
use common\models\User;
use common\models\UserClose;
use common\models\UserInfo;
use common\models\UserStatusAudit;

class Status
{
    /**
     * 根据所传期数获取当前期数是否可以因为复销得到奖金
     * @param $userId
     * @param $periodNum
     * @return bool
     */
    public static function fxGetBonusStatusByPeriodNum($userId, $periodNum){
        $period = Period::instance();
        $lastYearMonth = $period->getLastMonth($periodNum);
        return PerfMonth::find()->yearMonth($lastYearMonth['yearMonth'])->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH AND FX_STATUS=1', [':USER_ID'=>$userId, ':CALC_MONTH'=>$lastYearMonth['yearMonth']])->exists();
    }

//    public static function fxGetBonusStatusByYearMonth($userId, $yearMonth){
//        $period = Period::instance();
//        $lastYearMonth = Date::lastNumMonth($yearMonth);
//        return PerfMonth::find()->yearMonth($lastYearMonth)->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH AND FX_STATUS=1', [':USER_ID'=>$userId, ':CALC_MONTH'=>$lastYearMonth])->exists();
//    }

    /**
     * 是否所传的结算月加入
     * @param $userId
     * @param $calcMonth
     * @return bool
     * @throws \yii\db\Exception
     */
    public static function isMonthJoin($userId, $calcMonth){
        $oneUser = User::find()->select('PERIOD_AT')->where('ID=:USER_ID', [':USER_ID'=>$userId])->asArray()->one();
        $period = Period::instance();
        $addYearMonth = $period->getYearMonth($oneUser['PERIOD_AT']);
        return $addYearMonth == $calcMonth;
    }

    /**
     * 所传结算月之后加入
     * @param $userId
     * @param $calcMonth
     * @return bool
     * @throws \yii\db\Exception
     */
    public static function afterMonthJoin($userId, $calcMonth){
        $oneUser = User::find()->select('PERIOD_AT')->where('ID=:USER_ID', [':USER_ID'=>$userId])->asArray()->one();
        $period = Period::instance();
        $addYearMonth = $period->getYearMonth($oneUser['PERIOD_AT']);
        return $addYearMonth > $calcMonth;
    }

    /**
     * 是否完善了身份证照片
     * @param $userId
     * @return bool
     */
    public static function isFinishIdCard($userId){
        return User::find()->where('ID=:ID AND ID_IMAGE IS NOT NULL',[':ID'=>$userId])->exists();
    }

    /**
     * 是否存在点位合作情况
     * @param $userId
     * @return bool
     */
    public static function isTeamwork($userId){
        return UserInfo::find()->where('IS_TEAM=1 AND USER_ID=:USER_ID', [':USER_ID' => $userId])->exists();
    }

    /**
     * 点位绑定情况
     * @param $userId
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function chkBind($userId){
        $userBind=UserInfo::find()->select('IS_BIND,IS_BIND_MAIN')->where('USER_ID=:USER_ID',[':USER_ID'=>$userId])->asArray()->one();
        return $userBind;
    }

    /**
     * 会员状态
     * @param $userId
     * @param null $periodNum
     * @return string
     */
    public static function getStatus($userId, $periodNum = null){
        $period = Period::instance();
        $nowPeriod = $period->getNowPeriodNum();
        if($periodNum === null || $periodNum = $nowPeriod){
            $user = User::findOneAsArray('ID=:ID', ['ID'=>$userId], 'STATUS');
            return $user['STATUS'];
        } else {
            // 获取会员所传期数之前的最后一次修改状态的记录
            $data = UserStatusAudit::findUseSlaves()->where('USER_ID=:USER_ID AND AUDIT_STATUS=:AUDIT_STATUS AND PERIOD_NUM<=:PERIOD_NUM', [':USER_ID' => $userId, ':AUDIT_STATUS' => \Yii::$app->params['auditStatus']['true']['value'], ':PERIOD_NUM' => $periodNum])->orderBy('AUDITED_AT DESC')->asArray()->one();
            if($data) {
                return $data['STATUS_VALUE'];
            } else {
                return self::getStatus($userId, null);
            }
        }
    }

    /**
     * 获取超过关停时间的会员
     * @param bool $hasClear 清除过钱包
     * @return array|null|\yii\db\ActiveRecord[]
     */
    public static function getCloseUser($hasClear = false){
        $days = Cache::getSystemConfig()['closeInspectClearDays']['VALUE'];
        if($hasClear){
            $users = User::findAllAsArray('STATUS=:STATUS AND STATUS_AT<:STATUS_AT',[':STATUS'=>UserStatusForm::STATUS_CLOSE,'STATUS_AT'=>Date::lastNumDay($days)],'ID,STATUS,REAL_NAME,STATUS_AT');
        }else{
            $users = User::find()->from(User::tableName() . ' AS U')->select('U.ID,U.STATUS,U.REAL_NAME,U.STATUS_AT')->where('U.STATUS=:STATUS AND U.STATUS_AT<:STATUS_AT AND UW.CLEAR_BY_CLOSED_AT=0',[':STATUS'=>UserStatusForm::STATUS_CLOSE,'STATUS_AT'=>Date::lastNumDay($days)])->join('LEFT JOIN',UserWallet::tableName().' AS UW','U.ID=UW.USER_ID')->asArray()->all();
        }
        foreach ($users as $k=>$u){
            $users[$k]['STATUS_NAME'] = \Yii::$app->params['userStatus'][$u['STATUS']]['label'];
        }
        return $users;
    }

    public static function getLoginStatus() {

    }

//    /**
//     * 是否关停状态
//     * @param $userId
//     * @param null $periodNum
//     * @return bool
//     */
//    public static function isCloseAccount($userId, $periodNum=null){
//        if($periodNum === null){
//            $data = UserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId], 'CLOSE_ACCOUNT');
//            return boolval($data['CLOSE_ACCOUNT']);
//        } else {
//            // 获取会员所传期数之前的最后一次关停并审核的记录
//            $data = UserClose::find()->where('USER_ID=:USER_ID AND TYPE=:TYPE AND AUDIT_STATUS=:AUDIT_STATUS AND PERIOD_NUM<=:PERIOD_NUM', [':USER_ID'=>$userId, ':TYPE' => UserClose::TYPE_GT, ':AUDIT_STATUS'=>\Yii::$app->params['auditStatus']['true']['value'], ':PERIOD_NUM'=>$periodNum])->orderBy('AUDITED_AT DESC')->asArray()->one();
//            if($data) return boolval($data['IS_CLOSE']);
//            return false;
//        }
//    }
//
//    /**
//     * 是否停发状态
//     * @param $userId
//     * @param null $periodNum
//     * @return bool
//     */
//    public static function isCloseBonus($userId, $periodNum=null){
//        if($periodNum === null){
//            $data = UserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId], 'CLOSE_BONUS');
//            return boolval($data['CLOSE_BONUS']);
//        } else {
//            // 获取会员所传期数之前的最后一次关停并审核的记录
//            $data = UserClose::find()->where('USER_ID=:USER_ID AND TYPE=:TYPE AND AUDIT_STATUS=:AUDIT_STATUS AND PERIOD_NUM<=:PERIOD_NUM', [':USER_ID'=>$userId, ':TYPE' => UserClose::TYPE_TF, ':AUDIT_STATUS'=>\Yii::$app->params['auditStatus']['true']['value'], ':PERIOD_NUM'=>$periodNum])->orderBy('AUDITED_AT DESC')->asArray()->one();
//            if($data) return boolval($data['IS_CLOSE']);
//            return false;
//        }
//    }
}