<?php

namespace common\models;

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

/**
 * This is the model class for table "{{%USER_TEAMWORK}}".
 *
 * @property string $ID
 * @property string $USER_ID 会员ID
 * @property string $MAIN_UID 主会员ID
 * @property string $DIVIDE_PERCENT 分成比例
 * @property int $PERIOD_NUM 加入时期数
 * @property int $IS_DEL 是否删除
 * @property string $CREATE_ADMIN 创建人
 * @property string $UPDATE_ADMIN 修改人
 * @property string $CREATE_REMARK 创建备注
 * @property string $UPDATE_REMARK 修改备注
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 修改时间
 * @property int $UPDATED_PERIOD 修改期数
 * @property int $DELETED_AT 已删除
 */
class UserTeamwork extends \common\components\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%USER_TEAMWORK}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['USER_ID', 'MAIN_UID', 'PERIOD_NUM', 'CREATE_ADMIN', 'CREATED_AT'], 'required'],
            [['DIVIDE_PERCENT'], 'number'],
            [['PERIOD_NUM', 'IS_DEL', 'CREATED_AT', 'UPDATED_AT', 'UPDATED_PERIOD', 'DELETED_AT'], 'integer'],
            [['ID', 'USER_ID', 'MAIN_UID', 'CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
            [['CREATE_REMARK', 'UPDATE_REMARK'], 'string', 'max' => 4000],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '会员ID',
            'MAIN_UID' => '主会员ID',
            'DIVIDE_PERCENT' => '分成比例',
            'PERIOD_NUM' => '加入时期数',
            'IS_DEL' => '是否删除',
            'CREATE_ADMIN' => '创建人',
            'UPDATE_ADMIN' => '修改人',
            'CREATE_REMARK' => '创建备注',
            'UPDATE_REMARK' => '修改备注',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '修改时间',
            'UPDATED_PERIOD' => '修改期数',
            'DELETED_AT' => '已删除',
        ];
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'USER_ID' => '会员ID',
            'MAIN_UID' => [
                'label' => '主会员编号',
                'type' => function($data){
                    $value = $data['value'];
                    return Info::getUserNameByUserId($value);
                },
            ],
            'DIVIDE_PERCENT' => '分成比例',
            'CREATE_ADMIN' => [
                'label' => '操作人',
                'type' => function($data){
                    $value = is_array($data) && isset($data['value']) ? $data['value'] : '';
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return !empty($result) ? $result['ADMIN_NAME'] : '';
                },
            ],
            'UPDATE_ADMIN' => [
                'label' => '修改人',
                'type' => function($data){
                    $value = is_array($data) && isset($data['value']) ? $data['value'] : '';
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return !empty($result) ? $result['ADMIN_NAME'] : '';
                },
            ],
            'CREATED_AT' => [
                'label' => '操作时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
            'UPDATED_AT' => [
                'label' => '修改时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
        ];
    }
}
