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.dao.IBdSetpartDao;
import com.hzya.frame.plugin.lets.entity.*;
import com.hzya.frame.plugin.lets.ofsvo.QueryOfsAdjustOrderRequestVO;
import com.hzya.frame.plugin.lets.ofsvo.QueryOfsStockinOrderRequestVO;
import com.hzya.frame.plugin.lets.u8cdto.GeneralBillVO_4ADto;
import com.hzya.frame.plugin.lets.u8cdto.ReusltStrDto;
import com.hzya.frame.plugin.lets.u8cdto.SpecialbillVO;
import com.hzya.frame.plugin.lets.util.PushDataByU8cUtil;
import com.hzya.frame.plugin.lets.util.QueryU8CEntityUtil;
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.dto.InterfaceParamDto;
import com.hzya.frame.ttxofs.dto.ofsTransferOrderSearch.OFSAdjustOrderSearchResponse;
import com.hzya.frame.ttxofs.dto.stock.StockinOrderSearchResponse;
import com.hzya.frame.ttxofs.service.OfsUnifiedService;
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.factory.annotation.Autowired;

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

/**
 * OFS调整入库(组装入库)--->U8C组装订单
 * 使用code模式太麻烦了，转n手，使用pk模式
 *
 */

public class AdjustInZZPluginInitializer extends PluginBaseEntity {
    Logger logger = LoggerFactory.getLogger(AdjustInZZPluginInitializer.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.AdjustInZZPluginInitializer";
    }

    @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 QueryU8CEntityUtil queryU8CEntityUtil;

    @Autowired
    private IBdRdclDao iBdRdclDao;
    @Autowired
    private IBdSetpartDao bdSetpartDao;

    @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");

            QueryOfsStockinOrderRequestVO queryOfsStockinOrderRequestVO = new QueryOfsStockinOrderRequestVO();
            queryOfsStockinOrderRequestVO.setCreated_start(startTimeStr);
            queryOfsStockinOrderRequestVO.setCreated_end(endTimeStr);
            List<StockinOrderSearchResponse.StockinOrder> stockinOrders = queryOfsStockinOrder(queryOfsStockinOrderRequestVO);
            System.out.println(stockinOrders);

            if (stockinOrders.size() == 0) {
                return;
            }
            //过滤日志
            List<StockinOrderSearchResponse.StockinOrder> filterStockinOrders = filterData(stockinOrders);
            if (filterStockinOrders.size() == 0) {
                return;
            }

