package com.kingtom.shengtai.app.message.util;

import java.util.Map;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.kingtom.kirin.core.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 应用模块名称</p>
 * uniapp 云推送消息</p>
 * Copyright: Copyright (C) 2024 , Inc. All rights reserved. <p>
 * Company: 成都诚唐科技有限责任公司</p>
 *
 * @author wany
 * @since 2024/1/18
 */
@Slf4j
@Component
public class UniAppUtils{

    private static String url;

    @Value("${uniapp.cloud.url}")
    public void setUrl(String url){
        UniAppUtils.url = url;
    }

    public static void sendCloudMessage(String clientId, String body){
        if(StringUtils.isAnyEmpty(url, clientId, body)){
            return;
        }
        Map<String, Object> message = JsonUtils.toObject(body, Map.class);
        message.put("clientId", clientId);
        try(HttpResponse response = HttpUtil.createPost(url).body(JsonUtils.toJson(message)).contentType("application/json").execute()){
            if(response.getStatus() == 200){
                log.info("给客户端发送消息成功，消息内容：" + JsonUtils.toJson(message));
            } else{
                log.warn("给客户端发送消息失败，消息内容：" + JsonUtils.toJson(message));
            }
        }
    }

}
