<?php

namespace common\models;

use common\helpers\Date;
use common\helpers\Form;
use Exception;
use Yii;

/**
 * This is the model class for table "{{%DEC_LEVEL_LOG}}".
 *
 * @property string $ID
 * @property string $USER_ID 会员ID
 * @property string $FROM_ID 变动前的级别
 * @property string $TO_ID 变动后的级别
 * @property string $ADMIN_ID admin id
 * @property int $PERIOD_NUM 变动的期数
 * @property int $CALC_MONTH 变动的结算月
 * @property string $REMARK 备注
 * @property int $STATUS 状态
 * @property int $CREATED_AT 创建时间
 */
class DecLevelLog extends \common\components\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%DEC_LEVEL_LOG}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['USER_ID', 'FROM_ID', 'TO_ID', 'PERIOD_NUM', 'CALC_MONTH', 'CREATED_AT'], 'required'],
            [['PERIOD_NUM', 'CALC_MONTH', 'STATUS', 'CREATED_AT'], 'integer'],
            [['ID', 'USER_ID', 'FROM_ID', 'TO_ID', 'ADMIN_ID'], 'string', 'max' => 32],
            [['REMARK'], 'string', 'max' => 4000],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '会员ID',
            'FROM_ID' => '变动前的级别',
            'TO_ID' => '变动后的级别',
            'ADMIN_ID' => '管理员id',
            'PERIOD_NUM' => '变动的期数',
            'CALC_MONTH' => '变动的结算月',
            'REMARK' => '备注',
            'STATUS' => '状态',
            'CREATED_AT' => '创建时间',
        ];
    }

    // 会员端升级报单操作,添加记录
    public function frontendChange($data) {
        $db = \Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            // 恢复所传期数的和这个会员相关的其他调整
            $period = Period::instance();
            $periodNum = $nowPeriodNum = $period->getNowPeriodNum();
            DecLevelLog::updateAll(
                [
                    'STATUS' => 0
                ], 
                    'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', 
                [
                    ':USER_ID' => $data['userId'], ':PERIOD_NUM' => $periodNum
                ]
            );
            // 新增数据
            $model = new DecLevelLog();
            $model->USER_ID = $data['userId'];
            $model->FROM_ID = $data['fromId'];
            $model->TO_ID = $data['levelId'];
            $model->PERIOD_NUM = 0;
            $model->CALC_MONTH = 0;
            $model->REMARK = isset($data['remark']) ? $data['remark'] : '';
            $model->STATUS = 1;
            $model->ADMIN_ID = $data['actionId'];
            $model->CREATED_AT = Date::nowTime();
            $model->DEC_TYPE = 2;
            if (!$model->save()) {
                throw new Exception(Form::formatErrorsForApi($model->getErrors()));
            }
            //修改会员级别，如果是上期，则一起变化，如果不是，则只改变实时级别
            $nowTime = Date::nowTime();
            User::updateAll(
                [
                    'LAST_DEC_LV' => $data['levelId'], 
                    'DEC_LV' => $data['levelId'], 
                    'DEC_LV_UPDATED_AT'=>$nowTime, 
                    'DEC_LV_UPDATED_PERIOD'=>$nowPeriodNum, 
                    'LAST_DEC_LV_UPDATED_AT'=>$nowTime, 
                    'LAST_DEC_LV_UPDATED_PERIOD'=>$nowPeriodNum, 
                    'ZG_UPGRADE_PV'=>$data['lvPv']
                ], 
                'ID=:ID', 
                [
                    ':ID' => $data['userId']
                ]
            );

            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            $this->addError('adminChange', $e->getMessage());
            return null;
        }
        return $model;
    }
}
