<?php

namespace common\models;

use backendApi\modules\v1\models\Admin;
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 DEDUCT = '7E3255C33BD6457B8F7155F2A86FFF73';//责任业绩扣除
    CONST WITHDRAW_RETURN = '01A753D2548F44FA99B847133C1B5F79';//提现退回
    CONST TRANSFER_OUT = 'DAA0FB1465F748908680D941D589326E';//转出
    CONST TRANSFER_IN = 'DB62879E6C6B4B2F8591FAD920A2822C';//转入
//    CONST BIND_TRANSFER_OUT = 'E57A779B2E064081B335C5D6B9E4B59B';//点位绑定转出
//    CONST BIND_TRANSFER_IN = '082485287F3148B7A44DC62D5D17473A';//点位绑定转入
//    CONST TEAMWORK_TRANSFER_OUT = '38EB53BCAAF24BA38B2E5FDA313EC928';//点位合作转出
//    CONST TEAMWORK_TRANSFER_IN = '64198F701D374D2996AA8EF1D37769AB';//点位合作转入
    CONST RECONSUME_POINTS_SEND = 'A6BDF8A2E5F7450B80CBF3B28D8010D2';//复消积分发放
//    CONST LX_SEND = '12840830348E456696834E0624FE2331';//领袖分红发放
    CONST RECONSUME_POINTS_OUT = '01C2916A2ECA4D1B9C29AA333C6D13E7';//复消积分转出
//    CONST LX_TRANSFER_OUT = '3CD904D1D0764A3FA3E9114DA1D930BD';//领袖分红转出
    CONST RECONSUME_POINTS_TRANSFER_IN = 'F6AA8D767E654D86917BBF1A5C24F439';//复消积分转入
//    CONST LX_TRANSFER_IN = '0C092D8D6D804F4B84134DBAB5BBE05C';//领袖分红转入
    CONST RESEND = '37B81A45F38646A0A16499AB27B51923';//补发
    CONST SMS = '1145D8ABA7A7471FB59B6760BDB19019';//短信服务费
    CONST DEDUCT_CLOSE = '37EFF25E5E3F46A6BB84B8C55997F276';//永久关停扣除
    CONST DEDUCT_LOGOUT = 'A1F1E76F34EE4981AB3BF4F8D20BB53E';//注销扣除
    CONST RECONSUME_POINTS_EXCHANGE = 'YA627DTHN81OBO7NIQ1OP8N3PGZX5C66';//复消积分兑换


    /**
     * {@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|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();
    }

    /**
     * 获取全部交易类型
     * @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 = $data['value'];
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return $result['ADMIN_NAME'];
                },
            ],
            'CREATED_AT' => [
                'label' => '创建时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
            'UPDATE_ADMIN' => [
                'label' => '修改人',
                'type' => function($data){
                    $value = $data['value'];
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return $result['ADMIN_NAME'];
                },
            ],
            'UPDATED_AT' => [
                'label' => '修改时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
            'CREATE_REMARK' => '备注',
            'SORT_ORDER' => '排序',
        ];
    }
}
