AbstractCustomerService.java
12.2 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
package cn.fw.valhalla.service.bus.cust;
import cn.fw.hermes.sdk.api.ImSendMessage;
import cn.fw.oop.sdk.enums.BizTypeEnum;
import cn.fw.valhalla.common.utils.DateUtil;
import cn.fw.valhalla.component.CustomerChangeProducer;
import cn.fw.valhalla.domain.db.customer.Customer;
import cn.fw.valhalla.domain.db.customer.CustomerBaseInfo;
import cn.fw.valhalla.domain.db.customer.CustomerLoanInfo;
import cn.fw.valhalla.domain.db.pool.StammkundePool;
import cn.fw.valhalla.domain.dto.CustomerDetailDto;
import cn.fw.valhalla.domain.dto.StammkundeDto;
import cn.fw.valhalla.domain.enums.StammkundeSourcesEnum;
import cn.fw.valhalla.domain.enums.StammkundeStatusEnum;
import cn.fw.valhalla.rpc.angel.InsurerRpcService;
import cn.fw.valhalla.rpc.angel.dto.InsuranceDTO;
import cn.fw.valhalla.rpc.ehr.EhrRpcService;
import cn.fw.valhalla.rpc.erp.UserService;
import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO;
import cn.fw.valhalla.rpc.erp.dto.UserRoleDataRangeDTO;
import cn.fw.valhalla.rpc.oop.OopService;
import cn.fw.valhalla.rpc.oop.dto.ShopDTO;
import cn.fw.valhalla.rpc.oop.dto.SpecDTO;
import cn.fw.valhalla.sdk.enums.SourceTypeEnum;
import cn.fw.valhalla.sdk.result.CustomerInfoDto;
import cn.fw.valhalla.service.data.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import static cn.fw.common.businessvalidator.Validator.BV;
/**
* @author : kurisu
* @className : CustomerService
* @description : 档案抽象类
* @date: 2020-08-13 09:51
*/
public abstract class AbstractCustomerService {
@Autowired
protected UserService userService;
@Autowired
protected EhrRpcService ehrRpcService;
@Autowired
protected OopService oopService;
@Autowired
protected CustomerService customerService;
@Autowired
protected CustomerBaseInfoService customerBaseInfoService;
@Autowired
protected InsurerRpcService insurerRpcService;
@Autowired
protected CustomerChangeProducer customerChangeProducer;
@Autowired
protected ImSendMessage imSendMessage;
@Autowired
protected CustomerLoanInfoService customerLoanInfoService;
@Autowired
protected StammkundePoolService stammkundePoolService;
@Autowired
protected PublicCluePoolService publicCluePoolService;
/**
* 组合车辆档案和人员档案信息
*
* @param customer
* @return
*/
protected CustomerDetailDto renderDto(@NotNull Customer customer) {
CustomerBaseInfo baseInfo = customerBaseInfoService.queryById(customer.getBaseId());
return renderDto(customer, baseInfo);
}
/**
* 组合车辆档案和人员档案信息
*
* @param customer
* @param baseInfo
* @return
*/
protected CustomerDetailDto renderDto(@NotNull Customer customer, CustomerBaseInfo baseInfo) {
CustomerDetailDto dto = new CustomerDetailDto();
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());
}
CustomerLoanInfo loanInfo = customerLoanInfoService.getOne(Wrappers.<CustomerLoanInfo>lambdaQuery()
.eq(CustomerLoanInfo::getGroupId, customer.getGroupId())
.eq(CustomerLoanInfo::getFrameNo, customer.getFrameNo())
.last(" limit 1")
);
if (Objects.nonNull(loanInfo)) {
dto.setLoanCustomer(LocalDate.now().isBefore(DateUtil.date2LocalDate(loanInfo.getExpires())));
dto.setExpires(loanInfo.getExpires());
dto.setPeriods(loanInfo.getPeriods());
}
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.setInsuranceExpires(customer.getInsuranceExpires());
dto.setArrivalTime(customer.getArrivalTime());
dto.setAdviserId(customer.getAdviserId());
dto.setArrivalCount(customer.getArrivalCount());
UserInfoDTO user = ehrRpcService.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());
dto.setShopId(customer.getShopId());
return dto;
}
/**
* 根据userid和角色码查询商家id
*
* @param userId
* @param roleCOde
* @return
*/
protected ShopDTO queryDealId(Long userId, String roleCOde) {
List<UserRoleDataRangeDTO> dataRange = userService.getUserRoleDataRange(userId, roleCOde);
if (CollectionUtils.isEmpty(dataRange)) {
return null;
}
Long shopId = dataRange.get(0).getRangeValue();
ShopDTO shop = oopService.shop(shopId);
BV.isTrue(shop != null && BizTypeEnum.AFTER_SALE.getValue().equals(shop.getBizType()), "角色门店信息不正确");
return shop;
}
/**
* 查询档案正在生效的保险信息
*
* @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 customer 变更后的档案
*/
protected void customerChangeHandler(Customer customer) {
Long memberId = Optional.ofNullable(customerBaseInfoService.queryById(customer.getBaseId())).map(CustomerBaseInfo::getMemberId).orElse(null);
final CustomerInfoDto customerInfoDto = new CustomerInfoDto();
BeanUtils.copyProperties(customer, customerInfoDto);
customerInfoDto.setId(customer.getId());
customerInfoDto.setConsultantId(customer.getAdviserId());
customerInfoDto.setMemberId(memberId);
customerChangeProducer.send(customerInfoDto);
}
/**
* 创建新档案
*
* @param customer
* @param baseInfo
* @param orderCar
* @return
*/
protected Long saveNewOne(Customer customer, CustomerBaseInfo baseInfo, boolean orderCar) {
final SpecDTO spec = oopService.spec(customer.getSpecId());
customer.setCarImage(Optional.ofNullable(spec).orElse(new SpecDTO()).getThumbnail());
final String plateNo = customer.getPlateNo();
final int count = customerService.count(Wrappers.<Customer>lambdaQuery()
.eq(Customer::getYn, Boolean.TRUE)
.eq(Customer::getPlateNo, plateNo)
.eq(Customer::getFrameNo, customer.getFrameNo())
);
BV.isTrue(count == 0, "车辆档案已存在已存在");
CustomerBaseInfo info = customerBaseInfoService.queryByMemberId(baseInfo.getMemberId(), baseInfo.getGroupId());
if (Objects.isNull(info)) {
info = customerBaseInfoService.queryByMobile(baseInfo.getMobile(), baseInfo.getGroupId());
}
if (Objects.isNull(info)) {
info = customerBaseInfoService.queryByIdCode(baseInfo.getIdCode(), baseInfo.getGroupId());
}
if (Objects.nonNull(info)) {
baseInfo.setId(info.getId());
baseInfo.setCreateTime(null);
}
customerBaseInfoService.saveOrUpdate(baseInfo);
customer.setBaseId(baseInfo.getId());
customer.setCreateTime(new Date());
customer.setYn(Boolean.TRUE);
customer.setUpdateTime(new Date());
customer.setSource(SourceTypeEnum.TO_SHOP);
if (orderCar) {
customer.setSource(SourceTypeEnum.SALE_ORDER);
}
if (customer.getBuyDate() == null) {
customer.setBuyDate(customer.getRegDate());
}
boolean saved = customerService.save(customer);
if (saved) {
StammkundePool simpleInfo = createSimpleInfo(customer, customer.getAdviserId());
if (orderCar) {
simpleInfo.setSources(StammkundeSourcesEnum.ORDER);
} else {
simpleInfo.setSources(StammkundeSourcesEnum.RECEPTION);
}
simpleInfo.setPoolStatus(StammkundeStatusEnum.KUNDE);
simpleInfo.setAktiv(Boolean.TRUE);
simpleInfo.setActivationTime(new Date());
stammkundePoolService.save(simpleInfo);
final CustomerInfoDto customerInfoDto = new CustomerInfoDto();
BeanUtils.copyProperties(customer, customerInfoDto);
customerInfoDto.setConsultantId(customer.getAdviserId());
customerInfoDto.setMemberId(baseInfo.getMemberId());
this.customerChangeProducer.send(customerInfoDto);
}
return customer.getId();
}
protected Long create(Customer customer, CustomerBaseInfo baseInfo) {
return saveNewOne(customer, baseInfo, false);
}
protected StammkundePool rejectAndNew(StammkundeDto dto) {
stammkundePoolService.reject(dto);
StammkundePool stammkundePool = new StammkundePool();
stammkundePool.setCustomerId(dto.getCustomerId());
stammkundePool.setShopId(dto.getShopId());
stammkundePool.setGroupId(dto.getGroupId());
stammkundePool.setAdviserId(dto.getNewUserId());
stammkundePool.setAdviserName(dto.getNewUserName());
return stammkundePool;
}
protected StammkundePool createSimpleInfo(Customer customer, Long userId) {
StammkundePool stammkundePool = createSimpleInfo(customer);
if (Objects.nonNull(userId)) {
stammkundePool.setAdviserId(userId);
UserInfoDTO user = ehrRpcService.user(userId);
if (Objects.nonNull(user)) {
stammkundePool.setAdviserName(user.getUserName());
}
}
return stammkundePool;
}
protected StammkundePool createSimpleInfo(Customer customer) {
StammkundePool stammkundePool = new StammkundePool();
stammkundePool.setCustomerId(customer.getId());
stammkundePool.setShopId(customer.getShopId());
stammkundePool.setGroupId(customer.getGroupId());
stammkundePool.setAktiv(Boolean.FALSE);
stammkundePool.setCreateTime(new Date());
return stammkundePool;
}
}