CallReportDealTask.java
5.42 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
package cn.fw.valhalla.controller.task;
import cn.fw.valhalla.common.utils.StringUtils;
import cn.fw.valhalla.domain.db.customer.AccidentPool;
import cn.fw.valhalla.domain.db.customer.Customer;
import cn.fw.valhalla.domain.db.customer.CustomerBaseInfo;
import cn.fw.valhalla.domain.dto.CallReportDTO;
import cn.fw.valhalla.rpc.ehr.EhrRpcService;
import cn.fw.valhalla.rpc.ehr.dto.StaffInfoDTO;
import cn.fw.valhalla.service.bus.follow.FollowBizService;
import cn.fw.valhalla.service.data.AccidentPoolService;
import cn.fw.valhalla.service.data.CustomerBaseInfoService;
import cn.fw.valhalla.service.data.CustomerService;
import com.alibaba.fastjson.JSONObject;
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.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static cn.fw.common.businessvalidator.Validator.BV;
/**
* @author : kurisu
* @className : CallReportDealTask
* @description : 通话记录处理任务
* @date: 2021-01-19 16:02
*/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "task", name = "switch", havingValue = "on")
public class CallReportDealTask {
private final CustomerBaseInfoService baseInfoService;
private final CustomerService customerService;
private final AccidentPoolService accidentPoolService;
private final FollowBizService followBizService;
private final EhrRpcService ehrRpcService;
private final StringRedisTemplate redisTemplate;
@Value("${spring.cache.custom.global-prefix}:mq:call:report")
@Getter
private String callReportKey;
@Autowired
public CallReportDealTask(final CustomerBaseInfoService baseInfoService,
final CustomerService customerService,
final AccidentPoolService accidentPoolService,
final FollowBizService followBizService,
final EhrRpcService ehrRpcService,
final StringRedisTemplate redisTemplate) {
this.baseInfoService = baseInfoService;
this.customerService = customerService;
this.accidentPoolService = accidentPoolService;
this.followBizService = followBizService;
this.ehrRpcService = ehrRpcService;
this.redisTemplate = redisTemplate;
}
/**
* 处理通话记录
*/
@Scheduled(initialDelay = 1000 * 5, fixedRate = 1000 * 30)
public void dealCallReport() {
List<String> failList = new ArrayList<>();
String callStr;
while ((callStr = redisTemplate.opsForList().leftPop(getCallReportKey())) != null) {
CallReportDTO dto = JSONObject.parseObject(callStr, CallReportDTO.class);
if (Objects.isNull(dto)) {
continue;
}
final String mobileNo = dto.getPeerNo();
final Long groupId = dto.getGroupId();
final Long staffId = dto.getStaffId();
try {
boolean isValid = Objects.nonNull(staffId) && Objects.nonNull(groupId);
if (!isValid) {
StaffInfoDTO info = ehrRpcService.queryStaffInfoByMobile(mobileNo);
BV.notNull(info, () -> "员工信息获取失败");
assert info != null;
dto.setStaffId(info.getId());
dto.setGroupId(info.getGroupId());
}
followBizService.readCallReport(dto, true, queryAccidentCar(mobileNo, groupId));
followBizService.readCallReport(dto, false, queryCustomerIds(mobileNo, groupId));
} catch (Exception e) {
if (StringUtils.isValid(callStr)) {
failList.add(callStr);
}
log.error("处理通话记录失败", e);
}
}
if (!CollectionUtils.isEmpty(failList)) {
redisTemplate.opsForList().rightPushAll(getCallReportKey(), failList);
}
}
@Nullable
private Long queryAccidentCar(String mobileNo, Long groupId) {
AccidentPool accidentPool = accidentPoolService.getOne(Wrappers.<AccidentPool>lambdaQuery()
.eq(AccidentPool::getReportMobile, mobileNo)
.eq(AccidentPool::getGroupId, groupId)
.orderByDesc(AccidentPool::getCreateTime)
.last(" limit 1 ")
);
if (Objects.isNull(accidentPool)) {
return null;
}
return accidentPool.getId();
}
@Nullable
private Long[] queryCustomerIds(String mobileNo, Long groupId) {
CustomerBaseInfo baseInfo = baseInfoService.queryByMobile(mobileNo, groupId);
if (Objects.isNull(baseInfo)) {
return null;
}
Long baseInfoId = baseInfo.getId();
List<Customer> customerList = customerService.queryByBaseId(baseInfoId);
if (CollectionUtils.isEmpty(customerList)) {
return null;
}
return customerList.stream().map(Customer::getId).toArray(Long[]::new);
}
}