<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "crm_config".
 *
 * @property integer $id
 * @property string $sys_name
 * @property string $lever
 * @property string $smtp_from_mail
 * @property string $smtp_server
 * @property integer $smtp_port
 * @property string $smtp_username
 * @property string $smtp_password
 * @property integer $smtp_is_ssl
 * @property string $juhe_sms_key
 * @property integer $juhe_sms_tpl_id
 * @property integer $juhe_sms_mt4_tpl_id
 * @property integer $juhe_sms_reg_tpl_id
 * @property string $forex_symbols
 * @property string $cfd_symbols
 * @property string $xag_symbols
 * @property string $xau_symbols
 * @property string $metal_symbols
 * @property string $stock_symbols
 * @property string $btc_symbols
 * @property string $banks
 * @property string $mt4_manager_ip
 * @property integer $mt4_manager_login
 * @property string $mt4_manager_pwd
 * @property string $mt4_groups
 * @property string $end_his_trade_time
 * @property string $commission_deal_time
 * @property string $mail_code
 * @property string $mail_open
 * @property string $mail_ib_open
 * @property string $agreement
 * @property string $mail_deposit
 * * @property string $mail_reject_apply
 */
class Config extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'crm_config';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->get('dbXcrm');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['smtp_from_mail', 'smtp_server', 'smtp_port', 'smtp_username', 'smtp_password', 'juhe_sms_key', 'juhe_sms_tpl_id', 'juhe_sms_mt4_tpl_id', 'juhe_sms_reg_tpl_id', 'forex_symbols', 'cfd_symbols', 'xag_symbols', 'xau_symbols', 'metal_symbols', 'banks', 'mt4_manager_ip', 'mt4_manager_port', 'mt4_manager_login', 'mt4_manager_pwd', 'mt4_groups', 'end_his_trade_time', 'commission_deal_time', 'mail_code', 'mail_open', 'mail_ib_open', 'agreement', 'mail_deposit', 'mail_same_account','mail_reject_apply'], 'required'],
            [['smtp_port', 'smtp_is_ssl', 'juhe_sms_tpl_id', 'juhe_sms_mt4_tpl_id', 'juhe_sms_reg_tpl_id', 'mt4_manager_login', 'auto_deposit'], 'integer'],
            [['forex_symbols', 'cfd_symbols', 'xag_symbols', 'xau_symbols', 'metal_symbols', 'stock_symbols', 'btc_symbols', 'banks', 'mt4_groups', 'mail_code', 'mail_open', 'mail_ib_open', 'agreement', 'mail_deposit','mail_same_account','mail_reject_apply'], 'string'],
            [['end_his_trade_time', 'commission_deal_time'], 'safe'],
            [['sys_name', 'lever', 'smtp_from_mail', 'smtp_server', 'smtp_username', 'smtp_password', 'juhe_sms_key', 'mt4_manager_ip', 'mt4_manager_pwd',  'mt4_manager_port'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '主键ID',
            'sys_name' => '系统名称',
            'lever' => '杠杆',
            'smtp_from_mail' => '发送邮件名称',
            'smtp_server' => '发送邮件服务器名称',
            'smtp_port' => '发送邮件端口',
            'smtp_username' => '邮件发送人',
            'smtp_password' => '邮件发送密码',
            'smtp_is_ssl' => '邮件发送是否ssl',
            'juhe_sms_key' => 'juhe发短信key',
            'juhe_sms_tpl_id' => 'juhe发短信模板ID',
            'juhe_sms_mt4_tpl_id' => 'juhe发mt4短信模板ID',
            'juhe_sms_reg_tpl_id' => 'juhe发注册短信模板ID',
            'forex_symbols' => 'forex列表',
            'cfd_symbols' => 'cfd列表',
            'xag_symbols' => '白银',
            'xau_symbols' => '黄金',
            'metal_symbols' => 'metal列表',
            'stock_symbols' => '股指',
            'btc_symbols' => 'BTC',
            'banks' => '银行列表',
            'mt4_manager_ip' => 'mt4管理员IP',
            'mt4_manager_port' => 'mt4开户IP端口',
            'mt4_manager_login' => 'mt4管理员登录ID',
            'mt4_manager_pwd' => 'mt4管理员密码',
            'mt4_groups' => 'mt4组别',
            'end_his_trade_time' => '结束交易时间',
            'commission_deal_time' => 'commission时间',
            'mail_code' => '验证邮件内容',
            'mail_open' => 'xtrader开户邮件内容',
            'mail_same_account' => '同名账户邮件模板',
            'mail_ib_open' => 'ib开户邮件内容',
            'agreement' => '协议内容',
            'mail_deposit' => '出金邮件内容',
            'mail_reject_apply' => '开户拒绝申请审核',
            'auto_deposit' => '是否自动入金',
        ];
    }

    /**
     * 事件
     * @throws \yii\db\Exception
     */
    public function updateEvent()
    {
        $config = Config::findOne(1);
        $connection = Mt4Trades::getDb();
        $connection->createCommand("delete from mt4_trades where CLOSE_TIME !='1970-01-01' and close_time<:close_time", [
            ':close_time' => $config->end_his_trade_time
        ])->execute();
    }
}
