WeCustomerSeasController.java
10.2 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.linkwechat.web.controller.wecom;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.core.page.TableDataInfo;
import com.linkwechat.common.enums.CommunityTaskType;
import com.linkwechat.common.exception.CustomException;
import com.linkwechat.common.utils.DateUtils;
import com.linkwechat.common.utils.SnowFlakeUtil;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.common.utils.ValidateUtils;
import com.linkwechat.common.utils.poi.ExcelUtil;
import com.linkwechat.wecom.domain.WeCustomerSeas;
import com.linkwechat.wecom.domain.vo.CustomerSeasRecordVo;
import com.linkwechat.wecom.service.IWeCustomerMessagePushService;
import com.linkwechat.wecom.service.IWeCustomerSeasService;
import com.linkwechat.wecom.service.IWeMessagePushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* 客户公海相关
*/
@RestController
@RequestMapping("/wecom/seas")
public class WeCustomerSeasController extends BaseController {
@Autowired
private IWeCustomerSeasService iWeCustomerSeasService;
/**
* 客户公海模版下载
* @return
*/
@GetMapping("/importTemplate")
public AjaxResult importTemplate()
{
ExcelUtil<WeCustomerSeas> util = new ExcelUtil<WeCustomerSeas>(WeCustomerSeas.class);
return util.importTemplateExcel( DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD)+"_客户公海");
}
/**
* 导入公海数据
* @param file
* @param weCustomerSea
* @return
*/
@PostMapping("/importData")
public AjaxResult importData(MultipartFile file, WeCustomerSeas weCustomerSea) throws Exception {
ExcelUtil<WeCustomerSeas> util = new ExcelUtil<WeCustomerSeas>(WeCustomerSeas.class);
List<WeCustomerSeas> weCustomerSeas = util.importExcel(file.getInputStream());
String tip=new String("成功导入{0}条,去重复{1}条");
if(CollectionUtil.isNotEmpty(weCustomerSeas)){
if(StringUtils.isEmpty(weCustomerSea.getAddUserName())&&StringUtils.isEmpty(weCustomerSea.getAddUserId())){
return AjaxResult.error("分配人不可为空!");
}
//过滤不符合规范的手机号
List<WeCustomerSeas> noRuleWeCustomerSeas
= weCustomerSeas.stream().filter(s -> ValidateUtils.isMobile(s.getPhone())).collect(Collectors.toList());
if(CollectionUtil.isEmpty(noRuleWeCustomerSeas)){
return AjaxResult.error("请传入合法手机号!");
}
//过滤字段为空的数据
List<WeCustomerSeas> deduplicationSeas = noRuleWeCustomerSeas.stream().filter(s -> StringUtils.isNotEmpty(s.getCustomerName())
|| StringUtils.isNotEmpty(s.getPhone())).collect(Collectors.toList());
if(CollectionUtil.isEmpty(deduplicationSeas)){
return AjaxResult.error("导入用户数据不能为空!");
}
//根据手机号去重(去除excel中重复的号码)
List<WeCustomerSeas> deduplicationSeasNoRepeat=deduplicationSeas.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(WeCustomerSeas :: getPhone))), ArrayList::new));
//根据手机号去除数据库与excel中重复号码
List<WeCustomerSeas> dbExist = iWeCustomerSeasService.list(new LambdaQueryWrapper<WeCustomerSeas>()
.in(WeCustomerSeas::getPhone, deduplicationSeasNoRepeat.stream()
.map(WeCustomerSeas::getPhone).collect(Collectors.toList())));
if(CollectionUtil.isNotEmpty(dbExist)){
List<WeCustomerSeas> noRepetWeCustomerSeas
= deduplicationSeasNoRepeat.stream().filter(item ->
!dbExist.stream().map(e->e.getPhone()).collect(Collectors.toList())
.contains(item.getPhone())).collect(Collectors.toList());
deduplicationSeasNoRepeat.clear();
deduplicationSeasNoRepeat.addAll(
noRepetWeCustomerSeas
);
}
if(CollectionUtil.isNotEmpty(deduplicationSeasNoRepeat)){
Long tabaleExcelId= SnowFlakeUtil.nextId();
List<String> userIds = ListUtil.toList(weCustomerSea.getAddUserId().split(","));
List<String> userNames = ListUtil.toList(weCustomerSea.getAddUserName().split(","));
IntStream.range(0,deduplicationSeasNoRepeat.size()).forEach(i->{
WeCustomerSeas k = deduplicationSeasNoRepeat.get(i);
if(Objects.nonNull(k)){
k.setTagIds(weCustomerSea.getTagIds());
k.setTagNames(weCustomerSea.getTagNames());
k.setAddUserId( userIds.get(Math.floorMod(i, userIds.size())));
k.setAddUserName(userNames.get(Math.floorMod(i, userNames.size())));
k.setTableExcelName(file.getOriginalFilename());
k.setTableExcelId(tabaleExcelId);
}
});
if(iWeCustomerSeasService.saveBatch(deduplicationSeasNoRepeat)){
//发送提醒
deduplicationSeasNoRepeat.stream().collect(Collectors.groupingBy(WeCustomerSeas::getAddUserId)).forEach((k,v)->{
iWeCustomerSeasService.remidUser(ListUtil.toList(k),v.size());
});
}
tip = MessageFormat.format(tip, new Object[]{new Integer(deduplicationSeasNoRepeat.size()).toString(),
new Integer( weCustomerSeas.size()-deduplicationSeasNoRepeat.size()).toString()});
}else{
tip = MessageFormat.format(tip, new Object[] { "0",weCustomerSeas.size() });
}
;
}else{
return AjaxResult.error("导入用户数据不能为空!");
}
return AjaxResult.success(tip);
}
/**
* 获取公海客户列表
* @param weCustomerSea
* @return
*/
@GetMapping("list")
public TableDataInfo list(WeCustomerSeas weCustomerSea){
startPage();
List<WeCustomerSeas> weCustomerSeas = iWeCustomerSeasService.list(
new LambdaQueryWrapper<WeCustomerSeas>()
.like(StringUtils.isNotEmpty(weCustomerSea.getPhone()),WeCustomerSeas::getPhone,weCustomerSea.getPhone())
.like(StringUtils.isNotEmpty(weCustomerSea.getCustomerName()),WeCustomerSeas::getCustomerName,weCustomerSea.getCustomerName())
.like(StringUtils.isNotEmpty(weCustomerSea.getAddUserName()),WeCustomerSeas::getAddUserName,weCustomerSea.getAddUserName())
.eq(weCustomerSea.getAddState() !=null ,WeCustomerSeas::getAddState,weCustomerSea.getAddState())
.orderByDesc(WeCustomerSeas::getCreateTime)
);
return getDataTable(weCustomerSeas);
}
/**
* 删除公海客户
* @param ids
* @return
*/
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable Long[] ids)
{
iWeCustomerSeasService.removeByIds(
ListUtil.toList(ids)
);
return AjaxResult.success();
}
/**
* 提醒员工
* @param ids
* @return
*/
@PostMapping("/remidUser/{ids}")
public AjaxResult remidUser(@PathVariable Long[] ids){
List<WeCustomerSeas> weCustomerSeas
= iWeCustomerSeasService.listByIds(ListUtil.toList(ids));
if(CollectionUtil.isNotEmpty(weCustomerSeas)){
weCustomerSeas.stream().collect(Collectors.groupingBy(WeCustomerSeas::getAddUserId)).forEach((k,v)->{
iWeCustomerSeasService.remidUser(ListUtil.toList(k),v.size());
});
}
return AjaxResult.success();
}
/**
* 获取指定员工所拥有的状态(移动端)
* @param userId
* @param addState
* @return
*/
@GetMapping("/findEmployeeCustomer")
public TableDataInfo findEmployeeCustomer(String userId,Integer addState){
startPage();
List<WeCustomerSeas> weCustomerSeas = iWeCustomerSeasService.list(new LambdaQueryWrapper<WeCustomerSeas>()
.eq(StringUtils.isNotEmpty(userId),WeCustomerSeas::getAddUserId, userId)
.eq(addState != null,WeCustomerSeas::getAddState, addState));
return getDataTable(weCustomerSeas);
}
/**
* 详情头部统计
* @return
*/
@GetMapping("/countCustomerSeas")
public AjaxResult countCustomerSeas(){
return AjaxResult.success(
iWeCustomerSeasService.countCustomerSeas()
);
}
/**
* 导入记录或员工添加统计
* @param groupByType 1:获取导入记录数据 2:员工添加统计
* @return
*/
@GetMapping("/findSeasRecord")
public TableDataInfo findSeasRecord(@RequestParam(defaultValue = "1") Integer groupByType){
startPage();
List<CustomerSeasRecordVo> seasRecord = iWeCustomerSeasService.findSeasRecord(groupByType);
return getDataTable(seasRecord);
}
/**
* 设置公海客户状态
* @return
*/
@PostMapping("/setState")
public AjaxResult setState(@RequestBody WeCustomerSeas weCustomerSea){
if(StringUtils.isEmpty(weCustomerSea.getPhone())){
return AjaxResult.error("手机号不可为空");
}
iWeCustomerSeasService.update(WeCustomerSeas.builder().addState(new Integer(1)).build()
,new LambdaQueryWrapper<WeCustomerSeas>().eq(WeCustomerSeas::getPhone,weCustomerSea.getPhone()));
return AjaxResult.success();
}
}