MassageMsgCardSerivce.java 2.24 KB
package com.pipihelper.project.feishu.service.massage;

import com.alibaba.fastjson.JSONObject;
import com.pipihelper.project.feishu.dto.msg.FeiShuMsgDTO;
import com.pipihelper.project.feishu.service.FeiShuApiService;
import com.pipihelper.project.utils.GraphicsGenerationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description: TODO
 * @author: charles
 * @date: 2022年10月15日 16:17
 */
@Service
@Transactional
@Slf4j
public class MassageMsgCardSerivce {

    @Autowired
    private FeiShuApiService feiShuApiService;


    public String genMassageMsgCardForCompany(List<List<String>> pushUser){
        try {
            List<String> title = Arrays.asList("序号","姓名","部门","时间段","签到");
            pushUser.add(0,title);
            byte[] bufferedImage = GraphicsGenerationUtil.bufferedImageToByte(pushUser);
            //将图片上传
            String imgKey = feiShuApiService.uploadImage("message",  bufferedImage);
            String fileName  = String.format("/templates/massage-msg-card.json");
            String msgCardContent = String.format(getInteractiveCardStr(fileName), imgKey);
            return msgCardContent;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 读取模板文件
     * @param file
     * @return
     */
    public String getInteractiveCardStr(String file){
        try {
            InputStream in = this.getClass().getResourceAsStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(in);
            Stream<String> streamOfString= new BufferedReader(inputStreamReader).lines();
            String streamToString = streamOfString.collect(Collectors.joining());
            return streamToString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}