MassageService.java 6.66 KB
package com.pipihelper.project.feishu.service.massage;

import com.alibaba.fastjson.JSONObject;
import com.pipihelper.project.feishu.bo.PushPainBO;
import com.pipihelper.project.feishu.dto.FeiShuConfig;
import com.pipihelper.project.feishu.dto.chat.FeiShuChatDTO;
import com.pipihelper.project.feishu.dto.msg.FeiShuMsgDTO;
import com.pipihelper.project.feishu.entity.ChatMessage;
import com.pipihelper.project.feishu.entity.Pain;
import com.pipihelper.project.feishu.service.ChatMessageService;
import com.pipihelper.project.feishu.service.FeiShuApiService;
import com.pipihelper.project.feishu.service.PainService;
import com.pipihelper.project.utils.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @author: charles
 * @date: 2022年10月15日 17:21
 */
@Service
@Transactional
@Slf4j
public class MassageService {
    @Autowired
    private MassageMsgCardSerivce massageMsgCardSerivce;

    @Autowired
    private FeiShuApiService feiShuApiService;

    @Autowired
    private FeiShuConfig feiShuConfig;

    @Autowired
    private PainService painService;

    @Autowired
    private ChatMessageService chatMessageService;

    //给群里发送消息
    public void sendMassageMsgCardToPiPiChat(List<PushPainBO> pushPainBOList, String chatId) {
        List<List<String>> pushUser = new ArrayList<>();
        for (PushPainBO pushPainBO : pushPainBOList) {
            List<String> user = new ArrayList<>();
            if (chatId.equals(feiShuConfig.getChatId())) {
                //给单个用户发送
                String singleContent = massageMsgCardSerivce.genMassageMsgCardForSingle();
                log.info("给单个用户发送按摩消息:{}", singleContent);
                FeiShuMsgDTO feiShuMsgDTO = new FeiShuMsgDTO();
                feiShuMsgDTO.setMsgType("interactive");
                feiShuMsgDTO.setContent(singleContent);
                feiShuMsgDTO.setReceiveId(pushPainBO.getOpenId());
                log.info(feiShuMsgDTO.toString());
                JSONObject sendMsgResponse = feiShuApiService.sendMsg(feiShuMsgDTO, "open_id");
                String messageId = sendMsgResponse.getJSONObject("data").getString("message_id");
                //更新按摩记录表中的messageId
                Pain pain = painService.findByOpenId(pushPainBO.getOpenId());
                pain.setMessageId(messageId);
                painService.update(pain);
            }
            //构建给大群发送的名单
            user.add(pushPainBO.getIndex().toString());
            user.add(pushPainBO.getName());
            user.add(pushPainBO.getDepartMentName());
            user.add(pushPainBO.getTimeRange());
            user.add("");
            pushUser.add(user);
        }
        //给大群发送消息
        String content = massageMsgCardSerivce.genMassageMsgCardForCompany(pushUser);
        log.info("给大群发送按摩消息:{}", content);
        FeiShuMsgDTO feiShuMsgDTO = new FeiShuMsgDTO();
        feiShuMsgDTO.setMsgType("interactive");
        feiShuMsgDTO.setContent(content);
        feiShuMsgDTO.setReceiveId(chatId);
        log.info(feiShuMsgDTO.toString());
        JSONObject rep = feiShuApiService.sendMsg(feiShuMsgDTO, "chat_id");
        String messageId = rep.getJSONObject("data").getString("message_id");
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setChatId(chatId);
        chatMessage.setMessageId(messageId);
        chatMessage.setType(1);
        chatMessageService.create(chatMessage);

    }


    public void updateSingleMassageMsgCardWhenBegin(String messageId) {
        JSONObject patchMsg = new JSONObject();
        String msgCardContent = massageMsgCardSerivce.genMassageMsgCardForSingleStart();
        patchMsg.put("content", msgCardContent);
        feiShuApiService.patchMsg(messageId, patchMsg);
    }

    //更新群消息
    @Async
    public void updateMassageMsgCardToPiPiChat(String chatId) {
        List<Pain> pains = painService.findAllReorderByFloor(14);
        System.out.println(pains);
        String messageId = chatMessageService.findListChatAndType(chatId, 1).get(0).getMessageId();
        List<List<String>> pushUser = new ArrayList<>();
        for (Pain pain : pains) {
            List<String> user = new ArrayList<>();
            //构建给大群发送的名单
            user.add(pain.getPindex().toString());
            user.add(pain.getName());
            user.add(pain.getDepartMentName());
            user.add(pain.getTimeRange());
            user.add("");
            pushUser.add(user);
        }
        //给大群更新发送消息
        String content = massageMsgCardSerivce.genMassageMsgCardForCompany(pushUser);
        log.info("给大群发送按摩消息:{}", content);
        JSONObject patchMsg = new JSONObject();
        patchMsg.put("content", content);
        feiShuApiService.patchMsg(messageId, patchMsg);
    }

    @Async
    public void robSingle(String robUserId, String oldUserId, String name) {
        Pain oldPain = painService.findByOpenId(oldUserId);

        oldPain.setOpenId(robUserId);
        oldPain.setName(name);
        painService.update(oldPain);
        FeiShuChatDTO feiShuChatDTO = new FeiShuChatDTO();
        feiShuChatDTO.setChatId((String) CacheUtil.get("chatId"));
        feiShuChatDTO.setIdList(new String[]{robUserId});
        feiShuApiService.joinChatList(feiShuChatDTO);

        sendSingle(robUserId);
        // 更新大群和按摩群的大卡片,异步
        // 大群
        updateMassageMsgCardToPiPiChat(feiShuConfig.getChatId());
        //按摩群
        updateMassageMsgCardToPiPiChat((String) CacheUtil.get("chatId"));
    }

    public void sendSingle(String openId) {

        //给单个用户发送
        String singleContent = massageMsgCardSerivce.genMassageMsgCardForSingle();
        log.info("给单个用户发送按摩消息:{}", singleContent);
        FeiShuMsgDTO feiShuMsgDTO = new FeiShuMsgDTO();
        feiShuMsgDTO.setMsgType("interactive");
        feiShuMsgDTO.setContent(singleContent);
        feiShuMsgDTO.setReceiveId(openId);
        log.info(feiShuMsgDTO.toString());
        JSONObject sendMsgResponse = feiShuApiService.sendMsg(feiShuMsgDTO, "open_id");
        String messageId = sendMsgResponse.getJSONObject("data").getString("message_id");
        //更新按摩记录表中的messageId
        Pain pain = painService.findByOpenId(openId);
        pain.setMessageId(messageId);
        painService.update(pain);

    }
}