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

import com.alibaba.fastjson.JSONObject;
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.GDOrderStatusEnum;
import com.zfire.jiasm.syncdata.dao.mapper.*;
import com.zfire.jiasm.syncdata.gree.GreeLogic;
import com.zfire.jiasm.syncdata.helper.GreeFileResponseHelper;
import com.zfire.jiasm.syncdata.helper.GreeFileSaveHelper;
import com.zfire.jiasm.syncdata.http.PostObjectSample;
import com.zfire.jiasm.syncdata.parameter.*;
import com.zfire.jiasm.syncdata.plus.entity.*;
import com.zfire.jiasm.syncdata.plus.service.*;
import com.zfire.jiasm.syncdata.response.ResultData;
import com.zfire.jiasm.syncdata.response.WebsitSH;
import com.zfire.jiasm.syncdata.service.OrderService;
import com.zfire.jiasm.syncdata.service.PublicService;
import com.zfire.jiasm.syncdata.service.orderBaseInstallCjOrWgService;
import com.zfire.jiasm.syncdata.utils.MyBeanUtils;
import com.zfire.jiasm.syncdata.utils.oss.OSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

@Slf4j
@Service
public class OrderBaseInstallCjOrWgServiceImpl implements orderBaseInstallCjOrWgService {
    @Resource
    OrderBaseService orderBaseService;
    @Resource
    GDOrderBaseMapper gdOrderBaseMapper;
    @Resource
    BaseService baseService;
    @Resource
    ItfTblAzAssignLcLsUploadService itfTblAzAssignLcLsUploadService;
    @Resource
    OrderInstallDetailService orderInstallDetailService;
    @Resource
    DetailOrderInstallMapper detailOrderInstallMapper;
    @Resource
    BrandWebsitService brandWebsitService;
    @Resource
    ItfTblWangdianWxgDownloadService itfTblWangdianWxgDownloadService;
    @Resource
    FileOrderDetailMapper fileOrderDetailMapper;
    @Resource
    OrderDetailFileService orderDetailFileService;
    @Resource
    ItfTblAzWgmxJyktFjUploadService itfTblAzWgmxJyktFjUploadService;
    @Resource
    ItfTblAzWgmxSyktFjUploadService itfTblAzWgmxSyktFjUploadService;
    @Resource
    ItfTblAzWgmxQitaFjUploadService itfTblAzWgmxQitaFjUploadService;
    @Resource
    ItfTblAzWgmxJyktUploadService itfTblAzWgmxJyktUploadService;
    @Resource
    ItfTblAzWgmxSyktUploadService itfTblAzWgmxSyktUploadService;
    @Resource
    ItfTblAzWgmxQitaUploadService itfTblAzWgmxQitaUploadService;
    @Resource
    CodeOrderInstallDetailMapper codeOrderInstallDetailMapper;
    @Resource
    OrderInstallDetailCodeService orderInstallDetailCodeService;
    @Resource
    ItfTblAzWgmxSyktTmmxLsUploadService itfTblAzWgmxSyktTmmxLsUploadService;
    @Resource
    PublicService publicService;
    @Resource
    OrderProductService orderProductService;
    @Resource
    OSSUtil ossUtil;
    @Resource
    GreeLogic greeLogic;
    @Resource
    OrderInstallOperatingLogService orderInstallOperatingLogService;
    @Resource
    ItfTblazwgmxjyktfjService itfTblazwgmxjyktfjService;
    @Resource
    ItfTblazwgmxjyktfjCJMapper itfTblazwgmxjyktfjCJMapper;
    @Resource
    ItfTblazwgmxsyktfjService itfTblazwgmxsyktfjService;
    @Resource
    ItfTblazwgmxqitafjService itfTblazwgmxqitafjService;
    @Resource
    ItfTblazwgmxqitafjCJMapper itfTblazwgmxqitafjCJMapper;
    @Resource
    ItfTblazwgmxsykttmmxlsService itfTblazwgmxsykttmmxlsService;
    @Resource
    ItfTblazwgmxjyktService itfTblazwgmxjyktService;
    @Resource
    ItfTblazwgmxsyktService itfTblazwgmxsyktService;
    @Resource
    ItfTblazwgmxqitaService itfTblazwgmxqitaService;
    @Resource
    ItfInstallitemService itfInstallitemService;
    @Resource
    GetLcLsUploadMapper getLcLsUploadMapper;
    @Resource
    FaGreeSmsService faGreeSmsService;
    @Resource
    FaGreeSmsWorkerService faGreeSmsWorkerService;
    @Resource
    OrderService orderService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean orderBaseInstallCjOrWg(String cjOrWg, Brand brand, OutJiasmToGreeUploadTaskList ojtutl, Map<String, List<SysDictRef>> CategoryRefmap){
        OrderBase orderBase = orderBaseService.lambdaQuery()
                .eq(OrderBase::getId, ojtutl.getId())
                .last("limit 1")
                .one();
        if (orderBase !=null ){
            // 安装工单产品信息,操作日志,完工明细,完工明细附件
            // 预约改约-总部短信发送请求表,派服务人员-短信发送请求表 以主表的pgguid统一修改
            upPgguid(orderBase);

            // 生成待同步的安装工单主表
            ItfTblAzAssignLcLsUpload lcls = getItfTblAzAssignLcLsUpload(brand, orderBase);
            // 1： 插入工单主表数据
            if (!itfTblAzAssignLcLsUploadService.save(lcls)){
                return Boolean.FALSE;
            }

            // 同步采集明细数据
            List<OrderInstallDetail> details = orderInstallDetailService.lambdaQuery()
                    .eq(OrderInstallDetail::getOrderBaseId, orderBase.getId()).list();
            for (OrderInstallDetail detail:details){
                // 状态:3=临时保存;2=作废,1=已采集,0=未采集
                if ((detail.getStatus().equals(1) || detail.getStatus().equals(2)) &&
                        (("cj".equals(cjOrWg) && detail.getIsCjSend().equals("NO")) ||
                                ("wg".equals(cjOrWg) && detail.getIsWgSend().equals("NO")))) {
                    // 生成taskno,pgwcmxid 共用
                    String taskno = IdWorker.getIdStr();
                    String pgwcmxid = IdWorker.getIdStr();
                    // 根据产品类别名称 生成不同的完工明细数据
                    String mainName = CategoryRefmap.get(orderBase.getMainId()).get(0).getRefDictName();

                    // 数据生成
                    if (! installDtailJS(cjOrWg, brand, orderBase, lcls, detail, taskno, pgwcmxid, mainName,ojtutl.getSynReqId())){
                        return Boolean.FALSE;
                    }

                    // 数据明细检查(如果明细不存在则不写主表以免数据不完整)
                    if (!checkInstallitem(cjOrWg,taskno,detail,lcls,ojtutl)){
                        return Boolean.FALSE;
                    }
                }
            }
        }
        // 修改数据上传任务表中的状态   这里固定值 221
        publicService.updateUploadTaskListStatus(ojtutl.getOrderNo(),ojtutl.getSynReqId(),1,221);

        return Boolean.TRUE;
    }


