package com.gree.mall.manager.logic;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;

import com.gree.mall.manager.bean.ItfTblCollectionChargeBean;
import com.gree.mall.manager.bean.ItfTblWorkOrderCollection0Bean;
import com.gree.mall.manager.bean.WorkOrderRelationBean;
import com.gree.mall.manager.bean.WorkOrderSearchReq;

import com.gree.mall.manager.bean.policy.ItfGreeSynDownloadRecBean;
import com.gree.mall.manager.bean.workorder.WorkerOrderSyncBean;
import com.gree.mall.manager.commonmapper.workorder.SyncMapper;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.*;
import com.gree.mall.manager.utils.RedisUtil;
import com.gree.mall.manager.utils.StringUtil;
import com.gree.mall.manager.utils.http.HttpUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.util.ArrayListWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class SyncOrderInfoLogic {

    @Value("${gree.V3.gettoken}")
    private String tokenUrl;
    @Value("${gree.check.barcode}")
    private String checkBarcodeUrl;
    @Value("${gree.repair.settle.get}")
    private String getRepairSettleUrl;
    @Value("${gree.repair.settle.do}")
    private String doRepairSettleUrl;
    @Value("${gree.client.id}")
    private String clientId;
    @Value("${gree.V3.client.secert}")
    private String clientSecert;
    @Value("${gree.api.url}")
    private String apiUrl;
    @Value("${gree.gz.appid}")
    private String gzAppId;
    @Value("${gree.gz.appkey}")
    private String gzAppKey;
    @Value("${gree.fs.appid}")
    private String fsAppId;
    @Value("${gree.fs.appkey}")
    private String fsAppKey;
    //    @Value("${gree.cloud.api.url}")
//    private String cloudApiUrl;
    @Value("${gree.V3.api.url}")
    private String v3ApiUrl;
    @Value("${gree.V3.gettoken}")
    private String v3TokenUrl;
    @Value("${gree.V3.client.secert}")
    private String v3ClientSecert;

    @Autowired
    ItfTblWorkOrderService itfTblWorkOrderService;

    @Autowired
    ItfGreeSynRuleService itfGreeSynRuleService;

    @Autowired
    ItfGreeSynDownloadRecService itfGreeSynDownloadRecService;

    @Value("${gree.four.url}")
    private String greeUrl;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    RedisLockRegistry redisLockRegistry;

    @Autowired
    ItfTblWorkOrderFeedbackService itfTblWorkOrderFeedbackService;

    @Autowired
    ItfTblCollectionChargeService itfTblCollectionChargeService;

    @Autowired
    ItfTblWorkOrderDetailService itfTblWorkOrderDetailService;


    @Autowired
    ItfTblCollectionChargeDetailService itfTblCollectionChargeDetailService;

    @Autowired
    ItfTblOldProductOrderDetailService itfTblOldProductOrderDetailService;

    @Autowired
    ItfTblWorkOrderAddComplainService itfTblWorkOrderAddComplainService;

    @Autowired
    ItfTblWorkOrderAppointService itfTblWorkOrderAppointService;

    @Autowired
    ItfTblWorkOrderCancelService itfTblWorkOrderCancelService;

    @Autowired
    ItfTblWorkOrderComplainFeedbackService itfTblWorkOrderComplainFeedbackService;

    @Autowired
    ItfTblWorkOrderComplainService itfTblWorkOrderComplainService;

    @Autowired
    ItfTblWorkOrderCompleteInfoService itfTblWorkOrderCompleteInfoService;

    @Autowired
    ItfTblWorkOrderConsultService itfTblWorkOrderConsultService;

    @Autowired
    ItfTblWorkOrderDetailBarcodeService itfTblWorkOrderDetailBarcodeService;

    @Autowired
    ItfTblWorkOrderDetailRelocationService itfTblWorkOrderDetailRelocationService;

    @Autowired
    ItfTblWorkOrderDispatchOutletsService itfTblWorkOrderDispatchOutletsService;

    @Autowired
    ItfTblWorkOrderEngineerService itfTblWorkOrderEngineerService;

    @Autowired
    private ItfTblWorkOrderEvaluationService itfTblWorkOrderEvaluationService;

    @Autowired
    ItfTblWorkOrderExtService itfTblWorkOrderExtService;

    @Autowired
    ItfTblWorkOrderIndicatorsService itfTblWorkOrderIndicatorsService;

    @Autowired
    private ItfTblWorkOrderSignService itfTblWorkOrderSignService;

    @Autowired
    private ItfTblWorkOrderTagService itfTblWorkOrderTagService;

    @Autowired
    private ItfTblWorkOrderChargeService itfTblWorkOrderChargeService;

    @Autowired
    private ItfTblWorkOrderCollectionComplainService itfTblWorkOrderCollectionComplainService;

    @Autowired
    private ItfTblWorkOrderCollectionService itfTblWorkOrderCollectionService;

    @Autowired
    private ItfTblCollectionRepairService itfTblCollectionRepairService;

    @Autowired
    ItfTblCollectionRepairPartService itfTblCollectionRepairPartService;

    @Autowired
    ItfTblCollectionRepairProjectService itfTblCollectionRepairProjectService;

    @Autowired
    ItfTblWorkOrderDispatchEngineerService tblWorkOrderDispatchEngineerService;

    @Autowired
    ItfTblWorkOrderDispatchEngineerService itfTblWorkOrderDispatchEngineerService;

    @Autowired
    ItfTblWorkOrderStatusService itfTblWorkOrderStatusService;

    @Autowired
    ItfTblCollectionRepairSymptomReasonService itfTblCollectionRepairSymptomReasonService;

    @Autowired
    SyncMapper syncMapper;


    public void syncGuang() {
        List<ItfGreeSynRule> itfGreeSynRules = itfGreeSynRuleService.lambdaQuery().list();

        for (ItfGreeSynRule itfGreeSynRule : itfGreeSynRules) {

            Long time = itfGreeSynRule.getLastSynTime().getTime() / 1000;

            String accessToken = this.getAccessTokenV3();

            Map<String, String> params = new HashMap<>();

            Map<String, String> heard = new HashMap<>();
            heard.put("authorization", "Bearer " + accessToken);

            String body = HttpUtils.requestGet(greeUrl + Constant.gree.GET_UPDATE_ORDERS + "/" + itfGreeSynRule.getSalesWebsitNumber() + "/" + time, params, heard);

            Map<String, Object> bodyMap = JSON.parseObject(body);

            log.info(itfGreeSynRule.getSalesWebsitNumber() + "时间为：" + time + "【工单同步数据内容】:" + bodyMap.toString());

            String s = bodyMap.get("data").toString();


            JSONArray jsonArray = JSONUtil.parseArray(s);


            itfGreeSynRule.setLastSynUpdTime(new Date());

            itfGreeSynRule.updateById();

            List<ItfGreeSynDownloadRecBean> itfGreeSynDownloadRecBeans = JSONUtil.toList(jsonArray, ItfGreeSynDownloadRecBean.class);

            if (CollectionUtils.isEmpty(itfGreeSynDownloadRecBeans))
                continue;

            List<ItfGreeSynDownloadRec> itfGreeSynDownloadRecs = new ArrayList<>();

            Date date = new Date();


            for (ItfGreeSynDownloadRecBean itfGreeSynDownloadRecBean : itfGreeSynDownloadRecBeans) {
                ItfGreeSynDownloadRec itfGreeSynDownloadRec = new ItfGreeSynDownloadRec();
                itfGreeSynDownloadRec.setSynReqTime(date);
                itfGreeSynDownloadRec.setGreeStatSynStatus(0);
                itfGreeSynDownloadRec.setSalesWebsitNumber(itfGreeSynRule.getSalesWebsitNumber());

                itfGreeSynDownloadRec.setWorkOrderId(itfGreeSynDownloadRecBean.getWorkOrderId());
                itfGreeSynDownloadRec.setUpdatedTimestamp(DateUtil.date(Convert.toLong(itfGreeSynDownloadRecBean.getUpdatedTimestamp())));
                itfGreeSynDownloadRec.setOrderNode(itfGreeSynDownloadRecBean.getOrderNode());
                itfGreeSynDownloadRec.setRegionOutletsNo(itfGreeSynDownloadRecBean.getRegionOutletsNo());
                itfGreeSynDownloadRec.setRegionOutletsName(itfGreeSynDownloadRecBean.getRegionOutletsName());
                itfGreeSynDownloadRecs.add(itfGreeSynDownloadRec);
            }

            List<ItfGreeSynDownloadRec> itfGreeSynDownloadRecList = itfGreeSynDownloadRecs.stream()
                    .sorted(Comparator.comparing(ItfGreeSynDownloadRec::getUpdatedTimestamp).reversed()).collect(Collectors.toList());

            itfGreeSynDownloadRecService.lambdaUpdate().in(ItfGreeSynDownloadRec::getWorkOrderId,
                    itfGreeSynDownloadRecs.stream().map(ItfGreeSynDownloadRec::getWorkOrderId)
                            .collect(Collectors.toList()))
                    .eq(ItfGreeSynDownloadRec::getGreeStatSynStatus, 0)
                    .le(ItfGreeSynDownloadRec::getUpdatedTimestamp,new Date())
                    .set(ItfGreeSynDownloadRec::getGreeStatSynStatus, 9)
                    .update();

            itfGreeSynDownloadRecService.saveBatch(itfGreeSynDownloadRecs);

            itfGreeSynRule.setLastSynTime(itfGreeSynDownloadRecList.get(0).getUpdatedTimestamp());
            itfGreeSynRule.setLastSynUpdTime(new Date());

            itfGreeSynRule.updateById();
        }


    }


    /**
     * 获取总部接口的请求token
     *
     * @return
     * @throws RemoteServiceException
     */
    public String getAccessTokenV3() throws RemoteServiceException {
        String redisKey = Constant.RedisPrefix.TOKEN_GREE_V3;
        String token = (String) redisUtil.get(redisKey);
//        if(token != null){
//            return token;
//        }
        MultiValueMap<String, String> forms = new LinkedMultiValueMap<String, String>();

        forms.put("client_id", Collections.singletonList(clientId));
        forms.put("client_secret", Collections.singletonList(v3ClientSecert));

        forms.put("grant_type", Collections.singletonList("client_credentials"));
        String s = HttpUtils.requestXwww(v3TokenUrl, forms);
        //String s = HttpUtils.requestPostForm(tokenUrl, map, null);
        //log.info("【获取总部token】response:{}", s);
        Map<String, Object> bodyMap = JSON.parseObject(s);
        if (!bodyMap.get("statusCode").toString().equals("200")) {
            log.info("【获取总部token】response:{}", s);
        }
        Map<String, Object> map = JSON.parseObject(bodyMap.get("data").toString());


        return (String) map.get("access_token");
    }


    public synchronized void syncDetail() {

        while (true) {


            List<ItfGreeSynDownloadRec> itfGreeSynDownloadRecs = itfGreeSynDownloadRecService.lambdaQuery()
                    .in(ItfGreeSynDownloadRec::getGreeStatSynStatus, 0)
                    .last("limit 20")
                    .orderByAsc(ItfGreeSynDownloadRec::getGreeStatSynStatus)
                    .list();
            if (CollectionUtils.isEmpty(itfGreeSynDownloadRecs))
                return;


            try {

                String batch = IdWorker.getIdStr();

                String accessToken = this.getAccessTokenV3();


                Map<String, String> params = new HashMap<>();

                Map<String, String> heard = new HashMap<>();
                heard.put("authorization", "Bearer " + accessToken);

                WorkOrderSearchReq workOrderSearchReq = new WorkOrderSearchReq();

                List<String> collect = itfGreeSynDownloadRecs.stream().map(ItfGreeSynDownloadRec::getWorkOrderId).distinct().collect(Collectors.toList());
                List<Long> syncIds = itfGreeSynDownloadRecs.stream().map(ItfGreeSynDownloadRec::getSynReqId).collect(Collectors.toList());



                workOrderSearchReq.setWorkOrderIds(collect);

                String json = JSONUtil.toJsonStr(workOrderSearchReq);



                String body = HttpRequest.post(greeUrl + Constant.gree.POST_WORKER_ORDER_DETAIL)
                        .headerMap(heard, false)
                        .body(json).execute().body();

                log.info("工单同步详情返回：" + body+"同步token"+heard.toString()+"请求参数"+json);

                Map<String, Object> bodyMap = JSON.parseObject(body);
                String s = bodyMap.get("data").toString();

                itfGreeSynDownloadRecService.lambdaUpdate()
                        .in(ItfGreeSynDownloadRec::getWorkOrderId, collect)
                        .notIn(ItfGreeSynDownloadRec::getGreeStatSynStatus,1,9)
                        .set(ItfGreeSynDownloadRec::getGreeStatSynStatus, 1)
                        .set(ItfGreeSynDownloadRec::getDownloadBatchNo, batch)
                        .set(ItfGreeSynDownloadRec::getGreeStatSynTime, new Date())
                        .update();



                if (StringUtil.isEmpty(s))
                    continue;


                JSONArray jsonArray = JSONUtil.parseArray(s);

                List<WorkOrderRelationBean> itfGreeSynDownloadRecBeans = JSONUtil.toList(jsonArray, WorkOrderRelationBean.class);


                List<ItfTblWorkOrderFeedback> feedbackRespList = new ArrayList<>();
                List<ItfTblCollectionCharge> collectionChargeList = new ArrayList<>();
                List<ItfTblCollectionChargeDetail> collectionChargeDetails = new ArrayList<>();
                List<ItfTblOldProductOrderDetail> tblOldProductOrderDetailList = new ArrayList<>();
                List<ItfTblWorkOrderAddComplain> tblWorkOrderAddComplainList = new ArrayList<>();
                List<ItfTblWorkOrderAppoint> tblWorkOrderAppointList = new ArrayList<>();
                List<ItfTblWorkOrderCancel> tblWorkOrderCancelList = new ArrayList<>();
                List<ItfTblWorkOrderComplainFeedback> tblWorkOrderComplainFeedbackList = new ArrayList<>();
                List<ItfTblWorkOrderComplain> tblWorkOrderComplainList = new ArrayList<>();
                List<ItfTblWorkOrderCompleteInfo> tblWorkOrderCompleteInfoList = new ArrayList<>();
                List<ItfTblWorkOrderConsult> tblWorkOrderConsultList = new ArrayList<>();
                List<ItfTblWorkOrderDetailBarcode> tblWorkOrderDetailBarcodeList = new ArrayList<>();
                List<ItfTblWorkOrderDetailRelocation> tblWorkOrderDetailRelocationList = new ArrayList<>();
                List<ItfTblWorkOrderDispatchOutlets> tblWorkOrderDispatchOutletsList = new ArrayList<>();
                List<ItfTblWorkOrderEngineer> tblWorkOrderEngineerList = new ArrayList<>();
                List<ItfTblWorkOrderEvaluation> tblWorkOrderEvaluationList = new ArrayList<>();
                List<ItfTblWorkOrderExt> tblWorkOrderExtList = new ArrayList<>();
                List<ItfTblWorkOrderIndicators> tblWorkOrderIndicatorsList = new ArrayList<>();
                List<ItfTblWorkOrderSign> tblWorkOrderSignList = new ArrayList<>();
                List<ItfTblWorkOrderTag> tblWorkOrderTagList = new ArrayList<>();
                List<ItfTblWorkOrderCharge> workOrderChargeList = new ArrayList<>();
                List<ItfTblWorkOrderCollectionComplain> workOrderCollectionComplainList = new ArrayList<>();

                List<ItfTblWorkOrderDispatchEngineer> tblWorkOrderDispatchEngineerList = new ArrayList<>();
                List<ItfTblWorkOrderStatus> tblWorkOrderStatusList = new ArrayList<>();

                List<ItfTblWorkOrderCollection> workOrderCollectionList = new ArrayList<>();
                List<ItfTblCollectionRepair> collectionRepairList = new ArrayList<>();
                List<ItfTblCollectionRepairPart> collectionRepairPartList = new ArrayList<>();
                List<ItfTblCollectionRepairProject> collectionRepairProjectList = new ArrayList<>();
                List<ItfTblCollectionRepairSymptomReason> collectionRepairSymptomReasonList = new ArrayList<>();
                List<ItfTblWorkOrderDetail> tblWorkOrderDetailList = new ArrayList<>();

                List<ItfTblWorkOrder> itfTblWorkOrders = new ArrayList<>();


                for (WorkOrderRelationBean itfGreeSynDownloadRecBean : itfGreeSynDownloadRecBeans) {

                    List<ItfGreeSynDownloadRec> itfGreeSynDownloadRecList = itfGreeSynDownloadRecs.stream()
                            .filter(item -> item.getWorkOrderId().equals(itfGreeSynDownloadRecBean.getId())).collect(Collectors.toList());

                    itfGreeSynDownloadRecBean.setDownloadBatchNo(batch);

                    tblWorkOrderDetailList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderDetailList());


                    feedbackRespList.addAll(itfGreeSynDownloadRecBean.getFeedbackRespList());

                    for (ItfTblCollectionChargeBean itfTblCollectionChargeBean : itfGreeSynDownloadRecBean.getCollectionChargeList()) {
                        ItfTblCollectionCharge itfTblCollectionCharge = BeanUtil.toBean(itfTblCollectionChargeBean, ItfTblCollectionCharge.class);
                        collectionChargeList.add(itfTblCollectionCharge);
                        collectionChargeDetails.addAll(itfTblCollectionChargeBean.getCollectionChargeDetails());
                    }

                    tblOldProductOrderDetailList.addAll(itfGreeSynDownloadRecBean.getTblOldProductOrderDetailList());

                    tblWorkOrderAddComplainList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderAddComplainList());

                    tblWorkOrderAppointList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderAppointList());
                    tblWorkOrderCancelList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderCancelList());
                    tblWorkOrderComplainFeedbackList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderComplainFeedbackList());
                    tblWorkOrderComplainList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderComplainList());
                    tblWorkOrderCompleteInfoList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderCompleteInfoList());
                    tblWorkOrderConsultList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderConsultList());
                    tblWorkOrderDetailBarcodeList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderDetailBarcodeList());
                    tblWorkOrderDetailRelocationList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderDetailRelocationList());
                    tblWorkOrderDispatchOutletsList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderDispatchOutletsList());
                    tblWorkOrderEngineerList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderEngineerList());
                    tblWorkOrderEvaluationList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderEvaluationList());
                    tblWorkOrderExtList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderExtList());
                    tblWorkOrderIndicatorsList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderIndicatorsList());
                    tblWorkOrderSignList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderSignList());
                    tblWorkOrderTagList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderTagList());
                    workOrderCollectionComplainList.addAll(itfGreeSynDownloadRecBean.getWorkOrderCollectionComplainList());
                    workOrderChargeList.addAll(itfGreeSynDownloadRecBean.getWorkOrderChargeList());


                    for (ItfTblWorkOrderCollection0Bean itfTblWorkOrderCollectionBean : itfGreeSynDownloadRecBean.getWorkOrderCollectionList()) {

                        ItfTblWorkOrderCollection itfTblWorkOrderCollection = BeanUtil.toBean(itfTblWorkOrderCollectionBean, ItfTblWorkOrderCollection.class);
                        workOrderCollectionList.add(itfTblWorkOrderCollectionBean);

                        collectionRepairList.addAll(itfTblWorkOrderCollectionBean.getCollectionRepairList());
                        collectionRepairPartList.addAll(itfTblWorkOrderCollectionBean.getCollectionRepairPartList());
                        collectionRepairProjectList.addAll(itfTblWorkOrderCollectionBean.getCollectionRepairProjectList());
                        collectionRepairSymptomReasonList.addAll(itfTblWorkOrderCollectionBean.getCollectionRepairSymptomReasonList());
                    }
                    tblWorkOrderDispatchEngineerList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderDispatchEngineerList());
                    tblWorkOrderStatusList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderStatusList());

                    ItfTblWorkOrder itfTblWorkOrder = BeanUtil.toBean(itfGreeSynDownloadRecBean, ItfTblWorkOrder.class);
                    itfTblWorkOrders.add(itfTblWorkOrder);

                }

                if (!CollectionUtils.isEmpty(feedbackRespList)) {
                    feedbackRespList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderFeedbackService.saveBatch(feedbackRespList);
                }

                if (!CollectionUtils.isEmpty(tblWorkOrderDetailList)) {
                    tblWorkOrderDetailList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderDetailService.saveBatch(tblWorkOrderDetailList);
                }

                if (!CollectionUtils.isEmpty(collectionChargeList)) {
                    collectionChargeList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionChargeService.saveBatch(collectionChargeList);
                }
                if (!CollectionUtils.isEmpty(collectionChargeDetails)) {
                    collectionChargeDetails.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionChargeDetailService.saveBatch(collectionChargeDetails);
                }

                if (!CollectionUtils.isEmpty(tblOldProductOrderDetailList)) {
                    tblOldProductOrderDetailList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblOldProductOrderDetailService.saveBatch(tblOldProductOrderDetailList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderAddComplainList)) {
                    tblWorkOrderAddComplainList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderAddComplainService.saveBatch(tblWorkOrderAddComplainList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderAppointList)) {
                    tblWorkOrderAppointList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderAppointService.saveBatch(tblWorkOrderAppointList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderCancelList)) {
                    tblWorkOrderCancelList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderCancelService.saveBatch(tblWorkOrderCancelList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderComplainList)) {
                    tblWorkOrderComplainList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderComplainService.saveBatch(tblWorkOrderComplainList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderComplainFeedbackList)) {
                    tblWorkOrderComplainFeedbackList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderComplainFeedbackService.saveBatch(tblWorkOrderComplainFeedbackList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderCompleteInfoList)) {
                    tblWorkOrderCompleteInfoList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderCompleteInfoService.saveBatch(tblWorkOrderCompleteInfoList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderConsultList)) {
                    tblWorkOrderConsultList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderConsultService.saveBatch(tblWorkOrderConsultList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderDetailBarcodeList)) {
                    tblWorkOrderDetailBarcodeList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderDetailBarcodeService.saveBatch(tblWorkOrderDetailBarcodeList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderDetailRelocationList)) {
                    tblWorkOrderDetailRelocationList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderDetailRelocationService.saveBatch(tblWorkOrderDetailRelocationList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderDispatchOutletsList)) {
                    tblWorkOrderDispatchOutletsList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderDispatchOutletsService.saveBatch(tblWorkOrderDispatchOutletsList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderEngineerList)) {
                    tblWorkOrderEngineerList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderEngineerService.saveBatch(tblWorkOrderEngineerList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderEvaluationList)) {
                    tblWorkOrderEvaluationList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderEvaluationService.saveBatch(tblWorkOrderEvaluationList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderExtList)) {
                    tblWorkOrderExtList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderExtService.saveBatch(tblWorkOrderExtList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderIndicatorsList)) {
                    tblWorkOrderIndicatorsList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderIndicatorsService.saveBatch(tblWorkOrderIndicatorsList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderSignList)) {
                    tblWorkOrderSignList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderSignService.saveBatch(tblWorkOrderSignList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderTagList)) {
                    tblWorkOrderTagList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderTagService.saveBatch(tblWorkOrderTagList);
                }
                if (!CollectionUtils.isEmpty(workOrderChargeList)) {
                    workOrderChargeList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderChargeService.saveBatch(workOrderChargeList);
                }
                if (!CollectionUtils.isEmpty(workOrderCollectionComplainList)) {
                    workOrderCollectionComplainList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderCollectionComplainService.saveBatch(workOrderCollectionComplainList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderDispatchEngineerList)) {
                    tblWorkOrderDispatchEngineerList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderDispatchEngineerService.saveBatch(tblWorkOrderDispatchEngineerList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderStatusList)) {
                    tblWorkOrderStatusList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderStatusService.saveBatch(tblWorkOrderStatusList);
                }
                if (!CollectionUtils.isEmpty(workOrderCollectionList)) {
                    workOrderCollectionList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderCollectionService.saveBatch(workOrderCollectionList);
                }
                if (!CollectionUtils.isEmpty(collectionRepairList)) {
                    collectionRepairList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionRepairService.saveBatch(collectionRepairList);
                }
                if (!CollectionUtils.isEmpty(collectionRepairPartList)) {
                    collectionRepairPartList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionRepairPartService.saveBatch(collectionRepairPartList);
                }
                if (!CollectionUtils.isEmpty(collectionRepairProjectList)) {
                    collectionRepairProjectList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionRepairProjectService.saveBatch(collectionRepairProjectList);
                }
                if (!CollectionUtils.isEmpty(collectionRepairSymptomReasonList)) {
                    collectionRepairSymptomReasonList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionRepairSymptomReasonService.saveBatch(collectionRepairSymptomReasonList);
                }

                if (!CollectionUtils.isEmpty(itfTblWorkOrders)) {
                    itfTblWorkOrders.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderService.saveBatch(itfTblWorkOrders);
                }


            } catch (Exception e) {
                List<Long> syncIds = itfGreeSynDownloadRecs.stream().map(ItfGreeSynDownloadRec::getSynReqId).collect(Collectors.toList());

                log.error("工单同步错误" + e.toString());

                itfGreeSynDownloadRecService.lambdaUpdate()
                        .in(ItfGreeSynDownloadRec::getSynReqId, syncIds)
                        .set(ItfGreeSynDownloadRec::getGreeStatSynErrMsg, e.toString())
                        .set(ItfGreeSynDownloadRec::getGreeStatSynTime, new Date())
                        .set(ItfGreeSynDownloadRec::getGreeStatSynStatus, 99).update();


            }
        }

    }

    public void task3() {
        List<ItfTblWorkOrder> itfTblWorkOrderList = itfTblWorkOrderService.lambdaQuery()
                .lt(ItfTblWorkOrder::getSynTimes, 10)
                .eq(ItfTblWorkOrder::getSynStatus, 0)
                .orderByAsc(ItfTblWorkOrder::getSynReqId).list();

        if (CollectionUtils.isEmpty(itfTblWorkOrderList))
            return;

        for (ItfTblWorkOrder itfTblWorkOrder : itfTblWorkOrderList) {

            try {
                WorkerOrderSyncBean workerOrderSyncBean = new WorkerOrderSyncBean();
                workerOrderSyncBean.setInWorkOrderId(itfTblWorkOrder.getId());
                workerOrderSyncBean.setInSynReqId(Convert.toStr(itfTblWorkOrder.getSynReqId()));
                workerOrderSyncBean.setInDownloadBatchNo(itfTblWorkOrder.getDownloadBatchNo());
                WorkerOrderSyncBean workerOrderSyncBean1 = syncMapper.orderExecute(workerOrderSyncBean);

                log.info("工单同步返回内容：" + workerOrderSyncBean1);

                if (!workerOrderSyncBean1.getOutSynStatus()) {


                    itfTblWorkOrder.setSynTimes(itfTblWorkOrder.getSynTimes() + 1);
                    itfTblWorkOrder.setSynErrMsg(workerOrderSyncBean1.getOutSynErrMsg());
                    itfTblWorkOrder.updateById();
                }
            } catch (Exception e) {
                itfTblWorkOrder.setSynTimes(itfTblWorkOrder.getSynTimes() + 1);
                itfTblWorkOrder.setSynErrMsg(e.toString());
                itfTblWorkOrder.updateById();

                log.info("工单同步返回日志错误内容：" + itfTblWorkOrder.getDownloadBatchNo() + e);
            }


        }


    }

    public synchronized void syncDetailFail() {

        while (true) {


            List<ItfGreeSynDownloadRec> itfGreeSynDownloadRecs = itfGreeSynDownloadRecService.lambdaQuery()
                    .in(ItfGreeSynDownloadRec::getGreeStatSynStatus, 99)
                    .lt(ItfGreeSynDownloadRec::getSynTimes, 10)
                    .last("limit 1")
                    .orderByAsc(ItfGreeSynDownloadRec::getGreeStatSynStatus)
                    .list();
            if (CollectionUtils.isEmpty(itfGreeSynDownloadRecs))
                return;


            try {

                String batch = IdWorker.getIdStr();

                String accessToken = this.getAccessTokenV3();


                Map<String, String> params = new HashMap<>();

                Map<String, String> heard = new HashMap<>();
                heard.put("authorization", "Bearer " + accessToken);

                WorkOrderSearchReq workOrderSearchReq = new WorkOrderSearchReq();

                List<String> collect = itfGreeSynDownloadRecs.stream().map(ItfGreeSynDownloadRec::getWorkOrderId).collect(Collectors.toList());
                List<Long> syncIds = itfGreeSynDownloadRecs.stream().map(ItfGreeSynDownloadRec::getSynReqId).collect(Collectors.toList());

                workOrderSearchReq.setWorkOrderIds(collect);

                String json = JSONUtil.toJsonStr(workOrderSearchReq);




                String body = HttpRequest.post(greeUrl + Constant.gree.POST_WORKER_ORDER_DETAIL)
                        .headerMap(heard, false)
                        .body(json).execute().body();

                log.info("工单同步详情返回：" + body+"同步token"+heard.toString()+"请求参数"+json);

                Map<String, Object> bodyMap = JSON.parseObject(body);
                String s = bodyMap.get("data").toString();

                ItfGreeSynDownloadRec itfGreeSynDownloadRec = itfGreeSynDownloadRecs.get(0);

                itfGreeSynDownloadRec.setGreeStatSynTime(new Date());
                itfGreeSynDownloadRec.setGreeStatSynStatus(1);


                itfGreeSynDownloadRec.updateById();


                if (StringUtil.isEmpty(s))
                    continue;


                JSONArray jsonArray = JSONUtil.parseArray(s);

                List<WorkOrderRelationBean> itfGreeSynDownloadRecBeans = JSONUtil.toList(jsonArray, WorkOrderRelationBean.class);


                List<ItfTblWorkOrderFeedback> feedbackRespList = new ArrayList<>();
                List<ItfTblCollectionCharge> collectionChargeList = new ArrayList<>();
                List<ItfTblCollectionChargeDetail> collectionChargeDetails = new ArrayList<>();
                List<ItfTblOldProductOrderDetail> tblOldProductOrderDetailList = new ArrayList<>();
                List<ItfTblWorkOrderAddComplain> tblWorkOrderAddComplainList = new ArrayList<>();
                List<ItfTblWorkOrderAppoint> tblWorkOrderAppointList = new ArrayList<>();
                List<ItfTblWorkOrderCancel> tblWorkOrderCancelList = new ArrayList<>();
                List<ItfTblWorkOrderComplainFeedback> tblWorkOrderComplainFeedbackList = new ArrayList<>();
                List<ItfTblWorkOrderComplain> tblWorkOrderComplainList = new ArrayList<>();
                List<ItfTblWorkOrderCompleteInfo> tblWorkOrderCompleteInfoList = new ArrayList<>();
                List<ItfTblWorkOrderConsult> tblWorkOrderConsultList = new ArrayList<>();
                List<ItfTblWorkOrderDetailBarcode> tblWorkOrderDetailBarcodeList = new ArrayList<>();
                List<ItfTblWorkOrderDetailRelocation> tblWorkOrderDetailRelocationList = new ArrayList<>();
                List<ItfTblWorkOrderDispatchOutlets> tblWorkOrderDispatchOutletsList = new ArrayList<>();
                List<ItfTblWorkOrderEngineer> tblWorkOrderEngineerList = new ArrayList<>();
                List<ItfTblWorkOrderEvaluation> tblWorkOrderEvaluationList = new ArrayList<>();
                List<ItfTblWorkOrderExt> tblWorkOrderExtList = new ArrayList<>();
                List<ItfTblWorkOrderIndicators> tblWorkOrderIndicatorsList = new ArrayList<>();
                List<ItfTblWorkOrderSign> tblWorkOrderSignList = new ArrayList<>();
                List<ItfTblWorkOrderTag> tblWorkOrderTagList = new ArrayList<>();
                List<ItfTblWorkOrderCharge> workOrderChargeList = new ArrayList<>();
                List<ItfTblWorkOrderCollectionComplain> workOrderCollectionComplainList = new ArrayList<>();

                List<ItfTblWorkOrderDispatchEngineer> tblWorkOrderDispatchEngineerList = new ArrayList<>();
                List<ItfTblWorkOrderStatus> tblWorkOrderStatusList = new ArrayList<>();

                List<ItfTblWorkOrderCollection> workOrderCollectionList = new ArrayList<>();
                List<ItfTblCollectionRepair> collectionRepairList = new ArrayList<>();
                List<ItfTblCollectionRepairPart> collectionRepairPartList = new ArrayList<>();
                List<ItfTblCollectionRepairProject> collectionRepairProjectList = new ArrayList<>();
                List<ItfTblCollectionRepairSymptomReason> collectionRepairSymptomReasonList = new ArrayList<>();
                List<ItfTblWorkOrderDetail> tblWorkOrderDetailList = new ArrayList<>();

                List<ItfTblWorkOrder> itfTblWorkOrders = new ArrayList<>();


                for (WorkOrderRelationBean itfGreeSynDownloadRecBean : itfGreeSynDownloadRecBeans) {

                    List<ItfGreeSynDownloadRec> itfGreeSynDownloadRecList = itfGreeSynDownloadRecs.stream()
                            .filter(item -> item.getWorkOrderId().equals(itfGreeSynDownloadRecBean.getId())).collect(Collectors.toList());

                    itfGreeSynDownloadRecBean.setDownloadBatchNo(batch);

                    tblWorkOrderDetailList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderDetailList());


                    feedbackRespList.addAll(itfGreeSynDownloadRecBean.getFeedbackRespList());

                    for (ItfTblCollectionChargeBean itfTblCollectionChargeBean : itfGreeSynDownloadRecBean.getCollectionChargeList()) {
                        ItfTblCollectionCharge itfTblCollectionCharge = BeanUtil.toBean(itfTblCollectionChargeBean, ItfTblCollectionCharge.class);
                        collectionChargeList.add(itfTblCollectionCharge);
                        collectionChargeDetails.addAll(itfTblCollectionChargeBean.getCollectionChargeDetails());
                    }

                    tblOldProductOrderDetailList.addAll(itfGreeSynDownloadRecBean.getTblOldProductOrderDetailList());

                    tblWorkOrderAddComplainList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderAddComplainList());

                    tblWorkOrderAppointList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderAppointList());
                    tblWorkOrderCancelList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderCancelList());
                    tblWorkOrderComplainFeedbackList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderComplainFeedbackList());
                    tblWorkOrderComplainList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderComplainList());
                    tblWorkOrderCompleteInfoList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderCompleteInfoList());
                    tblWorkOrderConsultList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderConsultList());
                    tblWorkOrderDetailBarcodeList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderDetailBarcodeList());
                    tblWorkOrderDetailRelocationList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderDetailRelocationList());
                    tblWorkOrderDispatchOutletsList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderDispatchOutletsList());
                    tblWorkOrderEngineerList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderEngineerList());
                    tblWorkOrderEvaluationList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderEvaluationList());
                    tblWorkOrderExtList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderExtList());
                    tblWorkOrderIndicatorsList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderIndicatorsList());
                    tblWorkOrderSignList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderSignList());
                    tblWorkOrderTagList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderTagList());
                    workOrderCollectionComplainList.addAll(itfGreeSynDownloadRecBean.getWorkOrderCollectionComplainList());
                    workOrderChargeList.addAll(itfGreeSynDownloadRecBean.getWorkOrderChargeList());


                    for (ItfTblWorkOrderCollection0Bean itfTblWorkOrderCollectionBean : itfGreeSynDownloadRecBean.getWorkOrderCollectionList()) {

                        ItfTblWorkOrderCollection itfTblWorkOrderCollection = BeanUtil.toBean(itfTblWorkOrderCollectionBean, ItfTblWorkOrderCollection.class);
                        workOrderCollectionList.add(itfTblWorkOrderCollectionBean);

                        collectionRepairList.addAll(itfTblWorkOrderCollectionBean.getCollectionRepairList());
                        collectionRepairPartList.addAll(itfTblWorkOrderCollectionBean.getCollectionRepairPartList());
                        collectionRepairProjectList.addAll(itfTblWorkOrderCollectionBean.getCollectionRepairProjectList());
                        collectionRepairSymptomReasonList.addAll(itfTblWorkOrderCollectionBean.getCollectionRepairSymptomReasonList());
                    }
                    tblWorkOrderDispatchEngineerList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderDispatchEngineerList());
                    tblWorkOrderStatusList.addAll(itfGreeSynDownloadRecBean.getTblWorkOrderStatusList());

                    ItfTblWorkOrder itfTblWorkOrder = BeanUtil.toBean(itfGreeSynDownloadRecBean, ItfTblWorkOrder.class);
                    itfTblWorkOrders.add(itfTblWorkOrder);

                }

                if (!CollectionUtils.isEmpty(feedbackRespList)) {
                    feedbackRespList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderFeedbackService.saveBatch(feedbackRespList);
                }

                if (!CollectionUtils.isEmpty(tblWorkOrderDetailList)) {
                    tblWorkOrderDetailList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderDetailService.saveBatch(tblWorkOrderDetailList);
                }

                if (!CollectionUtils.isEmpty(collectionChargeList)) {
                    collectionChargeList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionChargeService.saveBatch(collectionChargeList);
                }
                if (!CollectionUtils.isEmpty(collectionChargeDetails)) {
                    collectionChargeDetails.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionChargeDetailService.saveBatch(collectionChargeDetails);
                }

                if (!CollectionUtils.isEmpty(tblOldProductOrderDetailList)) {
                    tblOldProductOrderDetailList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblOldProductOrderDetailService.saveBatch(tblOldProductOrderDetailList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderAddComplainList)) {
                    tblWorkOrderAddComplainList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderAddComplainService.saveBatch(tblWorkOrderAddComplainList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderAppointList)) {
                    tblWorkOrderAppointList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderAppointService.saveBatch(tblWorkOrderAppointList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderCancelList)) {
                    tblWorkOrderCancelList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderCancelService.saveBatch(tblWorkOrderCancelList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderComplainList)) {
                    tblWorkOrderComplainList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderComplainService.saveBatch(tblWorkOrderComplainList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderComplainFeedbackList)) {
                    tblWorkOrderComplainFeedbackList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderComplainFeedbackService.saveBatch(tblWorkOrderComplainFeedbackList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderCompleteInfoList)) {
                    tblWorkOrderCompleteInfoList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderCompleteInfoService.saveBatch(tblWorkOrderCompleteInfoList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderConsultList)) {
                    tblWorkOrderConsultList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderConsultService.saveBatch(tblWorkOrderConsultList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderDetailBarcodeList)) {
                    tblWorkOrderDetailBarcodeList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderDetailBarcodeService.saveBatch(tblWorkOrderDetailBarcodeList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderDetailRelocationList)) {
                    tblWorkOrderDetailRelocationList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderDetailRelocationService.saveBatch(tblWorkOrderDetailRelocationList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderDispatchOutletsList)) {
                    tblWorkOrderDispatchOutletsList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderDispatchOutletsService.saveBatch(tblWorkOrderDispatchOutletsList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderEngineerList)) {
                    tblWorkOrderEngineerList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderEngineerService.saveBatch(tblWorkOrderEngineerList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderEvaluationList)) {
                    tblWorkOrderEvaluationList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderEvaluationService.saveBatch(tblWorkOrderEvaluationList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderExtList)) {
                    tblWorkOrderExtList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderExtService.saveBatch(tblWorkOrderExtList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderIndicatorsList)) {
                    tblWorkOrderIndicatorsList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderIndicatorsService.saveBatch(tblWorkOrderIndicatorsList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderSignList)) {
                    tblWorkOrderSignList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderSignService.saveBatch(tblWorkOrderSignList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderTagList)) {
                    tblWorkOrderTagList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderTagService.saveBatch(tblWorkOrderTagList);
                }
                if (!CollectionUtils.isEmpty(workOrderChargeList)) {
                    workOrderChargeList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderChargeService.saveBatch(workOrderChargeList);
                }
                if (!CollectionUtils.isEmpty(workOrderCollectionComplainList)) {
                    workOrderCollectionComplainList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderCollectionComplainService.saveBatch(workOrderCollectionComplainList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderDispatchEngineerList)) {
                    tblWorkOrderDispatchEngineerList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderDispatchEngineerService.saveBatch(tblWorkOrderDispatchEngineerList);
                }
                if (!CollectionUtils.isEmpty(tblWorkOrderStatusList)) {
                    tblWorkOrderStatusList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderStatusService.saveBatch(tblWorkOrderStatusList);
                }
                if (!CollectionUtils.isEmpty(workOrderCollectionList)) {
                    workOrderCollectionList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderCollectionService.saveBatch(workOrderCollectionList);
                }
                if (!CollectionUtils.isEmpty(collectionRepairList)) {
                    collectionRepairList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionRepairService.saveBatch(collectionRepairList);
                }
                if (!CollectionUtils.isEmpty(collectionRepairPartList)) {
                    collectionRepairPartList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionRepairPartService.saveBatch(collectionRepairPartList);
                }
                if (!CollectionUtils.isEmpty(collectionRepairProjectList)) {
                    collectionRepairProjectList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionRepairProjectService.saveBatch(collectionRepairProjectList);
                }
                if (!CollectionUtils.isEmpty(collectionRepairSymptomReasonList)) {
                    collectionRepairSymptomReasonList.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblCollectionRepairSymptomReasonService.saveBatch(collectionRepairSymptomReasonList);
                }

                if (!CollectionUtils.isEmpty(itfTblWorkOrders)) {
                    itfTblWorkOrders.stream().forEach(item -> item.setDownloadBatchNo(batch));
                    itfTblWorkOrderService.saveBatch(itfTblWorkOrders);
                }


            } catch (Exception e) {
                List<Long> syncIds = itfGreeSynDownloadRecs.stream().map(ItfGreeSynDownloadRec::getSynReqId).collect(Collectors.toList());

                log.error("工单同步错误" + e.toString());

                ItfGreeSynDownloadRec itfGreeSynDownloadRec = itfGreeSynDownloadRecs.get(0);

                itfGreeSynDownloadRec.setGreeStatSynErrMsg(e.toString());
                itfGreeSynDownloadRec.setGreeStatSynTime(new Date());
                itfGreeSynDownloadRec.setGreeStatSynStatus(99);
                itfGreeSynDownloadRec.setSynTimes(itfGreeSynDownloadRec.getSynTimes()+1);

                itfGreeSynDownloadRec.updateById();


            }
        }

    }
}
