CustomerMergeServiceTest.java
5.74 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
package cn.com.glsx.entmicro.service;
import cn.com.glsx.entmicro.Main;
import cn.com.glsx.entmicro.entity.CustomerMerge;
import cn.com.glsx.entmicro.mapper.CustomerMergeMapper;
import cn.com.glsx.entmicro.util.FileUploadServerData;
import cn.com.glsx.entmicro.util.FileUploadUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import tk.mybatis.mapper.entity.Example;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Field;
import java.util.*;
/**
* @Author yelq
* @Version 1.0
* @Description:
* @Date 2022/5/15 14:23
*/
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Main.class)
public class CustomerMergeServiceTest {
@Autowired
private CustomerMergeMapper customerMergeMapper;
@Test
public void Test01() throws Exception {
int n = 2000;
int m = 10000;
//scrm_customer_merge_temp
List<Integer> list = customerMergeMapper.selectAllId("scrm_customer_merge");
if (list == null || list.isEmpty()) {
return;
}
List<Map<Integer, Object>> all = new ArrayList<>();
if (list.size()<=n){
Example example = new Example(CustomerMerge.class);
example.createCriteria().andIn("id",list);
List<CustomerMerge> mergeList = customerMergeMapper.selectByExample(example);
if (mergeList==null||mergeList.isEmpty()){
return;
}
List<Map<Integer, Object>> list1 = deelData(mergeList);
saveErrorUrl(list1);
}else {
int num = list.size()/n;
for (int i = 1; i <= num; i++) {
Example example = new Example(CustomerMerge.class);
if (i==num){
example.createCriteria().andIn("id",list.subList(i*n, list.size()-1));
}else {
example.createCriteria().andIn("id",list.subList(i-1, i * n));
}
List<CustomerMerge> mergeList = customerMergeMapper.selectByExample(example);
if (mergeList==null||mergeList.isEmpty()){
return;
}
List<Map<Integer, Object>> list2 = deelData(mergeList);
all.addAll(list2);
if (!all.isEmpty()&&all.size()>=m){
saveErrorUrl(all);
all.clear();
}
}
}
}
@Test
public void Test02() throws Exception{
List<Map<Integer,Object>> errList = new ArrayList<>();
Map<Integer,Object> map = new HashMap<>();
map.put(0,"hello");
errList.add(map);
saveErrorUrl(errList);
}
public List<Map<Integer,Object>> deelData(List<CustomerMerge> list){
List<Map<Integer,Object>> errList = new ArrayList<>();
for (CustomerMerge customerMerge : list) {
List<CustomerMerge> mergeList = customerMergeMapper.selectCustomerMergeByOneIdAndMerchantId(customerMerge.getOneId(),
customerMerge.getMerchantId(), "scrm_customer_merge");
if (mergeList == null || mergeList.isEmpty()) {
Map<Integer,Object> map = new HashMap<>();
map.put(0,customerMerge.getOneId());
map.put(1,customerMerge.getMerchantId());
map.put(2,"数据不存在");
errList.add(map);
continue;
}
CustomerMerge merge1 = mergeList.get(0);
String json = compareCustomer(customerMerge, merge1);
if (json==null){
continue;
}
Map<Integer, Object> map1 = new HashMap<>();
map1.put(0,customerMerge.getOneId());
map1.put(1,customerMerge.getMerchantId());
map1.put(2,json);
errList.add(map1);
}
return errList;
}
private String compareCustomer(CustomerMerge merge, CustomerMerge merge1) {
Field[] fields = merge.getClass().getDeclaredFields();
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(merge));
JSONObject jsonObject1 = JSON.parseObject(JSON.toJSONString(merge1));
JSONObject json = new JSONObject();
for (Field field : fields) {
if (Objects.equals(field.getName(), "id")||Objects.equals(field.getName(), "createdTime")||Objects.equals(field.getName(), "updatedTime")) {
continue;
}
Object o = jsonObject.get(field.getName());
Object o1 = jsonObject1.get(field.getName());
if (Objects.equals(o, o1)) {
continue;
}
json.put(field.getName(), "不相等");
}
return json.isEmpty() ? null : json.toString();
}
public void saveErrorUrl(List<Map<Integer,Object>> errList){
if (errList==null||errList.isEmpty()){
return;
}
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
EasyExcel.write(outputStream).sheet("不一致数据").doWrite(errList);
FileUploadServerData fileUploadServerData = FileUploadUtil.uploadToFileSystem("不一致数据列表"+System.currentTimeMillis(),outputStream.toByteArray(), "xlsx");
String url = fileUploadServerData.getUrl();
log.info("数据比对不一致数据列表url================== "+url);
} catch (Exception e) {
log.error("数据比对异常:{}",e);
}
}
}