<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "crm_deposit".
 *
 * @property integer $id
 * @property integer $type
 * @property integer $member_id
 * @property string $amount
 * @property string $rmb
 * @property string $rate
 * @property string $memo
 * @property integer $in_time
 * @property string $pay_name
 * @property integer $login
 * @property string $admin_name
 */
class Deposit extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'crm_deposit';
    }

    /**
     * @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 [
            [['type', 'member_id', 'amount', 'rmb', 'rate', 'in_time', 'pay_name', 'login', 'order_sn'], 'required'],
            [['type', 'member_id', 'in_time', 'login'], 'integer'],
            [['amount', 'rmb', 'rate'], 'number'],
            [['memo', 'pay_name', 'admin_name', 'order_sn'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '主键ID',
            'type' => '出金类型',
            'member_id' => '代理商ID',
            'amount' => '金额',
            'rmb' => '人民币数',
            'rate' => '汇率',
            'memo' => '备注',
            'in_time' => '创建时间',
            'pay_name' => '支付方式',
            'login' => '登录ID',
            'admin_name' => '操作人',
            'order_sn' => '支付订单号',
        ];
    }

    /**
     * 统计入金金额
     * @param int $type
     * @return mixed
     */
    public static function sumDepositByType($type)
    {
        return static::find()->where(['type' => $type])->sum('amount');
    }

    /**
     * @param int $id
     * @return array|null|\yii\db\ActiveRecord|Deposit
     */
    public static function findById($id)
    {
        return static::find()->where(['id' => $id])->limit(1)->one();
    }
}

