<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%RECHARGE}}".
 *
 * @property string $ID
 * @property string $CALC_ID 单号
 * @property string $ADMIN_ID 会员ID
 * @property int $PERIOD_NUM 期数
 * @property int $START_AT 开始时间
 * @property int $END_AT 结束时间
 * @property int $STATUS 状态
 */

class CalcOperation extends \common\components\ActiveRecord {
    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%CALC_OPERATION}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'ID' => 'ID',
            'CALC_ID' => '计算ID',
            'ADMIN_ID' => '管理员ID',
            'PERIOD_NUM' => '期数',
            'START_TIME' => '开始时间',
            'END_TIME' => '结束时间',
            'STATUS' => '状态',
        ];
    }

    /**
     * @param $periodNum
     * @param $text
     * @return bool
     * @throws \yii\db\Exception
     */
    public static function record($calcId, $periodNum)
    {
        self::insertOne([
            'CALC_ID' => $calcId,
            'ADMIN_ID' => '',
            'PERIOD_NUM' => $periodNum,
            'START_TIME' => time(),
            'END_TIME' => '',
            'STATUS' => 0,
        ]);
        return true;
    }

    public static function closeOperation($calcId){
        self::updateAll(['STATUS' => 1, 'END_TIME' => time()], 'CALC_ID=:CALC_ID', [':CALC_ID' => $calcId]);
    }
}
