MassDetailStaffService.java
10.8 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package cn.com.glsx.entmicro.service;
import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.constants.DracConstant;
import cn.com.glsx.entmicro.converter.MassDetailStaffConvert;
import cn.com.glsx.entmicro.entity.Mass;
import cn.com.glsx.entmicro.entity.MassDetailCustomer;
import cn.com.glsx.entmicro.entity.MassDetailStaff;
import cn.com.glsx.entmicro.entity.mass.MassStatisticItemEntity;
import cn.com.glsx.entmicro.mapper.MassDetailCustomerMapper;
import cn.com.glsx.entmicro.mapper.MassDetailStaffMapper;
import cn.com.glsx.entmicro.model.request.mass.MassStaffDetailListRpcRequest;
import cn.com.glsx.entmicro.model.response.MassDetailStaffResponse;
import cn.com.glsx.entmicro.model.response.MassStaffSendStatusResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.core.util.RpcAssert;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class MassDetailStaffService {
@Resource
private MassDetailStaffMapper massDetailStaffMapper;
@Resource
private MassDetailCustomerMapper massDetailCustomerMapper;
/**
* 批量保存群发 员工列表
* @param staffList
* @return
*/
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
public Boolean insertMassDetailStaffList(List<MassDetailStaff> staffList) {
if (CollectionUtils.isEmpty(staffList)) {
return false;
}
massDetailStaffMapper.insertList(staffList);
return true;
}
/**
* 通过群发ID获取员工列表
* @param massId
* @return
*/
public List<MassDetailStaffResponse> getStaffListByMassId(Integer massId) {
Example selectCondition = new Example(MassDetailStaff.class);
selectCondition.createCriteria().
andEqualTo("massId", massId).
andEqualTo("delFlag", DracConstant.DelFlag.OK);
List<MassDetailStaff> massDetailList = massDetailStaffMapper.selectByExample(selectCondition);
return MassDetailStaffConvert.convertStaffList(massDetailList);
}
/**
* 更新微信消息-msgId
* @param id
* @param msgId
*/
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
public Boolean updateQyMessageSenderResult(Integer id, String msgId) {
if (StringUtils.isEmpty(msgId) || id==null) {
return false;
}
MassDetailStaff massDetailStaff = new MassDetailStaff();
massDetailStaff.setId(id);
massDetailStaff.setMsgId(msgId);
massDetailStaffMapper.updateByPrimaryKeySelective(massDetailStaff);
return true;
}
/**
* 更新状态
* @param id
* @param status
* @return
*/
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
public Boolean updateQyMessageSenderResultStatus(Integer id, Integer status) {
if (status==null || id==null) {
return false;
}
MassDetailStaff massDetailStaff = new MassDetailStaff();
massDetailStaff.setId(id);
massDetailStaff.setStatus(status);
massDetailStaffMapper.updateByPrimaryKeySelective(massDetailStaff);
//更新客户接收状态为失败
MassDetailCustomer customer = new MassDetailCustomer();
customer.setStatus(Constants.MASS_DETAIL_CUSTOMER_STATUS.SEND_ERROR_FOR_OTHER_MSG);
Example example = new Example(MassDetailCustomer.class);
example.createCriteria().andEqualTo("staffRelationId",id);
massDetailCustomerMapper.updateByExampleSelective(customer,example);
return true;
}
/**
* 根据massId更新员工发送状态
* @param massId
* @param status
* @return
*/
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
public Boolean updateQyMessageSenderStatusByMassId(Integer massId, Integer status) {
if (status==null || massId==null) {
return false;
}
MassDetailStaff massDetailStaff = new MassDetailStaff();
massDetailStaff.setStatus(status);
Example massDetailStaffExample = new Example(MassDetailStaff.class);
massDetailStaffExample.createCriteria().andEqualTo("massId",massId);
List<MassDetailStaff> massDetailStaffs = massDetailStaffMapper.selectByExample(massDetailStaffExample);
List<Integer> massDetailStaffIds = massDetailStaffs.stream().map(MassDetailStaff::getId).collect(Collectors.toList());
massDetailStaffMapper.updateByExampleSelective(massDetailStaff,massDetailStaffExample);
//更新客户接收状态为失败
MassDetailCustomer customer = new MassDetailCustomer();
customer.setStatus(Constants.MASS_DETAIL_CUSTOMER_STATUS.SEND_ERROR_FOR_OTHER_MSG);
Example example = new Example(MassDetailCustomer.class);
example.createCriteria().andIn("staffRelationId",massDetailStaffIds);
massDetailCustomerMapper.updateByExampleSelective(customer,example);
return true;
}
/**
* 根据msgId更新员工发送状态
* @param msgId
* @param status
* @return
*/
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
public Boolean updateQyMessageSenderResultStatusByMsgId(String msgId, Integer status) {
if (status==null || msgId==null) {
return false;
}
Example example = new Example(MassDetailStaff.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("msgId",msgId);
MassDetailStaff massDetailStaff = new MassDetailStaff();
massDetailStaff.setStatus(status);
massDetailStaffMapper.updateByExampleSelective(massDetailStaff,example);
//更新客户接收状态为失败-3
massDetailCustomerMapper.updateMassDetailCustomerSendStatus(msgId);
return true;
}
public Map<Integer, Integer> countStaffSender(Integer massId) {
List<MassStatisticItemEntity> massStatisticItemEntityList = massDetailStaffMapper.countStaffSender(massId);
if (CollectionUtils.isEmpty(massStatisticItemEntityList)) {
return new HashMap<>();
}
Map<Integer, Integer> result = massStatisticItemEntityList.stream().collect(Collectors.toMap(MassStatisticItemEntity::getDataType, MassStatisticItemEntity::getDataCount, (item1, item2) -> item1));
return result;
}
public List<MassDetailStaffResponse> getStaffListByMassIdAndStatus(Integer massId, Integer status) {
Example selectCondition = new Example(MassDetailStaff.class);
Example.Criteria criteria = selectCondition.createCriteria();
criteria.andEqualTo("massId", massId).
andEqualTo("delFlag", DracConstant.DelFlag.OK);
if (status!=null) {
criteria.andEqualTo("status", status);
}
List<MassDetailStaff> massDetailList = massDetailStaffMapper.selectByExample(selectCondition);
// 组装客户数
buildCustomerCount(massDetailList, massId, status);
return MassDetailStaffConvert.convertStaffList(massDetailList);
}
private void buildCustomerCount(List<MassDetailStaff> massDetailList, Integer massId, Integer status) {
if (CollectionUtils.isEmpty(massDetailList)) {
return ;
}
// 补充客户数量
List<MassStatisticItemEntity> massStaffCustomerCountList = massDetailStaffMapper.getStaffCustomerCount(massId, status);
if (CollectionUtils.isEmpty(massStaffCustomerCountList)) {
return;
}
Map<Integer, Integer> customerCountMapper = massStaffCustomerCountList.stream().collect(Collectors.toMap(MassStatisticItemEntity::getDataType, MassStatisticItemEntity::getDataCount, (item1, item2) -> item1));
for (MassDetailStaff staff:massDetailList) {
staff.setCustomerNum(customerCountMapper.get(staff.getStaffId()));
}
}
public void updateQyMessageSenderStatus(Integer msgId, Integer status, Date date) {
MassDetailStaff massDetailStaff = new MassDetailStaff();
massDetailStaff.setId(msgId);
massDetailStaff.setStatus(status);
massDetailStaff.setSendTime(date);
massDetailStaffMapper.updateByPrimaryKeySelective(massDetailStaff);
}
/**
* 通过主键获取群发员工记录信息
* @param id
* @return
*/
public MassDetailStaff getStaffDetailById(Integer id) {
if (id==null) {
return null;
}
MassDetailStaff selectCondition = new MassDetailStaff();
selectCondition.setId(id);
MassDetailStaff massDetailStaff = massDetailStaffMapper.selectByPrimaryKey(selectCondition);
return massDetailStaff;
}
/**
* 根据massId判断是否创建任务中心任务
* @param massId
* @return
*/
public List<MassDetailStaff> canAddTask(Integer massId) {
Example example = new Example(MassDetailStaff.class);
example.createCriteria()
.andEqualTo("massId",massId)
.andIn("status", Arrays.asList(Constants.MASS_DETAIL_STAFF_STATUS.UNSEND,Constants.MASS_DETAIL_STAFF_STATUS.SENDED))
.andIsNotNull("msgId")
.andEqualTo("delFlag",Constants.NORMAL_FLAG);
List<MassDetailStaff> massDetailStaffs = massDetailStaffMapper.selectByExample(example);
return massDetailStaffs;
}
public List<Integer> getSucessStaffListByMass(Mass mass) {
if (mass==null||mass.getId()==null){
return null;
}
List<Integer> list = massDetailStaffMapper.getSucessStaffListByMass(mass.getId());
return list;
}
public MassStaffSendStatusResponse selectMassStaffDetail(MassStaffDetailListRpcRequest request){
RpcAssert.assertNotNull(request, DefaultErrorEnum.PARAMETER_NULL,"参数不能为空!");
RpcAssert.assertNotNull(request.getMassId(), DefaultErrorEnum.PARAMETER_NULL,"massId 参数不能为空!");
RpcAssert.assertNotNull(request.getStaffId(), DefaultErrorEnum.PARAMETER_NULL,"staffId 参数不能为空!");
List<MassStaffSendStatusResponse> massDetailStaffs = massDetailStaffMapper.selectMassStaffDetail(request);
if (massDetailStaffs!=null && !massDetailStaffs.isEmpty()){
return massDetailStaffs.get(0);
}
return null;
}
}