MassageService.java 4.45 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.msg.FeiShuMsgDTO;
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 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<>();
            //给单个用户发送
            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());
        feiShuApiService.sendMsg(feiShuMsgDTO, "chat_id");
    }


    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.findAll();
        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.getIndex().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);
    }




}