<?php

namespace common\models;

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

/**
 * This is the model class for table "{{%DEAL_TYPE}}".
 *
 * @property string $ID
 * @property string $TYPE_NAME 类型名称
 * @property string $CREATE_ADMIN 创建人
 * @property string $UPDATE_ADMIN 修改人
 * @property string $CREATE_REMARK 创建备注
 * @property string $UPDATE_REMARK 修改备注
 * @property int $IS_PRESET 是否系统预置
 * @property int $IS_ENABLE 是否启用
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 修改时间
 * @property int $SORT_ORDER 排序
 */
class DealType extends \common\components\ActiveRecord
{
    //系统预置类型id
    CONST BONUS_SEND = '141E52002AAE43B7AC304F4BAEFDA268';//奖金发放
    CONST WITHDRAW = '6626ED606DB34A11B6CCEA768B6E36C2';//奖金提现
    CONST WITHDRAW_RETURN = '01A753D2548F44FA99B847133C1B5F79';//提现退回
    CONST TRANSFER_OUT = 'DAA0FB1465F748908680D941D589326E';//转出
    CONST TRANSFER_IN = 'DB62879E6C6B4B2F8591FAD920A2822C';//转入
    CONST RESEND = '37B81A45F38646A0A16499AB27B51923';//补发
    CONST SMS = '1145D8ABA7A7471FB59B6760BDB19019';//短信服务费
    CONST DEDUCT_CLOSE = '37EFF25E5E3F46A6BB84B8C55997F276';//永久关停扣除
    CONST DEDUCT_LOGOUT = 'A1F1E76F34EE4981AB3BF4F8D20BB53E';//注销扣除
    CONST RECONSUME_POINTS_EXCHANGE = 'YA627DTHN81OBO7NIQ1OP8N3PGZX5C66';//复消积分兑换
    CONST EXCHANGE_POINTS_EXCHANGE = '316872011148300288';//兑换积分兑换
    CONST USER_PERFORMANCE_SEND = 'ABN9H5ZW3GEIJTFM';// 绩效奖金发放
    CONST USER_PERFORMANCE_EXCHANGE = '7XPS0UZ89V2B5DRE';// 绩效奖金兑换
    CONST USER_PERFORMANCE_EXPIRED = 'SN60AKUHDW8Q91F3';// 绩效奖金过期

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

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['TYPE_NAME', 'CREATE_ADMIN', 'CREATED_AT'], 'required'],
            [['IS_PRESET', 'IS_ENABLE', 'CREATED_AT', 'UPDATED_AT', 'SORT_ORDER'], 'integer'],
            [['ID', 'CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
            [['TYPE_NAME'], 'string', 'max' => 255],
            [['CREATE_REMARK', 'UPDATE_REMARK'], 'string', 'max' => 4000],
            [['ID','TYPE_NAME'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'TYPE_NAME' => '类型名称',
            'CREATE_ADMIN' => '创建人',
            'UPDATE_ADMIN' => '修改人',
            'CREATE_REMARK' => '创建备注',
            'UPDATE_REMARK' => '修改备注',
            'IS_PRESET' => '是否系统预置',
            'IS_ENABLE' => '是否启用',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '修改时间',
            'SORT_ORDER' => '排序',
        ];
    }

    /**
     * 从缓存获取信息
     * @return array|mixed|\yii\db\ActiveRecord[]
     */
    public static function getFromCache(){
        $data = Yii::$app->cache->get(Cache::DEAL_TYPE_CONFIG_KEY);
        if(!$data){
            // 获取信息
            $data = static::find()->where('1=1')->orderBy('CREATED_AT ASC')->indexBy('ID')->asArray()->all();
            Yii::$app->cache->set(Cache::DEAL_TYPE_CONFIG_KEY, $data);
        }
        // i18n转换
        foreach ($data as &$item) {
            $item['TYPE_NAME'] = Yii::t('ctx', $item['LANGUAGE_KEY']);
        }

        return $data;
    }

    /**
     * 获取注册类型
     * @return array|null
     */
    public static function getTypes(){
        return self::find()->select('ID,TYPE_NAME')->where('IS_PRESET=0 AND IS_ENABLE=1')->indexBy('ID')->orderBy('SORT_ORDER DESC')->asArray()->all();
    }

    public static function getDealTypeIdByTag($tag)
    {
        $record = self::find()->select('ID,TYPE_NAME,COMMENT')->where('TYPE_NAME=:TYPE_NAME', [':TYPE_NAME' => $tag])->asArray()->one();

        return $record['ID'] ?? '';
    }

    public static function getDealTypeTagById($dealTypeId)
    {
        $record = self::find()->select('ID,TYPE_NAME,COMMENT')->where('ID=:ID', [':ID' => $dealTypeId])->asArray()->one();

        return $record['TYPE_NAME'] ?? '';
    }

    /**
     * 获取全部交易类型
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAllTypes(){
        return self::find()->select('ID,TYPE_NAME,IS_PRESET')->where('IS_ENABLE=1')->indexBy('ID')->orderBy('IS_PRESET ASC,SORT_ORDER DESC')->asArray()->all();
    }

    /**
     * 获取用于显示的交易类型
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAllTypesForShow(){
        return self::find()->select('ID,TYPE_NAME,IS_PRESET')->indexBy('ID')->orderBy('IS_PRESET ASC,SORT_ORDER DESC')->asArray()->all();
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'TYPE_NAME' => '类型名称',
            'IS_PRESET' => [
                'label' => '是否系统预置',
                'type' => ValueTypeConfig::YES_NO_TYPE,
            ],
            'IS_ENABLE' => [
                'label' => '是否启用',
                'type' => ValueTypeConfig::YES_NO_TYPE,
            ],
            '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'] : '';
                },
            ],
            'CREATED_AT' => [
                'label' => '创建时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
            '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'] : '';
                },
            ],
            'UPDATED_AT' => [
                'label' => '修改时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
            'CREATE_REMARK' => '备注',
            'SORT_ORDER' => '排序',
        ];
    }
}
