<?php


namespace App\Modules\User\Services;


use App\Consts\QuickConst;
use App\Modules\SendOrder\Models\SendOrder;
use App\Modules\User\Models\QappUserAddDesktopRealStats;
use App\Modules\User\Models\User;

class QappAddDeskTopService
{
    /**
     * @param int    $uid
     * @param string $field
     * @param int    $num
     * @return bool
     */
    public static function incrAddDeskTop(int $uid, string $field, $num = 1): bool
    {
        //myLog('incrAddDeskTop')->info('start', compact('uid', 'field'));

        // 参数判断
        if ($uid < 1 || $num < 1 ||
            !in_array($field, [QuickConst::FIELD_ADD_DESKTOP, QuickConst::FIELD_REGISTER], true)) {
            return false;
        }

        // 获取用户信息
        $user        = User::getUser($uid);
        $sendOrderId = (int)getProp($user, 'send_order_id');
        $channelId   = (int)getProp($user, 'distribution_channel_id');
        //myLog('incrAddDeskTop')->info('user_data', compact('uid', 'sendOrderId', 'channelId'));
        if (empty($sendOrderId) || empty($channelId)) {
            return false;
        }

        // 获取渠道id
        $date     = date('Y-m-d', SERVER_TIME);
        $realStat = QappUserAddDesktopRealStats::getDesktopRealStat($date, $sendOrderId);
        if (!$realStat) {
            // 初始化统计数据
            QappUserAddDesktopRealStats::insertDesktopRealStat([
                'send_order_id'           => $sendOrderId,
                'distribution_channel_id' => $channelId,
                'date'                    => $date,
                'created_at'              => date('Y-m-d H:i:s', SERVER_TIME),
                'updated_at'              => date('Y-m-d H:i:s', SERVER_TIME),
            ]);
        }

        // 增加计数
        //myLog('incrAddDeskTop')->info('end', compact('date', 'sendOrderId', 'field', 'num'));
        return QappUserAddDesktopRealStats::incrDesktopRealStat($date, $sendOrderId, $field, $num);
    }
}