AbstractNoticeService.java 3.76 KB
package com.pipihelper.project.feishu.service.anniversary;

import com.pipihelper.project.feishu.dto.FeiShuConfig;
import com.pipihelper.project.feishu.dto.department.FeiShuDepartmentDTO;
import com.pipihelper.project.feishu.dto.employee.FeiShuEmployeeDTO;
import com.pipihelper.project.feishu.dto.msg.FeiShuMsgDTO;
import com.pipihelper.project.feishu.service.FeiShuApiService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public abstract class AbstractNoticeService {

    @Resource
    private FeiShuConfig feiShuConfig;

    @Autowired
    private FeiShuApiService feiShuApiService;

    /**
     * 发送通知
     */
    public abstract void notice();

    /**
     * 生成员工消息卡片
     * @param feiShuEmployeeDTO
     * @return
     */
    public abstract FeiShuMsgDTO generateEmployeeMsgCard(FeiShuEmployeeDTO feiShuEmployeeDTO);

    /**
     * 生产领导消息卡片
     * @param feiShuEmployeeDTO
     * @return
     */
    public abstract FeiShuMsgDTO generateLeaderMsgCard(FeiShuEmployeeDTO feiShuEmployeeDTO);

    /**
     * 获取领导OpenId
     *
     * @param departmentId
     * @return
     */
    public String getLeaderOpenId(String departmentId) {
        if (StringUtils.isBlank(departmentId)) {
            return "";
        }
        if ("0".equals(departmentId)) {
            // 若递归无上级负责人,则获取企业创建人
            return feiShuApiService.getUser("0", feiShuConfig.getAnniversaryApp()).getJSONObject("data").getJSONArray("items").getJSONObject(0).getString("open_id");
        }
        FeiShuDepartmentDTO feiShuDepartmentDTO = feiShuApiService.getDepartment(departmentId, feiShuConfig.getAnniversaryApp());
        return StringUtils.isNotBlank(feiShuDepartmentDTO.getLeaderUserId()) ? feiShuDepartmentDTO.getLeaderUserId() : getLeaderOpenId(feiShuDepartmentDTO.getParentDepartmentId());
    }

    /**
     * 给部门领导发送消息,若领导是自己,则找父部门领导
     *
     * @param feiShuMsgDTO
     * @param feiShuEmployeeDTO
     */
    public void sendMsgToLeader(FeiShuMsgDTO feiShuMsgDTO, FeiShuEmployeeDTO feiShuEmployeeDTO) {
        String leaderOpenId = getLeaderOpenId(feiShuEmployeeDTO.getSystemFields().getDepartmentId());
        // 领导是自己,找父部门领导
        if (leaderOpenId.equals(feiShuEmployeeDTO.getUserId()) && !"0".equals(feiShuEmployeeDTO.getSystemFields().getDepartmentId())) {
            leaderOpenId = getLeaderOpenId(feiShuApiService.getDepartment(feiShuEmployeeDTO.getSystemFields().getDepartmentId(), feiShuConfig.getAnniversaryApp()).getParentDepartmentId());
        }
        if (!leaderOpenId.equals(feiShuEmployeeDTO.getUserId()) && StringUtils.isNotBlank(leaderOpenId)) {
            // 给领导发送消息
            feiShuMsgDTO.setReceiveId(leaderOpenId);
            feiShuApiService.sendMsg(feiShuMsgDTO, "open_id", feiShuConfig.getAnniversaryApp());
        }
    }

    /**
     * 读取模板文件
     * @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 "";
    }
}