    /**
     * 1100=已采集  1120=已完工
     * 1100=预保存工单  1110=作废 1301=服务人员待接收  1304=服务人员报完工  1309=已关闭工单
     * 完工明细(家用空调)上传记录表 itf_tblazwgmxjykt
     * 完工明细(商用空调)上传记录表 itf_tblAzWgmxSykt
     * 完工明细(其他)上传记录表 itf_tblAzWgmxQita
     * 完工明细表中必须有一个表存在数据
     *   向主表添加数据 itf_installItem
     *   若插入的数据为采访集状态 检查是否已产生了1120完工请求的数据
     *      若产生过完工数据再次生成一条完工数据
     */
    Boolean checkInstallitem(String cjOrWg,String taskno,OrderInstallDetail detail,ItfTblAzAssignLcLsUpload lcls,OutJiasmToGreeUploadTaskList ojtutl){
        int itemStatus = 1;
        int itemStat   = 1100;
        int itemSyjd   = 1301;
        int itemdqjd   = 1304;

        List<ItfTblazwgmxjykt> jys =  itfTblazwgmxjyktService.lambdaQuery()
                .eq(ItfTblazwgmxjykt::getSynTaskNo,taskno).list();
        List<ItfTblazwgmxsykt> sys =  itfTblazwgmxsyktService.lambdaQuery()
                .eq(ItfTblazwgmxsykt::getSynTaskNo,taskno).list();
        List<ItfTblazwgmxqita> qts =  itfTblazwgmxqitaService.lambdaQuery()
                .eq(ItfTblazwgmxqita::getSynTaskNo,taskno).list();
        // 状态:3=临时保存;2=作废,1=已采集,0=未采集

        if (CollectionUtils.isNotEmpty(jys) || CollectionUtils.isNotEmpty(sys) || CollectionUtils.isNotEmpty(qts)){
            if ("cj".equals(cjOrWg)){
                itemStatus = 1;
                if (detail.getStatus().equals(1)){
                    itemStat   = 1100;
                    itemSyjd   = 1301;
                    itemdqjd   = 1304;
                } else if (detail.getStatus().equals(2)){
                    itemStat   = 1110;
                    itemSyjd   = 1301;
                    itemdqjd   = 1304;
                }
            } else {
                itemStatus = 2;
                if (detail.getStatus().equals(1)){
                    itemStat   = 1120;
                    itemSyjd   = 1304;
                    itemdqjd   = 1309;
                } else if (detail.getStatus().equals(2)){
                    itemStat   = 1110;
                    itemSyjd   = 1304;
                    itemdqjd   = 1309;
                }
            }

            // itf_installitem 安装主表上传同步记录表
            ItfInstallitem m = new ItfInstallitem();
            LcLsUploadPar req = new LcLsUploadPar();
            req.setOrderNo(lcls.getWorkerOrderNo());
            req.setSynReqId(lcls.getSynReqId());
            m = getLcLsUploadMapper.getInstallitem(req);
            m.setSynTaskNo(taskno);
            m.setStatus(detail.getStatus());
            m.setStat(itemStat);
            m.setSyjd(itemSyjd * 1L);
            m.setDqjd(itemdqjd * 1L);
            if (!itfInstallitemService.save(m)){
                return Boolean.FALSE;
            }
            // 检查是否已产生了1120完工请求
            if (itemStat == 1100){
                List<ItfInstallitem> old = itfInstallitemService.lambdaQuery()
                        .eq(ItfInstallitem::getWorkerOrderNo,ojtutl.getId())
                        .eq(ItfInstallitem::getStat,1120)
                        .lt(ItfInstallitem::getSynReqTime,new Date()).list();
                if (CollectionUtils.isNotEmpty(old)){
                    taskno = IdWorker.getIdStr();
                    itemStatus = 2;
                    if (detail.getStatus().equals(1)) {
                        itemStat   = 1120;
                        itemSyjd   = 1304;
                        itemdqjd   = 1309;
                    } else if (detail.getStatus().equals(2)){
                        itemStat   = 1110;
                        itemSyjd   = 1304;
                        itemdqjd   = 1309;
                    }
                    ItfInstallitem mt = new ItfInstallitem();
                    MyBeanUtils.copyProperties(lcls,mt);
                    mt.setSynTaskNo(taskno);
                    mt.setStatus(itemStatus);
                    mt.setId(lcls.getPgguid());
                    mt.setStat(itemStat);
                    mt.setSyjd(itemSyjd * 1L);
                    mt.setDqjd(itemdqjd * 1L);
                    if (!itfInstallitemService.save(mt)){
                        return Boolean.FALSE;
                    }
                }
            }
            // 修改
            if ("cj".equals(cjOrWg)){
                if (detail.getIsCjSend().equals("NO")){
                    detail.setIsCjSend("YES");
                    detail.updateById();
                }
            } else {
                if (detail.getIsWgSend().equals("NO")){
                    detail.setIsWgSend("YES");
                    detail.updateById();
                }
            }
        }
        return Boolean.TRUE;
    }


