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.GDAppointStatusEnum;
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.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
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;
    @Resource
    OutJiasmToGreeUploadTaskListService outJiasmToGreeUploadTaskListService;
    @Resource
    OrderInstallService orderInstallService;
    @Resource
    ItfTblAzAssignFeeUploadService itfTblAzAssignFeeUploadService;
    @Resource
    OrderPayOutlayItemService orderPayOutlayItemService;
    @Resource
    ItfMessagehuifangentityService itfMessagehuifangentityService;
    @Resource
    ItfTblAzWgmxQitaTmmxUploadService itfTblAzWgmxQitaTmmxUploadService;
    @Resource
    ItfTblAzWgmxJyktTmmxUploadService itfTblAzWgmxJyktTmmxUploadService;



    @Override
    public boolean orderBaseInstall(Brand brand, @NotNull OutJiasmToGreeUploadTaskList ojtutl, Map<String, List<SysDictRef>> CategoryRefmap){
        OrderBase orderBase = orderBaseService.lambdaQuery()
                .eq(OrderBase::getId, ojtutl.getId())
                .last("limit 1")
                .one();

        if (orderBase !=null && orderBase.getUploadFlag()){
            // 若工单被删除不再同步 不写生成数据
            // 条件判断
            if (!checkPare(ojtutl, orderBase)) {
                return Boolean.FALSE;
            }

            // 初始化数据
            if (!upInit(ojtutl, CategoryRefmap, orderBase)){
                return  Boolean.FALSE;
            }
        }

        return orderBaseInstallProc(brand, ojtutl, CategoryRefmap, orderBase);
    }

    public Boolean upInit(@NotNull OutJiasmToGreeUploadTaskList ojtutl, Map<String, List<SysDictRef>> CategoryRefmap, OrderBase orderBase) {
        List<OrderInstallDetail> details = new ArrayList<>();
        // 修改采集明细表与采集条码表ID OrderInstallDetail.pgwcmxid自生成
        upOutId(ojtutl.getId());

        // 采集明细表中的pgguid 改成工单基础表的pgguid
        upInstallDetailPgguid(orderBase);

        // 修改采集明细的序 pgwcmxid(自生成) 生成格力总部-采集明细序号
        upPgwcmxid(ojtutl);

        // 采集条码表中的pgguid 统一改成工单基础表的pgguid,tmguid自生成
        upOrderInstallDetailCodePgguid(ojtutl, orderBase);

        // 特殊安装确认书图从采集明细表中移到附件表中
        addTSHJP(ojtutl, orderBase);
        // 上传附件表图片信息
        if (uploadOrderDetailFile(ojtutl, CategoryRefmap, orderBase)){
            publicService.updateUploadTaskLisError(ojtutl,"上传附件表图片信息出错");
            return Boolean.FALSE;
        }
        // 管理端采集的条码图并没全部给到采集条码表中，这里需要转移到采集条码表中
        details = orderInstallDetailService.lambdaQuery()
                .eq(OrderInstallDetail::getOrderBaseId, orderBase.getId())
                .eq(OrderInstallDetail::getStatus, 1).list();

        // 其他类型的单独处理
        // 家用空调，家用空气能热水器，驻车空调,商用空调
        if (!(orderBase.getMainName().equals("家用空调") ||
                orderBase.getMainName().equals("家用空气能热水器") ||
                orderBase.getMainName().equals("驻车空调") ||
                orderBase.getMainName().equals("商用空调"))){
            for (OrderInstallDetail detail:details){
                upCodepOther(detail);
            }
        } else {
            for (OrderInstallDetail detail:details){
                upCodep(detail);
            }
        }

        // 图片数据同步检测
        return checkOrderDetailFile(ojtutl);
    }

    public Boolean checkOrderDetailFile(@NotNull OutJiasmToGreeUploadTaskList ojtutl) {
        List<OrderDetailFile> detailFiles = new ArrayList<>();
        Boolean flag = Boolean.TRUE;
        detailFiles = orderDetailFileService.lambdaQuery()
                .eq(OrderDetailFile::getOrderBaseId, ojtutl.getId()).list();
        for (OrderDetailFile file:detailFiles){
            // 工单号， file的id即附件表的id,uuid统一值
            // path未准备上传被忽略
            if (StringUtils.isEmpty(file.getFilePath())){
                flag = Boolean.FALSE;
                break;
            }
            // 品牌方fileindex未获取上传被忽略
            if (StringUtils.isEmpty(file.getBrandFilePath())){
                flag = Boolean.FALSE;
                break;
            }
        }
        detailFiles.clear();
        return flag;
    }

    public void addTSHJP(@NotNull OutJiasmToGreeUploadTaskList ojtutl, OrderBase orderBase) {
        OrderInstallDetail iods = orderInstallDetailService.lambdaQuery()
                .eq(OrderInstallDetail::getOrderBaseId, orderBase.getId())
                .eq(OrderInstallDetail::getIsSpecial, "YES")
                .isNotNull(OrderInstallDetail::getSpecialSignUrl)
                .eq(OrderInstallDetail::getStatus, 1)
                .last("limit 1").one();
        OrderDetailFile detailFiles = orderDetailFileService.lambdaQuery()
                .eq(OrderDetailFile::getOrderBaseId, ojtutl.getId())
                .eq(OrderDetailFile::getFileType, 44).last("limit 1").one();
        if (iods != null){
            OrderDetailFile addFile = new OrderDetailFile();
            // 存在且相同时不处理
            if (detailFiles != null && detailFiles.getFilePath().equals(iods.getSpecialSignUrl())){
                return;
            }
            // 存在且不相同时重修改
            if (detailFiles != null && !detailFiles.getFilePath().equals(iods.getSpecialSignUrl())){

                detailFiles.setFilePath(iods.getSpecialSignUrl());
                detailFiles.setBrandFilePath("");

                detailFiles.setUpdateTime(new Date());
                if (iods.getUpdateTime() !=null){
                    detailFiles.setUpdateTime(iods.getUpdateTime());
                }
                detailFiles.setUpdateBy("system");
                if (StringUtils.isNotEmpty(iods.getUpdateBy())){
                    detailFiles.setUpdateBy(iods.getUpdateBy());
                }
                MyBeanUtils.copyProperties(detailFiles, addFile);
            }
            // 不存在生成新的
            if (detailFiles == null){
                addFile.setId(IdWorker.getIdStr());
                addFile.setFjguid(publicService.getUUID());
                addFile.setOrderBaseId(ojtutl.getId());
                addFile.setOrderDetailId(iods.getId());
                addFile.setFileName("特殊环境安装确认书");
                addFile.setFilePath(iods.getSpecialSignUrl());
                addFile.setFileRemark("特殊环境安装确认书");
                addFile.setFileType(44);
                addFile.setCreateTime(new Date());
                if (iods.getCreateTime() !=null){
                    addFile.setCreateTime(iods.getCreateTime());
                }
                addFile.setCreateBy("system");
                if (StringUtils.isNotEmpty(iods.getCreateBy())){
                    addFile.setCreateBy(iods.getCreateBy());
                }
                addFile.setUpdateTime(new Date());
                if (iods.getUpdateTime() !=null){
                    addFile.setUpdateTime(iods.getUpdateTime());
                }
                addFile.setUpdateBy("system");
                if (StringUtils.isNotEmpty(iods.getUpdateBy())){
                    addFile.setUpdateBy(iods.getUpdateBy());
                }
            }

            orderDetailFileService.saveOrUpdate(addFile);
        }
    }

    @Override
    public Boolean orderBaseInstallCjOrWg(String cjOrWg, Brand brand, OutJiasmToGreeUploadTaskList ojtutl,
                                          Map<String, List<SysDictRef>> CategoryRefmap) throws Exception {
        OrderBase orderBase = orderBaseService.lambdaQuery()
                .eq(OrderBase::getId, ojtutl.getId())
                .last("limit 1")
                .one();

        if (orderBase !=null  && orderBase.getUploadFlag()){
            // 安装工单产品信息,操作日志,完工明细,完工明细附件
            // 预约改约-总部短信发送请求表,派服务人员-短信发送请求表 以主表的pgguid统一修改
            upPgguid(orderBase);
            // 初始化数据
            if (!upInit(ojtutl, CategoryRefmap, orderBase)){
                return Boolean.FALSE;
            }
        }
        return orderInstallCjOrWg(cjOrWg, brand, ojtutl, CategoryRefmap,orderBase);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean orderBaseInstallProc(Brand brand, OutJiasmToGreeUploadTaskList ojtutl,
                                        Map<String, List<SysDictRef>> CategoryRefmap, OrderBase orderBase) {
        if (orderBase !=null ){
            // 安装工单产品信息,操作日志,完工明细,完工明细附件 以主表的pgguid统一修改
            // 生成待同步的工单主表数据
            ItfTblAzAssignLcLsUpload lcls = getItfTblAzAssignLcLsUpload(brand, ojtutl, orderBase);
            // 1： 插入工单主表数据
            if (!itfTblAzAssignLcLsUploadService.save(lcls)){
                return Boolean.FALSE;
            }

            // 2 ：生成安装辅材费用数据
            if (!getFeeUpload(orderBase)){
                return Boolean.FALSE;
            }

            //3 ：生成采集数据 采集明细，采集附件，采集条码(及家用，商用、其他)
            if (!generateCJData(brand, CategoryRefmap, orderBase, lcls)){
                return Boolean.FALSE;
            }

            // 生成评价短信 只写一次
            if (!getPJmessage(orderBase, lcls, brand)){
                return Boolean.FALSE;
            }
        }
        // 修改数据上传任务表中的状态   这里固定值 22
        publicService.updateUploadTaskListStatus(ojtutl.getOrderNo(),ojtutl.getSynReqId(),1,22);

        return Boolean.TRUE;
    }


    public Boolean getPJmessage(OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls,Brand brand) {
        WebsitSH websitSH = new WebsitSH();

        // 生成评价短信 不能写在产品中要写在表头，只写一次 派工状态为4(1304，1309与工单状态相同) 完工或已关闭的单
        if ((orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G7.getCode()) ||
                orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G10.getCode())) &&
                (orderBase.getDispatchStatus().equals(GDAppointStatusEnum.INSTALL_STATUS_A4_0.getCode()))) {
            // 查看是否已发送 工单号，只能插入一次
            ItfMessagehuifangentity message = itfMessagehuifangentityService.lambdaQuery()
                    .eq(ItfMessagehuifangentity::getOrderBaseId, orderBase.getId())
                    .last("limit 1")
                    .one();

            // 自建工单 这里可放在定时任务中执行，代码是不会走这里的
            if (!orderBase.getUploadFlag()){
                if (message == null){
                    // 生成短信数据
                    OrderPar orderPar = new OrderPar();
                    orderPar.setBrandId(brand.getBrandId());
                    orderPar.setOrderNo(orderBase.getId());
                    ItfMessagehuifangentity addms = gdOrderBaseMapper.getAZItfMessagehuifangentity(orderPar);
                    if (addms==null){
                        return Boolean.TRUE;
                    }
                    // 售后网点处理 fjwd
                    websitSH = publicService.ptshwdToshshwd(addms.getFjwd(), orderBase.getMainId());
                    addms.setFjwd(websitSH.getWdId());

                    String id = publicService.getUUID();
                    if (id.length()>32){
                        addms.setSynTaskNo(id.substring(0,32));
                    } else {
                        addms.setSynTaskNo(id);
                    }
                    addms.setFsma(publicService.getfsma(addms.getSjid()));
                    addms.setFsnr("尊敬的用户：请对本次服务评价：满意回复1；一般回复2；不满意回复3。并注明原因！关注官微“格力售后服务”，为您提供更优质服务！");
                    addms.setDxguid(publicService.getUUID());
                    return publicService.addItfMessagehuifangentity(addms,orderBase.getOrderType(),6);
                }
            } else if ((orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G7.getCode()) ||
                    orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G10.getCode())) &&
                    !orderBase.getSaleType().equals("工程机"))  {
                if (message == null){
                    // 生成短信数据 sendtype (1=原总部接口发送，2=用联通的接口进行发送) 默认为1
                    OrderPar orderPar = new OrderPar();
                    orderPar.setBrandId(brand.getBrandId());
                    orderPar.setOrderNo(orderBase.getId());
                    ItfMessagehuifangentity addms = gdOrderBaseMapper.getAZItfMessagehuifangentityT(orderPar);
                    if (addms==null){
                        return Boolean.TRUE;
                    }
                    String id = publicService.getUUID();
                    if (id.length()>32){
                        addms.setSynTaskNo(id.substring(0,32));
                    } else {
                        addms.setSynTaskNo(id);
                    }
                    addms.setFsnr("尊敬的用户：请对本次服务评价：满意回复1；一般回复2；不满意回复3。并注明原因！关注官微“格力售后服务”，为您提供更优质服务！");
                    addms.setFsma(publicService.getfsma(addms.getSjid()));
                    return publicService.addItfMessagehuifangentity(addms,orderBase.getOrderType(),6);
                }
            }
        }
        return Boolean.TRUE;
    }


    public Boolean generateCJData(Brand brand, Map<String, List<SysDictRef>> CategoryRefmap, OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls) {
        // 工单状态order_status 6, 7, 9, 10 且为非预派工订单
        if ((orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G6.getCode()) ||
                orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G7.getCode()) ||
                orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G9.getCode()) ||
                orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G10.getCode())) &&
                orderBase.getIsReservation().equals(0)){
            // 提取采集的数据
            List<OrderInstallDetail> orderInstallDetails = orderInstallDetailService.lambdaQuery()
                    .eq(OrderInstallDetail::getOrderBaseId, orderBase.getId())
                    // .eq(OrderInstallDetail::getStatus, 1)
                    .orderByAsc(OrderInstallDetail::getId)
                    .list();
            Map<String, Integer> val = new HashMap<String,Integer>();
            // 根据产品类别名称 生成不同的完工明细数据
            String mainName = CategoryRefmap.get(orderBase.getMainId()).get(0).getRefDictName();

            if (orderInstallDetails !=null && orderInstallDetails.size()>0){
                for (OrderInstallDetail detail :orderInstallDetails){
                    // 状态:3=临时保存;2=作废,1=已采集,0=未采集
                    // 临时保存与未采集的记录不处理
                    if (detail.getStatus().equals(0) || detail.getStatus().equals(3)){
                        break;
                    }

                    // 获取完工采集明细数据 (家用空调、商用空调、其他) 内外机条码在这里生成
                    getCJMXData(brand, orderBase, lcls, detail,mainName);

                    // 获取采集附件数据 (家用、商用、其他)  显示的图片信息在这里生成
                    getcjfjdata(orderBase, detail, mainName);

                    // 生成条码明细数据 这里好像并没用可以去掉
                    getDetailCode(orderBase, detail, mainName);
                }
            }
        }

        return Boolean.TRUE;
    }

    public void getDetailCode(OrderBase orderBase, OrderInstallDetail detail, String mainName) {
        String oldFile ="";
        String newFile ="";

        // 获取条码采集明细id
        InstalDetailCode instalDetailCode = new InstalDetailCode();
        instalDetailCode.setOrderNo(detail.getOrderBaseId());
        instalDetailCode.setId(detail.getId());
        List<OrderInstallDetailCode> codes = codeOrderInstallDetailMapper.getId(instalDetailCode);
        // 处理条码明细
        for (OrderInstallDetailCode code :codes){
            if (StringUtils.isEmpty(code.getPath())){
                continue;
            }
            if (StringUtils.isEmpty(code.getGreeImageId())){
                // 上传图片
                /*if (!uploadImage(orderBase, code)) {
                    continue;
                }*/
                continue;
            }

            /*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);
            }*/

            // 生成采集条码表数据
            generateDetailCode(orderBase, code, mainName);
        }
    }

    public void generateDetailCode(OrderBase orderBase, OrderInstallDetailCode code, String mainName) {
        // 采集条码表中的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("家用空调") ||
                mainName.equals("家用空气能热水器") ||
                mainName.equals("驻车空调")) {
            // 家用空调 完工明细 数据生成(完工采集条码数据)
            ItfTblAzWgmxJyktTmmxUpload jy = codeOrderInstallDetailMapper.getJyCode(codep);
            if (jy != null){
                itfTblAzWgmxJyktTmmxUploadService.save(jy);
            }
        } else if (mainName.equals("商用空调")){
            // 家用空调 完工明细 数据生成(完工采集条码数据)
            ItfTblAzWgmxSyktTmmxLsUpload sy = codeOrderInstallDetailMapper.getSyCode(codep);
            if (sy != null){
                itfTblAzWgmxSyktTmmxLsUploadService.save(sy);
            }
        } else {
            ItfTblAzWgmxQitaTmmxUpload other = codeOrderInstallDetailMapper.getOtherCode(codep);
            if (other != null){
                itfTblAzWgmxQitaTmmxUploadService.save(other);
            }
        }
    }

    public void getCJMXData(Brand brand, OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls,
                             OrderInstallDetail detail,String mainName) {

        /*// 修改采集明细的序 pgwcmxid 生成格力总部-采集明细序号
        if (StringUtils.isEmpty(detail.getPgwcmxid())){
            upCerrentRec(detail);
        }
        // 采集表中的pgguid 统一改成工单基础表的pgguid
        upInstallDetailPgguid(orderBase);

        // 采集表与采集条码表ID超范围 - 采集表及采集明细表中的outid统一修改
        upOutId(detail);*/


        // 采集标志 1100:保存,1110:作废,1120:转结算
        Integer cjstat = 1100;

        if (orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G7.getCode()) ||
                orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G10.getCode())){
            cjstat = 1120;
        }
        if (detail.getStatus().equals(2)){
            cjstat = 1110;
        }

        InstallDetail request = new InstallDetail();
        request.setOrderNo(detail.getOrderBaseId());
        request.setId(detail.getId());
        request.setBrandId(brand.getBrandId());
        // 为空时为 家用空调
        if (StringUtils.isEmpty(mainName) ||
                mainName.equals("家用空调") || mainName.equals("家用空气能热水器") || mainName.equals("驻车空调")){
            // 家用空调 完工明细 数据生成(完工采集数据)
            ItfTblAzWgmxJyktUpload mxup = detailOrderInstallMapper.getOrderInstallUpload(request);
            if (mxup != null){
                // 部分参数单独处理
                setParma(brand, orderBase, lcls, cjstat, mxup, detail);
                itfTblAzWgmxJyktUploadService.save(mxup);
            }
        } else if (mainName.equals("商用空调")){
            // 商用空调 完工明细数据生成(完工采集数据)
            ItfTblAzWgmxSyktUpload mxup = detailOrderInstallMapper.getSyOrderInstallUpload(request);
            if (mxup != null){
                // 部分参数单独处理
                setSyParm(orderBase, lcls, cjstat, mxup, detail, brand);
                itfTblAzWgmxSyktUploadService.save(mxup);
            }
        } else {
            // 其他 完工明细数据生成
            // 获取家用空调完工明细(完工采集数据)
            ItfTblAzWgmxQitaUpload mxup = detailOrderInstallMapper.getOtherOrderInstallUpload(request);
            if (mxup != null){
                // 部分参数单独处理
                setOtherParm(orderBase, lcls, cjstat, mxup, detail, brand);
                itfTblAzWgmxQitaUploadService.save(mxup);
            }
        }
    }


    // 处理附件采集数据上传
    public Boolean getcjfjdata(OrderBase orderBase, OrderInstallDetail detail, String mainName) {
        String oldFile ="";
        String newFile ="";

        // 附件上传总部标识判断
        if (orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G7.getCode()) ||
                orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G10.getCode())){
            // 安装工单采集附件表提取数据
            InstallCJDetailFile installCJDetailFile = new InstallCJDetailFile();
            installCJDetailFile.setOrderNo(detail.getOrderBaseId());
            installCJDetailFile.setId(detail.getId());
            List<OrderDetailFile> detailFiles = fileOrderDetailMapper.getId(installCJDetailFile);
            for (OrderDetailFile file:detailFiles){
                // 工单号， file的id即附件表的id,uuid统一值
                // path未准备上传被忽略
                if (StringUtils.isEmpty(file.getFilePath())){
                    continue;
                }
                /*// 品牌方fileindex未获取上传被忽略
                if (StringUtils.isEmpty(file.getBrandFilePath())){
                    // 上传图片
                    oldFile = IdWorker.getIdStr() + ".jpeg";
                    baseService.saveToFile(ossUtil.getUrlWw(file.getFilePath()),oldFile);
                    // 获取文件
                    newFile = oldFile;
                    WebsitFWS websitFWS = publicService.shshwdToptshwd(orderBase.getWebsitId(),orderBase);
                    Boolean flag = uploadFileexc(newFile, oldFile,websitFWS.getWebsitId(), file);
                    new File(newFile).delete();
                    if (!flag) {
                        continue;
                    }
                }*/
                // 提取获取采集明细表数据中获取pgwcmxid(生成格力总部-采集明细序号)
                OrderInstallDetail detailone = orderInstallDetailService.lambdaQuery()
                        .eq(OrderInstallDetail::getOrderBaseId,file.getOrderBaseId())
                        .eq(OrderInstallDetail::getId,file.getOrderDetailId())
                        .last("limit 1")
                        .one();

                // 获取完工明细数据(采集附件表) 家用、商用、其他
                getwgmxData(mainName, file, detailone);
            }
        }

        return Boolean.TRUE;
    }

    public void getwgmxData(String mainName, OrderDetailFile file, OrderInstallDetail detailone) {
        // 修改附件的pgwcmxid
        /*if (detailone != null){
            OrderDetailFile upf = new OrderDetailFile();
            upf.setId(file.getId());
            upf.setPgwcmxid(detailone.getPgwcmxid());
            orderDetailFileService.updateById(upf);
        }
        // 采集附件表中的pgguid 统一改成工单基础表的pgguid 可以不用，从主表取即可 无pgguid字段
        InstallCJDetailFile upfile = new InstallCJDetailFile();
        upfile.setOrderNo(file.getOrderBaseId());
        upfile.setPgguId(orderBase.getPgguid());
        fileOrderDetailMapper.updateFilePgguid(upfile);


        // 修改附件的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.getFileUpload(request);
            if (jyktfj !=null){
                // 修改类型对应关系
                jyktfj.setType(setfjType(jyktfj.getType()));
                itfTblAzWgmxJyktFjUploadService.save(jyktfj);
            }
        } else if (mainName.equals("商用空调")){
            // 商用空调 完工明细 数据生成
            ItfTblAzWgmxSyktFjUpload syktfj = fileOrderDetailMapper.getSyFileUpload(request);
            if (syktfj !=null){
                // 修改类型对应关系
                syktfj.setType(setfjType(syktfj.getType()));
                itfTblAzWgmxSyktFjUploadService.save(syktfj);
            }
        } else {
            // 其他 完工明细 数据生成
            ItfTblAzWgmxQitaFjUpload otherfj = fileOrderDetailMapper.getOtherFileUpload(request);
            if (otherfj != null){
                // 修改类型对应关系
                otherfj.setType(setfjType(otherfj.getType()));
                itfTblAzWgmxQitaFjUploadService.save(otherfj);
            }
        }
    }

    public Integer setfjType(Integer type) {
        Integer result = type;
        switch (type){
            case 1:
                result = 0;
                break;
            case 2:
                result = 7;
                break;
            case 3:
                result = 1;
                break;
            case 6:
                result = 4;
                break;
            case 7:
                result = 5;
                break;
            case 9:
                result = 5;
                break;
            case 11:
                result = 5;
                break;
            case 12:
                result = 5;
                break;
            case 13:
                result = 4;
                break;
            case 15:
                result = 4;
                break;
            case 16:
                result = 5;
                break;
            case 39:
                result = 4;
                break;
            case 40:
                result = 5;
                break;
            case 43:
                result = 5;
                break;
            case 44:
                result = 8;
                break;
            case 33:
                result = 12;
                break;
            case 34:
                result = 12;
                break;
            case 35:
                result = 12;
                break;
            default:
                result = 4;
                break;
        }
        return result;
    }

    public Boolean getFeeUpload(OrderBase orderBase) {
        // 根据辅材订单支付表存在支付时生成安装费用数据
        List<OrderPayOutlayItem> pitems = orderPayOutlayItemService.lambdaQuery()
                .eq(OrderPayOutlayItem::getOrderBaseId, orderBase.getId()).list();
        if (pitems!=null && pitems.size()>0){
            BigDecimal totalAmount=BigDecimal.ZERO;
            for (OrderPayOutlayItem ps :pitems){
                totalAmount = totalAmount.add(ps.getTotalAmount());
            }

            ItfTblAzAssignFeeUpload feeUpload = new ItfTblAzAssignFeeUpload();
            // 从工单扩展表提取辅材信息
            OrderInstall orderInstall =orderInstallService.lambdaQuery()
                    .eq(OrderInstall::getOrderBaseId, orderBase.getId())
                    .last("limit 1")
                    .one();
            if (orderInstall !=null){
                feeUpload.setWorkerOrderNo(orderBase.getId());
                feeUpload.setId(orderBase.getPgguid());
                feeUpload.setCreatedBy("guangfo");
                feeUpload.setCreatedDate(orderInstall.getCreateTime());
                feeUpload.setLastModifiedBy("guangfo");
                feeUpload.setLastModifiedDate(orderInstall.getUpdateTime());
                feeUpload.setOtherfee(String.valueOf(totalAmount));
                feeUpload.setTotalfee(String.valueOf(totalAmount));
                feeUpload.setAjia(orderInstall.getBracket());
                feeUpload.setJcguan(orderInstall.getPipe());
                feeUpload.setKqkg(orderInstall.getSwithFlag());
                feeUpload.setGkzy(orderInstall.getHighAltitude());
                feeUpload.setYccxqk(orderInstall.getHole());
                feeUpload.setFlbz(orderBase.getId());
            } else {
                feeUpload.setWorkerOrderNo(orderBase.getId());
                feeUpload.setId(orderBase.getPgguid());
                feeUpload.setCreatedBy("guangfo");
                feeUpload.setCreatedDate(orderBase.getCreateTime());
                feeUpload.setLastModifiedBy("guangfo");
                feeUpload.setLastModifiedDate(orderBase.getUpdateTime());
                feeUpload.setOtherfee(String.valueOf(totalAmount));
                feeUpload.setTotalfee(String.valueOf(totalAmount));
                feeUpload.setFlbz(orderBase.getId());
            }
            return itfTblAzAssignFeeUploadService.save(feeUpload);
        }

        return Boolean.TRUE;
    }

    public Boolean uploadOrderDetailFile(@NotNull OutJiasmToGreeUploadTaskList ojtutl, Map<String, List<SysDictRef>> CategoryRefmap, OrderBase orderBase) {
        try{
            // 修改序 outid
            upOrderDetailFileOutId(ojtutl);
            // 上传图片
            uploadOrderDetailFilePiture(ojtutl, CategoryRefmap, orderBase);
            return Boolean.FALSE;
        } catch (Exception ex) {
            return Boolean.TRUE;
        }
    }

    public void uploadOrderDetailFilePiture(@NotNull OutJiasmToGreeUploadTaskList ojtutl, Map<String, List<SysDictRef>> CategoryRefmap, OrderBase orderBase) throws IOException {
        String oldFile ="";
        String newFile ="";
        // 根据产品类别名称 生成不同的完工明细数据
        String mainName = CategoryRefmap.get(orderBase.getMainId()).get(0).getRefDictName();
        InstallCJDetailFile installCJDetailFile = new InstallCJDetailFile();
        List<OrderDetailFile> detailFiles = new ArrayList<>();
        installCJDetailFile.setOrderNo(ojtutl.getId());

        detailFiles = orderDetailFileService.lambdaQuery()
                .eq(OrderDetailFile::getOrderBaseId, ojtutl.getId()).list();
        for (OrderDetailFile file:detailFiles){
            // 工单号， file的id即附件表的id,uuid统一值
            // path未准备上传被忽略
            if (StringUtils.isEmpty(file.getFilePath())){
                continue;
            }
            if (StringUtils.isNotEmpty(file.getBrandFilePath())){
                continue;
            }
            // 品牌方fileindex未获取上传被忽略
            if (StringUtils.isEmpty(file.getBrandFilePath())){
                // 上传图片
                oldFile = publicService.uploadPath(file.getFilePath());
                File nFile = publicService.createf(oldFile);

                baseService.saveToFile(ossUtil.getUrlWw(file.getFilePath()),nFile);
                // 获取文件
                newFile = oldFile;
                WebsitSH websitSH = publicService.ptshwdToshshwd(orderBase.getWebsitId(), orderBase.getMainId());
                log.info("图片上传网点编号:{}",websitSH.getWdId());
                Boolean flag = uploadFileexc(newFile, oldFile,websitSH.getWdId(), file);
                new File(newFile).delete();
                if (!flag) {
                    continue;
                }
            }
            // 提取获取采集明细表数据中获取pgwcmxid(生成格力总部-采集明细序号)
            OrderInstallDetail detailone = orderInstallDetailService.lambdaQuery()
                    .eq(OrderInstallDetail::getOrderBaseId,file.getOrderBaseId())
                    .eq(OrderInstallDetail::getId,file.getOrderDetailId())
                    .last("limit 1")
                    .one();

            // 修改附件的pgwcmxid,fjguid
            if (detailone != null){
                OrderDetailFile upf = new OrderDetailFile();
                upf.setId(file.getId());
                upf.setPgwcmxid(detailone.getPgwcmxid());
                // 修改附件的fjguid
                if (StringUtils.isEmpty(file.getFjguid())){
                    upf.setFjguid(publicService.getUUID());
                }
                orderDetailFileService.updateById(upf);
            }

            // 采集附件表中的pgguid 统一改成工单基础表的pgguid 可以不用，从主表取即可 无pgguid字段
            /*InstallCJDetailFile upfile = new InstallCJDetailFile();
            upfile.setOrderNo(file.getOrderBaseId());
            upfile.setPgguId(orderBase.getPgguid());
            fileOrderDetailMapper.updateFilePgguid(upfile);*/
        }
    }

    public void upOrderDetailFileOutId(@NotNull OutJiasmToGreeUploadTaskList ojtutl) {
        List<OrderDetailFile> upList = new ArrayList<>();
        // 查看是否有需要修改的数据
        List<OrderDetailFile> oids = orderDetailFileService.lambdaQuery()
                .eq(OrderDetailFile::getOrderBaseId, ojtutl.getId())
                .eq(OrderDetailFile::getOutId, 0)
                .list();
        if (CollectionUtils.isEmpty(oids)){
            return;
        }

        List<OrderDetailFile> detaillist = orderDetailFileService.lambdaQuery()
                .eq(OrderDetailFile::getOrderBaseId, ojtutl.getId())
                .list();
        // 找出最大的序 默认值为0
        int outId=0;
        if (CollectionUtils.isNotEmpty(detaillist)){
            for (OrderDetailFile item:detaillist){
                if (outId<item.getOutId()){
                    outId = item.getOutId();
                }
            }
        }

        for (OrderDetailFile ite:oids){
            if (ite.getOutId() == 0){
                outId++;
                ite.setOutId(outId);
                upList.add(ite);
            }
        }
        // 修改序
        if (upList.size()>0){
            orderDetailFileService.updateBatchById(upList);
            upList.clear();
        }
        detaillist.clear();
    }

    public void upOrderInstallDetailCodePgguid(@NotNull OutJiasmToGreeUploadTaskList ojtutl, OrderBase orderBase) {
        List<OrderInstallDetailCode> oidcsUp = new ArrayList<>();
        List<OrderInstallDetailCode> oidcs = orderInstallDetailCodeService.lambdaQuery()
                .eq(OrderInstallDetailCode::getOrderBaseId, ojtutl.getId())
                .isNull(OrderInstallDetailCode::getPgguid)
                .list();
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(oidcs)){
            for (OrderInstallDetailCode code:oidcs){
                if (StringUtils.isEmpty(code.getPgguid()) || StringUtils.isEmpty(code.getTmguid())){
                    code.setPgguid(orderBase.getPgguid());
                    code.setTmguid(publicService.getUUID());
                    oidcsUp.add(code);
                }
            }

            if (oidcsUp.size()>0){
                orderInstallDetailCodeService.updateBatchById(oidcsUp);
                oidcsUp.clear();
            }
        }
    }

    public void upPgwcmxid(OutJiasmToGreeUploadTaskList ojtutl) {
        List<OrderInstallDetail> upList = new ArrayList<>();
        List<OrderInstallDetail> detaillist = orderInstallDetailService.lambdaQuery()
                .eq(OrderInstallDetail::getOrderBaseId, ojtutl.getId())
                .isNull(OrderInstallDetail::getPgwcmxid)
                .list();
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(detaillist)){
            for (OrderInstallDetail item:detaillist){
                if (StringUtils.isEmpty(item.getPgwcmxid())){
                    item.setPgwcmxid(IdWorker.getIdStr());
                    upList.add(item);
                }
            }
            // 修改序
            if (upList.size()>0){
                orderInstallDetailService.updateBatchById(upList);
                upList.clear();
            }
        }
    }

    public void stopUpload(@NotNull OutJiasmToGreeUploadTaskList ojtutl, String msg) {
        OutJiasmToGreeUploadTaskList up = new OutJiasmToGreeUploadTaskList();
        up.setSynReqId(ojtutl.getSynReqId());
        up.setSynStatus(99);
        up.setSynTime(new Date());
        up.setSynErrMsg(msg);
        outJiasmToGreeUploadTaskListService.updateById(up);
    }

    public void upInstallDetailPgguid(OrderBase orderBase) {
        List<OrderInstallDetail> detaillist = orderInstallDetailService.lambdaQuery()
                .eq(OrderInstallDetail::getOrderBaseId, orderBase.getId())
                .isNull(OrderInstallDetail::getPgguid)
                .list();
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(detaillist)){
            InstallDetail installDetail = new InstallDetail();
            installDetail.setOrderNo(orderBase.getId());
            installDetail.setPgguId(orderBase.getPgguid());
            detailOrderInstallMapper.installUpdatePgguid(installDetail);
        }
    }

    public boolean checkPare(OutJiasmToGreeUploadTaskList ojtutl, OrderBase orderBase) {
        // 接收派工网点为空上传忽略
        if (StringUtils.isEmpty(orderBase.getReceWebsitId()) || orderBase.getReceWebsitId().equals("0")){
            stopUpload(ojtutl, "接收派工网点为空上传忽略");
            return Boolean.FALSE;
        }
        if (StringUtils.isEmpty(orderBase.getPgguid()) || orderBase.getPgguid().equals("0")){
            stopUpload(ojtutl, "pgguid异常上传忽略");
            return Boolean.FALSE;
        }
        if (StringUtils.isEmpty(orderBase.getPgid()) || orderBase.getPgid().equals("0")){
            stopUpload(ojtutl, "pgid异常上传忽略");
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean orderInstallCjOrWg(String cjOrWg, Brand brand, OutJiasmToGreeUploadTaskList ojtutl,
                                      Map<String, List<SysDictRef>> CategoryRefmap, OrderBase orderBase) throws Exception {
        try{
            // TL_fa_order_install_js
            if (orderBase !=null  && orderBase.getUploadFlag()){
                // 生成待同步的安装工单主表
                ItfTblAzAssignLcLsUpload lcls = getItfTblAzAssignLcLsUpload(brand, orderBase);
                // 1： 插入工单主表数据
                if (!itfTblAzAssignLcLsUploadService.save(lcls)){
                    return Boolean.FALSE;
                }

                // 同步采集明细数据
                List<OrderInstallDetail> details = orderInstallDetailService.lambdaQuery()
                        .eq(OrderInstallDetail::getOrderBaseId, orderBase.getId())
                        .list();
                // TL_fa_order_install_detail_js
                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 mainName = CategoryRefmap.get(orderBase.getMainId()).get(0).getRefDictName();

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

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

            return Boolean.TRUE;
        } catch (Exception ex) {
            ex.printStackTrace();
            // 抛异常目的是把前期的数据操作回滚
            throw new Exception(ex);
        }

    }


    /**
     * 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(itemStatus);
            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;
    }


    public 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<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
    public ItfTblAzAssignLcLsUpload getItfTblAzAssignLcLsUpload(@NotNull Brand brand, OutJiasmToGreeUploadTaskList ojtutl,
                                                                 OrderBase orderBase) {
        WebsitSH websitSH = new WebsitSH();
        OrderPar orderPar = new OrderPar();
        orderPar.setBrandId(brand.getBrandId());
        orderPar.setOrderNo(ojtutl.getId());
        ItfTblAzAssignLcLsUpload lcls = gdOrderBaseMapper.getAzOrderBase(orderPar);
        lcls.setUploadTaskSynReqId(ojtutl.getSynReqId());

        // 创建人及创建人名称改为对应的创建网点
        websitSH = publicService.ptxswdToshxswd(lcls.getCjren(), orderBase.getMainId());
        if (StringUtils.isEmpty(websitSH.getWdId())){
            websitSH = publicService.ptshwdToshshwd(lcls.getCjren(), orderBase.getMainId());
        }
        lcls.setCjren(websitSH.getWdId());
        lcls.setCjrmc(websitSH.getWdmc());

        // 销售单位处理 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());


        // 售后网点处理
        websitSH = publicService.ptshwdToshshwd(lcls.getAzwdno(), orderBase.getMainId());
        lcls.setAzwdno(websitSH.getWdId());
        lcls.setAzwdmc(websitSH.getWdmc());
        lcls.setAzwdxtbh(websitSH.getBrandSysWebsitNumber());
        lcls.setCjwdno(websitSH.getWdId());
        lcls.setCjwdxtbh(websitSH.getBrandSysWebsitNumber());
        lcls.setZjczwd(websitSH.getWdId());
        lcls.setZjczwdxtbh(websitSH.getBrandSysWebsitNumber());
        // 提取安装师傅的账号信息
        lcls.setAzrenid(publicService.getBrandWorkerId(orderBase.getWorkerId(),websitSH.getWdId(),brand.getBrandId(),orderBase.getWebsitId()));

        // 接收派工网点也可认为是售后网点处理
        lcls.setJspgwdno(websitSH.getWdId());
        lcls.setJspgwdmc(websitSH.getWdmc());
        lcls.setJspgwdxtbh(websitSH.getBrandSysWebsitNumber());
        // 所属区域处理
        BrandWebsit bb = brandWebsitService.lambdaQuery()
                .eq(BrandWebsit::getGroupCompanyId,brand.getBrandId())
                .eq(BrandWebsit::getBrandWebsitNumber,lcls.getJspgwdno())
                .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");
                }
            }
        }
        // 省市等信息单独处理
        orderService.tpPositionTosh(lcls);
        return lcls;
    }

    @NotNull
    public 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;
    }

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

        // 创建人及创建人名称改为对应的创建网点
        websitSH = publicService.ptxswdToshxswd(lcls.getCjren(), orderBase.getMainId());
        if (StringUtils.isEmpty(websitSH.getWdId())){
            websitSH = publicService.ptshwdToshshwd(lcls.getCjren(), orderBase.getMainId());
        }
        lcls.setCjren(websitSH.getWdId());
        lcls.setCjrmc(websitSH.getWdmc());

        // 销售单位处理 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());
    }

    public Boolean installDtailJS(String cjOrWg, Brand brand, OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls,
                                   OrderInstallDetail detail, String taskno, String mainName,
                                   Long synReqId) throws Exception {


        // 获取完工采集明细数据 (家用空调、商用空调、其他) TL_fa_order_install_detail_js
        if (!getCJMXDataCj(cjOrWg, brand, orderBase, lcls, detail, mainName, taskno,synReqId)){
            return Boolean.FALSE;
        }
        // 获取采集附件数据 (家用、商用、其他)  TL_fa_order_install_detail_append_js
        if ("cj".equals(cjOrWg)){
            if (!getcjfjdataCj(detail, mainName, taskno)){
                return Boolean.FALSE;
            }
        }

        // 管理端采集的条码图并没全部给到采集条码表中，这里需要转移到采集条码表中
        // upCodep(detail);

        // 处理条码明细 只有商用空调才处理
        if (!getDetailCodeCj(orderBase, detail, mainName, taskno, synReqId)){
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Override
    public void upCodep(OrderInstallDetail detail) {
        List<OrderInstallDetailCode> ucodes = new ArrayList<>();
        List<OrderInstallDetailCode> codes = orderInstallDetailCodeService.lambdaQuery()
                .eq(OrderInstallDetailCode::getOrderBaseId, detail.getOrderBaseId())
                .eq(OrderInstallDetailCode::getStatus,1).list();

        List<OrderDetailFile> detailFiles = orderDetailFileService.lambdaQuery()
                .eq(OrderDetailFile::getOrderBaseId, detail.getOrderBaseId())
                .in(OrderDetailFile::getFileType,1,3).list();
        if (CollectionUtils.isNotEmpty(detailFiles) && CollectionUtils.isNotEmpty(codes)){
            for (OrderInstallDetailCode code:codes){
                for (OrderDetailFile f:detailFiles){
                    // 条码类型相同，采集图与上传图同步
                    if (code.getType().equals(String.valueOf(f.getFileType())) &&
                            code.getSortNum().equals(f.getSortNum()) &&
                            (!code.getPath().equals(f.getFilePath()) || !code.getGreeImageId().equals(f.getBrandFilePath()))){
                        code.setPath(f.getFilePath());
                        code.setGreeImageId(f.getBrandFilePath());
                        ucodes.add(code);
                    }
                    // 条码类型相同，采集图与上传图同步 管理端外机条码为3
                    if (code.getType().equals(String.valueOf(f.getFileType()-1)) &&
                            code.getSortNum().equals(f.getSortNum()) &&
                            (!code.getPath().equals(f.getFilePath()) || !code.getGreeImageId().equals(f.getBrandFilePath()))){
                        code.setPath(f.getFilePath());
                        code.setGreeImageId(f.getBrandFilePath());
                        ucodes.add(code);
                    }
                }
            }
        }
        if (ucodes.size()>0){
            orderInstallDetailCodeService.updateBatchById(ucodes);
            ucodes.clear();
        }
    }


    @Override
    public void upCodepOther(OrderInstallDetail detail) {
        List<OrderInstallDetailCode> ucodes = new ArrayList<>();
        // 类型值 2 ,41
        List<OrderInstallDetailCode> codes = orderInstallDetailCodeService.lambdaQuery()
                .eq(OrderInstallDetailCode::getOrderBaseId, detail.getOrderBaseId())
                .eq(OrderInstallDetailCode::getType,2)
                .eq(OrderInstallDetailCode::getStatus,1).list();

        OrderDetailFile detailFile = orderDetailFileService.lambdaQuery()
                .eq(OrderDetailFile::getOrderBaseId, detail.getOrderBaseId())
                .eq(OrderDetailFile::getFileType,41).last("limit 1").one();
        if (detailFile != null && CollectionUtils.isNotEmpty(codes)){
            for (OrderInstallDetailCode code:codes){
                // 条码类型相同，采集图与上传图同步
                if (!code.getPath().equals(detailFile.getFilePath()) || !code.getGreeImageId().equals(detailFile.getBrandFilePath())){
                    code.setPath(detailFile.getFilePath());
                    code.setGreeImageId(detailFile.getBrandFilePath());
                    ucodes.add(code);
                }
            }
        }

        if (ucodes.size()>0){
            orderInstallDetailCodeService.updateBatchById(ucodes);
            ucodes.clear();
        }
    }


    public Boolean getDetailCodeCj(OrderBase orderBase, OrderInstallDetail detail,
                                    String mainName, String taskno, Long synReqId) throws Exception {
        // 获取条码采集明细id
        InstalDetailCode instalDetailCode = new InstalDetailCode();
        instalDetailCode.setOrderNo(detail.getOrderBaseId());
        instalDetailCode.setId(detail.getId());
        List<OrderInstallDetailCode> codes = codeOrderInstallDetailMapper.getIdCj(instalDetailCode);
        // 采集附近件表可能已上传条码图片，如上传这里不再上传直接赋值即可
        // 处理条码明细
        for (OrderInstallDetailCode code :codes){
            // 作废的不处理
            if (code.getStatus() == 0 ){
                continue;
            }
            if (StringUtils.isEmpty(code.getPath())){
                // publicService.updateUploadTaskListInfo(synReqId,"order_install_detail_code.path未准备上传被忽略");
                throw new Exception("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;
    }

    public boolean uploadImage(OrderBase orderBase, OrderInstallDetailCode code, Long synReqId) throws IOException {
        String oldFile;
        String newFile;
        oldFile = publicService.path(code.getPath());
        File nFile = publicService.createf(oldFile);

        baseService.saveToFile(ossUtil.getUrlWw(code.getPath()),nFile);
        // 获取文件
        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;
    }


    public 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;
        }
    }

    public 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;
    }

    public 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);
    }

    public Boolean getcjfjdataCj(OrderInstallDetail detail, String mainName, String taskno) {
        // 安装工单采集附件表提取数据
        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())){
                return Boolean.TRUE;
            }
            if (StringUtils.isEmpty(file.getFjguid())){
                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;
    }


    public 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());
                        file.setSynErrMsg("");
                        orderDetailFileService.updateById(file);
                    } else {
                        // 失败
                        file.setSynErrMsg("保存图片信息失败");
                        orderDetailFileService.updateById(file);
                        return Boolean.FALSE;
                    }
                } else {
                    // 上传图片到OSS失败
                    file.setSynErrMsg("上传图片到OSS失败");
                    orderDetailFileService.updateById(file);
                    return Boolean.FALSE;
                }
            } else {
                //  获取签名信息失败
                file.setSynErrMsg("获取签名信息失败");
                orderDetailFileService.updateById(file);
                return Boolean.FALSE;
            }
            return Boolean.TRUE;
        } catch (Exception ex) {
            file.setSynErrMsg("操作图片信息失败");
            orderDetailFileService.updateById(file);
            return Boolean.FALSE;
        }
    }

    public 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");
        req.put("name", id1);
    }

    public Boolean getwgmxDataCj(String mainName, OrderDetailFile file, OrderInstallDetail detailone, String taskno) {
        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){
                // 修改类型对应关系
                jyktfj.setType(setfjType(jyktfj.getType()));
                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){
                // 修改类型对应关系
                syktfj.setType(setfjType(syktfj.getType()));
                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){
                // 修改类型对应关系
                otherfj.setType(setfjType(otherfj.getType()));
                if (!itfTblAzWgmxQitaFjUploadService.save(otherfj)){
                    return Boolean.FALSE;
                }
                // 生成其他数据采集附件表上传附件表数据itf_tblazwgmxqitafj
                if (!saveItfTblazwgmxqitafj(taskno, otherfj)){
                    return Boolean.FALSE;
                }
                upItfTblazwgmxqitafj(file, taskno);
            }
        }

        return Boolean.TRUE;
    }

    public 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;
    }

    public 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);
    }

    public 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);
    }


    public 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);
    }


    public 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);
    }

    public 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);
    }

    public Boolean getCJMXDataCj(String cjOrWg, Brand brand, OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls,
                                  OrderInstallDetail item, String mainName, String taskno,Long synReqId) {
        /*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;
    }

    public 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);
    }


    public 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);
    }

    public 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);
    }

    @Override
    public void upOutId(String orderBaseId) {

        upOrderInstallDetailOutId(orderBaseId);

        upOrderInstallDetailCodeOutId(orderBaseId);

    }

    public void upOrderInstallDetailCodeOutId(String orderBaseId) {
        // 采集条码码 outid统一修改 自生成
        /*List<OrderInstallDetailCode> codelist = orderInstallDetailCodeService.lambdaQuery()
                .eq(OrderInstallDetailCode::getOrderBaseId, orderBaseId)
                .eq(OrderInstallDetailCode::getOutId,0).list();
        if (CollectionUtils.isNotEmpty(codelist)){
            InstalDetailCode codep = new InstalDetailCode();
            codep.setOrderNo(orderBaseId);
            codeOrderInstallDetailMapper.updateCodeOutId(codep);
        }*/

        List<OrderInstallDetailCode> upList = new ArrayList<>();
        // 查看是否有需要修改的数据
        List<OrderInstallDetailCode> oids = orderInstallDetailCodeService.lambdaQuery()
                .eq(OrderInstallDetailCode::getOrderBaseId, orderBaseId)
                .eq(OrderInstallDetailCode::getOutId, 0)
                .list();
        if (CollectionUtils.isEmpty(oids)){
            return;
        }

        List<OrderInstallDetailCode> detaillist = orderInstallDetailCodeService.lambdaQuery()
                .eq(OrderInstallDetailCode::getOrderBaseId, orderBaseId)
                .ne(OrderInstallDetailCode::getOutId, 0)
                .list();
        // 找出最大的序 默认值为0
        int outId=0;
        if (CollectionUtils.isNotEmpty(detaillist)){
            for (OrderInstallDetailCode item:detaillist){
                if (outId<item.getOutId()){
                    outId = item.getOutId();
                }
            }
            for (OrderInstallDetailCode ite:oids){
                outId++;
                OrderInstallDetailCode n = new OrderInstallDetailCode();
                MyBeanUtils.copyProperties(ite,n);
                n.setOutId(outId);
                upList.add(n);
            }
            // 修改序
            if (upList.size()>0){
                orderInstallDetailCodeService.updateBatchById(upList);
                upList.clear();
            }
            detaillist.clear();
        }
    }

    public void upOrderInstallDetailOutId(String orderBaseId) {
        List<OrderInstallDetail> upList = new ArrayList<>();
        // 查看是否有需要修改的数据
        List<OrderInstallDetail> oids = orderInstallDetailService.lambdaQuery()
                .eq(OrderInstallDetail::getOrderBaseId, orderBaseId)
                .eq(OrderInstallDetail::getOutId, 0)
                .list();
        if (CollectionUtils.isEmpty(oids)){
            return;
        }

        List<OrderInstallDetail> detaillist = orderInstallDetailService.lambdaQuery()
                .eq(OrderInstallDetail::getOrderBaseId, orderBaseId)
                .ne(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:oids){
                outId++;
                OrderInstallDetail n = new OrderInstallDetail();
                MyBeanUtils.copyProperties(ite,n);
                n.setOutId(outId);
                // 修改首次提交时间
                n.setFirstTime(new Date());
                n.setPgwcmxid(publicService.getUUID());
                upList.add(n);
            }
            // 修改序
            if (upList.size()>0){
                orderInstallDetailService.updateBatchById(upList);
                upList.clear();
            }
            detaillist.clear();
        }
    }

    public void setJysWdno(OrderBase orderBase, ItfTblAzWgmxJyktUpload mxup) {
        WebsitSH websitSH = new WebsitSH();

        // 先从售后网点查
        websitSH = publicService.ptshwdToshshwd(mxup.getZjczwdxtbh(), orderBase.getMainId());
        if (StringUtils.isEmpty(websitSH.getWdId())){
            websitSH = publicService.ptxswdToshxswd(mxup.getZjczwdxtbh(), orderBase.getMainId());
        } else {
            mxup.setJsdwxtbh(websitSH.getBrandSysWebsitNumber());
        }
        mxup.setZjczwdxtbh(websitSH.getBrandSysWebsitNumber());
    }

    public void setParma(Brand brand, OrderBase orderBase, ItfTblAzAssignLcLsUpload lcls, Integer cjstat,
                         ItfTblAzWgmxJyktUpload mxup, OrderInstallDetail detail) {
        WebsitSH websitSH = new WebsitSH();

        setnwjtm(orderBase,mxup);

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

        if (StringUtils.isNotEmpty(mxup.getSjcjjd()) && mxup.getSjcjjd().length()>12){
            mxup.setSjcjjd(mxup.getSjcjjd().substring(0,12));
        }
        if (StringUtils.isNotEmpty(mxup.getSjcjwd()) && mxup.getSjcjwd().length()>12){
            mxup.setSjcjwd(mxup.getSjcjwd().substring(0,12));
        }
        if (StringUtils.isNotEmpty(mxup.getSjcjjwdu()) && mxup.getSjcjjwdu().length()>50){
            mxup.setSjcjjwdu(mxup.getSjcjjwdu().substring(0,50));
        }

        // 售后网点处理
        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());

    }

    public void setnwjtm(OrderBase orderBase,ItfTblAzWgmxJyktUpload mxup) {
        List<OrderInstallDetailCode> codes = orderInstallDetailCodeService.lambdaQuery()
                .eq(OrderInstallDetailCode::getOrderDetailId,orderBase.getId())
                .in(OrderInstallDetailCode::getType,"1","2").list();
        if (CollectionUtils.isNotEmpty(codes)){
            for (OrderInstallDetailCode item:codes){
                if (item.getType().equals("1") && StringUtils.isNotEmpty(item.getCode())){
                    mxup.setNjtm(item.getCode());
                }
                if (item.getType().equals("2") && StringUtils.isNotEmpty(item.getCode())){
                    mxup.setWjtm(item.getCode());
                }
            }
        }
    }

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

        setnwjtm(orderBase,mxup);

        mxup.setCjren(orderBase.getCreateBy());
        mxup.setCjrmc(orderBase.getCreateName());
        if (StringUtils.isNotEmpty(mxup.getSjcjjd()) && mxup.getSjcjjd().length()>12){
            mxup.setSjcjjd(mxup.getSjcjjd().substring(0,12));
        }
        if (StringUtils.isNotEmpty(mxup.getSjcjwd()) && mxup.getSjcjwd().length()>12){
            mxup.setSjcjwd(mxup.getSjcjwd().substring(0,12));
        }
        if (StringUtils.isNotEmpty(mxup.getSjcjjwdu()) && mxup.getSjcjjwdu().length()>50){
            mxup.setSjcjjwdu(mxup.getSjcjjwdu().substring(0,50));
        }

        // 售后网点处理
        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()));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        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());
    }

    public 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());

        if (StringUtils.isNotEmpty(mxup.getSjcjjd()) && mxup.getSjcjjd().length()>12){
            mxup.setSjcjjd(mxup.getSjcjjd().substring(0,12));
        }
        if (StringUtils.isNotEmpty(mxup.getSjcjwd()) && mxup.getSjcjwd().length()>12){
            mxup.setSjcjwd(mxup.getSjcjwd().substring(0,12));
        }
        if (StringUtils.isNotEmpty(mxup.getSjcjjwdu()) && mxup.getSjcjjwdu().length()>50){
            mxup.setSjcjjwdu(mxup.getSjcjjwdu().substring(0,50));
        }

        // 提取安装师傅的账号信息
        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());

    }

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

        setSyWdno(orderBase, mxup);

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

        if (StringUtils.isNotEmpty(mxup.getSjcjjd()) && mxup.getSjcjjd().length()>12){
            mxup.setSjcjjd(mxup.getSjcjjd().substring(0,12));
        }
        if (StringUtils.isNotEmpty(mxup.getSjcjwd()) && mxup.getSjcjwd().length()>12){
            mxup.setSjcjwd(mxup.getSjcjwd().substring(0,12));
        }
        if (StringUtils.isNotEmpty(mxup.getSjcjjwdu()) && mxup.getSjcjjwdu().length()>50){
            mxup.setSjcjjwdu(mxup.getSjcjjwdu().substring(0,50));
        }

        // 售后网点处理
        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());

        // 网点维修工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());
        }

    }

    public 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());

        if (StringUtils.isNotEmpty(mxup.getSjcjjd()) && mxup.getSjcjjd().length()>12){
            mxup.setSjcjjd(mxup.getSjcjjd().substring(0,12));
        }
        if (StringUtils.isNotEmpty(mxup.getSjcjwd()) && mxup.getSjcjwd().length()>12){
            mxup.setSjcjwd(mxup.getSjcjwd().substring(0,12));
        }
        if (StringUtils.isNotEmpty(mxup.getSjcjjwdu()) && mxup.getSjcjjwdu().length()>50){
            mxup.setSjcjjwdu(mxup.getSjcjjwdu().substring(0,50));
        }

        // 售后网点处理
        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());

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


    public 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());
    }


    public 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());
    }



}
