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

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zfire.jiasm.syncdata.constant.BrandWebsitStatEnum;
import com.zfire.jiasm.syncdata.constant.DictTypeEnum;
import com.zfire.jiasm.syncdata.constant.InterFaceTypeEnum;
import com.zfire.jiasm.syncdata.plus.entity.*;
import com.zfire.jiasm.syncdata.plus.service.*;
import com.zfire.jiasm.syncdata.service.GLPPGFWebsitService;
import com.zfire.jiasm.syncdata.service.PublicService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

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


@Slf4j
@Service
public class GLPPGFWebsitServiceImpl implements GLPPGFWebsitService {
    @Resource
    ItfTblXinWangdianDownloadService itfTblXinWangdianDownloadService;
    @Resource
    BrandWebsitService brandWebsitService;
    @Resource
    SysDictService sysDictService;
    @Resource
    BelongcompanyService belongcompanyService;
    @Resource
    private PublicService publicService;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void extractedFSXS(JSONArray jsonArray, Brand record, String synTaskNo) {

        List<ItfTblXinWangdianDownload> addRec = new ArrayList<>();
        List<ItfTblXinWangdianDownload> updateRec = new ArrayList<>();


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

        // 数据同步到本地表
        extractedFSXSWDLocalAddOrUpdate(addRec, updateRec, record,
                InterFaceTypeEnum.XS_WEBSIT_TYPE.getCode());

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

        // 初始化特殊网点 99999999 格力电器总部
        insertTSWDInfo(record);
        // X9910000  京东商城
        insertTSWDInfoJD(record);
        // 初始化特殊网点 X9920000 天猫商城
        insertTSWDInfoTM(record);
        // X9930000  其他电商
        insertTSWDInfoQT(record);
        // S9329829 天猫优品
        insertTSWDInfoTMYP(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void extractedGZXS(JSONArray jsonArray, Brand record, String synTaskNo){
        List<ItfTblXinWangdianDownload> addRec = new ArrayList<>();
        List<ItfTblXinWangdianDownload> updateRec = new ArrayList<>();

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

        // 数据同步到本地表
        extractedFSXSWDLocalAddOrUpdate(addRec, updateRec, record,
                InterFaceTypeEnum.XS_WEBSIT_TYPE.getCode());

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void extractedFSSH(JSONArray jsonArray, Brand record, String synTaskNo){
        List<ItfTblXinWangdianDownload> addRec = new ArrayList<>();
        List<ItfTblXinWangdianDownload> updateRec = new ArrayList<>();

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

        // 数据同步到本地表
        extractedFSXSWDLocalAddOrUpdate(addRec, updateRec, record,
                InterFaceTypeEnum.SH_WEBSIT_TYPE.getCode());

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void extractedGZSH(JSONArray jsonArray, Brand record, String synTaskNo){
        List<ItfTblXinWangdianDownload> addRec = new ArrayList<>();
        List<ItfTblXinWangdianDownload> updateRec = new ArrayList<>();

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

        // 数据同步到本地表
        extractedFSXSWDLocalAddOrUpdate(addRec, updateRec, record,
                InterFaceTypeEnum.SH_WEBSIT_TYPE.getCode());

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

    }

    private void insertTSWDInfoJD(Brand record) {
        extractedProcess("",record, "X9910000", InterFaceTypeEnum.XS_WEBSIT_TYPE, "京东商城");
    }

    private void insertTSWDInfoTM(Brand record) {
        extractedProcess("",record, "X9920000", InterFaceTypeEnum.XS_WEBSIT_TYPE, "天猫商城");
    }

    private void insertTSWDInfoTMYP(Brand record) {
        extractedProcess("80552261",record, "S9329829", InterFaceTypeEnum.SH_WEBSIT_TYPE, "天猫优品");
    }

    private void insertTSWDInfoQT(Brand record) {
        extractedProcess("",record, "X9930000", InterFaceTypeEnum.XS_WEBSIT_TYPE, "其他商城");
    }

    private void insertTSWDInfo(Brand record) {
        extractedProcess("",record, "99999999", InterFaceTypeEnum.XS_WEBSIT_TYPE, "格力总部");
    }

    private void extractedProcess(String sysWebsitNumber,Brand record, String s, InterFaceTypeEnum xsWebsitType, String 格力总部) {
        BrandWebsit oldWebsitRec = brandWebsitService.lambdaQuery()
                .eq(BrandWebsit::getBrandWebsitNumber, s)
                .eq(BrandWebsit::getType, xsWebsitType.getCode())
                .last("limit 1")
                .one();
        if (oldWebsitRec == null) {
            BrandWebsit v = new BrandWebsit();

            v.setSysWebsitNumber(sysWebsitNumber);
            v.setId(publicService.getUUID());
            v.setBrandPkId(publicService.getUUID());
            v.setBelongCompanyId(0);
            v.setBelongCompany(s);
            v.setType(xsWebsitType.getCode());
            v.setBrandWebsitNumber(s);
            v.setBrandWebsitName(格力总部);
            v.setFzren("/");
            v.setYddh("4008365315");
            v.setDhhm("4008365315");
            v.setSfdlfr("否");
            v.setLxren("/");
            v.setFzrsfz("");
            v.setShao("");
            v.setKhyh("");
            v.setHanghao("");
            v.setYhzh("");
            v.setSplb("");
            v.setSplbmc("");
            v.setFwlm("");
            v.setWdjb("");
            v.setSfdsqy("");
            v.setFwrx("");
            v.setStat("TOW");
            v.setSfen("广东省");
            v.setCshi("珠海市");
            v.setXian("/");
            v.setXzhen("/");
            v.setDizi("珠海");
            v.setBeiz("特殊网点");
            v.setGroupCompanyId(record.getBrandId());
            v.setGroupCompanyName(record.getBrand());
            v.setCreateBy("system");
            v.setUpdateBy("system");
            v.setCreateTime(new Date());
            v.setUpdateTime(new Date());

            brandWebsitService.save(v);
        }
    }

    @NotNull
    private StringBuffer getStringBuffer(Map<String, String> oldSysDictRecMap, String splb) {
        List<String> resultData = new ArrayList<>();
        StringBuffer splbmc = new StringBuffer();
        if (!StringUtils.isEmpty(splb)){
            if (splb.contains(",")){
                List<String> result = Arrays.asList(splb.split(","));
                resultData.addAll(result);
            } else {
                resultData.add(splb);
            }

            for (int i=0;i<resultData.size();i++){
                if (oldSysDictRecMap.containsKey(resultData.get(i))){
                    if (i>0){
                        splbmc.append(",");
                    }
                    splbmc.append(oldSysDictRecMap.get(resultData.get(i)));
                }
            }
        }
        return splbmc;
    }

    private void extractedFSXSWDLocalAddOrUpdate(List<ItfTblXinWangdianDownload> addRec,
                                                 List<ItfTblXinWangdianDownload> updateRec,
                                                 Brand record, String type) {
        Boolean flag;
        List<BrandWebsit> addWebsitRec = new ArrayList<>();
        List<BrandWebsit> updateWebsitRec = new ArrayList<>();
        Map<String, String> oldSysDictRecMap  = new HashMap<>();
        List<BrandWebsit> oldWebsitRec = new ArrayList<>();
        Map<String, List<BrandWebsit>> oldWebsitRecMap = new HashMap<>();
        List<SysDict> oldSysDictRec = new ArrayList<>();
        List<Belongcompany> bcs =  new ArrayList<>();
        Map<String, List<Belongcompany>> bMapxs = new HashMap<>();
        List<String> saleList = new ArrayList<>();
        List<String> serviceList = new ArrayList<>();
        List<String> pkguids = new ArrayList<>();

        // 提取原有的网点信息
        oldWebsitRec =  brandWebsitService.lambdaQuery().eq(BrandWebsit::getGroupCompanyId,record.getBrandId()).list();
        if (CollectionUtils.isNotEmpty(oldWebsitRec)){
            oldWebsitRecMap = oldWebsitRec.stream().collect(Collectors.groupingBy(BrandWebsit::getBrandPkId));
        }

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

        // 取门户对应关系
        bcs =   belongcompanyService.lambdaQuery()
                .select(Belongcompany::getId,Belongcompany::getBelongCompanySale,Belongcompany::getBelongCompanyProfession).list();
        bMapxs = bcs.stream().collect(Collectors.groupingBy(Belongcompany::getBelongCompanySale));
        Map<String, List<Belongcompany>> bcMap = bcs.stream().collect(Collectors.groupingBy(Belongcompany::getBelongCompanyProfession));

        // 销售与售后门户全加载
        bMapxs.forEach((k, v) -> bcMap.merge(k, v, (v1, v2) -> v2));

        saleList = bcs.stream().map(Belongcompany::getBelongCompanySale).collect(Collectors.toList());
        serviceList = bcs.stream().map(Belongcompany::getBelongCompanyProfession).collect(Collectors.toList());
        // 销售与售后的门户信息 'X2300000','S9219807','X2100000','S9219801','X9920000','S9329829'
        serviceList.addAll(saleList);


        for (SysDict item :oldSysDictRec){
            oldSysDictRecMap.put(item.getDictCode(),item.getDictValue());
        }

        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblXinWangdianDownload item : addRec){
                if (oldWebsitRecMap.containsKey(item.getPkGuid()) || pkguids.contains(item.getPkGuid())){
                    // 修改
                    upXinWdinfo(updateWebsitRec, oldSysDictRecMap, serviceList, bcMap, item, oldWebsitRecMap);
                } else {
                    // 新增
                    addXinWdinfo(record, type, addWebsitRec, oldSysDictRecMap, serviceList, bcMap, item);
                    pkguids.add(item.getPkGuid());
                }
            }
        }

        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblXinWangdianDownload item : updateRec){
                if (oldWebsitRecMap.containsKey(item.getPkGuid()) || pkguids.contains(item.getPkGuid())){
                    // 修改
                    upXinWdinfo(updateWebsitRec, oldSysDictRecMap, serviceList, bcMap, item, oldWebsitRecMap);
                } else {
                    // 新增
                    addXinWdinfo(record, type, addWebsitRec, oldSysDictRecMap, serviceList, bcMap, item);
                    pkguids.add(item.getPkGuid());
                }
            }
        }

        // 新增的插入到表
        if (addWebsitRec.size()>0){
            brandWebsitService.saveBatch(addWebsitRec);
            log.info("本地网点信息表新增插入了{}条数据", addWebsitRec.size());
        }
        // 更新
        if (updateWebsitRec.size()>0){
            brandWebsitService.updateBatchById(updateWebsitRec);
            log.info("本地网点信息表更新修改了{}条数据", updateWebsitRec.size());
        }
        pkguids.clear();
    }