    private void upPgguid(OrderBase orderBase) {
        List<OrderProduct> upOps = new ArrayList<>();
        List<OrderInstallOperatingLog> uplog = new ArrayList<>();
        List<OrderInstallDetail> updetail = new ArrayList<>();
        List<FaGreeSmsWorker> upfgsw = new ArrayList<>();
        List<FaGreeSms> upfgs = new ArrayList<>();

        List<OrderProduct> ops = orderProductService.lambdaQuery()
                .eq(OrderProduct::getOrderBaseId, orderBase.getId()).list();
        for (OrderProduct o:ops){
            if (StringUtils.isEmpty(o.getPgguid())){
                o.setPgguid(orderBase.getPgguid());
                upOps.add(o);
            }
        }
        // 操作日志
        List<OrderInstallOperatingLog> olog = orderInstallOperatingLogService.lambdaQuery()
                .eq(OrderInstallOperatingLog::getOrderBaseId, orderBase.getId()).list();
        for (OrderInstallOperatingLog o:olog){
            if (org.apache.commons.lang3.StringUtils.isEmpty(o.getPgguid())){
                o.setPgguid(orderBase.getPgguid());
                uplog.add(o);
            }
        }
        // 采集明细
        List<OrderInstallDetail> od = orderInstallDetailService.lambdaQuery()
                .eq(OrderInstallDetail::getOrderBaseId, orderBase.getId()).list();
        for (OrderInstallDetail o:od){
            if (org.apache.commons.lang3.StringUtils.isEmpty(o.getPgguid())){
                o.setPgguid(orderBase.getPgguid());
                updetail.add(o);
            }
        }
        // 预约改约-总部短信发送请求表
        List<FaGreeSms> fgs = faGreeSmsService.lambdaQuery()
                .eq(FaGreeSms::getWorkerOrderNo, orderBase.getId())
                .eq(FaGreeSms::getAzorwx, 1).list();
        for (FaGreeSms o:fgs){
            if (StringUtils.isEmpty(o.getFjid())){
                o.setFjid(orderBase.getPgguid());
                upfgs.add(o);
            }
        }
        // 派服务人员-短信发送请求表
        List<FaGreeSmsWorker> fgsw = faGreeSmsWorkerService.lambdaQuery()
                .eq(FaGreeSmsWorker::getWorkerOrderNo, orderBase.getId())
                .eq(FaGreeSmsWorker::getAzorwx, 1).list();
        for (FaGreeSmsWorker o:fgsw){
            if (StringUtils.isEmpty(o.getFjid())){
                o.setFjid(orderBase.getPgguid());
                upfgsw.add(o);
            }
        }

        if (upOps.size()>0){
            orderProductService.updateBatchById(upOps);
            upOps.clear();
        }
        if (uplog.size()>0){
            orderInstallOperatingLogService.updateBatchById(uplog);
            uplog.clear();
        }
        if (updetail.size()>0){
            orderInstallDetailService.updateBatchById(updetail);
            updetail.clear();
        }
        if (fgs.size()>0){
            faGreeSmsService.updateBatchById(fgs);
            updetail.clear();
        }
        if (fgsw.size()>0){
            faGreeSmsWorkerService.updateBatchById(fgsw);
            fgsw.clear();
        }
    }

    @NotNull
    private ItfTblAzAssignLcLsUpload getItfTblAzAssignLcLsUpload(Brand brand, OrderBase orderBase) {
        OrderPar orderPar = new OrderPar();

        orderPar.setBrandId(brand.getBrandId());
        orderPar.setOrderNo(orderBase.getId());
        ItfTblAzAssignLcLsUpload lcls = gdOrderBaseMapper.getAzOrderBaseCj(orderPar);
        // 网点参数处理
        setWebsit(brand, orderBase, lcls);
        // 省市等信息单独处理 转成格力总部的信息
        orderService.tpPositionTosh(lcls);
        return lcls;
    }

    private void setWebsit(Brand brand, OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls) {
        WebsitSH websitSH = new WebsitSH();

        // 销售单位处理 xsdwno
        websitSH = publicService.ptxswdToshxswd(lcls.getXsdwno(), orderBase.getMainId());
        lcls.setXsdwno(websitSH.getWdId());
        lcls.setXswdmc(websitSH.getWdmc());
        lcls.setXsdwxtbh(websitSH.getBrandSysWebsitNumber());
        lcls.setServicewdno(websitSH.getWdId());
        lcls.setShopno(websitSH.getWdId());
        lcls.setShopname(websitSH.getWdmc());

        // 所属区域处理
        BrandWebsit bb = brandWebsitService.lambdaQuery()
                .eq(BrandWebsit::getGroupCompanyId, brand.getBrandId())
                .eq(BrandWebsit::getBrandWebsitNumber, lcls.getSsqy())
                .last("limit 1")
                .one();
        lcls.setSsqy("其他");
        if (bb!=null){
            if (bb.getBelongCompany().equals("X2300000") || bb.getBelongCompany().equals("S9219807")) {
                lcls.setSsqy("佛山");
            } else if (bb.getBelongCompany().equals("X2100000") || bb.getBelongCompany().equals("S9219801")) {
                lcls.setSsqy("广州");
            }
        }
        if (StringUtils.isEmpty(lcls.getQuhao())){
            if (bb!=null){
                if (bb.getBelongCompany().equals("S9219807")){
                    lcls.setQuhao("0756");
                } else if (bb.getBelongCompany().equals("S9219801")){
                    lcls.setQuhao("020");
                }
            }
        }
        // 售后网点处理 azwdno
        websitSH = publicService.ptshwdToshshwd(lcls.getAzwdno(), orderBase.getMainId());
        lcls.setAzwdno(websitSH.getWdId());
        lcls.setAzwdmc(websitSH.getWdmc());
        lcls.setAzwdxtbh(websitSH.getBrandSysWebsitNumber());
        // 安装网点同售后
        lcls.setZjczwd(websitSH.getWdId());
        lcls.setZjczwdxtbh(websitSH.getBrandSysWebsitNumber());
        // 提取安装师傅的账号信息
        lcls.setAzrenid(publicService.getBrandWorkerId(orderBase.getWorkerId(),websitSH.getWdId(),brand.getBrandId(),orderBase.getWebsitId()));

        // 接收派工网点也可认为是售后网点处理 jspgwdno
        lcls.setJspgwdno(websitSH.getWdId());
        lcls.setJspgwdmc(websitSH.getWdmc());
        lcls.setJspgwdxtbh(websitSH.getBrandSysWebsitNumber());

        // 先从销售网点查
        websitSH = publicService.ptxswdToshxswd(orderBase.getCreateSys(), orderBase.getMainId());
        if (StringUtils.isEmpty(websitSH.getWdId())){
            websitSH = publicService.ptshwdToshshwd(orderBase.getCreateSys(), orderBase.getMainId());
        }
        lcls.setCjwdno(websitSH.getWdId());
        lcls.setCjwdxtbh(websitSH.getBrandSysWebsitNumber());
    }

