package com.zcloud.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fangjiakai
 * @date 2023/03/08 14:51
 */
public class PushUtil {
    static final String  push_url="https://fc-mp-356b2575-7ebe-471b-b689-8fd2b9b6c518.next.bspapp.com/unipush_gwj";
    private static final Map<String,String> PUSH_TYPE = new HashMap() {{
        put("first", "__UNI__A117F2D");
        put("other", "__UNI__A0F75C1");
    }};

    public static void push(Object cid,String title,String content,String type) {
        Map<String,Object> requestBody = new HashMap<String,Object>();
        requestBody.put("push_clientid", cid);
        requestBody.put("title", title);
        requestBody.put("content", content);
        requestBody.put("APPID", PUSH_TYPE.get(type));
        WebClient client = WebClient.create();
        client.post()
                .uri(push_url)
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromObject(new JSONObject(requestBody)))
                .exchange()
                .flatMap(response -> {
                    System.out.println("Status code: " + response.statusCode().value());
                    return response.bodyToMono(String.class);
                })
                .doOnSuccess(body -> {
                    System.out.println("Response body: " + body);
                })
                .doOnError(throwable -> {
                    System.out.println("Error occurred: " + throwable.getMessage());
                })
                .block();
    }

    public static void main(String[] args) {
        push("889ba5a8b47e432f36c3841df3c32480","标题","内容","other");
    }
}