WeMomentsServiceImpl.java
6.92 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeMomentsClient;
import com.linkwechat.wecom.domain.WeMoments;
import com.linkwechat.wecom.domain.WeUser;
import com.linkwechat.wecom.domain.dto.WeResultDto;
import com.linkwechat.wecom.domain.dto.moments.MomentsParamDto;
import com.linkwechat.wecom.mapper.WeMomentsMapper;
import com.linkwechat.wecom.service.IWeMomentsService;
import com.linkwechat.wecom.service.IWeUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class WeMomentsServiceImpl extends ServiceImpl<WeMomentsMapper, WeMoments> implements IWeMomentsService {
@Autowired
WeMomentsClient weMomentsClient;
@Autowired
IWeUserService iWeUserService;
/**
* 发送更新朋友圈
* @param weMoments
*/
@Override
public void addOrUpdateMoments(WeMoments weMoments) {
String addUser = weMoments.getAddUser();
if(StringUtils.isNotEmpty(addUser)){
List<WeUser> weUsers = iWeUserService.listByIds(
ListUtil.toList(addUser.split(","))
);
if(CollectionUtil.isNotEmpty(weUsers)){
weMoments.setAddUserName(
Joiner.on(",").join( weUsers.stream().map(WeUser::getName).collect(Collectors.toList()))
);
}
List<WeUser> noWeUser = iWeUserService.list(new LambdaQueryWrapper<WeUser>()
.notIn(WeUser::getUserId, ListUtil.toList(addUser.split(",")))
);
if(CollectionUtil.isNotEmpty(noWeUser)){
weMoments.setNoAddUser(
Joiner.on(",").join( noWeUser.stream().map(WeUser::getUserId).collect(Collectors.toList()))
);
weMoments.setNoAddUserName(
Joiner.on(",").join( noWeUser.stream().map(WeUser::getName).collect(Collectors.toList()))
);
}
}
if(this.saveOrUpdate(weMoments)){
//企业动态,同步企业微信端
if(weMoments.getType()
.equals(new Integer(1))){
//附件封装
List<MomentsParamDto.BaseAttachments> attachments=new ArrayList<>();
WeMoments.OtherContent otherContent = weMoments.getOtherContent();
if(null != otherContent){
//图片
if(otherContent.getAnnexType().equals(new Integer(1))){
if(StringUtils.isNotEmpty(otherContent.getAnnexMediaid())){
Arrays.stream(otherContent.getAnnexMediaid().split(","))
.forEach(k->{
attachments.add(
MomentsParamDto.ImageAttachments.builder().image(
MomentsParamDto.Image.builder()
.mediaId(null)
.build()
).build()
);
});
}
}
//视频
if(otherContent.getAnnexType().equals(new Integer(2))){
if(StringUtils.isNotEmpty(otherContent.getAnnexMediaid())){
attachments.add(
MomentsParamDto.VideoAttachments.builder().video(
MomentsParamDto.Video.builder()
.mediaId(otherContent.getAnnexMediaid())
.build()
).build()
);
}
}
//链接
if(otherContent.getAnnexType().equals(new Integer(3))){
if(StringUtils.isNotEmpty(otherContent.getAnnexUrl())
&&StringUtils.isNotEmpty(otherContent.getAnnexMediaid())){
attachments.add(
MomentsParamDto.LinkAttachments.builder().link(
MomentsParamDto.Link.builder()
.url(otherContent.getAnnexUrl())
.mediaId(otherContent.getAnnexMediaid())
.build()
).build()
);
}
}
}
MomentsParamDto.VisibleRange visibleRange
= MomentsParamDto.VisibleRange.builder().build();
if(weMoments.getScopeType().equals(new Integer(2))){ //部分
if(StringUtils.isNotEmpty(weMoments.getCustomerTag())){ //客户标签
visibleRange.setExternal_contact_list(
MomentsParamDto.ExternalContactList.builder()
.tag_list(weMoments.getCustomerTag().split(","))
.build()
);
}
if(StringUtils.isNotEmpty(weMoments.getAddUser())){//指定发送人
visibleRange.setSender_list(
MomentsParamDto.SenderList.builder()
.user_list(weMoments.getAddUser().split(","))
.build()
);
}
}
WeResultDto weResultDto = weMomentsClient.addMomentTask(
MomentsParamDto.builder()
.text(MomentsParamDto.Text.builder()
.content(weMoments.getTextContent())
.build())
.visible_range(visibleRange)
.attachments(attachments)
.build()
);
//更新jobid
if(weResultDto.getErrcode().equals(WeConstans.WE_SUCCESS_CODE)){
weMoments.setJobId(weMoments.getJobId());
this.updateById(weMoments);
}
}
}
}
}