<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/5/18
 * Time: 下午4:53
 */

namespace common\helpers\bonus;


use common\helpers\Cache;
use common\helpers\Date;
use common\models\forms\PeriodForm;
use common\models\Period;
use yii\base\BaseObject;
use yii\base\StaticInstanceTrait;

class AutoClosePeriod extends BaseObject
{
    use StaticInstanceTrait;

    const CACHE_PERIOD_NUM_KEY = 'autoClosePeriodNum';
    const CACHE_PERIOD_CLOSE_STATUS_KEY = 'autoClosePeriodCloseStatus';
    const CACHE_CLOSE_PERIOD_TIME_KEY = 'autoCloseClosePeriodTime';
    const CLOSE_PERIOD_STATUS_NORMAL = 1;
    const CLOSE_PERIOD_STATUS_CLOSING = 2;

    private $_closeInfo;

    public function init()
    {
        parent::init();
    }

    /**
     * 自动封期
     * @throws \yii\db\Exception
     */
    public function autoClose(){
        if($this->isAutoClose()){
            if($this->isToCloseTime()){
                // 把正在封期状态设置为正在封期
                $this->setClosingStat();
                $formModel = new PeriodForm();
                $formModel->scenario = 'autoClose';
                $formModel->periodNum = $this->_closeInfo['periodNum'];
                $formModel->autoClosePeriod();
                // 更新新的缓存
                $this->setCloseTimeAndPeriodStat();
            }
        }
    }

    /**
     * 获取当前系统是否自动封期
     * @return bool
     */
    public function isAutoClose(){
        $systemConfig = Cache::getSystemConfig();
        return boolval($systemConfig['autoClosePeriod']['VALUE']);
    }

    /**
     * 判断是否到达可以封期
     * @return bool
     */
    public function isToCloseTime(){
        $closeInfo = $this->_closeInfo = self::getCloseTimeAndPeriodStat();
        if($closeInfo['periodCloseStatus'] == self::CLOSE_PERIOD_STATUS_CLOSING){
            return false;
        }
        if(Date::nowTime() < $closeInfo['closePeriodTime']){
            return false;
        }
        return true;
    }

    /**
     * 从缓存中获取封期状态和封期时间
     * @return array
     */
    public function getCloseTimeAndPeriodStat(){
        $periodNum = \Yii::$app->redis->get(self::CACHE_PERIOD_NUM_KEY);
        $periodCloseStatus = \Yii::$app->redis->get(self::CACHE_PERIOD_CLOSE_STATUS_KEY);
        $closePeriodTime = \Yii::$app->redis->get(self::CACHE_CLOSE_PERIOD_TIME_KEY);
        if((!$periodNum) || (!$periodCloseStatus) || (!$closePeriodTime)){
            return $this->setCloseTimeAndPeriodStat();
        }
        return [
            'periodNum' => $periodNum,
            'periodCloseStatus' => $periodCloseStatus,
            'closePeriodTime' => $closePeriodTime,
        ];
    }

    /**
     * 设置下次封期时间和把本期封期状态设置为正常存入缓存
     * @return array
     */
    public function setCloseTimeAndPeriodStat(){
        // 先把封期状态设置为正常
        $periodCloseStatus = self::CLOSE_PERIOD_STATUS_NORMAL;
        \Yii::$app->redis->set(self::CACHE_PERIOD_CLOSE_STATUS_KEY, $periodCloseStatus);
        // 找到最近的未封期的期数
        $periodModel = Period::instance();
        $periodNum = $periodModel->getNowPeriodNum();
        \Yii::$app->redis->set(self::CACHE_PERIOD_NUM_KEY, $periodNum);
        $closePeriodTime = $periodModel->getNowPeriodEnd();
        \Yii::$app->redis->set(self::CACHE_CLOSE_PERIOD_TIME_KEY, $closePeriodTime);
        return [
            'periodNum' => $periodNum,
            'periodCloseStatus' => $periodCloseStatus,
            'closePeriodTime' => $closePeriodTime,
        ];
    }

    /**
     * 设置为正在封期
     */
    public function setClosingStat(){
        \Yii::$app->redis->set(self::CACHE_PERIOD_CLOSE_STATUS_KEY, self::CLOSE_PERIOD_STATUS_CLOSING);
    }
}