    private Boolean installDtailJS(String cjOrWg, Brand brand, OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls,
                                   OrderInstallDetail detail, String taskno, String pgwcmxid, String mainName,
                                   Long synReqId) {
        // 获取完工采集明细数据 (家用空调、商用空调、其他) TL_fa_order_install_detail_js
        if (!getCJMXDataCj(cjOrWg, brand, orderBase, lcls, detail, pgwcmxid, mainName, taskno)){
            return Boolean.FALSE;
        }

        // 获取采集附件数据 (家用、商用、其他)  TL_fa_order_install_detail_append_js
        if ("cj".equals(cjOrWg)){
            if (!getcjfjdataCj(orderBase, detail, mainName, taskno)){
                return Boolean.FALSE;
            }
        }

        // 处理条码明细
        if (!getDetailCodeCj(orderBase, detail, mainName, taskno, synReqId)){
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }


    private Boolean getDetailCodeCj(OrderBase orderBase, OrderInstallDetail detail,
                                    String mainName, String taskno, Long synReqId) {
        // 获取条码采集明细id
        InstalDetailCode instalDetailCode = new InstalDetailCode();
        instalDetailCode.setOrderNo(detail.getOrderBaseId());
        instalDetailCode.setId(detail.getId());
        List<OrderInstallDetailCode> codes = codeOrderInstallDetailMapper.getIdCj(instalDetailCode);
        // 采集附近件表可能已上传条码图片，如上传这里不再上传直接赋值即可


        // 处理条码明细
        for (OrderInstallDetailCode code :codes){
            if (StringUtils.isEmpty(code.getPath())){
                publicService.updateUploadTaskListInfo(synReqId,"order_install_detail_code.path未准备上传被忽略");
                return Boolean.FALSE;
            }
            // 条码上传
            if (StringUtils.isEmpty(code.getGreeImageId())){
                // 上传图片
                if (!uploadImage(orderBase, code, synReqId)) {
                    return Boolean.FALSE;
                }
            }

            if (StringUtils.isEmpty(code.getTmguid())){
                OrderInstallDetailCode upCode = new OrderInstallDetailCode();
                upCode.setId(code.getId());
                upCode.setTmguid(UUID.randomUUID().toString());
                orderInstallDetailCodeService.updateById(upCode);
            }
            // 采集明细表中取pgwcmxid
            OrderInstallDetail orderInstallDetail = orderInstallDetailService.lambdaQuery()
                    .eq(OrderInstallDetail::getOrderBaseId,code.getOrderBaseId())
                    .eq(OrderInstallDetail::getId,code.getOrderDetailId())
                    .last("limit 1")
                    .one();
            if (orderInstallDetail == null || StringUtils.isEmpty(orderInstallDetail.getPgwcmxid())){
                continue;
            } else {
                OrderInstallDetailCode upCode = new OrderInstallDetailCode();
                upCode.setId(code.getId());
                upCode.setPgwcmxid(orderInstallDetail.getPgwcmxid());
                orderInstallDetailCodeService.updateById(upCode);
            }

            // 生成采集条码表数据
            if (!generateDetailCodeCj(orderBase, code, mainName, taskno)){
                return Boolean.FALSE;
            }
        }

        return Boolean.TRUE;
    }

    private boolean uploadImage(OrderBase orderBase, OrderInstallDetailCode code, Long synReqId) {
        String oldFile;
        String newFile;
        oldFile = IdWorker.getIdStr() + ".jpeg";
        baseService.saveToFile(ossUtil.getUrlWw(code.getPath()),oldFile);
        // 获取文件
        newFile = oldFile;
        WebsitSH websitSH = publicService.ptshwdToshshwd(orderBase.getWebsitId(),orderBase.getMainId());
        Boolean flag = uploadBarCodeexc(newFile, oldFile,websitSH.getWdId(), code, synReqId);
        new File(newFile).delete();
        if (!flag) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }


    private Boolean uploadBarCodeexc(String newFile, String oldFile,String wdno,OrderInstallDetailCode code, Long synReqId) {
        try{
            // 1: 获取签名信息
            GreeFileResponseHelper response =  greeLogic.ossGetSignt(wdno,"GET");
            if (response.getStatusCode() == 200){
                ResultData resultData = JSONObject.parseObject(response.getData().toString(), ResultData.class);
                resultData.getExtData().setDir(resultData.getExtData().getDir()+"/"+oldFile);
                // 2:文件上传
                PostObjectSample ossPostObject = new PostObjectSample();
                String result = ossPostObject.PostObject(newFile, resultData.getExtData());
                // 3: 保存文件
                if (org.apache.commons.lang3.StringUtils.isEmpty(result.trim())){
                    Map<String,String>req = new HashMap<>();
                    // 生成对象
                    getSaveFileInfo(oldFile, resultData, req);
                    GreeFileSaveHelper responses = greeLogic.greePostSaveFile(req);

                    if (responses.getStatusCode() == 200){
                        code.setGreeImageId(responses.getData().getId());
                        codeOrderInstallDetailMapper.updateById(code);
                    } else {
                        // 失败
                        log.info(responses.getMsg());
                        return Boolean.FALSE;
                    }
                } else {
                    // 上传图片到OSS失败
                    log.info("上传图片到OSS失败");
                    publicService.updateUploadTaskListInfo(synReqId,"order_install_detail_code.path上传图片到OSS失败");
                    return Boolean.FALSE;
                }
            } else {
                //  获取签名信息失败
                log.info("获取签名信息失败");
                publicService.updateUploadTaskListInfo(synReqId,"order_install_detail_code.path上传图片获取签名信息失败");
                return Boolean.FALSE;
            }
            return Boolean.TRUE;
        } catch (Exception ex) {
            return Boolean.FALSE;
        }
    }

    private Boolean generateDetailCodeCj(OrderBase orderBase, OrderInstallDetailCode code, String mainName, String taskno) {
        // 采集条码表中的pgguid 统一改成工单基础表的pgguid
        InstalDetailCode upcode = new InstalDetailCode();
        upcode.setOrderNo(code.getOrderBaseId());
        upcode.setPgguId(orderBase.getPgguid());
        codeOrderInstallDetailMapper.updateCodePgguid(upcode);

        InstalDetailCode codep = new InstalDetailCode();
        codep.setOrderNo(code.getOrderBaseId());
        codep.setId(code.getId());

        if (mainName.equals("商用空调")){
            // 商用用空调 完工明细 数据生成(完工采集条码数据)
            ItfTblAzWgmxSyktTmmxLsUpload sy = codeOrderInstallDetailMapper.getSyCodeCj(codep);
            if (sy != null){
                if (!itfTblAzWgmxSyktTmmxLsUploadService.save(sy)){
                    return Boolean.FALSE;
                }
                // 生成商用完工明细数据采集条码明细表数据
                return getItfTblazwgmxsykttmmxls(taskno, sy);
            }
        }
        return Boolean.TRUE;
    }

    private Boolean getItfTblazwgmxsykttmmxls(String taskno, ItfTblAzWgmxSyktTmmxLsUpload sy) {
        ItfTblazwgmxsykttmmxls ls = new ItfTblazwgmxsykttmmxls();
        MyBeanUtils.copyProperties(sy,ls);
        ls.setSynTaskNo(taskno);
        ls.setRelationid(sy.getPgwcmxid());
        ls.setId(sy.getTmguid());
        return itfTblazwgmxsykttmmxlsService.save(ls);
    }

    private Boolean getcjfjdataCj(OrderBase orderBase, OrderInstallDetail detail, String mainName, String taskno) {
        String oldFile ="";
        String newFile ="";
        // 安装工单采集附件表提取数据
        InstallCJDetailFile installCJDetailFile = new InstallCJDetailFile();
        installCJDetailFile.setOrderNo(detail.getOrderBaseId());
        installCJDetailFile.setId(detail.getId());
        List<OrderDetailFile> detailFiles = fileOrderDetailMapper.getIdCj(installCJDetailFile);
        for (OrderDetailFile file:detailFiles){
            // 工单号， file的id即附件表的id,uuid统一值
            // path未准备上传被忽略
            if (StringUtils.isEmpty(file.getFilePath())){
                return Boolean.TRUE;
            }
            // 品牌方fileindex未获取上传被忽略
            if (StringUtils.isEmpty(file.getBrandFilePath())){
                // 上传图片
                oldFile = IdWorker.getIdStr() + ".jpeg";
                baseService.saveToFile(ossUtil.getUrlWw(file.getFilePath()),oldFile);
                // 获取文件
                newFile = oldFile;
                WebsitSH websitSH = publicService.ptshwdToshshwd(orderBase.getWebsitId(),orderBase.getMainId());
                Boolean flag = uploadFileexc(newFile, oldFile,websitSH.getWdId(), file);
                new File(newFile).delete();
                // 生成图片失败不上传
                if (!flag) {
                    return Boolean.TRUE;
                }
            }
            // 提取获取采集明细表数据中获取pgwcmxid(生成格力总部-采集明细序号)
            OrderInstallDetail detailone = orderInstallDetailService.lambdaQuery()
                    .eq(OrderInstallDetail::getOrderBaseId,file.getOrderBaseId())
                    .eq(OrderInstallDetail::getId,file.getOrderDetailId())
                    .last("limit 1")
                    .one();

            // 获取完工明细数据(采集附件表) 家用、商用、其他
            if (!getwgmxDataCj(mainName, file, detailone, taskno)){
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }


    private Boolean uploadFileexc(String newFile, String oldFile,String wdno,OrderDetailFile file) {
        try{
            // 1: 获取签名信息
            GreeFileResponseHelper response =  greeLogic.ossGetSignt(wdno,"GET");
            if (response.getStatusCode() == 200){
                ResultData resultData = JSONObject.parseObject(response.getData().toString(), ResultData.class);
                resultData.getExtData().setDir(resultData.getExtData().getDir()+"/"+oldFile);
                // 2:文件上传
                PostObjectSample ossPostObject = new PostObjectSample();
                String result = ossPostObject.PostObject(newFile, resultData.getExtData());
                // 3: 保存文件
                if (org.apache.commons.lang3.StringUtils.isEmpty(result.trim())){
                    Map<String,String>req = new HashMap<>();
                    // 生成对象
                    getSaveFileInfo(oldFile, resultData, req);
                    GreeFileSaveHelper responses = greeLogic.greePostSaveFile(req);

                    if (responses.getStatusCode() == 200){
                        file.setBrandFilePath(responses.getData().getId());
                        orderDetailFileService.updateById(file);
                    } else {
                        // 失败
                        return Boolean.FALSE;
                    }
                } else {
                    // 上传图片到OSS失败
                    return Boolean.FALSE;
                }
            } else {
                //  获取签名信息失败
                return Boolean.FALSE;
            }
            return Boolean.TRUE;
        } catch (Exception ex) {
            return Boolean.FALSE;
        }
    }

    private void getSaveFileInfo(String id1, ResultData resultData, Map<String, String> req) {
        req.put("bizAppId","flydiy-auto-sit-default-front-greeshmerge");
        req.put("bizTable","default");
        req.put("bizType","default");
        req.put("accessType","auth");
        req.put("url", resultData.getExtData().getDir());
        req.put("name", id1 +".jpeg");
    }

    public Boolean getwgmxDataCj(String mainName, OrderDetailFile file, OrderInstallDetail detailone, String taskno) {
        // 修改附件的pgwcmxid
        if (detailone != null){
            OrderDetailFile upf = new OrderDetailFile();
            upf.setId(file.getId());
            upf.setPgwcmxid(detailone.getPgwcmxid());
            orderDetailFileService.updateById(upf);
        }
        // 修改附件的fjguid
        if (detailone != null && StringUtils.isEmpty(file.getFjguid())){
            OrderDetailFile upf = new OrderDetailFile();
            upf.setId(file.getId());
            upf.setFjguid(UUID.randomUUID().toString());
            orderDetailFileService.updateById(upf);
        }

        InstallCJDetailFile request = new InstallCJDetailFile();
        request.setOrderNo(file.getOrderBaseId());
        request.setId(file.getId());

        // 生成家用空调 数据采集附件上传记录
        if (mainName.equals("家用空调") ||
                mainName.equals("家用空气能热水器") ||
                mainName.equals("驻车空调")){
            // 家用空调 完工明细 数据生成
            ItfTblAzWgmxJyktFjUpload jyktfj = fileOrderDetailMapper.getFileUploadCJ(request);
            if (jyktfj !=null){
                if (!itfTblAzWgmxJyktFjUploadService.save(jyktfj)){
                    return Boolean.FALSE;
                }
                // 生成家用空调数据采集附件上传记录表数据 itf_tblazwgmxjyktfj
                if (!processItfTblazwgmxjyktfj(mainName, file, taskno, jyktfj)){
                    return Boolean.FALSE;
                }
            }
        } else if (mainName.equals("商用空调")){
            // 商用空调 完工明细 数据生成
            ItfTblAzWgmxSyktFjUpload syktfj = fileOrderDetailMapper.getSyFileUploadCj(request);
            if (syktfj !=null){
                if (!itfTblAzWgmxSyktFjUploadService.save(syktfj)){
                    return Boolean.FALSE;
                }
                // 生成商用空调数据采集附件表上传记录表数据 itf_tblazwgmxsyktfj
                if (!getItfTblazwgmxsyktfj(taskno, syktfj)){
                    return Boolean.FALSE;
                }
            }
        } else {
            // 其他 完工明细 数据生成
            ItfTblAzWgmxQitaFjUpload otherfj = fileOrderDetailMapper.getOtherFileUploadCj(request);
            if (otherfj != null){
                if (!itfTblAzWgmxQitaFjUploadService.save(otherfj)){
                    return Boolean.FALSE;
                }
                // 生成其他数据采集附件表上传附件表数据itf_tblazwgmxqitafj
                if (!saveItfTblazwgmxqitafj(taskno, otherfj)){
                    return Boolean.FALSE;
                }
                upItfTblazwgmxqitafj(file, taskno);
            }
        }

        return Boolean.TRUE;
    }

    private Boolean processItfTblazwgmxjyktfj(String mainName, OrderDetailFile file, String taskno, ItfTblAzWgmxJyktFjUpload jyktfj) {
        if (! getItfTblazwgmxjyktfj(taskno, jyktfj)){
            return Boolean.FALSE;
        }
        // 空气能热水器转换图片类型
        // 原内机其他4 ,改为 12,原上墙改为12，且fjindex=2 原外机其他改为13选填图片
        if (mainName.equals("家用空气能热水器")){
            // 修改
            modifyItfTblazwgmxjyktfj(file, taskno);
        }
        return Boolean.TRUE;
    }

    private Boolean getItfTblazwgmxjyktfj(String taskno, ItfTblAzWgmxJyktFjUpload jyktfj) {
        ItfTblazwgmxjyktfj fj = new ItfTblazwgmxjyktfj();
        MyBeanUtils.copyProperties(jyktfj,fj);
        fj.setSynTaskNo(taskno);
        fj.setId(jyktfj.getFjguid());
        return itfTblazwgmxjyktfjService.save(fj);
    }

    private Boolean getItfTblazwgmxsyktfj(String taskno, ItfTblAzWgmxSyktFjUpload syktfj) {
        ItfTblazwgmxsyktfj fj = new ItfTblazwgmxsyktfj();
        MyBeanUtils.copyProperties(syktfj,fj);
        fj.setSynTaskNo(taskno);
        fj.setId(syktfj.getFjguid());
        fj.setRelationid(syktfj.getPgwcmxid());
        return itfTblazwgmxsyktfjService.save(fj);
    }


    private void modifyItfTblazwgmxjyktfj(OrderDetailFile file, String taskno) {
        InstallDetail req = new InstallDetail();
        req.setOrderNo(file.getOrderBaseId());
        req.setId(taskno);
        itfTblazwgmxjyktfjCJMapper.updateType(req);
        itfTblazwgmxjyktfjCJMapper.updateType1(req);
        itfTblazwgmxjyktfjCJMapper.updateType2(req);
        itfTblazwgmxjyktfjCJMapper.updateType3(req);
        itfTblazwgmxjyktfjCJMapper.updateType4(req);
        itfTblazwgmxjyktfjCJMapper.updateType5(req);
    }


    private Boolean saveItfTblazwgmxqitafj(String taskno, ItfTblAzWgmxQitaFjUpload otherfj) {
        ItfTblazwgmxqitafj fj = new ItfTblazwgmxqitafj();
        MyBeanUtils.copyProperties(otherfj,fj);
        fj.setSynTaskNo(taskno);
        fj.setId(otherfj.getFjguid());
        return itfTblazwgmxqitafjService.save(fj);
    }

    private void upItfTblazwgmxqitafj(OrderDetailFile file, String taskno) {
        InstallDetail req = new InstallDetail();
        req.setOrderNo(file.getOrderBaseId());
        req.setId(taskno);
        itfTblazwgmxqitafjCJMapper.updateType(req);
        itfTblazwgmxqitafjCJMapper.updateType1(req);
        itfTblazwgmxqitafjCJMapper.updateType2(req);
    }

    private Boolean getCJMXDataCj(String cjOrWg, Brand brand, OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls,
                                  OrderInstallDetail item, String pgwcmxid, String mainName, String taskno) {
        if (org.apache.commons.lang3.StringUtils.isEmpty(item.getPgwcmxid())){
            item.setPgwcmxid(pgwcmxid);
            item.updateById();
        }
        // 采集表与采集条码表ID超范围 - 采集表及采集明细表中的outid统一修改
        upOutId(item);
        // 采集标志 1100:保存,1110:作废,1120:转结算
        Integer cjstat = 1100;
        if (orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G7.getCode()) ||
                orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G10.getCode())){
            if ("wg".equals(cjOrWg)){
                cjstat = 1120;
            }
        }
        // 作废
        if (item.getStatus().equals(2)){
            cjstat = 1110;
        }
        // 修改首次提交时间
        item.setFirstTime(new Date());
        item.updateById();

        InstallDetail request = new InstallDetail();
        request.setOrderNo(item.getOrderBaseId());
        request.setId(item.getId());
        request.setBrandId(brand.getBrandId());
        // 为空时为 家用空调
        if (org.apache.commons.lang3.StringUtils.isEmpty(mainName) ||
                mainName.equals("家用空调") || mainName.equals("家用空气能热水器") || mainName.equals("驻车空调")){
            // 家用空调 完工明细 数据生成(完工采集数据)
            ItfTblAzWgmxJyktUpload mxup = detailOrderInstallMapper.getOrderInstallUploadCj(request);
            if (mxup != null){
                // 部分参数单独处理
                setParm(orderBase, lcls, cjstat, mxup, brand);
                if (!itfTblAzWgmxJyktUploadService.save(mxup)){
                    return Boolean.FALSE;
                }
                // 生成完工明细(家用空调)上传记录表数据
                return getItfTblazwgmxjykt(taskno, mxup);
            }
        } else if (mainName.equals("商用空调")){
            // 商用空调 完工明细数据生成(完工采集数据)
            ItfTblAzWgmxSyktUpload mxup = detailOrderInstallMapper.getSyOrderInstallUploadCj(request);
            if (mxup != null){
                // 部分参数单独处理
                setSyParm(orderBase, lcls, cjstat, mxup, brand);
                if (!itfTblAzWgmxSyktUploadService.save(mxup)){
                    return Boolean.FALSE;
                }
                // 生成完工明细(商用空调)上传记录表数据
                return getItfTblazwgmxsykt(taskno, mxup);
            }
        } else {
            // 其他 完工明细数据生成
            // 获取家用空调完工明细(完工采集数据)
            ItfTblAzWgmxQitaUpload mxup = detailOrderInstallMapper.getOtherOrderInstallUploadCj(request);
            if (mxup != null){
                // 部分参数单独处理
                setOtherParm(orderBase, lcls, cjstat, mxup, item, brand);
                if (!itfTblAzWgmxQitaUploadService.save(mxup)){
                    return Boolean.FALSE;
                }
                // 完工明细(其他)上传记录表 itf_tblazwgmxqita
                return getItfTblazwgmxqita(taskno, mxup);
            }
        }
        return Boolean.TRUE;
    }

    private Boolean getItfTblazwgmxjykt(String taskno, ItfTblAzWgmxJyktUpload mxup) {
        ItfTblazwgmxjykt kt = new ItfTblazwgmxjykt();
        MyBeanUtils.copyProperties(mxup,kt);
        kt.setSynTaskNo(taskno);
        kt.setId(mxup.getPgwcmxid());
        return itfTblazwgmxjyktService.save(kt);
    }


    private Boolean getItfTblazwgmxsykt(String taskno, ItfTblAzWgmxSyktUpload mxup) {
        ItfTblazwgmxsykt kt = new ItfTblazwgmxsykt();
        MyBeanUtils.copyProperties(mxup,kt);
        kt.setSynTaskNo(taskno);
        kt.setId(mxup.getPgwcmxid());
        kt.setKjmm2("");
        kt.setKjfs(0);
        return itfTblazwgmxsyktService.save(kt);
    }

    private Boolean getItfTblazwgmxqita(String taskno, ItfTblAzWgmxQitaUpload mxup) {
        ItfTblazwgmxqita ta = new ItfTblazwgmxqita();
        MyBeanUtils.copyProperties(mxup,ta);
        ta.setSynTaskNo(taskno);
        ta.setId(mxup.getPgwcmxid());
        ta.setKjfs(0);
        return itfTblazwgmxqitaService.save(ta);
    }

    private void upOutId(OrderInstallDetail detail) {
        List<OrderInstallDetail> upList = new ArrayList<>();
        List<OrderInstallDetail> detaillist = orderInstallDetailService.lambdaQuery()
                .eq(OrderInstallDetail::getOrderBaseId, detail.getOrderBaseId())
                .eq(OrderInstallDetail::getOutId,0).list();
        // 找出最大的序 默认值为0
        int outId=0;
        if (CollectionUtils.isNotEmpty(detaillist)){
            for (OrderInstallDetail item:detaillist){
                if (outId<item.getOutId()){
                    outId = item.getOutId();
                }
            }
            for (OrderInstallDetail ite:detaillist){
                if (ite.getOutId() == 0){
                    outId++;
                    ite.setOutId(outId);
                    upList.add(ite);
                }
            }
            // 修改序
            if (upList.size()>0){
                orderInstallDetailService.updateBatchById(upList);
                upList.clear();
            }
            detaillist.clear();
        }

        // 采集条码码 outid统一修改
        // 采集表与采集条码表ID超范围 - 采集表及采集明细表中的outid统一修改
        List<OrderInstallDetailCode> codelist = orderInstallDetailCodeService.lambdaQuery()
                .eq(OrderInstallDetailCode::getOrderBaseId, detail.getOrderBaseId())
                .eq(OrderInstallDetailCode::getOutId,0).list();
        if (CollectionUtils.isNotEmpty(codelist)){
            InstalDetailCode codep = new InstalDetailCode();
            codep.setOrderNo(detail.getOrderBaseId());
            codeOrderInstallDetailMapper.updateCodeOutId(codep);
        }
    }

    private void setParm(OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls, Integer cjstat,
                         ItfTblAzWgmxJyktUpload mxup, Brand brand) {
        WebsitSH websitSH = new WebsitSH();

        mxup.setCjren(orderBase.getCreateBy());
        mxup.setCjrmc(orderBase.getCreateName());

        // 售后网点处理
        websitSH = publicService.ptshwdToshshwd(mxup.getCzwd(), orderBase.getMainId());
        mxup.setCzwd(websitSH.getWdId());
        mxup.setCjwdno(websitSH.getWdId());
        mxup.setZjczwd(websitSH.getWdId());
        mxup.setZjczwdxtbh(websitSH.getBrandSysWebsitNumber());

        // 提取安装师傅的账号信息
        mxup.setAzrenid(publicService.getBrandWorkerId(orderBase.getWorkerId(),websitSH.getWdId(),brand.getBrandId(),orderBase.getWebsitId()));

        mxup.setStat(cjstat);
        if (cjstat.equals("1120")){
            if (orderBase.getOverTime()!=null){
                mxup.setWcsj(orderBase.getOverTime());
            } else {
                mxup.setWcsj(mxup.getLastModifiedDate());
            }
        }
        mxup.setAzwdno(lcls.getAzwdno());
        mxup.setAzwdmc(lcls.getAzwdmc());
        mxup.setAzwdxtbh(lcls.getAzwdxtbh());
        if (StringUtils.isEmpty(mxup.getSjcjgps())){
            if (orderBase.getAddress().length()>140){
                mxup.setSjcjgps(orderBase.getAddress().substring(0,140));
            } else {
                mxup.setSjcjgps(orderBase.getAddress());
            }
        }
        mxup.setGmsj(orderBase.getBuyTime());

        if (StringUtils.isNotEmpty(orderBase.getLat()) && orderBase.getLat().length()>12){
            mxup.setSjcjjd(orderBase.getLat().substring(0,12));
        }
        if (StringUtils.isNotEmpty(orderBase.getLng()) && orderBase.getLng().length()>12){
            mxup.setSjcjwd(orderBase.getLng().substring(0,12));
        }
        mxup.setSjcjjwdu(mxup.getSjcjjd()+","+ mxup.getSjcjwd());

        // 网点维修工ID单独处理

        ItfTblWangdianWxgDownload wxg = itfTblWangdianWxgDownloadService.lambdaQuery()
                .eq(ItfTblWangdianWxgDownload::getSfzen,mxup.getAzrenid())
                .eq(ItfTblWangdianWxgDownload::getWdno,mxup.getCzwd())
                .last("limit 1")
                .one();

        if (wxg !=null){
            mxup.setAzrenid(wxg.getWxgid());
        }

    }

    private void setOtherParm(OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls, Integer cjstat,
                              ItfTblAzWgmxQitaUpload mxup, OrderInstallDetail detail, Brand brand) {
        WebsitSH websitSH = new WebsitSH();

        setOtherWdno(orderBase, mxup);
        mxup.setCjren(orderBase.getCreateBy());
        mxup.setCjrmc(orderBase.getCreateName());

        // 售后网点处理
        websitSH = publicService.ptshwdToshshwd(mxup.getCzwd(), orderBase.getMainId());
        mxup.setCzwd(websitSH.getWdId());

        // 提取安装师傅的账号信息
        mxup.setAzrenid(publicService.getBrandWorkerId(orderBase.getWorkerId(),websitSH.getWdId(),brand.getBrandId(),orderBase.getWebsitId()));

        mxup.setStat(cjstat);
        if (cjstat.equals("1120")){
            if (orderBase.getOverTime()!=null){
                mxup.setWcsj(orderBase.getOverTime());
            } else {
                mxup.setWcsj(mxup.getLastModifiedDate());
            }
        }
        mxup.setAzwdno(lcls.getAzwdno());
        mxup.setAzwdmc(lcls.getAzwdmc());
        mxup.setAzwdxtbh(lcls.getAzwdxtbh());
        if (StringUtils.isEmpty(mxup.getSjcjgps())){
            if (orderBase.getAddress().length()>140){
                mxup.setSjcjgps(orderBase.getAddress().substring(0,140));
            } else {
                mxup.setSjcjgps(orderBase.getAddress());
            }
        }
        mxup.setGmsj(orderBase.getBuyTime());
        mxup.setCjwdno(mxup.getAzwdno());
        mxup.setZjczwd(mxup.getAzwdno());
        if (StringUtils.isNotEmpty(orderBase.getLat()) && orderBase.getLat().length()>12){
            mxup.setSjcjjd(orderBase.getLat().substring(0,12));
        }
        if (StringUtils.isNotEmpty(orderBase.getLng()) && orderBase.getLng().length()>12){
            mxup.setSjcjwd(orderBase.getLng().substring(0,12));
        }
        mxup.setSjcjjwdu(mxup.getSjcjjd()+","+ mxup.getSjcjwd());

        // 网点维修工ID单独处理
        // 先找出网点编号对应的品牌商网点编号
        // 先从售后网点查
        websitSH = publicService.ptshwdToshshwd(detail.getWebsitId(), orderBase.getMainId());
        if (StringUtils.isEmpty(websitSH.getWdId())){
            websitSH = publicService.ptxswdToshxswd(detail.getWebsitId(), orderBase.getMainId());
        }

        ItfTblWangdianWxgDownload wxg = itfTblWangdianWxgDownloadService.lambdaQuery()
                .eq(ItfTblWangdianWxgDownload::getSfzen,mxup.getAzrenid())
                .eq(ItfTblWangdianWxgDownload::getWdno,websitSH.getWdId())
                .last("limit 1")
                .one();
        if (wxg !=null){
            mxup.setAzrenid(wxg.getWxgid());
        }

    }

    private void setSyParm(OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls, Integer cjstat,
                           ItfTblAzWgmxSyktUpload mxup, Brand brand) {
        WebsitSH websitSH = new WebsitSH();

        setSyWdno(orderBase, mxup);
        mxup.setCjren(orderBase.getCreateBy());
        mxup.setCjrmc(orderBase.getCreateName());

        // 售后网点处理
        websitSH = publicService.ptshwdToshshwd(mxup.getCzwd(), orderBase.getMainId());
        mxup.setCzwd(websitSH.getWdId());

        // 提取安装师傅的账号信息
        mxup.setAzrenid(publicService.getBrandWorkerId(orderBase.getWorkerId(),websitSH.getWdId(),brand.getBrandId(),orderBase.getWebsitId()));

        mxup.setStat(cjstat);
        if (cjstat.equals("1120")){
            if (orderBase.getOverTime()!=null){
                mxup.setWcsj(orderBase.getOverTime());
            } else {
                mxup.setWcsj(mxup.getLastModifiedDate());
            }
        }
        mxup.setAzwdno(lcls.getAzwdno());
        mxup.setAzwdmc(lcls.getAzwdmc());
        mxup.setAzwdxtbh(lcls.getAzwdxtbh());
        if (StringUtils.isEmpty(mxup.getSjcjgps())){
            if (orderBase.getAddress().length()>140){
                mxup.setSjcjgps(orderBase.getAddress().substring(0,140));
            } else {
                mxup.setSjcjgps(orderBase.getAddress());
            }
        }
        mxup.setGmsj(orderBase.getBuyTime());
        mxup.setCjwdno(mxup.getAzwdno());
        mxup.setZjczwd(mxup.getAzwdno());
        if (StringUtils.isNotEmpty(orderBase.getLat()) && orderBase.getLat().length()>12){
            mxup.setSjcjjd(orderBase.getLat().substring(0,12));
        }
        if (StringUtils.isNotEmpty(orderBase.getLng()) && orderBase.getLng().length()>12){
            mxup.setSjcjwd(orderBase.getLng().substring(0,12));
        }
        mxup.setSjcjjwdu(mxup.getSjcjjd()+","+ mxup.getSjcjwd());

        // 网点维修工ID单独处理
        // 先找出网点编号对应的品牌商网点编号
        // 先从售后网点查
        websitSH = publicService.ptshwdToshshwd(mxup.getCzwd(), orderBase.getMainId());
        if (StringUtils.isEmpty(websitSH.getWdId())){
            websitSH = publicService.ptxswdToshxswd(lcls.getXsdwno(), orderBase.getMainId());
        }

        ItfTblWangdianWxgDownload wxg = itfTblWangdianWxgDownloadService.lambdaQuery()
                .eq(ItfTblWangdianWxgDownload::getSfzen,mxup.getAzrenid())
                .eq(ItfTblWangdianWxgDownload::getWdno,websitSH.getWdId())
                .last("limit 1")
                .one();
        if (wxg !=null){
            mxup.setAzrenid(wxg.getWxgid());
        }
    }


    private void setSyWdno(OrderBase orderBase, ItfTblAzWgmxSyktUpload mxup) {
        WebsitSH websitSH = new WebsitSH();

        // 售后网点处理
        websitSH = publicService.ptshwdToshshwd(mxup.getZjczwdxtbh(), orderBase.getMainId());
        if (StringUtils.isEmpty(websitSH.getWdId())){
            websitSH = publicService.ptxswdToshxswd(mxup.getZjczwdxtbh(), orderBase.getMainId());
        }
        mxup.setZjczwdxtbh(websitSH.getBrandSysWebsitNumber());
    }


    private void setOtherWdno(OrderBase orderBase, ItfTblAzWgmxQitaUpload mxup) {
        // 先从售后网点查
        WebsitSH websitSH = new WebsitSH();

        // 售后网点处理
        websitSH = publicService.ptshwdToshshwd(mxup.getZjczwdxtbh(), orderBase.getMainId());
        if (StringUtils.isEmpty(websitSH.getWdId())){
            websitSH = publicService.ptxswdToshxswd(mxup.getZjczwdxtbh(), orderBase.getMainId());
        }

        mxup.setZjczwdxtbh(websitSH.getBrandSysWebsitNumber());
    }



}
