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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zfire.jiasm.syncdata.base.BaseService;
import com.zfire.jiasm.syncdata.constant.DictTypeEnum;
import com.zfire.jiasm.syncdata.data.Token;
import com.zfire.jiasm.syncdata.plus.entity.*;
import com.zfire.jiasm.syncdata.plus.service.*;
import com.zfire.jiasm.syncdata.service.MasterDownloadService;
import com.zfire.jiasm.syncdata.service.PublicService;
import com.zfire.jiasm.syncdata.utils.DataUtil;
import com.zfire.jiasm.syncdata.utils.HttpUtil;
import com.zfire.jiasm.syncdata.utils.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

import static jdk.nashorn.internal.objects.NativeString.trim;

@Slf4j
@Service
public class MasterDownloadServiceImpl implements MasterDownloadService {
	@Value("${masterbaseurl}")
	private String masterbaseurl = "";

	@Resource
	ItfTblWxCpgzDownloadService itfTblWxCpgzDownloadService;
	@Resource
	ItfTblWxGzdmWxxmjcDownloadService itfTblWxGzdmWxxmjcDownloadService;
	@Resource
	ItfTblWxWxxmjcDownloadService itfTblWxWxxmjcDownloadService;
	@Resource
	RepairBugAppService repairBugAppService;
	@Resource
	RepairProjectService repairProjectService;
	@Resource
	ItfJhAzdCauseSortNewDownloadService itfJhAzdCauseSortNewDownloadService;
	@Resource
	ItfTblWxCpgzXjdDownloadService itfTblWxCpgzXjdDownloadService;
	@Resource
	ItfJhAzdCauseAtterDownloadService itfJhAzdCauseAtterDownloadService;
	@Resource
	ItfTblWxWxxmXjdDownloadService itfTblWxWxxmXjdDownloadService;
	@Resource
	RepairProjectTypeService repairProjectTypeService;
	@Resource
	ItfGreeToFaDownloadTaskService itfGreeToFaDownloadTaskService;
	@Resource
	SysDictService sysDictService;
	@Resource
	ItfGreeToFaSynTaskListService itfGreeToFaSynTaskListService;
	@Resource
	BaseService baseService;
	@Resource
	private PublicService publicService;