    private void upXinWdinfo(List<BrandWebsit> updateWebsitRec, Map<String, String> oldSysDictRecMap,
                             List<String> serviceList, Map<String, List<Belongcompany>> bcMap,
                             ItfTblXinWangdianDownload item, Map<String, List<BrandWebsit>> oldWebsitRecMap) {
        Boolean flag;
        BrandWebsit old = oldWebsitRecMap.get(item.getPkGuid()).get(0);

        flag = Boolean.FALSE;
        if (item.getSsxsgs() != null ){
            if (!old.getBelongCompany().equals(item.getSsxsgs())){
                old.setBelongCompanyId(0);
                if (serviceList.contains(item.getSsxsgs())){
                    old.setBelongCompanyId(bcMap.get(item.getSsxsgs()).get(0).getId());
                }
                old.setBelongCompany(item.getSsxsgs());
                flag = Boolean.TRUE;
            }
        }

        if (!old.getSysWebsitNumber().equals(item.getXtwdbh())){
            old.setSysWebsitNumber(item.getXtwdbh());
            flag = Boolean.TRUE;
        }
        if (!old.getBrandWebsitNumber().equals(item.getWdno())){
            old.setBrandWebsitNumber(item.getWdno());
            flag = Boolean.TRUE;
        }
        if (!old.getBrandWebsitName().equals(item.getWdmc())){
            old.setBrandWebsitName(item.getWdmc());
            flag = Boolean.TRUE;
        }
        if (!StringUtils.isEmpty(old.getFzren()) || !StringUtils.isEmpty(item.getFzren())){
            if (!StringUtils.isEmpty(old.getFzren()) && !StringUtils.isEmpty(item.getFzren())){
                if (!old.getFzren().equals(item.getFzren())){
                    old.setFzren(item.getFzren());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getFzren())) {
                    old.setFzren(item.getFzren());
                } else {
                    old.setFzren("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getYddh()) || !StringUtils.isEmpty(item.getYddh())){
            if (!StringUtils.isEmpty(old.getYddh()) && !StringUtils.isEmpty(item.getYddh())){
                if (!old.getYddh().equals(item.getYddh())){
                    old.setYddh(item.getYddh());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getYddh())) {
                    old.setYddh(item.getYddh());
                } else {
                    old.setYddh("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getDhhm()) || !StringUtils.isEmpty(item.getDhhm())){
            if (!StringUtils.isEmpty(old.getYddh()) && !StringUtils.isEmpty(item.getDhhm())){
                if (!old.getDhhm().equals(item.getDhhm())){
                    old.setDhhm(item.getDhhm());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getDhhm())) {
                    old.setDhhm(item.getDhhm());
                } else {
                    old.setDhhm("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getSfdlfr()) || !StringUtils.isEmpty(item.getSfdlfr())){
            if (!StringUtils.isEmpty(old.getSfdlfr()) && !StringUtils.isEmpty(item.getSfdlfr())){
                if (!old.getSfdlfr().equals(item.getSfdlfr())){
                    old.setSfdlfr(item.getSfdlfr());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getSfdlfr())) {
                    old.setSfdlfr(item.getSfdlfr());
                } else {
                    old.setSfdlfr("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getLxren()) || !StringUtils.isEmpty(item.getLxren())){
            if (!StringUtils.isEmpty(old.getLxren()) && !StringUtils.isEmpty(item.getLxren())){
                if (!old.getLxren().equals(item.getLxren())){
                    old.setLxren(item.getLxren());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getLxren())) {
                    old.setLxren(item.getLxren());
                } else {
                    old.setLxren("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getFzrsfz()) || !StringUtils.isEmpty(item.getFzrsfz())){
            if (!StringUtils.isEmpty(old.getFzrsfz()) && !StringUtils.isEmpty(item.getFzrsfz())){
                if (!old.getFzrsfz().equals(item.getFzrsfz())){
                    old.setFzrsfz(item.getFzrsfz());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getFzrsfz())) {
                    old.setFzrsfz(item.getFzrsfz());
                } else {
                    old.setFzrsfz("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getShao()) || !StringUtils.isEmpty(item.getShao())){
            if (!StringUtils.isEmpty(old.getShao()) && !StringUtils.isEmpty(item.getShao())){
                if (!old.getShao().equals(item.getShao())){
                    old.setShao(item.getShao());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getShao())) {
                    old.setShao(item.getShao());
                } else {
                    old.setShao("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getKhyh()) || !StringUtils.isEmpty(item.getKhyh())){
            if (!StringUtils.isEmpty(old.getKhyh()) && !StringUtils.isEmpty(item.getKhyh())){
                if (!old.getKhyh().equals(item.getKhyh())){
                    old.setKhyh(item.getKhyh());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getKhyh())) {
                    old.setKhyh(item.getKhyh());
                } else {
                    old.setKhyh("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getHanghao()) || !StringUtils.isEmpty(item.getHanghao())){
            if (!StringUtils.isEmpty(old.getHanghao()) && !StringUtils.isEmpty(item.getHanghao())){
                if (!old.getHanghao().equals(item.getHanghao())){
                    old.setHanghao(item.getHanghao());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getHanghao())) {
                    old.setHanghao(item.getHanghao());
                } else {
                    old.setHanghao("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getYhzh()) || !StringUtils.isEmpty(item.getYhzh())){
            if (!StringUtils.isEmpty(old.getYhzh()) && !StringUtils.isEmpty(item.getYhzh())){
                if (!old.getYhzh().equals(item.getYhzh())){
                    old.setYhzh(item.getYhzh());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getYhzh())) {
                    old.setYhzh(item.getYhzh());
                } else {
                    old.setYhzh("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getSplb()) || !StringUtils.isEmpty(item.getSplb())){
            if (!StringUtils.isEmpty(old.getSplb()) && !StringUtils.isEmpty(item.getSplb())){
                if (!old.getSplb().equals(","+ item.getSplb()+",")){
                    old.setSplb(","+ item.getSplb()+",");
                    // 提取品类名称
                    StringBuffer splbmc = getStringBuffer(oldSysDictRecMap, item.getSplb());
                    old.setSplbmc(splbmc.toString());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getSplb())) {
                    old.setSplb(","+ item.getSplb()+",");
                } else {
                    old.setSplb("");
                }
                // 提取品类名称
                StringBuffer splbmc = getStringBuffer(oldSysDictRecMap, item.getSplb());
                old.setSplbmc(splbmc.toString());
            }
        }
        if (!StringUtils.isEmpty(old.getWdjb()) || !StringUtils.isEmpty(item.getWdjb())){
            if (!StringUtils.isEmpty(old.getWdjb()) && !StringUtils.isEmpty(item.getWdjb())){
                if (!old.getWdjb().equals(item.getWdjb())){
                    old.setWdjb(item.getWdjb());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getWdjb())) {
                    old.setWdjb(item.getWdjb());
                } else {
                    old.setWdjb("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getSfdsqy()) || !StringUtils.isEmpty(item.getSfdsqy())){
            if (!StringUtils.isEmpty(old.getSfdsqy()) && !StringUtils.isEmpty(item.getSfdsqy())){
                if (!old.getSfdsqy().equals(item.getSfdsqy())){
                    old.setSfdsqy(item.getSfdsqy());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getSfdsqy())) {
                    old.setSfdsqy(item.getSfdsqy());
                } else {
                    old.setSfdsqy("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getFwrx()) || !StringUtils.isEmpty(item.getFwrx())){
            if (!StringUtils.isEmpty(old.getFwrx()) && !StringUtils.isEmpty(item.getFwrx())){
                if (!old.getFwrx().equals(item.getFwrx())){
                    old.setFwrx(item.getFwrx());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getFwrx())) {
                    old.setFwrx(item.getFwrx());
                } else {
                    old.setFwrx("");
                }
            }
        }

        if (!old.getStat().equals(BrandWebsitStatEnum.getMessageByCode(item.getStat()))){
            old.setStat(BrandWebsitStatEnum.getMessageByCode(item.getStat()));
            flag = Boolean.TRUE;
        }

        if (!old.getShnum().equals(item.getShnum()) && !StringUtils.isEmpty(item.getShnum()) && old.getShnum().equals(0)){
            old.setShnum(item.getShnum());
            flag = Boolean.TRUE;
        }
        if (!old.getPsry().equals(item.getPsry()) && !StringUtils.isEmpty(item.getPsry()) && old.getPsry().equals(0)){
            old.setPsry(item.getPsry());
            flag = Boolean.TRUE;
        }
        if (!old.getAzry().equals(item.getAzry()) && !StringUtils.isEmpty(item.getAzry()) && old.getAzry().equals(0)){
            old.setAzry(item.getAzry());
            flag = Boolean.TRUE;
        }
        if (!old.getWxry().equals(item.getWxry()) && !StringUtils.isEmpty(item.getWxry()) && old.getWxry().equals(0)){
            old.setWxry(item.getWxry());
            flag = Boolean.TRUE;
        }
        if (!old.getShqcnum().equals(item.getShqcnum()) && !StringUtils.isEmpty(item.getShqcnum()) && old.getShqcnum().equals(0)){
            old.setShqcnum(item.getShqcnum());
            flag = Boolean.TRUE;
        }
        if (!old.getRwxnum().equals(item.getRwxnum()) && !StringUtils.isEmpty(item.getRwxnum()) && old.getRwxnum().equals(0)){
            old.setRwxnum(item.getRwxnum());
            flag = Boolean.TRUE;
        }
        if (!old.getMrzgczl().equals(item.getMrzgczl()) && !StringUtils.isEmpty(item.getMrzgczl()) && old.getMrzgczl().equals(0)){
            old.setMrzgczl(item.getMrzgczl());
            flag = Boolean.TRUE;
        }
        if (!StringUtils.isEmpty(old.getCkmj()) || !StringUtils.isEmpty(item.getCkmj())){
            if (!StringUtils.isEmpty(old.getCkmj()) && !StringUtils.isEmpty(item.getCkmj())){
                if (!old.getCkmj().equals(item.getCkmj())){
                    old.setCkmj(item.getCkmj());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getCkmj())) {
                    old.setCkmj(item.getCkmj());
                } else {
                    old.setCkmj("");
                }
            }
        }

        // 省市区街道不修改
        /*
        if (!StringUtils.isEmpty(old.getSfen()) || !StringUtils.isEmpty(item.getSfen())){
            if (!StringUtils.isEmpty(old.getSfen()) && !StringUtils.isEmpty(item.getSfen())){
                if (!old.getSfen().equals(item.getSfen())){
                    old.setSfen(item.getSfen());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getSfen())) {
                    old.setSfen(item.getSfen());
                } else {
                    old.setSfen("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getCshi()) || !StringUtils.isEmpty(item.getCshi())){
            if (!StringUtils.isEmpty(old.getCshi()) && !StringUtils.isEmpty(item.getCshi())){
                if (!old.getCshi().equals(item.getCshi())){
                    old.setCshi(item.getCshi());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getCshi())) {
                    old.setCshi(item.getCshi());
                } else {
                    old.setCshi("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getXian()) || !StringUtils.isEmpty(item.getXian())){
            if (!StringUtils.isEmpty(old.getXian()) && !StringUtils.isEmpty(item.getXian())){
                if (!old.getXian().equals(item.getXian())){
                    old.setXian(item.getXian());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getXian())) {
                    old.setXian(item.getXian());
                } else {
                    old.setXian("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getXzhen()) || !StringUtils.isEmpty(item.getXzhen())){
            if (!StringUtils.isEmpty(old.getXzhen()) && !StringUtils.isEmpty(item.getXzhen())){
                if (!old.getXzhen().equals(item.getXzhen())){
                    old.setXzhen(item.getXzhen());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getXzhen())) {
                    old.setXzhen(item.getXzhen());
                } else {
                    old.setXzhen("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getDizi()) || !StringUtils.isEmpty(item.getDizi())){
            if (!StringUtils.isEmpty(old.getDizi()) && !StringUtils.isEmpty(item.getDizi())){
                if (!old.getDizi().equals(item.getDizi())){
                    old.setDizi(item.getDizi());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getDizi())) {
                    old.setDizi(item.getDizi());
                } else {
                    old.setDizi("");
                }
            }
        }
        */



        if ((item.getJingdu() !=null) && (old.getJingdu().compareTo(item.getJingdu()) !=0)){
            old.setJingdu(item.getJingdu());
            flag = Boolean.TRUE;
        }
        if ((item.getWeidu() !=null) && (old.getWeidu().compareTo(item.getWeidu()) !=0)){
            old.setWeidu(item.getWeidu());
            flag = Boolean.TRUE;
        }
        if (!StringUtils.isEmpty(old.getQuhao()) || !StringUtils.isEmpty(item.getQuhao())){
            if (!StringUtils.isEmpty(old.getQuhao()) && !StringUtils.isEmpty(item.getQuhao())){
                if (!old.getQuhao().equals(item.getQuhao())){
                    old.setQuhao(item.getQuhao());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getQuhao())) {
                    old.setQuhao(item.getQuhao());
                } else {
                    old.setQuhao("");
                }
            }
        }
        if (!StringUtils.isEmpty(old.getBeiz()) || !StringUtils.isEmpty(item.getBeiz())){
            if (!StringUtils.isEmpty(old.getBeiz()) && !StringUtils.isEmpty(item.getBeiz())){
                if (!old.getBeiz().equals(item.getBeiz())){
                    old.setBeiz(item.getBeiz());
                    flag = Boolean.TRUE;
                }
            } else {
                flag = Boolean.TRUE;
                if (!StringUtils.isEmpty(item.getBeiz())) {
                    old.setBeiz(item.getBeiz());
                } else {
                    old.setBeiz("");
                }
            }
        }
        if (flag){
            old.setUpdateBy("system");
            old.setUpdateTime(new Date());
            if (!StringUtils.isEmpty(item.getCzren())){
                old.setUpdateBy(item.getCzren());
            }
            if (!StringUtils.isEmpty(item.getZhczsj())){
                old.setUpdateTime(item.getZhczsj());
            }
            updateWebsitRec.add(old);
        }
    }

    private void addXinWdinfo(Brand record, String type, List<BrandWebsit> addWebsitRec, Map<String, String> oldSysDictRecMap, List<String> serviceList, Map<String, List<Belongcompany>> bcMap, ItfTblXinWangdianDownload item) {
        BrandWebsit v = new BrandWebsit();
        BeanUtils.copyProperties(item,v);

        v.setStat(BrandWebsitStatEnum.getMessageByCode(item.getStat()));
        if (InterFaceTypeEnum.SH_WEBSIT_TYPE.getCode().equals("S")){
            v.setFwlm("家电售后");
        }
        // 网点所属销售或售后公司编码赋值 这里可能是销售与可能是售后
        if (BeanUtil.isNotEmpty(item.getSsxsgs())){
            if (serviceList.contains(item.getSsxsgs())){
                v.setBelongCompanyId(bcMap.get(item.getSsxsgs()).get(0).getId());
            }
            v.setBelongCompany(item.getSsxsgs());
        }

        v.setId(publicService.getUUID());
        // 格力商户的网主键id
        v.setBrandPkId(item.getPkGuid());
        v.setSysWebsitNumber(item.getXtwdbh());
        v.setBrandWebsitNumber(item.getWdno());
        v.setBrandWebsitName(item.getWdmc());
        v.setGroupCompanyId(record.getBrandId());
        v.setGroupCompanyName(record.getBrand());
        v.setType(type);
        // 提取品类名称
        StringBuffer splbmc = getStringBuffer(oldSysDictRecMap, v.getSplb());

        v.setSplbmc(splbmc.toString());
        v.setCreateBy("system");
        v.setUpdateBy("system");
        v.setCreateTime(new Date());
        v.setUpdateTime(new Date());
        v.setSplb(","+ item.getSplb()+",");

        // 操作人
        if (!StringUtils.isEmpty(item.getCzren())){
            v.setCreateBy(item.getCzren());
            v.setUpdateBy(item.getCzren());
        }

        // 创建时间
        if (!StringUtils.isEmpty(item.getCjsj())){
            v.setCreateTime(item.getCjsj());
            v.setUpdateTime(item.getCjsj());
        }
        addWebsitRec.add(v);
    }

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

        // 提取现有的数据信息
        oldRec =  itfTblXinWangdianDownloadService.lambdaQuery().list();
        if (CollectionUtils.isNotEmpty(oldRec)){
            oldRecpkGuid = oldRec.stream().map(ItfTblXinWangdianDownload::getPkGuid).collect(Collectors.toList());
            oldRecMap = oldRec.stream().collect(Collectors.groupingBy(ItfTblXinWangdianDownload::getPkGuid));
        }

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject aData = jsonArray.getJSONObject(i);
            ItfTblXinWangdianDownload item = JSON.toJavaObject(aData,ItfTblXinWangdianDownload.class);

            // 新增或修改数据
            flag = Boolean.FALSE;
            // 修改
            if (oldRecpkGuid.contains(item.getPkGuid())){
                ItfTblXinWangdianDownload old = oldRecMap.get(item.getPkGuid()).get(0);
                if (!StringUtils.isEmpty(old.getFzren()) || !StringUtils.isEmpty(item.getFzren())){
                    if (!StringUtils.isEmpty(old.getFzren()) && !StringUtils.isEmpty(item.getFzren())){
                        if (!old.getFzren().equals(item.getFzren())){
                            old.setFzren(item.getFzren());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getFzren())) {
                            old.setFzren(item.getFzren());
                        } else {
                            old.setFzren("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getYddh()) || !StringUtils.isEmpty(item.getYddh())){
                    if (!StringUtils.isEmpty(old.getYddh()) && !StringUtils.isEmpty(item.getYddh())){
                        if (!old.getYddh().equals(item.getYddh())){
                            old.setYddh(item.getYddh());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getYddh())) {
                            old.setYddh(item.getYddh());
                        } else {
                            old.setYddh("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getDhhm()) || !StringUtils.isEmpty(item.getDhhm())){
                    if (!StringUtils.isEmpty(old.getDhhm()) && !StringUtils.isEmpty(item.getDhhm())){
                        if (!old.getDhhm().equals(item.getDhhm())){
                            old.setDhhm(item.getDhhm());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getDhhm())) {
                            old.setDhhm(item.getDhhm());
                        } else {
                            old.setDhhm("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getSfdlfr()) || !StringUtils.isEmpty(item.getSfdlfr())){
                    if (!StringUtils.isEmpty(old.getSfdlfr()) && !StringUtils.isEmpty(item.getSfdlfr())){
                        if (!old.getSfdlfr().equals(item.getSfdlfr())){
                            old.setSfdlfr(item.getSfdlfr());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getSfdlfr())) {
                            old.setSfdlfr(item.getSfdlfr());
                        } else {
                            old.setSfdlfr("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getLxren()) || !StringUtils.isEmpty(item.getLxren())){
                    if (!StringUtils.isEmpty(old.getLxren()) && !StringUtils.isEmpty(item.getLxren())){
                        if (!old.getLxren().equals(item.getLxren())){
                            old.setLxren(item.getLxren());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getLxren())) {
                            old.setLxren(item.getLxren());
                        } else {
                            old.setLxren("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getFzrsfz()) || !StringUtils.isEmpty(item.getFzrsfz())){
                    if (!StringUtils.isEmpty(old.getFzrsfz()) && !StringUtils.isEmpty(item.getFzrsfz())){
                        if (!old.getFzrsfz().equals(item.getFzrsfz())){
                            old.setFzrsfz(item.getFzrsfz());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getFzrsfz())) {
                            old.setFzrsfz(item.getFzrsfz());
                        } else {
                            old.setFzrsfz("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getShao()) || !StringUtils.isEmpty(item.getShao())){
                    if (!StringUtils.isEmpty(old.getShao()) && !StringUtils.isEmpty(item.getShao())){
                        if (!old.getShao().equals(item.getShao())){
                            old.setShao(item.getShao());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getShao())) {
                            old.setShao(item.getShao());
                        } else {
                            old.setShao("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getKhyh()) || !StringUtils.isEmpty(item.getKhyh())){
                    if (!StringUtils.isEmpty(old.getKhyh()) && !StringUtils.isEmpty(item.getKhyh())){
                        if (!old.getKhyh().equals(item.getKhyh())){
                            old.setKhyh(item.getKhyh());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getKhyh())) {
                            old.setKhyh(item.getKhyh());
                        } else {
                            old.setKhyh("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getHanghao()) || !StringUtils.isEmpty(item.getHanghao())){
                    if (!StringUtils.isEmpty(old.getHanghao()) && !StringUtils.isEmpty(item.getHanghao())){
                        if (!old.getHanghao().equals(item.getHanghao())){
                            old.setHanghao(item.getHanghao());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getHanghao())) {
                            old.setHanghao(item.getHanghao());
                        } else {
                            old.setHanghao("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getYhzh()) || !StringUtils.isEmpty(item.getYhzh())){
                    if (!StringUtils.isEmpty(old.getYhzh()) && !StringUtils.isEmpty(item.getYhzh())){
                        if (!old.getYhzh().equals(item.getYhzh())){
                            old.setYhzh(item.getYhzh());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getYhzh())) {
                            old.setYhzh(item.getYhzh());
                        } else {
                            old.setYhzh("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getSplb()) || !StringUtils.isEmpty(item.getSplb())){
                    if (!StringUtils.isEmpty(old.getSplb()) && !StringUtils.isEmpty(item.getSplb())){
                        if (!old.getSplb().equals(item.getSplb())){
                            old.setSplb(item.getSplb());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getSplb())) {
                            old.setSplb(item.getSplb());
                        } else {
                            old.setSplb("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getWdjb()) || !StringUtils.isEmpty(item.getWdjb())){
                    if (!StringUtils.isEmpty(old.getWdjb()) && !StringUtils.isEmpty(item.getWdjb())){
                        if (!old.getWdjb().equals(item.getWdjb())){
                            old.setWdjb(item.getWdjb());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getWdjb())) {
                            old.setWdjb(item.getWdjb());
                        } else {
                            old.setWdjb("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getSfdsqy()) || !StringUtils.isEmpty(item.getSfdsqy())){
                    if (!StringUtils.isEmpty(old.getSfdsqy()) && !StringUtils.isEmpty(item.getSfdsqy())){
                        if (!old.getSfdsqy().equals(item.getSfdsqy())){
                            old.setSfdsqy(item.getSfdsqy());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getSfdsqy())) {
                            old.setSfdsqy(item.getSfdsqy());
                        } else {
                            old.setSfdsqy("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getFwrx()) || !StringUtils.isEmpty(item.getFwrx())){
                    if (!StringUtils.isEmpty(old.getFwrx()) && !StringUtils.isEmpty(item.getFwrx())){
                        if (!old.getFwrx().equals(item.getFwrx())){
                            old.setFwrx(item.getFwrx());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getFwrx())) {
                            old.setFwrx(item.getFwrx());
                        } else {
                            old.setFwrx("");
                        }
                    }
                }
                if (!old.getStat().equals(item.getStat())){
                    old.setStat(item.getStat());
                    flag = Boolean.TRUE;
                }
                // /////////////
                if (!old.getXsry().equals(item.getXsry()) && !StringUtils.isEmpty(item.getXsry()) && old.getXsry().equals(0)){
                    old.setXsry(item.getXsry());
                    flag = Boolean.TRUE;
                }
                if (!old.getPsry().equals(item.getPsry()) && !StringUtils.isEmpty(item.getPsry()) && old.getPsry().equals(0)){
                    old.setPsry(item.getPsry());
                    flag = Boolean.TRUE;
                }
                if (!old.getAzry().equals(item.getAzry()) && !StringUtils.isEmpty(item.getAzry()) && old.getAzry().equals(0)){
                    old.setAzry(item.getAzry());
                    flag = Boolean.TRUE;
                }
                if (!old.getWxry().equals(item.getWxry()) && !StringUtils.isEmpty(item.getWxry()) && old.getWxry().equals(0)){
                    old.setWxry(item.getWxry());
                    flag = Boolean.TRUE;
                }
                if (!old.getJsry().equals(item.getJsry()) && !StringUtils.isEmpty(item.getJsry()) && old.getJsry().equals(0)){
                    old.setJsry(item.getJsry());
                    flag = Boolean.TRUE;
                }
                if (!old.getShnum().equals(item.getShnum()) && !StringUtils.isEmpty(item.getShnum()) && old.getShnum().equals(0)){
                    old.setShnum(item.getShnum());
                    flag = Boolean.TRUE;
                }
                if (!old.getShqcnum().equals(item.getShqcnum()) && !StringUtils.isEmpty(item.getShqcnum()) && old.getShqcnum().equals(0)){
                    old.setShqcnum(item.getShqcnum());
                    flag = Boolean.TRUE;
                }
                if (!old.getRaznum().equals(item.getRaznum()) && !StringUtils.isEmpty(item.getRaznum()) && old.getRaznum().equals(0)){
                    old.setRaznum(item.getRaznum());
                    flag = Boolean.TRUE;
                }
                if (!old.getRwxnum().equals(item.getRwxnum()) && !StringUtils.isEmpty(item.getRwxnum()) && old.getRwxnum().equals(0)){
                    old.setRwxnum(item.getRwxnum());
                    flag = Boolean.TRUE;
                }
                if (!old.getMrzgczl().equals(item.getMrzgczl()) && !StringUtils.isEmpty(item.getMrzgczl()) && old.getMrzgczl().equals(0)){
                    old.setMrzgczl(item.getMrzgczl());
                    flag = Boolean.TRUE;
                }
                if (!StringUtils.isEmpty(old.getCkmj()) || !StringUtils.isEmpty(item.getCkmj())){
                    if (!StringUtils.isEmpty(old.getCkmj()) && !StringUtils.isEmpty(item.getCkmj())){
                        if (!old.getCkmj().equals(item.getCkmj())){
                            old.setCkmj(item.getCkmj());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getCkmj())) {
                            old.setCkmj(item.getCkmj());
                        } else {
                            old.setCkmj("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getSfen()) || !StringUtils.isEmpty(item.getSfen())){
                    if (!StringUtils.isEmpty(old.getSfen()) && !StringUtils.isEmpty(item.getSfen())){
                        if (!old.getSfen().equals(item.getSfen())){
                            old.setSfen(item.getSfen());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getSfen())) {
                            old.setSfen(item.getSfen());
                        } else {
                            old.setSfen("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getCshi()) || !StringUtils.isEmpty(item.getCshi())){
                    if (!StringUtils.isEmpty(old.getCshi()) && !StringUtils.isEmpty(item.getCshi())){
                        if (!old.getCshi().equals(item.getCshi())){
                            old.setCshi(item.getCshi());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getCshi())) {
                            old.setCshi(item.getCshi());
                        } else {
                            old.setCshi("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getXian()) || !StringUtils.isEmpty(item.getXian())){
                    if (!StringUtils.isEmpty(old.getXian()) && !StringUtils.isEmpty(item.getXian())){
                        if (!old.getXian().equals(item.getXian())){
                            old.setXian(item.getXian());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getXian())) {
                            old.setXian(item.getXian());
                        } else {
                            old.setXian("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getXzhen()) || !StringUtils.isEmpty(item.getXzhen())){
                    if (!StringUtils.isEmpty(old.getXzhen()) && !StringUtils.isEmpty(item.getXzhen())){
                        if (!old.getXzhen().equals(item.getXzhen())){
                            old.setXzhen(item.getXzhen());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getXzhen())) {
                            old.setXzhen(item.getXzhen());
                        } else {
                            old.setXzhen("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getDizi()) || !StringUtils.isEmpty(item.getDizi())){
                    if (!StringUtils.isEmpty(old.getDizi()) && !StringUtils.isEmpty(item.getDizi())){
                        if (!old.getDizi().equals(item.getDizi())){
                            old.setDizi(item.getDizi());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getDizi())) {
                            old.setDizi(item.getDizi());
                        } else {
                            old.setDizi("");
                        }
                    }
                }

                if ((item.getJingdu() !=null) && (old.getJingdu().compareTo(item.getJingdu()) !=0)){
                    old.setJingdu(item.getJingdu());
                    flag = Boolean.TRUE;
                }
                if ((item.getWeidu() !=null) && (old.getWeidu().compareTo(item.getWeidu()) !=0)){
                    old.setWeidu(item.getWeidu());
                    flag = Boolean.TRUE;
                }

                if (!StringUtils.isEmpty(old.getQuhao()) || !StringUtils.isEmpty(item.getQuhao())){
                    if (!StringUtils.isEmpty(old.getQuhao()) && !StringUtils.isEmpty(item.getQuhao())){
                        if (!old.getQuhao().equals(item.getQuhao())){
                            old.setQuhao(item.getQuhao());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getQuhao())) {
                            old.setQuhao(item.getQuhao());
                        } else {
                            old.setQuhao("");
                        }
                    }
                }
                if (!StringUtils.isEmpty(old.getBeiz()) || !StringUtils.isEmpty(item.getBeiz())){
                    if (!StringUtils.isEmpty(old.getBeiz()) && !StringUtils.isEmpty(item.getBeiz())){
                        if (!old.getBeiz().equals(item.getBeiz())){
                            old.setBeiz(item.getBeiz());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (!StringUtils.isEmpty(item.getBeiz())) {
                            old.setBeiz(item.getBeiz());
                        } else {
                            old.setBeiz("");
                        }
                    }
                }
                if (flag){
                    old.setLastModifiedBy("system");
                    old.setLastModifiedDate(new Date());
                    // 操作人
                    if (!StringUtils.isEmpty(item.getLastModifiedBy())){
                        old.setLastModifiedBy(item.getLastModifiedBy());
                    }
                    // 修改时间
                    if (!StringUtils.isEmpty(item.getLastModifiedDate())){
                        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);
                addRec.add(item);
            }
        }

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

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


    private void extractedFSXSWDAddOrUpdate(List<ItfTblXinWangdianDownload> addRec, List<ItfTblXinWangdianDownload> updateRec, JSONArray jsonArray, String synTaskNo) {
        List<ItfTblXinWangdianDownload> oldRec =  new ArrayList<>();
        List<String> oldRecpkGuid = new ArrayList<>();
        Map<String, List<ItfTblXinWangdianDownload>> oldRecMap = new HashMap<>();

        // 提取现有的数据信息
        oldRec =  itfTblXinWangdianDownloadService.lambdaQuery().list();
        if (CollectionUtils.isNotEmpty(oldRec)){
            oldRecpkGuid = oldRec.stream().map(ItfTblXinWangdianDownload::getPkGuid).collect(Collectors.toList());
            oldRecMap = oldRec.stream().collect(Collectors.groupingBy(ItfTblXinWangdianDownload::getPkGuid));
        }

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject aData = jsonArray.getJSONObject(i);
            ItfTblXinWangdianDownload item = JSON.toJavaObject(aData,ItfTblXinWangdianDownload.class);

            /*if (item.getWdno().equals("S21A0365")){
                log.info("测试网点S21A0365 {}",JSONObject.toJSONString(aData));
            }*/


            // 修改
            if (oldRecpkGuid.contains(item.getPkGuid())){
                ItfTblXinWangdianDownload old = oldRecMap.get(item.getPkGuid()).get(0);
                item.setSynReqId(old.getSynReqId());

                item.setLastModifiedBy("system");
                item.setLastModifiedDate(new Date());
                // 操作人
                if (!StringUtils.isEmpty(item.getLastModifiedBy())){
                    item.setLastModifiedBy(item.getLastModifiedBy());
                }
                // 修改时间
                if (!StringUtils.isEmpty(item.getLastModifiedDate())){
                    item.setLastModifiedDate(item.getLastModifiedDate());
                }
                updateRec.add(item);
            } else {
                // 新增
                item.setSynTaskNo(synTaskNo);
                item.setSynReqTime(new Date());
                item.setSynStatus(1);
                item.setSynTime(new Date());
                item.setSynTimes(0);
                addRec.add(item);
            }
        }

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

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