<?php

namespace common\models;

/**
 * This is the model class for table "{{%DECLARATION_PACKAGE}}".
 *
 * @property string $ID
 * @property string $PACKAGE_NAME 套餐名称
 * @property string $PACKAGE_NO 套餐编号
 * @property string $LEVEL_ID 所属报单级别ID
 * @property string $AMOUNT 套餐金额
 * @property string $PV 套餐PV
 * @property string $AMOUNT_STANDARD 标准金额
 * @property string $PACKAGE_CONTENT 套餐内容
 * @property string $STORE_NUMS 套餐库存
 * @property string $CREATED_AT 创建时间
 * @property string $CREATE_ADMIN 创建人
 * @property string $UPDATE_ADMIN 更新人
 * @property int $UPDATED_AT 更新时间
 * @property int $IS_DEL 是否删除
 * @property int $STATUS 状态
 * @property int $DELETED_AT 删除时间
 */
class DeclarationPackage extends \common\components\ActiveRecord
{
    const STATUS_NAME = [
        1 => '已上架',
        0 => '已下架',
    ];


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%DECLARATION_PACKAGE}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['PACKAGE_NAME','PACKAGE_NO','LEVEL_ID', 'AMOUNT', 'PV', 'PACKAGE_CONTENT', 'CREATED_AT','CREATE_ADMIN','AMOUNT_STANDARD'], 'required'],
            [['ID','LEVEL_ID','CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
            [['PACKAGE_NAME'], 'string', 'max' => 48],
            [['PACKAGE_NAME'], 'unique'],
            [['AMOUNT', 'PV','STORE_NUMS'], 'number'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'PACKAGE_NAME' => '套餐名称',
            'PACKAGE_NO' => '套餐编号',
            'LEVEL_ID' => '所属报单级别ID',
            'AMOUNT' => '套餐金额',
            'AMOUNT_STANDARD' => 'US price',// 标准价格
            'PV' => '套餐BV',
            'PACKAGE_CONTENT' => '套餐内容',
            'CREATED_AT' => '创建时间',
            'CREATE_ADMIN' => '创建人',
            'UPDATE_ADMIN' => '更新人',
            'UPDATED_AT' => '更新时间',
            'IS_DEL' => '是否删除',
            'STATUS' => '是否上架',
            'DELETED_AT' => '删除时间',
			'STORE_NUMS' => '套餐库存',
        ];
    }

    /**
     * 判断上下架状态
     * @param $nowStatus
     * @param $toStatus
     * @return string
     */
    public static function chkAuditStatus($nowStatus, $toStatus) {
        $statusName = self::STATUS_NAME;
        $msg = '当前套餐状态为【' . $statusName[$nowStatus] . '】，无法设置为【' . $statusName[$toStatus] . '】';
        switch ($toStatus) {
            // 下架
            case 0:
                if ($nowStatus == 1) {
                    $msg = '';
                }
                break;
            //上架
            case 1:
                if ($nowStatus == 0) {
                    $msg = '';
                }
                break;
            default:
        }
        return $msg;
    }

    /**
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAllData(){
        return static::find()->where('IS_DEL=0 AND STATUS=1')->indexBy('ID')->orderBy('ID ASC')->asArray()->all();
    }

    /**
     * 通过报单ID获取对应的报单套餐
     * @param $levelId
     * @return mixed
     */
    public static function getPackageFromLevelId($levelId){
        return static::findAllAsArray('LEVEL_ID=:LEVEL_ID', [':LEVEL_ID'=>$levelId]);
    }

    /**
     * 通过ID获取套餐所属报单级别ID
     * @param $packageId
     * @return mixed
     */
    public static function getLevelById($packageId){
        $data = self::findOneAsArray('ID=:ID', [':ID'=>$packageId]);
        return $data['LEVEL_ID'];
    }
}