package com.zfire.jiasm.syncdata.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zfire.jiasm.syncdata.base.BaseService;
import com.zfire.jiasm.syncdata.constant.DictTypeEnum;
import com.zfire.jiasm.syncdata.constant.ServiceCategoryEnum;
import com.zfire.jiasm.syncdata.plus.entity.*;
import com.zfire.jiasm.syncdata.plus.service.*;
import com.zfire.jiasm.syncdata.service.GLPPLeiBieService;
import com.zfire.jiasm.syncdata.service.PublicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GLPPLeiBieServiceImpl implements GLPPLeiBieService {
    @Resource
    ItfTblSplbDaleiDownloadService itfTblSplbDaleiDownloadService;
    @Resource
    ItfTblSplbXiaoleiDownloadService itfTblSplbXiaoleiDownloadService;
    @Resource
    ItfTblSplbXilieDownloadService itfTblSplbXilieDownloadService;
    @Resource
    ItfTblSplbJixinDownloadService itfTblSplbJixinDownloadService;
    @Resource
    SysDictService sysDictService;
    @Resource
    SysDictRefService sysDictRefService;
    @Resource
    ProductService productService;
    @Resource
    BaseService baseService;
    @Resource
    private PublicService publicService;
    @Resource
    ProductSeriesService productSeriesService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void extractedDL(JSONArray jsonArray) {
        List<ItfTblSplbDaleiDownload> addRec = new ArrayList<>();
        List<ItfTblSplbDaleiDownload> updateRec = new ArrayList<>();

        // 提取品牌信息
        Brand brandRec = baseService.getBrand();
        if (brandRec == null){
            log.info("格力品牌信息不存在【brand】");
            return;
        }

        String synTaskNo = publicService.getUUID();

        // 数据同步落到同步信息表
        extractedDaleiAddOrUpdate(addRec, updateRec,jsonArray, synTaskNo);

        // 数据同步到本地字典表
        extractedLocalDaleiAddOrUpdate(addRec, updateRec);

        // 数据同步到本地字典对应关系
        extractedLoaclRefDaleiAddOrUpdate(addRec, updateRec, brandRec);
        addRec.clear();
        updateRec.clear();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void extractedXL(JSONArray jsonArray) {
        List<ItfTblSplbXiaoleiDownload> addRec = new ArrayList<>();
        List<ItfTblSplbXiaoleiDownload> updateRec = new ArrayList<>();

        // 提取品牌信息
        Brand brandRec = baseService.getBrand();
        if (brandRec == null){
            log.info("格力品牌信息不存在【brand】");
            return;
        }

        String synTaskNo = publicService.getUUID();

        // 数据同步落到同步信息表
        extractedGLXLAddOrUpdate(addRec, updateRec, jsonArray, synTaskNo);

        // 数据同步到本地字典表
        extractedLocalXLAddOrUpdate(addRec, updateRec);

        // 数据同步到本地字典对应关系
        extractedLocalRefXLAddOrUpdate(addRec, updateRec, brandRec);
        addRec.clear();
        updateRec.clear();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void extractedXiLie(JSONArray jsonArray){
        List<ItfTblSplbXilieDownload> addRec = new ArrayList<>();
        List<ItfTblSplbXilieDownload> updateRec = new ArrayList<>();

        // 提取品牌信息
        Brand brandRec = baseService.getBrand();
        if (brandRec == null){
            log.info("格力品牌信息不存在【brand】");
            return;
        }

        String synTaskNo = publicService.getUUID();

        // 数据同步落到同步信息表
        extractedPPXLAddOrUpdate(addRec, updateRec, jsonArray, synTaskNo);

        // 数据同步到本地字典表
        extractedLocalPPXLAddOrUpdate(addRec, updateRec);

        // 数据同步到本地字典对应关系
        extractedLoaclRefPPXLAddOrUpdate(addRec, updateRec, brandRec);

        // 同步到本地系列表
        extractedProductSeries(addRec, updateRec, brandRec);


        addRec.clear();
        updateRec.clear();
    }

    private void extractedProductSeries(List<ItfTblSplbXilieDownload> addRec, List<ItfTblSplbXilieDownload> updateRec, Brand brandRec) {
        List<String> xlid = new ArrayList<>();
        List<ProductSeries> addps = new ArrayList<>();
        List<ProductSeries> upps = new ArrayList<>();
        List<SysDict> oldSysDictRec = new ArrayList<>();
        Map<String, List<SysDict>> oldSysDictRecMap = new HashMap<>();
        List<SysDict> oldSysDictXRec = new ArrayList<>();
        Map<String, List<SysDict>> oldSysDictRecXMap = new HashMap<>();
        Map<String, List<ProductSeries>> oldpsMap = new HashMap<>();

        // 提取大类字典数据
        oldSysDictRec =  sysDictService.lambdaQuery()
                .eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode())
                .list();
        if (CollectionUtils.isNotEmpty(oldSysDictRec)){
            oldSysDictRecMap = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictCode));
            oldSysDictRec.clear();
        }

        // 提取小类字典数据  sysDictService
        oldSysDictXRec =  sysDictService.lambdaQuery()
                .eq(SysDict::getDictType,DictTypeEnum.DICT_TYPE_SMALL_TYPE.getCode()).list();
        if (CollectionUtils.isNotEmpty(oldSysDictXRec)){
            oldSysDictRecXMap = oldSysDictXRec.stream().collect(Collectors.groupingBy(SysDict::getDictCode));
            oldSysDictXRec.clear();
        }

        // 提取现有数据防重使用
        List<ProductSeries> oldps =  productSeriesService.lambdaQuery().list();
        if (CollectionUtils.isNotEmpty(oldps)){
            oldps.stream().forEach(en->{
                xlid.add(en.getXlId());
            });
            oldpsMap = oldps.stream().collect(Collectors.groupingBy(ProductSeries::getXlId));
            oldps.clear();
        }

        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblSplbXilieDownload item : addRec){
                if (xlid.contains(item.getId())){
                    upProductSeries(brandRec, upps, oldSysDictRecMap, oldSysDictRecXMap, oldpsMap, item);
                } else {
                    getProductSeries(brandRec, addps, oldSysDictRecMap, oldSysDictRecXMap, item);
                    xlid.add(item.getId());
                }
            }
        }
        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblSplbXilieDownload item : updateRec){
                if (xlid.contains(item.getId())){
                    upProductSeries(brandRec, upps, oldSysDictRecMap, oldSysDictRecXMap, oldpsMap, item);
                } else {
                    getProductSeries(brandRec, addps, oldSysDictRecMap, oldSysDictRecXMap, item);
                    xlid.add(item.getId());
                }
            }
        }

        // 新增的插入到表
        if (addps.size()>0){
            productSeriesService.saveBatch(addps);
            log.info("本地系列信息表新增插入了{}条数据",addps.size());
            addps.clear();
        }
        // 更新
        if (upps.size()>0){
            productSeriesService.updateBatchById(upps);
            log.info("本地系列信息表更新修改了{}条数据", upps.size());
            upps.clear();
        }
        oldSysDictRecMap.clear();
        oldSysDictRecXMap.clear();
        oldpsMap.clear();
        xlid.clear();
    }

    private void upProductSeries(Brand brandRec, List<ProductSeries> upps, Map<String, List<SysDict>> oldSysDictRecMap,
                                 Map<String, List<SysDict>> oldSysDictRecXMap, Map<String, List<ProductSeries>> oldpsMap,
                                 ItfTblSplbXilieDownload item) {
        ProductSeries add = new ProductSeries();
        add.setSeriesNumber(item.getXino());// 编号
        add.setBrandId(brandRec.getBrandId());// 品牌id
        add.setBrandName(brandRec.getBrand());// 品牌名称
        add.setMainId(String.valueOf(item.getSpid()));// 大类id
        add.setMainName(oldSysDictRecMap.get(String.valueOf(item.getSpid())).get(0).getDictValue());// 大类名称
        add.setSmallId(String.valueOf(item.getXlid()));// 小类id
        add.setSmallName(oldSysDictRecXMap.get(String.valueOf(item.getXlid())).get(0).getDictValue());// 小类名称
        add.setSeriesName(item.getXimc());// 系列名
        add.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
        if (item.getFlag().equals(DictTypeEnum.DICT_TYPE_FLAG_ON.getCode())){
            add.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
        }
        add.setCreateTime(new Date());
        if (item.getCreatedDate() !=null){
            add.setCreateTime(item.getCreatedDate());
        }
        add.setCreateBy("system");
        if (StringUtils.isNotEmpty(item.getCreatedBy())){
            add.setCreateBy(item.getCreatedBy());
        }
        add.setUpdateTime(new Date());
        if (item.getLastModifiedDate() !=null){
            add.setUpdateTime(item.getLastModifiedDate());
        }
        add.setUpdateBy("system");
        if (StringUtils.isNotEmpty(item.getLastModifiedBy())){
            add.setUpdateBy(item.getLastModifiedBy());
        }
        add.setXlId(item.getId());// 三方主键
        add.setProductSeriesId(oldpsMap.get(item.getId()).get(0).getProductSeriesId());
        upps.add(add);
    }

    private void getProductSeries(Brand brandRec, List<ProductSeries> addps, Map<String, List<SysDict>> oldSysDictRecMap, Map<String, List<SysDict>> oldSysDictRecXMap, ItfTblSplbXilieDownload item) {
        ProductSeries add = new ProductSeries();
        add.setProductSeriesId(String.valueOf(item.getXiid()));
        add.setSeriesNumber(item.getXino());// 编号
        add.setBrandId(brandRec.getBrandId());// 品牌id
        add.setBrandName(brandRec.getBrand());// 品牌名称
        add.setMainId(String.valueOf(item.getSpid()));// 大类id
        if (oldSysDictRecMap.containsKey(String.valueOf(item.getSpid()))){
            add.setMainName(oldSysDictRecMap.get(String.valueOf(item.getSpid())).get(0).getDictValue());// 大类名称
        }

        add.setSmallId(String.valueOf(item.getXlid()));// 小类id
        if (oldSysDictRecXMap.containsKey(String.valueOf(item.getXlid()))){
            add.setSmallName(oldSysDictRecXMap.get(String.valueOf(item.getXlid())).get(0).getDictValue());// 小类名称
        }

        add.setSeriesName(item.getXimc());// 系列名
        add.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
        if (item.getFlag().equals(DictTypeEnum.DICT_TYPE_FLAG_ON.getCode())){
            add.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
        }

        add.setCreateTime(new Date());
        if (item.getCreatedDate() !=null){
            add.setCreateTime(item.getCreatedDate());
        }
        add.setCreateBy("system");
        if (StringUtils.isNotEmpty(item.getCreatedBy())){
            add.setCreateBy(item.getCreatedBy());
        }
        add.setUpdateTime(new Date());
        if (item.getLastModifiedDate() !=null){
            add.setUpdateTime(item.getLastModifiedDate());
        }
        add.setUpdateBy("system");
        if (StringUtils.isNotEmpty(item.getLastModifiedBy())){
            add.setUpdateBy(item.getLastModifiedBy());
        }
        add.setXlId(item.getId());// 三方主键
        addps.add(add);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void extractedJX(JSONArray jsonArray){
        List<ItfTblSplbJixinDownload> addAllRec = new ArrayList<>();
        List<ItfTblSplbJixinDownload> addRec = new ArrayList<>();
        List<ItfTblSplbJixinDownload> updateRec = new ArrayList<>();

        // 提取品牌信息
        Brand brandRec = baseService.getBrand();
        if (brandRec == null){
            log.info("格力品牌信息不存在【brand】");
            return;
        }

        String synTaskNo = publicService.getUUID();

        // 数据同步落到同步信息表
        extractedJXAddOrUpdate(addAllRec, addRec, updateRec, jsonArray, synTaskNo);

        // 数据同步到本地表
        extractedLocalJXAddOrUpdate(addAllRec, updateRec, brandRec);
        addAllRec.clear();
        updateRec.clear();
    }

    private void extractedLocalJXAddOrUpdate(List<ItfTblSplbJixinDownload> addRec, List<ItfTblSplbJixinDownload> updateRec, Brand brandRec) {
        List<Product> addProduct = new ArrayList<>();
        List<Product> updateProduct = new ArrayList<>();
        List<Product> oldSysProduct = new ArrayList<>();
        Map<String, List<Product>> oldProductRecMap = new HashMap<>();
        List<String> dictcodes = new ArrayList<>();

        // 提取现有数据
        oldSysProduct =  productService.lambdaQuery()
                .eq(Product::getProductBrandId, brandRec.getBrandId()).list();
        if (CollectionUtils.isNotEmpty(oldSysProduct)){
            oldProductRecMap = oldSysProduct.stream().collect(Collectors.groupingBy(Product::getProductId));
        }

        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblSplbJixinDownload item : addRec){
                if (oldProductRecMap.containsKey(item.getId())){
                    upGetProduct(updateProduct, oldProductRecMap, item);
                } else {
                    if (!dictcodes.contains(item.getId())){
                        addGetProduct(brandRec, addProduct, item);
                        dictcodes.add(item.getId());
                    }
                }
            }
        }
        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblSplbJixinDownload item : updateRec){
                if (oldProductRecMap.containsKey(item.getId())){
                    upGetProduct(updateProduct, oldProductRecMap, item);
                } else {
                    if (!dictcodes.contains(item.getId())){
                        addGetProduct(brandRec, addProduct, item);
                        dictcodes.add(item.getId());
                    }
                }
            }
        }

        // 新增的插入到表
        if (addProduct.size()>0){
            productService.saveBatch(addProduct);
            log.info("本地机型信息表新增插入了{}条数据",addProduct.size());
            addProduct.clear();
        }
        // 更新
        if (updateProduct.size()>0){
            productService.updateBatchById(updateProduct);
            log.info("本地机型信息表更新修改了{}条数据", updateProduct.size());
            updateProduct.clear();
        }
        dictcodes.clear();
    }

    private void upGetProduct(List<Product> updateProduct, Map<String, List<Product>> oldProductRecMap, ItfTblSplbJixinDownload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        Product v = oldProductRecMap.get(item.getId()).get(0);
        if (!v.getJxid().equals(item.getJxid())){
            v.setJxid(item.getJxid());
            flag = Boolean.TRUE;
        }
        if (!v.getName().equals(item.getJxmc())){
            v.setName(item.getJxmc());
            flag = Boolean.TRUE;
        }
        if (!v.getMainId().equals(String.valueOf(item.getSpid()))){
            v.setMainId(String.valueOf(item.getSpid()));
            flag = Boolean.TRUE;
        }
        if (!v.getMainName().equals(item.getSpmc())){
            v.setMainName(item.getSpmc());
            flag = Boolean.TRUE;
        }
        if (!v.getSmallId().equals(String.valueOf(item.getXlid()))){
            v.setSmallId(String.valueOf(item.getXlid()));
            flag = Boolean.TRUE;
        }
        if (!v.getSmallName().equals(item.getXlmc())){
            v.setSmallName(item.getXlmc());
            flag = Boolean.TRUE;
        }
        if (!v.getSeriesId().equals(String.valueOf(item.getXiid()))){
            v.setSeriesId(String.valueOf(item.getXiid()));
            flag = Boolean.TRUE;
        }
        if (!v.getSeriesName().equals(item.getXiimc())){
            v.setSeriesName(item.getXiimc());
            flag = Boolean.TRUE;
        }
        if (!v.getMaterialNumber().equals(item.getJxno())){
            v.setMaterialNumber(item.getJxno());
            flag = Boolean.TRUE;
        }
        if (item.getJsdj() !=null){
            if (v.getSettlePrice().compareTo(BigDecimal.valueOf(Long.valueOf(item.getJsdj()))) !=0){
                v.setSettlePrice(BigDecimal.valueOf(Long.valueOf(item.getJsdj())));
                flag = Boolean.TRUE;
            }
        } else {
            if (v.getSettlePrice().compareTo(BigDecimal.ZERO) !=0){
                v.setSettlePrice(BigDecimal.ZERO);
                flag = Boolean.TRUE;
            }
        }
        if (v.getInstallSettleType() !=null || item.getAzjsfl() !=null){
            if (v.getInstallSettleType() !=null && item.getAzjsfl() !=null){
                if (!v.getInstallSettleType().equals(item.getAzjsfl())){
                    v.setInstallSettleType(item.getAzjsfl());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getAzjsfl() !=null){
                    v.setInstallSettleType(item.getAzjsfl());
                } else {
                    v.setInstallSettleType(null);
                }
            }
        }
        if (v.getRepairSettleType() !=null || item.getWxjsfl() !=null){
            if (v.getRepairSettleType() !=null && item.getWxjsfl() !=null){
                if (!v.getRepairSettleType().equals(item.getWxjsfl())){
                    v.setRepairSettleType(item.getWxjsfl());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getWxjsfl() !=null){
                    v.setRepairSettleType(item.getWxjsfl());
                } else {
                    v.setRepairSettleType(null);
                }
            }
        }
        if (v.getRepairStandardNumber() !=null || item.getWxbzdm() !=null){
            if (v.getRepairStandardNumber() !=null && item.getWxbzdm() !=null){
                if (!v.getRepairStandardNumber().equals(item.getWxbzdm())){
                    v.setRepairStandardNumber(item.getWxbzdm());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getWxbzdm() !=null){
                    v.setRepairStandardNumber(item.getWxbzdm());
                } else {
                    v.setRepairStandardNumber(null);
                }
            }
        }
        if (v.getRepairStandardRemark() !=null || item.getWxbzms() !=null){
            if (v.getRepairStandardRemark() !=null && item.getWxbzms() !=null){
                if (!v.getRepairStandardRemark().equals(item.getWxbzms())){
                    v.setRepairStandardRemark(item.getWxbzms());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getWxbzms() !=null){
                    v.setRepairStandardRemark(item.getWxbzms());
                } else {
                    v.setRepairStandardRemark(null);
                }
            }
        }
        if (v.getRefrigerantType() !=null || item.getLmlx() !=null){
            if (v.getRefrigerantType() !=null && item.getLmlx() !=null){
                if (!v.getRefrigerantType().equals(item.getLmlx())){
                    v.setRefrigerantType(item.getLmlx());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getLmlx() !=null){
                    v.setRefrigerantType(item.getLmlx());
                } else {
                    v.setRefrigerantType(null);
                }
            }
        }
        if (v.getProductColor() !=null || item.getCpys() !=null){
            if (v.getProductColor() !=null && item.getCpys() !=null){
                if (!v.getProductColor().equals(item.getCpys())){
                    v.setProductColor(item.getCpys());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getCpys() !=null){
                    v.setProductColor(item.getCpys());
                } else {
                    v.setProductColor(null);
                }
            }
        }
        if (v.getProductLevel() !=null || item.getCpdc() !=null){
            if (v.getProductLevel() !=null && item.getCpdc() !=null){
                if (!v.getProductLevel().equals(item.getCpdc())){
                    v.setProductLevel(item.getCpdc());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getCpdc() !=null){
                    v.setProductLevel(item.getCpdc());
                } else {
                    v.setProductLevel(null);
                }
            }
        }
        if (v.getIsBp() !=null || item.getSfbp() !=null){
            if (v.getIsBp() !=null && item.getSfbp() !=null){
                if (!v.getIsBp().equals(item.getSfbp())){
                    v.setIsBp(item.getSfbp());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getSfbp() !=null){
                    v.setIsBp(item.getSfbp());
                } else {
                    v.setIsBp(null);
                }
            }
        }
        if (v.getCoolingCapacity() !=null || item.getZll() !=null){
            if (v.getCoolingCapacity() !=null && item.getZll() !=null){
                if (!v.getCoolingCapacity().equals(item.getZll())){
                    v.setCoolingCapacity(item.getZll());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getZll() !=null){
                    v.setCoolingCapacity(item.getZll());
                } else {
                    v.setCoolingCapacity(null);
                }
            }
        }
        if (v.getEfficiencyLevel() !=null || item.getNxdj() !=null){
            if (v.getEfficiencyLevel() !=null && item.getNxdj() !=null){
                if (!v.getEfficiencyLevel().equals(item.getNxdj())){
                    v.setEfficiencyLevel(item.getNxdj());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getNxdj() !=null){
                    v.setEfficiencyLevel(item.getNxdj());
                } else {
                    v.setEfficiencyLevel(null);
                }
            }
        }
        if (v.getProductType() !=null || item.getCplx() !=null){
            if (v.getProductType() !=null && item.getCplx() !=null){
                if (!v.getProductType().equals(item.getCplx())){
                    v.setProductType(item.getCplx());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getCplx() !=null){
                    v.setProductType(item.getCplx());
                } else {
                    v.setProductType(null);
                }
            }
        }
        if (v.getCoolingCapacityRange() !=null || item.getZllfw() !=null){
            if (v.getCoolingCapacityRange() !=null && item.getZllfw() !=null){
                if (!v.getCoolingCapacityRange().equals(item.getZllfw())){
                    v.setCoolingCapacityRange(item.getZllfw());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getZllfw() !=null){
                    v.setCoolingCapacityRange(item.getZllfw());
                } else {
                    v.setCoolingCapacityRange(null);
                }
            }
        }
        if (v.getCrewAbility() !=null || item.getJzll() !=null){
            if (v.getCrewAbility() !=null && item.getJzll() !=null){
                if (!v.getCrewAbility().equals(item.getJzll())){
                    v.setCrewAbility(item.getJzll());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getJzll() !=null){
                    v.setCrewAbility(item.getJzll());
                } else {
                    v.setCrewAbility(null);
                }
            }
        }
        if (v.getOutCode() !=null || item.getWjtm() !=null){
            if (v.getOutCode() !=null && item.getWjtm() !=null){
                if (!v.getOutCode().equals(item.getWjtm())){
                    v.setOutCode(item.getWjtm());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getWjtm() !=null){
                    v.setOutCode(item.getWjtm());
                } else {
                    v.setOutCode(null);
                }
            }
        }
        if (v.getOutMaterialNumber() !=null || item.getWjwldm() !=null){
            if (v.getOutMaterialNumber() !=null && item.getWjwldm() !=null){
                if (!v.getOutMaterialNumber().equals(item.getWjwldm())){
                    v.setOutMaterialNumber(item.getWjwldm());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getWjwldm() !=null){
                    v.setOutMaterialNumber(item.getWjwldm());
                } else {
                    v.setOutMaterialNumber(null);
                }
            }
        }
        if (v.getOutProductModel() !=null || item.getWjxh() !=null){
            if (v.getOutProductModel() !=null && item.getWjxh() !=null){
                if (!v.getOutProductModel().equals(item.getWjxh())){
                    v.setOutProductModel(item.getWjxh());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (item.getWjxh() !=null){
                    v.setOutProductModel(item.getWjxh());
                } else {
                    v.setOutProductModel(null);
                }
            }
        }
        if (!v.getSortLevel().equals(item.getPxdj())){
            v.setSortLevel(item.getPxdj());
            flag = Boolean.TRUE;
        }
        if (flag){
            v.setUpdateBy("system");
            v.setUpdateTime(new Date());
            // 操作人
            if (item.getLastModifiedBy() != null){
                v.setUpdateBy(item.getLastModifiedBy());
            }
            // 修改时间
            if (item.getLastModifiedDate() != null){
                v.setUpdateTime(item.getLastModifiedDate());
            }
            updateProduct.add(v);
        }
    }

    private void addGetProduct(Brand brandRec, List<Product> addProduct, ItfTblSplbJixinDownload item) {
        Product v = new Product();
        v.setProductId(item.getId());
        v.setProductBrandId(brandRec.getBrandId());
        v.setProductBrandName(brandRec.getBrand());
        v.setName(item.getJxmc());
        // 对应三方工单产品表的jxno
        v.setJxid(item.getJxid());
        v.setMainId(String.valueOf(item.getSpid()));
        v.setMainName(item.getSpmc());
        v.setSmallId(String.valueOf(item.getXlid()));
        v.setSmallName(item.getXlmc());
        v.setSeriesId(String.valueOf(item.getXiid()));
        v.setSeriesName(item.getXiimc());
        v.setMaterialNumber(item.getJxno());
        v.setSettlePrice(BigDecimal.ZERO);
        if (StringUtils.isNotEmpty(item.getJsdj())){
            v.setSettlePrice(BigDecimal.valueOf(Long.valueOf(item.getJsdj())));
        }
        v.setInstallSettleType(item.getAzjsfl());
        v.setRepairSettleType(item.getWxjsfl());
        v.setRepairStandardNumber(item.getWxbzdm());
        v.setRepairStandardRemark(item.getWxbzms());
        v.setRefrigerantType(item.getLmlx());
        v.setProductColor(item.getCpys());
        v.setProductLevel(item.getCpdc());
        v.setIsBp(item.getSfbp());
        v.setCoolingCapacity(item.getZll());
        v.setEfficiencyLevel(item.getNxdj());
        v.setProductType(item.getCplx());
        v.setCoolingCapacityRange(item.getZllfw());
        v.setCrewAbility(item.getJzll());
        v.setOutCode(item.getWjtm());
        v.setOutMaterialNumber(item.getWjwldm());
        v.setOutProductModel(item.getWjxh());
        v.setSortLevel(item.getPxdj());
        v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
        if (item.getFlag().equals("0")){
            v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
        }

        v.setCreateBy("system");
        v.setCreateTime(new Date());
        v.setUpdateBy("system");
        v.setUpdateTime(new Date());

        if (item.getCreatedBy() !=null){
            v.setCreateBy(item.getCreatedBy());
        }
        if (item.getCreatedDate() == null){
            v.setCreateTime(item.getCreatedDate());
        }
        if (item.getLastModifiedBy() !=null){
            v.setUpdateBy(item.getLastModifiedBy());
        }
        if (item.getLastModifiedDate() == null){
            v.setUpdateTime(item.getLastModifiedDate());
        }

        addProduct.add(v);
        if (addProduct.size()>=1000){
            productService.saveBatch(addProduct);
            log.info("本地机型信息表新增插入了{}条数据", addProduct.size());
            addProduct.clear();
        }
    }

    private void extractedJXAddOrUpdate(List<ItfTblSplbJixinDownload> addAllRec, List<ItfTblSplbJixinDownload> addRec, List<ItfTblSplbJixinDownload> updateRec, JSONArray jsonArray, String synTaskNo) {
        Boolean flag;
        List<ItfTblSplbJixinDownload> oldRec = new ArrayList<>();
        List<String> oldRecId = new ArrayList<>();
        Map<String, List<ItfTblSplbJixinDownload>> oldRecMap = new HashMap<>();

        // 提取现有的数据信息
        oldRec =  itfTblSplbJixinDownloadService.lambdaQuery().list();
        if (CollectionUtils.isNotEmpty(oldRec)){
            oldRecId = oldRec.stream().map(ItfTblSplbJixinDownload::getId).collect(Collectors.toList());
            oldRecMap = oldRec.stream().collect(Collectors.groupingBy(ItfTblSplbJixinDownload::getId));
        }

        for (int i = 0; i < jsonArray.size(); i++) {
            flag = Boolean.FALSE;
            JSONObject aData = jsonArray.getJSONObject(i);
            ItfTblSplbJixinDownload item = JSON.toJavaObject(aData,ItfTblSplbJixinDownload.class);
            // 新增或修改数据
            // 修改
            if (oldRecId.contains(item.getId())){
                ItfTblSplbJixinDownload old = oldRecMap.get(item.getId()).get(0);
                if (!old.getSpid().equals(item.getSpid())){
                    old.setSpid(item.getSpid());
                    flag = Boolean.TRUE;
                }
                if (!old.getSpmc().equals(item.getSpmc())){
                    old.setSpmc(item.getSpmc());
                    flag = Boolean.TRUE;
                }
                if (!old.getXlid().equals(item.getXlid())){
                    old.setXlid(item.getXlid());
                    flag = Boolean.TRUE;
                }
                if (!old.getXlmc().equals(item.getXlmc())){
                    old.setXlmc(item.getXlmc());
                    flag = Boolean.TRUE;
                }
                if (!old.getXiino().equals(item.getXiino())){
                    old.setXiino(item.getXiino());
                    flag = Boolean.TRUE;
                }
                if (!old.getXiimc().equals(item.getXiimc())){
                    old.setXiimc(item.getXiimc());
                    flag = Boolean.TRUE;
                }
                if (old.getZjwldm() !=null || item.getZjwldm() !=null){
                    if (old.getZjwldm() !=null && item.getZjwldm() !=null){
                        if (!old.getZjwldm().equals(item.getZjwldm())){
                            old.setZjwldm(item.getZjwldm());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getZjwldm() !=null){
                            old.setZjwldm(item.getZjwldm());
                        } else {
                            old.setZjwldm(null);
                        }
                    }
                }
                if (!old.getZjwlmc().equals(item.getZjwlmc())){
                    old.setZjwlmc(item.getZjwlmc());
                    flag = Boolean.TRUE;
                }
                if (!old.getJxid().equals(item.getJxid())){
                    old.setJxid(item.getJxid());
                    flag = Boolean.TRUE;
                }
                if (!old.getJxno().equals(item.getJxno())){
                    old.setJxno(item.getJxno());
                    flag = Boolean.TRUE;
                }
                if (!old.getJxmc().equals(item.getJxmc())){
                    old.setJxmc(item.getJxmc());
                    flag = Boolean.TRUE;
                }
                if (old.getJsdj() !=null || item.getJsdj() !=null){
                    if (old.getJsdj() !=null && item.getJsdj() !=null){
                        if (!old.getJsdj().equals(item.getJsdj())){
                            old.setJsdj(item.getJsdj());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getJsdj() !=null){
                            old.setJsdj(item.getJsdj());
                        } else {
                            old.setJsdj(null);
                        }
                    }
                }
                if (old.getAzjsfl() !=null || item.getAzjsfl() !=null){
                    if (old.getAzjsfl() !=null && item.getAzjsfl() !=null){
                        if (!old.getAzjsfl().equals(item.getAzjsfl())){
                            old.setAzjsfl(item.getAzjsfl());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getAzjsfl() !=null){
                            old.setAzjsfl(item.getAzjsfl());
                        } else {
                            old.setAzjsfl(null);
                        }
                    }
                }
                if (old.getWxjsfl() !=null || item.getWxjsfl() !=null){
                    if (old.getWxjsfl() !=null && item.getWxjsfl() !=null){
                        if (!old.getWxjsfl().equals(item.getWxjsfl())){
                            old.setWxjsfl(item.getWxjsfl());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getWxjsfl() !=null){
                            old.setWxjsfl(item.getWxjsfl());
                        } else {
                            old.setWxjsfl(null);
                        }
                    }
                }
                if (old.getWxbzdm() !=null || item.getWxbzdm() !=null){
                    if (old.getWxbzdm() !=null && item.getWxbzdm() !=null){
                        if (!old.getWxbzdm().equals(item.getWxbzdm())){
                            old.setWxbzdm(item.getWxbzdm());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getWxbzdm() !=null){
                            old.setWxbzdm(item.getWxbzdm());
                        } else {
                            old.setWxbzdm(null);
                        }
                    }
                }
                if (old.getWxbzms() !=null || item.getWxbzms() !=null){
                    if (old.getWxbzms() !=null && item.getWxbzms() !=null){
                        if (!old.getWxbzms().equals(item.getWxbzms())){
                            old.setWxbzms(item.getWxbzms());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getWxbzms() !=null){
                            old.setWxbzms(item.getWxbzms());
                        } else {
                            old.setWxbzms(null);
                        }
                    }
                }
                if (old.getLmlx() !=null || item.getLmlx() !=null){
                    if (old.getLmlx() !=null && item.getLmlx() !=null){
                        if (!old.getLmlx().equals(item.getLmlx())){
                            old.setLmlx(item.getLmlx());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getLmlx() !=null){
                            old.setLmlx(item.getLmlx());
                        } else {
                            old.setLmlx(null);
                        }
                    }
                }
                if (old.getCpys() !=null || item.getCpys() !=null){
                    if (old.getCpys() !=null && item.getCpys() !=null){
                        if (!old.getCpys().equals(item.getCpys())){
                            old.setCpys(item.getCpys());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getCpys() !=null){
                            old.setCpys(item.getCpys());
                        } else {
                            old.setCpys(null);
                        }
                    }
                }
                if (old.getCpdc() !=null || item.getCpdc() !=null){
                    if (old.getCpdc() !=null && item.getCpdc() !=null){
                        if (!old.getCpdc().equals(item.getCpdc())){
                            old.setCpdc(item.getCpdc());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getCpdc() !=null){
                            old.setCpdc(item.getCpdc());
                        } else {
                            old.setCpdc(null);
                        }
                    }
                }
                if (old.getNwbs() !=null || item.getNwbs() !=null){
                    if (old.getNwbs() !=null && item.getNwbs() !=null){
                        if (!old.getNwbs().equals(item.getNwbs())){
                            old.setNwbs(item.getNwbs());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getNwbs() !=null){
                            old.setNwbs(item.getNwbs());
                        } else {
                            old.setNwbs(null);
                        }
                    }
                }
                if (old.getSfbp() !=null || item.getSfbp() !=null){
                    if (old.getSfbp() !=null && item.getSfbp() !=null){
                        if (!old.getSfbp().equals(item.getSfbp())){
                            old.setSfbp(item.getSfbp());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getSfbp() !=null){
                            old.setSfbp(item.getSfbp());
                        } else {
                            old.setSfbp(null);
                        }
                    }
                }
                if (old.getZll() !=null || item.getZll() !=null){
                    if (old.getZll() !=null && item.getZll() !=null){
                        if (!old.getZll().equals(item.getZll())){
                            old.setZll(item.getZll());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getZll() !=null){
                            old.setZll(item.getZll());
                        } else {
                            old.setZll(null);
                        }
                    }
                }
                if (old.getNxdj() !=null || item.getNxdj() !=null){
                    if (old.getNxdj() !=null && item.getNxdj() !=null){
                        if (!old.getNxdj().equals(item.getNxdj())){
                            old.setNxdj(item.getNxdj());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getNxdj() !=null){
                            old.setNxdj(item.getNxdj());
                        } else {
                            old.setNxdj(null);
                        }
                    }
                }
                if (old.getCplx() !=null || item.getCplx() !=null){
                    if (old.getCplx() !=null && item.getCplx() !=null){
                        if (!old.getCplx().equals(item.getCplx())){
                            old.setCplx(item.getCplx());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getCplx() !=null){
                            old.setCplx(item.getCplx());
                        } else {
                            old.setCplx(null);
                        }
                    }
                }
                if (old.getZllfw() !=null || item.getZllfw() !=null){
                    if (old.getZllfw() !=null && item.getZllfw() !=null){
                        if (!old.getZllfw().equals(item.getZllfw())){
                            old.setZllfw(item.getZllfw());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getZllfw() !=null){
                            old.setZllfw(item.getZllfw());
                        } else {
                            old.setZllfw(null);
                        }
                    }
                }
                if (old.getNwptfs() !=null || item.getNwptfs() !=null){
                    if (old.getNwptfs() !=null && item.getNwptfs() !=null){
                        if (!old.getNwptfs().equals(item.getNwptfs())){
                            old.setNwptfs(item.getNwptfs());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getNwptfs() !=null){
                            old.setNwptfs(item.getNwptfs());
                        } else {
                            old.setNwptfs(null);
                        }
                    }
                }
                if (old.getJzll() !=null || item.getJzll() !=null){
                    if (old.getJzll() !=null && item.getJzll() !=null){
                        if (!old.getJzll().equals(item.getJzll())){
                            old.setJzll(item.getJzll());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getJzll() !=null){
                            old.setJzll(item.getJzll());
                        } else {
                            old.setJzll(null);
                        }
                    }
                }
                if (old.getWjtm() !=null || item.getWjtm() !=null){
                    if (old.getWjtm() !=null && item.getWjtm() !=null){
                        if (!old.getWjtm().equals(item.getWjtm())){
                            old.setWjtm(item.getWjtm());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getWjtm() !=null){
                            old.setWjtm(item.getWjtm());
                        } else {
                            old.setWjtm(null);
                        }
                    }
                }
                if (old.getWjwldm() !=null || item.getWjwldm() !=null){
                    if (old.getWjwldm() !=null && item.getWjwldm() !=null){
                        if (!old.getWjwldm().equals(item.getWjwldm())){
                            old.setWjwldm(item.getWjwldm());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getWjwldm() !=null){
                            old.setWjwldm(item.getWjwldm());
                        } else {
                            old.setWjwldm(null);
                        }
                    }
                }
                if (old.getWjxh() !=null || item.getWjxh() !=null){
                    if (old.getWjxh() !=null && item.getWjxh() !=null){
                        if (!old.getWjxh().equals(item.getWjxh())){
                            old.setWjxh(item.getWjxh());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getWjxh() !=null){
                            old.setWjxh(item.getWjxh());
                        } else {
                            old.setWjxh(null);
                        }
                    }
                }
                if (!old.getPxdj().equals(item.getPxdj())){
                    old.setPxdj(item.getPxdj());
                    flag = Boolean.TRUE;
                }
                if (!old.getFlag().equals(item.getFlag())){
                    old.setFlag(item.getFlag());
                    flag = Boolean.TRUE;
                }
                if (!old.getXiid().equals(item.getXiid())){
                    old.setXiid(item.getXiid());
                    flag = Boolean.TRUE;
                }
                if (flag){
                    old.setLastModifiedBy("system");
                    old.setLastModifiedDate(new Date());
                    // 操作人
                    if (item.getLastModifiedBy() != null){
                        old.setLastModifiedBy(item.getLastModifiedBy());
                    }
                    // 修改时间
                    if (item.getLastModifiedDate() != null){
                        old.setLastModifiedDate(item.getLastModifiedDate());
                    }
                    updateRec.add(old);
                }
            } else {
                // 新增
                item.setSynTaskNo(synTaskNo);
                item.setSynReqTime(new Date());
                item.setSynStatus(1);
                item.setSynTime(new Date());
                item.setSynTimes(0);
                if (item.getCreatedBy() ==null){
                    item.setCreatedBy("system");
                }
                if (item.getCreatedDate() == null){
                    item.setCreatedDate(new Date());
                }
                oldRecId.add(item.getId());
                addRec.add(item);

                if (addRec.size()>=500){
                    itfTblSplbJixinDownloadService.saveBatch(addRec);
                    log.info("格力品牌机型基础资料信息同步新增插入了{}条数据", addRec.size());
                    addAllRec.addAll(addRec);
                    addRec.clear();
                }
            }
        }

        // 新增的插入到表
        if (addRec.size()>0){
            itfTblSplbJixinDownloadService.saveBatch(addRec);
            log.info("格力品牌机型基础资料信息同步新增插入了{}条数据", addRec.size());
            addAllRec.addAll(addRec);
            addRec.clear();
        }

        // 更新
        if (updateRec.size()>0){
            itfTblSplbJixinDownloadService.updateBatchById(updateRec);
            log.info("格力品牌机型基础资料信息同步更新修改了{}条数据", updateRec.size());
        }
    }

    private void extractedLoaclRefPPXLAddOrUpdate(List<ItfTblSplbXilieDownload> addRec, List<ItfTblSplbXilieDownload> updateRec, Brand brandRec) {
        List<SysDictRef> addSysDictRef = new ArrayList<>();
        List<SysDictRef> updateSysDictRef = new ArrayList<>();
        List<SysDictRef> oldSysDictRefRec = new ArrayList<>();
        Map<String, List<SysDictRef>> oldSysDictRefRecMap = new HashMap<>();
        List<String> xiids = new ArrayList<>();

        // 提取字典对应关系
        oldSysDictRefRec =  sysDictRefService.lambdaQuery()
                .eq(SysDictRef::getBrandId, brandRec.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_SERIES.getCode()).list();
        if (CollectionUtils.isNotEmpty(oldSysDictRefRec)){
            oldSysDictRefRecMap = oldSysDictRefRec.stream().collect(Collectors.groupingBy(SysDictRef::getDictCode));
        }

        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblSplbXilieDownload item : addRec){
                if (oldSysDictRefRecMap.containsKey(String.valueOf(item.getXiid()))){
                    upSysDictRef(updateSysDictRef, oldSysDictRefRecMap, item);
                } else {
                    if (!xiids.contains(String.valueOf(item.getXiid()))){
                        addSysDictRef(brandRec, addSysDictRef, item);
                        xiids.add(String.valueOf(item.getXiid()));
                    }
                }
            }
        }

        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblSplbXilieDownload item : updateRec){
                if (oldSysDictRefRecMap.containsKey(String.valueOf(item.getXiid()))){
                    upSysDictRef(updateSysDictRef, oldSysDictRefRecMap, item);
                } else {
                    if (!xiids.contains(String.valueOf(item.getXiid()))){
                        addSysDictRef(brandRec, addSysDictRef, item);
                        xiids.add(String.valueOf(item.getXiid()));
                    }
                }
            }
        }

        // 新增的插入到表
        if (addSysDictRef.size()>0){
            sysDictRefService.saveBatch(addSysDictRef);
            log.info("本地字典对应关系信息表新增插入了{}条数据",addSysDictRef.size());
            addSysDictRef.clear();
        }
        // 更新
        if (updateSysDictRef.size()>0){
            sysDictRefService.updateBatchById(updateSysDictRef);
            log.info("本地字典对应关系信息表更新修改了{}条数据", updateSysDictRef.size());
            updateSysDictRef.clear();
        }
        xiids.clear();
    }

    private void addSysDictRef(Brand brandRec, List<SysDictRef> addSysDictRef, ItfTblSplbXilieDownload item) {
        SysDictRef v = new SysDictRef();
        v.setId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_SERIES.getCode());
        v.setDictCode(String.valueOf(item.getXiid()));
        v.setDictValue(item.getXimc());
        v.setSortNum(item.getPxdj());
        v.setBrandId(brandRec.getBrandId());
        v.setRefDictCode(String.valueOf(item.getXiid()));
        v.setRefDictName(item.getXimc());
        v.setCreateTime(new Date());
        v.setCreateBy("system");
        addSysDictRef.add(v);
    }

    private void upSysDictRef(List<SysDictRef> updateSysDictRef, Map<String, List<SysDictRef>> oldSysDictRefRecMap, ItfTblSplbXilieDownload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDictRef v = oldSysDictRefRecMap.get(String.valueOf(item.getXiid())).get(0);
        if (!v.getDictValue().equals(item.getXimc())){
            v.setDictValue(item.getXimc());
            flag = Boolean.TRUE;
        }
        if (!v.getRefDictName().equals(item.getXimc())){
            v.setRefDictName(item.getXimc());
            flag = Boolean.TRUE;
        }
        if (v.getSortNum() != item.getPxdj()){
            v.setSortNum(item.getPxdj());
            flag = Boolean.TRUE;
        }
        if (flag){
            v.setUpdateBy("system");
            v.setUpdateTime(new Date());
            if (item.getLastModifiedBy()!=null){
                v.setUpdateBy(item.getLastModifiedBy());
            }
            if (item.getLastModifiedDate()!=null){
                v.setUpdateTime(item.getLastModifiedDate());
            }
            updateSysDictRef.add(v);
        }
    }

    private void extractedLocalPPXLAddOrUpdate(List<ItfTblSplbXilieDownload> addRec, List<ItfTblSplbXilieDownload> updateRec) {
        List<SysDict> addSysDict = new ArrayList<>();
        List<SysDict> updateSysDict = new ArrayList<>();
        List<SysDict> oldSysDictRec = new ArrayList<>();
        Map<String, List<SysDict>> oldSysDictRecMap = new HashMap<>();
        List<String> xiids = new ArrayList<>();

        // 提取系列字典数据  sysDictService
        oldSysDictRec =  sysDictService.lambdaQuery()
                .eq(SysDict::getDictType,DictTypeEnum.DICT_TYPE_SERIES.getCode()).list();
        if (CollectionUtils.isNotEmpty(oldSysDictRec)){
            oldSysDictRecMap = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictCode));
            oldSysDictRec.clear();
        }

        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblSplbXilieDownload item : addRec){
                if (oldSysDictRecMap.containsKey(String.valueOf(item.getXiid()))){
                    upSysDict(updateSysDict, oldSysDictRecMap, item);
                } else {
                    if (!xiids.contains(String.valueOf(item.getXiid()))){
                        addSysDict(addSysDict, item);
                        xiids.add(String.valueOf(item.getXiid()));
                    }
                }
            }
        }
        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblSplbXilieDownload item : updateRec){
                if (oldSysDictRecMap.containsKey(String.valueOf(item.getXiid()))){
                    upSysDict(updateSysDict, oldSysDictRecMap, item);
                } else {
                    if (!xiids.contains(String.valueOf(item.getXiid()))){
                        addSysDict(addSysDict, item);
                        xiids.add(String.valueOf(item.getXiid()));
                    }
                }
            }
        }

        // 新增的插入到表
        if (addSysDict.size()>0){
            sysDictService.saveBatch(addSysDict);
            log.info("本地字典信息表新增插入了{}条数据",addSysDict.size());
        }
        // 更新
        if (updateSysDict.size()>0){
            sysDictService.updateBatchById(updateSysDict);
            log.info("本地字典信息表更新修改了{}条数据", updateSysDict.size());
        }
        xiids.clear();
        oldSysDictRecMap.clear();
    }

    private void addSysDict(List<SysDict> addSysDict, ItfTblSplbXilieDownload item) {
        SysDict v = new SysDict();
        v.setSysDictId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_SERIES.getCode());
        v.setDictCode(String.valueOf(item.getXiid()));
        v.setDictValue(item.getXimc());
        v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
        if (item.getFlag().equals(DictTypeEnum.DICT_TYPE_FLAG_ON.getCode())){
            v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
        }
        v.setSortNum(item.getPxdj());
        v.setParentDictCode(String.valueOf(item.getXlid()));
        v.setParentDictType(DictTypeEnum.DICT_TYPE_SMALL_TYPE.getCode());
        v.setCreateTime(new Date());
        v.setFlag(DictTypeEnum.DICT_TYPE_FLAG_JY.getCode());
        addSysDict.add(v);
    }

    private void upSysDict(List<SysDict> updateSysDict, Map<String, List<SysDict>> oldSysDictRecMap, ItfTblSplbXilieDownload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDict v = oldSysDictRecMap.get(String.valueOf(item.getXiid())).get(0);
        if (!v.getDictValue().equals(item.getXimc())){
            v.setDictValue(item.getXimc());
            flag = Boolean.TRUE;
        }
        // 修改的为有效
        if (item.getFlag().equals(DictTypeEnum.DICT_TYPE_FLAG_ON.getCode())){
            if (v.getStatus().equals(DictTypeEnum.DICT_TYPE_OFF.getCode())){
                v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
                flag = Boolean.TRUE;
            }
        } else {
            if (v.getStatus().equals(DictTypeEnum.DICT_TYPE_ON.getCode())){
                v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
                flag = Boolean.TRUE;
            }
        }
        if (v.getSortNum() != item.getPxdj()){
            v.setSortNum(item.getPxdj());
            flag = Boolean.TRUE;
        }
        if (v.getParentDictCode().equals(String.valueOf(item.getXlid()))){
            v.setParentDictCode(String.valueOf(item.getXlid()));
            flag = Boolean.TRUE;
        }
        if (flag){
            updateSysDict.add(v);
        }
    }

    private void extractedPPXLAddOrUpdate(List<ItfTblSplbXilieDownload> addRec, List<ItfTblSplbXilieDownload> updateRec, JSONArray jsonArray, String synTaskNo) {
        Boolean flag = Boolean.FALSE;
        List<ItfTblSplbXilieDownload> oldRec = new ArrayList<>();
        List<String> oldRecId = new ArrayList<>();
        Map<String, List<ItfTblSplbXilieDownload>> oldRecMap = new HashMap<>();

        // 提取现有的数据信息
        oldRec =  itfTblSplbXilieDownloadService.lambdaQuery().list();
        if (CollectionUtils.isNotEmpty(oldRec)){
            oldRecId = oldRec.stream().map(ItfTblSplbXilieDownload::getId).collect(Collectors.toList());
            oldRecMap = oldRec.stream().collect(Collectors.groupingBy(ItfTblSplbXilieDownload::getId));
        }

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject aData = jsonArray.getJSONObject(i);
            ItfTblSplbXilieDownload item = JSON.toJavaObject(aData,ItfTblSplbXilieDownload.class);
            // 新增或修改数据
            flag = Boolean.FALSE;
            // 修改
            if (oldRecId.contains(item.getId())){
                ItfTblSplbXilieDownload old = oldRecMap.get(item.getId()).get(0);
                if (old.getSpid() != item.getSpid()){
                    old.setSpid(item.getSpid());
                    flag = Boolean.TRUE;
                }
                if (!old.getXlid().equals(item.getXlid())){
                    old.setXlid(item.getXlid());
                    flag = Boolean.TRUE;
                }
                if (!old.getXiid().equals(item.getXiid())){
                    old.setXiid(item.getXiid());
                    flag = Boolean.TRUE;
                }
                if (old.getXino() !=null || item.getXino() !=null){
                    if (old.getXino() !=null && item.getXino() !=null){
                        if (!old.getXino().equals(item.getXino())){
                            old.setXino(item.getXino());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        if (item.getXino() !=null){
                            old.setXino(item.getXino());
                            flag = Boolean.TRUE;
                        } else {
                            old.setXino(null);
                            flag = Boolean.TRUE;
                        }
                    }
                }
                if (!old.getXimc().equals(item.getXimc())){
                    old.setXimc(item.getXimc());
                    flag = Boolean.TRUE;
                }
                if (!old.getPxdj().equals(item.getPxdj())){
                    old.setPxdj(item.getPxdj());
                    flag = Boolean.TRUE;
                }
                if (!old.getFlag().equals(item.getFlag())){
                    old.setFlag(item.getFlag());
                    flag = Boolean.TRUE;
                }
                if (flag){
                    old.setLastModifiedBy("system");
                    old.setLastModifiedDate(new Date());
                    old.setCzsj(new Date());
                    // 操作人
                    if (item.getLastModifiedBy() != null){
                        old.setLastModifiedBy(item.getLastModifiedBy());
                    }
                    // 修改时间
                    if (item.getLastModifiedDate() != null){
                        old.setLastModifiedDate(item.getLastModifiedDate());
                    }
                    // 操作时间
                    if (item.getCzsj() != null){
                        old.setCzsj(item.getCzsj());
                    }
                    updateRec.add(old);
                }
            } else {
                // 新增
                item.setSynTaskNo(synTaskNo);
                item.setSynReqTime(new Date());
                item.setSynStatus(1);
                item.setSynTime(new Date());
                item.setSynTimes(0);
                if (item.getCreatedBy() ==null){
                    item.setCreatedBy("system");
                }
                if (item.getCreatedDate() == null){
                    item.setCreatedDate(new Date());
                }
                oldRecId.add(item.getId());
                addRec.add(item);
            }
        }

        // 新增的插入到表
        if (addRec.size()>0){
            itfTblSplbXilieDownloadService.saveBatch(addRec);
            log.info("格力品牌系列基础资料信息同步新增插入了{}条数据", addRec.size());
        }

        // 更新
        if (updateRec.size()>0){
            itfTblSplbXilieDownloadService.updateBatchById(updateRec);
            log.info("格力品牌系列基础资料信息同步更新修改了{}条数据", updateRec.size());
        }
    }

    private void extractedLocalRefXLAddOrUpdate(List<ItfTblSplbXiaoleiDownload> addRec, List<ItfTblSplbXiaoleiDownload> updateRec, Brand brandRec) {
        List<SysDictRef> addSysDictRef = new ArrayList<>();
        List<SysDictRef> updateSysDictRef = new ArrayList<>();
        List<SysDictRef> oldSysDictRefRec = new ArrayList<>();
        Map<String, List<SysDictRef>> oldSysDictRefRecMap = new HashMap<>();
        List<String> dictcodes = new ArrayList<>();


        // 提取字典对应关系
        oldSysDictRefRec =  sysDictRefService.lambdaQuery()
                .eq(SysDictRef::getBrandId, brandRec.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_SMALL_TYPE.getCode()).list();
        if (CollectionUtils.isNotEmpty(oldSysDictRefRec)){
            oldSysDictRefRecMap = oldSysDictRefRec.stream().collect(Collectors.groupingBy(SysDictRef::getDictCode));
        }

        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblSplbXiaoleiDownload item : addRec){
                if (oldSysDictRefRecMap.containsKey(String.valueOf(item.getXlid()))){
                    upxlSysdRef(updateSysDictRef, oldSysDictRefRecMap, item);
                } else {
                    if (!dictcodes.contains(String.valueOf(item.getXlid()))){
                        addxlSysdRef(brandRec, addSysDictRef, item);
                        dictcodes.add(String.valueOf(item.getXlid()));
                    }
                }
            }
        }
        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblSplbXiaoleiDownload item : updateRec){
                if (oldSysDictRefRecMap.containsKey(String.valueOf(item.getXlid()))){
                    upxlSysdRef(updateSysDictRef, oldSysDictRefRecMap, item);
                } else {
                    if (!dictcodes.contains(String.valueOf(item.getXlid()))){
                        addxlSysdRef(brandRec, addSysDictRef, item);
                        dictcodes.add(String.valueOf(item.getXlid()));
                    }
                }
            }
        }

        // 新增的插入到表
        if (addSysDictRef.size()>0){
            sysDictRefService.saveBatch(addSysDictRef);
            log.info("本地字典对应关系信息表新增插入了{}条数据",addSysDictRef.size());
            addSysDictRef.clear();
        }
        // 更新
        if (updateSysDictRef.size()>0){
            sysDictRefService.updateBatchById(updateSysDictRef);
            log.info("本地字典对应关系信息表更新修改了{}条数据", updateSysDictRef.size());
            updateSysDictRef.clear();
        }
        dictcodes.clear();
    }

    private void upxlSysdRef(List<SysDictRef> updateSysDictRef, Map<String, List<SysDictRef>> oldSysDictRefRecMap, ItfTblSplbXiaoleiDownload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDictRef v = oldSysDictRefRecMap.get(String.valueOf(item.getXlid())).get(0);
        if (!v.getDictValue().equals(item.getXlmc())){
            v.setDictValue(item.getXlmc());
            flag = Boolean.TRUE;
        }
        if (!v.getRefDictName().equals(item.getXlmc())){
            v.setRefDictName(item.getXlmc());
            flag = Boolean.TRUE;
        }
        if (v.getSortNum() != item.getPxdj()){
            v.setSortNum(item.getPxdj());
            flag = Boolean.TRUE;
        }
        if (flag){
            v.setUpdateBy("system");
            v.setUpdateTime(new Date());
            if (item.getLastModifiedBy()!=null){
                v.setUpdateBy(item.getLastModifiedBy());
            }
            if (item.getLastModifiedDate()!=null){
                v.setUpdateTime(item.getLastModifiedDate());
            }
            updateSysDictRef.add(v);
        }
    }

    private void addxlSysdRef(Brand brandRec, List<SysDictRef> addSysDictRef, ItfTblSplbXiaoleiDownload item) {
        SysDictRef v = new SysDictRef();
        v.setId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_SMALL_TYPE.getCode());
        v.setDictCode(String.valueOf(item.getXlid()));
        v.setDictValue(item.getXlmc());
        v.setSortNum(item.getPxdj());
        v.setBrandId(brandRec.getBrandId());
        v.setRefDictCode(String.valueOf(item.getXlid()));
        v.setRefDictName(item.getXlmc());
        v.setCreateTime(new Date());
        v.setCreateBy("system");
        addSysDictRef.add(v);
    }

    private void upXiaoLei(List<SysDict> updateSysDict, Map<String, List<SysDict>> oldSysDictRecMap, ItfTblSplbXiaoleiDownload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDict v = oldSysDictRecMap.get(String.valueOf(item.getXlid())).get(0);
        if (!v.getDictValue().equals(item.getXlmc())){
            v.setDictValue(item.getXlmc());
            flag = Boolean.TRUE;
        }
        // 修改的为有效
        if (item.getFlag().equals(DictTypeEnum.DICT_TYPE_FLAG_ON.getCode())){
            if (v.getStatus().equals(DictTypeEnum.DICT_TYPE_OFF.getCode())){
                v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
                flag = Boolean.TRUE;
            }
        } else {
            if (v.getStatus().equals(DictTypeEnum.DICT_TYPE_ON.getCode())){
                v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
                flag = Boolean.TRUE;
            }
        }
        if (v.getSortNum() != item.getPxdj()){
            v.setSortNum(item.getPxdj());
            flag = Boolean.TRUE;
        }
        if (flag){
            updateSysDict.add(v);
        }
    }

    private void addXiaoLei(List<SysDict> addSysDict, ItfTblSplbXiaoleiDownload item) {
        SysDict v = new SysDict();
        v.setSysDictId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_SMALL_TYPE.getCode());
        v.setDictCode(String.valueOf(item.getXlid()));
        v.setDictValue(item.getXlmc());
        v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
        if (item.getFlag().equals(DictTypeEnum.DICT_TYPE_FLAG_ON.getCode())){
            v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
        }
        v.setSortNum(item.getPxdj());
        // spid即大类
        v.setParentDictCode(String.valueOf(item.getSpid()));
        v.setParentDictType(DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode());
        v.setCreateTime(new Date());
        v.setFlag(DictTypeEnum.DICT_TYPE_FLAG_JY.getCode());
        addSysDict.add(v);
    }

    private void extractedLocalXLAddOrUpdate(List<ItfTblSplbXiaoleiDownload> addRec, List<ItfTblSplbXiaoleiDownload> updateRec) {
        List<SysDict> addSysDict = new ArrayList<>();
        List<SysDict> updateSysDict = new ArrayList<>();
        List<SysDict> oldSysDictRec = new ArrayList<>();
        Map<String, List<SysDict>> oldSysDictRecMap = new HashMap<>();
        List<String> dictcodes = new ArrayList<>();

        // 提取小类字典数据  sysDictService
        oldSysDictRec =  sysDictService.lambdaQuery()
                .eq(SysDict::getDictType,DictTypeEnum.DICT_TYPE_SMALL_TYPE.getCode()).list();
        if (CollectionUtils.isNotEmpty(oldSysDictRec)){
            oldSysDictRecMap = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictCode));
        }

        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblSplbXiaoleiDownload item : addRec){
                if (oldSysDictRecMap.containsKey(String.valueOf(item.getXlid()))){
                    upXiaoLei(updateSysDict, oldSysDictRecMap, item);
                } else {
                    if (!dictcodes.contains(String.valueOf(item.getXlid()))){
                        addXiaoLei(addSysDict, item);
                        dictcodes.add(String.valueOf(item.getXlid()));
                    }
                }
            }
        }
        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblSplbXiaoleiDownload item : updateRec){
                if (oldSysDictRecMap.containsKey(String.valueOf(item.getXlid()))){
                    upXiaoLei(updateSysDict, oldSysDictRecMap, item);
                } else {
                    if (!dictcodes.contains(String.valueOf(item.getXlid()))){
                        addXiaoLei(addSysDict, item);
                        dictcodes.add(String.valueOf(item.getXlid()));
                    }
                }
            }
        }

        // 新增的插入到表
        if (addSysDict.size()>0){
            sysDictService.saveBatch(addSysDict);
            log.info("本地字典信息表新增插入了{}条数据",addSysDict.size());
        }
        // 更新
        if (updateSysDict.size()>0){
            sysDictService.updateBatchById(updateSysDict);
            log.info("本地字典信息表更新修改了{}条数据", updateSysDict.size());
        }
        dictcodes.clear();
    }

    private void extractedGLXLAddOrUpdate(List<ItfTblSplbXiaoleiDownload> addRec, List<ItfTblSplbXiaoleiDownload> updateRec, JSONArray jsonArray, String synTaskNo) {
        Boolean flag = Boolean.FALSE;
        List<ItfTblSplbXiaoleiDownload> oldRec =  new ArrayList<>();
        List<String> oldRecId = new ArrayList<>();
        Map<String, List<ItfTblSplbXiaoleiDownload>> oldRecMap = new HashMap<>();

        // 提取现有的数据信息
        oldRec = itfTblSplbXiaoleiDownloadService.lambdaQuery().list();
        if (CollectionUtils.isNotEmpty(oldRec)){
            oldRecId = oldRec.stream().map(ItfTblSplbXiaoleiDownload::getId).collect(Collectors.toList());
            oldRecMap = oldRec.stream().collect(Collectors.groupingBy(ItfTblSplbXiaoleiDownload::getId));
        }

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject aData = jsonArray.getJSONObject(i);
            ItfTblSplbXiaoleiDownload item = JSON.toJavaObject(aData,ItfTblSplbXiaoleiDownload.class);
            // 新增或修改数据
            flag = Boolean.FALSE;
            // 修改
            if (oldRecId.contains(item.getId())){
                ItfTblSplbXiaoleiDownload old = oldRecMap.get(item.getId()).get(0);
                if (!old.getSpid().equals(item.getSpid())){
                    old.setSpid(item.getSpid());
                    flag = Boolean.TRUE;
                }
                if (!old.getXlid().equals(item.getXlid())){
                    old.setXlid(item.getXlid());
                    flag = Boolean.TRUE;
                }
                if (old.getXlno() !=null || item.getXlno() !=null){
                    if (old.getXlno() !=null && item.getXlno() !=null){
                        if (!old.getXlno().equals(item.getXlno())){
                            old.setXlno(item.getXlno());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        if (item.getXlno() !=null){
                            old.setXlno(item.getXlno());
                            flag = Boolean.TRUE;
                        } else {
                            old.setXlno(null);
                            flag = Boolean.TRUE;
                        }
                    }
                }

                if (!old.getXlmc().equals(item.getXlmc())){
                    old.setXlmc(item.getXlmc());
                    flag = Boolean.TRUE;
                }
                if (!old.getPxdj().equals(item.getPxdj())){
                    old.setPxdj(item.getPxdj());
                    flag = Boolean.TRUE;
                }
                if (!old.getFlag().equals(item.getFlag())){
                    old.setFlag(item.getFlag());
                    flag = Boolean.TRUE;
                }
                if (flag){
                    old.setLastModifiedBy("system");
                    old.setLastModifiedDate(new Date());
                    // 操作人
                    if (item.getLastModifiedBy() != null){
                        old.setLastModifiedBy(item.getLastModifiedBy());
                    }
                    // 修改时间
                    if (item.getLastModifiedDate() != null){
                        old.setLastModifiedDate(item.getLastModifiedDate());
                    }
                    updateRec.add(old);
                }
            } else {
                // 新增
                item.setSynTaskNo(synTaskNo);
                item.setSynReqTime(new Date());
                item.setSynStatus(1);
                item.setSynTime(new Date());
                item.setSynTimes(0);
                if (item.getCreatedBy() ==null){
                    item.setCreatedBy("system");
                }
                if (item.getCreatedDate() == null){
                    item.setCreatedDate(new Date());
                }
                oldRecId.add(item.getId());
                addRec.add(item);
            }
        }

        // 新增的插入到表
        if (addRec.size()>0){
            itfTblSplbXiaoleiDownloadService.saveBatch(addRec);
            log.info("格力小类基础资料信息同步新增插入了{}条数据", addRec.size());
        }

        // 更新
        if (updateRec.size()>0){
            itfTblSplbXiaoleiDownloadService.updateBatchById(updateRec);
            log.info("格力小类基础资料信息同步更新修改了{}条数据", updateRec.size());
        }
    }

    private void updalei(List<SysDict> updateSysDict, Map<String, List<SysDict>> oldSysDictRecMap, ItfTblSplbDaleiDownload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDict v = oldSysDictRecMap.get(String.valueOf(item.getSpid())).get(0);
        if (!v.getDictValue().equals(item.getSpmc())){
            v.setDictValue(item.getSpmc());
            flag = Boolean.TRUE;
        }
        // 修改的为有效
        if (item.getFlag().equals(DictTypeEnum.DICT_TYPE_FLAG_ON.getCode())){
            if (v.getStatus().equals(DictTypeEnum.DICT_TYPE_OFF.getCode())){
                v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
                flag = Boolean.TRUE;
            }
        } else {
            if (v.getStatus().equals(DictTypeEnum.DICT_TYPE_ON.getCode())){
                v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
                flag = Boolean.TRUE;
            }
        }
        if (v.getSortNum() != item.getPxdj()){
            v.setSortNum(item.getPxdj());
            flag = Boolean.TRUE;
        }
        if (flag){
            updateSysDict.add(v);
        }
    }

    private void addDalei(List<SysDict> addSysDict, ItfTblSplbDaleiDownload item) {
        SysDict v = new SysDict();
        v.setSysDictId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode());
        v.setDictCode(String.valueOf(item.getSpid()));
        v.setDictValue(item.getSpmc());
        v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
        if (item.getFlag().equals(DictTypeEnum.DICT_TYPE_FLAG_ON.getCode())){
            v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
        }
        v.setSortNum(item.getPxdj());
        // v.setParentDictCode(v.getDictCode());
        // v.setParentDictType(v.getDictType());
        v.setCreateTime(new Date());
        v.setFlag(ServiceCategoryEnum.HOME_TYPE.getCode());
        if (item.getSpid() == Long.valueOf(DictTypeEnum.DICT_TYPE_SY_102.getCode()) ||
                item.getSpid() == Long.valueOf(DictTypeEnum.DICT_TYPE_SY_109.getCode())){
            v.setFlag(ServiceCategoryEnum.COMMERCIAL_TYPE.getCode());
        }
        addSysDict.add(v);
    }

    public void extractedLocalDaleiAddOrUpdate(List<ItfTblSplbDaleiDownload> addRec, List<ItfTblSplbDaleiDownload> updateRec) {
        List<SysDict> addSysDict = new ArrayList<>();
        List<SysDict> updateSysDict = new ArrayList<>();
        List<SysDict> oldSysDictRec = new ArrayList<>();
        Map<String, List<SysDict>> oldSysDictRecMap = new HashMap<>();
        List<String> dictcodes = new ArrayList<>();

        // 提取大类字典数据  sysDictService
        oldSysDictRec =  sysDictService.lambdaQuery()
                .eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode())
                .list();

        if (CollectionUtils.isNotEmpty(oldSysDictRec)){
            oldSysDictRecMap = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictCode));
        }

        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblSplbDaleiDownload item : addRec){
                if (oldSysDictRecMap.containsKey(String.valueOf(item.getSpid()))){
                    updalei(updateSysDict, oldSysDictRecMap, item);
                } else {
                    if (!dictcodes.contains(String.valueOf(item.getSpid()))){
                        addDalei(addSysDict, item);
                        dictcodes.add(String.valueOf(item.getSpid()));
                    }
                }
            }
        }
        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblSplbDaleiDownload item : updateRec){
                if (oldSysDictRecMap.containsKey(String.valueOf(item.getSpid()))){
                    updalei(updateSysDict, oldSysDictRecMap, item);
                } else {
                    if (!dictcodes.contains(String.valueOf(item.getSpid()))){
                        addDalei(addSysDict, item);
                        dictcodes.add(String.valueOf(item.getSpid()));
                    }
                }
            }
        }

        // 新增的插入到表
        if (addSysDict.size()>0){
            sysDictService.saveBatch(addSysDict);
            log.info("本地字典信息表新增插入了{}条数据",addSysDict.size());
        }
        // 更新
        if (updateSysDict.size()>0){
            sysDictService.updateBatchById(updateSysDict);
            log.info("本地字典信息表更新修改了{}条数据", updateSysDict.size());
        }
        dictcodes.clear();
    }

    public void extractedDaleiAddOrUpdate(List<ItfTblSplbDaleiDownload> addRec, List<ItfTblSplbDaleiDownload> updateRec, JSONArray jsonArray, String synTaskNo) {
        Boolean flag = Boolean.FALSE;
        // 提取现有的数据信息
        List<ItfTblSplbDaleiDownload> oldRec =  itfTblSplbDaleiDownloadService.lambdaQuery().list();
        List<Long> oldDaLeiSpId = oldRec.stream().map(ItfTblSplbDaleiDownload::getSpid).collect(Collectors.toList());
        Map<Long, List<ItfTblSplbDaleiDownload>> oldRecMap = oldRec.stream().collect(Collectors.groupingBy(ItfTblSplbDaleiDownload::getSpid));

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject aData = jsonArray.getJSONObject(i);
            ItfTblSplbDaleiDownload item = JSON.toJavaObject(aData,ItfTblSplbDaleiDownload.class);
            // 新增或修改数据
            flag = Boolean.FALSE;
            // 修改
            if (oldDaLeiSpId.contains(item.getSpid())){
                ItfTblSplbDaleiDownload old = oldRecMap.get(item.getSpid()).get(0);
                if (!old.getSpno().equals(item.getSpno())){
                    old.setSpno(item.getSpno());
                    flag = Boolean.TRUE;
                }
                if (!old.getSpmc().equals(item.getSpmc())){
                    old.setSpmc(item.getSpmc());
                    flag = Boolean.TRUE;
                }
                if (!old.getPxdj().equals(item.getPxdj())){
                    old.setPxdj(item.getPxdj());
                    flag = Boolean.TRUE;
                }
                if (!old.getFlag().equals(item.getFlag())){
                    old.setFlag(item.getFlag());
                    flag = Boolean.TRUE;
                }
                if (!old.getSfaz().equals(item.getSfaz())){
                    old.setSfaz(item.getSfaz());
                    flag = Boolean.TRUE;
                }
                if (flag){
                    old.setLastModifiedBy("system");
                    old.setLastModifiedDate(new Date());
                    // 操作人
                    if (item.getLastModifiedBy() != null){
                        old.setLastModifiedBy(item.getLastModifiedBy());
                    }
                    // 修改时间
                    if (item.getLastModifiedDate() != null){
                        old.setLastModifiedDate(item.getLastModifiedDate());
                    }
                    updateRec.add(old);
                }
            } else {
                // 新增
                item.setSynTaskNo(synTaskNo);
                item.setSynReqTime(new Date());
                item.setSynStatus(1);
                item.setSynTime(new Date());
                item.setSynTimes(0);
                if (item.getCreatedBy() ==null){
                    item.setCreatedBy("system");
                }
                if (item.getCreatedDate() == null){
                    item.setCreatedDate(new Date());
                }
                oldDaLeiSpId.add(item.getSpid());
                addRec.add(item);
            }
        }

        // 新增的插入到表
        if (addRec.size()>0){
            itfTblSplbDaleiDownloadService.saveBatch(addRec);
            log.info("格力大类基础资料信息同步新增插入了{}条数据", addRec.size());
        }

        // 更新
        if (updateRec.size()>0){
            itfTblSplbDaleiDownloadService.updateBatchById(updateRec);
            log.info("格力大类基础资料信息同步更新修改了{}条数据", updateRec.size());
        }
    }

    private void updateLeibie(List<SysDictRef> updateSysDictRef, Map<String, List<SysDictRef>> oldSysDictRefRecMap, ItfTblSplbDaleiDownload item) {
        Boolean flag = Boolean.FALSE;
        SysDictRef v = oldSysDictRefRecMap.get(String.valueOf(item.getSpid())).get(0);
        if (!v.getDictValue().equals(item.getSpmc())){
            v.setDictValue(item.getSpmc());
            flag = Boolean.TRUE;
        }
        if (!v.getRefDictName().equals(item.getSpmc())){
            v.setRefDictName(item.getSpmc());
            flag = Boolean.TRUE;
        }
        if (v.getSortNum() != item.getPxdj()){
            v.setSortNum(item.getPxdj());
            flag = Boolean.TRUE;
        }
        if (flag){
            v.setUpdateBy("system");
            v.setUpdateTime(new Date());
            if (item.getLastModifiedBy()!=null){
                v.setUpdateBy(item.getLastModifiedBy());
            }
            if (item.getLastModifiedDate()!=null){
                v.setUpdateTime(item.getLastModifiedDate());
            }
            updateSysDictRef.add(v);
        }
    }

    private void addLeibie(Brand brandRec, List<SysDictRef> addSysDictRef, ItfTblSplbDaleiDownload item) {
        SysDictRef v = new SysDictRef();
        v.setId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode());
        v.setDictCode(String.valueOf(item.getSpid()));
        v.setDictValue(item.getSpmc());
        v.setSortNum(item.getPxdj());
        v.setBrandId(brandRec.getBrandId());
        v.setRefDictCode(String.valueOf(item.getSpid()));
        v.setRefDictName(item.getSpmc());
        v.setCreateTime(new Date());
        v.setCreateBy("system");
        addSysDictRef.add(v);
    }

    public void extractedLoaclRefDaleiAddOrUpdate(List<ItfTblSplbDaleiDownload> addRec,
                                                  List<ItfTblSplbDaleiDownload> updateRec,
                                                  Brand brandRec) {
        List<SysDictRef> addSysDictRef = new ArrayList<>();
        List<SysDictRef> updateSysDictRef = new ArrayList<>();
        List<String> dictcodes = new ArrayList<>();

        // 提取字典对应关系
        List<SysDictRef> oldSysDictRefRec =  sysDictRefService.lambdaQuery()
                .eq(SysDictRef::getBrandId, brandRec.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode()).list();
        Map<String, List<SysDictRef>> oldSysDictRefRecMap = oldSysDictRefRec.stream().collect(Collectors.groupingBy(SysDictRef::getDictCode));


        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblSplbDaleiDownload item : addRec){
                if (oldSysDictRefRecMap.containsKey(String.valueOf(item.getSpid()))){
                    updateLeibie(updateSysDictRef, oldSysDictRefRecMap, item);
                } else {
                    if (!dictcodes.contains(String.valueOf(item.getSpid()))){
                        addLeibie(brandRec, addSysDictRef, item);
                        dictcodes.add(String.valueOf(item.getSpid()));
                    }
                }
            }
        }
        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblSplbDaleiDownload item : updateRec){
                if (oldSysDictRefRecMap.containsKey(String.valueOf(item.getSpid()))){
                    updateLeibie(updateSysDictRef, oldSysDictRefRecMap, item);
                } else {
                    if (!dictcodes.contains(String.valueOf(item.getSpid()))){
                        addLeibie(brandRec, addSysDictRef, item);
                        dictcodes.add(String.valueOf(item.getSpid()));
                    }
                }
            }
        }

        // 新增的插入到表
        if (addSysDictRef.size()>0){
            sysDictRefService.saveBatch(addSysDictRef);
            log.info("本地字典对应关系信息表新增插入了{}条数据",addSysDictRef.size());
            addSysDictRef.clear();
        }
        // 更新
        if (updateSysDictRef.size()>0){
            sysDictRefService.updateBatchById(updateSysDictRef);
            log.info("本地字典对应关系信息表更新修改了{}条数据", updateSysDictRef.size());
            updateSysDictRef.clear();
        }
        dictcodes.clear();
    }
}
