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

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
import com.hzya.frame.plugin.lets.ofsvo.QueryOfsSoSaleOutVo;
import com.hzya.frame.ttxofs.dto.InterfaceParamDto;
import com.hzya.frame.ttxofs.dto.ofspoorder.OfsPoOrderBean;
import com.hzya.frame.ttxofs.dto.ofspoorder.OfsPoOrderData;
import com.hzya.frame.ttxofs.dto.ofspoorder.OfsPoOrderDetails;
import com.hzya.frame.ttxofs.dto.ofspoorder.OfsPoOrderHeader;
import com.hzya.frame.ttxofs.dto.ofspurchasereturnorder.PurchaseReturnOrder;
import com.hzya.frame.ttxofs.dto.ofspurchasereturnorder.PurchaseReturnOrderBean;
import com.hzya.frame.ttxofs.dto.ofssaleorderoutsearch.HeaderDetailsDto;
import com.hzya.frame.ttxofs.dto.ofssaleorderoutsearch.SaleOutReturnMessageDto;
import com.hzya.frame.ttxofs.dto.ofssalesordersearch.SaleOrderMessageDto;
import com.hzya.frame.ttxofs.dto.stock.StockinOrderSearchResponse;
import com.hzya.frame.ttxofs.service.OfsUnifiedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * OFS标准接口查询，业务区分类
 *
 * @Author：liuyang
 * @Package：com.hzya.frame.plugin.lets.util
 * @Project：kangarooDataCenterV3
 * @name：OfsStandardUtil
 * @Date：2024/8/22 13:30
 * @Filename：OfsStandardUtil
 */
@Component
public class OfsStandardUtil {

    Logger logger = LoggerFactory.getLogger(OfsStandardUtil.class);

    @Autowired
    private OfsUnifiedService ofsUnifiedService;

    /**
     * OFS出库单查询
     *
     * @param queryOfsSoSaleOutVo  接口查询入参
     * @param headerDetailsDtoList 收集对象
     * @param pageNo               从第几页开始查询
     * @param api                  api接口名称
     * @author liuyang
     */
    public void queryOfsSaleOrder(QueryOfsSoSaleOutVo queryOfsSoSaleOutVo, List<HeaderDetailsDto> headerDetailsDtoList, Long pageNo, String api) throws Exception {
        Assert.notNull(queryOfsSoSaleOutVo, "queryOfsSoSaleOutVo不能为空！");
        Assert.notNull(headerDetailsDtoList, "headerDetailsDtoList不能为空");
        Assert.notNull(pageNo, "pageNo不能为空");
        Assert.notNull(api, "api不能为空");

        Long pageSize = 200L;
        queryOfsSoSaleOutVo.setPageNo(pageNo);
        queryOfsSoSaleOutVo.setPageSize(pageSize);

        InterfaceParamDto interfaceParamDto = new InterfaceParamDto();
        interfaceParamDto.setApi(api);
        interfaceParamDto.setData(JSON.toJSONString(queryOfsSoSaleOutVo));
        SaleOutReturnMessageDto saleOutReturnMessageDto = (SaleOutReturnMessageDto) ofsUnifiedService.unified(interfaceParamDto);
        if (saleOutReturnMessageDto != null) {
            if ("false".equals(saleOutReturnMessageDto.getError()) && "0".equals(saleOutReturnMessageDto.getCode()) && "Success".equals(saleOutReturnMessageDto.getMsg())) {
                List<HeaderDetailsDto> headerDetailsDtoList1 = saleOutReturnMessageDto.getData();
                if (headerDetailsDtoList1 != null && headerDetailsDtoList1.size() > 0) {
                    headerDetailsDtoList.addAll(headerDetailsDtoList1);
                    //2024年8月23日 14:58:46 调用下一页，直到接口不返回数据为止
                    queryOfsSaleOrder(queryOfsSoSaleOutVo, headerDetailsDtoList, ++pageNo, api);
                }
            } else {
                //2024年7月30日 13:47:04 查询失败
                logger.error("查询失败，失败原因:{}", JSON.toJSON(saleOutReturnMessageDto));
            }
        } else {
            logger.error("returnMessageBasics为空！queryOfsSoSaleOutVo对象结果：{}", JSON.toJSON(queryOfsSoSaleOutVo));
        }
    }

