<?php

namespace App\Modules\User\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

class QappUserAddDesktopRealStats extends Model
{
    protected $table = 'qapp_user_add_desktop_real_stats';
    protected $fillable = [
        'send_order_id',
        'distribution_channel_id',
        'register_num',
        'add_desktop_num',
        'date',
    ];

    /**
     * 根据日期、派单id获取统计信息
     * @param string $date
     * @param int    $sendOrderId
     * @return array
     */
    public static function getDesktopRealStat(string $date, int $sendOrderId)
    {
        if (empty($date) || empty($sendOrderId)) {
            return [];
        }

        // 读主库
        return DB::connection('mysql::write')
            ->table('qapp_user_add_desktop_real_stats')
            ->where('date', $date)
            ->where('send_order_id', $sendOrderId)
            ->first();
    }

    /**
     * 写入数据
     * @param $insertData
     * @return bool
     */
    public static function insertDesktopRealStat($insertData)
    {
        if (empty($insertData)) {
            return false;
        }

        return self::insertGetId($insertData);
    }

    /**
     * 增加计数
     * @param string $date
     * @param int    $sendOrderId
     * @param string $field
     * @param int    $num
     * @return bool
     */
    public static function incrDesktopRealStat(string $date, int $sendOrderId, string $field, $num = 1)
    {
        if (empty($date) || empty($sendOrderId) || empty($field)) {
            return false;
        }

        return self::where('date', $date)
            ->where('send_order_id', $sendOrderId)
            ->increment($field, $num);
    }
}
