<?php

namespace common\models;

use backendApi\modules\v1\models\Admin;
use common\helpers\NetPoint;
use common\helpers\user\Info;
use Yii;
use common\libs\logging\operate\valueType\Config as ValueTypeConfig;

/**
 * This is the model class for table "{{%USER_MOVE}}".
 *
 * @property string $ID
 * @property string $USER_ID 移动的会员ID
 * @property string $FROM_UID 移动前接点UID
 * @property string $TO_UID 移动后接点UID
 * @property int $LOCATION 移动到位置
 * @property string $TYPE 移网类型
 * @property int $IS_MOVING 是否正在移动
 * @property int $MOVE_PERCENT 移动百分比
 * @property int $AUDIT_STATUS 审核状态
 * @property int $PERIOD_NUM 移动时的期数
 * @property int $UPDATE_PERIOD_NUM 修改期数
 * @property int $AUDIT_PERIOD_NUM 修改期数
 * @property string $MOVE_ADMIN_ID 移网管理员
 * @property string $AUDIT_ADMIN_ID 审核管理员
 * @property string $NET_TABLE_NAME 移点备份表名
 * @property string $CREATE_REMARK 创建备注
 * @property string $AUDIT_REMARK 审核备注
 * @property int $CREATED_AT 创建时间
 * @property int $STARTED_AT 移网开始时间
 * @property int $ENDED_AT 移网结束时间
 * @property int $AUDITED_AT 审核时间
 */
class UserMove extends \common\components\ActiveRecord
{
    const TYPE_NETWORK = NetPoint::NET_TYPE_NETWORK;
    const TYPE_RELATION = NetPoint::NET_TYPE_RELATION;

    const TYPE_NAME = [
        self::TYPE_NETWORK => ['id' => self::TYPE_NETWORK, 'name' => '安置网络',],
        self::TYPE_RELATION => ['id' => self::TYPE_RELATION, 'name' => '开拓网络',],
    ];

    const STATUS_APPLIED = 0;       // 已申请
    const STATUS_AUDITED = 1;       // 已审核
    const STATUS_FAILED = 2;        // 审核失败
    const STATUS_REFUSED = 3;       // 已拒绝

    const STATUS_NAME = [
        self::STATUS_APPLIED => '待审核',
        self::STATUS_AUDITED => '审核通过',
        self::STATUS_FAILED =>  '审核失败',
        self::STATUS_REFUSED => '审核拒绝',
    ];

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

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['USER_ID', 'FROM_UID', 'TO_UID', 'TYPE', 'PERIOD_NUM', 'MOVE_ADMIN_ID', 'CREATED_AT'], 'required'],
            [['LOCATION', 'IS_MOVING', 'MOVE_PERCENT', 'AUDIT_STATUS', 'PERIOD_NUM', 'UPDATE_PERIOD_NUM', 'AUDIT_PERIOD_NUM', 'CREATED_AT', 'STARTED_AT', 'ENDED_AT', 'AUDITED_AT'], 'integer'],
            [['ID', 'USER_ID', 'FROM_UID', 'TO_UID', 'MOVE_ADMIN_ID', 'AUDIT_ADMIN_ID', 'NET_TABLE_NAME'], 'string', 'max' => 32],
            [['TYPE'], 'string', 'max' => 48],
            [['CREATE_REMARK', 'AUDIT_REMARK'], 'string', 'max' => 4000],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '移动的会员ID',
            'FROM_UID' => '移动前接点UID',
            'TO_UID' => '移动后接点UID',
            'LOCATION' => '移动到位置',
            'TYPE' => '移网类型',
            'IS_MOVING' => '是否正在移动',
            'MOVE_PERCENT' => '移动百分比',
            'AUDIT_STATUS' => '审核状态',
            'PERIOD_NUM' => '移动时的期数',
            'UPDATE_PERIOD_NUM' => '修改期数',
            'AUDIT_PERIOD_NUM' => '修改期数',
            'MOVE_ADMIN_ID' => '移网管理员',
            'AUDIT_ADMIN_ID' => '审核管理员',
            'NET_TABLE_NAME' => '移点备份表名',
            'CREATE_REMARK' => '创建备注',
            'AUDIT_REMARK' => '审核备注',
            'CREATED_AT' => '创建时间',
            'STARTED_AT' => '移网开始时间',
            'ENDED_AT' => '移网结束时间',
            'AUDITED_AT' => '审核时间',
        ];
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'USER_ID' => '移动的会员ID',
            'FROM_UID' => [
                'label' => '移动前上级',
                'type' => function($data){
                    $value = $data['value'];
                    return Info::getUserNameByUserId($value);
                },
            ],
            'TO_UID' => [
                'label' => '移动后上级',
                'type' => function($data){
                    $value = $data['value'];
                    return Info::getUserNameByUserId($value);
                },
            ],
            'LOCATION' => '移动到位置',
            'TYPE' => [
                'label' => '移网类型',
                'type' => function($data){
                    $value = $data['value'];
                    return $value==self::TYPE_NETWORK?'安置网络':'开拓网络';
                },
            ],
            'AUDIT_STATUS' => [
                'label' => '审核状态',
                'type' => ValueTypeConfig::AUDIT_STATUS_TYPE,
            ],
            'MOVE_ADMIN_ID' => [
                'label' => '创建人',
                'type' => function($data){
                    $value = $data['value'];
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return $result['ADMIN_NAME'];
                },
            ],
            'AUDIT_ADMIN_ID' => [
                'label' => '审核人',
                'type' => function($data){
                    $value = $data['value'];
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return $result['ADMIN_NAME'];
                },
            ],
            'CREATE_REMARK' => '备注',
            'CREATED_AT' => [
                'label' => '创建时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
            'AUDITED_AT' => [
                'label' => '审核时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
        ];
    }
}
