WeCustomerMessageOriginalServiceImpl.java
6.11 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
package com.linkwechat.wecom.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.utils.SnowFlakeUtil;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeCustomerMessagePushClient;
import com.linkwechat.wecom.domain.WeCustomerMessage;
import com.linkwechat.wecom.domain.WeCustomerMessageOriginal;
import com.linkwechat.wecom.domain.dto.message.*;
import com.linkwechat.wecom.domain.vo.CustomerMessagePushVo;
import com.linkwechat.wecom.mapper.WeCustomerMessageMapper;
import com.linkwechat.wecom.mapper.WeCustomerMessageOriginalMapper;
import com.linkwechat.wecom.mapper.WeCustomerMessgaeResultMapper;
import com.linkwechat.wecom.service.IWeCustomerMessageOriginalService;
import com.linkwechat.wecom.service.IWeCustomerMessageService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 群发消息 原始数据信息表 we_customer_messageOriginal
*
* @author kewen
* @date 2020-12-12
*/
@SuppressWarnings("all")
@Service
public class WeCustomerMessageOriginalServiceImpl extends ServiceImpl<WeCustomerMessageOriginalMapper, WeCustomerMessageOriginal> implements IWeCustomerMessageOriginalService {
@Autowired
private WeCustomerMessageOriginalMapper weCustomerMessageOriginalMapper;
@Autowired
private WeCustomerMessagePushClient weCustomerMessagePushClient;
@Autowired
private WeCustomerMessgaeResultMapper weCustomerMessgaeResultMapper;
@Autowired
private IWeCustomerMessageService weCustomerMessageService;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private WeCustomerMessageMapper customerMessageMapper;
@Override
public List<CustomerMessagePushVo> customerMessagePushs(String sender, String content, String pushType, String beginTime, String endTime) {
return weCustomerMessageOriginalMapper.selectCustomerMessagePushs(sender, content, pushType, beginTime, endTime);
}
@Override
public CustomerMessagePushVo CustomerMessagePushDetail(Long messageId) {
CustomerMessagePushVo customerMessagePushDetail = weCustomerMessageOriginalMapper.findCustomerMessagePushDetail(messageId);
//检查是否已经同步发送结果
if( weCustomerMessgaeResultMapper.checkSendStatus(messageId)==0){
//拉取消息发送结果
CompletableFuture.runAsync(() -> syncSendResult(customerMessagePushDetail.getMsgid(), messageId));
}
return customerMessagePushDetail;
}
@Override
public void asyncResult(AsyncResultDto asyncResultDto) throws JsonProcessingException {
String msgid=new ObjectMapper().writeValueAsString(asyncResultDto.getMsgids());
syncSendResult(msgid, asyncResultDto.getMessageId());
}
@Override
public long saveWeCustomerMessageOriginal(CustomerMessagePushDto customerMessagePushDto) {
//保存原始数据信息表 WeCustomerMessageOriginal 主键id
long messageOriginalId = SnowFlakeUtil.nextId();
WeCustomerMessageOriginal original = new WeCustomerMessageOriginal();
original.setMessageOriginalId(messageOriginalId);
original.setStaffId(customerMessagePushDto.getStaffId());
original.setDepartment(customerMessagePushDto.getDepartment());
original.setPushType(customerMessagePushDto.getPushType());
original.setMessageType(customerMessagePushDto.getMessageType());
original.setPushRange(customerMessagePushDto.getPushRange());
original.setTag(customerMessagePushDto.getTag());
original.setDelFlag(0);
this.save(original);
return messageOriginalId;
}
public void syncSendResult(String msgid, Long messageId) {
AtomicInteger atomicInteger = new AtomicInteger();
if (StringUtils.isNotEmpty(msgid)) {
List<String> msgIds = null;
try {
msgIds = objectMapper.readValue(msgid, new TypeReference<List<String>>() {
});
} catch (JsonProcessingException e) {
e.printStackTrace();
}
if (CollectionUtils.isNotEmpty(msgIds)) {
msgIds.forEach(m -> {
QueryCustomerMessageStatusResultDataObjectDto dataObjectDto = new QueryCustomerMessageStatusResultDataObjectDto();
dataObjectDto.setMsgid(m);
//拉取发送结果
QueryCustomerMessageStatusResultDto queryCustomerMessageStatusResultDto = weCustomerMessagePushClient.queryCustomerMessageStatus(dataObjectDto);
if (WeConstans.WE_SUCCESS_CODE.equals(queryCustomerMessageStatusResultDto.getErrcode())) {
List<DetailMessageStatusResultDto> detailList = queryCustomerMessageStatusResultDto.getDetail_list();
detailList.forEach(d -> {
// if (d.getStatus().equals(WeConstans.sendMessageStatusEnum.SEND.getStatus())) {
//
// atomicInteger.incrementAndGet();
// //更新消息发送状态
// customerMessageMapper.updateWeCustomerMessageCheckStatusById(messageId,WeConstans.sendMessageStatusEnum.SEND.getStatus());
// }
weCustomerMessgaeResultMapper.updateWeCustomerMessgaeResult(messageId, d.getChat_id(), d.getExternal_userid(), d.getStatus(), d.getSend_time());
});
}
});
}
}
//更新微信实际发送条数
weCustomerMessageService.updateWeCustomerMessageActualSend(messageId, atomicInteger.get());
}
}