InsurerRpcService.java
5.53 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
package cn.fw.valhalla.rpc.angel;
import cn.fw.angel.sdk.api.InsuranceOrderApiService;
import cn.fw.angel.sdk.api.InsurerApiService;
import cn.fw.angel.sdk.result.InsuranceOrderDTO;
import cn.fw.data.base.domain.common.Message;
import cn.fw.valhalla.common.utils.StringUtils;
import cn.fw.valhalla.rpc.angel.dto.InsuranceDTO;
import cn.fw.valhalla.rpc.angel.dto.InsurerDTO;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* <p>
* create at 2020-07-22
*
* @author kurisu
*/
@Slf4j
@Service
public class InsurerRpcService {
private final InsuranceOrderApiService insuranceEstimateApiService;
private final InsurerApiService insurerApiService;
/**
* Redis工具
*/
private final StringRedisTemplate redisTemplate;
@Value("${spring.cache.custom.global-prefix}:rpc:angel")
@Getter
private String keyPrefix;
@Autowired
InsurerRpcService(final InsuranceOrderApiService insuranceEstimateApiService,
final InsurerApiService insurerApiService,
final StringRedisTemplate redisTemplate) {
this.insuranceEstimateApiService = insuranceEstimateApiService;
this.insurerApiService = insurerApiService;
this.redisTemplate = redisTemplate;
}
public InsurerDTO getById(Long insurerId) {
try {
Message<cn.fw.angel.sdk.result.InsurerDTO> msg = insurerApiService.getById(insurerId);
log.info("查询保险公司结果:{}", msg);
if (!msg.isSuccess() || Objects.isNull(msg.getData())) {
return null;
}
InsurerDTO dto = new InsurerDTO();
BeanUtils.copyProperties(msg.getData(), dto);
return dto;
} catch (Exception e) {
log.error("查询保险公司失败", e);
return null;
}
}
/**
* 根据档案id查询正在生效的保险信息
*
* @param customerId
* @return
*/
public InsuranceDTO queryInsByCustId(Long customerId) {
final String key = generateKey(customerId);
try {
String cache = getFromCache(key);
if (StringUtils.isValid(cache)) {
return JSONObject.parseObject(cache, InsuranceDTO.class);
}
Message<List<InsuranceOrderDTO>> msg = insuranceEstimateApiService.getCusLatest(customerId);
log.info("查询正在生效的保险信息:{}", msg.getResult());
List<InsuranceOrderDTO> data = msg.getData();
if (!msg.isSuccess() || CollectionUtils.isEmpty(data)) {
return null;
}
Date tciEndTime = null;
Date vciEndTime = null;
Date tciStartTime = null;
Date vciStartTime = null;
for (InsuranceOrderDTO insuranceOrderDTO : data) {
if (1 == insuranceOrderDTO.getInsuranceInfo().getInsuranceType()) {
tciEndTime = insuranceOrderDTO.getInsuranceInfo().getEndTime();
tciStartTime = insuranceOrderDTO.getInsuranceInfo().getStartTime();
} else if (2 == insuranceOrderDTO.getInsuranceInfo().getInsuranceType()) {
vciEndTime = insuranceOrderDTO.getInsuranceInfo().getEndTime();
vciStartTime = insuranceOrderDTO.getInsuranceInfo().getStartTime();
}
}
final Date now = new Date();
boolean before1 = !Objects.isNull(tciEndTime) && now.before(tciEndTime);
boolean before2 = !Objects.isNull(vciEndTime) && now.before(vciEndTime);
if (before1 || before2) {
InsuranceDTO dto = new InsuranceDTO();
BeanUtils.copyProperties(data, dto);
dto.setEffectDate(vciStartTime);
dto.setExpiryDate(vciEndTime);
dto.setTciEffectDate(tciStartTime);
dto.setTciExpiryDate(tciEndTime);
setToCache(key, JSONObject.toJSONString(dto), 30);
return dto;
}
return null;
} catch (Exception e) {
log.error("查询正在生效的保险信息失败:", e);
return null;
}
}
private String getFromCache(final String key) {
String json = null;
try {
BoundValueOperations<String, String> ops = redisTemplate.boundValueOps(key);
json = ops.get();
} catch (Exception e) {
log.error("从缓存获取保有客保险信息失败", e);
}
return json;
}
private void setToCache(final String key, final String value, long timeout) {
try {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("缓存保有客保险信息失败", e);
}
}
private String generateKey(final Long customerId) {
Assert.notNull(customerId, "customerId cannot be null");
return String.format("%s:%s", getKeyPrefix(), customerId);
}
}