    /**
     * OFS入库单查询
     *
     * @param queryOfsSoSaleOutVo  接口入参
     * @param headerDetailsDtoList 收集对象
     * @param pageNo               从第几页开始查询
     * @param api                  api接口名称
     * @author liuyang
     */
    public void queryOfsReturnGoods(QueryOfsSoSaleOutVo queryOfsSoSaleOutVo, List<StockinOrderSearchResponse.StockinOrder> headerDetailsDtoList, Long pageNo, String api) throws Exception {
        Assert.notNull(queryOfsSoSaleOutVo, "queryOfsSoSaleOutVo不能为空！");
        Assert.notNull(headerDetailsDtoList, "headerDetailsDtoList不能为空");
        Assert.notNull(pageNo, "pageNo不能为空");
        Assert.notNull(api, "api不能为空");

        Long pageSize = 200L;
        queryOfsSoSaleOutVo.setPageNo(pageNo);
        queryOfsSoSaleOutVo.setPageSize(pageSize);

        InterfaceParamDto interfaceParamDto = new InterfaceParamDto();
        interfaceParamDto.setApi(api);
        interfaceParamDto.setData(JSON.toJSONString(queryOfsSoSaleOutVo));
        StockinOrderSearchResponse rertunGoodsRootBean = (StockinOrderSearchResponse) ofsUnifiedService.unified(interfaceParamDto);
        if (rertunGoodsRootBean != null) {
            if ("false".equals(rertunGoodsRootBean.getError()) && "0".equals(rertunGoodsRootBean.getCode()) && "Success".equals(rertunGoodsRootBean.getMsg())) {
                List<StockinOrderSearchResponse.StockinOrder> stockinOrderList = rertunGoodsRootBean.getData();
                if (stockinOrderList != null && stockinOrderList.size() > 0) {
                    headerDetailsDtoList.addAll(stockinOrderList);
                    queryOfsReturnGoods(queryOfsSoSaleOutVo, headerDetailsDtoList, ++pageNo, api);
                }
            } else {
                logger.error("查询失败，失败原因:{}", JSON.toJSON(interfaceParamDto));
            }
        } else {
            logger.error("rertunGoodsRootBean为空！interfaceParamDto对象的结果集json：{}", JSON.toJSON(interfaceParamDto));
        }
    }

    /**
     * OFS采退订单查询
     *
     * @author liuyang
     */
    public void queryPurchaseReturnOrder(QueryOfsSoSaleOutVo queryOfsSoSaleOutVo, List<PurchaseReturnOrder> headerDetailsDtoList, Long pageNo, String api) throws Exception {
        Assert.notNull(queryOfsSoSaleOutVo, "queryOfsSoSaleOutVo不能为空!");
        Assert.notNull(headerDetailsDtoList, "headerDetailsDtoList不能为空!");
        Assert.notNull(pageNo, "pageNo不能为空!");
        Assert.notNull(api, "api不能为空!");

        Long pageSize = 200L;
        queryOfsSoSaleOutVo.setPageNo(pageNo);
        queryOfsSoSaleOutVo.setPageSize(pageSize);

        InterfaceParamDto interfaceParamDto = new InterfaceParamDto();
        interfaceParamDto.setApi(api);
        interfaceParamDto.setData(JSON.toJSONString(queryOfsSoSaleOutVo));
        PurchaseReturnOrderBean purchaseReturnOrderBean = (PurchaseReturnOrderBean) ofsUnifiedService.unified(interfaceParamDto);
        if (purchaseReturnOrderBean != null) {
            if ("false".equals(purchaseReturnOrderBean.getError()) && "0".equals(purchaseReturnOrderBean.getCode()) && "Success".equals(purchaseReturnOrderBean.getMsg())) {
                List<PurchaseReturnOrder> purchaseReturnOrders = purchaseReturnOrderBean.getData();
                if (purchaseReturnOrders != null && purchaseReturnOrders.size() > 0) {
                    headerDetailsDtoList.addAll(purchaseReturnOrders);
                    queryPurchaseReturnOrder(queryOfsSoSaleOutVo, headerDetailsDtoList, ++pageNo, api);
                }
            } else {
                logger.error("查询失败，失败原因:{}", JSON.toJSON(interfaceParamDto));
            }
        } else {
            logger.error("purchaseReturnOrderBean为空！interfaceParamDto对象的结果集json：{}", JSON.toJSON(interfaceParamDto));
        }
    }

