WeCustomerMessagePushServiceImpl.java
7.97 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
184
185
186
187
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.exception.wecom.WeComException;
import com.linkwechat.common.utils.DateUtils;
import com.linkwechat.common.utils.SnowFlakeUtil;
import com.linkwechat.wecom.client.WeCustomerMessagePushClient;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeCustomerMessageTimeTask;
import com.linkwechat.wecom.domain.WeGroup;
import com.linkwechat.wecom.domain.dto.message.CustomerMessagePushDto;
import com.linkwechat.wecom.domain.vo.CustomerMessagePushVo;
import com.linkwechat.wecom.mapper.WeCustomerMessageTimeTaskMapper;
import com.linkwechat.wecom.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @description: 群发消息服务类
* @author: KeWen
* @create: 2020-12-01 20:23
**/
@Slf4j
@Service
@SuppressWarnings("all")
public class WeCustomerMessagePushServiceImpl implements IWeCustomerMessagePushService {
@Autowired
private IWeCustomerMessageOriginalService weCustomerMessageOriginalService;
@Autowired
private WeCustomerMessagePushClient weCustomerMessagePushClient;
@Autowired
private IWeCustomerMessageService weCustomerMessageService;
@Autowired
private IWeCustomerSeedMessageService weCustomerSeedMessageService;
@Autowired
private IWeCustomerService weCustomerService;
@Autowired
private IWeGroupService weGroupService;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private IWeCustomerMessgaeResultService weCustomerMessgaeResultService;
@Autowired
private RedisCache redisCache;
@Autowired
private WeCustomerMessageTimeTaskMapper customerMessageTimeTaskMapper;
@Override
@Transactional(rollbackFor = Exception.class)
public void addWeCustomerMessagePush(CustomerMessagePushDto customerMessagePushDto) throws JsonProcessingException, ParseException {
// if ((null != customerMessagePushDto.getSettingTime() && !"".equals(customerMessagePushDto.getSettingTime()))
// && DateUtils.diffTime(new Date(), DateUtil.parse(customerMessagePushDto.getSettingTime(), "yyyy-MM-dd HH:mm")) > 0) {
// throw new WeComException("发送时间不能小于当前时间");
// }
if(StrUtil.isNotBlank(customerMessagePushDto.getSettingTime())){
if(DateUtils.diffTime(new Date(), DateUtil.parse(customerMessagePushDto.getSettingTime(), "yyyy-MM-dd HH:mm")) > 0){
throw new WeComException("发送时间不能小于当前时间");
}
}
/*if(StrUtil.isBlank(customerMessagePushDto.getSettingTime())){
throw new WeComException("发送时间不能为空");
}*/
List<WeCustomer> customers = Lists.newArrayList();
List<WeGroup> groups = new ArrayList<>();
// 0 发给客户
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_CUSTOMER)) {
//查询客户信息列表
if ("all".equals(customerMessagePushDto.getTag())) {
customers = externalUserIds(WeConstans.SEND_MESSAGE_CUSTOMER_ALL, customerMessagePushDto.getStaffId(), customerMessagePushDto.getDepartment(), customerMessagePushDto.getTag());
} else {
customers = externalUserIds(customerMessagePushDto.getPushRange(), customerMessagePushDto.getStaffId()
, customerMessagePushDto.getDepartment(), customerMessagePushDto.getTag());
}
if (CollectionUtils.isEmpty(customers)) {
throw new WeComException("没有外部联系人");
}
}
// 1 发给客户群
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_GROUP)) {
if (customerMessagePushDto.getStaffId() == null || customerMessagePushDto.getStaffId().equals("")) {
throw new WeComException("请选择人员!");
}
//通过员工id查询群列表
WeGroup weGroup = new WeGroup();
weGroup.setUserIds(customerMessagePushDto.getStaffId());
groups = weGroupService.selectWeGroupList(weGroup);
if (CollectionUtils.isEmpty(groups)) {
throw new WeComException("没有客户群!");
}
}
//保存原始数据信息表
long messageOriginalId = weCustomerMessageOriginalService.saveWeCustomerMessageOriginal(customerMessagePushDto);
long messageId = SnowFlakeUtil.nextId();
//保存映射信息
int size = weCustomerMessgaeResultService.workerMappingCustomer(customerMessagePushDto, messageId, customers, groups);
//保存微信消息
weCustomerMessageService.saveWeCustomerMessage(messageId, messageOriginalId, customerMessagePushDto, size, customerMessagePushDto.content());
//保存分类消息信息
weCustomerSeedMessageService.saveSeedMessage(customerMessagePushDto, messageId);
//发送群发消息
//调用微信api发送消息
if (null == customerMessagePushDto.getSettingTime() || customerMessagePushDto.getSettingTime().equals("")) {
weCustomerMessageService.sendMessgae(customerMessagePushDto, messageId, customers, groups);
} else {
WeCustomerMessageTimeTask timeTask = new WeCustomerMessageTimeTask(messageId, customerMessagePushDto, customers, groups
, DateUtils.getMillionSceondsBydate(customerMessagePushDto.getSettingTime()));
customerMessageTimeTaskMapper.saveWeCustomerMessageTimeTask(timeTask);
}
}
@Override
public List<CustomerMessagePushVo> customerMessagePushs(String sender, String content, String pushType, String beginTime, String endTime) {
return weCustomerMessageOriginalService.customerMessagePushs(sender, content, pushType, beginTime, endTime);
}
/**
* 客户的外部联系人id列表,仅在chat_type为single时有效,不可与sender同时为空,最多可传入1万个客户
*
* @param pushRange 消息范围 0 全部客户 1 指定客户
* @param staffId 员工id
* @param department 部门id
* @param tag 客户标签id列表
* @return {@link List}s 客户的外部联系人id列表
*/
public List<WeCustomer> externalUserIds(String pushRange, String staffId, String department, String tag) {
if (pushRange.equals(WeConstans.SEND_MESSAGE_CUSTOMER_ALL)) {
//从redis中读取数据
List<WeCustomer> customers = redisCache.getCacheList(WeConstans.WECUSTOMERS_KEY);
if (CollectionUtils.isEmpty(customers)) {
customers = weCustomerService.selectWeCustomerList(null);
redisCache.setCacheList(WeConstans.WECUSTOMERS_KEY, customers);
}
WeCustomer weCustomer = new WeCustomer();
weCustomer.setUserIds(staffId);
weCustomer.setDepartmentIds(department);
return weCustomerService.selectWeCustomerList(weCustomer);
} else {
//按条件查询客户
//通过部门id查询所有的员工
//通过员工列表查询该员工的外部联系人列表
WeCustomer weCustomer = new WeCustomer();
weCustomer.setUserIds(staffId);
weCustomer.setTagIds(tag);
weCustomer.setDepartmentIds(department);
return weCustomerService.selectWeCustomerList(weCustomer);
}
}
}