            //推送
            implement(filterStockinOrders);


        } catch (Exception e) {
            logger.error("丽知：OFS调整入库(组装入库)--->U8C组装订单,start()方法报错：", e);
        }
    }

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

            QueryOfsStockinOrderRequestVO queryOfsStockinOrderRequestVO = new QueryOfsStockinOrderRequestVO();
            queryOfsStockinOrderRequestVO.setCode(stockinNo);
            List<StockinOrderSearchResponse.StockinOrder> stockinOrders = queryOfsStockinOrder(queryOfsStockinOrderRequestVO);
            System.out.println(stockinOrders);

            if (stockinOrders.size() == 0) {
                Assert.state(false, "丽知：OFS调整入库(组装入库)--->U8C组装订单根据单据号[入库完成]：{}，未查询到调整出库单", stockinNo);
            }
            //过滤日志
            List<StockinOrderSearchResponse.StockinOrder> filterStockinOrders = filterData(stockinOrders);
            if (filterStockinOrders.size() == 0) {
                Assert.state(false, "丽知：OFS调整入库(组装入库)--->U8C组装订单根据单据号[入库完成]：{}，该调整出库单已被推送成功，请勿重新推送", stockinNo);
            }

            //推送
            implement(filterStockinOrders);

        } catch (Exception e) {
            logger.error("丽知：OFS调整入库(组装入库)--->U8C组装订单,start(String goodsName)方法报错：", e);
        }

    }

    /**
     * 按时间区间
     */
    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";

            QueryOfsStockinOrderRequestVO queryOfsStockinOrderRequestVO = new QueryOfsStockinOrderRequestVO();
            queryOfsStockinOrderRequestVO.setClosedAt_start(start);
            queryOfsStockinOrderRequestVO.setClosedAt_end(end);
            List<StockinOrderSearchResponse.StockinOrder> stockinOrders = queryOfsStockinOrder(queryOfsStockinOrderRequestVO);
            System.out.println(stockinOrders);

            if (stockinOrders.size() == 0) {
                Assert.state(false, "丽知：OFS调整入库(组装入库)--->U8C组装订单根据日期期间[开始时间]-[结束时间]：{}-{}，未查询到调整入库单[入库完成]", startTime, endTime);
            }
            //过滤日志
            List<StockinOrderSearchResponse.StockinOrder> filterStockinOrders = filterData(stockinOrders);
            if (filterStockinOrders.size() == 0) {
                Assert.state(false, "丽知：OFS调整入库(组装入库)--->U8C组装订单根据日期期间[开始时间]-[结束时间]：{}-{}，该调期间整入库单[入库完成]已被推送成功，请勿重新推送", startTime, endTime);
            }

            //推送
            implement(filterStockinOrders);


        } catch (Exception e) {
            logger.error("丽知：OFS调整入库(组装入库)--->U8C组装订单,start(String startTime, String endTime)方法报错：", e);
        }
    }

    /**
     * 过滤成功日志,收发类别非0106 组装入库 的过滤
     * //订单号+sku过滤
     *
     * @return
     */
    public List<StockinOrderSearchResponse.StockinOrder> filterData(List<StockinOrderSearchResponse.StockinOrder> stockinOrders) throws Exception {
        List<StockinOrderSearchResponse.StockinOrder> filterStockinOrders = new ArrayList<>();
        for (StockinOrderSearchResponse.StockinOrder stockinOrder : stockinOrders) {
            StockinOrderSearchResponse.StockinOrder.StockinH header = stockinOrder.getHeader();

            //cdispatcherid--收发类别,U8_RECEIPT_TYPE	0106    组装入库
            List<OFSAdjustOrderSearchResponse.OFSAdjustOrder> ofsAdjustOrders = queryOfsAdjustOrders(header.getRefOrderCode());
            if(!"0106".equals(ofsAdjustOrders.get(0).getHeader().getU8ReceiptType())){
                continue;
            }
            List<StockinOrderSearchResponse.StockinOrder.StockinB> filterStockinB=new ArrayList<>();
            for (StockinOrderSearchResponse.StockinOrder.StockinB detail : stockinOrder.getDetails()) {
                String rootAppPk = header.getCode()+"_"+detail.getSkuCode();
                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) {
                    filterStockinB.add(detail);
                }

            }
            if (filterStockinB.size() == 0) {//该单全被过滤
                continue;
            }else if(filterStockinB.size() != 0){
                stockinOrder.setDetails(filterStockinB);
                filterStockinOrders.add(stockinOrder);
            }
        }
        return filterStockinOrders;
    }

    /**
     * 推送U8C组装单
     * 根据货品编码查询成套键，找到组件，套件。
     * 组件数量=子母比*数量
     *
     * @param filterStockinOrders
     */
    public void implement(List<StockinOrderSearchResponse.StockinOrder> filterStockinOrders) {
        for (StockinOrderSearchResponse.StockinOrder stockinOrder : filterStockinOrders) {
            String mapStr = "";
            String format = "";
            String response = "";
            String ofsCode = "";
            try {

                StockinOrderSearchResponse.StockinOrder.StockinH ofsHeader = stockinOrder.getHeader();
                System.out.println(ofsHeader);
                //主表
                SpecialbillVO specialbillVO = new SpecialbillVO();
                SpecialbillVO.Parentvo parentvo = new SpecialbillVO.Parentvo();

                //pk_corp 公司
                BdCorpEntity bdCorpEntity = queryU8CEntityUtil.queryBdCorpByUnitCode(ofsHeader.getCompanyCode());
                Assert.notNull(bdCorpEntity,"根据公司编码：{}，未查询到公司主键，请检查U8C公司档案",ofsHeader.getCompanyCode());
                parentvo.setPk_corp(bdCorpEntity.getPkCorp());
                //dbilldate 单据日期
                format = ofsHeader.getCreated();
                parentvo.setDbilldate(StringUtils.substring(format, 0, 10));
                //coperatorid 制单人
                parentvo.setCoperatorid(OverallConstant.getOverAllValue("u8cApiZdrPK"));
                //cinwarehouseid 入库仓库(审批，3.6及上版本支持)
                BdStordocEntity bdStordocEntity = queryU8CEntityUtil.queryBdStordocByCode(ofsHeader.getFacilityCode());
                Assert.notNull(bdStordocEntity,"根据仓库编码：{}，未查询到公司主键，请检查U8C公司档案",ofsHeader.getCompanyCode());
                parentvo.setCinwarehouseid(bdStordocEntity.getPkStordoc());

                //vnote 备注
                parentvo.setVnote(ofsHeader.getNote());

                //vuserdef15,,组装单标识
                parentvo.setVuserdef15("ZZ");
                //vuserdef17，来源系统
                parentvo.setVuserdef17(ProfilesActiveConstant.sourceSystem1);
                //vuserdef19,ofs调整单单号
                parentvo.setVuserdef19(ofsHeader.getCode());
                //vuserdef20,ofs调整单id
                parentvo.setVuserdef20(ofsHeader.getId());


                //子表
                List<SpecialbillVO.Childrenvo> childrenvo =new ArrayList<>();
                int indexNum=0;//主数量（套件数量）
                int i=1;//行号
                //////////套件，每个成套件变成一张组装单，
                for (StockinOrderSearchResponse.StockinOrder.StockinB detail : stockinOrder.getDetails()) {
                    try {

                        BdInvmandocEntity bdInvmandocEntity = queryBdInvmandocBySkuCodeAndPkcorp(detail.getSkuCode(), bdCorpEntity.getPkCorp());
                        SpecialbillVO.Childrenvo children = new SpecialbillVO.Childrenvo();
                        //行号
                        children.setCrowno(i++ + "0");
                        //fbillrowflag 类型（0：成套件，1：配件）
                        children.setFbillrowflag("0");
                        //cinventoryid 存货（存货管理档案）
                        children.setCinventoryid(bdInvmandocEntity.getPkInvmandoc());
                        //dshldtransnum 数量
                        children.setDshldtransnum(detail.getRequestQty());
                        //cwarehouseid 仓库
                        children.setCwarehouseid(bdStordocEntity.getPkStordoc());

                        //套件数量
                        indexNum = Integer.parseInt(detail.getRequestQty());
                        childrenvo.add(children);

                        //配件
                        List<BdSetpartEntity> bdSetpartEntities = queryBdSetPart(bdInvmandocEntity.getPkInvmandoc());
                        for (BdSetpartEntity bdSetpartEntity : bdSetpartEntities) {
                            SpecialbillVO.Childrenvo children1 = new SpecialbillVO.Childrenvo();
                            //行号
                            children1.setCrowno(i++ + "0");
                            //fbillrowflag 类型（0：成套件，1：配件）
                            children1.setFbillrowflag("1");
                            //cinventoryid 存货（存货管理档案）
                            children1.setCinventoryid(bdSetpartEntity.getPkInvmandocpart());
                            //dshldtransnum 数量 套件数*配料表数
                            children1.setDshldtransnum(String.valueOf(indexNum*Integer.parseInt(bdSetpartEntity.getChildsnum())));
                            //cwarehouseid 仓库
                            children1.setCwarehouseid(bdStordocEntity.getPkStordoc());
                            childrenvo.add(children1);
                        }
                        specialbillVO.setParentvo(parentvo);
                        specialbillVO.setChildrenvo(childrenvo);
                        System.out.println(specialbillVO);
                        mapStr="{\"specialbillvo\": ["+JSONUtil.toJsonStr(specialbillVO)+"]}";
                        System.out.println(mapStr);

                        //推送
                        ZTResult ztResult = pushU8CByApiCode.pushByPK(OverallConstant.getOverAllValue("special4lSaveCode"), 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 = "";
                        String cSpecialHId = "";
                        if ("success".equals(attribute.getStatus())) {
                            isSuccess = true;
                            String data = attribute.getData();
                            SpecialbillVO resSpecialbillVO = resultDataHandle(data);
                            System.out.println(resSpecialbillVO);
                            vbillcode = resSpecialbillVO.getParentvo().getVbillcode();
                            cSpecialHId = resSpecialbillVO.getParentvo().getCspecialhid();
                        }
                        if (!isSuccess) {
                            Assert.state(false, "推送U8C--> 丽知：OFS调整入库(组装入库)--->U8C组装订单 失败 接口返回结果：{} 接口入参:{}", response, mapStr);
                        }

                        //成功
                        IntegrationTaskLivingDetailsEntity integrationTaskLivingDetailsEntity = new IntegrationTaskLivingDetailsEntity();
                        integrationTaskLivingDetailsEntity.setNewState(ProfilesActiveConstant.LOG_STATUS_Y);
                        integrationTaskLivingDetailsEntity.setRootAppNewData(mapStr);
                        integrationTaskLivingDetailsEntity.setNewTransmitInfo(response);
                        integrationTaskLivingDetailsEntity.setNewPushDate(new Date());
                        integrationTaskLivingDetailsEntity.setBusinessDate(format);
                        integrationTaskLivingDetailsEntity.setRootAppPk(ofsCode+"_"+detail.getSkuCode());
                        integrationTaskLivingDetailsEntity.setRootAppBill(ofsCode);
                        integrationTaskLivingDetailsEntity.setNewSystemNumber(vbillcode);
                        integrationTaskLivingDetailsEntity.setNewSystemPrimary(cSpecialHId);
                        integrationTaskLivingDetailsEntity.setPluginId(getPluginId());
                        saveOrUpdateBusinessLogUtil.saveOrUpdate(integrationTaskLivingDetailsEntity);

                    }catch (Exception e){//内层catch
                        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+"_"+detail.getSkuCode());
                        integrationTaskLivingDetailsEntity.setRootAppBill(ofsCode);
                        integrationTaskLivingDetailsEntity.setPluginId(getPluginId());
                        saveOrUpdateBusinessLogUtil.saveOrUpdate(integrationTaskLivingDetailsEntity);

                    }
                }
            } catch (Exception e) {//外层catch 多个物料，多条记录
                e.printStackTrace();
                logger.error("推送U8C，丽知：OFS调整入库(组装入库)--->U8C组装订单失败，失败原因：" + e);
                String ErrMessage = "推送U8C，丽知：OFS调整入库(组装入库)--->U8C组装订单失败，失败原因：" + e.getMessage();
                //失败
                for (StockinOrderSearchResponse.StockinOrder.StockinB detail : stockinOrder.getDetails()) {
                    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+"_"+detail.getSkuCode());
                    integrationTaskLivingDetailsEntity.setRootAppBill(ofsCode);
                    integrationTaskLivingDetailsEntity.setPluginId(getPluginId());
                    saveOrUpdateBusinessLogUtil.saveOrUpdate(integrationTaskLivingDetailsEntity);
                }
            }
        }
    }


    public List<StockinOrderSearchResponse.StockinOrder> queryOfsStockinOrder(QueryOfsStockinOrderRequestVO queryOfsStockinOrderRequestVO) throws Exception {
        Long pageNo = 1L;
        List<StockinOrderSearchResponse.StockinOrder> all = new ArrayList<>();


        InterfaceParamDto interfaceParamDto = new InterfaceParamDto();
        interfaceParamDto.setApi("ofs.receipt.search");
        queryOfsStockinOrderRequestVO.setInternalInstructionType("ADJUST");//入库单类型 SALES销售 EXCHANGE换货 REISSUE补发RETURN销退 PURCHASE采购 PURCHASE_RETURN采退 TRANSFER调拨 WORK_ORDER加工 OTHER其他 ADJUST调整 EXCHANGE换货
        queryOfsStockinOrderRequestVO.setClientCode("LETS");
        queryOfsStockinOrderRequestVO.setStatus("900");//状态 500待同步 510已同步 700收货中 900入库完成 911作废 100订单池 200暂挂 400待合单 410待出库单出库
        queryOfsStockinOrderRequestVO.setPageNo(pageNo);
        queryOfsStockinOrderRequestVO.setPageSize(500L);
        interfaceParamDto.setData(JSON.toJSONString(queryOfsStockinOrderRequestVO));
        StockinOrderSearchResponse stockinOrderSearchResponse = (StockinOrderSearchResponse) ofsUnifiedService.unified(interfaceParamDto);
        if (stockinOrderSearchResponse.getData() == null || stockinOrderSearchResponse.getData().size() == 0) {
            return null;
        }

        all.addAll(stockinOrderSearchResponse.getData());
        int count = stockinOrderSearchResponse.getData().size();
        while (count >= 500) {//>=500,可能还有下一页。
            //还有下一页
            pageNo++;
            queryOfsStockinOrderRequestVO.setPageNo(pageNo);
            interfaceParamDto.setData(JSON.toJSONString(queryOfsStockinOrderRequestVO));
            StockinOrderSearchResponse stockinOrderSearchResponse1 = (StockinOrderSearchResponse) ofsUnifiedService.unified(interfaceParamDto);

            all.addAll(stockinOrderSearchResponse1.getData());

            count = stockinOrderSearchResponse1.getData().size();
        }
        return all;
    }


    private SpecialbillVO resultDataHandle(String resultData) {
        try {
            if (resultData != null && !"".equals(resultData)) {
                if (resultData.contains("[")) {
                    resultData = resultData.substring(1, resultData.length() - 1);
                }
                return JSON.parseObject(resultData, SpecialbillVO.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;
    }


    /**
     * 根据存货编码+公司主键查询查询货品管理档案
     */
    public BdInvmandocEntity queryBdInvmandocBySkuCodeAndPkcorp(String skuCode,String pkCorp){

        //查询存货基本档案
        BdInvbasdocEntity bdInvbasdocEntity = queryU8CEntityUtil.queryBdInvbasdocByCode(skuCode);
        if (bdInvbasdocEntity == null) {
            Assert.state(false,"根据存货编码：{},查询存货基本档案失败，请检查。",skuCode);
        }

        //查询存货管理档案
        BdInvmandocEntity bdInvmandocEntity = queryU8CEntityUtil.queryBdInvmandocByPkInvbasdocAndPkCorp(bdInvbasdocEntity.getPkInvbasdoc(), pkCorp);
        if (bdInvmandocEntity == null) {
            Assert.state(false,"根据存货编码：{},存货基本档案主键：{}，公司编码：{},查询存货管理档案失败，请检查是否分配。",skuCode,bdInvbasdocEntity.getPkInvbasdoc(),pkCorp);
        }
        return bdInvmandocEntity;
    }

    /**
     * 查询成套件信息,因为是调整入库，只有套件信息
     * @param pkinvmandoc 套件存货管理档案
     * 套件 存货管理档案的主键
     * @return 配件的 存货管理档案
     */
    public List<BdSetpartEntity> queryBdSetPart(String pkinvmandoc){
        //查询配件信息
        BdSetpartEntity bdSetpartEntity = new BdSetpartEntity();
        bdSetpartEntity.setPkInvmandocset(pkinvmandoc);
        bdSetpartEntity.setDataSourceCode(ProfilesActiveConstant.LETS_DATE_SOURCE);
        List<BdSetpartEntity> query = bdSetpartDao.query(bdSetpartEntity);
        if(query.size()==0){
            Assert.state(false,"OFS调整入库（组装）,查询");
        }
        return query;
    }

}