    /**
     * 2024年8月20日 16:11:46 查询OFS采购订单
     *
     * @author liuyang
     */
    public OfsPoOrderData queryOfsPoOrder(String code) throws Exception {
        if (code != null && !"".equals(code)) {
            Long pageSize = 200L;
            Long pageNo = 1L;

            QueryOfsSoSaleOutVo queryOfsSoSaleOutVo = new QueryOfsSoSaleOutVo();
            queryOfsSoSaleOutVo.setPageNo(pageNo);
            queryOfsSoSaleOutVo.setPageSize(pageSize);
            queryOfsSoSaleOutVo.setCode(code);

            InterfaceParamDto interfaceParamDto = new InterfaceParamDto();
            interfaceParamDto.setApi("ofs.purchaseOrder.search");
            interfaceParamDto.setData(JSON.toJSONString(queryOfsSoSaleOutVo));
            OfsPoOrderBean ofsPoOrderBean = (OfsPoOrderBean) ofsUnifiedService.unified(interfaceParamDto);
            if (ofsPoOrderBean != null) {
                if ("false".equals(ofsPoOrderBean.getError()) && "0".equals(ofsPoOrderBean.getCode()) && "Success".equals(ofsPoOrderBean.getMsg())) {
                    List<OfsPoOrderData> data = ofsPoOrderBean.getData();
                    if (data != null && data.size() > 0) {
                        OfsPoOrderData ofsPoOrderData = data.get(0);
                        Assert.notNull(ofsPoOrderData, "OFS采购订单无法解析json!");

                        OfsPoOrderHeader header = ofsPoOrderData.getHeader();
                        Assert.notNull(header, "表头对象不能为空!");

                        List<OfsPoOrderDetails> details = ofsPoOrderData.getDetails();
                        Assert.notNull(details, "明细对象不能为空");
                        return data.get(0);
                    }
                } else {
                    logger.error("查询采购订单失败，失败原因:{}", JSON.toJSON(interfaceParamDto));
                }
            } else {
                logger.error("ofsPoOrderBean为空！interfaceParamDto接口入参：{}", JSON.toJSON(interfaceParamDto));
            }
        } else {
            Assert.state(false, "采购订单编码不能为空！");
        }
        return null;
    }

