package com.hzya.frame.plugin.lets.plugin.adjust;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.base.PluginBaseEntity;
import com.hzya.frame.plugin.lets.constant.OverallConstant;
import com.hzya.frame.plugin.lets.constant.ProfilesActiveConstant;
import com.hzya.frame.plugin.lets.dao.IBdRdclDao;
import com.hzya.frame.plugin.lets.entity.BdRdclEntity;
import com.hzya.frame.plugin.lets.ofsvo.QueryOfsAdjustOrderRequestVO;
import com.hzya.frame.plugin.lets.ofsvo.QueryOfsStockinOrderRequestVO;
import com.hzya.frame.plugin.lets.ofsvo.QueryOfsStockoutOrderRequestVO;
import com.hzya.frame.plugin.lets.u8cdto.GeneralBillVO_4ADto;
import com.hzya.frame.plugin.lets.u8cdto.GeneralBillVO_4IDto;
import com.hzya.frame.plugin.lets.u8cdto.ReusltStrDto;
import com.hzya.frame.plugin.lets.util.PushDataByU8cUtil;
import com.hzya.frame.plugin.lets.util.SaveOrUpdateBusinessLogUtil;
import com.hzya.frame.plugin.lets.util.pushData.Attribute;
import com.hzya.frame.plugin.lets.util.pushData.PushU8CByApiCode;
import com.hzya.frame.plugin.lets.util.pushData.ZTResult;
import com.hzya.frame.sysnew.integtationTaskLivingDetails.dao.IIntegrationTaskLivingDetailsDao;
import com.hzya.frame.sysnew.integtationTaskLivingDetails.entity.IntegrationTaskLivingDetailsEntity;
import com.hzya.frame.ttxofs.basics.ReturnMessageBasics;
import com.hzya.frame.ttxofs.dto.InterfaceParamDto;
import com.hzya.frame.ttxofs.dto.ofsTransferOrderSearch.OFSAdjustOrderSearchResponse;
import com.hzya.frame.ttxofs.dto.ofssaleorderoutsearch.DetailsDto;
import com.hzya.frame.ttxofs.dto.ofssaleorderoutsearch.HeaderDetailsDto;
import com.hzya.frame.ttxofs.dto.ofssaleorderoutsearch.HeaderDto;
import com.hzya.frame.ttxofs.dto.ofssaleorderoutsearch.SaleOutReturnMessageDto;
import com.hzya.frame.ttxofs.dto.stock.StockinOrderSearchResponse;
import com.hzya.frame.ttxofs.dto.stock.StockoutOrderSearchResponse;
import com.hzya.frame.ttxofs.service.OfsUnifiedService;
import com.hzya.frame.u8c.ax.entity.Ass;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 丽知：OFS调整出库--->U8C其它出库
 * 排除（U8_SHIPMENT_TYPE）：组装出库
 * U8_SHIPMENT_TYPE	0206	 	组装出库
 * U8出库类型（U8_SHIPMENT_TYPE）
 */
public class AdjustOutPluginInitializer extends PluginBaseEntity {
    Logger logger = LoggerFactory.getLogger(AdjustOutPluginInitializer.class);

    private static final ReentrantLock LOCK = new ReentrantLock(true);

    @Override
    public void initialize() {
        logger.info(getPluginLabel() + "執行初始化方法initialize()");
    }

    @Override
    public void destroy() {
        logger.info(getPluginLabel() + "執行銷毀方法destroy()");
    }

    @Override
    public String getPluginId() {
        return "adjust.AdjustOutPluginInitializer";
    }

    @Override
    public String getPluginName() {
        return "丽知：OFS调整出库--->U8C其它出库";
    }

    @Override
    public String getPluginLabel() {
        return "丽知：OFS调整出库--->U8C其它出库";
    }

    @Override
    public String getPluginType() {
        return "1";
    }

