GroupOneService.java
9.86 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
package cn.com.glsx.entmicro.service;
import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.converter.GroupOneConvert;
import cn.com.glsx.entmicro.entity.CustomerBase;
import cn.com.glsx.entmicro.entity.GroupOne;
import cn.com.glsx.entmicro.entity.GroupOneTemplate;
import cn.com.glsx.entmicro.mapper.CustomerBaseMapper;
import cn.com.glsx.entmicro.mapper.GroupOneMapper;
import cn.com.glsx.entmicro.mapper.GroupOneTemplateMapper;
import cn.com.glsx.entmicro.model.request.GroupOneCustomerDetailPageRequest;
import cn.com.glsx.entmicro.model.request.GroupOneRequest;
import cn.com.glsx.entmicro.model.request.oneGroup.OneGroupCountRpcRequest;
import cn.com.glsx.entmicro.model.response.GroupOneCustomerDetailResponse;
import cn.com.glsx.entmicro.model.response.GroupOneResponse;
import cn.com.glsx.entmicro.model.response.oneGroup.OneGroupCountRpcResponse;
import cn.com.glsx.entmicro.model.response.oneGroup.OneGroupStaffCountRpcResponse;
import cn.com.glsx.entmicro.util.BeanConverUtil;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
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 com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Slf4j
@Service
public class GroupOneService{
@Resource
private GroupOneMapper groupOneMapper;
@Resource
private GroupOneTemplateMapper oneTemplateMapper;
@Resource
private CustomerBaseMapper customerBaseMapper;
public int deleteByPrimaryKey(Integer id) {
return groupOneMapper.deleteByPrimaryKey(id);
}
public Boolean insert(GroupOneRequest request) {
GroupOneResponse response = selectByCondition(request);
if(response != null){
throw new RpcServiceException(DefaultErrorEnum.DATA_EXIST);
}
GroupOne one = BeanConverUtil.conver(request, GroupOne.class);
one.setUserId(request.getExternalUserid());
one.setDelFlag(Constants.NORMAL_FLAG);
one.setCreatedTime(new Date());
groupOneMapper.insert(one);
return true;
}
public GroupOneResponse selectByCondition(GroupOneRequest request) {
Example example = new Example(GroupOne.class);
example.createCriteria().
andEqualTo("templateId", request.getTemplateId()).
andEqualTo("userId", request.getExternalUserid());
List<GroupOne> groupOnes = groupOneMapper.selectByExample(example);
if (groupOnes!=null&&!groupOnes.isEmpty()){
List<String> chatIds = groupOnes.stream().map(GroupOne::getChatId).collect(Collectors.toList());
Example example1 = new Example(CustomerBase.class);
example1.createCriteria().andIn("chatId",chatIds).andEqualTo("delFlag",Constants.NORMAL_FLAG);
List<CustomerBase> customerBases = customerBaseMapper.selectByExample(example1);
if (customerBases!=null&&!customerBases.isEmpty()){
return GroupOneConvert.convertGroupOneEntity(groupOnes.get(0));
}
}
return null;
}
public List<GroupOneResponse> selectByTemplateId(Integer templateId, Date startTime, Date endTime) {
return groupOneMapper.selectByTemplateId(templateId, startTime, endTime);
}
public List<GroupOneResponse> selectByExternalUserId(String externalUserId) {
if (StringUtils.isEmpty(externalUserId)) {
return null;
}
Example example = new Example(GroupOne.class);
example.createCriteria().andEqualTo("userId", externalUserId);
List<GroupOne> groupOneList = groupOneMapper.selectByExample(example);
List<String> nomalChatIds = new ArrayList<>();
if (groupOneList!=null&&!groupOneList.isEmpty()){
List<String> chatIds = groupOneList.stream().map(GroupOne::getChatId).collect(Collectors.toList());
Example example1 = new Example(CustomerBase.class);
example1.createCriteria().andIn("chatId",chatIds).andEqualTo("delFlag",Constants.NORMAL_FLAG);
List<CustomerBase> customerBases = customerBaseMapper.selectByExample(example1);
if (customerBases!=null&&!customerBases.isEmpty()){
nomalChatIds=customerBases.stream().map(CustomerBase::getChatId).collect(Collectors.toList());
}
if (!nomalChatIds.isEmpty()){
List<GroupOne> groupOneList1=new ArrayList<>();
for (GroupOne groupOne : groupOneList) {
if (nomalChatIds.contains(groupOne.getChatId())){
groupOneList1.add(groupOne);
}
}
return GroupOneConvert.convertGroupOneEntityList(groupOneList1);
}
}
return null;
}
/**
* @param templateId 一客一群模板id
* @return 此模板id创建的一客一群群聊数量
*/
public Integer countGroupNumberByTemplateId(Integer templateId){
Example example = new Example(GroupOne.class);
example.createCriteria().andEqualTo("templateId", templateId);
int i = groupOneMapper.selectCountByExample(example);
return i;
}
public RpcResponse<String> getGroupName(GroupOneRequest request) {
Integer templateId = request.getTemplateId();
GroupOneTemplate groupOneTemplate = oneTemplateMapper.selectByPrimaryKey(templateId);
Integer nameRule = groupOneTemplate.getGroupNameRule();
if (nameRule==null){
return RpcResponse.success(groupOneTemplate.getGroupName());
}
switch (nameRule){
case Constants.OneGroupName.ZERO:
return RpcResponse.success(groupOneTemplate.getGroupName());
case Constants.OneGroupName.ONE:
break;
case Constants.OneGroupName.TWO:
break;
default:
return RpcResponse.success(groupOneTemplate.getGroupName());
}
return null;
}
/**
* chatId查询
* @return
*/
public List<GroupOneResponse> getGroupOneByChatId(String chatId) {
return groupOneMapper.getGroupOneByChatId(chatId);
}
public Page<GroupOneCustomerDetailResponse> getGroupOneCustomerDetailList(RpcPagination<GroupOneCustomerDetailPageRequest> pagination){
RpcAssert.assertNotNull(pagination,DefaultErrorEnum.PARAMETER_NULL,"pagination param is must not be null!");
RpcAssert.assertNotNull(pagination.getCondition(),DefaultErrorEnum.PARAMETER_NULL,"condition param is must not be null!");
RpcAssert.assertNotNull(pagination.getCondition().getTemplateId(),DefaultErrorEnum.PARAMETER_NULL,"templateId param is must not be null!");
RpcAssert.assertNotNull(pagination.getCondition().getMerchantId(),DefaultErrorEnum.PARAMETER_NULL,"merchantId param is must not be null!");
GroupOneCustomerDetailPageRequest condition = pagination.getCondition();
PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
Page<GroupOneCustomerDetailResponse> page = groupOneMapper.selectGroupOneCustomers(condition.getMerchantId(),condition.getTemplateId());
if (page.getResult().isEmpty()){
return page;
}
List<GroupOneCustomerDetailResponse > list = page.getResult();
List<String> chatIds = list.stream().map(GroupOneCustomerDetailResponse ::getChatId).collect(Collectors.toList());
if (!chatIds.isEmpty()){
List<GroupOneCustomerDetailResponse > lords = groupOneMapper.selectLord(chatIds);
if (!lords.isEmpty()){
Map<String, GroupOneCustomerDetailResponse > map = lords.stream().collect(Collectors.toMap(GroupOneCustomerDetailResponse ::getChatId, Function.identity()));
list.forEach(s->{
GroupOneCustomerDetailResponse model = map.get(s.getChatId());
if (model!=null){
s.setLordName(model.getLordName());
s.setDepartName(model.getDepartName());
s.setDepartId(model.getDepartId());
}
});
}
}
return page;
}
/**
* 查询一客一群群聊统计
* @param request
* @return
*/
public OneGroupCountRpcResponse getOneGroupCount(OneGroupCountRpcRequest request) {
OneGroupCountRpcResponse response = new OneGroupCountRpcResponse();
response.setMerchantId(request.getMerchantId());
response.setStaffUuid(request.getStaffUuid());
response.setTotal(0);
response.setStaffCount(0);
List<OneGroupStaffCountRpcResponse> list = groupOneMapper.getOneGroupCount(request);
if (list.size() > 0) {
Map<String, OneGroupStaffCountRpcResponse> map = list.stream().collect(Collectors.toMap(OneGroupStaffCountRpcResponse::getChatId, Function.identity(), (s1, s2) -> s1));
map.forEach((k, v) -> response.setTotal(response.getTotal() + (v.getGroupCount() == null ? 0 : v.getGroupCount())));
for (OneGroupStaffCountRpcResponse staffCount : list) {
if (response.getStaffUuid().equals(staffCount.getOneId())) {
response.setStaffCount(response.getStaffCount() + (staffCount.getGroupCount() == null ? 0 : staffCount.getGroupCount()));
}
}
}
log.info("查询一客一群群聊统计返回:{}", JSON.toJSONString(response));
return response;
}
}