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.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.usky.system.domain.MceRequestVO;

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

    @Autowired
    private MceMbuserService mceMbuserService;

    @Value("${appUrl}")
    private String appUrl;

    // 异步多线程调用
    @Override
    @Async
    public void sendApp(JSONObject mceReceiveVO, String cids, Integer mceReceiveId, Integer sendType) {
        if (sendType.equals(0)) {
            JsonObject jsonObject = new JsonObject();
            JsonObject jsonObject1 = new JsonObject();
            jsonObject1.addProperty("infoType", mceReceiveVO.get("infoType").toString());
            jsonObject1.addProperty("moduleId", (int) mceReceiveVO.get("id"));
            jsonObject.addProperty("cids", cids);
            jsonObject.addProperty("title", mceReceiveVO.get("infoTitle").toString());
            jsonObject.addProperty("content", mceReceiveVO.get("infoContent").toString());
            jsonObject.add("payload", jsonObject1);
            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);
        }
    }

    @Override
    @Async
    public void sendAppNew(MceRequestVO mceReceiveVO, String cids, Integer mceReceiveId, Integer sendType) {
        if (sendType.equals(0)) {
            JsonObject jsonObject = new JsonObject();
            JsonObject jsonObject1 = new JsonObject();
            jsonObject1.addProperty("infoType", mceReceiveVO.getInfoType());
            jsonObject1.addProperty("moduleId", mceReceiveVO.getId());
            jsonObject.addProperty("cids", cids);
            jsonObject.addProperty("title", mceReceiveVO.getInfoTitle());
            if ("3".equals(mceReceiveVO.getInfoType())) {
                jsonObject.addProperty("content", mceReceiveVO.getInfoContent().split("-")[0]);
            }
            jsonObject.addProperty("content", mceReceiveVO.getInfoContent());
            jsonObject.add("payload", jsonObject1);
            String resultString = HttpClientUtils.doPostJson(appUrl, jsonObject.toString());
        } else if (sendType.equals(1)) {
            SendWeChatMessageRequestVO sendWeChatMessageRequestVO = new SendWeChatMessageRequestVO();
            sendWeChatMessageRequestVO.setInfoContent(mceReceiveVO.getInfoContent());
            sendWeChatMessageRequestVO.setInfoTitle(mceReceiveVO.getInfoTitle());
            sendWeChatMessageRequestVO.setInfoId(mceReceiveId);
            sendWeChatMessageRequestVO.setOpenId(cids);
            sendWeChatMessageRequestVO.setInfoType(mceReceiveVO.getInfoType());
            if (StringUtils.isNotEmpty(mceReceiveVO.getInfoTypeName())) {
                sendWeChatMessageRequestVO.setInfoTypeName(mceReceiveVO.getInfoTypeName());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getIpAddress())) {
                sendWeChatMessageRequestVO.setIpAddress(mceReceiveVO.getIpAddress());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getUserName())) {
                sendWeChatMessageRequestVO.setUserName(mceReceiveVO.getUserName());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getLoginAddress())) {
                sendWeChatMessageRequestVO.setLoginAddress(mceReceiveVO.getLoginAddress());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getLoginType())) {
                sendWeChatMessageRequestVO.setLoginType(mceReceiveVO.getLoginType());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getApprovalResult())) {
                sendWeChatMessageRequestVO.setApprovalResult(mceReceiveVO.getApprovalResult());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getProcessName())) {
                sendWeChatMessageRequestVO.setProcessName(mceReceiveVO.getProcessName());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getApprovalNode())) {
                sendWeChatMessageRequestVO.setApprovalNode(mceReceiveVO.getApprovalNode());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getRealName())) {
                sendWeChatMessageRequestVO.setRealName(mceReceiveVO.getRealName());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getOaType())) {
                sendWeChatMessageRequestVO.setOaType(mceReceiveVO.getOaType());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getDeviceId())) {
                sendWeChatMessageRequestVO.setDeviceId(mceReceiveVO.getDeviceId());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getAlarmTime())) {
                sendWeChatMessageRequestVO.setAlarmTime(mceReceiveVO.getAlarmTime());
            }
            if (StringUtils.isNotEmpty(mceReceiveVO.getRemark())) {
                sendWeChatMessageRequestVO.setRemark(mceReceiveVO.getRemark());
            }
            mceMbuserService.sendWeChatMessage(sendWeChatMessageRequestVO);
        }
    }
}
