<?php

namespace backend\models;

use common\helpers\Utils;
use Yii;

/**
 * This is the model class for table "crm_batch_remit_log".
 *
 * @property integer $id
 * @property string $batch_no
 * @property string $memo
 * @property integer $admin_id
 * @property string $admin_name
 * @property string $admin_ip
 * @property integer $create_time
 */
class BatchRemitLog extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'crm_batch_remit_log';
    }

    /**
     * @return null|object
     * @throws \yii\base\InvalidConfigException
     */
    public static function getDb()
    {
        return Yii::$app->get('dbXcrm');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['batch_no', 'create_time'], 'required'],
            [['batch_no', 'admin_id', 'create_time'], 'integer'],
            [['memo'], 'string', 'max' => 255],
            [['admin_name', 'admin_ip'], 'string', 'max' => 32],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'batch_no' => 'Batch No',
            'memo' => 'Memo',
            'admin_id' => 'Admin ID',
            'admin_name' => 'Admin Name',
            'admin_ip' => 'Admin Ip',
            'create_time' => 'Create Time',
        ];
    }

    /**
     * 记录日志
     * @param $remitOrder
     * @param array $logData
     * @return mixed
     */
    public static function addLog($remitOrder, $logData = [])
    {
        $model = new static();
        $model->batch_no = $remitOrder['batch_no'];
        isset($logData['admin_id']) && $model->admin_id = trim($logData['admin_id']);
        isset($logData['admin_name']) && $model->admin_name = trim($logData['admin_name']);
        $model->admin_ip = isset($logData['admin_ip']) ? trim($logData['admin_ip']) : Utils::getClientIp();
        isset($logData['memo']) && $model->memo = trim($logData['memo']);
        $model->create_time = time();
        return $model->save();
    }
}
