ScrmBaseInfoService.java
5.55 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
package cn.com.glsx.entmicro.service;
import cn.com.glsx.entmicro.entity.magic.MagicTemplateConfig;
import cn.com.glsx.entmicro.entity.magic.MagicTemplateConfigInfo;
import cn.com.glsx.entmicro.entity.magic.TableConfig;
import cn.com.glsx.entmicro.model.request.MagicDynamicInfoRequest;
import cn.com.glsx.entmicro.model.request.MagicDynamicRequest;
import cn.com.glsx.scrm.system.remote.SystemLoginLogRemote;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.metrics.StringUtils;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author zhouhaibao
* @version 1.0
* @description: 数仓数据处理
* @date 2022/1/18 21:07
*/
@Service
@Slf4j
public class ScrmBaseInfoService {
@Autowired
private CustomerService customerService;
@Autowired
private MagicTemplateConfigInfoService magicTemplateConfigInfoService;
public void addCustomer(List<String> baseInfo) {
if (!CollectionUtils.isEmpty(baseInfo)) {
List<JSONObject> collect = baseInfo.stream().map(index -> {
JSONObject jsonObject = JSONObject.parseObject(index);
return jsonObject;
}).collect(Collectors.toList());
Map<Object, List<JSONObject>> merchant_id = collect.stream().collect(Collectors.groupingBy(item -> item.get("merchant_id")));
Set<Map.Entry<Object, List<JSONObject>>> entries = merchant_id.entrySet();
entries.forEach(entry -> {
Integer merchantId = Integer.parseInt(entry.getKey() + "");
TableConfig byMerchantIdAndType = magicTemplateConfigInfoService.findByMerchantIdAndType(merchantId, 2);
if (byMerchantIdAndType != null) {
List<MagicTemplateConfigInfo> magicConfigList = byMerchantIdAndType.getMagicTemplateConfigInfoList();
log.info("==" + JSONObject.toJSONString(magicConfigList));
Map<String, String> magicConfigMapper = magicConfigList.stream().filter(magic -> StringUtils.isNotBlank(magic.getFieldName())).collect(Collectors.toMap(MagicTemplateConfigInfo::getFieldName, MagicTemplateConfigInfo::getCloumnName, (item1, item2) -> item1));
MagicDynamicRequest syncJson = parseDataJson(entry.getValue(), magicConfigMapper, byMerchantIdAndType.getMagicTemplateConfig());
syncJson.setDataType(2);
customerService.customerAddMagic(syncJson);
} else {
log.info("未找到该商户的数仓配置");
}
});
}
}
/**
* 数仓数据转化
*
* @param dataJson 入参
* @param magicConfigMapper 转化mapper
* @param magicTemplateConfig
* @return
*/
private MagicDynamicRequest parseDataJson(List<JSONObject> dataJson, Map<String, String> magicConfigMapper, MagicTemplateConfig magicTemplateConfig) {
MagicDynamicRequest magicDynamicRequest = new MagicDynamicRequest();
log.info("映射关系{}", magicConfigMapper);
List<MagicDynamicInfoRequest> collect = dataJson.stream().map(index -> {
MagicDynamicInfoRequest magicDynamicInfoRequest = new MagicDynamicInfoRequest();
Map<String, Object> custAttr = new HashMap<>();
Map<String, Object> baseAtt = new HashMap<>();
for (Map.Entry<String, String> entryItem : magicConfigMapper.entrySet()) {
String dataValue = index.getString(entryItem.getKey());
if (StringUtils.isBlank(dataValue)) {
dataValue = null;
}
log.info("key值为{},value为{}", entryItem.getKey(), dataValue);
if (entryItem.getValue().equals("customer_phone")) {
magicDynamicInfoRequest.setCustomerPhone(dataValue);
custAttr.put(entryItem.getValue(), dataValue);
} else if (entryItem.getValue().equals("customer_name") || entryItem.getValue().equals("customer_sex") || entryItem.getValue().equals("customer_idcard_no")) {
if (entryItem.getValue().equals("customer_sex")) {
custAttr.put(entryItem.getValue(), converSex(dataValue));
} else {
custAttr.put(entryItem.getValue(), dataValue);
}
} else {
baseAtt.put(entryItem.getValue(), dataValue);
}
}
magicDynamicInfoRequest.setBaseAttr(baseAtt);
magicDynamicInfoRequest.setCustAttr(custAttr);
return magicDynamicInfoRequest;
}).collect(Collectors.toList());
List<MagicDynamicInfoRequest> newList = collect.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MagicDynamicInfoRequest::getCustomerPhone))), ArrayList::new));
magicDynamicRequest.setMerchantId(magicTemplateConfig.getMerchantId());
magicDynamicRequest.setTableName(magicTemplateConfig.getTableName());
magicDynamicRequest.setCustomers(newList);
return magicDynamicRequest;
}
private String converSex(String dataValue) {
String sex = null;
if (dataValue != null) {
if (dataValue.equals("男")) {
sex = "1";
} else {
sex = "2";
}
}
return sex;
}
}