AbstractNoticeService.java
3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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 "";
}
}