AbstractFollowStrategy.java
30.1 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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
package cn.fw.valhalla.service.bus.follow.strategy;
import cn.fw.common.web.auth.LoginAuthBean;
import cn.fw.valhalla.common.utils.DateUtil;
import cn.fw.valhalla.domain.db.OriginalData;
import cn.fw.valhalla.domain.db.customer.Customer;
import cn.fw.valhalla.domain.db.customer.CustomerBaseInfo;
import cn.fw.valhalla.domain.db.follow.FollowNoticeRecord;
import cn.fw.valhalla.domain.db.follow.FollowRecord;
import cn.fw.valhalla.domain.db.follow.FollowRecordAttachments;
import cn.fw.valhalla.domain.db.follow.FollowTask;
import cn.fw.valhalla.domain.dto.CustomerDetailDto;
import cn.fw.valhalla.domain.dto.FollowAttachmentDTO;
import cn.fw.valhalla.domain.enums.*;
import cn.fw.valhalla.domain.vo.follow.FollowDetailVO;
import cn.fw.valhalla.domain.vo.follow.FollowRecordVO;
import cn.fw.valhalla.domain.vo.setting.SettingVO;
import cn.fw.valhalla.rpc.angel.InsurerRpcService;
import cn.fw.valhalla.rpc.angel.dto.InsuranceDTO;
import cn.fw.valhalla.rpc.erp.TodoRpcService;
import cn.fw.valhalla.rpc.erp.UserService;
import cn.fw.valhalla.rpc.erp.dto.BackLogItemDTO;
import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO;
import cn.fw.valhalla.rpc.oop.OopService;
import cn.fw.valhalla.rpc.oop.dto.ShopDTO;
import cn.fw.valhalla.service.bus.setting.SettingBizService;
import cn.fw.valhalla.service.data.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.validation.constraints.NotNull;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import static cn.fw.common.businessvalidator.Validator.BV;
/**
* @author : kurisu
* @className : AbstractFollowStrategy
* @description : 策略抽象类 FIXME 跟进功能职责可以在拆分得更细
* @date: 2020-08-17 10:42
*/
@Slf4j
public abstract class AbstractFollowStrategy implements FollowStrategy {
@Autowired
protected CustomerService customerService;
@Autowired
protected CustomerBaseInfoService customerBaseInfoService;
@Autowired
protected InsurerRpcService insurerRpcService;
@Autowired
protected FollowTaskService followTaskService;
@Autowired
protected FollowRecordService followRecordService;
@Autowired
protected UserService userService;
@Autowired
protected FollowRecordAttachmentsService followRecordAttachmentsService;
@Autowired
protected FollowNoticeRecordService followNoticeRecordService;
@Autowired
protected SettingBizService settingBizService;
@Autowired
protected OopService oopService;
@Autowired
protected ApplicationEventPublisher eventPublisher;
@Autowired
protected TodoRpcService todoRpcService;
@Value("${follow.todo.FMCode}")
@Getter
private String FMCode;
@Value("${follow.todo.RMCode}")
@Getter
private String RMCode;
@Value("${follow.todo.IRCode}")
@Getter
private String IRCode;
@Value("${follow.todo.ACCode}")
@Getter
private String ACCode;
/**
* 单位转换
*
* @param unit
* @return
*/
public static int getCalendarType(SettingUnitEnum unit) {
switch (unit) {
case HOUR:
return Calendar.HOUR;
case MONTH:
return Calendar.MONTH;
case MINUTE:
return Calendar.MINUTE;
default:
return Calendar.DATE;
}
}
@Override
public List<FollowRecordVO> getRecordList(Long taskId) {
List<FollowRecord> followRecordList = followRecordService.getRecordList(taskId);
if (CollectionUtils.isEmpty(followRecordList)) {
return new ArrayList<>();
}
List<Long> recordIds = followRecordList.stream().map(FollowRecord::getId).collect(Collectors.toList());
List<FollowRecordAttachments> attachments = followRecordAttachmentsService.getListByRecordIds(recordIds);
if (CollectionUtils.isEmpty(attachments)) {
return new ArrayList<>();
}
List<FollowRecordVO> list = new ArrayList<>();
for (FollowRecordAttachments attachment : attachments) {
FollowRecordVO vo = new FollowRecordVO();
vo.setAttachments(attachment.getAttachments());
vo.setId(attachment.getId());
vo.setRecordId(attachment.getRecordId());
vo.setAttType(attachment.getAttType());
vo.setFollowType(followRecordList.get(0).getType());
vo.setUploadTime(attachment.getUploadTime());
int i = queryIndexFromRecords(followRecordList, attachment.getRecordId());
vo.setTimes(i + 1);
list.add(vo);
}
return list;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void completeRecord(Long id, LoginAuthBean currentUser) {
FollowRecord record = followRecordService.getById(id);
if (Objects.isNull(record)) {
return;
}
Long customerId = record.getCustomerId();
Customer customer = customerService.getById(customerId);
BV.notNull(customer, () -> "档案信息异常");
if (Boolean.FALSE.equals(customer.getYn())) {
customer = customerService.queryByFrameNo(customer.getFrameNo(), customer.getGroupId());
BV.notNull(customer, () -> "用户档案不存在");
FollowTask task = new FollowTask();
task.setId(record.getTaskId());
task.setCustomerId(customer.getId());
followTaskService.updateById(task);
}
BV.isTrue(record.getUserId().equals(currentUser.getUserId()), () -> "无法完成非本人待办任务");
BV.isTrue(Boolean.FALSE.equals(record.getOutTime()), () -> "无法完成已逾期的待办任务");
final Date followTime = DateUtil.localDateTime2Date(LocalDateTime.now());
record.setFollowTime(followTime);
int count = followRecordAttachmentsService.count(Wrappers.<FollowRecordAttachments>lambdaQuery()
.eq(FollowRecordAttachments::getRecordId, record.getId())
);
BV.isTrue(count > 0, () -> "请上传跟进记录");
followRecordService.updateById(record);
record.setCustomerId(customer.getId());
this.addTaskRecord(record, false);
BackLogItemDTO dto = new BackLogItemDTO(record.getUserId(), getItemCode(record.getType()), String.valueOf(record.getId()), followTime);
todoRpcService.complete(dto);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void completeRecord2(FollowRecord record) {
BV.isTrue(Boolean.FALSE.equals(record.getOutTime()), () -> "无法完成已逾期的待办任务");
final Date followTime = DateUtil.localDateTime2Date(LocalDateTime.now());
record.setFollowTime(followTime);
followRecordService.updateById(record);
this.addTaskRecord(record, false);
BackLogItemDTO dto = new BackLogItemDTO(record.getUserId(), getItemCode(record.getType()), String.valueOf(record.getId()), followTime);
todoRpcService.complete(dto);
}
/**
* 生成跟进记录
*
* @param task
*/
@Override
@Transactional(rollbackFor = Exception.class)
public Long createFirstRecord(FollowTask task) {
Customer customer = customerService.queryById(task.getCustomerId());
if (Objects.isNull(customer)) {
log.error("档案信息异常,生成跟进记录失败");
return null;
}
if (!task.getDeadline().after(task.getBeginTime()) || !task.getDeadline().after(DateUtil.localDateTime2Date(LocalDateTime.now()))) {
return task.getOriginUser();
}
final FollowRecord record = new FollowRecord();
record.setTaskId(task.getId());
record.setCustomerId(task.getCustomerId());
record.setType(task.getType());
record.setUserId(task.getFollowUser());
record.setPlanTime(task.getBeginTime());
settingBizService.querySettingByType(task.getType(), SettingTypeEnum.EFFECTIVE_TIME, task.getGroupId())
.ifPresent(setting -> {
Timestamp timestamp = getValidTime(setting, task.getBeginTime());
record.setDeadline(timestamp);
});
record.setOutTime(Boolean.FALSE);
record.setGroupId(task.getGroupId());
record.setShopId(task.getOriginShop());
record.setDealerId(task.getDealerId());
record.setCreateTime(DateUtil.localDateTime2Date(LocalDateTime.now()));
boolean saved = followRecordService.save(record);
return saved ? record.getUserId() : null;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void overdueProcessing(FollowRecord record) {
BackLogItemDTO dto = new BackLogItemDTO(record.getUserId(), getItemCode(record.getType()), record.getId().toString(), record.getPlanTime());
todoRpcService.cancel(dto);
boolean bool = this.addTaskRecord(record, true);
if (bool) {
followRecordService.overdue(record.getId());
}
}
@Override
public void stopFollowTask(FollowTask task) {
cancelFollowTodo(task.getId());
}
/**
* 查询跟进记录
*
* @param startIndex
* @param pageSize
* @param userId
* @return
*/
protected List<FollowRecord> list(Integer startIndex, Integer pageSize, Long userId, FollowTypeEnum followType) {
String sql = String.format("limit %s,%s", startIndex, pageSize);
return followRecordService.list(Wrappers.<FollowRecord>lambdaQuery()
.eq(FollowRecord::getUserId, userId)
.eq(FollowRecord::getType, followType)
.eq(FollowRecord::getOutTime, Boolean.FALSE)
.le(FollowRecord::getPlanTime, new Date())
.ge(FollowRecord::getDeadline, new Date())
.isNull(FollowRecord::getFollowTime)
.last(sql)
);
}
/**
* 查询档案信息
*
* @param customerId
* @return
*/
protected CustomerDetailDto queryCustomerInfo(@NotNull Long customerId) {
Customer customer = customerService.queryById(customerId);
CustomerDetailDto dto = new CustomerDetailDto();
if (Objects.isNull(customer)) {
log.error("档案不存在: custId[{}]", customerId);
return dto;
}
CustomerBaseInfo baseInfo = customerBaseInfoService.queryById(customer.getBaseId());
if (Objects.nonNull(baseInfo)) {
dto.setMemberId(baseInfo.getMemberId());
dto.setName(baseInfo.getName());
dto.setCompanyName(baseInfo.getCompanyName());
dto.setMobile(baseInfo.getMobile());
dto.setRegion(baseInfo.getCityName());
dto.setCusType(Objects.isNull(baseInfo.getCusType()) ? 0 : baseInfo.getCusType().getValue());
dto.setIdCode(baseInfo.getIdCode());
dto.setGender(baseInfo.getGender().getValue());
dto.setContact(baseInfo.getContact());
dto.setBirthday(baseInfo.getBirthday());
dto.setAddress(baseInfo.getAddress());
dto.setLat(baseInfo.getLat());
dto.setLng(baseInfo.getLng());
dto.setHobby(baseInfo.getHobby());
dto.setCityCode(baseInfo.getCityCode());
dto.setCityName(baseInfo.getCityName());
}
dto.setId(customer.getId());
dto.setPlateNo(customer.getPlateNo());
dto.setBrandId(customer.getBrandId());
dto.setBrandName(customer.getBrandName());
dto.setSeriesId(customer.getSeriesId());
dto.setSeriesName(customer.getSeriesName());
dto.setSpecId(customer.getSpecId());
dto.setFrameNo(customer.getFrameNo());
dto.setSpecName(customer.getSpecName());
dto.setCarImage(customer.getCarImage());
dto.setTags(customer.getTags());
dto.setCusLevel(customer.getCusLevel());
dto.setCurrentMileage(customer.getCurrentMileage());
dto.setAdviserId(customer.getAdviserId());
UserInfoDTO user = userService.user(customer.getAdviserId());
if (Objects.nonNull(user)) {
dto.setAdviserName(user.getUserName());
}
dto.setEngineNo(customer.getEngineNo());
dto.setRegDate(customer.getRegDate());
dto.setSpecCode(customer.getSpecCode());
dto.setBuyDate(customer.getBuyDate());
dto.setUseType(Objects.isNull(customer.getUseType()) ? null : customer.getUseType().getValue());
dto.setCreateTime(customer.getCreateTime());
dto.setGroupId(customer.getGroupId());
dto.setVehicleLicense(customer.getVehicleLicense());
dto.setShopId(customer.getShopId());
dto.setBuyPrice(customer.getBuyPrice());
dto.setSource(Objects.isNull(customer.getSource()) ? null : customer.getSource().getValue());
dto.setYn(customer.getYn());
return dto;
}
abstract public FollowDetailVO assemble(Long customerId);
/**
* 查询档案正在生效的保险信息
*
* @param customerId
* @return
*/
protected Optional<InsuranceDTO> queryInsuInfo(Long customerId) {
if (Objects.isNull(customerId)) {
return Optional.empty();
}
InsuranceDTO insuranceDTO = insurerRpcService.queryInsByCustId(customerId);
return Optional.ofNullable(insuranceDTO);
}
/**
* 查询下表
*
* @param list
* @param recordId
* @return
*/
private int queryIndexFromRecords(List<FollowRecord> list, Long recordId) {
for (int i = 0; i < list.size(); i++) {
Long id = list.get(i).getId();
if (Objects.equals(id, recordId)) {
return i;
}
}
return 0;
}
/**
* 取消生成的跟进待办
*
* @param taskId
*/
@Transactional(rollbackFor = Exception.class)
protected void cancelFollowTodo(Long taskId) {
List<FollowRecord> list = Optional.ofNullable(followRecordService.list(Wrappers.<FollowRecord>lambdaQuery()
.eq(FollowRecord::getTaskId, taskId)
.isNull(FollowRecord::getFollowTime)
.ge(FollowRecord::getDeadline, new Date())
)).orElse(new ArrayList<>());
if (CollectionUtils.isEmpty(list)) {
return;
}
List<Long> idList = new ArrayList<>();
for (FollowRecord record : list) {
if (Boolean.TRUE.equals(record.getAddTodo())) {
BackLogItemDTO dto = new BackLogItemDTO(record.getUserId(), getItemCode(record.getType()), String.valueOf(record.getId()), record.getPlanTime());
todoRpcService.cancel(dto);
}
idList.add(record.getId());
}
followRecordService.removeByIds(idList);
}
/**
* 完成生成的跟进待办
*
* @param taskId
*/
protected void completeTodo(Long taskId) {
List<FollowRecord> list = Optional.ofNullable(followRecordService.list(Wrappers.<FollowRecord>lambdaQuery()
.eq(FollowRecord::getTaskId, taskId)
.eq(FollowRecord::getOutTime, Boolean.FALSE)
.isNull(FollowRecord::getFollowTime)
.ge(FollowRecord::getDeadline, new Date())
)).orElse(new ArrayList<>());
if (CollectionUtils.isEmpty(list)) {
return;
}
for (FollowRecord record : list) {
if (Boolean.TRUE.equals(record.getAddTodo())) {
BackLogItemDTO dto = new BackLogItemDTO(record.getUserId(), getItemCode(record.getType()), String.valueOf(record.getId()), record.getPlanTime());
todoRpcService.complete(dto);
}
record.setFollowTime(new Date());
record.setOutTime(Boolean.FALSE);
}
followRecordService.updateBatchById(list);
}
/**
* 回滚完成的跟进
*
* @param task
*/
@Transactional(rollbackFor = Exception.class)
protected void rollback(FollowTask task) {
if (Boolean.FALSE.equals(task.getFinished())) {
return;
}
if (System.currentTimeMillis() >= task.getDeadline().getTime()) {
followTaskService.update(Wrappers.<FollowTask>lambdaUpdate()
.set(FollowTask::getState, TaskStateEnum.END)
.set(FollowTask::getFinished, Boolean.FALSE)
.set(FollowTask::getFinishTime, task.getDeadline())
.eq(FollowTask::getId, task.getId())
);
return;
}
if (System.currentTimeMillis() < task.getBeginTime().getTime()) {
followTaskService.update(Wrappers.<FollowTask>lambdaUpdate()
.set(FollowTask::getState, TaskStateEnum.WAITING)
.set(FollowTask::getFinished, Boolean.FALSE)
.set(FollowTask::getFinishTime, null)
.eq(FollowTask::getId, task.getId())
);
return;
}
if (System.currentTimeMillis() >= task.getBeginTime().getTime()) {
followTaskService.update(Wrappers.<FollowTask>lambdaUpdate()
.set(FollowTask::getState, TaskStateEnum.ONGOING)
.set(FollowTask::getFinished, Boolean.FALSE)
.set(FollowTask::getFinishTime, null)
.eq(FollowTask::getId, task.getId())
);
}
}
/**
* 更新任务
*
* @param list
* @param setting
*/
@Transactional(rollbackFor = Exception.class)
protected void updateTask(List<FollowTask> list, List<SettingVO> setting) {
for (SettingVO vo : setting) {
final Integer unit = vo.getUnit();
final int value = Optional.ofNullable(vo.getDetailValue()).orElse(0);
SettingUnitEnum unitEnum = SettingUnitEnum.ofValue(unit);
if (SettingTypeEnum.FIRST_TRIGGER_TIME.getValue().equals(vo.getType())) {
if (Objects.nonNull(unitEnum) && value > 0) {
final int calendarType = getCalendarType(unitEnum);
list.forEach(task -> {
Date originTime = task.getOriginTime();
Timestamp timestamp = DateUtil.getExpired(originTime, value, calendarType);
task.setBeginTime(timestamp);
});
}
}
if (SettingTypeEnum.CHANGE_TIME.getValue().equals(vo.getType())) {
if (Objects.nonNull(unitEnum) && value > 0) {
final int calendarType = getCalendarType(unitEnum);
list.forEach(task -> {
Date originTime = task.getOriginTime();
Timestamp timestamp = DateUtil.getExpired(originTime, value, calendarType);
task.setBeginTime(timestamp);
});
}
}
if (SettingTypeEnum.FAIL_TIME.getValue().equals(vo.getType())) {
if (Objects.nonNull(unitEnum) && value > 0) {
final int calendarType = getCalendarType(unitEnum);
list.forEach(task -> {
Date originTime = task.getOriginTime();
Timestamp timestamp = DateUtil.getExpired(originTime, value, calendarType);
task.setBeginTime(timestamp);
});
}
}
}
followTaskService.updateBatchById(list);
}
/**
* 更新服务号消息记录
*
* @param list
* @param vo
*/
@Transactional(rollbackFor = Exception.class)
protected void updateNoticeRecord(List<FollowTask> list, SettingVO vo) {
List<Long> idList = list.stream().map(FollowTask::getId).collect(Collectors.toList());
List<FollowNoticeRecord> noticeList = followNoticeRecordService.list(Wrappers.<FollowNoticeRecord>lambdaQuery()
.eq(FollowNoticeRecord::getStatus, SendStatusEnum.NOT_SENT)
.in(FollowNoticeRecord::getTaskId, idList)
);
Integer unit = vo.getUnit();
int value = Optional.ofNullable(vo.getDetailValue()).orElse(0);
SettingUnitEnum unitEnum = SettingUnitEnum.ofValue(unit);
if (Objects.isNull(unitEnum) || value <= 0) {
return;
}
final int calendarType = getCalendarType(unitEnum);
for (FollowTask task : list) {
for (FollowNoticeRecord noticeRecord : noticeList) {
if (task.getId().equals(noticeRecord.getTaskId())) {
Date originTime = task.getOriginTime();
Timestamp timestamp = DateUtil.getExpired(originTime, value, calendarType);
noticeRecord.setSendTime(timestamp);
}
}
}
followNoticeRecordService.updateBatchById(noticeList);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void uploadAtt(FollowAttachmentDTO dto, Long userId) {
FollowRecord record = followRecordService.getById(dto.getRecordId());
if (Objects.isNull(record)) {
return;
}
BV.isTrue(record.getUserId().equals(userId), () -> "无法跟进非本人待办任务");
BV.isTrue(Boolean.FALSE.equals(record.getOutTime()), () -> "无法跟进已逾期的待办任务");
FollowRecordAttachments attachments = new FollowRecordAttachments();
attachments.setAttachments(dto.getAttachments());
attachments.setAttType(dto.getAttType());
attachments.setDescribes(dto.getDescribes());
attachments.setRecordId(dto.getRecordId());
attachments.setUploadTime(new Date());
followRecordAttachmentsService.save(attachments);
}
/**
* 生成任务实体
*
* @param originalData
* @return
*/
protected FollowTask createTask(final OriginalData originalData, FollowTypeEnum followType) {
final FollowTask followTask = new FollowTask();
followTask.setCustomerId(originalData.getCustomerId());
followTask.setType(followType);
followTask.setOriginTime(originalData.getGenerateTime());
followTask.setState(TaskStateEnum.WAITING);
followTask.setFinished(Boolean.FALSE);
followTask.setGroupId(originalData.getGroupId());
followTask.setCreateTime(originalData.getGenerateTime());
settingBizService.querySettingByType(followType, SettingTypeEnum.FIRST_TRIGGER_TIME, originalData.getGroupId())
.ifPresent(r -> followTask.setBeginTime(calDate(r, originalData.getGenerateTime(), false)));
settingBizService.querySettingByType(followType, SettingTypeEnum.CHANGE_TIME, originalData.getGroupId())
.ifPresent(r -> followTask.setChangeUserTime(calDate(r, originalData.getGenerateTime(), false)));
settingBizService.querySettingByType(followType, SettingTypeEnum.FAIL_TIME, originalData.getGroupId())
.ifPresent(r -> followTask.setDeadline(calDate(r, originalData.getGenerateTime(), false)));
return followTask;
}
/**
* 生成消息推送
*
* @param task
*/
protected boolean createFirstNotice(final FollowTask task) {
if (!task.getDeadline().after(task.getBeginTime()) || !task.getDeadline().after(DateUtil.localDateTime2Date(LocalDateTime.now()))) {
return true;
}
final FollowNoticeRecord record = new FollowNoticeRecord();
record.setTaskId(task.getId());
record.setFollowType(task.getType());
record.setCustomerId(task.getCustomerId());
record.setStatus(SendStatusEnum.NOT_SENT);
settingBizService.querySettingByType(task.getType(), SettingTypeEnum.FIRST_NOTICE_TIME, task.getGroupId())
.ifPresent(r -> record.setSendTime(calDate(r, task.getOriginTime(), false)));
record.setCreateTime(new Date());
return followNoticeRecordService.save(record);
}
/**
* 计算时间
*
* @param vo
* @param origin
* @return
*/
protected Timestamp calDate(SettingVO vo, Date origin, boolean reverse) {
if (vo.getDetailValue() == null || vo.getDetailValue() <= 0) {
log.info("关键设置数据异常,请检查数据 : settingType[{}] settingId[{}]", vo.getType(), vo.getId());
return null;
}
Integer value = vo.getDetailValue();
SettingUnitEnum unit = Objects.requireNonNull(SettingUnitEnum.ofValue(vo.getUnit()));
if (value == null || value <= 0) {
log.info("关键设置数据异常,请检查数据 : settingType[{}] settingId[{}]", vo.getType(), vo.getId());
return null;
}
if (reverse) {
value = -1 * value;
}
Timestamp expired = DateUtil.getExpired(origin, value, getCalendarType(unit));
DateUtil.date2LocalDateTime(expired);
if (SettingUnitEnum.DAY.equals(unit) && !DateUtil.isBeforeDawn(expired)) {
expired = DateUtil.getExpired(DateUtil.startDate(expired), 1, Calendar.DATE);
}
return expired;
}
protected String getItemCode(FollowTypeEnum type) {
switch (type) {
case AC:
return getACCode();
case FM:
return getFMCode();
case IR:
return getIRCode();
case RM:
return getRMCode();
default:
return getFMCode();
}
}
/**
* 生成新的跟进
*
* @param record
* @return
*/
private boolean addTaskRecord(FollowRecord record, boolean overdue) {
final FollowTask task = followTaskService.getById(record.getTaskId());
if (Objects.isNull(task)) {
return true;
}
if (!TaskStateEnum.ONGOING.equals(task.getState())) {
return true;
}
//任务截止日期
final Date deadline = task.getDeadline();
//更换用户跟进日期
final Date changeTime = task.getChangeUserTime();
Optional<SettingVO> fcsetting = settingBizService.querySettingByType(record.getType(), SettingTypeEnum.FOLLOW_CYCLE, record.getGroupId());
if (!fcsetting.isPresent()) {
log.info("关键设置数据异常,请检查数据 : settingType[{}] groupId[{}]", SettingTypeEnum.FOLLOW_CYCLE.getName(), record.getGroupId());
return false;
}
Optional<SettingVO> efsetting = settingBizService.querySettingByType(record.getType(), SettingTypeEnum.EFFECTIVE_TIME, record.getGroupId());
if (!efsetting.isPresent()) {
log.info("关键设置数据异常,请检查数据 : settingType[{}] groupId[{}]", SettingTypeEnum.EFFECTIVE_TIME.getName(), record.getGroupId());
return false;
}
Date originTime = record.getDeadline();
if (!overdue) {
originTime = record.getFollowTime();
}
final Timestamp newTime = calDate(fcsetting.get(), originTime, false);
if (Objects.isNull(newTime)) {
return false;
}
//时间允许的情况下切换顾问跟进前会再次给当前顾问一次跟进机会
if (Boolean.FALSE.equals(task.getChanged()) && Objects.nonNull(changeTime) && changeTime.before(newTime)) {
Timestamp changeUserTime = DateUtil.getExpired(changeTime, -24, Calendar.HOUR);
LocalDateTime dateTime = DateUtil.date2LocalDateTime(changeUserTime);
if (dateTime.isAfter(LocalDateTime.now().plus(72, ChronoUnit.HOURS))) {
FollowRecord entity = this.getEntity(record, task, changeUserTime, changeTime);
return followRecordService.save(entity);
}
}
//时间允许的情况下任务逾期前会再给顾问一次跟进机会
if (!deadline.after(newTime)) {
Timestamp deadNextTime = DateUtil.getExpired(deadline, -24, Calendar.HOUR);
LocalDateTime dateTime = DateUtil.date2LocalDateTime(deadNextTime);
if (dateTime.isAfter(LocalDateTime.now().plus(3, ChronoUnit.DAYS))) {
FollowRecord entity = this.getEntity(record, task, deadNextTime, deadline);
return followRecordService.save(entity);
}
return true;
}
Date deadTime = calDate(efsetting.get(), newTime, false);
if (Objects.isNull(deadTime)) {
return false;
}
if (deadTime.after(deadline)) {
deadTime = deadline;
}
final FollowRecord newRecord = this.getEntity(record, task, newTime, deadTime);
return followRecordService.save(newRecord);
}
private FollowRecord getEntity(FollowRecord preRecord, FollowTask task, Date newTime, Date deadline) {
FollowRecord record = new FollowRecord();
if (Objects.isNull(task)) {
record.setUserId(preRecord.getUserId());
record.setCustomerId(preRecord.getCustomerId());
record.setShopId(preRecord.getShopId());
record.setDealerId(preRecord.getDealerId());
} else {
record.setUserId(task.getFollowUser());
record.setCustomerId(task.getCustomerId());
record.setShopId(task.getFollowShop());
ShopDTO shop = oopService.shop(task.getFollowShop());
record.setDealerId(Objects.isNull(shop) ? preRecord.getDealerId() : shop.getDealerId());
}
record.setTaskId(preRecord.getTaskId());
record.setType(preRecord.getType());
record.setPlanTime(newTime);
record.setDeadline(deadline);
record.setOutTime(Boolean.FALSE);
record.setGroupId(preRecord.getGroupId());
record.setCreateTime(DateUtil.localDateTime2Date(LocalDateTime.now()));
return record;
}
private Timestamp getValidTime(SettingVO setting, Date origin) {
Timestamp timestamp = calDate(setting, origin, false);
while (!timestamp.after(DateUtil.localDateTime2Date(LocalDateTime.now()))) {
timestamp = calDate(setting, timestamp, false);
}
return timestamp;
}
}