<?php

namespace App\Services\Log;

use App\Models\NoticeLogModel;
use App\Models\User\UserSysTagModel;
use App\Services\Service;

class NoticeLogService extends Service
{
    /**
     * 通过通知进入
     * @param  int  $uid
     * @return bool
     */
    public function logAfterNoticeLessHourInto(int $uid)
    {
        $systag = UserSysTagModel::findOrFail($uid);
        if (time() < $systag->last_send_notice_at + 3600) {
            $systag->increment('after_notice_less_h_into_cnt', 1);
        }

        return true;
    }

    /**
     * 通知日志记录
     * @param int|string $to_uid
     * @param string $title
     * @param string $notice_type
     * @param string $uuid
     * @param  int  $result
     * @param  string  $content
     * @return bool
     */
    public function record($to_user, $title, $notice_type, $uuid, int $result = 0, $content = "")
    {
        $uid = 0;
        $openid = '';
        if (is_int($to_user)) {
            $uid = $to_user;
        } else {
            $openid = $to_user;
        }
        if (is_array($content)) {
            $content = json_encode($content, JSON_UNESCAPED_UNICODE);
        }
        NoticeLogModel::create([
            'uid' => $uid,
            'title' => $title,
            'notice_type' => $notice_type,
            'content' => $content,
            'result' => $result,
        ]);
        if (app()->environment() == 'production') {
            \DB::connection('mysql_datalog')->table("notice_logs")->insert([
                'uid' => $uid,
                'openid' => $openid,
                'title' => $title,
                'notice_type' => $notice_type,
                'content' => $content,
                'uuid' => $uuid,
                'result' => $result,
                'created_at' => time(),
                'date' => date('Y-m-d'),
            ]);
        }

        return true;
    }
}
