CustomerDetailMagicAsyncComponent.java
10.5 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
package cn.com.glsx.entmicro.manager;
import cn.com.glsx.entmicro.converter.MagicImportCustomerConvert;
import cn.com.glsx.entmicro.entity.Customer;
import cn.com.glsx.entmicro.entity.CustomerDetail;
import cn.com.glsx.entmicro.mapper.CustomerDetailMapper;
import cn.com.glsx.entmicro.mapper.CustomerMapper;
import cn.com.glsx.entmicro.model.CustomerMagicRopRequest;
import cn.com.glsx.entmicro.model.request.CustomerMagicRequest;
import cn.com.glsx.entmicro.model.request.MagicDynamicInfoRequest;
import cn.com.glsx.entmicro.model.request.MagicDynamicRequest;
import cn.com.glsx.entmicro.service.SyncCustomerService;
import cn.com.glsx.entmicro.util.BeanConverUtil;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author wangxw
* @version 1.0.0
* @ClassName CustomerDetailMagicAsyncComponent.java
* @description: ${description}
* @createTime 2021年12月11日 18:15:00
*/
@Slf4j
@Component
public class CustomerDetailMagicAsyncComponent {
@Autowired
private CustomerMapper customerMapper;
@Autowired
private CustomerDetailMapper customerDetailMapper;
@Autowired
private SyncCustomerService syncCustomerService;
@Value("${addway.magic}")
private Integer addwayMagic;
@Async("taskExecutor")
public void asyncUpdateCustomerDetailForMagic(List<CustomerMagicRequest> addList, List<CustomerMagicRequest> updateList, Map<String, Customer> existPhonesMap, MagicDynamicRequest magicDynamicRequest) {
List<MagicDynamicInfoRequest> customers = magicDynamicRequest.getCustomers();
Map<String, MagicDynamicInfoRequest> customer_phone = customers.stream().collect(Collectors.toMap(MagicDynamicInfoRequest::getCustomerPhone, Function.identity()));
// 插入数据
saveMagicCustomer(addList, magicDynamicRequest.getMerchantId(), customer_phone);
// 更新数据
long start = System.currentTimeMillis();
updateMagicCustomer(existPhonesMap, updateList, magicDynamicRequest.getMerchantId(), customer_phone);
long end = System.currentTimeMillis();
long time = end - start;
log.info("批量更新开始时间:" + start);
log.info("结束时间:" + end);
log.info("用时:" + time);
log.info("时间:" + (time / 1000));
// 同步数据
syncCustomerService.syncBatchCustomer(magicDynamicRequest);
}
/**
*
* @param addList
* @param updateList
* @param existPhonesMap
* @param magicDynamicRequest
*/
public void updateCustomerDetailForMagic(List<CustomerMagicRequest> addList, List<CustomerMagicRequest> updateList, Map<String, Customer> existPhonesMap, MagicDynamicRequest magicDynamicRequest) {
List<MagicDynamicInfoRequest> customers = magicDynamicRequest.getCustomers();
Map<String, MagicDynamicInfoRequest> customer_phone = customers.stream().collect(Collectors.toMap(MagicDynamicInfoRequest::getCustomerPhone, Function.identity(), (t1, t2) -> t1));
// 插入数据
saveMagicCustomer(addList, magicDynamicRequest.getMerchantId(), customer_phone);
// 更新数据
long start = System.currentTimeMillis();
updateMagicCustomer(existPhonesMap, updateList, magicDynamicRequest.getMerchantId(), customer_phone);
long end = System.currentTimeMillis();
long time = end - start;
log.info("批量更新开始时间:" + start);
log.info("结束时间:" + end);
log.info("用时:" + time);
log.info("时间:" + (time / 1000));
// 同步数据
syncCustomerService.syncBatchCustomer(magicDynamicRequest);
}
/**
* 保存魔方客户数据到基础表
* @param addList 添加数据
* @param merchantId 门店号
* @param customer_phone
*/
private void saveMagicCustomer(List<CustomerMagicRequest> addList, Integer merchantId, Map<String, MagicDynamicInfoRequest> customer_phone) {
// 整理插入数据
List<Customer> saveCustomerList = addList.stream().filter(req -> !StringUtils.isEmpty(req.getCustomerPhone())).map(req -> {
Customer customer = MagicImportCustomerConvert.buildCustomerByMagic(merchantId, req, true, addwayMagic, req.getAddWay());
return customer;
}).collect(Collectors.toList());
if (CollectionUtils.isEmpty(saveCustomerList)) {
return;
}
// 批量保存客户数据
log.info("保存魔方客户数据到基础表: {}", JSONArray.toJSONString(saveCustomerList));
customerMapper.insertList(saveCustomerList);
//构建客户车辆详情数据
Map<String, CustomerMagicRequest> custMap = addList.stream().collect(Collectors.toMap(CustomerMagicRequest::getCustomerPhone, Function.identity()));
List<CustomerDetail> customerDetailAddList = saveCustomerList.stream().map(req -> {
CustomerMagicRequest request = custMap.get(req.getCustomerPhone());
MagicDynamicInfoRequest magicDynamicInfoRequest = customer_phone.get(req.getCustomerPhone());
CustomerDetail detail = MagicImportCustomerConvert.buildCustomerDetailByMagic(merchantId, request, true);
detail.setCustomerId(req.getId());
Map<String, Object> custAttr = magicDynamicInfoRequest.getCustAttr();
custAttr.put("customer_id", req.getId());
magicDynamicInfoRequest.setCustomerId(req.getId());
return detail;
}).collect(Collectors.toList());
List<CustomerDetail> custDetailAddList = filterEmpty(customerDetailAddList);
if (!CollectionUtils.isEmpty(custDetailAddList)) {
// 批量保存客户详情
customerDetailMapper.insertList(custDetailAddList);
}
}
/**
* 更新魔方客户数据
* @param existPhonesMap
* @param updateList
* @param merchantId
* @param customer_phone
*/
public void updateMagicCustomer(Map<String, Customer> existPhonesMap, List<CustomerMagicRequest> updateList, Integer merchantId, Map<String, MagicDynamicInfoRequest> customer_phone) {
// 1、客户更新数据
List<Customer> customerUpdateList = updateList.stream().filter(req -> existPhonesMap.get(req.getCustomerPhone()) != null).map(req -> {
Customer customer = existPhonesMap.get(req.getCustomerPhone());
Customer customerUpdate = MagicImportCustomerConvert.buildCustomerByMagic(merchantId, req, false, addwayMagic, req.getAddWay());
customerUpdate.setId(customer.getId());
return customerUpdate;
}).collect(Collectors.toList());
// 2、客户详情更新数据
List<CustomerDetail> customerDetailUpdateList = updateList.stream().filter(req -> existPhonesMap.get(req.getCustomerPhone()) != null).map(req -> {
Customer customer = existPhonesMap.get(req.getCustomerPhone());
MagicDynamicInfoRequest magicDynamicInfoRequest = customer_phone.get(req.getCustomerPhone());
CustomerDetail detail = MagicImportCustomerConvert.buildCustomerDetailByMagic(merchantId, req, false);
detail.setCustomerId(customer.getId());
Map<String, Object> custAttr = magicDynamicInfoRequest.getCustAttr();
custAttr.put("customer_id", customer.getId());
magicDynamicInfoRequest.setCustomerId(customer.getId());
return detail;
}).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(customerUpdateList)) {
log.info("更新魔方客户数据到基础表: {}", JSONArray.toJSONString(customerUpdateList));
//以下来源才可以修改企微名称
List<Integer> addWayList = Arrays.asList(0,1,2,3,4,5,6,7,8,9,201,202,104);
customerUpdateList.stream().filter(s -> !addWayList.contains(s.getAddWay())).forEach(s -> s.setCustomerName(null));
customerMapper.batchUpdate(customerUpdateList);
}
if (!CollectionUtils.isEmpty(customerDetailUpdateList)) {
customerDetailUpdateList = filterEmpty(customerDetailUpdateList);
List<CustomerDetail> addlist = new ArrayList<>();
List<CustomerDetail> updatelist = new ArrayList<>();
List<Integer> collect = customerDetailUpdateList.stream().map(CustomerDetail::getCustomerId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(collect)) {
List<CustomerDetail> customerDetails = customerDetailMapper.selectByCustomerIds(collect);
if (CollectionUtils.isEmpty(customerDetails)) {
customerDetails = new ArrayList<>();
}
List<CustomerDetail> finalCustomerDetails = customerDetails;
updatelist = customerDetailUpdateList.stream().filter(index -> finalCustomerDetails.contains(index)).collect(Collectors.toList());
addlist = customerDetailUpdateList.stream().filter(index -> !finalCustomerDetails.contains(index)).collect(Collectors.toList());
}
if (!CollectionUtils.isEmpty(updatelist)) {
customerDetailMapper.batchUpdate(updatelist);
}
if (!CollectionUtils.isEmpty(addlist)) {
customerDetailMapper.insertList(addlist);
}
}
}
private List<CustomerDetail> filterEmpty(List<CustomerDetail> customerDetailUpdateList) {
List<CustomerDetail> newCustomerDetail = new ArrayList<>();
for (CustomerDetail index : customerDetailUpdateList) {
if (StringUtils.isEmpty(index.getAudi()) && StringUtils.isEmpty(index.getCarBrand())
&& StringUtils.isEmpty(index.getCarBrandIds()) && StringUtils.isEmpty(index.getCarNumber())
&& StringUtils.isEmpty(index.getCarVin()) && StringUtils.isEmpty(index.getCarType())
&& index.getMaintenanceDate() == null && index.getComInsuranceEndtime() == null
&& index.getComInsuranceStarttime() == null && index.getBuyDate() == null && index.getArrivalDate() == null) {
continue;
}
newCustomerDetail.add(index);
}
return newCustomerDetail;
}
}