AccidentPoolBizService.java
12 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
package cn.fw.valhalla.service.bus.cust;
import cn.fw.common.cache.locker.DistributedLocker;
import cn.fw.common.web.auth.LoginAuthBean;
import cn.fw.valhalla.common.constant.RoleCode;
import cn.fw.valhalla.common.utils.DateUtil;
import cn.fw.valhalla.domain.db.customer.AccidentPool;
import cn.fw.valhalla.domain.db.customer.Customer;
import cn.fw.valhalla.domain.db.follow.FollowRecord;
import cn.fw.valhalla.domain.db.follow.FollowTask;
import cn.fw.valhalla.domain.db.pool.CustomerCluePool;
import cn.fw.valhalla.domain.dto.AccidentPoolDTO;
import cn.fw.valhalla.domain.enums.*;
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.PostUserDTO;
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.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.redisson.api.RLock;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static cn.fw.common.businessvalidator.Validator.BV;
import static cn.fw.valhalla.common.utils.DateUtil.getNowExpiredDay;
import static cn.fw.valhalla.service.bus.follow.strategy.AbstractFollowStrategy.getCalendarType;
/**
* @author : kurisu
* @className : AccidentPoolBizService
* @description : 事故池
* @date: 2020-08-15 15:20
*/
@Service
@Slf4j
@RequiredArgsConstructor
public class AccidentPoolBizService {
private final DistributedLocker distributedLocker;
private final AccidentPoolService accidentPoolService;
private final FollowTaskService followTaskService;
private final FollowRecordService followRecordService;
private final UserService userService;
private final OopService oopService;
private final SettingBizService settingBizService;
private final TodoRpcService todoRpcService;
private final CustomerCluePoolService customerCluePoolService;
private final CustomerService customerService;
@Value("${spring.cache.custom.global-prefix}:AccidentPool")
@Getter
private String keyPrefix;
@Value("${follow.todo.ACCode}")
@Getter
private String ACCode;
@Transactional(rollbackFor = Exception.class)
public void add2Pool(LoginAuthBean currentUser, AccidentPoolDTO poolDTO) {
final String lockKey = getLockKey(poolDTO.getPlateNo(), currentUser.getGroupId());
Pair<Boolean, RLock> pair = distributedLocker.tryLock(lockKey, TimeUnit.SECONDS, 0, 15);
BV.isTrue(Boolean.TRUE.equals(pair.getKey()), () -> "请勿重复提交");
try {
BV.isTrue(Boolean.TRUE.equals(pair.getKey()), () -> "请勿重复提交");
boolean repetition = isRepetition(currentUser.getGroupId(), poolDTO.getPlateNo());
BV.isFalse(repetition, () -> "该记录已存在,请勿重复添加");
AccidentPool pool = conversion2db(poolDTO, currentUser);
pool.setShopId(poolDTO.getShopId());
pool.setShopName(poolDTO.getName());
BV.notNull(pool.getShopId(), () -> "服务站信息不正确,请确认");
accidentPoolService.save(pool);
afterAddPool(pool);
} catch (Exception e) {
distributedLocker.unlock(lockKey);
throw e;
}
}
private String getLockKey(String plateNo, Long groupId) {
BV.isNotBlank(plateNo, "plateNo cannot be null");
BV.notNull(groupId, "groupId cannot be null");
return String.format("%s:lock:%s:%s", getKeyPrefix(), groupId, plateNo);
}
private void afterAddPool(final AccidentPool pool) {
final String plateNo = pool.getPlateNo();
CustomerCluePool cluePool = customerCluePoolService.queryByPlateNo(plateNo, pool.getGroupId(), FollowTypeEnum.AC);
if (Objects.nonNull(cluePool) && ClueStatusEnum.ONGOING.equals(cluePool.getClueStatus())) {
cluePool.setClueStatus(ClueStatusEnum.FAILURE);
cluePool.setCloseTime(new Date());
customerCluePoolService.updateById(cluePool);
clearTask(cluePool.getId(), cluePool.getGroupId());
}
CustomerCluePool clue = createClue(pool);
FollowTask task = createTask(clue);
createTaskRecord(task);
}
private AccidentPool conversion2db(AccidentPoolDTO dto, LoginAuthBean currentUser) {
AccidentPool pool = new AccidentPool();
BeanUtils.copyProperties(dto, pool);
pool.setAddress(dto.getReportAddress());
pool.setCreateTime(new Date());
pool.setUpdateTime(new Date());
pool.setGroupId(currentUser.getGroupId());
return pool;
}
private boolean isRepetition(Long groupId, String plateNo) {
int count = accidentPoolService.count(Wrappers.<AccidentPool>lambdaQuery()
.eq(AccidentPool::getPlateNo, plateNo)
.eq(AccidentPool::getGroupId, groupId)
.ge(AccidentPool::getCreateTime, DateUtil.startDate(getNowExpiredDay(-1)))
);
return count > 0;
}
/**
* 清理跟进任务
*
* @param clueId
* @param groupId
*/
private void clearTask(Long clueId, Long groupId) {
List<FollowTask> taskList = followTaskService.list(Wrappers.<FollowTask>lambdaQuery()
.eq(FollowTask::getGroupId, groupId)
.eq(FollowTask::getState, TaskStateEnum.ONGOING)
.eq(FollowTask::getClueId, clueId)
.eq(FollowTask::getType, FollowTypeEnum.AC)
);
if (!CollectionUtils.isEmpty(taskList)) {
for (FollowTask task : taskList) {
clearRecord(task.getId());
task.setCloseTime(new Date());
task.setReason(TaskDefeatTypeEnum.E);
task.setState(TaskStateEnum.DEFEAT);
}
followTaskService.updateBatchById(taskList);
}
}
/**
* 清理对应任务的记录
*
* @param taskId
*/
private void clearRecord(Long taskId) {
followRecordService.remove(Wrappers.<FollowRecord>lambdaQuery()
.eq(FollowRecord::getTaskId, taskId)
.eq(FollowRecord::getAddTodo, Boolean.FALSE)
);
List<FollowRecord> list = followRecordService.list(Wrappers.<FollowRecord>lambdaQuery()
.eq(FollowRecord::getTaskId, taskId)
.eq(FollowRecord::getAddTodo, Boolean.TRUE)
.eq(FollowRecord::getOutTime, Boolean.FALSE)
.isNull(FollowRecord::getFollowTime)
);
if (CollectionUtils.isEmpty(list)) {
return;
}
for (FollowRecord record : list) {
record.setLimitTime(DateUtil.localDateTime2Date(LocalDateTime.now()));
BackLogItemDTO dto = new BackLogItemDTO(record.getUserId(), getACCode(), String.valueOf(record.getId()), DateUtil.localDateTime2Date(LocalDateTime.now()), record.getShopId());
todoRpcService.cancel(dto);
}
followRecordService.updateBatchById(list);
}
/**
* 新增线索
*
* @param pool
* @return
*/
private CustomerCluePool createClue(final AccidentPool pool) {
final Customer customer = customerService.queryByPlateNo(pool.getPlateNo(), pool.getGroupId());
final Date datetime = new Date();
final CustomerCluePool cluePool = new CustomerCluePool();
cluePool.setRefererId(pool.getId());
cluePool.setPlateNo(pool.getPlateNo());
cluePool.setClueType(FollowTypeEnum.AC);
cluePool.setAddTime(datetime);
cluePool.setStartTime(datetime);
settingBizService.querySettingByType(FollowTypeEnum.AC, SettingTypeEnum.FAIL_TIME, pool.getGroupId())
.ifPresent(r -> {
Timestamp expired = DateUtil.getExpired(datetime, r.getDetailValue(), getCalendarType(Objects.requireNonNull(SettingUnitEnum.ofValue(r.getUnit()))));
cluePool.setDeadline(expired);
});
cluePool.setClueStatus(ClueStatusEnum.ONGOING);
cluePool.setRedistribution(Boolean.FALSE);
ShopDTO shop = oopService.shop(pool.getShopId());
BV.notNull(shop, () -> "门店信息有误");
List<PostUserDTO> userByRole = userService.getUserByRole(pool.getShopId(), RoleCode.SGCGJ);
BV.isFalse(CollectionUtils.isEmpty(userByRole), () -> "该门店没有事故车跟进人员");
Collections.shuffle(userByRole);
PostUserDTO userDTO = userByRole.get(0);
cluePool.setOriginalUserId(userDTO.getUserId());
cluePool.setOriginalUserName(userDTO.getUserName());
if (Objects.nonNull(customer)) {
if (shop.getId().equals(customer.getShopId())) {
Optional<PostUserDTO> dto = userByRole.stream().filter(u -> u.getUserId().equals(customer.getAdviserId())).findFirst();
dto.ifPresent(d -> {
cluePool.setOriginalUserId(d.getUserId());
cluePool.setOriginalUserName(d.getUserName());
});
}
}
cluePool.setOriginalShopId(shop.getId());
cluePool.setOriginalShopName(shop.getShortName());
cluePool.setGroupId(pool.getGroupId());
cluePool.setCreateTime(new Date());
customerCluePoolService.save(cluePool);
return cluePool;
}
/**
* 新增跟进任务
*
* @param pool
* @return
*/
private FollowTask createTask(final CustomerCluePool pool) {
final FollowTask task = new FollowTask();
task.setClueId(pool.getId());
task.setCustomerId(pool.getRefererId());
task.setType(FollowTypeEnum.AC);
task.setState(TaskStateEnum.ONGOING);
task.setBeginTime(pool.getStartTime());
task.setRedistribution(Boolean.FALSE);
task.setDeadline(pool.getDeadline());
task.setFollowUser(pool.getOriginalUserId());
task.setFollowUserName(pool.getOriginalUserName());
task.setFollowShop(pool.getOriginalShopId());
task.setGroupId(pool.getGroupId());
task.setCreateTime(new Date());
followTaskService.save(task);
return task;
}
/**
* 新增跟进代办任务
*
* @param task
*/
private void createTaskRecord(final FollowTask task) {
final FollowRecord record = new FollowRecord();
record.setTaskId(task.getId());
record.setType(FollowTypeEnum.AC);
record.setCustomerId(task.getCustomerId());
record.setUserId(task.getFollowUser());
record.setUserName(task.getFollowUserName());
record.setPlanTime(task.getBeginTime());
settingBizService.querySettingByType(FollowTypeEnum.AC, SettingTypeEnum.EFFECTIVE_TIME, task.getGroupId())
.ifPresent(r -> {
Timestamp expired = DateUtil.getExpired(task.getBeginTime(), r.getDetailValue(), getCalendarType(Objects.requireNonNull(SettingUnitEnum.ofValue(r.getUnit()))));
record.setDeadline(expired);
});
record.setOutTime(Boolean.FALSE);
record.setAddTodo(Boolean.FALSE);
record.setGroupId(task.getGroupId());
record.setShopId(task.getFollowShop());
settingBizService.querySettingByType(FollowTypeEnum.AC, SettingTypeEnum.LIMITATION, task.getGroupId())
.ifPresent(r -> {
Timestamp expired = DateUtil.getExpired(task.getBeginTime(), r.getDetailValue(), getCalendarType(Objects.requireNonNull(SettingUnitEnum.ofValue(r.getUnit()))));
record.setLimitTime(expired);
});
followRecordService.queryAndSave(record);
}
}