SyncCustomerService.java
11.7 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
package cn.com.glsx.entmicro.service;/**
* @author zhouhaibao
* @date 2021/12/11
*/
import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.entity.CustomerMerge;
import cn.com.glsx.entmicro.mapper.CustomerMapper;
import cn.com.glsx.entmicro.mapper.CustomerMergeMapper;
import cn.com.glsx.entmicro.mapper.EntmicroCustomerFollowMapper;
import cn.com.glsx.entmicro.model.CustomerMagicRopResponse;
import cn.com.glsx.entmicro.model.MerchantRopResponse;
import cn.com.glsx.entmicro.model.request.MagicDynamicInfoRequest;
import cn.com.glsx.entmicro.model.request.MagicDynamicRequest;
import cn.com.glsx.entmicro.model.response.CustomerInfoResponse;
import cn.com.glsx.entmicro.service.cache.RedisCacheService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author zhouhaibao
* @version 1.0
* @description: TODO
* @date 2021/12/11 12:15
*/
@Slf4j
@Component
public class SyncCustomerService {
@Autowired
private RopCommonServie ropCommonServie;
@Autowired
private MerchantService merchantService;
@Value("${addway.magic}")
private Integer addwayMagic;
@Autowired
private Environment environment;
@Autowired
private CustomerMergeMapper customerMergeMapper;
@Autowired
private CustomerMapper customerMapper;
@Resource
private EntmicroCustomerFollowMapper entmicroCustomerFollowMapper;
@Resource
private ScrmDataCubeJoinRestCloudService scrmDataCubeJoinRestCloudService;
@Resource
private RedisCacheService redisCacheService;
private static final String SYNC_CUSTOMER_REMARK_TO_CDP_CACHE = "sync_customer_remark_to_cdp_cache";
public void syncCustomerRemarkToCdp() {
Date updateTime = null;
try {
Object cache = redisCacheService.getCache(SYNC_CUSTOMER_REMARK_TO_CDP_CACHE);
if (cache != null) {
updateTime = (Date) cache;
}
redisCacheService.setCache(SYNC_CUSTOMER_REMARK_TO_CDP_CACHE, new Date(), 60 * 60 * 24 * 7L);
} catch (Exception e) {
log.warn(e.getMessage(), e);
}
String merchants = environment.getProperty("scrm_syncCustomerTypeTag_merchants");
List<Integer> merchantIdList = Arrays.stream(merchants.split(",")).map(Integer::valueOf).collect(Collectors.toList());
List<Map<String, Object>> maps = entmicroCustomerFollowMapper.selectCustomerFollowRemark(updateTime, merchantIdList);
log.info("syncCustomerRemarkToCdp data:{}", maps);
if (CollectionUtils.isEmpty(maps)) {
log.warn("maps is empty");
return;
}
scrmDataCubeJoinRestCloudService.reportDataProcess(JSON.toJSONString(maps));
}
/**
* 同步数据到大数据,取消异步操作;异步操作交给组件实现({@link cn.com.glsx.entmicro.manager.CustomerDetailMagicAsyncComponent})
*
* @param magicDynamicRequest
*/
public void syncBatchCustomer(MagicDynamicRequest magicDynamicRequest) {
List<MagicDynamicInfoRequest> customers = magicDynamicRequest.getCustomers();
if (!CollectionUtils.isEmpty(customers)) {
String customerBatchCount = environment.getProperty("customer.batchCount");
List<List<MagicDynamicInfoRequest>> partition = ListUtils.partition(customers, Integer.parseInt(customerBatchCount));
partition.forEach(index -> {
magicDynamicRequest.setCustomers(index);
syncBatchCustomerData(magicDynamicRequest, 0);
});
}
}
/**
* 批量同步数仓、企微魔方、小程序客户到大数据
*
* @param magicDynamicRequest
* @param merChangeStatus
*/
public void syncBatchCustomerData(MagicDynamicRequest magicDynamicRequest, Integer merChangeStatus) {
log.info("数仓客户同步到大数据: {}", JSONArray.toJSONString(magicDynamicRequest));
//渠道号
MerchantRopResponse merchantByMerchantNo = merchantService.findMerchantByMerchantNo(magicDynamicRequest.getMerchantId());
Integer channelId = merchantByMerchantNo.getChannelId();
// 门店(商户)变更状态 0-正常 1-门店已变更
String jsonStr = JSONObject.toJSONStringWithDateFormat(magicDynamicRequest, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat);
List<CustomerMagicRopResponse> customerMagicRopResponses = ropCommonServie.syncEntCubeImp(magicDynamicRequest, channelId, addwayMagic);
if (!CollectionUtils.isEmpty(customerMagicRopResponses)) {
//客戶來源赋值
List<MagicDynamicInfoRequest> customers = magicDynamicRequest.getCustomers();
for (CustomerMagicRopResponse customerMagicRopRespons : customerMagicRopResponses) {
for (MagicDynamicInfoRequest customer : customers) {
if (null == customerMagicRopRespons.getCustomerId() || null == customer.getCustomerId()) {
continue;
} else if (customerMagicRopRespons.getCustomerId().equals(customer.getCustomerId())) {
customerMagicRopRespons.setAddWay(customer.getAddWay());
break;
}
}
}
//批量更新oneid
log.info("魔方客户更新oneId: {}", JSONArray.toJSONString(customerMagicRopResponses));
customerMapper.batchUpdateOneId(customerMagicRopResponses);
List<String> oneIds = customerMagicRopResponses.stream().map(CustomerMagicRopResponse::getOneId).collect(Collectors.toList());
//构建参数
customerMerge(customerMagicRopResponses, magicDynamicRequest);
Map<String, CustomerInfoResponse> customerExists = customerMergeMapper.getCustomerByOneIds(oneIds, magicDynamicRequest.getMerchantId());
if (null != customerExists && customerExists.size() > 0) {
List<CustomerMerge> addList = new ArrayList<>();
List<CustomerMerge> updateList = new ArrayList<>();
for (CustomerMagicRopResponse customerMagicRopRespons : customerMagicRopResponses) {
String oneId = customerMagicRopRespons.getOneId();
if (StringUtils.isEmpty(oneId)) {
continue;
}
if (customerExists.containsKey(oneId)) {
CustomerMerge customerMerge = buildCustomerMerge(customerMagicRopRespons, false);
CustomerInfoResponse customerInfoResponse = customerExists.get(oneId);
Integer id = customerInfoResponse.getId();
if (null != id) {
customerMerge.setId(id);
updateList.add(customerMerge);
}
} else {
CustomerMerge customerMerge = buildCustomerMerge(customerMagicRopRespons, true);
addList.add(customerMerge);
}
}
//更新merge客户表
if (!CollectionUtils.isEmpty(updateList)) {
log.info("数仓客户更新merge数据: {}", updateList);
customerMergeMapper.batchUpdate(updateList);
}
//新增merge客户表
if (!CollectionUtils.isEmpty(addList)) {
log.info("数仓客户新增merge数据: {}", addList);
customerMergeMapper.insertList(addList);
}
} else {
//新增merge客户表
List<CustomerMerge> custsAdd = customerMagicRopResponses.stream().map(c -> buildCustomerMerge(c, true)).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(custsAdd)) {
log.info("新增客户size{}, 数仓客户新增merge数据: {}", custsAdd.size(), JSONArray.toJSONString(custsAdd));
customerMergeMapper.insertList(custsAdd);
}
}
}
}
private boolean verifyAddWay(Integer nowAddWay, Integer initialAddWay) {
nowAddWay = nowAddWay == null ? 0 : nowAddWay;
initialAddWay = initialAddWay == null ? 0 : initialAddWay;
// 初始来源是魔方,则当前来源为魔方才能修改
if (initialAddWay == 101 && nowAddWay != 101) {
return true;
// 初始来源非魔方,则当前来源为魔方和保单才能修改
} else if (initialAddWay != 101 && nowAddWay != 101 && nowAddWay != 103) {
return true;
}
return false;
}
/**
* 客户优先级合并
*
* @param customerMagicRopResponses 返回的客户
* @param magicDynamicRequest 魔方导入客户
*/
private void customerMerge(List<CustomerMagicRopResponse> customerMagicRopResponses, MagicDynamicRequest magicDynamicRequest) {
List<MagicDynamicInfoRequest> customers = magicDynamicRequest.getCustomers();
Map<Integer, MagicDynamicInfoRequest> customer_phone = customers.stream().collect(Collectors.toMap(MagicDynamicInfoRequest::getCustomerId, Function.identity()));
customerMagicRopResponses.forEach(index -> {
MagicDynamicInfoRequest customerMagicRopRequest = customer_phone.get(index.getCustomerId());
if (customerMagicRopRequest != null) {
Map<String, Object> custAttr = customerMagicRopRequest.getCustAttr();
if (custAttr.get("customer_name") != null) {
index.setCustomerName(custAttr.get("customer_name") + "");
}
if (custAttr.get("customer_idcard_no") != null) {
index.setIdCard(custAttr.get("customer_idcard_no") + "");
}
index.setCustomerPhone(custAttr.get("customer_phone") + "");
Object customerSex = custAttr.get("customer_sex");
if (customerSex != null) {
index.setCustomerSex(Integer.parseInt(customerSex + ""));
}
}
});
}
/**
* 构建魔方合并表数据
*
* @param res
* @param isAdd
* @return
*/
private CustomerMerge buildCustomerMerge(CustomerMagicRopResponse res, boolean isAdd) {
CustomerMerge customerMerge = new CustomerMerge();
customerMerge.setCustomerPhone(res.getCustomerPhone());
customerMerge.setCustomerName(res.getCustomerName());
customerMerge.setIdCard(res.getIdCard());
customerMerge.setCustomerSex(res.getCustomerSex());
customerMerge.setAddWay(res.getAddWay());
customerMerge.setMerchantId(res.getMerchantId());
if (isAdd) {
//新增
customerMerge.setOneId(res.getOneId());
customerMerge.setCreatedTime(new Date());
customerMerge.setDelFlag(Constants.NORMAL_FLAG);
//正常
customerMerge.setStatus(0);
//未加入企微
customerMerge.setIsJoin(0);
}
log.info("构建魔方合并表数据:{}, 构建后数据: {}", JSONArray.toJSONString(res), JSONArray.toJSONString(customerMerge));
return customerMerge;
}
}