	/**
	 * 落本地表
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchDownload(Token token, String[] arySrcTabName,String[] aryDestTabName,
							  String belongCompany, int reqId, Integer downloadTaskType) throws Exception {
		String synTaskNo = publicService.getUUID();
		for (int i = 0; i < arySrcTabName.length; i++) {
			String srcTabName = arySrcTabName[i].trim();
			String serviceName = DataUtil.fromDbName2ServiceName(srcTabName);
			String url = masterbaseurl + serviceName + "/querylist";
			Map<String, String> paramMap = new HashMap<String, String>();
			if(belongCompany!=null && belongCompany.trim().length()>0) {
				paramMap.put("belongCompany", belongCompany);
			}
			String paramJson= JSON.toJSONString(paramMap);
			JSONResult jsonResult = HttpUtil.httpRequestWithToken(url, "POST", paramJson, token);
			JSONArray jsonArray = (JSONArray) jsonResult.getData();
			if (jsonArray != null) {
				log.info("["+url+"]下载了["+jsonArray.size()+"]条数据");

				masterProcess(aryDestTabName, synTaskNo, i, jsonArray, reqId);

				log.info("["+url+"]插入了["+jsonArray.size()+"]条数据");
			}else {
				log.info("["+url+"]无数据");
			}
		}

		// 插入新数据进行下步处理，修改同步状态不再同步
		downFinish(reqId, downloadTaskType, synTaskNo);
	}



	private void downFinish(int reqId, Integer downloadTaskType, String synTaskNo) {
		ItfGreeToFaSynTaskList a = new ItfGreeToFaSynTaskList();
		a.setSynTaskNo(synTaskNo);
		a.setSynTaskType(downloadTaskType);
		itfGreeToFaSynTaskListService.save(a);

		ItfGreeToFaDownloadTask up = new ItfGreeToFaDownloadTask();
		up.setId(reqId);
		up.setNeedDownload(0);
		up.setLastDownloadTime(new Date());
		itfGreeToFaDownloadTaskService.updateById(up);
	}

	private void processDelete(Brand brandRec) {
		QueryWrapper del = new QueryWrapper<RepairBugApp>()
				.eq("brand_id", brandRec.getBrandId());
		repairBugAppService.remove(del);

		QueryWrapper delt = new QueryWrapper<RepairProject>()
				.eq("brand_id", brandRec.getBrandId());
		repairProjectService.remove(delt);
	}

	public void masterProcess(String[] aryDestTabName, String synTaskNo, int i, JSONArray jsonArray,int reqId) {
		if (aryDestTabName[i].equals("itf_tbl_wx_cpgz_download")){
			// 维修－产品故障基表下载记录
			addItfTblWxCpgzDownload(synTaskNo, jsonArray);
		} else if (aryDestTabName[i].equals("itf_tbl_wx_gzdm_wxxmjc_download")){
			// 故障代码与维修项目基础表下载记录
			addItfTblWxGzdmWxxmjcDownload(synTaskNo, jsonArray);
		} else if (aryDestTabName[i].equals("itf_tbl_wx_wxxmjc_download")){
			// 维修项目基础表下载记录
			addItfTblWxWxxmjcDownload(synTaskNo, jsonArray);
		} else if (aryDestTabName[i].equals("itf_jh_azd_cause_sort_new_download")){
			// 晶弘冰箱故障代码基础表下载记录
			addItfJhAzdCauseSortNewDownload(synTaskNo, jsonArray);
		} else if (aryDestTabName[i].equals("itf_jh_azd_cause_atter_download")){
			// 晶弘冰箱维修项目基础表下载记录
			addItfJhAzdCauseAtterDownload(synTaskNo, jsonArray);
		} else if (aryDestTabName[i].equals("itf_tbl_wx_cpgz_xjd_download")){
			// 小家电故障现象基础表下载记录
			addItfTblWxCpgzXjdDownload(synTaskNo, jsonArray);
		} else if (aryDestTabName[i].equals("itf_tbl_wx_wxxm_xjd_download")){
			// 小家电维修项目基础表下载记录
			addItfTblWxWxxmXjdDownload(synTaskNo, jsonArray);
		}
	}



	@Override
	@Transactional(rollbackFor = Exception.class)
	public void masterProcessData(String synTaskNo,Integer id) {
		// 提取品牌信息
		Brand brandRec = baseService.getBrand();
		if (brandRec == null){
			log.info("格力品牌信息不存在【brand】");
			return;
		}

		//  清除 维修故障现象表、维修项目表
		processDelete(brandRec);
		// 处理空调
		// 生成故障现象数据(repair_bug) 故障现象类型为家用空调及商用空调，通用的分两部分家用与商用 1-2
		// 进行了大小类处理，但同步过来的数据并无大小类数据     OK
		addItfTblWxCpgzDownloadOld(synTaskNo, brandRec);

		// 维修项目分类信息表 添加一条 家用空调
		addRepairProjectType(1, "家用空调");

		// 最后放在这里
		// 维修项目基础数据处理 生成维修项目分类表数据(取xmfl)-维修项目(商用空调)
		// 维修项目分组取
		// 无大小类信息
		// 无故障代码 有维修项目(商用空调)
		// 用到了维修项目分类表ID已处理                                       OK
		addItfTblWxWxxmjcDownloadOld(synTaskNo,brandRec);


		// 生成故障代码与维修项目对应关系数据
		// 故障代码与维修项目有(商用空调｜家用空调)
		// 用到了维修项目分类表ID已处理      维修项目分类表上面只加了一条家用的，并无商用的有问题
		addItfTblWxGzdmWxxmjcDownloadOld(synTaskNo, brandRec);

		// 处理冰箱 维修项目分类信息表 添加一条 晶弘冰箱
		addRepairProjectType(4, "晶弘冰箱");

		// 生成晶弘冰箱故障代码数据(repair_bug)
		// 无大小类信息，故障现象类型为晶弘冰箱  4
		// 分两个层级，有故障名称 第二层给到故障代码
		// 这里的故障代码都会在晶弘冰箱维修项目数据中存在                        OK
		addItfJhAzdCauseSortNewDownloadOld(synTaskNo, brandRec);
		// 生成晶弘冰箱维修项目数据
		// (repair_project)   有故障代码 维修项目(晶弘冰箱)
		// 用到了维修项目分类表ID已处理                                       OK
		addItfJhAzdCauseAtterDownloadOld(synTaskNo, brandRec);

		// 处理小家电 维修项目分类信息表 添加一条 生活电器
		addRepairProjectType(3, "生活电器");

		// 小家电故障现象处理
		//  故障代码只放入了大类    故障现象类型为生活电器  3
		//  第一级为大类,故障现象从第二节开始获取 未级有故障编码                OK
		addItfTblWxCpgzXjdDownloadOld(synTaskNo,brandRec);
		// 小家电维修项目数据处理
		// 这里给了小类信息  用到了维修项目分类表ID已处理
		// 无故障代码  有维修项目(生活电器)                                   OK
		addItfTblWxWxxmXjdDownloadOld(synTaskNo, brandRec);

		// 修改同步状态
		ItfGreeToFaSynTaskList record =  new ItfGreeToFaSynTaskList();
		record.setId(id);
		record.setSynStatus(1);
		record.setSynTime(new Date());
		itfGreeToFaSynTaskListService.updateById(record);
	}


	public void addItfTblWxCpgzDownload(String synTaskNo, JSONArray jsonArray){
		List<ItfTblWxCpgzDownload> addRec = new ArrayList<>();
		getItfTblWxCpgzDownloadT(synTaskNo, jsonArray, addRec);
		// 新增
		if (addRec.size()>0){
			itfTblWxCpgzDownloadService.saveBatch(addRec);
			log.info("产品故障基表信息同步新增插入了{}条数据", addRec.size());
			addRec.clear();
		}
	}

	private void addItfTblWxCpgzDownloadOld(String synTaskNo, Brand brandRec){

		List<ItfTblWxCpgzDownload> addRec = itfTblWxCpgzDownloadService.lambdaQuery()
				.eq(ItfTblWxCpgzDownload::getSynTaskNo,synTaskNo).list();
		if (CollectionUtils.isNotEmpty(addRec)){
			// 生成故障现象数据
			getAddRepairBugAppT(brandRec, addRec);
			getAddRepairBugAppSY(brandRec, addRec);
			addRec.clear();
		}
	}

	public void addRepairProjectType(Integer i, String name) {
		// 移除原有的
		List<RepairProjectType> rpts = repairProjectTypeService.lambdaQuery()
				.eq(RepairProjectType::getType,i).list();
		if (CollectionUtils.isNotEmpty(rpts)){
			QueryWrapper del = new QueryWrapper<RepairProjectType>()
					.eq("type", i);
			repairProjectTypeService.remove(del);
		}


		RepairProjectType n = new RepairProjectType();
		n.setParentId(0);
		n.setName(name);
		n.setStatus(1);
		n.setType(i);
		n.setMalSmallName("");
		n.setCreateBy("system");
		n.setUpdateBy("system");
		repairProjectTypeService.save(n);
	}


	public void addItfTblWxGzdmWxxmjcDownload(String synTaskNo, JSONArray jsonArray){
		List<ItfTblWxGzdmWxxmjcDownload> addRec = new ArrayList<>();
		getItfTblWxGzdmWxxmjcDownloadT(synTaskNo, jsonArray, addRec);
		if (addRec.size()>0){
			itfTblWxGzdmWxxmjcDownloadService.saveBatch(addRec);
			log.info("故障代码与维修项目基表信息同步新增插入了{}条数据", addRec.size());
			addRec.clear();
		}
	}

	public void addItfTblWxGzdmWxxmjcDownloadOld(String synTaskNo, Brand brandRec){
		List<RepairProject> addres = new ArrayList<>();

		List<ItfTblWxGzdmWxxmjcDownload> addRec = itfTblWxGzdmWxxmjcDownloadService.lambdaQuery()
				.eq(ItfTblWxGzdmWxxmjcDownload::getSynTaskNo,synTaskNo).list();

		if (CollectionUtils.isNotEmpty(addRec)){
			// 新增到维修项目表
			getAddres(brandRec, addRec, addres);
			addRec.clear();

			if (addres.size()>0){
				repairProjectService.saveBatch(addres);
				log.info("维修项目表信息同步新增了{}条数据", addres.size());
				addres.clear();
			}
		}
	}

	private void getAddres(Brand brandRec, List<ItfTblWxGzdmWxxmjcDownload> addRec, List<RepairProject> addres) {
		List<RepairProjectType> rpts = repairProjectTypeService.lambdaQuery()
				.in(RepairProjectType::getType,1,2).list();
		Map<Integer,List<RepairProjectType>> rptsMap = new HashMap<>();
		if (CollectionUtils.isNotEmpty(rpts)){
			rptsMap = rpts.stream().collect(Collectors.groupingBy(RepairProjectType::getType));
		}

		for (ItfTblWxGzdmWxxmjcDownload item: addRec){
			String repairProjectType="1";
			if (item.getCpsx().equals(2)){
				repairProjectType = "2";
			}
			// 存在不处理
			List<RepairProject> rps = repairProjectService.lambdaQuery()
					.eq(RepairProject::getRepairProjectType,repairProjectType)
					.eq(RepairProject::getBugObjCode,item.getXxyyh())
					.eq(RepairProject::getRepairProjectCode,item.getWxxmh())
					.in(RepairProject::getRepairProjectType,"1","2").list();
			if (CollectionUtils.isNotEmpty(rps)){
				continue;
			}

			RepairProject v = new RepairProject();
			v.setRepairProjectId(item.getId());
			v.setBrandId(brandRec.getBrandId());
			v.setBrandName(brandRec.getBrand());
			v.setBugObjCode(item.getXxyyh());
			v.setRepairProjectCode(item.getWxxmh());
			v.setRemark(item.getWxxm());
			v.setRepairProjectType(repairProjectType);
			v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
			if (rptsMap.containsKey(repairProjectType)){
				v.setProjectTypeId(rptsMap.get(repairProjectType).get(0).getId());
			}
			repairProjectService.save(v);
		}
	}

	public void addItfTblWxWxxmjcDownload(String synTaskNo, JSONArray jsonArray){
		List<ItfTblWxWxxmjcDownload> addRec = new ArrayList<>();
		getItfTblWxWxxmjcDownload(synTaskNo, jsonArray, addRec);
		// 新增的插入到表
		if (addRec.size()>0){
			itfTblWxWxxmjcDownloadService.saveBatch(addRec);
			log.info("维修项目基表信息同步新增插入了{}条数据", addRec.size());
			addRec.clear();
		}
	}

	public void addItfTblWxWxxmjcDownloadOld(String synTaskNo, Brand brandRec){
		List<RepairProjectType> rpts = new ArrayList<>();

		// 移除原有的
		List<RepairProjectType> rpt = repairProjectTypeService.lambdaQuery()
				.eq(RepairProjectType::getType,2).list();
		if (CollectionUtils.isNotEmpty(rpt)){
			QueryWrapper del = new QueryWrapper<RepairProjectType>()
					.eq("type", 2);
			repairProjectTypeService.remove(del);
		}


		List<ItfTblWxWxxmjcDownload> addRec = itfTblWxWxxmjcDownloadService.lambdaQuery()
				.eq(ItfTblWxWxxmjcDownload::getSynTaskNo,synTaskNo).list();

		if (CollectionUtils.isNotEmpty(addRec)){
			getAddRepairProjectType(addRec, rpts);
			// 新增的插入到表
			if (rpts.size()>0){
				repairProjectTypeService.saveBatch(rpts);
				rpts.clear();
			}
			getAddRepairProject(brandRec, addRec);
			addRec.clear();
		}
	}

	public void addItfJhAzdCauseSortNewDownload(String synTaskNo, JSONArray jsonArray){
		List<ItfJhAzdCauseSortNewDownload> addRec = new ArrayList<>();
		getItfJhAzdCauseSortNewDownloadT(synTaskNo, jsonArray, addRec);
		if (addRec.size()>0){
			itfJhAzdCauseSortNewDownloadService.saveBatch(addRec);
			log.info("晶弘冰箱故障代码信息同步新增插入了{}条数据", addRec.size());
			addRec.clear();
		}
	}

	public void addItfJhAzdCauseSortNewDownloadOld(String synTaskNo, Brand brandRec){
		List<ItfJhAzdCauseSortNewDownload> addRec = itfJhAzdCauseSortNewDownloadService.lambdaQuery()
				.eq(ItfJhAzdCauseSortNewDownload::getSynTaskNo,synTaskNo).list();

		if (CollectionUtils.isNotEmpty(addRec)){
			// 新增冰箱故障固定为两级
			getAddRepairBugAppJH(brandRec, addRec);
			addRec.clear();
		}
	}

	public void addItfJhAzdCauseAtterDownloadOld(String synTaskNo, Brand brandRec){
		List<ItfJhAzdCauseAtterDownload> addRec = itfJhAzdCauseAtterDownloadService.lambdaQuery()
				.eq(ItfJhAzdCauseAtterDownload::getSynTaskNo,synTaskNo).list();

		if (CollectionUtils.isNotEmpty(addRec)){
			getAddRepairProjectJH(brandRec, addRec);
			addRec.clear();
		}
	}

	public void addItfJhAzdCauseAtterDownload(String synTaskNo, JSONArray jsonArray){
		List<ItfJhAzdCauseAtterDownload> addRec = new ArrayList<>();
		getItfJhAzdCauseAtterDownload(synTaskNo, jsonArray, addRec);
		// 新增的插入到表
		if (addRec.size()>0){
			itfJhAzdCauseAtterDownloadService.saveBatch(addRec);
			log.info("晶弘冰箱维修项目基表信息同步新增插入了{}条数据", addRec.size());
			addRec.clear();
		}
	}


	public void addItfTblWxCpgzXjdDownload(String synTaskNo, JSONArray jsonArray){
		List<ItfTblWxCpgzXjdDownload> addRec = new ArrayList<>();
		getItfTblWxCpgzXjdDownload(synTaskNo, jsonArray, addRec);
		// 新增的插入到表 维修－产品故障基表下载记录
		if (addRec.size()>0){
			itfTblWxCpgzXjdDownloadService.saveBatch(addRec);
			log.info("生活电话故障代码信息同步新增插入了{}条数据", addRec.size());
			addRec.clear();
		}
	}

	public void addItfTblWxCpgzXjdDownloadOld(String synTaskNo, Brand brandRec){
		List<ItfTblWxCpgzXjdDownload> addRec = itfTblWxCpgzXjdDownloadService.lambdaQuery()
				.eq(ItfTblWxCpgzXjdDownload::getSynTaskNo,synTaskNo).list();

		if (CollectionUtils.isNotEmpty(addRec)){
			getAddRepairBugAppXJD(brandRec, addRec);
			addRec.clear();
		}
	}

	public void addItfTblWxWxxmXjdDownload(String synTaskNo, JSONArray jsonArray){
		List<ItfTblWxWxxmXjdDownload> addRec = new ArrayList<>();
		getItfTblWxWxxmXjdDownload(synTaskNo, jsonArray, addRec);
		// 新增的插入到表
		if (addRec.size()>0){
			itfTblWxWxxmXjdDownloadService.saveBatch(addRec);
			log.info("晶弘冰箱维修项目基表信息同步新增插入了{}条数据", addRec.size());
			addRec.clear();
		}
	}

	public void addItfTblWxWxxmXjdDownloadOld(String synTaskNo,Brand brandRec){
		List<ItfTblWxWxxmXjdDownload> addRec = itfTblWxWxxmXjdDownloadService.lambdaQuery()
				.eq(ItfTblWxWxxmXjdDownload::getSynTaskNo,synTaskNo).list();

		if (CollectionUtils.isNotEmpty(addRec)){
			getAddRepairProjectXJD(brandRec, addRec);
			addRec.clear();
		}
	}


	private void getAddRepairBugAppT(Brand brandRec, List<ItfTblWxCpgzDownload> addRec) {
		String q = "-";
		Map<String, List<SysDict>> dl = new HashMap<>();

		// 提取大类字典数据
		List<SysDict> oldDSysDictRec =  sysDictService.lambdaQuery()
				.eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode())
				.eq(SysDict::getStatus, DictTypeEnum.DICT_TYPE_ON.getCode()).list();
		// 大类编号
		dl = oldDSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictValue));

		// 提取小类字典数据
		List<SysDict> oldSysDictRec =  sysDictService.lambdaQuery()
				.eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_SMALL_TYPE.getCode())
				.eq(SysDict::getStatus, DictTypeEnum.DICT_TYPE_ON.getCode())
				.list();
		// 小类编号
		Map<String, List<SysDict>> xl = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictValue));


		for (ItfTblWxCpgzDownload item: addRec){
			if (item.getShux().equals("家用") || item.getShux().equals("通用")){
				String mshu = trim(item.getMshu());
				// 去掉右则-
				if (mshu.length()>2 &&
						mshu.substring(mshu.length()-2,mshu.length()).equals("--")){
					mshu = mshu.substring(0,mshu.length()-2);
				}
				if (mshu.length()>1 &&
						mshu.substring(mshu.length()-1,mshu.length()).equals("-")){
					mshu = mshu.substring(0,mshu.length()-1);
				}

				// 无层级关系
				if (!mshu.contains(q)){
					getAddRepairBugApp(brandRec, item, mshu,dl,xl);
				} else {
					getAddRepairBugAppParent(brandRec, q, item, mshu,dl,xl);
				}
			}
		}
	}

	private void getAddRepairBugAppSY(Brand brandRec, List<ItfTblWxCpgzDownload> addRec) {
		String q = "-";
		for (ItfTblWxCpgzDownload item: addRec){
			if (!(item.getShux().equals("商用") || item.getShux().equals("通用"))){
				continue;
			}
			String mshu = trim(item.getMshu());
			if (mshu.length()>2 &&
					mshu.substring(mshu.length()-2,mshu.length()).equals("--")){
				mshu = item.getMshu().substring(0,mshu.length()-2);
			}
			if (mshu.length()>1 &&
					mshu.substring(mshu.length()-1,mshu.length()).equals("-")){
				mshu = item.getMshu().substring(0,mshu.length()-1);
			}

			// 无层级关系
			if (!mshu.contains(q)){
				getAddRepairBugAppSY(brandRec, item, mshu);
			} else {
				getAddRepairBugAppParentSY(brandRec, q, item, mshu);
			}
		}
	}

	private void getAddRepairBugAppParent(Brand brandRec, String q, ItfTblWxCpgzDownload item, String mshu,
									   Map<String, List<SysDict>> dl,Map<String, List<SysDict>> xl) {
		String[] result = mshu.split(q);
		String bugId="0";
		String parentPathName = "";
		for (int i=0;i<result.length;i++){
			if (i == result.length-1){
				// 查看是否存在，已存在不处理
				List<RepairBugApp> oldBugs = repairBugAppService.lambdaQuery()
						.eq(RepairBugApp::getBugObjType,1)
						.eq(RepairBugApp::getBugObjCode, item.getXxyyh())
						.eq(RepairBugApp::getLevel,1)
						.ne(RepairBugApp::getBugObjCode,"").list();
				if (CollectionUtils.isNotEmpty(oldBugs)){
					bugId = oldBugs.get(0).getBugId();
					continue;
				}
			} else {
				// 查看是否存在，已存在不处理
				List<RepairBugApp> oldBugs = repairBugAppService.lambdaQuery()
						.eq(RepairBugApp::getBugObjType,1)
						.eq(RepairBugApp::getBugObj,result[i])
						.eq(RepairBugApp::getBugObjCode,"")
						.eq(RepairBugApp::getParentPathName,parentPathName).list();
				if (CollectionUtils.isNotEmpty(oldBugs)){
					if (StringUtils.isEmpty(oldBugs.get(0).getParentPathName())){
						parentPathName="`" + oldBugs.get(0).getBugObj()+"`";
					} else {
						parentPathName=oldBugs.get(0).getParentPathName() + ",`" + oldBugs.get(0).getBugObj()+"`";
					}
					bugId = oldBugs.get(0).getBugId();
					continue;
				}
			}
			RepairBugApp v = new RepairBugApp();
			v.setBugId(publicService.getUUID());
			v.setBrandId(brandRec.getBrandId());
			v.setBrandName(brandRec.getBrand());
			v.setMainName(item.getCpdl());
			v.setSmallName(item.getCpxl());
			v.setMainId("");
			if (dl.containsKey(item.getCpdl())){
				v.setMainId(dl.get(item.getCpdl()).get(0).getDictCode());
			}
			if (xl.containsKey(item.getCpdl())){
				v.setSmallId(xl.get(item.getCpdl()).get(0).getDictCode());
			}
			v.setBugObj(result[i]);
			v.setParentId(bugId);
			bugId = v.getBugId();
			v.setBugObjCode("");
			if (i == result.length-1){
				v.setBugObjCode(item.getXxyyh());
			}
			v.setBugObjType(1);
			v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
			if (item.getSfyx().equals(1L)){
				v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
			}
			v.setGzid(String.valueOf(item.getId()));
			v.setLevel(i+1);
			v.setParentPathName(parentPathName);
			v.setCreateBy("system");
			v.setCreateTime(new Date());
			if (item.getCreatedBy() !=null){
				v.setCreateBy(item.getCreatedBy());
			}
			if (item.getCreatedDate() != null){
				v.setCreateTime(item.getCreatedDate());
			}
			repairBugAppService.save(v);

			if (StringUtils.isEmpty(parentPathName)){
				parentPathName="`" + result[i]+"`";
			} else {
				parentPathName=parentPathName + ",`" + result[i]+"`";
			}
		}
	}

	private void getAddRepairBugAppParentSY(Brand brandRec, String q, ItfTblWxCpgzDownload item, String mshu) {
		String[] result = mshu.split(q);
		String bugId="0";
		String parentPathName = "";
		for (int i=0;i<result.length;i++){
			if (i == result.length-1){
				// 查看是否存在，已存在不处理
				List<RepairBugApp> oldBugs = repairBugAppService.lambdaQuery()
						.eq(RepairBugApp::getBugObjType,2)
						.eq(RepairBugApp::getBugObjCode,item.getXxyyh())
						.gt(RepairBugApp::getLevel,0)
						.ne(RepairBugApp::getBugObjCode,"").list();
				if (CollectionUtils.isNotEmpty(oldBugs)){
					bugId = oldBugs.get(0).getBugId();
					continue;
				}
			} else {
				// 查看是否存在，已存在不处理
				List<RepairBugApp> oldBugs = repairBugAppService.lambdaQuery()
						.eq(RepairBugApp::getBugObjType,2)
						.eq(RepairBugApp::getBugObj,result[i])
						.eq(RepairBugApp::getBugObjCode,"")
						.eq(RepairBugApp::getParentPathName,parentPathName).list();
				if (CollectionUtils.isNotEmpty(oldBugs)){
					if (StringUtils.isEmpty(oldBugs.get(0).getParentPathName())){
						parentPathName="`" + oldBugs.get(0).getBugObj()+"`";
					} else {
						parentPathName=oldBugs.get(0).getParentPathName() + ",`" + oldBugs.get(0).getBugObj()+"`";
					}
					bugId = oldBugs.get(0).getBugId();
					continue;
				}
			}

			RepairBugApp v = new RepairBugApp();
			v.setBugId(publicService.getUUID());
			v.setBrandId(brandRec.getBrandId());
			v.setBrandName(brandRec.getBrand());
			v.setMainId(item.getCpdl());
			v.setSmallId(item.getCpxl());
			v.setBugObj(result[i]);
			v.setParentId(bugId);
			bugId = v.getBugId();
			v.setBugObjCode("");
			if (i == result.length-1){
				v.setBugObjCode(item.getXxyyh());
			}
			v.setBugObjType(2);
			v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
			if (item.getSfyx().equals(1L)){
				v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
			}
			v.setGzid(String.valueOf(item.getId()));
			v.setLevel(i+1);
			v.setParentPathName(parentPathName);
			v.setCreateBy("system");
			v.setCreateTime(new Date());
			if (item.getCreatedBy() !=null){
				v.setCreateBy(item.getCreatedBy());
			}
			if (item.getCreatedDate() != null){
				v.setCreateTime(item.getCreatedDate());
			}
			// addRepairBug.add(v);
			repairBugAppService.save(v);

			if (StringUtils.isEmpty(parentPathName)){
				parentPathName="`" + result[i]+"`";
			} else {
				parentPathName=parentPathName + ",`" + result[i]+"`";
			}
		}
	}

	private void getAddRepairBugApp(Brand brandRec, ItfTblWxCpgzDownload item, String mshu,
								 Map<String, List<SysDict>> dl,Map<String, List<SysDict>> xl) {
		// 查看是否存在，已存在不处理
		List<RepairBugApp> oldBugs = repairBugAppService.lambdaQuery()
				.eq(RepairBugApp::getBugObjType,1)
				.eq(RepairBugApp::getBugObjCode,item.getXxyyh())
				.eq(RepairBugApp::getLevel,1)
				.ne(RepairBugApp::getBugObjCode,"").list();
		if (CollectionUtils.isNotEmpty(oldBugs)){
			return;
		}

		RepairBugApp v = new RepairBugApp();
		v.setBugId(publicService.getUUID());
		v.setBrandId(brandRec.getBrandId());
		v.setParentId("0");
		v.setBrandName(brandRec.getBrand());
		v.setMainName(item.getCpdl());
		v.setSmallName(item.getCpxl());
		v.setMainId("");
		if (dl.containsKey(item.getCpdl())){
			v.setMainId(dl.get(item.getCpdl()).get(0).getDictCode());
		}
		if (xl.containsKey(item.getCpdl())){
			v.setSmallId(xl.get(item.getCpdl()).get(0).getDictCode());
		}
		v.setBugObj(mshu);
		v.setBugObjType(1);
		v.setBugObjCode(item.getXxyyh());
		v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
		if (item.getSfyx().equals(1L)){
			v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
		}
		v.setGzid(String.valueOf(item.getId()));
		v.setParentPathName("");
		v.setLevel(1);
		v.setCreateBy("system");
		v.setCreateTime(new Date());
		if (item.getCreatedBy() !=null){
			v.setCreateBy(item.getCreatedBy());
		}
		if (item.getCreatedDate() != null){
			v.setCreateTime(item.getCreatedDate());
		}
		// addRepairBug.add(v);
		repairBugAppService.save(v);
	}

	private void getAddRepairBugAppSY(Brand brandRec, ItfTblWxCpgzDownload item, String mshu) {
		// 查看是否存在，已存在不处理
		List<RepairBugApp> oldBugs = repairBugAppService.lambdaQuery()
				.eq(RepairBugApp::getBugObjType,2)
				.eq(RepairBugApp::getBugObjCode,item.getXxyyh())
				.gt(RepairBugApp::getLevel,0)
				.ne(RepairBugApp::getBugObjCode,"").list();
		if (CollectionUtils.isNotEmpty(oldBugs)){
			return;
		}

		RepairBugApp v = new RepairBugApp();
		v.setBugId(publicService.getUUID());
		v.setBrandId(brandRec.getBrandId());
		v.setParentId("0");
		v.setBrandName(brandRec.getBrand());
		v.setMainId(item.getCpdl());
		v.setSmallId(item.getCpxl());
		v.setBugObj(mshu);
		v.setBugObjType(2);
		v.setBugObjCode(item.getXxyyh());
		v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
		if (item.getSfyx().equals(1L)){
			v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
		}
		v.setGzid(String.valueOf(item.getId()));
		v.setParentPathName("");
		v.setLevel(1);
		v.setCreateBy("system");
		v.setCreateTime(new Date());
		if (item.getCreatedBy() !=null){
			v.setCreateBy(item.getCreatedBy());
		}
		if (item.getCreatedDate() != null){
			v.setCreateTime(item.getCreatedDate());
		}
		// addRepairBug.add(v);
		repairBugAppService.save(v);
	}

	public void getAddRepairBugAppJH(Brand brandRec, List<ItfJhAzdCauseSortNewDownload> addRec) {
		String parentPathName = "";
		String pid="0";
		List<RepairBugApp> rblist = new ArrayList<>();
		for (ItfJhAzdCauseSortNewDownload item: addRec){
			pid="0";
			List<RepairBugApp> rb = repairBugAppService.lambdaQuery()
					.eq(RepairBugApp::getBugObjType,4)
					.eq(RepairBugApp::getBugObj,item.getSort()).list();
			if (CollectionUtils.isEmpty(rb)){
				RepairBugApp o = new RepairBugApp();
				o.setBugId(publicService.getUUID());
				o.setGzid(String.valueOf(item.getId()));
				o.setBrandId(brandRec.getBrandId());
				o.setBrandName(brandRec.getBrand());
				o.setBugObjType(4);
				o.setMainName("");
				o.setMainId("");
				o.setSmallName("");
				o.setSmallId("");
				o.setBugObj(item.getSort());
				o.setBugObjCode("");
				o.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
				if (item.getStatus().equals(1L)){
					o.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
				}

				o.setParentId(pid);
				pid=o.getBugId();
				o.setParentPathName("");
				o.setLevel(0);

				o.setCreateBy("system");
				o.setCreateTime(new Date());
				if (item.getCreatedBy() !=null){
					o.setCreateBy(item.getCreatedBy());
				}
				if (item.getCreatedDate() != null){
					o.setCreateTime(item.getCreatedDate());
				}
				rblist.add(o);
				parentPathName="`" + item.getSort() +"`";
			} else {
				parentPathName="`" + rb.get(0).getBugObj() +"`";
				pid = rb.get(0).getBugId();
			}

			List<RepairBugApp> rbs = repairBugAppService.lambdaQuery()
					.eq(RepairBugApp::getBugObjType,4)
					.eq(RepairBugApp::getBugObj,item.getDescription()).list();
			if (CollectionUtils.isEmpty(rbs)){
				RepairBugApp v = new RepairBugApp();
				v.setBugId(publicService.getUUID());
				v.setGzid(String.valueOf(item.getId()));
				v.setBrandId(brandRec.getBrandId());
				v.setBrandName(brandRec.getBrand());
				v.setBugObjType(4);
				v.setMainName("");
				v.setMainId("");
				v.setSmallName("");
				v.setSmallId("");
				v.setBugObj(item.getDescription());
				v.setBugObjCode(item.getCauseno());
				v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
				if (item.getStatus().equals(1L)){
					v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
				}

				v.setParentId(pid);
				v.setParentPathName(parentPathName);
				v.setLevel(0);

				v.setCreateBy("system");
				v.setCreateTime(new Date());
				if (item.getCreatedBy() !=null){
					v.setCreateBy(item.getCreatedBy());
				}
				if (item.getCreatedDate() != null){
					v.setCreateTime(item.getCreatedDate());
				}
				rblist.add(v);
			}

			if (rblist.size()>0){
				repairBugAppService.saveBatch(rblist);
				rblist.clear();
			}
		}
	}


	public void getAddRepairBugAppXJD(Brand brandRec, List<ItfTblWxCpgzXjdDownload> addRec) {
		String q = "-";
		Map<String, List<SysDict>> oldDSysDictRecMapN = new HashMap<>();
		// 提取大类字典数据
		List<SysDict> oldDSysDictRec =  sysDictService.lambdaQuery()
				.eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode())
				.eq(SysDict::getStatus, DictTypeEnum.DICT_TYPE_ON.getCode()).list();
		// 大类编号
		oldDSysDictRecMapN = oldDSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictValue));

		for (ItfTblWxCpgzXjdDownload item: addRec){
			String gzms = trim(item.getGzms());
			// 去掉右则-
			if (gzms.length()>2 &&
					gzms.substring(gzms.length()-2,gzms.length()).equals("--")){
				gzms = gzms.substring(0,gzms.length()-2);
			}
			if (gzms.length()>1 &&
					gzms.substring(gzms.length()-1,gzms.length()).equals("-")){
				gzms = gzms.substring(0,gzms.length()-1);
			}
			// 生成对像
			getRepairBugAppInfo(brandRec, q, item, gzms, oldDSysDictRecMapN);
		}
	}

	public void getRepairBugAppInfo(Brand brandRec, String q, ItfTblWxCpgzXjdDownload item, String gzms,Map<String, List<SysDict>> oldDSysDictRecMapN) {
		String[] result = gzms.split(q);
		String bugId="0";
		String parentPathName = "";
		// 第一级为大类,故障现象从第二节开始获取
		for (int i=1;i<result.length;i++){
			// 查看是否存在，已存在不处理
			List<RepairBugApp> oldBugs = repairBugAppService.lambdaQuery()
					.eq(RepairBugApp::getBugObjType,3)
					.eq(RepairBugApp::getMainName, item.getCpdl())
					.eq(RepairBugApp::getBugObj,result[i]).list();
			if (CollectionUtils.isNotEmpty(oldBugs)){
				if (StringUtils.isEmpty(oldBugs.get(0).getParentPathName())){
					parentPathName="`" + oldBugs.get(0).getBugObj()+"`";
				} else {
					parentPathName=oldBugs.get(0).getParentPathName() + ",`" + oldBugs.get(0).getBugObj()+"`";
				}
				bugId = oldBugs.get(0).getBugId();
				continue;
			}
			RepairBugApp v = new RepairBugApp();
			v.setBugId(publicService.getUUID());
			v.setGzid(String.valueOf(item.getId()));
			v.setBrandId(brandRec.getBrandId());
			v.setBrandName(brandRec.getBrand());
			v.setBugObjType(3);
			v.setMainName(item.getCpdl());
			v.setMainId("");
			if (oldDSysDictRecMapN.containsKey(item.getCpdl())){
				v.setMainId(oldDSysDictRecMapN.get(item.getCpdl()).get(0).getDictCode());
			}
			v.setSmallName("");
			v.setSmallId("");
			v.setBugObj(result[i]);
			v.setBugObjCode("");
			if (i == result.length-1){
				v.setBugObjCode(item.getGzdm());
			}
			v.setParentId(bugId);
			bugId = v.getBugId();
			v.setParentPathName(parentPathName);
			if (i==1){
				v.setParentId("0");
				v.setParentPathName("");
			}
			v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
			v.setCreateBy("system");
			v.setCreateTime(new Date());
			if (item.getCreatedBy() !=null){
				v.setCreateBy(item.getCreatedBy());
			}
			if (item.getCreatedDate() != null){
				v.setCreateTime(item.getCreatedDate());
			}
			repairBugAppService.save(v);

			if (StringUtils.isEmpty(parentPathName)){
				parentPathName="`" + result[i]+"`";
			} else {
				parentPathName=parentPathName + ",`" + result[i]+"`";
			}
		}
	}


	private void getAddRepairProjectType(List<ItfTblWxWxxmjcDownload> addRec,List<RepairProjectType> rpts) {
		// 按项目分类分组，只取其一
		List<String> xmfls = new ArrayList<>();
		for (ItfTblWxWxxmjcDownload item: addRec){
			if (xmfls.contains(item.getXmfl())){
				continue;
			}
			xmfls.add(item.getXmfl());
			RepairProjectType v = new RepairProjectType();
			v.setParentId(0);
			v.setName(item.getXmfl());
			v.setStatus(1);
			v.setType(2);
			v.setMalSmallName("");
			v.setCreateBy("system");
			v.setUpdateBy("system");
			if (item.getCreatedBy() !=null){
				v.setCreateBy(item.getCreatedBy());
			}
			rpts.add(v);
		}
	}

	private void getAddRepairProject(Brand brandRec, List<ItfTblWxWxxmjcDownload> addRec) {
		Map<String,List<RepairProjectType>> rptsMap = new HashMap<>();
		// 提取项目类型ID
		List<RepairProjectType> rpts = repairProjectTypeService.lambdaQuery()
				.eq(RepairProjectType::getType,2).list();
		if (CollectionUtils.isNotEmpty(rpts)){
			rptsMap = rpts.stream().collect(Collectors.groupingBy(RepairProjectType::getName));
		}
		for (ItfTblWxWxxmjcDownload item: addRec){
			// 提取原有的
			List<RepairProject> rps = repairProjectService.lambdaQuery()
					.eq(RepairProject::getRepairProjectType,"2")
					.eq(RepairProject::getRepairProjectCode,item.getWxxmh()).list();
			if (CollectionUtils.isNotEmpty(rps)){
				continue;
			}
			RepairProject v = new RepairProject();
			v.setRepairProjectId(IdWorker.getIdStr());
			v.setOutXmId(item.getId());
			v.setBrandId(brandRec.getBrandId());
			v.setBrandName(brandRec.getBrand());
			v.setRepairProjectCode(item.getWxxmh());
			if (rptsMap.containsKey(item.getXmfl())){
				v.setProjectTypeId(rptsMap.get(item.getXmfl()).get(0).getId());
			}
			v.setBugObjCode("");
			v.setRemark(item.getMshu());
			v.setRepairProjectType("2");
			v.setMainId("");
			v.setMainName("");
			v.setSmallName("");
			v.setSmallId("");
			v.setStatus(DictTypeEnum.DICT_TYPE_OFF.getCode());
			if (item.getSfyx().equals(1L)){
				v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
			}

			repairProjectService.save(v);
		}
	}

	private void getAddRepairProjectJH(Brand brandRec, List<ItfJhAzdCauseAtterDownload> addRec) {
		List<RepairProjectType> rpts = repairProjectTypeService.lambdaQuery()
				.eq(RepairProjectType::getType,4).list();

		for (ItfJhAzdCauseAtterDownload item: addRec){
			// 已存在不处理
			List<RepairProject> rbs = repairProjectService.lambdaQuery()
					.eq(RepairProject::getRepairProjectType,"4")
					.eq(RepairProject::getRepairProjectCode,item.getAtterno())
					.eq(RepairProject::getBugObjCode,item.getCauseno()).list();
			if (CollectionUtils.isNotEmpty(rbs)){
				continue;
			}

			RepairProject v = new RepairProject();
			v.setRepairProjectId(IdWorker.getIdStr());
			v.setOutId(item.getId());
			v.setBrandId(brandRec.getBrandId());
			v.setBrandName(brandRec.getBrand());
			v.setBugObjCode(item.getCauseno());
			v.setRepairProjectCode(item.getAtterno());
			v.setRemark(item.getAtter());
			v.setRepairProjectType("4");
			if (CollectionUtils.isNotEmpty(rpts)){
				v.setProjectTypeId(rpts.get(0).getId());
			}
			v.setSmallId("");
			v.setSmallName("");
			v.setMainId("");
			v.setMainName("");
			v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());

			repairProjectService.save(v);
		}
	}

	private void getAddRepairProjectXJD(Brand brandRec, List<ItfTblWxWxxmXjdDownload> addRec) {
		String q = "、";
		List<RepairProjectType> rpts = repairProjectTypeService.lambdaQuery()
				.eq(RepairProjectType::getType,3).list();

		// 提取小类字典数据
		List<SysDict> oldSysDictRec =  sysDictService.lambdaQuery()
				.eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_SMALL_TYPE.getCode())
				.eq(SysDict::getStatus, DictTypeEnum.DICT_TYPE_ON.getCode())
				.list();
		// 小类编号
		Map<String, List<SysDict>> oldSysDictRecMap = oldSysDictRec.stream().collect(Collectors.groupingBy(SysDict::getDictValue));


		for (ItfTblWxWxxmXjdDownload item: addRec){
			String samllname = trim(item.getCpdl());
			// 去掉右则、
			if (samllname.length()>2 &&
					samllname.substring(samllname.length()-2,samllname.length()).equals("、、")){
				samllname = samllname.substring(0,samllname.length()-2);
			}
			if (samllname.length()>1 &&
					samllname.substring(samllname.length()-1,samllname.length()).equals("、")){
				samllname = samllname.substring(0,samllname.length()-1);
			}

			String[] result = samllname.split(q);
			for (int i=0;i<result.length;i++){
				// 查看是否存在，已存在不处理
				List<RepairProject> oldBugs = repairProjectService.lambdaQuery()
						.eq(RepairProject::getRepairProjectType,3)
						.eq(RepairProject::getSmallName, result[i])
						.eq(RepairProject::getRepairProjectCode,item.getWxdm()).list();
				if (CollectionUtils.isNotEmpty(oldBugs)){
					continue;
				}
				RepairProject v = new RepairProject();
				v.setRepairProjectId(IdWorker.getIdStr());
				v.setOutId(Long.valueOf(item.getId()));
				v.setBrandId(brandRec.getBrandId());
				v.setBrandName(brandRec.getBrand());
				v.setBugObjCode("");
				v.setRepairProjectCode(item.getWxdm());
				v.setRemark(item.getCpdl());
				v.setRepairProjectType("3");
				v.setSmallName(result[i]);
				v.setSmallId("");
				if (oldSysDictRecMap.containsKey(result[i])){
					v.setSmallId(oldSysDictRecMap.get(result[i]).get(0).getDictCode());
				}
				v.setMainId("");
				v.setMainName("");
				v.setStatus(DictTypeEnum.DICT_TYPE_ON.getCode());
				if (CollectionUtils.isNotEmpty(rpts)){
					v.setProjectTypeId(rpts.get(0).getId());
				}

				repairProjectService.save(v);
			}
		}
	}

	private void getItfTblWxCpgzDownloadT(String synTaskNo, JSONArray jsonArray,
										  List<ItfTblWxCpgzDownload> addRec) {
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject aData = jsonArray.getJSONObject(i);
			ItfTblWxCpgzDownload item = JSON.toJavaObject(aData,ItfTblWxCpgzDownload.class);
			if (StringUtils.isEmpty(item.getXxyyh())){
				continue;
			}
			if (StringUtils.isEmpty(item.getGzxx())){
				item.setGzxx("");
			}
			if (StringUtils.isEmpty(item.getMshu())){
				item.setMshu("");
			}
			if (StringUtils.isEmpty(item.getCpdl())){
				item.setCpdl("");
			}
			if (StringUtils.isEmpty(item.getCpxl())){
				item.setCpxl("");
			}
			if (StringUtils.isEmpty(item.getShux())){
				item.setShux("");
			}
			if (StringUtils.isEmpty(item.getGzxx())){
				item.setGzxx("");
			}
			if (StringUtils.isEmpty(item.getGzzyy())){
				item.setGzzyy("");
			}
			if (StringUtils.isEmpty(item.getGzfyy())){
				item.setGzfyy("");
			}

			item.setSynTaskNo(synTaskNo);
			item.setSynReqTime(new Date());
			item.setSynStatus(1);
			item.setSynTime(new Date());
			item.setSynTimes(0);
			addRec.add(item);

			if (addRec.size()>=500){
				itfTblWxCpgzDownloadService.saveBatch(addRec);
				log.info("产品故障基表信息同步新增插入了{}条数据", addRec.size());
				addRec.clear();
			}
		}
	}


	private void getItfJhAzdCauseSortNewDownloadT(String synTaskNo, JSONArray jsonArray,
												  List<ItfJhAzdCauseSortNewDownload> addRec) {
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject aData = jsonArray.getJSONObject(i);
			ItfJhAzdCauseSortNewDownload item = JSON.toJavaObject(aData,ItfJhAzdCauseSortNewDownload.class);
			if (StringUtils.isEmpty(item.getCauseno())){
				continue;
			}
			if (StringUtils.isEmpty(item.getDescription())){
				continue;
			}
			if (StringUtils.isEmpty(item.getSort())){
				item.setSort("");
			}

			item.setSynTaskNo(synTaskNo);
			item.setSynReqTime(new Date());
			item.setSynStatus(1);
			item.setSynTime(new Date());
			item.setSynTimes(0);
			addRec.add(item);

			if (addRec.size()>=500){
				itfJhAzdCauseSortNewDownloadService.saveBatch(addRec);
				log.info("晶弘冰箱故障代码信息同步新增插入了{}条数据", addRec.size());
				addRec.clear();
			}
		}
	}

	private void getItfTblWxCpgzXjdDownload(String synTaskNo, JSONArray jsonArray,
											List<ItfTblWxCpgzXjdDownload> addRec) {
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject aData = jsonArray.getJSONObject(i);
			ItfTblWxCpgzXjdDownload item = JSON.toJavaObject(aData,ItfTblWxCpgzXjdDownload.class);
			if (StringUtils.isEmpty(item.getGzdm())){
				continue;
			}
			if (StringUtils.isEmpty(item.getGzms())){
				continue;
			}
			item.setSynTaskNo(synTaskNo);
			item.setSynReqTime(new Date());
			item.setSynStatus(1);
			item.setSynTime(new Date());
			item.setSynTimes(0);
			addRec.add(item);

			if (addRec.size()>=500){
				itfTblWxCpgzXjdDownloadService.saveBatch(addRec);
				log.info("生活电话故障代码信息同步新增插入了{}条数据", addRec.size());
				addRec.clear();
			}
		}
	}

	private void getItfTblWxGzdmWxxmjcDownloadT(String synTaskNo, JSONArray jsonArray,
												List<ItfTblWxGzdmWxxmjcDownload> addRec) {
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject aData = jsonArray.getJSONObject(i);
			ItfTblWxGzdmWxxmjcDownload item = JSON.toJavaObject(aData,ItfTblWxGzdmWxxmjcDownload.class);

			if (StringUtils.isEmpty(item.getWxxmh())){
				continue;
			}
			if (StringUtils.isEmpty(item.getWxxm())){
				continue;
			}
			if (StringUtils.isEmpty(item.getXxyyh())){
				continue;
			}
			if (StringUtils.isEmpty(item.getXxyyh())){
				continue;
			}
			if (item.getCpsx()==null){
				item.setCpsx(1);
			}
			if (item.getXxyy()==null){
				item.setXxyy("");
			}

			item.setSynTaskNo(synTaskNo);
			item.setSynReqTime(new Date());
			item.setSynStatus(1);
			item.setSynTime(new Date());
			item.setSynTimes(0);
			addRec.add(item);

			if (addRec.size()>=500){
				itfTblWxGzdmWxxmjcDownloadService.saveBatch(addRec);
				log.info("故障代码与维修项目基表信息同步新增插入了{}条数据", addRec.size());
				addRec.clear();
			}
		}
	}

	private void getItfTblWxWxxmjcDownload(String synTaskNo, JSONArray jsonArray,
										   List<ItfTblWxWxxmjcDownload> addRec) {
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject aData = jsonArray.getJSONObject(i);
			ItfTblWxWxxmjcDownload item = JSON.toJavaObject(aData,ItfTblWxWxxmjcDownload.class);
			if (StringUtils.isEmpty(item.getWxxmh())){
				continue;
			}
			if (StringUtils.isEmpty(item.getXmfl())){
				continue;
			}
			if (StringUtils.isEmpty(item.getMshu())){
				item.setMshu("");
			}
			if (StringUtils.isEmpty(item.getLeib())){
				item.setLeib("");
			}
			if (StringUtils.isEmpty(item.getLbsm())){
				item.setLbsm("");
			}
			if (StringUtils.isEmpty(item.getJdm())){
				item.setJdm("");
			}
			if (StringUtils.isEmpty(item.getLeib())){
				item.setLeib("");
			}
			if (StringUtils.isEmpty(item.getXmfl())){
				item.setXmfl("");
			}
			if (item.getKzzt()==null){
				item.setKzzt(0L);
			}
			if (item.getCjkz()==null){
				item.setCjkz(0L);
			}
			if (item.getSfyx()==null){
				item.setSfyx(1L);
			}
			if (item.getWgkz()==null){
				item.setWgkz(0L);
			}

			item.setSynTaskNo(synTaskNo);
			item.setSynReqTime(new Date());
			item.setSynStatus(1);
			item.setSynTime(new Date());
			item.setSynTimes(0);
			addRec.add(item);

			if (addRec.size()>=500){
				itfTblWxWxxmjcDownloadService.saveBatch(addRec);
				log.info("维修项目基表信息同步新增插入了{}条数据", addRec.size());
				addRec.clear();
			}
		}
	}


	private void getItfJhAzdCauseAtterDownload(String synTaskNo, JSONArray jsonArray,
											   List<ItfJhAzdCauseAtterDownload> addRec) {
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject aData = jsonArray.getJSONObject(i);
			ItfJhAzdCauseAtterDownload item = JSON.toJavaObject(aData,ItfJhAzdCauseAtterDownload.class);
			if (StringUtils.isEmpty(item.getAtterno())){
				continue;
			}
			if (StringUtils.isEmpty(item.getAtter())){
				continue;
			}
			if (StringUtils.isEmpty(item.getCauseno())){
				item.setCauseno("");
			}
			if (StringUtils.isEmpty(item.getCause())){
				item.setCause("");
			}

			item.setSynTaskNo(synTaskNo);
			item.setSynReqTime(new Date());
			item.setSynStatus(1);
			item.setSynTime(new Date());
			item.setSynTimes(0);
			addRec.add(item);

			if (addRec.size()>=500){
				itfJhAzdCauseAtterDownloadService.saveBatch(addRec);
				log.info("晶弘冰箱维修项目基表信息同步新增插入了{}条数据", addRec.size());
				addRec.clear();
			}
		}
	}

	private void getItfTblWxWxxmXjdDownload(String synTaskNo, JSONArray jsonArray,
											List<ItfTblWxWxxmXjdDownload> addRec) {
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject aData = jsonArray.getJSONObject(i);
			ItfTblWxWxxmXjdDownload item = JSON.toJavaObject(aData,ItfTblWxWxxmXjdDownload.class);
			if (StringUtils.isEmpty(item.getWxdm())){
				continue;
			}
			if (StringUtils.isEmpty(item.getCpdl())){
				continue;
			}

			if (StringUtils.isEmpty(item.getMshu())){
				item.setMshu("");
			}

			if (StringUtils.isEmpty(item.getXmfl())){
				item.setXmfl("");
			}

			item.setSynTaskNo(synTaskNo);
			item.setSynReqTime(new Date());
			item.setSynStatus(1);
			item.setSynTime(new Date());
			item.setSynTimes(0);
			addRec.add(item);

			if (addRec.size()>=500){
				itfTblWxWxxmXjdDownloadService.saveBatch(addRec);
				log.info("晶弘冰箱维修项目基表信息同步新增插入了{}条数据", addRec.size());
				addRec.clear();
			}
		}
	}

}
