<?php

namespace common\models;

use common\helpers\Tool;
use Yii;

/**
 * This is the model class for table "{{%TRANSFER}}".
 *
 * @property string $ID
 * @property string $TRANSFER_SN 转账记录流水号
 * @property string $OUT_UID 转出会员ID
 * @property string $LAST_OUT_USER_NAME 转账时转出会员编号
 * @property string $LAST_OUT_REAL_NAME 转账时转出会员姓名
 * @property string $LAST_OUT_DEC_LV 转账时转出会员级别
 * @property string $LAST_OUT_DEC_ROLE_ID 转账时转出会员报单中心级别
 * @property string $OUT_WALLET 转出账户
 * @property string $LAST_OUT_SYSTEM_ID 转账时转出会员体系
 * @property string $IN_UID 转入会员ID
 * @property string $LAST_IN_USER_NAME 转账时转入会员编号
 * @property string $LAST_IN_REAL_NAME 转账时转入会员姓名
 * @property string $LAST_IN_DEC_LV 转账时转入会员级别
 * @property string $IN_WALLET 转入账户
 * @property string $LAST_IN_SYSTEM_ID 转账时转入会员体系
 * @property string $ORI_AMOUNT 转账金额
 * @property string $FEE 手续费
 * @property string $AMOUNT 实际转入金额
 * @property string $REMARK 备注
 * @property int $PERIOD_NUM 所在期数
 * @property int $CALC_MONTH 所在结算月
 * @property int $CREATED_AT 创建时间
 */
class Transfer extends \common\components\ActiveRecord
{

    const WALLET_NAME = [
        'bonus'=>'增值点数',
        'cash'=>'消费点数',
        'point'=>'复销点数'
    ];

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

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['TRANSFER_SN', 'OUT_UID', 'LAST_OUT_USER_NAME', 'LAST_OUT_REAL_NAME', 'IN_UID', 'LAST_IN_USER_NAME', 'LAST_IN_REAL_NAME', 'PERIOD_NUM', 'CALC_MONTH', 'CREATED_AT'], 'required'],
            [['ORI_AMOUNT', 'FEE', 'AMOUNT'], 'number'],
            [['PERIOD_NUM', 'CALC_MONTH', 'CREATED_AT'], 'integer'],
            [['ID', 'TRANSFER_SN', 'OUT_UID', 'LAST_OUT_DEC_LV', 'LAST_OUT_DEC_ROLE_ID', 'LAST_OUT_SYSTEM_ID', 'IN_UID', 'LAST_IN_DEC_LV', 'LAST_IN_SYSTEM_ID'], 'string', 'max' => 32],
            [['LAST_OUT_USER_NAME', 'LAST_IN_USER_NAME'], 'string', 'max' => 16],
            [['LAST_OUT_REAL_NAME', 'OUT_WALLET', 'LAST_IN_REAL_NAME', 'IN_WALLET'], 'string', 'max' => 128],
            [['REMARK'], 'string', 'max' => 4000],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'TRANSFER_SN' => '转账记录流水号',
            'OUT_UID' => '转出会员ID',
            'LAST_OUT_USER_NAME' => '转账时转出会员编号',
            'LAST_OUT_REAL_NAME' => '转账时转出会员姓名',
            'LAST_OUT_DEC_LV' => '转账时转出会员级别',
            'LAST_OUT_DEC_ROLE_ID' => '转账时转出会员报单中心级别',
            'OUT_WALLET' => '转出账户',
            'LAST_OUT_SYSTEM_ID' => '转账时转出会员体系',
            'IN_UID' => '转入会员ID',
            'LAST_IN_USER_NAME' => '转账时转入会员编号',
            'LAST_IN_REAL_NAME' => '转账时转入会员姓名',
            'LAST_IN_DEC_LV' => '转账时转入会员级别',
            'IN_WALLET' => '转入账户',
            'LAST_IN_SYSTEM_ID' => '转账时转入会员体系',
            'ORI_AMOUNT' => '转账金额',
            'FEE' => '手续费',
            'AMOUNT' => '实际转入金额',
            'REMARK' => '备注',
            'PERIOD_NUM' => '所在期数',
            'CALC_MONTH' => '所在结算月',
            'CREATED_AT' => '创建时间',
        ];
    }

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

    /**
     * 本周是否转账
     * @param $uid
     * @return bool
     */
    public static function hasThisWeekTransfer($uid) {
        $period = Period::instance();
        if (static::find()->where('OUT_UID=:OUT_UID AND PERIOD_NUM=:PERIOD_NUM', [':OUT_UID' => $uid, ':PERIOD_NUM' => $period->getNowPeriodNum()])->exists()) {
            return true;
        }
        return false;
    }

    /**
     * 本周提现总额
     * @param $uid
     * @return mixed
     */
    public static function weekTransfer($uid){
        $period = Period::instance();
        return static::find()->where('OUT_UID=:OUT_UID AND PERIOD_NUM=:PERIOD_NUM', [':OUT_UID' => $uid, ':PERIOD_NUM' => $period->getNowPeriodNum()])->sum('ORI_AMOUNT');
    }

    /**
     * 本月提现总额
     * @param $uid
     * @return mixed
     */
    public static function monthTransfer($uid){
        $period = Period::instance();
        return static::find()->where('OUT_UID=:OUT_UID AND CALC_MONTH=:CALC_MONTH', [':OUT_UID' => $uid, ':CALC_MONTH' => $period->getNowYearMonth()])->sum('ORI_AMOUNT');
    }

}
