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


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zfire.jiasm.syncdata.base.BaseService;
import com.zfire.jiasm.syncdata.constant.DictTypeEnum;
import com.zfire.jiasm.syncdata.constant.GDAppointStatusEnum;
import com.zfire.jiasm.syncdata.constant.GDOrderStatusEnum;
import com.zfire.jiasm.syncdata.constant.OrderTypeEnum;
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.WebsitFWS;
import com.zfire.jiasm.syncdata.response.WebsitPP;
import com.zfire.jiasm.syncdata.response.WebsitSH;
import com.zfire.jiasm.syncdata.service.InstallProduceDataUploadService;
import com.zfire.jiasm.syncdata.service.OrderService;
import com.zfire.jiasm.syncdata.service.PublicService;
import com.zfire.jiasm.syncdata.utils.MyBeanUtils;
import com.zfire.jiasm.syncdata.utils.RemoteServiceException;
import com.zfire.jiasm.syncdata.utils.oss.OSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class InstallProduceDataUploadServiceImpl implements InstallProduceDataUploadService {
    @Value("${azuploadyscc}")
    private String azuploadyscc = "";




    @Resource
    OutJiasmToGreeUploadTaskTypeService outJiasmToGreeUploadTaskTypeService;
    @Resource
    OutJiasmToGreeUploadTaskListService outJiasmToGreeUploadTaskListService;
    @Resource
    OrderBaseService orderBaseService;
    @Resource
    GDOrderBaseMapper gdOrderBaseMapper;
    @Resource
    BaseService baseService;
    @Resource
    ItfTblAzAssignLcLsUploadService itfTblAzAssignLcLsUploadService;
    @Resource
    OrderInstallDetailService orderInstallDetailService;
    @Resource
    DetailOrderInstallMapper detailOrderInstallMapper;
    @Resource
    SysDictRefService sysDictRefService;
    @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
    ItfTblAzWgmxJyktTmmxUploadService itfTblAzWgmxJyktTmmxUploadService;
    @Resource
    ItfTblAzWgmxSyktTmmxLsUploadService itfTblAzWgmxSyktTmmxLsUploadService;
    @Resource
    ItfTblAzWgmxQitaTmmxUploadService itfTblAzWgmxQitaTmmxUploadService;
    @Resource
    ItfMessagehuifangentityService itfMessagehuifangentityService;
    @Resource
    OrderPayOutlayItemService orderPayOutlayItemService;
    @Resource
    OrderInstallService orderInstallService;
    @Resource
    ItfTblAzAssignFeeUploadService itfTblAzAssignFeeUploadService;
    @Resource
    OperatingLogOrderInstallMapper operatingLogOrderInstallMapper;
    @Resource
    ItfTblAzAssignFkmxUploadService itfTblAzAssignFkmxUploadService;
    @Resource
    ReAppointmentOrderMapper reAppointmentOrderMapper;
    @Resource
    ItfTblAzAssignAppointmentUploadService itfTblAzAssignAppointmentUploadService;
    @Resource
    PublicService publicService;
    @Resource
    OrderProductService orderProductService;
    @Resource
    GetOrderProductMapper getOrderProductMapper;
    @Resource
    ItfTblAzAssignMxUploadService itfTblAzAssignMxUploadService;
    @Resource
    OSSUtil ossUtil;
    @Resource
    GreeLogic greeLogic;
    @Resource
    ItfTblazwgmxjyktfjService itfTblazwgmxjyktfjService;
    @Resource
    ItfTblazwgmxjyktfjCJMapper itfTblazwgmxjyktfjCJMapper;
    @Resource
    ItfTblazwgmxsyktfjService itfTblazwgmxsyktfjService;
    @Resource
    ItfTblazwgmxqitafjService itfTblazwgmxqitafjService;
    @Resource
    ItfTblazwgmxqitafjCJMapper itfTblazwgmxqitafjCJMapper;
    @Resource
    com.zfire.jiasm.syncdata.service.orderBaseInstallCjOrWgService orderBaseInstallCjOrWgService;
    @Resource
    OrderInformationService orderInformationService;
    @Resource
    OrderInformationNewMapper orderInformationNewMapper;
    @Resource
    ItfTblAzAssignXzydUploadService itfTblAzAssignXzydUploadService;
    @Resource
    OrderService orderService;
    @Resource
    OrderReAppointmentService orderReAppointmentService;



    @Override
    public void processProduce() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String cjOrWg = "";
        List<OutJiasmToGreeUploadTaskType> ojtutts = new ArrayList<>();
        List<OutJiasmToGreeUploadTaskList> ojtutls = new ArrayList<>();

        // 提取品牌信息
        Brand brand = baseService.getBrand();
        if (brand == null) {
            return;
        }

        // 提取大类信息
        List<SysDictRef> sysDictRefRecs =  sysDictRefService.lambdaQuery()
                .select(SysDictRef::getRefDictName,
                        SysDictRef::getDictCode)
                .eq(SysDictRef::getBrandId, brand.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode())
                .list();
        Map<String, List<SysDictRef>> CategoryRefmap = sysDictRefRecs.stream().collect(Collectors.groupingBy(SysDictRef::getDictCode));

        log.info("开始生成需要上传的安装工单数据: \uF0B7{}",sdf.format(new Date()));

        // 数据准备类型定义表中提取所有数据
        ojtutts = outJiasmToGreeUploadTaskTypeService.lambdaQuery()
                .orderByAsc(OutJiasmToGreeUploadTaskType::getId).list();
        try{
            for (OutJiasmToGreeUploadTaskType item:ojtutts){
                // 任务类型
                int uploadTaskType = item.getUploadTaskType();

                // 数据上传任务表提取对应的任务类型数据
                ojtutls = outJiasmToGreeUploadTaskListService.lambdaQuery()
                        .eq(OutJiasmToGreeUploadTaskList::getSynStatus,0)
                        .eq(OutJiasmToGreeUploadTaskList::getUploadTaskType,uploadTaskType)

                        // .eq(OutJiasmToGreeUploadTaskList::getSynReqId,1699L) // 测试用
                        // .eq(OutJiasmToGreeUploadTaskList::getOrderNo,"A1691659257781723138") // 测试用
                        // .eq(OutJiasmToGreeUploadTaskList::getTableName,"order_base") // 测试用

                        .lt(OutJiasmToGreeUploadTaskList::getSynTimes,10)
                        .orderByAsc(OutJiasmToGreeUploadTaskList::getSynReqId).list();
                for (OutJiasmToGreeUploadTaskList ojtutl:ojtutls){
                    try{
                        // 安装工单数据生成
                        // 同步任务表中的主键,安装工单号,源表主键
                        // TL_PrePareSynDataby_fa_order_install_test
                        // 安装工单 基础表数据同步
                        // 多生成221多出主表上传，采集的相关数据根据 order_install_detail的状态及发送状态处理，并不会多出采集数据的生成

                        if (ojtutl.getTableName().equals("order_base") &&
                                ojtutl.getOrderType().equals(OrderTypeEnum.ORDER_TYPE_INSTALL.getCode())){
                            // 22 - TL_PrePareSynDataby_fa_order_install_test
                            // 221 - TL_PrePareSynDataby_az_assign_cj
                            // 222 - TL_PrePareSynDataby_az_assign_wg
                            if (uploadTaskType==22){
                                if (!orderBaseInstallCjOrWgService.orderBaseInstall(brand, ojtutl, CategoryRefmap)) {
                                    continue;
                                }
                            } else if (uploadTaskType==221 || uploadTaskType==222){
                                // 报完工这里执行
                                cjOrWg="cj";
                                if (uploadTaskType==222) {
                                    cjOrWg = "wg";
                                }
                                if (!orderBaseInstallCjOrWgService.orderBaseInstallCjOrWg(cjOrWg, brand, ojtutl, CategoryRefmap)) {
                                    continue;
                                }
                            }
                        }
                        // 安装工单操作明细 TL_PrePareSynDataby_fa_order_install_operating_log
                        if (ojtutl.getTableName().equals("order_install_operating_log") &&
                                ojtutl.getOrderType().equals(OrderTypeEnum.ORDER_TYPE_INSTALL.getCode())){
                            getFkmx(ojtutl);
                        }
                        // 安装工单用户改约明细 与操作明细最好放在一起，因表设计分开，所以独立开来处理
                        if (ojtutl.getTableName().equals("order_re_appointment") &&
                                ojtutl.getOrderType().equals(OrderTypeEnum.ORDER_TYPE_INSTALL.getCode())){
                            getAppointment(ojtutl);
                        }
                        // 生成安装工单明细数据 TL_PrePareSynDataby_fa_order_install_product
                        if (ojtutl.getTableName().equals("order_product") &&
                                ojtutl.getOrderType().equals(OrderTypeEnum.ORDER_TYPE_INSTALL.getCode())){
                            generateAzMx(brand, ojtutl);
                        }
                        // 生成安装工单子信息数据 TL_PrePareSynDataby_fa_order_install_information
                        if (ojtutl.getTableName().equals("order_information") &&
                                ojtutl.getOrderType().equals(OrderTypeEnum.ORDER_TYPE_INSTALL.getCode())){
                            generateInformation(ojtutl);
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        String msg ="out_jiasm_to_gree_upload_task_list syn_req_id="+ojtutl.getSynReqId() +
                                " table="+ojtutl.getTableName() + " id="+ojtutl.getOrderNo() + " id="+
                                ojtutl.getId();
                        log.info(msg);
                        if (ex.getMessage() !=null){
                            msg = ex.getMessage();
                        }
                        publicService.updateUploadTaskLisError(ojtutl,msg);
                        log.info("开始预处理生成需要上传的安装工单数据出错");
                    }
                }
            }

            log.info("生成需要上传的安装工单数据结束: \uF0B7{}",sdf.format(new Date()));

        } catch (Exception ex) {
            ex.printStackTrace();
            log.info("开始预处理生成需要上传的安装工单数据出错");
        }
    }




















    @Transactional(rollbackFor = Exception.class)
    public void generateInformation(OutJiasmToGreeUploadTaskList ojtutl) {
        WebsitFWS websitFWS = new WebsitFWS();

        // 提取对应的子信息
        OrderInformation oi = orderInformationService.lambdaQuery()
                .eq(OrderInformation::getId, ojtutl.getId())
                .last("limit 1")
                .one();
        if (oi!=null){
            // 更新 pgguid,pgid 因该表无这两个字段，从工单主表中取即可
            // 修改内部序
            upOutId(ojtutl);

            // 生成待同步表数据
            OrderPar orderPar = new OrderPar();
            orderPar.setId(ojtutl.getId());
            ItfTblAzAssignXzydUpload rec = orderInformationNewMapper.getOrderInformation(orderPar);
            if (rec.getYdren().length()>20){
                rec.setYdren(rec.getYdren().substring(0,20));
            }
            if (rec.getYdrmc().length()>120){
                rec.setYdrmc(rec.getYdrmc().substring(0,120));
            }

            OrderBase orderBase = orderBaseService.lambdaQuery()
                    .eq(OrderBase::getId,rec.getWorkerOrderNo())
                    .last("limit 1")
                    .one();
            rec.setPgguid(orderBase.getPgguid());
            rec.setPgid(orderBase.getPgid());

            websitFWS = publicService.shshwdToptshwd(rec.getWdno(),orderBase);
            if (StringUtils.isEmpty(websitFWS.getWebsitId())){
                websitFWS = publicService.shxswdToptxswd(rec.getWdno(),orderBase);
            }

            rec.setWdno(websitFWS.getWebsitId());
            itfTblAzAssignXzydUploadService.save(rec);
        }
        // 修改数据上传任务表中的状态 单号,id,同步状态,类型
        publicService.updateUploadTaskListStatus(ojtutl.getOrderNo(), ojtutl.getSynReqId(),1, ojtutl.getUploadTaskType());
    }

    private void upOutId(OutJiasmToGreeUploadTaskList ojtutl) {
        List<OrderInformation> upList = new ArrayList<>();
        List<OrderInformation> detaillist = orderInformationService.lambdaQuery()
                .eq(OrderInformation::getOrderBaseId, ojtutl.getOrderNo())
                .orderByAsc(OrderInformation::getCreateTime).list();
        // 找出为空的序
        if (detaillist!=null && detaillist.size()>0){
            for (OrderInformation ite:detaillist){
                if (StringUtils.isEmpty(ite.getOutId())){
                    OrderInformation n = new OrderInformation();
                    n.setId(ite.getId());
                    n.setOutId(publicService.getUUID());
                    upList.add(n);
                }
            }
            // 修改序
            if (upList.size()>0){
                orderInformationService.updateBatchById(upList);
                upList.clear();
            }
            detaillist.clear();
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void generateAzMx(Brand brand, OutJiasmToGreeUploadTaskList ojtutl) {
        OrderBase orderBase = orderBaseService.lambdaQuery()
                .eq(OrderBase::getId, ojtutl.getOrderNo())
                .last("limit 1")
                .one();
        if (orderBase !=null && orderBase.getUploadFlag()){
            // 提取工单机型数据
            OrderProduct op = orderProductService.lambdaQuery()
                    .eq(OrderProduct::getId, ojtutl.getId())
                    .last("limit 1")
                    .one();
            if (op==null){
                throw new RemoteServiceException("order_product找不到记录id="+ojtutl.getId());
            }
            if (StringUtils.isEmpty(op.getPgguid())){
                // 更新pgguid
                upPgid(ojtutl, orderBase);
                op.setPgguid(orderBase.getPgguid());
            }
            // 更新pgmxid
            upPgmix(ojtutl, op);



            // 生成数据
            generateMx(brand, ojtutl, op,orderBase);
        }

        // 修改数据上传任务表中的状态 单号,id,同步状态,类型
        publicService.updateUploadTaskListStatus(ojtutl.getOrderNo(), ojtutl.getSynReqId(),1, ojtutl.getUploadTaskType());
    }

    private void upPgmix(OutJiasmToGreeUploadTaskList ojtutl, OrderProduct op) {
        OrderPar orderPar = new OrderPar();
        orderPar.setOrderNo(ojtutl.getOrderNo());
        orderPar.setPgid(publicService.getUUID());
        orderPar.setId(op.getId());
        getOrderProductMapper.upPgmxid(orderPar);
        op.setPgmxid(orderPar.getPgid());

       /* List<OrderProduct> opuplist = new ArrayList<>();
        List<OrderProduct> oldoplist = new ArrayList<>();
        oldoplist = orderProductService.lambdaQuery()
                .eq(OrderProduct::getOrderBaseId,ojtutl.getOrderNo()).list();
        for (OrderProduct item:oldoplist){
            if (StringUtils.isEmpty(item.getPgmxid())){
                item.setPgmxid(publicService.getUUID());
                opuplist.add(item);
            }
        }
        if (opuplist.size()>0){
            orderProductService.updateBatchById(opuplist);
            opuplist.clear();
        }*/
    }

    private void generateMx(Brand brand, OutJiasmToGreeUploadTaskList ojtutl, OrderProduct op,OrderBase orderBase) {
        OrderPar orderPar = new OrderPar();
        WebsitSH websitSH = new WebsitSH();

        orderPar.setId(op.getId());
        orderPar.setBrandId(brand.getBrandId());
        orderPar.setId(op.getId());
        orderPar.setOrderNo(ojtutl.getOrderNo());
        ItfTblAzAssignMxUpload mx = getOrderProductMapper.getItfTblAzAssignMxUpload(orderPar);
        if (mx.getDanw().length()>6){
            mx.setDanw(mx.getDanw().substring(0,6));
        }
        // 服务单位处理 售后网点处理
        websitSH = publicService.ptshwdToshshwd(orderBase.getReceWebsitId(), orderBase.getMainId());
        mx.setCzwd(websitSH.getWdId());


        mx.setWorkerOrderNo(op.getOrderBaseId());
        itfTblAzAssignMxUploadService.save(mx);
    }

    private void upPgid(OutJiasmToGreeUploadTaskList ojtutl, OrderBase orderBase) {
        // 更新pgguid
        OrderPar orderPar = new OrderPar();
        orderPar.setPgid(orderBase.getPgguid());
        orderPar.setOrderNo(ojtutl.getOrderNo());
        getOrderProductMapper.upPgguid(orderPar);
    }

    @Transactional(rollbackFor = Exception.class)
    public  void getAppointment(OutJiasmToGreeUploadTaskList ojtutl) {
        OrderBase orderBase = orderBaseService.lambdaQuery()
                .eq(OrderBase::getId, ojtutl.getOrderNo())
                .last("limit 1")
                .one();
        // 待审批改约时不同步处理
        OrderReAppointment itau = orderReAppointmentService.lambdaQuery()
                .eq(OrderReAppointment::getId,ojtutl.getId())
                .last("limit 1").one();

        if (orderBase !=null  && orderBase.getUploadFlag() && itau.getExamineStatus().equals("OK")){
            ReAppointment request = new ReAppointment();
            request.setOrderNo(ojtutl.getOrderNo());
            request.setId(ojtutl.getId());
            ItfTblAzAssignAppointmentUpload add = reAppointmentOrderMapper.getReAppointment(request);
            // 开始时间与结束时间 先取最大改约审核成功的，如无再取最大预约的
            /*List<OrderReAppointment> recs = orderReAppointmentService.lambdaQuery()
                    .eq(OrderReAppointment::getOrderBaseId,ojtutl.getOrderNo())
                    .eq(OrderReAppointment::getFlag,0)
                    .eq(OrderReAppointment::getExamineStatus,"OK")
                    .orderByDesc(OrderReAppointment::getId).list();
            if(CollectionUtils.isNotEmpty(recs)){
                add.setKssj(recs.get(0).getNewAppointmentTime());
                add.setJssj(recs.get(0).getNewAppointmentEndTime());
            } else {
                List<OrderReAppointment> rs = orderReAppointmentService.lambdaQuery()
                        .eq(OrderReAppointment::getOrderBaseId,ojtutl.getOrderNo())
                        .eq(OrderReAppointment::getFlag,1)
                        .orderByDesc(OrderReAppointment::getId).list();
                add.setKssj(recs.get(0).getOldAppointmentTime());
                add.setJssj(recs.get(0).getOldAppointmentEndTime());
            }*/

            itfTblAzAssignAppointmentUploadService.save(add);
        }

        // 修改数据上传任务表中的状态 单号,id,同步状态,类型
        publicService.updateUploadTaskListStatus(ojtutl.getOrderNo(), ojtutl.getSynReqId(),1, ojtutl.getUploadTaskType());
    }

    @Transactional(rollbackFor = Exception.class)
    public void getFkmx(OutJiasmToGreeUploadTaskList ojtutl) {
        OrderBase orderBase = orderBaseService.lambdaQuery()
                .eq(OrderBase::getId, ojtutl.getOrderNo())
                .last("limit 1")
                .one();
        if (orderBase !=null  && orderBase.getUploadFlag()){
            // 修改fikd
            OperatingLog request = new OperatingLog();
            request.setFkid(publicService.getUUID());
            request.setId(ojtutl.getId());
            operatingLogOrderInstallMapper.upLogfkid(request);

            // 修改pgguid
            OperatingLog up = new OperatingLog();
            up.setOrderNo(ojtutl.getOrderNo());
            up.setPgguid(orderBase.getPgguid());
            operatingLogOrderInstallMapper.upLogpgguid(up);

            OperatingLog que = new OperatingLog();
            que.setOrderNo(ojtutl.getOrderNo());
            que.setId(ojtutl.getId());
            ItfTblAzAssignFkmxUpload add = operatingLogOrderInstallMapper.getOperatingLog(que);
            add.setUploadTaskSynReqId(ojtutl.getSynReqId());

            // 反馈人取了网点编号需单独处理 fkren
            // 反馈网点编号 做同样处理
            WebsitPP resultData = getPPWebsitInfo(add.getFkren(), orderBase);
            add.setFkren(resultData.getWdno()+"01");
            add.setXtwdbh(resultData.getXtwdno());
            add.setWdno(resultData.getWdno());
            add.setWdmc(resultData.getWdmc());
            itfTblAzAssignFkmxUploadService.save(add);
        }

        // 修改数据上传任务表中的状态 单号,id,同步状态,类型
        publicService.updateUploadTaskListStatus(ojtutl.getOrderNo(), ojtutl.getSynReqId(),1, ojtutl.getUploadTaskType());
    }


    private WebsitPP getPPWebsitInfo(String websitId, OrderBase orderBase){
        WebsitPP resultData = new WebsitPP();
        WebsitSH websitSH = new WebsitSH();

        resultData.setXtwdno("");
        resultData.setWdno("");
        resultData.setWdmc("");

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

        resultData.setWdno(websitSH.getWdId());
        resultData.setWdmc(websitSH.getWdmc());
        resultData.setXtwdno(websitSH.getBrandSysWebsitNumber());

        return resultData;
    }

    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 ){
            // 若工单被删除不再同步 不写生成数据

            // 条件判断
            if (!checkPare(ojtutl, orderBase)) {
                return Boolean.FALSE;
            }

            // 修改采集明细表与采集条码表ID超范围 - 采集表及采集条码明细表中的outid统一修改
            // 修改采集明细表与采集条码表 outid 依采集明细表的outid为准
            orderBaseInstallCjOrWgService.upOutId(ojtutl.getId());

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

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

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

            // 上传附件表图片信息
            uploadOrderDetailFile(ojtutl, CategoryRefmap, orderBase);

            // 管理端采集的条码图并没全部给到采集条码表中，这里需要转移到采集条码表中
            List<OrderInstallDetail> details = orderInstallDetailService.lambdaQuery()
                    .eq(OrderInstallDetail::getOrderBaseId, orderBase.getId()).list();

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

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

    private void uploadOrderDetailFile(@NotNull OutJiasmToGreeUploadTaskList ojtutl, Map<String, List<SysDictRef>> CategoryRefmap, OrderBase orderBase) {
        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.path(file.getFilePath());
                File nFile = new File(oldFile);

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

            // 修改附件的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(publicService.getUUID());
                orderDetailFileService.updateById(upf);
            }
        }
    }

    private void upOrderInstallDetailCodePgguid(@NotNull OutJiasmToGreeUploadTaskList ojtutl, OrderBase orderBase) {
        List<OrderInstallDetailCode> oidcs = orderInstallDetailCodeService.lambdaQuery()
                .eq(OrderInstallDetailCode::getOrderBaseId, ojtutl.getId())
                .isNull(OrderInstallDetailCode::getPgguid)
                .list();
        if (CollectionUtils.isNotEmpty(oidcs)){
            InstalDetailCode upcode = new InstalDetailCode();
            upcode.setOrderNo(orderBase.getId());
            upcode.setPgguId(orderBase.getPgguid());
            upcode.setTmguid(publicService.getUUID());
            codeOrderInstallDetailMapper.updateCodePgguid(upcode);
        }
    }

    private void upPgwcmxid(OutJiasmToGreeUploadTaskList ojtutl) {
        List<OrderInstallDetail> upList = new ArrayList<>();
        List<OrderInstallDetail> detaillist = orderInstallDetailService.lambdaQuery()
                .eq(OrderInstallDetail::getOrderBaseId, ojtutl.getId())
                .isNull(OrderInstallDetail::getPgwcmxid)
                .list();
        if (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();
            }
        }
    }

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




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

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


    private 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())
                    .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)){
                        continue;
                    }

                    // 获取完工采集明细数据 (家用空调、商用空调、其他)
                    getCJMXData(brand, orderBase, lcls, detail,mainName);

                    // 获取采集附件数据 (家用、商用、其他)
                    getcjfjdata(orderBase, detail, mainName);

                    // 生成条码明细数据
                    getDetailCode(orderBase, detail, mainName);
                }
            }
        }

        return Boolean.TRUE;
    }

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

    private boolean uploadImage(OrderBase orderBase, OrderInstallDetailCode code) {
        String oldFile;
        String newFile;
        oldFile = publicService.path(code.getPath());
        File nFile = new File(oldFile);

        baseService.saveToFile(ossUtil.getUrlWw(code.getPath()),nFile);
        // 获取文件
        newFile = oldFile;
        WebsitFWS websitFWS = publicService.shshwdToptshwd(orderBase.getWebsitId(),orderBase);
        Boolean flag = uploadBarCodeexc(newFile, oldFile,websitFWS.getWebsitId(), code);
        new File(newFile).delete();
        if (!flag) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

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

    @Nullable
    private 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){
                // 部分参数单独处理
                setParm(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);
            }
        }
    }

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

            detaillist.clear();
        }

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

    // 处理附件采集数据上传
    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;
    }

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

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

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

    private Boolean uploadBarCodeexc(String newFile, String oldFile,String wdno,OrderInstallDetailCode code) {
        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 {
                        // 失败
                        return Boolean.FALSE;
                    }
                } else {
                    // 上传图片到OSS失败
                    return Boolean.FALSE;
                }
            } else {
                //  获取签名信息失败
                return Boolean.FALSE;
            }
            return Boolean.TRUE;
        } catch (Exception ex) {
            return Boolean.FALSE;
        }
    }

    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){
                itfTblAzWgmxJyktFjUploadService.save(jyktfj);
            }
        } else if (mainName.equals("商用空调")){
            // 商用空调 完工明细 数据生成
            ItfTblAzWgmxSyktFjUpload syktfj = fileOrderDetailMapper.getSyFileUpload(request);
            if (syktfj !=null){
                itfTblAzWgmxSyktFjUploadService.save(syktfj);
            }
        } else {
            // 其他 完工明细 数据生成
            ItfTblAzWgmxQitaFjUpload otherfj = fileOrderDetailMapper.getOtherFileUpload(request);
            if (otherfj != null){
                itfTblAzWgmxQitaFjUploadService.save(otherfj);
            }
        }
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

    }

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

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

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

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

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

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

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

    }

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

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

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

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

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

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

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

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

    }

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

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

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

    private void setOtherWdno(OrderBase orderBase, ItfTblAzWgmxQitaUpload mxup) {
        WebsitSH websitSH = new WebsitSH();

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


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

    public void upCerrentRec(OrderInstallDetail detail) {
        OrderInstallDetail updetail = new OrderInstallDetail();
        updetail.setId(detail.getId());
        updetail.setPgwcmxid(publicService.getUUID());
        orderInstallDetailService.updateById(updetail);
    }

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

    public void addAndUpdate(OutJiasmToGreeUploadTaskList ojtutl, ItfTblAzAssignLcLsUpload lcls) {

        // 插入工单主表数据
        itfTblAzAssignLcLsUploadService.save(lcls);

        // 修改数据上传任务表中的状态
        // updateUploadTaskList(ojtutl);
    }


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


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