package com.usky.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.usky.system.domain.MceContent;
import com.usky.system.mapper.MceContentMapper;
import com.usky.system.service.MceContentService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.system.service.MceMbuserService;
import com.usky.system.service.config.websocket.HttpClientUtils;
import com.usky.system.service.vo.SendWeChatMessageRequestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 消息内容表 服务实现类
 * </p>
 *
 * @author han
 * @since 2024-05-09
 */
@Slf4j
@Service
public class MceContentServiceImpl extends AbstractCrudService<MceContentMapper, MceContent> implements MceContentService {

    @Autowired
    private MceMbuserService mceMbuserService;

    //异步多线程调用
    @Async("asyncServiceExecutor")
    public void sendApp(JSONObject mceReceiveVO, String cids, Integer mceReceiveId, Integer sendType){
        if (sendType.equals(0)){
            String appUrl = "https://fc-mp-85a26092-1305-4bf1-998f-b609512c8f7b.next.bspapp.com/uniPushMessage";
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("cids", cids);
            jsonObject.addProperty("title", mceReceiveVO.get("infoTitle").toString());
            jsonObject.addProperty("content", mceReceiveVO.get("infoContent").toString());
            String resultString = HttpClientUtils.doPostJson(appUrl,jsonObject.toString());
        }else if (sendType.equals(1)){
            SendWeChatMessageRequestVO sendWeChatMessageRequestVO = new SendWeChatMessageRequestVO();
            sendWeChatMessageRequestVO.setInfoContent(mceReceiveVO.get("infoContent").toString());
            sendWeChatMessageRequestVO.setInfoTitle(mceReceiveVO.get("infoTitle").toString());
            sendWeChatMessageRequestVO.setInfoId(mceReceiveId);
            sendWeChatMessageRequestVO.setOpenId(cids);
            sendWeChatMessageRequestVO.setInfoType(mceReceiveVO.get("infoType").toString());
            if (mceReceiveVO.containsKey("infoTypeName")) {
                sendWeChatMessageRequestVO.setInfoTypeName(mceReceiveVO.get("infoTypeName").toString());
            }
            mceMbuserService.sendWeChatMessage(sendWeChatMessageRequestVO);
        }
    }
}