    /**
     * 查询OFS销售订单
     *
     * @param queryOfsSoSaleOutVo  查询条件
     * @param headerDetailsDtoList 需要填充原始的数组集合
     * @param pageNo               从第几页开始查询
     * @author liuyang
     */
    public void getOfsSaleOrder(QueryOfsSoSaleOutVo queryOfsSoSaleOutVo, List<com.hzya.frame.ttxofs.dto.ofssalesordersearch.HeaderDetailsDto> headerDetailsDtoList, Long pageNo) throws Exception {
        Assert.notNull(queryOfsSoSaleOutVo, "queryOfsSoSaleOutVo不能为空!");
        Assert.notNull(headerDetailsDtoList, "saleOrderMessageDtoList不能为空！");
        Assert.notNull(pageNo, "pageNo不能为空！");

        Long pageSize = 200L;
//        Long pageNo = 1L;
        queryOfsSoSaleOutVo.setPageNo(pageNo);
        queryOfsSoSaleOutVo.setPageSize(pageSize);

//        QueryOfsSoSaleOutVo queryOfsSoSaleOutVo = new QueryOfsSoSaleOutVo();
//        queryOfsSoSaleOutVo.setPageNo(pageNo);
//        queryOfsSoSaleOutVo.setPageSize(pageSize);
//        queryOfsSoSaleOutVo.setCode(code);

        InterfaceParamDto interfaceParamDto = new InterfaceParamDto();
        interfaceParamDto.setApi("ofs.salesOrder.search");
        interfaceParamDto.setData(JSON.toJSONString(queryOfsSoSaleOutVo));
        SaleOrderMessageDto saleOrderMessageDto = (SaleOrderMessageDto) ofsUnifiedService.unified(interfaceParamDto);
        if (saleOrderMessageDto != null) {
            if ("false".equals(saleOrderMessageDto.getError()) && "0".equals(saleOrderMessageDto.getCode()) && "Success".equals(saleOrderMessageDto.getMsg())) {
                List<com.hzya.frame.ttxofs.dto.ofssalesordersearch.HeaderDetailsDto> data = saleOrderMessageDto.getData();
                if (data != null && data.size() > 0) {
                    headerDetailsDtoList.addAll(data);
                    getOfsSaleOrder(queryOfsSoSaleOutVo, headerDetailsDtoList, ++pageNo);
                }
            } else {
                logger.error("查询失败，失败原因：{}", JSON.toJSON(saleOrderMessageDto));
            }
        } else {
            logger.error("getOfsSaleOrder为空！interfaceParamDto对象的结果集json：{}", JSON.toJSON(interfaceParamDto));
        }
    }

    /**
     * 查询OFS采购订单，根据审批时间+审批状态查询
     *
     * @param queryOfsSoSaleOutVo 查询条件
     * @param allOfsPoOrderList   结果集
     * @param pageNo              从第几页开始
     * @author liuyang
     */
    public void queryOfsPoOrder(QueryOfsSoSaleOutVo queryOfsSoSaleOutVo, List<OfsPoOrderData> allOfsPoOrderList, Long pageNo) throws Exception {
        Assert.notNull(queryOfsSoSaleOutVo, "queryOfsSoSaleOutVo不能为空");
        Assert.notNull(allOfsPoOrderList, "allOfsPoOrderList不能为空");
        Assert.notNull(pageNo, "pageNo不能为空");

        Long pageSize = 200L;
//        Long pageNo = 1L;

        queryOfsSoSaleOutVo.setPageNo(pageNo);
        queryOfsSoSaleOutVo.setPageSize(pageSize);

//        QueryOfsSoSaleOutVo queryOfsSoSaleOutVo = new QueryOfsSoSaleOutVo();
//        queryOfsSoSaleOutVo.setPageNo(pageNo);
//        queryOfsSoSaleOutVo.setPageSize(pageSize);
//        queryOfsSoSaleOutVo.setShipAt_start(startTime);
//        queryOfsSoSaleOutVo.setShipAt_end(endTime);
//        queryOfsSoSaleOutVo.setStatus(status);

        InterfaceParamDto interfaceParamDto = new InterfaceParamDto();
        interfaceParamDto.setApi("ofs.purchaseOrder.search");
        interfaceParamDto.setData(JSON.toJSONString(queryOfsSoSaleOutVo));
        OfsPoOrderBean ofsPoOrderBean = (OfsPoOrderBean) ofsUnifiedService.unified(interfaceParamDto);
        if (ofsPoOrderBean != null) {
            if ("false".equals(ofsPoOrderBean.getError()) && "0".equals(ofsPoOrderBean.getCode()) && "Success".equals(ofsPoOrderBean.getMsg())) {
                List<OfsPoOrderData> ofsPoOrderDataList = ofsPoOrderBean.getData();
                if (ofsPoOrderDataList != null && ofsPoOrderDataList.size() > 0) {
                    allOfsPoOrderList.addAll(ofsPoOrderDataList);
                    queryOfsPoOrder(queryOfsSoSaleOutVo, allOfsPoOrderList, ++pageNo);
                }
            } else {
                logger.error("查询采购订单失败，失败原因:{}", JSON.toJSON(interfaceParamDto));
            }
        } else {
            logger.error("ofsPoOrderBean为空！interfaceParamDto接口入参：{}", JSON.toJSON(interfaceParamDto));
        }
    }
}