PubFollowBizService.java
11 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.pub;
import cn.fw.common.constant.CommonConstant;
import cn.fw.valhalla.common.utils.StringUtils;
import cn.fw.valhalla.common.utils.ThreadPoolUtil;
import cn.fw.valhalla.domain.db.customer.Customer;
import cn.fw.valhalla.domain.db.customer.CustomerBaseInfo;
import cn.fw.valhalla.domain.db.follow.ClueTask;
import cn.fw.valhalla.domain.db.pool.PublicPool;
import cn.fw.valhalla.domain.db.pub.PubCluePool;
import cn.fw.valhalla.domain.enums.*;
import cn.fw.valhalla.domain.vo.pool.PubPoolSimpleVO;
import cn.fw.valhalla.rpc.oop.OopService;
import cn.fw.valhalla.rpc.oop.dto.ShopDTO;
import cn.fw.valhalla.service.bus.follow.strategy.impl.PubFollowStrategy;
import cn.fw.valhalla.service.data.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AccessLevel;
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.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import static cn.fw.common.businessvalidator.Validator.BV;
/**
* 公共池跟进处理
*
* @author : kurisu
* @version : 1.0
* @className : PubFollowBizService
* @description : 公共池跟进处理
* @date : 2023-03-17 17:23
*/
@Slf4j
@Service
public class PubFollowBizService {
private final PubCluePoolService pubCluePoolService;
private final PublicPoolService publicPoolService;
private final StammkundePoolService stammkundePoolService;
private final CustomerService customerService;
private final CustomerBaseInfoService customerBaseInfoService;
private final ClueTaskService clueTaskService;
private final StringRedisTemplate stringRedisTemplate;
private final OopService oopService;
private final PubFollowStrategy pubFollowStrategy;
@Value("${follow.todo.PubFollowCode}")
@Getter(AccessLevel.PRIVATE)
private String pubFollowCode;
@Value("${spring.cache.custom.global-prefix}:role-change-need-close:clue")
@Getter(AccessLevel.PRIVATE)
private String roleChangeNeedCloseClueKey;
@Autowired
public PubFollowBizService(final PubCluePoolService pubCluePoolService,
final PublicPoolService publicPoolService,
final StammkundePoolService stammkundePoolService,
final CustomerService customerService,
final CustomerBaseInfoService customerBaseInfoService,
final ClueTaskService clueTaskService,
final StringRedisTemplate stringRedisTemplate,
final OopService oopService,
final PubFollowStrategy pubFollowStrategy) {
this.pubCluePoolService = pubCluePoolService;
this.publicPoolService = publicPoolService;
this.stammkundePoolService = stammkundePoolService;
this.customerService = customerService;
this.customerBaseInfoService = customerBaseInfoService;
this.clueTaskService = clueTaskService;
this.stringRedisTemplate = stringRedisTemplate;
this.oopService = oopService;
this.pubFollowStrategy = pubFollowStrategy;
}
/**
* 人员角色变动或缓存需要战败的vin
*
* @param userId
* @param shopId
*/
public void onRoleChange(Long userId, Long shopId) {
List<PubCluePool> list = pubCluePoolService.list(Wrappers.<PubCluePool>lambdaQuery()
.eq(PubCluePool::getAdviserId, userId)
.eq(PubCluePool::getState, PublicClueStateEnum.ONGOING)
);
if (CollectionUtils.isEmpty(list)) {
return;
}
BoundSetOperations<String, String> setOps = stringRedisTemplate.boundSetOps(getRoleChangeNeedCloseClueKey());
for (PubCluePool pool : list) {
setOps.add(String.valueOf(pool.getId()));
}
}
/**
* 开始线索
*
* @param pool
*/
@Transactional(rollbackFor = Exception.class)
public void startClue(PubCluePool pool) {
pubFollowStrategy.startClue(pool);
pool.setBegun(Boolean.TRUE);
pubCluePoolService.updateById(pool);
}
/**
* 关闭线索
*
* @param str
*/
@Transactional(rollbackFor = Exception.class)
public void closeClue(String str) {
String[] dataArr = str.split(CommonConstant.Symbol.AT);
if (dataArr.length < 4) {
log.info("关闭公共池线索失败:{}", str);
return;
}
String vin = dataArr[0];
Long adviserId = Long.parseLong(dataArr[1]);
Long groupId = Long.parseLong(dataArr[2]);
Long shopId = Long.parseLong(dataArr[3]);
PubCluePool pool = pubCluePoolService.getOne(Wrappers.<PubCluePool>lambdaQuery()
.eq(PubCluePool::getVin, vin)
.eq(PubCluePool::getGroupId, groupId)
.eq(PubCluePool::getState, PublicClueStateEnum.ONGOING)
, Boolean.FALSE
);
if (Objects.isNull(pool)) {
return;
}
if (pool.getAdviserId().equals(adviserId)) {
pubFollowStrategy.clueConvertSuccess(pool);
return;
}
pubFollowStrategy.clueConvertFailed(pool, adviserId, shopId);
}
/**
* 查询最近分配的20条线索
*
* @param userId
* @param groupId
* @return
*/
public List<PubPoolSimpleVO> log(Long userId, Long groupId) {
List<PubCluePool> list = pubCluePoolService.list(Wrappers.<PubCluePool>lambdaQuery()
.eq(PubCluePool::getAdviserId, userId)
.eq(PubCluePool::getGroupId, groupId)
.orderByDesc(PubCluePool::getStartTime)
.last(" limit 20")
);
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
List<PubPoolSimpleVO> simpleList = list.stream()
.map(PubPoolSimpleVO::with)
.collect(Collectors.toList());
CompletableFuture<Void>[] futureArr = simpleList.stream()
.map(pool -> CompletableFuture.runAsync(() -> {
Customer customer = customerService.queryByFrameNo(pool.getVin(), pool.getGroupId());
if (Objects.nonNull(customer)) {
pool.setPlateNo(customer.getPlateNo());
String brandName = StringUtils.isEmpty(customer.getBrandName()) ? "" : customer.getBrandName() + " ";
String seriesName = StringUtils.isEmpty(customer.getSeriesName()) ? "" : customer.getSeriesName() + " ";
String specName = StringUtils.isEmpty(customer.getSpecName()) ? "" : customer.getSpecName();
String carName = String.format("%s%s%s", brandName, seriesName, specName);
pool.setCarName(carName);
}
}, ThreadPoolUtil.getInstance().getExecutor())).<CompletableFuture<Void>>toArray(CompletableFuture[]::new);
try {
CompletableFuture.allOf(futureArr).get();
} catch (Exception e) {
log.error("数据查询失败", e);
}
return simpleList;
}
/**
* 角色变动结束线索
*
* @param id
*/
@Transactional(rollbackFor = Exception.class)
public void roleChangeDefeatClue(String id) {
PubCluePool pool = pubCluePoolService.getById(Long.valueOf(id));
if (Objects.isNull(pool)) {
return;
}
Customer customer = customerService.queryByFrameNo(pool.getVin(), pool.getGroupId());
endClue(pool);
if (Objects.nonNull(customer)) {
customerRemoval(customer);
}
}
/**
* 结束线索
*
* @param pool
*/
@Transactional(rollbackFor = Exception.class)
public void endClue(PubCluePool pool) {
ClueTask task = clueTaskService.getOne(Wrappers.<ClueTask>lambdaQuery()
.eq(ClueTask::getClueId, pool.getId())
.eq(ClueTask::getType, FollowTypeEnum.PL)
.eq(ClueTask::getState, TaskStateEnum.ONGOING)
, Boolean.FALSE);
if (Objects.nonNull(task)) {
pubFollowStrategy.onRoleChangeCloseTask(task);
}
pool.setState(PublicClueStateEnum.DEFEAT);
pool.setCloseTime(LocalDateTime.now());
pool.setDefeatReason(TaskDefeatTypeEnum.D);
pubCluePoolService.updateById(pool);
}
/**
* 移除档案到公共池
*
* @param customer
*/
private void customerRemoval(Customer customer) {
PublicPool publicPool = createPublicPool(customer);
publicPoolService.save(publicPool);
stammkundePoolService.reject(customer.getId(), customer.getGroupId(), DefeatReasonEnum.RC);
customerService.update(Wrappers.<Customer>lambdaUpdate()
.set(Customer::getTemporary, null)
.set(Customer::getShopId, null)
.set(Customer::getAdviserId, null)
.eq(Customer::getId, customer.getId())
);
}
private PublicPool createPublicPool(Customer customer) {
final Long shopId = Optional.ofNullable(customer.getLastArrivalShop()).orElse(customer.getShopId());
ShopDTO shop = oopService.shop(shopId);
BV.notNull(shop, () -> String.format("门店[%s]信息获取失败", shopId));
String shopName = shop.getShortName();
final Long baseId = customer.getBaseId();
CustomerBaseInfo baseInfo = customerBaseInfoService.getById(baseId);
BV.notNull(baseInfo, () -> String.format("档案车主信息获取失败[%s]", baseId));
PublicPool pool = new PublicPool();
pool.setCustomerId(customer.getId());
pool.setCustomerName(baseInfo.getName());
pool.setPlateNo(customer.getPlateNo());
pool.setFrameNo(customer.getFrameNo());
pool.setBrandId(customer.getBrandId());
pool.setBrandName(customer.getBrandName());
pool.setSeriesId(customer.getSeriesId());
pool.setSeriesName(customer.getSeriesName());
pool.setSpecId(customer.getSpecId());
pool.setSpecName(customer.getSpecName());
pool.setAddress(baseInfo.getAddress());
pool.setCompany(baseInfo.getCompanyName());
pool.setType(PublicPoolTypeEnum.RC);
pool.setShopId(shopId);
pool.setShopName(shopName);
pool.setTimes(customer.getArrivalCount());
pool.setReason(PublicPoolTypeEnum.RC.getName());
pool.setGroupId(customer.getGroupId());
return pool;
}
}