<?php

namespace common\models;

use common\helpers\Tool;
use Yii;

/**
 * This is the model class for table "{{%RECONSUME_POOL_FLOW}}".
 *
 * @property string $ID
 * @property string $USER_ID 会员ID
 * @property string $RECONSUME_POOL_SN 流水号
 * @property string $LAST_DEC_LV 流水产生时会员级别
 * @property string $LAST_EMP_LV 流水产生时会员聘级
 * @property int $LAST_STATUS 流水产生时会员状态
 * @property int $RECONSUME_POOL_TYPE 流水类型
 * @property int $DEAL_TYPE 交易类型
 * @property string $DEDUCT_PV 变动PV
 * @property int $DEDUCT_MONTH 变动PV
 * @property string $UNUSED_PV 当前剩余未扣除PV
 * @property int $UNUSED_MONTH 当前剩余未扣除PV
 * @property int $IS_FX_DEDUCT 是否复销扣除
 * @property string $REMARK 备注
 * @property int $REMARK_IS_SHOW 备注前台显示
 * @property string $PERIOD_NUM 期数
 * @property int $CALC_MONTH 结算月
 * @property string $P_CALC_MONTH 分区表标识
 * @property int $CREATED_AT 创建时间
 * @property string $ADMIN_NAME 操作人
 */
class ReconsumePoolFlow extends \common\components\ActiveRecord
{
    const POOL_TYPE_PV = 1;
    const POOL_TYPE_MONTH = 2;
    const POOL_TYPE_NAME = [
        self::POOL_TYPE_PV=>'复销池余额流水',
        self::POOL_TYPE_MONTH=>'复销池月数流水',
    ];


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

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['USER_ID', 'RECONSUME_POOL_SN', 'CALC_MONTH', 'P_CALC_MONTH', 'CREATED_AT'], 'required'],
            [['LAST_STATUS', 'RECONSUME_POOL_TYPE', 'DEAL_TYPE', 'DEDUCT_MONTH', 'UNUSED_MONTH', 'IS_FX_DEDUCT', 'REMARK_IS_SHOW', 'CALC_MONTH', 'CREATED_AT'], 'integer'],
            [['DEDUCT_PV', 'UNUSED_PV', 'PERIOD_NUM'], 'number'],
            [['ID', 'USER_ID', 'RECONSUME_POOL_SN', 'LAST_DEC_LV', 'LAST_EMP_LV', 'ADMIN_NAME'], 'string', 'max' => 32],
            [['REMARK'], 'string', 'max' => 255],
            [['P_CALC_MONTH'], 'string', 'max' => 7],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '会员ID',
            'RECONSUME_POOL_SN' => '流水号',
            'LAST_DEC_LV' => '流水产生时会员级别',
            'LAST_EMP_LV' => '流水产生时会员聘级',
            'LAST_STATUS' => '流水产生时会员状态',
            'RECONSUME_POOL_TYPE' => '流水类型',
            'DEAL_TYPE' => '交易类型',
            'DEDUCT_PV' => '变动PV',
            'DEDUCT_MONTH' => '变动PV',
            'UNUSED_PV' => '当前剩余未扣除PV',
            'UNUSED_MONTH' => '当前剩余未扣除PV',
            'IS_FX_DEDUCT' => '是否复销扣除',
            'REMARK' => '备注',
            'REMARK_IS_SHOW' => '备注前台显示',
            'PERIOD_NUM' => '期数',
            'CALC_MONTH' => '结算月',
            'P_CALC_MONTH' => '分区表标识',
            'CREATED_AT' => '创建时间',
            'ADMIN_NAME' => '操作人',
        ];
    }

    /**
     * 生成sn
     * @param array $snArr
     * @param int $length
     * @return string
     */
    public static function generateSN() {
        $date = date('ymdHis');
        $sn = 'FX' . $date . Tool::numFix(rand(1, 9999999999), 10, '0');
        if (self::find()->where('RECONSUME_POOL_SN=:SN', [':SN' => $sn])->exists()) {
            return self::generateSN();
        }
        return $sn;
    }
}