LeaveNeedDoBizService.java
25.3 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package cn.fw.valhalla.service.bus;
import cn.fw.common.data.mybatis.pagination.PageData;
import cn.fw.common.page.AppPage;
import cn.fw.common.web.annotation.DisLock;
import cn.fw.common.web.auth.LoginAuthBean;
import cn.fw.data.base.domain.common.Message;
import cn.fw.hermes.sdk.api.ImSendMessage;
import cn.fw.hermes.sdk.api.para.MsgParamCondition;
import cn.fw.valhalla.common.constant.RoleCode;
import cn.fw.valhalla.common.utils.DateUtil;
import cn.fw.valhalla.domain.db.LeaveNeedDo;
import cn.fw.valhalla.domain.db.customer.Customer;
import cn.fw.valhalla.domain.db.follow.ClueTask;
import cn.fw.valhalla.domain.db.follow.FollowClue;
import cn.fw.valhalla.domain.db.pool.StammkundePool;
import cn.fw.valhalla.domain.dto.DistributableDTO;
import cn.fw.valhalla.domain.dto.LeaveAllocationDTO;
import cn.fw.valhalla.domain.dto.RoleChangeDTO;
import cn.fw.valhalla.domain.dto.StammkundeDto;
import cn.fw.valhalla.domain.enums.*;
import cn.fw.valhalla.domain.query.LeaveQueryVO;
import cn.fw.valhalla.domain.vo.DistributableVO;
import cn.fw.valhalla.domain.vo.LeaveNeedDoVO;
import cn.fw.valhalla.rpc.backlog.TodoRpcService;
import cn.fw.valhalla.rpc.backlog.dto.BackLogItemDTO;
import cn.fw.valhalla.rpc.ehr.EhrRpcService;
import cn.fw.valhalla.rpc.ehr.dto.StaffInfoDTO;
import cn.fw.valhalla.rpc.erp.UserService;
import cn.fw.valhalla.rpc.erp.dto.PostUserDTO;
import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO;
import cn.fw.valhalla.rpc.erp.dto.UserRoleDataRangeDTO;
import cn.fw.valhalla.rpc.oop.OopService;
import cn.fw.valhalla.rpc.oop.dto.ShopDTO;
import cn.fw.valhalla.service.bus.follow.FollowBizService;
import cn.fw.valhalla.service.data.*;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;
import static cn.fw.common.businessvalidator.Validator.BV;
/**
* @author : kurisu
* @className : LeaveNeedDoBizService
* @description : 人员变动处理
* @date: 2020-10-16 17:04
*/
@Service
@Slf4j
@RequiredArgsConstructor
public class LeaveNeedDoBizService {
private final LeaveNeedDoService leaveNeedDoService;
private final CustomerService customerService;
private final UserService userService;
private final EhrRpcService ehrRpcService;
private final OopService oopService;
private final FollowBizService followBizService;
private final ImSendMessage imSendMessage;
private final TodoRpcService todoRpcService;
private final StammkundePoolService stammkundePoolService;
private final FollowClueService followClueService;
private final ClueTaskService clueTaskService;
/**
* Redis工具
*/
private final StringRedisTemplate redisTemplate;
@Value("${spring.cache.custom.global-prefix}:LeaveNeedDo")
@Getter
private String keyPrefix;
@Value("${follow.todo.leave2do}")
@Getter
private String leave2doCode;
@Value("${spring.cache.custom.global-prefix}:role-change:need-end-task")
@Getter
private String needDealTaskKey;
@Transactional(rollbackFor = Exception.class)
@DisLock(prefix = "#this.getKeyPrefix()", key = "#shopId + ':add:' + #userId", message = "请勿重复提交")
public void add(final Long userId, final Long shopId) {
List<PostUserDTO> postUserDTOS = userService.getUserByRole(shopId, RoleCode.BYKFP);
BV.isFalse(CollectionUtils.isEmpty(postUserDTOS), () -> "该门店没有配置保有客分配人员");
LeaveNeedDo db = addable(userId, shopId);
leaveNeedDoService.save(db);
push2Todo(db.getId(), postUserDTOS.get(0).getUserId(), shopId);
}
@Transactional(rollbackFor = Exception.class)
public void addLeaveTodo(LeaveNeedDo leaveNeedDo) {
Long userId = leaveNeedDo.getUserId();
Long shopId = leaveNeedDo.getShopId();
List<PostUserDTO> postUserDTOS = userService.getUserByRole(shopId, RoleCode.BYKFP);
BV.isFalse(CollectionUtils.isEmpty(postUserDTOS), () -> "该门店没有配置保有客分配人员");
boolean bool = leaveNeedDoService.count(Wrappers.<LeaveNeedDo>lambdaQuery()
.eq(LeaveNeedDo::getUserId, userId)
.eq(LeaveNeedDo::getShopId, shopId)
.eq(LeaveNeedDo::getDone, Boolean.FALSE)
) > 0;
if (bool) {
return;
}
int count = customerService.count(Wrappers.<Customer>lambdaQuery()
.eq(Customer::getAdviserId, userId)
.eq(Customer::getShopId, shopId)
.eq(Customer::getYn, Boolean.TRUE)
);
if (count <= 0) {
return;
}
Long todoUser = postUserDTOS.get(0).getUserId();
leaveNeedDo.setTodoUser(todoUser);
leaveNeedDoService.save(leaveNeedDo);
Map<String, String> map = new HashMap<>();
map.put("id", String.valueOf(leaveNeedDo.getId()));
map.put("shopId", String.valueOf(shopId));
map.put("userName", leaveNeedDo.getUserName());
map.put("customerNum", String.valueOf(count));
map.put("leaveTime", DateUtil.getFullDateString(leaveNeedDo.getEffectiveTime(), "-"));
push2Todo(leaveNeedDo.getId(), todoUser, shopId, map);
}
public AppPage<LeaveNeedDoVO> getList(LoginAuthBean currentUser, LeaveQueryVO queryVO) {
List<UserRoleDataRangeDTO> dataRange = userService.getUserRoleDataRange(currentUser.getUserId(), RoleCode.BYKFP);
List<Long> shopIds = dataRange.stream().map(UserRoleDataRangeDTO::getRangeValue).collect(Collectors.toList());
BV.isFalse(CollectionUtils.isEmpty(shopIds), () -> "无权限操作,请检查角色权限是否正确");
PageData<LeaveNeedDo> pageData = leaveNeedDoService.page(new PageData<>(queryVO), Wrappers.<LeaveNeedDo>lambdaQuery()
.in(LeaveNeedDo::getShopId, shopIds)
.eq(LeaveNeedDo::getDone, Boolean.FALSE)
.eq(Objects.nonNull(queryVO.getType()), LeaveNeedDo::getType, queryVO.getType())
);
AppPage<LeaveNeedDoVO> page = AppPage.empty(queryVO);
List<LeaveNeedDoVO> list = new ArrayList<>();
List<LeaveNeedDo> records = Optional.ofNullable(pageData.getRecords()).orElse(new ArrayList<>());
for (LeaveNeedDo needDo : records) {
int i = customerService.count(Wrappers.<Customer>lambdaQuery()
.eq(Customer::getAdviserId, needDo.getUserId())
.eq(Customer::getYn, Boolean.TRUE)
);
list.add(LeaveNeedDoVO.with(needDo, i));
}
page.setData(list);
return page;
}
@Transactional(rollbackFor = Exception.class)
@DisLock(prefix = "#this.getKeyPrefix()", key = "#dto.id", message = "请勿重复提交")
public void allocation(LoginAuthBean user, LeaveAllocationDTO dto) {
final String key = generateKey(dto.getId());
try {
//准备分配 常规校验
prepareAllocation(dto);
//变更档案
doAllocation(dto, key);
//完成分配发送消息提醒等
finish(user, dto.getId(), key);
} finally {
clearKey(key);
}
}
@Transactional(rollbackFor = Exception.class)
public void checkAllcotaion(Long leaveId) {
LeaveNeedDo needDo = leaveNeedDoService.getById(leaveId);
int i = customerService.count(Wrappers.<Customer>lambdaQuery()
.eq(Customer::getAdviserId, needDo.getUserId())
.eq(Customer::getYn, Boolean.TRUE)
);
if (i > 0) {
return;
}
needDo.setDone(Boolean.TRUE);
leaveNeedDoService.updateById(needDo);
Long todoUser = needDo.getTodoUser();
if (Objects.nonNull(todoUser)) {
BackLogItemDTO backLogItemDTO = new BackLogItemDTO(todoUser, getLeave2doCode(), String.valueOf(leaveId), new Date(), null);
todoRpcService.complete(backLogItemDTO);
}
}
/**
* 服务顾问角色变动跟进处理
*
* @param leaveId
*/
@Transactional(rollbackFor = Exception.class)
public void fwgwChanged(Long leaveId) {
LeaveNeedDo leaveNeedDo = leaveNeedDoService.getById(leaveId);
if (Objects.isNull(leaveNeedDo)) {
return;
}
List<ClueTask> list = clueTaskService.list(Wrappers.<ClueTask>lambdaQuery()
.in(ClueTask::getType, FollowTypeEnum.FM, FollowTypeEnum.RM)
.eq(ClueTask::getState, TaskStateEnum.ONGOING)
.eq(ClueTask::getFollowUser, leaveNeedDo.getUserId())
);
if (CollectionUtils.isEmpty(list)) {
return;
}
for (ClueTask task : list) {
followBizService.roleChangeEndTask(task);
}
}
/**
* 续保角色变动
*
* @param dto
*/
@Transactional(rollbackFor = Exception.class)
public void xbgjChanged(RoleChangeDTO dto) {
final Long userId = dto.getUserId();
List<ClueTask> list = clueTaskService.list(Wrappers.<ClueTask>lambdaQuery()
.eq(ClueTask::getType, FollowTypeEnum.IR)
.eq(ClueTask::getState, TaskStateEnum.ONGOING)
.eq(ClueTask::getFollowUser, userId)
);
if (CollectionUtils.isEmpty(list)) {
return;
}
for (ClueTask task : list) {
followBizService.roleChangeEndTask(task);
}
}
public List<DistributableVO> distributableList(Long shopId) {
List<DistributableVO> list = new ArrayList<>();
List<Customer> customerList = distributableCustomer(shopId);
if (CollectionUtils.isEmpty(customerList)) {
return list;
}
Map<Long, List<Customer>> userCustomer = customerList.stream().collect(Collectors.groupingBy(Customer::getAdviserId));
for (Map.Entry<Long, List<Customer>> entry : userCustomer.entrySet()) {
DistributableVO vo = new DistributableVO();
StaffInfoDTO staffInfoDTO = ehrRpcService.queryStaffInfo(entry.getKey());
vo.setUserId(entry.getKey());
vo.setUserName(Optional.ofNullable(staffInfoDTO).map(StaffInfoDTO::getName).orElse(""));
vo.setDistributableNum(entry.getValue().size());
vo.setShopId(shopId);
list.add(vo);
}
return list;
}
@Transactional(rollbackFor = Exception.class)
@DisLock(prefix = "#this.getKeyPrefix()", key = "'shop:' + #dto.shopId", message = "请勿重复提交")
public void activeAllocation(LoginAuthBean user, DistributableDTO dto) {
//准备分配 常规校验
Map<Long, List<Customer>> map = verifyAndPrepare(dto);
//变更档案
List<Customer> list = activeDoAllocation(dto, map);
int size = list.size();
if (size == 0) {
return;
}
try {
String text = String.format("%s通过资源分配给你%s台保有客", user.getUserName(), size);
final MsgParamCondition msgPara = MsgParamCondition.getOfflineTxetPara(text, null, "保有客分配",
"保有客分配", null, dto.getAdviserId()).build();
final Message<Boolean> msg = imSendMessage.synSendMsg(msgPara);
log.info("给[{}]推送im消息结果:[{}]", dto.getAdviserName(), msg.getResult());
} catch (Exception e) {
log.error("给[{}]推送im消息失败:]", dto.getAdviserName(), e);
}
}
private void prepareAllocation(LeaveAllocationDTO dto) {
LeaveNeedDo needDo = leaveNeedDoService.queryProcessableById(dto.getId());
BV.notNull(needDo, () -> "该条记录已处理或不存在,请刷新后重试");
dto.setAdviserId(needDo.getUserId());
List<LeaveAllocationDTO.Allocation> ruleList = dto.getRuleList();
BV.notNull(ruleList, () -> "分配规则不能为空");
for (LeaveAllocationDTO.Allocation rule : ruleList) {
List<UserRoleDataRangeDTO> dataRange = userService.getUserRoleDataRange(rule.getUserId(), RoleCode.FWGW);
List<Long> shopIdList = dataRange.stream().map(UserRoleDataRangeDTO::getRangeValue).collect(Collectors.toList());
BV.isNotEmpty(shopIdList, () -> String.format("[%s]无服务接待角色,请核对", rule.getUserName()));
Long shopId = shopIdList.get(0);
BV.isTrue(needDo.getShopId().equals(shopId), () -> String.format("[%s]所属门店与离职人员门店不符", rule.getUserName()));
rule.setShopId(shopId);
}
}
/**
* 处理数据
*
* @param dto
* @return 处理完的档案
*/
private void doAllocation(LeaveAllocationDTO dto, String key) {
List<Customer> customerList = customerService.queryByAdviserId(dto.getAdviserId());
if (CollectionUtils.isEmpty(customerList)) {
return;
}
List<LeaveAllocationDTO.Allocation> ruleList = dto.getRuleList();
final List<Customer> list = new ArrayList<>();
final List<StammkundePool> spl = new ArrayList<>();
final LinkedList<UserInfo> queue = new LinkedList<>();
for (LeaveAllocationDTO.Allocation rule : ruleList) {
int size = customerList.size();
List<Customer> subList = customerList.subList(0, rule.getNum() > size ? size : rule.getNum());
if (CollectionUtils.isEmpty(subList)) {
continue;
}
for (Customer customer : subList) {
customer.setShopId(rule.getShopId());
customer.setAdviserId(rule.getUserId());
rejectPool(customer.getId(), rule.getShopId(), rule.getUserName(), rule.getUserId(), customer.getGroupId());
spl.add(createPool(customer, rule.getUserName()));
list.add(customer);
}
queue.add(new UserInfo(rule.getUserId(), rule.getUserName(), subList.size()));
subList.clear();
}
customerService.updateBatchById(list);
stammkundePoolService.saveBatch(spl);
setToCache(key, queue);
}
private void finish(LoginAuthBean user, Long leaveId, String key) {
List<UserInfo> list = getAllFromCache(key);
for (UserInfo info : list) {
try {
String text = String.format("%s通过资源分配给你%s台保有客", user.getUserName(), info.getCount());
final MsgParamCondition msgPara = MsgParamCondition.getOfflineTxetPara(text, null, "保有客分配",
"保有客分配", null, info.getUserId()).build();
final Message<Boolean> msg = imSendMessage.synSendMsg(msgPara);
log.info("给[{}]推送im消息结果:[{}]", info.getUserName(), msg.getResult());
} catch (Exception e) {
log.error("给[{}]推送im消息失败:", info, e);
}
}
clearKey(key);
BoundListOperations<String, String> listOps = redisTemplate.boundListOps(getNeedDealTaskKey());
listOps.rightPush(String.valueOf(leaveId));
}
private void rejectPool(Long customerId, Long shopId, String userName, Long userId, Long groupId) {
StammkundeDto stammkundeDto = new StammkundeDto();
stammkundeDto.setCustomerId(customerId);
stammkundeDto.setReason(DefeatReasonEnum.LC);
stammkundeDto.setShopId(shopId);
stammkundeDto.setNewUserId(userId);
stammkundeDto.setNewUserName(userName);
ShopDTO shop = oopService.shop(shopId);
stammkundeDto.setNewShopId(shopId);
if (Objects.nonNull(shop)) {
stammkundeDto.setNewShopName(shop.getShortName());
}
stammkundeDto.setGroupId(groupId);
stammkundePoolService.reject(stammkundeDto);
}
private StammkundePool createPool(Customer customer, String userName) {
StammkundePool sp = new StammkundePool();
sp.setCustomerId(customer.getId());
sp.setAdviserId(customer.getAdviserId());
sp.setAdviserName(userName);
sp.setSources(StammkundeSourcesEnum.SYSTEMATIC);
sp.setPoolStatus(StammkundeStatusEnum.KUNDE);
sp.setAktiv(Boolean.TRUE);
sp.setActivationTime(new Date());
sp.setShopId(customer.getShopId());
sp.setGroupId(customer.getGroupId());
return sp;
}
private String generateKey(final Long leaveId) {
Assert.notNull(leaveId, "leaveId cannot be null");
return String.format("%s:allocation:%s", getKeyPrefix(), leaveId);
}
public List<UserInfo> getAllFromCache(final String key) {
try {
Boolean hasKey = redisTemplate.hasKey(key);
if (!Boolean.TRUE.equals(hasKey)) {
return new ArrayList<>();
}
BoundListOperations<String, String> ops = redisTemplate.boundListOps(key);
List<String> stringList = ops.range(0, -1);
if (CollectionUtils.isEmpty(stringList)) {
return new ArrayList<>();
}
List<UserInfo> dtos = stringList.stream().map(str -> JSONObject.parseObject(str, UserInfo.class)).collect(Collectors.toList());
redisTemplate.delete(key);
return dtos;
} catch (Exception e) {
log.error("缓存设置信息失败 key[{}]", key, e);
return new ArrayList<>();
}
}
private void setToCache(final String key, final LinkedList<UserInfo> userInfos) {
try {
if (CollectionUtils.isEmpty(userInfos)) {
return;
}
BoundListOperations<String, String> ops = redisTemplate.boundListOps(key);
String[] users = userInfos.stream().map(JSONObject::toJSONString).toArray(String[]::new);
ops.rightPushAll(users);
} catch (Exception e) {
log.error("缓存设置信息失败 key[{}]", key, e);
}
}
private void clearKey(final String key) {
try {
redisTemplate.delete(key);
} catch (Exception e) {
log.error("清空缓存信息失败 key[{}]", key, e);
}
}
private LeaveNeedDo addable(final Long userId, final Long shopId) {
List<UserRoleDataRangeDTO> dataRange = userService.getUserRoleDataRange(userId, RoleCode.FWGW);
boolean ok = CollectionUtils.isEmpty(dataRange) || !Objects.equals(dataRange.get(0).getRangeValue(), shopId);
BV.isTrue(ok, () -> "请先移除对对应服务站服务顾问角色后操作");
int count = leaveNeedDoService.count(Wrappers.<LeaveNeedDo>lambdaQuery()
.eq(LeaveNeedDo::getUserId, userId)
.eq(LeaveNeedDo::getShopId, shopId)
.eq(LeaveNeedDo::getDone, Boolean.FALSE)
);
BV.isTrue(count == 0, () -> "已存在待分配记录,请勿重复添加");
UserInfoDTO user = ehrRpcService.user(userId);
BV.notNull(user, () -> "用户不存在");
boolean bool = customerService.count(Wrappers.<Customer>lambdaQuery()
.eq(Customer::getAdviserId, userId)
.eq(Customer::getShopId, shopId)
.eq(Customer::getYn, Boolean.TRUE)
) > 0;
BV.isTrue(bool, () -> "该顾问没有可用档案无需分配保有客");
LeaveNeedDo db = new LeaveNeedDo();
db.setDone(Boolean.FALSE);
db.setEffectiveTime(DateUtil.getMonthEndDay(new Date()));
db.setReason(LeaveReasonEnum.OTHER);
db.setType(LeaveTodoTypeEnum.CUSTOMER);
db.setShopId(shopId);
db.setUserId(userId);
db.setUserName(user.getUserName());
db.setCreateTime(new Date());
db.setUpdateTime(new Date());
return db;
}
private void push2Todo(Long id, Long userId, Long shopId) {
Map<String, String> map = new HashMap<>();
map.put("shopId", String.valueOf(shopId));
map.put("id", String.valueOf(id));
push2Todo(id, userId, shopId, map);
}
private void push2Todo(Long id, Long userId, Long shopId, Map<String, String> map) {
BackLogItemDTO dto = new BackLogItemDTO(userId, getLeave2doCode(), String.valueOf(id), new Date(), shopId);
dto.setDynamicMap(map);
todoRpcService.push(dto);
}
private List<Customer> distributableCustomer(Long shopId) {
Set<String> vinSet = new HashSet<>();
List<FollowClue> clueList = followClueService.list(Wrappers.<FollowClue>lambdaQuery()
.eq(FollowClue::getClueState, ClueStatusEnum.ONGOING)
.ne(FollowClue::getClueType, FollowTypeEnum.AC)
);
if (!CollectionUtils.isEmpty(clueList)) {
vinSet.addAll(clueList.stream().map(FollowClue::getVin).collect(Collectors.toSet()));
}
return customerService.list(Wrappers.<Customer>lambdaQuery()
.eq(Customer::getYn, Boolean.TRUE)
.eq(Customer::getShopId, shopId)
.notIn(!CollectionUtils.isEmpty(vinSet), Customer::getFrameNo, vinSet)
.isNull(Customer::getTags)
.isNull(Customer::getCusLevel)
);
}
private Map<Long, List<Customer>> verifyAndPrepare(DistributableDTO dto) {
List<UserRoleDataRangeDTO> dataRange = userService.getUserRoleDataRange(dto.getAdviserId(), RoleCode.FWGW);
List<Long> shopIdList = dataRange.stream().map(UserRoleDataRangeDTO::getRangeValue).collect(Collectors.toList());
BV.isNotEmpty(shopIdList, () -> String.format("[%s]无服务接待角色,请核对", dto.getAdviserName()));
List<DistributableDTO.BeAssignedDTO> beAssignedList = dto.getBeAssignedList();
BV.isNotEmpty(beAssignedList, () -> "分配规则不能为空");
List<Customer> customerList = distributableCustomer(dto.getShopId());
BV.isNotEmpty(customerList, () -> "无可分配的档案");
final Map<Long, List<Customer>> map = customerList.stream().collect(Collectors.groupingBy(Customer::getAdviserId));
beAssignedList.forEach(c -> {
int size = Optional.ofNullable(map.get(c.getUserId())).orElse(new ArrayList<>()).size();
Integer distributableNum = c.getDistributableNum();
Integer num = c.getNum();
if (size < distributableNum) {
c.setDistributableNum(size);
}
if (size < num) {
c.setDistributableNum(size);
c.setNum(size);
}
});
List<DistributableDTO.BeAssignedDTO> dtoList = beAssignedList.stream().filter(r -> r.getDistributableNum() <= 0 || r.getDistributableNum() < r.getNum()).collect(Collectors.toList());
BV.isEmpty(dtoList, () -> "分配规则有误,请刷新页面重试");
return map;
}
/**
* 处理数据
*
* @param dto
* @param map
* @return 处理完的档案
*/
private List<Customer> activeDoAllocation(DistributableDTO dto, Map<Long, List<Customer>> map) {
List<Customer> customerList = new ArrayList<>();
List<DistributableDTO.BeAssignedDTO> assignedList = dto.getBeAssignedList();
for (DistributableDTO.BeAssignedDTO assignedDTO : assignedList) {
customerList.addAll(map.get(assignedDTO.getUserId()).subList(0, assignedDTO.getNum()));
}
if (CollectionUtils.isEmpty(customerList)) {
return new ArrayList<>();
}
Long shopId = dto.getShopId();
Long adviserId = dto.getAdviserId();
String adviserName = dto.getAdviserName();
final List<Customer> list = new ArrayList<>();
final List<StammkundePool> spl = new ArrayList<>();
for (Customer customer : customerList) {
customer.setShopId(shopId);
customer.setAdviserId(adviserId);
rejectPool(customer.getId(), shopId, adviserName, adviserId, customer.getGroupId());
spl.add(createPool(customer, adviserName));
list.add(customer);
}
customerService.updateBatchById(list);
stammkundePoolService.saveBatch(spl);
return list;
}
static class UserInfo {
private Long userId;
private String userName;
private int count;
public UserInfo() {
}
public UserInfo(Long userId, String userName) {
this.userId = userId;
this.userName = userName;
}
public UserInfo(Long userId, String userName, int count) {
this.userId = userId;
this.userName = userName;
this.count = count;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public String toString() {
return "UserInfo{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", count=" + count +
'}';
}
}
}