CustomerMergeDataCompareService.java 5.59 KB
package cn.com.glsx.entmicro.service;

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.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @Author yelq
 * @Version 1.0
 * @Description: scrm_customer_merge 表数据比对
 * @Date 2022/5/15 14:13
 */
@Slf4j
@Service
public class CustomerMergeDataCompareService {
    @Autowired
    private CustomerMergeMapper customerMergeMapper;

    @Async
    public void dataCompare(String tableName){
        try {
            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,tableName);
                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,tableName);
                    all.addAll(list2);
                    if (!all.isEmpty()&&all.size()>=m){
                        saveErrorUrl(all);
                        all.clear();
                    }
                }
            }
        } catch (Exception e) {
            log.info("数据比对程序异常:{}",e);
        }
    }

    public List<Map<Integer,Object>> deelData(List<CustomerMerge> list,String tableName){
        List<Map<Integer,Object>> errList = new ArrayList<>();
        for (CustomerMerge customerMerge : list) {
            List<CustomerMerge> mergeList = customerMergeMapper.selectCustomerMergeByOneIdAndMerchantId(customerMerge.getOneId(),
                    customerMerge.getMerchantId(), tableName);
            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);
        }
    }

}