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.*;
import com.zfire.jiasm.syncdata.service.GLPPYHSXXXQDService;
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 GLPPYHSXXXQDServiceImpl implements GLPPYHSXXXQDService {
    @Resource
    SysDictService sysDictService;
    @Resource
    ItfTblAssignYhsxWxDowloadService itfTblAssignYhsxWxDowloadService;
    @Resource
    ItfTblAssignXxqdDowloadService itfTblAssignXxqdDowloadService;
    @Resource
    ItfTblAssignXxlyDowloadService itfTblAssignXxlyDowloadService;
    @Resource
    ItfTblYhsxViprankDowloadService itfTblYhsxViprankDowloadService;
    @Resource
    private PublicService publicService;



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

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

        // 数据同步到本地字典表
        extractedLocalYHSXAddOrUpdate(addRec, updateRec);
        addRec.clear();
        updateRec.clear();
    }

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

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

        // 数据同步到本地字典表
        extractedLocalYHDJAddOrUpdate(addRec, updateRec);
        addRec.clear();
        updateRec.clear();
    }

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

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

        // 数据同步到本地字典表
        extractedLocalXXQDAddOrUpdate(addRec, updateRec);
        addRec.clear();
        updateRec.clear();
    }

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

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

        // 数据同步到本地字典表
        extractedLocalXXLYAddOrUpdate(addRec, updateRec);
        addRec.clear();
        updateRec.clear();
    }



    private void extractedLocalYHDJAddOrUpdate(List<ItfTblYhsxViprankDowload> addRec, List<ItfTblYhsxViprankDowload> 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_USER_LEVEL.getCode()).list();
        Map<String, List<SysDict>> oldSysDictRecMap = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictCode));


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

        // 新增的插入到表
        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 upYhsxVip(List<SysDict> updateSysDict, Map<String, List<SysDict>> oldSysDictRecMap, ItfTblYhsxViprankDowload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDict v = oldSysDictRecMap.get(String.valueOf(item.getVipid())).get(0);
        if (!v.getDictValue().equals(item.getVipmc())){
            v.setDictValue(item.getVipmc());
            flag = Boolean.TRUE;
        }
        if (v.getSortNum() != item.getPxdj()){
            v.setSortNum(item.getPxdj());
            flag = Boolean.TRUE;
        }
        if (flag){
            updateSysDict.add(v);
        }
    }

    private void addYhsxWip(List<SysDict> addSysDict, ItfTblYhsxViprankDowload item) {
        SysDict v = new SysDict();
        v.setSysDictId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_USER_LEVEL.getCode());
        v.setDictCode(String.valueOf(item.getVipid()));
        v.setDictValue(item.getVipmc());
        v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
        v.setSortNum(item.getPxdj());
        v.setCreateTime(new Date());
        if (item.getCjdt() !=null){
            v.setCreateTime(item.getCjdt());
        }
        addSysDict.add(v);
    }

    private void extractedYHDJAddOrUpdate(JSONArray jsonArray, List<ItfTblYhsxViprankDowload> addRec, List<ItfTblYhsxViprankDowload> updateRec) {
        Boolean flag = Boolean.FALSE;
        String synTaskNo = publicService.getUUID();
        // 提取现有的数据信息
        List<ItfTblYhsxViprankDowload> oldRec =  itfTblYhsxViprankDowloadService.lambdaQuery().list();
        List<Long> oldRecVipId = oldRec.stream().map(ItfTblYhsxViprankDowload::getVipid).collect(Collectors.toList());
        Map<Long, List<ItfTblYhsxViprankDowload>> oldRecMap = oldRec.stream().collect(Collectors.groupingBy(ItfTblYhsxViprankDowload::getVipid));
        for (int i = 0; i < jsonArray.size(); i++) {
            flag = Boolean.FALSE;
            JSONObject aData = jsonArray.getJSONObject(i);
            ItfTblYhsxViprankDowload item = JSON.toJavaObject(aData,ItfTblYhsxViprankDowload.class);
            // 新增或修改数据
            // 修改
            if (oldRecVipId.contains(item.getVipid().longValue())){
                ItfTblYhsxViprankDowload old = oldRecMap.get(item.getVipid()).get(0);
                if (!old.getVipmc().equals(item.getVipmc())){
                    old.setVipmc(item.getVipmc());
                    flag = Boolean.TRUE;
                }
                if (!old.getPxdj().equals(item.getPxdj())){
                    old.setPxdj(item.getPxdj());
                    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){
            itfTblYhsxViprankDowloadService.saveBatch(addRec);
            log.info("格力品牌用户等级信息同步新增插入了{}条数据", addRec.size());
        }

        // 更新
        if (updateRec.size()>0){
            itfTblYhsxViprankDowloadService.updateBatchById(updateRec);
            log.info("格力品牌用户等级信息同步更新修改了{}条数据", updateRec.size());
        }
    }

    private void extractedLocalXXLYAddOrUpdate(List<ItfTblAssignXxlyDowload> addRec, List<ItfTblAssignXxlyDowload> 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_XXQD_SOURCE.getCode()).list();
        Map<String, List<SysDict>> oldSysDictRecMap = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictCode));


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

        // 新增的插入到表
        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 upXxly(List<SysDict> updateSysDict, Map<String, List<SysDict>> oldSysDictRecMap, ItfTblAssignXxlyDowload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDict v = oldSysDictRecMap.get(String.valueOf(item.getLyid())).get(0);
        if (!v.getDictValue().equals(item.getLymc())){
            v.setDictValue(item.getLymc());
            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 addXxly(List<SysDict> addSysDict, ItfTblAssignXxlyDowload item) {
        SysDict v = new SysDict();
        v.setSysDictId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_XXQD_SOURCE.getCode());
        v.setDictCode(String.valueOf(item.getLyid()));
        v.setDictValue(item.getLymc());
        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.setCreateTime(new Date());
        if (item.getCjdt() !=null){
            v.setCreateTime(item.getCjdt());
        }
        addSysDict.add(v);
    }

    private void extractedXXLYAddOrUpdate(JSONArray jsonArray, List<ItfTblAssignXxlyDowload> addRec, List<ItfTblAssignXxlyDowload> updateRec) {
        Boolean flag = Boolean.FALSE;
        String synTaskNo = publicService.getUUID();
        // 提取现有的数据信息
        List<ItfTblAssignXxlyDowload> oldRec =  itfTblAssignXxlyDowloadService.lambdaQuery().list();
        List<Long> oldRecQdId = oldRec.stream().map(ItfTblAssignXxlyDowload::getLyid).collect(Collectors.toList());
        Map<Long, List<ItfTblAssignXxlyDowload>> oldRecMap = oldRec.stream().collect(Collectors.groupingBy(ItfTblAssignXxlyDowload::getLyid));
        for (int i = 0; i < jsonArray.size(); i++) {
            flag = Boolean.FALSE;
            JSONObject aData = jsonArray.getJSONObject(i);
            ItfTblAssignXxlyDowload item = JSON.toJavaObject(aData,ItfTblAssignXxlyDowload.class);
            // 新增或修改数据
            // 修改
            if (oldRecQdId.contains(item.getLyid())){
                ItfTblAssignXxlyDowload old = oldRecMap.get(item.getLyid()).get(0);
                if (old.getLyno() !=null || item.getLyno() !=null){
                    if (old.getLyno() !=null && item.getLyno() !=null){
                        if (!old.getLyno().equals(item.getLyno())){
                            old.setLyno(item.getLyno());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getLyno() !=null) {
                            old.setLyno(item.getLyno());
                        } else {
                            old.setLyno("");
                        }
                    }
                }

                if (!old.getLymc().equals(item.getLymc())){
                    old.setLymc(item.getLymc());
                    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){
            itfTblAssignXxlyDowloadService.saveBatch(addRec);
            log.info("格力品牌信息来源信息同步新增插入了{}条数据", addRec.size());
        }

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

    private void extractedLocalXXQDAddOrUpdate(List<ItfTblAssignXxqdDowload> addRec, List<ItfTblAssignXxqdDowload> 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_XXQD_CHANNEL.getCode()).list();
        Map<String, List<SysDict>> oldSysDictRecMap = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictCode));

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

        // 新增的插入到表
        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 upXxqd(List<SysDict> updateSysDict, Map<String, List<SysDict>> oldSysDictRecMap, ItfTblAssignXxqdDowload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDict v = oldSysDictRecMap.get(String.valueOf(item.getQdid())).get(0);
        if (!v.getDictValue().equals(item.getQdmc())){
            v.setDictValue(item.getQdmc());
            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 addXxqd(List<SysDict> addSysDict, ItfTblAssignXxqdDowload item) {
        SysDict v = new SysDict();
        v.setSysDictId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_XXQD_CHANNEL.getCode());
        v.setDictCode(String.valueOf(item.getQdid()));
        v.setDictValue(item.getQdmc());
        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.setCreateTime(new Date());
        if (item.getCjdt() !=null){
            v.setCreateTime(item.getCjdt());
        }
        addSysDict.add(v);
    }

    private void extractedXXQDAddOrUpdate(JSONArray jsonArray, List<ItfTblAssignXxqdDowload> addRec, List<ItfTblAssignXxqdDowload> updateRec) {
        Boolean flag = Boolean.FALSE;
        String synTaskNo = publicService.getUUID();
        // 提取现有的数据信息
        List<ItfTblAssignXxqdDowload> oldRec =  itfTblAssignXxqdDowloadService.lambdaQuery().list();
        List<Long> oldRecQdId = oldRec.stream().map(ItfTblAssignXxqdDowload::getQdid).collect(Collectors.toList());
        Map<Long, List<ItfTblAssignXxqdDowload>> oldRecMap = oldRec.stream().collect(Collectors.groupingBy(ItfTblAssignXxqdDowload::getQdid));
        for (int i = 0; i < jsonArray.size(); i++) {
            flag = Boolean.FALSE;
            JSONObject aData = jsonArray.getJSONObject(i);
            ItfTblAssignXxqdDowload item = JSON.toJavaObject(aData,ItfTblAssignXxqdDowload.class);
            // 新增或修改数据
            // 修改
            if (oldRecQdId.contains(item.getQdid())){
                ItfTblAssignXxqdDowload old = oldRecMap.get(item.getQdid()).get(0);
                if (old.getQdno() !=null || item.getQdno() !=null){
                    if (old.getQdno() !=null && item.getQdno() !=null){
                        if (!old.getQdno().equals(item.getQdno())){
                            old.setQdno(item.getQdno());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getQdno() !=null) {
                            old.setQdno(item.getQdno());
                        } else {
                            old.setQdno("");
                        }
                    }
                }

                if (!old.getQdmc().equals(item.getQdmc())){
                    old.setQdmc(item.getQdmc());
                    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){
            itfTblAssignXxqdDowloadService.saveBatch(addRec);
            log.info("格力品牌信息渠道信息同步新增插入了{}条数据", addRec.size());
        }

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


    private void extractedLocalYHSXAddOrUpdate(List<ItfTblAssignYhsxWxDowload> addRec, List<ItfTblAssignYhsxWxDowload> 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_USER_ATTR.getCode()).list();
        Map<String, List<SysDict>> oldSysDictRecMap = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictCode));


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

        // 新增的插入到表
        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 upYhsxWx(List<SysDict> updateSysDict, Map<String, List<SysDict>> oldSysDictRecMap, ItfTblAssignYhsxWxDowload item) {
        Boolean flag = Boolean.FALSE;
        flag = Boolean.FALSE;
        SysDict v = oldSysDictRecMap.get(String.valueOf(item.getYhsxid())).get(0);
        if (!v.getDictValue().equals(item.getYhsxmc())){
            v.setDictValue(item.getYhsxmc());
            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 addYhsxWx(List<SysDict> addSysDict, ItfTblAssignYhsxWxDowload item) {
        SysDict v = new SysDict();
        v.setSysDictId(publicService.getUUID());
        v.setDictType(DictTypeEnum.DICT_TYPE_USER_ATTR.getCode());
        v.setDictCode(String.valueOf(item.getYhsxid()));
        v.setDictValue(item.getYhsxmc());
        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.setCreateTime(new Date());
        if (item.getCjdt() !=null){
            v.setCreateTime(item.getCjdt());
        }
        addSysDict.add(v);
    }

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

            // 修改
            if (oldRecYhsxId.contains(item.getYhsxid())){
                ItfTblAssignYhsxWxDowload old = oldRecMap.get(item.getYhsxid()).get(0);
                if (!old.getYhsxmc().equals(item.getYhsxmc())){
                    old.setYhsxmc(item.getYhsxmc());
                    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.getYhsxmcvl() !=null || item.getYhsxmcvl() !=null){
                    if (old.getYhsxmcvl() !=null && item.getYhsxmcvl() !=null){
                        if (!old.getYhsxmcvl().equals(item.getYhsxmcvl())){
                            old.setYhsxmcvl(item.getYhsxmcvl());
                            flag = Boolean.TRUE;
                        }
                    } else {
                        flag = Boolean.TRUE;
                        if (item.getYhsxmcvl() !=null) {
                            old.setYhsxmcvl(item.getYhsxmcvl());
                        } else {
                            old.setYhsxmcvl("");
                        }
                    }
                }
                if (!old.getIfdx().equals(item.getIfdx())){
                    old.setIfdx(item.getIfdx());
                    flag = Boolean.TRUE;
                }
                if (!old.getRols().equals(item.getRols())){
                    old.setRols(item.getRols());
                    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){
            itfTblAssignYhsxWxDowloadService.saveBatch(addRec);
            log.info("格力品牌用户属性信息同步新增插入了{}条数据", addRec.size());
        }

        // 更新
        if (updateRec.size()>0){
            itfTblAssignYhsxWxDowloadService.updateBatchById(updateRec);
            log.info("格力品牌用户属性信息同步更新修改了{}条数据", updateRec.size());
        }
    }





}