    @Autowired
    private IIntegrationTaskLivingDetailsDao iIntegrationTaskLivingDetailsDao;
    @Autowired
    private PushDataByU8cUtil pushDataByU8cUtil;
    @Autowired
    private PushU8CByApiCode pushU8CByApiCode;
    @Autowired
    private SaveOrUpdateBusinessLogUtil saveOrUpdateBusinessLogUtil;

    @Autowired
    private OfsUnifiedService ofsUnifiedService;
    @Autowired
    private IBdRdclDao iBdRdclDao;

    @Override
    public JsonResultEntity executeBusiness(JSONObject requestJson) throws Exception {
        try {
            logger.info("调用：" + getPluginName() + "-插件");
            String prod = "prod";
            String param = String.valueOf(requestJson.get("param"));
            if (requestJson != null && ProfilesActiveConstant.TYPE_DATE.equals(requestJson.get("type"))) {
                //按日期
                if (param != null && !"".equals(param)) {
                    String[] split = param.split("/");
                    if (!(split.length == 2)) {
                        Assert.state(false, "时间格式传递不正确");
                    }
                    Assert.notNull(split[0], "开始时间不能为空");
                    Assert.notNull(split[1], "结束时间不能为空");
                    start(split[0], split[1]);
                }
            } else if (requestJson != null && ProfilesActiveConstant.TYPE_VBILLCODE.equals(requestJson.get("type"))) {
                //按单号
                if (param != null && !"".equals(param)) {
                    start(param);
                }
            } else if (ProfilesActiveConstant.TYPE_DETAIL_ERROR.equals(requestJson.get("type"))) {
                String integrationTaskLivingDetails = (String) requestJson.get("integration_task_living_details_id");
                IntegrationTaskLivingDetailsEntity integrationTaskLivingDetailsEntity = saveOrUpdateBusinessLogUtil.queryIntegrationTaskLivingDetailsEntity(integrationTaskLivingDetails);
                start(integrationTaskLivingDetailsEntity.getRootAppPk());
            } else if (ProfilesActiveConstant.LETS_PROFILES_ACTIVE.equals(prod)) {
                //默认
                start();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("executeBusiness方法抛出异常", e);
        }
        return null;
    }


    /**
     * 默认推送
     */
    public void start() {
        try {
            //获取当前时间
            Date currentDate = new Date();
            Date startTime = DateUtil.offset(currentDate, DateField.MINUTE, -10);
            Date endTime = DateUtil.offset(currentDate, DateField.MINUTE, -2);
            String startTimeStr = DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss");
            String endTimeStr = DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss");

            QueryOfsStockoutOrderRequestVO queryOfsStockoutOrderRequestVO = new QueryOfsStockoutOrderRequestVO();
            queryOfsStockoutOrderRequestVO.setClosedAt_start(startTimeStr);
            queryOfsStockoutOrderRequestVO.setClosedAt_end(endTimeStr);
            List<StockoutOrderSearchResponse.StockoutOrder> stockoutOrders = queryOfsStockoutOrder(queryOfsStockoutOrderRequestVO);
            if (stockoutOrders == null || stockoutOrders.size() == 0) {
                return;
            }
            //过滤日志
            List<StockoutOrderSearchResponse.StockoutOrder> filterStockoutOrders = filterData(stockoutOrders);
            if (filterStockoutOrders == null || filterStockoutOrders.size() == 0) {
                return;
            }
            //推送
            implement(filterStockoutOrders);


        } catch (Exception e) {
            logger.error("丽知：OFS调整出库--->U8C其它出库,start()方法报错：", e);
        }
    }

    /**
     * 按单号
     */
    public void start(String stockoutNo) {
        try {

            QueryOfsStockoutOrderRequestVO queryOfsStockoutOrderRequestVO = new QueryOfsStockoutOrderRequestVO();
            queryOfsStockoutOrderRequestVO.setCode(stockoutNo);
            List<StockoutOrderSearchResponse.StockoutOrder> stockoutOrders = queryOfsStockoutOrder(queryOfsStockoutOrderRequestVO);
            if (stockoutOrders == null || stockoutOrders.size() == 0) {
                Assert.state(false, "丽知：OFS调整出库--->U8C其它出库根据单据号[出库完成]：{}，未查询到调整出库单", stockoutNo);
            }
            //过滤日志
            List<StockoutOrderSearchResponse.StockoutOrder> filterStockoutOrders = filterData(stockoutOrders);
            if (filterStockoutOrders == null || filterStockoutOrders.size() == 0) {
                Assert.state(false, "丽知：OFS调整出库--->U8C其它出库根据单据号[出库完成]：{}，该调整出库单已被推送成功，请勿重新推送", stockoutNo);
            }
            //推送
            implement(filterStockoutOrders);


        } catch (Exception e) {
            logger.error("丽知：OFS调整出库--->U8C其它出库,start(String goodsName)方法报错：", e);
        }

    }

    /**
     * 按时间区间,ofs接口最大显示7天，时间计算7天分段。
     */
    public void start(String startTime, String endTime) {
        try {
            Date business_start = DateUtil.parse(startTime);
            Date business_end = DateUtil.parse(endTime);
            String start = DateUtil.format(business_start, "yyyy-MM-dd") + " 00:00:00";
            String end = DateUtil.format(business_end, "yyyy-MM-dd") + " 23:59:59";

            QueryOfsStockoutOrderRequestVO queryOfsStockoutOrderRequestVO = new QueryOfsStockoutOrderRequestVO();
            queryOfsStockoutOrderRequestVO.setShipAt_start(start);
            queryOfsStockoutOrderRequestVO.setShipAt_end(end);
            List<StockoutOrderSearchResponse.StockoutOrder> stockoutOrders = queryOfsStockoutOrder(queryOfsStockoutOrderRequestVO);
            if (stockoutOrders == null || stockoutOrders.size() == 0) {
                Assert.state(false, "丽知：OFS调整出库--->U8C其它出库根据日期期间[开始时间]-[结束时间]：{}-{}，未查询到调整出库单[出库完成]", startTime, endTime);

            }
            //过滤日志
            List<StockoutOrderSearchResponse.StockoutOrder> filterStockoutOrders = filterData(stockoutOrders);
            if (filterStockoutOrders == null || filterStockoutOrders.size() == 0) {
                Assert.state(false, "丽知：OFS调整出库--->U8C其它出库根据日期期间[开始时间]-[结束时间]：{}-{}，该调期间整出库单[出库完成]已被推送成功，请勿重新推送", startTime, endTime);
            }
            //推送
            implement(filterStockoutOrders);


        } catch (Exception e) {
            logger.error("丽知：OFS调整出库--->U8C其它出库,start(String startTime, String endTime)方法报错：", e);
        }
    }


    /**
     * 过滤成功日志
     */
    public List<StockoutOrderSearchResponse.StockoutOrder> filterData(List<StockoutOrderSearchResponse.StockoutOrder> stockoutOrders) {
        List<StockoutOrderSearchResponse.StockoutOrder> filterStockoutOrders = new ArrayList<>();
        for (StockoutOrderSearchResponse.StockoutOrder stockoutOrder : stockoutOrders) {
            StockoutOrderSearchResponse.StockoutOrder.StockoutH header = stockoutOrder.getHeader();
            String rootAppPk = header.getCode();
            boolean isExis = true;
            IntegrationTaskLivingDetailsEntity integrationTaskLivingDetailsEntity = new IntegrationTaskLivingDetailsEntity();
            integrationTaskLivingDetailsEntity.setRootAppPk(rootAppPk);
            integrationTaskLivingDetailsEntity.setNewState(ProfilesActiveConstant.LOG_STATUS_Y);
            integrationTaskLivingDetailsEntity.setPluginId(getPluginId());
            List<IntegrationTaskLivingDetailsEntity> integrationTaskLivingDetailsEntities = iIntegrationTaskLivingDetailsDao.query(integrationTaskLivingDetailsEntity);
            if (integrationTaskLivingDetailsEntities == null || integrationTaskLivingDetailsEntities.size() == 0) {
                isExis = false;
            }
            if (!isExis) {
                filterStockoutOrders.add(stockoutOrder);
            }
        }
        return filterStockoutOrders;

    }

    /**
     * 丽知：OFS调整出库--->U8C其它出库
     *
     * @param filterStockoutOrders
     */
    public void implement(List<StockoutOrderSearchResponse.StockoutOrder> filterStockoutOrders) {
        for (StockoutOrderSearchResponse.StockoutOrder stockoutOrder : filterStockoutOrders) {
            String mapStr = "";
            String format = "";
            String response = "";
            String ofsCode = "";
            try {
                GeneralBillVO_4IDto generalBillVO_4I = new GeneralBillVO_4IDto();

                //主表
                StockoutOrderSearchResponse.StockoutOrder.StockoutH ofsHeader = stockoutOrder.getHeader();

                GeneralBillVO_4IDto.Parentvo parentvo = new GeneralBillVO_4IDto.Parentvo();


                //ofs订单号，自定义项19
                ofsCode = ofsHeader.getCode();
                parentvo.setVuserdef19(ofsCode);

                //pk_corp--公司
                parentvo.setPk_corp(ofsHeader.getCompanyCode());
                //pk_calbody--库存组织PK
                parentvo.setPk_calbody(ofsHeader.getCompanyCode());
                //cwarehouseid--仓库
                parentvo.setCwarehouseid(ofsHeader.getFacilityCode());

                //vnote--备注
                parentvo.setVnote(ofsHeader.getShipmentNote());
                //dbilldate--单据日期--oms入库时间
                String created1 = ofsHeader.getCreated();
//                Date created = ofsHeader.getCreated();
//                format = DateUtil.format(created, "yyyy-MM-dd HH:mm:ss");
                format = created1;
                parentvo.setDbilldate(StringUtils.substring(format, 0, 10));
                //coperatorid--制单人
                parentvo.setCoperatorid("tbadmin");
                //审核时间dauditdate
                parentvo.setDauditdate(StringUtils.substring(format, 0, 10));

                //cdispatcherid--收发类别
                List<OFSAdjustOrderSearchResponse.OFSAdjustOrder> ofsAdjustOrders = queryOfsAdjustOrders(ofsHeader.getRefOrderCode());
                if (ofsAdjustOrders.size() != 1) {
                    Assert.state(false, "根据调整出库单号：{}，查询调整单：{}，失败。不存在或不唯一。", ofsCode, ofsHeader.getRefOrderCode());
                }
                String u8ShipmentType = ofsAdjustOrders.get(0).getHeader().getU8ShipmentType();//出库类型
                if("0206".equals(u8ShipmentType)){//20240910：排除组装出库，不做处理
                    continue;
                }

//                BdRdclEntity bdRdclEntity = new BdRdclEntity();
//                bdRdclEntity.setRdname("调整出库");
//                bdRdclEntity.setRdcode(u8ShipmentType);
//                bdRdclEntity.setDr(0);
//                bdRdclEntity.setDataSourceCode(ProfilesActiveConstant.LETS_DATE_SOURCE);
//                List<BdRdclEntity> BdRdclEntityList = iBdRdclDao.query(bdRdclEntity);
//                if (BdRdclEntityList.size() == 0) {
//                    Assert.state(false, "查询U8C收发类型：调整出库失败。未查询到U8C收发类型。");
//                }
//                String RdclCode = BdRdclEntityList.get(0).getRdcode();//收发类别编码
                parentvo.setCdispatcherid(u8ShipmentType);

                //设置自动拣货
//              parentvo.setIsautopick("Y");

                //子表
                List<GeneralBillVO_4IDto.Childrenvo> childrenvo = new ArrayList<>();
                for (StockoutOrderSearchResponse.StockoutOrder.StockoutB ofsDetail : stockoutOrder.getDetails()) {
                    System.out.println(ofsDetail);
                    GeneralBillVO_4IDto.Childrenvo children = new GeneralBillVO_4IDto.Childrenvo();

                    //dbizdate业务日期
                    children.setDbizdate(format);

                    //存货档案id
                    children.setCinventoryid(ofsDetail.getSkuCode());

                    //nshouldinnum--应出数量
                    children.setNshouldoutnum(String.valueOf(ofsDetail.getRequestQty()));

                    //nintnum--实出数量
                    children.setNoutnum(String.valueOf(ofsDetail.getShipQty()));


                    childrenvo.add(children);
                }

                generalBillVO_4I.setParentvo(parentvo);
                generalBillVO_4I.setChildrenvo(childrenvo);
                String dataJson = JSONUtil.toJsonStr(generalBillVO_4I);
                mapStr = "{\"GeneralBillVO_4I\":[" + dataJson + "]}";
//                response = pushDataByU8cUtil.pushU8CByCode(OverallConstant.getOverAllValue("otheroutqz"), mapStr);
                ZTResult ztResult = pushU8CByApiCode.pushByCode(OverallConstant.getOverAllValue("otheroutqzCode"), mapStr);

                //两层判断,一层中台转发,一层u8c返回
                if ("false".equals(ztResult.getFlag())) {
                    //转发失败
                    response = JSONUtil.toJsonStr(ztResult.getAttribute());
                    Assert.state(false, "丽知：OFS调整出库--->U8C其它出库.中台转发失败,失败原因:{}", ztResult.getAttribute());
                }
                Attribute attribute = ztResult.getAttribute();
                System.out.println(attribute);

                boolean isSuccess = false;
                String vbillcode = "";
                if ("success".equals(attribute.getStatus())) {
                    isSuccess = true;
                    String data = attribute.getData();
                    GeneralBillVO_4IDto resGeneralBillVO = resultDataHandle(data);
                    System.out.println(resGeneralBillVO);
                    vbillcode = resGeneralBillVO.getParentvo().getVbillcode();
                }
                if (!isSuccess) {
                    Assert.state(false, "推送U8C--> 丽知：OFS调整出库--->U8C其它出库 失败 接口返回结果：{} 接口入参:{}", attribute.getErrormsg(), mapStr);
                }
                //成功
                IntegrationTaskLivingDetailsEntity integrationTaskLivingDetailsEntity = new IntegrationTaskLivingDetailsEntity();
                integrationTaskLivingDetailsEntity.setNewState(ProfilesActiveConstant.LOG_STATUS_Y);
                integrationTaskLivingDetailsEntity.setRootAppNewData(mapStr);
                integrationTaskLivingDetailsEntity.setNewTransmitInfo(attribute.getData());
                integrationTaskLivingDetailsEntity.setNewPushDate(new Date());
                integrationTaskLivingDetailsEntity.setBusinessDate(format);
                integrationTaskLivingDetailsEntity.setRootAppPk(ofsCode);
                integrationTaskLivingDetailsEntity.setRootAppBill(ofsCode);
                integrationTaskLivingDetailsEntity.setNewSystemNumber(vbillcode);//u8c其他出库单号
                integrationTaskLivingDetailsEntity.setNewSystemPrimary(vbillcode);//u8c其他出库单号
                integrationTaskLivingDetailsEntity.setPluginId(getPluginId());
                saveOrUpdateBusinessLogUtil.saveOrUpdate(integrationTaskLivingDetailsEntity);

            } catch (Exception e) {
                e.printStackTrace();
                logger.error("推送U8C，丽知：OFS调整出库--->U8C其它出库失败，失败原因：" + e);
                ReusltStrDto reusltStrDto = JSON.parseObject(response, ReusltStrDto.class);

                String ErrMessage = "推送U8C，丽知：OFS调整出库--->U8C其它出库失败，失败原因：" + reusltStrDto.getErrormsg();

                //失败
                IntegrationTaskLivingDetailsEntity integrationTaskLivingDetailsEntity = new IntegrationTaskLivingDetailsEntity();
                integrationTaskLivingDetailsEntity.setNewState(ProfilesActiveConstant.LOG_STATUS_N);
                integrationTaskLivingDetailsEntity.setRootAppNewData(mapStr);
                integrationTaskLivingDetailsEntity.setNewTransmitInfo(ErrMessage);
                integrationTaskLivingDetailsEntity.setNewPushDate(new Date());
                integrationTaskLivingDetailsEntity.setBusinessDate(format);
                integrationTaskLivingDetailsEntity.setRootAppPk(ofsCode);
                integrationTaskLivingDetailsEntity.setRootAppBill(ofsCode);
                integrationTaskLivingDetailsEntity.setPluginId(getPluginId());
                saveOrUpdateBusinessLogUtil.saveOrUpdate(integrationTaskLivingDetailsEntity);
            }
        }

    }

    public List<StockoutOrderSearchResponse.StockoutOrder> queryOfsStockoutOrder(QueryOfsStockoutOrderRequestVO queryOfsStockoutOrderRequestVO) throws Exception {
        Long pageNo = 1L;
        List<StockoutOrderSearchResponse.StockoutOrder> all = new ArrayList<>();

        InterfaceParamDto interfaceParamDto = new InterfaceParamDto();
        interfaceParamDto.setApi("ofs.shipment.search");
        queryOfsStockoutOrderRequestVO.setInternalInstructionType("ADJUST");//入库单类型 SALES销售 EXCHANGE换货 REISSUE补发RETURN销退 PURCHASE采购 PURCHASE_RETURN采退 TRANSFER调拨 WORK_ORDER加工 OTHER其他 ADJUST调整 EXCHANGE换货
        queryOfsStockoutOrderRequestVO.setClientCode("LETS");
        //状态 500待同步 510已同步 700收货中 900入库完成 911作废 100订单池 200暂挂 400待合单 410待出库单出库
        queryOfsStockoutOrderRequestVO.setStatus("900");
        queryOfsStockoutOrderRequestVO.setPageNo(pageNo);
        queryOfsStockoutOrderRequestVO.setPageSize(500L);
        interfaceParamDto.setData(JSON.toJSONString(queryOfsStockoutOrderRequestVO));
        SaleOutReturnMessageDto saleOutReturnMessageDto = (SaleOutReturnMessageDto) ofsUnifiedService.unified(interfaceParamDto);
        StockoutOrderSearchResponse stockoutOrderSearchResponse = SaleOutReturnMessageDto2StockoutOrderSearchResponse(saleOutReturnMessageDto);
        if (stockoutOrderSearchResponse.getData() == null || stockoutOrderSearchResponse.getData().size() == 0) {
            return null;
        }
        all.addAll(stockoutOrderSearchResponse.getData());
        int count = stockoutOrderSearchResponse.getData().size();
        while (count >= 500) {//>=500,可能还有下一页。
            //还有下一页
            pageNo++;
            queryOfsStockoutOrderRequestVO.setPageNo(pageNo);
            interfaceParamDto.setData(JSON.toJSONString(queryOfsStockoutOrderRequestVO));
            SaleOutReturnMessageDto saleOutReturnMessageDto1 = (SaleOutReturnMessageDto) ofsUnifiedService.unified(interfaceParamDto);
            StockoutOrderSearchResponse stockoutOrderSearchResponse1 = SaleOutReturnMessageDto2StockoutOrderSearchResponse(saleOutReturnMessageDto1);
            all.addAll(stockoutOrderSearchResponse1.getData());
            count = stockoutOrderSearchResponse1.getData().size();
        }

        return all;
    }

    /**
     * 只是命名不一样，为了方便区分，顾转换为stockout
     *
     * @param saleOutReturnMessageDto
     * @return
     */
    public StockoutOrderSearchResponse SaleOutReturnMessageDto2StockoutOrderSearchResponse(SaleOutReturnMessageDto saleOutReturnMessageDto) {
        StockoutOrderSearchResponse stockoutOrderSearchResponse = new StockoutOrderSearchResponse();
        List<StockoutOrderSearchResponse.StockoutOrder> data = new ArrayList<>();
        BeanUtils.copyProperties(saleOutReturnMessageDto, stockoutOrderSearchResponse);
        for (HeaderDetailsDto saleOutData : saleOutReturnMessageDto.getData()) {
            HeaderDto saleOutHeader = saleOutData.getHeader();
            StockoutOrderSearchResponse.StockoutOrder stockoutOrder = new StockoutOrderSearchResponse.StockoutOrder();
            StockoutOrderSearchResponse.StockoutOrder.StockoutH stockoutH = new StockoutOrderSearchResponse.StockoutOrder.StockoutH();
            List<StockoutOrderSearchResponse.StockoutOrder.StockoutB> stockoutBList = new ArrayList<>();
            for (DetailsDto saleOutDetail : saleOutData.getDetails()) {
                StockoutOrderSearchResponse.StockoutOrder.StockoutB stockoutB = new StockoutOrderSearchResponse.StockoutOrder.StockoutB();
                BeanUtils.copyProperties(saleOutDetail, stockoutB);
                stockoutBList.add(stockoutB);
            }
            //创建时间，修改时间，创建人，修改人，赋给表头
            StockoutOrderSearchResponse.StockoutOrder.StockoutB stockoutB = stockoutBList.get(0);
            stockoutH.setCreated(stockoutB.getCreated());
            stockoutH.setCreatedBy(stockoutB.getCreatedBy());
            stockoutH.setLastUpdated(stockoutB.getLastUpdated());
            stockoutH.setLastUpdatedBy(stockoutB.getLastUpdatedBy());

            BeanUtils.copyProperties(saleOutHeader, stockoutH);
            stockoutOrder.setHeader(stockoutH);
            stockoutOrder.setDetails(stockoutBList);
            data.add(stockoutOrder);
        }
        stockoutOrderSearchResponse.setData(data);
        System.out.println(stockoutOrderSearchResponse);
        return stockoutOrderSearchResponse;
    }

    private GeneralBillVO_4IDto resultDataHandle(String resultData) {
        try {
            if (resultData != null && !"".equals(resultData)) {
                if (resultData.contains("[")) {
                    resultData = resultData.substring(1, resultData.length() - 1);
                }
                return JSON.parseObject(resultData, GeneralBillVO_4IDto.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("解析返回参数失败的错误", e);
            //如果解析失败，记录原因，但是不能影响结果的记录
        }
        return null;
    }


    /**
     * ofs调整单查询
     * ofs. adjustOrder.search
     */
    public List<OFSAdjustOrderSearchResponse.OFSAdjustOrder> queryOfsAdjustOrders(String ofsAdjustCode) throws Exception {
        QueryOfsAdjustOrderRequestVO queryOfsAdjustOrderRequestVO = new QueryOfsAdjustOrderRequestVO();
        queryOfsAdjustOrderRequestVO.setClientCode("LETS");
        queryOfsAdjustOrderRequestVO.setCode(ofsAdjustCode);
        queryOfsAdjustOrderRequestVO.setPageNo(1L);
        queryOfsAdjustOrderRequestVO.setPageSize(500L);

        InterfaceParamDto interfaceParamDto = new InterfaceParamDto();
        interfaceParamDto.setApi("ofs.adjustOrder.search");
        interfaceParamDto.setData(JSON.toJSONString(queryOfsAdjustOrderRequestVO));
        OFSAdjustOrderSearchResponse ofsAdjustOrderSearchResponse = (OFSAdjustOrderSearchResponse) ofsUnifiedService.unified(interfaceParamDto);
        System.out.println(ofsAdjustOrderSearchResponse);
        List<OFSAdjustOrderSearchResponse.OFSAdjustOrder> data = ofsAdjustOrderSearchResponse.getData();
        return data;
    }
}
