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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zfire.jiasm.syncdata.constant.DictTypeEnum;
import com.zfire.jiasm.syncdata.plus.entity.*;
import com.zfire.jiasm.syncdata.plus.service.ItfTblAssignXqxlDowloadService;
import com.zfire.jiasm.syncdata.plus.service.ItfTblAssignXxlbDowloadService;
import com.zfire.jiasm.syncdata.plus.service.SysDictRefService;
import com.zfire.jiasm.syncdata.plus.service.SysDictService;
import com.zfire.jiasm.syncdata.service.GLPPXuQiuLeiBieService;
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.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GLPPXuQiuLeiBieServiceImpl implements GLPPXuQiuLeiBieService {
    @Resource
    ItfTblAssignXxlbDowloadService itfTblAssignXxlbDowloadService;
    @Resource
    SysDictService sysDictService;
    @Resource
    SysDictRefService sysDictRefService;
    @Resource
    ItfTblAssignXqxlDowloadService itfTblAssignXqxlDowloadService;
    @Resource
    private PublicService publicService;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void extractedXQDL(Brand brandRec, JSONArray jsonArray) {
        List<ItfTblAssignXxlbDowload> addRec = new ArrayList<>();
        List<ItfTblAssignXxlbDowload> updateRec = new ArrayList<>();

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

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void extractedXQXL(Brand brandRec,JSONArray jsonArray) {
        List<ItfTblAssignXqxlDowload> addRec = new ArrayList<>();
        List<ItfTblAssignXqxlDowload> updateRec = new ArrayList<>();

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

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

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


    private void extractedXQXLLocalRefAddOrUpdate(Brand brandRec, List<ItfTblAssignXqxlDowload> addRec, List<ItfTblAssignXqxlDowload> updateRec) {

        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_DEMAND_SMALL_TYPE.getCode()).list();
        Map<String, List<SysDictRef>> oldSysDictRefRecMap = oldSysDictRefRec.stream().collect(Collectors.groupingBy(SysDictRef::getDictCode));

        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblAssignXqxlDowload item : addRec){
                if (oldSysDictRefRecMap.containsKey(item.getId()) || dictcodes.contains(item.getId())){
                    upXqxlRef(updateSysDictRef, oldSysDictRefRecMap, item);
                } else {
                    addXqxlRef(brandRec, addSysDictRef, item);
                    dictcodes.add(String.valueOf(item.getId()));
                }
            }
        }
        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblAssignXqxlDowload item : updateRec){
                if (oldSysDictRefRecMap.containsKey(item.getId()) || dictcodes.contains(item.getId())){
                    upXqxlRef(updateSysDictRef, oldSysDictRefRecMap, item);
                } else {
                    addXqxlRef(brandRec, addSysDictRef, item);
                    dictcodes.add(String.valueOf(item.getId()));
                }
            }
        }

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

    private void upXqxlRef(List<SysDictRef> updateSysDictRef, Map<String, List<SysDictRef>> oldSysDictRefRecMap, ItfTblAssignXqxlDowload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDictRef v = oldSysDictRefRecMap.get(item.getId()).get(0);
        if (!v.getDictValue().equals(item.getLbmc())){
            v.setDictValue(item.getLbmc());
            flag = Boolean.TRUE;
        }
        if (!v.getRefDictName().equals(item.getLbmc())){
            v.setRefDictName(item.getLbmc());
            flag = Boolean.TRUE;
        }
        if (!v.getSortNum().equals(Integer.valueOf(item.getPxdj()))){
            v.setSortNum(Integer.valueOf(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 addXqxlRef(Brand brandRec, List<SysDictRef> addSysDictRef, ItfTblAssignXqxlDowload item) {
        SysDictRef v = new SysDictRef();
        v.setId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_DEMAND_SMALL_TYPE.getCode());
        v.setDictCode(item.getId());
        v.setDictValue(item.getLbmc());
        v.setSortNum(Integer.valueOf(item.getPxdj()));
        v.setBrandId(brandRec.getBrandId());
        v.setRefDictCode((item.getId()));
        v.setRefDictName(item.getLbmc());
        v.setCreateTime(new Date());
        v.setCreateBy("system");
        addSysDictRef.add(v);
    }

    private void extractedLocalAddOrUPdate(List<ItfTblAssignXqxlDowload> addRec, List<ItfTblAssignXqxlDowload> updateRec) {
        List<SysDict> addSysDict = new ArrayList<>();
        List<SysDict> updateSysDict = new ArrayList<>();
        List<String> dictcodes = new ArrayList<>();

        // 提取大类字典数据  sysDictService
        List<SysDict> oldSysDictRec =  sysDictService.lambdaQuery().eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_DEMAND_SMALL_TYPE.getCode()).list();
        Map<String, List<SysDict>> oldSysDictRecMap = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictCode));



        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblAssignXqxlDowload item : addRec){
                if (oldSysDictRecMap.containsKey(item.getId()) || dictcodes.contains(item.getId())){
                    upXqxl(updateSysDict, oldSysDictRecMap, item);
                } else {
                    addXqxl(addSysDict, item);
                    dictcodes.add(item.getId());
                }
            }
        }
        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblAssignXqxlDowload item : updateRec){
                if (oldSysDictRecMap.containsKey(item.getId()) || dictcodes.contains(item.getId())){
                    upXqxl(updateSysDict, oldSysDictRecMap, item);
                } else {
                    addXqxl(addSysDict, item);
                    dictcodes.add(item.getId());
                }
            }
        }

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

    private void upXqxl(List<SysDict> updateSysDict, Map<String, List<SysDict>> oldSysDictRecMap, ItfTblAssignXqxlDowload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDict v = oldSysDictRecMap.get(item.getId()).get(0);
        if (!v.getDictValue().equals(item.getLbmc())){
            v.setDictValue(item.getLbmc());
            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().equals(Integer.valueOf(item.getPxdj()))){
            v.setSortNum(Integer.valueOf(item.getPxdj()));
            flag = Boolean.TRUE;
        }
        if (!v.getParentDictCode().equals(item.getDaleiid())){
            v.setParentDictCode(item.getDaleiid());
            flag = Boolean.TRUE;
        }

        if (flag){
            updateSysDict.add(v);
        }
    }

    private void addXqxl(List<SysDict> addSysDict, ItfTblAssignXqxlDowload item) {
        SysDict v = new SysDict();
        v.setSysDictId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_DEMAND_SMALL_TYPE.getCode());
        v.setDictCode(item.getId());
        v.setDictValue(item.getLbmc());
        v.setSortNum(Integer.valueOf(item.getPxdj()));
        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.setParentDictCode(item.getDaleiid());
        v.setParentDictType(DictTypeEnum.DICT_TYPE_DEMAND_TYPE.getCode());
        v.setCreateTime(new Date());
        if (item.getCreatedDate() !=null){
            v.setCreateTime(item.getCreatedDate());
        }
        addSysDict.add(v);
    }

    private void extractedXQXLAddOrUpdate(JSONArray jsonArray, List<ItfTblAssignXqxlDowload> addRec, List<ItfTblAssignXqxlDowload> updateRec) {
        Boolean flag = Boolean.FALSE;
        String synTaskNo = publicService.getUUID();
        // 提取现有的数据信息
        List<ItfTblAssignXqxlDowload> oldRec =  itfTblAssignXqxlDowloadService.lambdaQuery().list();
        List<String> oldRecId = oldRec.stream().map(ItfTblAssignXqxlDowload::getId).collect(Collectors.toList());
        Map<String, List<ItfTblAssignXqxlDowload>> oldRecMap = oldRec.stream().collect(Collectors.groupingBy(ItfTblAssignXqxlDowload::getId));
        for (int i = 0; i < jsonArray.size(); i++) {
            flag = Boolean.FALSE;
            JSONObject aData = jsonArray.getJSONObject(i);
            ItfTblAssignXqxlDowload item = JSON.toJavaObject(aData,ItfTblAssignXqxlDowload.class);
            // 新增或修改数据

            // 修改
            if (oldRecId.contains(item.getId())){
                ItfTblAssignXqxlDowload old = oldRecMap.get(item.getId()).get(0);
                if (old.getLbno() !=null || item.getLbno() !=null){
                    if (old.getLbno() !=null && item.getLbno() !=null){
                        if (!old.getLbno().equals(item.getLbno())){
                            old.setLbno(item.getLbno());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getLbno() !=null) {
                            old.setLbno(item.getLbno());
                        } else {
                            old.setLbno("");
                        }
                    }
                }

                if (!old.getLbmc().equals(item.getLbmc())){
                    old.setLbmc(item.getLbmc());
                    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.getDaleiid().equals(item.getDaleiid())){
                    old.setDaleiid(item.getDaleiid());
                    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);
                if (item.getCreatedBy() ==null){
                    item.setCreatedBy("system");
                }
                if (item.getCreatedDate() == null){
                    item.setCreatedDate(new Date());
                }
                addRec.add(item);
            }
        }
        // 新增的插入到表
        if (addRec.size()>0){
            itfTblAssignXqxlDowloadService.saveBatch(addRec);
            log.info("格力品牌需求小类信息同步新增插入了{}条数据", addRec.size());
        }

        // 更新
        if (updateRec.size()>0){
            itfTblAssignXqxlDowloadService.updateBatchById(updateRec);
            log.info("格力品牌需求小类信息同步更新修改了{}条数据", updateRec.size());
        }
    }

    private void extractedLocalRefAddOrUpdate(Brand brandRec, List<ItfTblAssignXxlbDowload> addRec, List<ItfTblAssignXxlbDowload> updateRec) {
        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_DEMAND_TYPE.getCode()).list();
        Map<String, List<SysDictRef>> oldSysDictRefRecMap = oldSysDictRefRec.stream().collect(Collectors.groupingBy(SysDictRef::getDictCode));

        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblAssignXxlbDowload item : addRec){
                if (oldSysDictRefRecMap.containsKey(String.valueOf(item.getLbid())) ||
                        dictcodes.contains(String.valueOf(item.getLbid()))){
                    upXxlRef(updateSysDictRef, oldSysDictRefRecMap, item);
                } else {
                    addXxlbRef(brandRec, addSysDictRef, item);
                    dictcodes.add(String.valueOf(item.getLbid()));
                }
            }
        }
        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblAssignXxlbDowload item : updateRec){
                if (oldSysDictRefRecMap.containsKey(String.valueOf(item.getLbid())) ||
                        dictcodes.contains(String.valueOf(item.getLbid()))){
                    upXxlRef(updateSysDictRef, oldSysDictRefRecMap, item);
                } else {
                    addXxlbRef(brandRec, addSysDictRef, item);
                    dictcodes.add(String.valueOf(item.getLbid()));
                }
            }
        }

        // 新增的插入到表
        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 upXxlRef(List<SysDictRef> updateSysDictRef, Map<String, List<SysDictRef>> oldSysDictRefRecMap, ItfTblAssignXxlbDowload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDictRef v = oldSysDictRefRecMap.get(String.valueOf(item.getLbid())).get(0);
        if (!v.getDictValue().equals(item.getLbmc())){
            v.setDictValue(item.getLbmc());
            flag = Boolean.TRUE;
        }
        if (!v.getRefDictName().equals(item.getLbmc())){
            v.setRefDictName(item.getLbmc());
            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 addXxlbRef(Brand brandRec, List<SysDictRef> addSysDictRef, ItfTblAssignXxlbDowload item) {
        SysDictRef v = new SysDictRef();
        v.setId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_DEMAND_TYPE.getCode());
        v.setDictCode(String.valueOf(item.getLbid()));
        v.setDictValue(item.getLbmc());
        v.setSortNum(item.getPxdj());
        v.setBrandId(brandRec.getBrandId());
        v.setRefDictCode(String.valueOf(item.getLbid()));
        v.setRefDictName(item.getLbmc());
        v.setCreateTime(new Date());
        v.setCreateBy("system");
        addSysDictRef.add(v);
    }

    private void extractedLocalAddOrUpdate(List<ItfTblAssignXxlbDowload> addRec, List<ItfTblAssignXxlbDowload> updateRec) {
        List<SysDict> addSysDict = new ArrayList<>();
        List<SysDict> updateSysDict = new ArrayList<>();
        List<String> dictcodes = new ArrayList<>();

        // 提取大类字典数据  sysDictService
        List<SysDict> oldSysDictRec =  sysDictService.lambdaQuery().eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_DEMAND_TYPE.getCode()).list();
        Map<String, List<SysDict>> oldSysDictRecMap = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictCode));

        // 获取新增的
        if (addRec.size()>0){
            for (ItfTblAssignXxlbDowload item : addRec){
                if (oldSysDictRecMap.containsKey(String.valueOf(item.getLbid())) ||
                        dictcodes.contains(String.valueOf(item.getLbid()))){
                    upXxlb(updateSysDict, oldSysDictRecMap, item);
                } else {
                    addXxlb(addSysDict, item);
                    dictcodes.add(String.valueOf(item.getLbid()));
                }
            }
        }
        // 获取修改的
        if (updateRec.size()>0){
            for (ItfTblAssignXxlbDowload item : updateRec){
                if (oldSysDictRecMap.containsKey(String.valueOf(item.getLbid())) ||
                        dictcodes.contains(String.valueOf(item.getLbid()))){
                    upXxlb(updateSysDict, oldSysDictRecMap, item);
                } else {
                    addXxlb(addSysDict, item);
                    dictcodes.add(String.valueOf(item.getLbid()));
                }
            }
        }

        // 新增的插入到表
        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 upXxlb(List<SysDict> updateSysDict, Map<String, List<SysDict>> oldSysDictRecMap, ItfTblAssignXxlbDowload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDict v = oldSysDictRecMap.get(String.valueOf(item.getLbid())).get(0);
        if (!v.getDictValue().equals(item.getLbmc())){
            v.setDictValue(item.getLbmc());
            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 addXxlb(List<SysDict> addSysDict, ItfTblAssignXxlbDowload item) {
        SysDict v = new SysDict();
        v.setSysDictId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_DEMAND_TYPE.getCode());
        v.setDictCode(String.valueOf(item.getLbid()));
        v.setDictValue(item.getLbmc());
        v.setSortNum(item.getPxdj());
        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.setCreateTime(new Date());
        if (item.getCreatedDate() !=null){
            v.setCreateTime(item.getCreatedDate());
        }
        addSysDict.add(v);
    }

    private void extractedXQLBAddOrUpdate(JSONArray jsonArray, List<ItfTblAssignXxlbDowload> addRec, List<ItfTblAssignXxlbDowload> updateRec) {
        Boolean flag = Boolean.FALSE;
        String synTaskNo = publicService.getUUID();
        // 提取现有的数据信息
        List<ItfTblAssignXxlbDowload> oldRec =  itfTblAssignXxlbDowloadService.lambdaQuery().list();
        List<Long> oldRecLbId = oldRec.stream().map(ItfTblAssignXxlbDowload::getLbid).collect(Collectors.toList());
        Map<Long, List<ItfTblAssignXxlbDowload>> oldRecMap = oldRec.stream().collect(Collectors.groupingBy(ItfTblAssignXxlbDowload::getLbid));
        for (int i = 0; i < jsonArray.size(); i++) {
            flag = Boolean.FALSE;
            JSONObject aData = jsonArray.getJSONObject(i);
            ItfTblAssignXxlbDowload item = JSON.toJavaObject(aData,ItfTblAssignXxlbDowload.class);
            // 新增或修改数据

            // 修改
            if (oldRecLbId.contains(item.getLbid())){
                ItfTblAssignXxlbDowload old = oldRecMap.get(item.getLbid()).get(0);
                if (old.getLbno() !=null || item.getLbno() !=null){
                    if (old.getLbno() !=null && item.getLbno() !=null){
                        if (!old.getLbno().equals(item.getLbno())){
                            old.setLbno(item.getLbno());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getLbno() !=null) {
                            old.setLbno(item.getLbno());
                        } else {
                            old.setLbno("");
                        }
                    }
                }

                if (!old.getLbmc().equals(item.getLbmc())){
                    old.setLbmc(item.getLbmc());
                    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);
                if (item.getCreatedBy() ==null){
                    item.setCreatedBy("system");
                }
                if (item.getCreatedDate() == null){
                    item.setCreatedDate(new Date());
                }
                addRec.add(item);
            }
        }
        // 新增的插入到表
        if (addRec.size()>0){
            itfTblAssignXxlbDowloadService.saveBatch(addRec);
            log.info("格力品牌需求大类信息同步新增插入了{}条数据", addRec.size());
        }

        // 更新
        if (updateRec.size()>0){
            itfTblAssignXxlbDowloadService.updateBatchById(updateRec);
            log.info("格力品牌需求大类信息同步更新修改了{}条数据", updateRec.size());
        }
    }



}
