package com.ruoyi.push;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.code.Token;
import com.ruoyi.enums.user.UserEnums;
import com.ruoyi.vo.UserVo;
import io.goeasy.GoEasy;
import io.goeasy.publish.GoEasyError;
import io.goeasy.publish.PublishListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

/**
 * @author a
 * @date 2022/5/7 9:38
 */
@Component
@Slf4j
public class PushGoeasyUtil {


    @Value("${push.goeasy.regionHost}")
    private String regionHost;

    @Value("${push.goeasy.appkey}")
    private String appkey;

    /**
     * @param title 标题
     * @param content 内容
     * @param objectId
     * @param type
     * @param messageType
     */
    public void pushMessage(UserVo userVo, Integer messageType, String title, String content, Long objectId, Long messageId, Integer type) {

        GoEasy goEasy = new GoEasy(regionHost, appkey);
        Map<String, Object> map = new HashMap<>();
        map.put("title", title);//消息标题
        map.put("content", content + "");//消息内容
        map.put("objectId", objectId + "");//主体id
        map.put("messageType", messageType+"");//对应消息列表的类型
        map.put("messageId", messageId+"");//对应消息列表的id
        map.put("type", type + "");//普通推送消息类型

        String jsonString = JSONObject.toJSONString(map);

        String channel = "";

        switch (UserEnums.getUserEnums(userVo.getUserType())){
            case customer:
                channel = Token.customer + userVo.getUserId();
                break;
            case store:
                channel = Token.store + userVo.getUserId();
                break;
            case sale:
                channel = Token.sale + userVo.getUserId();
                break;

            case enterprise:
                channel = Token.enterprise + userVo.getUserId();
                break;

            default:
                throw new IllegalStateException("Unexpected value: " + userVo.getUserType());
        }
        goEasy.publish(channel, jsonString, title, content, new PublishListener() {
            @Override
            public void onSuccess() {
                log.info("消息发送成功");
            }

            @Override
            public void onFailed(GoEasyError error) {
                log.error("消息发送失败" + error.getCode() + " , " + error.getContent());
            }
        });


    }

    public static void main(String[] args) {
        GoEasy goEasy = new GoEasy("https://rest-hangzhou.goeasy.io", "BC-d4519ea15c3f4d67a0d923dc980cbdac");
        Map<String, Object> map = new HashMap<>();
        map.put("title", "b标题");//消息标题
        map.put("content", "消息内容");//消息内容
        map.put("objectId", "1111");//主体id
//        map.put("type", type+"");
        map.put("pushType", "2");//普通推送消息类型

        String jsonString = JSONObject.toJSONString(map);
        goEasy.publish("user511346310252466176", jsonString, "title", "content", new PublishListener() {
            @Override
            public void onSuccess() {
                log.info("消息发送成功");
            }

            @Override
            public void onFailed(GoEasyError error) {
                log.error("消息发送失败" + error.getCode() + " , " + error.getContent());
            }
        });
    }

}
