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.plugin.lets.plugin.sales.SoSaleReturnPluginInitializerToB;
import com.hzya.frame.ttxofs.dto.InterfaceParamDto;
import com.hzya.frame.ttxofs.dto.ofssalesordersearch.SaleOrderMessageDto;
import com.hzya.frame.ttxofs.dto.returngoodordersearch.RerturnGoodsOrderSearchData;
import com.hzya.frame.ttxofs.dto.returngoodordersearch.RerturnGoodsOrderSearchJsonRootBean;
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;

/**
 * @Author：liuyang
 * @Package：com.hzya.frame.plugin.lets.util
 * @Project：kangarooDataCenterV3
 * @name：AfterSalesOrderUtil
 * @Date：2024/8/19 14:59
 * @Filename：AfterSalesOrderUtil
 */
@Component
public class AfterSalesOrderUtil {

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

    @Autowired
    private OfsUnifiedService ofsUnifiedService;

    /**
     * 查询OFS售后订单
     *
     * @author liuyang
     */
    public SaleOrderMessageDto queryAfterSalesOrder(StockinOrderSearchResponse.StockinOrder.StockinH header) throws Exception {
        if (header != null) {
            //根据售后入库单、查询销售订单
            RerturnGoodsOrderSearchData ofsRertunOrder = getOfsRertunOrder(header.getRefOrderCode());
            if (ofsRertunOrder == null || ofsRertunOrder.getHeader() == null || ofsRertunOrder.getDetails() == null) {
                Assert.state(false, "根据OFS售后订单编码{}，无法查询到售后订单信息！", header.getRefOrderCode());
            }
            SaleOrderMessageDto ofsOrder = getOfsOrder(ofsRertunOrder.getHeader().getRefOrderCode());
            if (ofsOrder == null) {
                Assert.state(false, "根据OFS销售订单编码{}，无法查询到销售订单信息!", ofsRertunOrder.getHeader().getRefOrderCode());
            }
            return ofsOrder;
        } else {
            logger.error("queryAfterSalesOrder方法入参为null（header为null！）");
        }
        return null;
    }

    /**
     * 批量查询OFS售后订单
     *
     * @author liuyang
     */
//    public List<SaleOrderMessageDto> queryAfterSalesOrder(StockinOrderSearchResponse.StockinOrder.StockinH header) throws Exception {
//        if (header != null && !"".equals(header.getCode())) {
//            //根据售后入库单、查询销售订单
//            RerturnGoodsOrderSearchData ofsRertunOrder = getOfsRertunOrder(header.getRefOrderCode());
//            if (ofsRertunOrder == null || ofsRertunOrder.getHeader() == null || ofsRertunOrder.getDetails() == null) {
//                Assert.state(false, "根据OFS售后订单编码{}，无法查询到售后订单信息！", header.getRefOrderCode());
//            }
//        } else {
//            logger.error("queryAfterSalesOrder方法入参为null（header为null！）");
//        }
//        return null;
//    }

    /**
     * 根据售后订单号，查询售后订单
     *
     * @param refOrderCode 售后订单号
     * @author liuyang
     */
    public RerturnGoodsOrderSearchData getOfsRertunOrder(String refOrderCode) throws Exception {
        Long pageSize = 200L;
        Long pageNo = 1L;

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

        InterfaceParamDto interfaceParamDto = new InterfaceParamDto();
        interfaceParamDto.setApi("ofs.returnOrder.search");
        interfaceParamDto.setData(JSON.toJSONString(queryOfsSoSaleOutVo));
        RerturnGoodsOrderSearchJsonRootBean rerturnGoodsOrderSearchJsonRootBean = (RerturnGoodsOrderSearchJsonRootBean) ofsUnifiedService.unified(interfaceParamDto);
        if (rerturnGoodsOrderSearchJsonRootBean != null) {
            if ("false".equals(rerturnGoodsOrderSearchJsonRootBean.getError()) && "0".equals(rerturnGoodsOrderSearchJsonRootBean.getCode()) && "Success".equals(rerturnGoodsOrderSearchJsonRootBean.getMsg())) {
                List<RerturnGoodsOrderSearchData> data = rerturnGoodsOrderSearchJsonRootBean.getData();
                if (data != null && data.size() > 0) {
                    return data.get(0);
                }
            } else {
                logger.error("查询售后订单失败，接口返回结果:{}", JSON.toJSON(rerturnGoodsOrderSearchJsonRootBean));
            }
        } else {
            logger.error("rerturnGoodsOrderSearchJsonRootBean为空（返回结果为空），查询入参条件：{}！", JSON.toJSON(queryOfsSoSaleOutVo));
        }
        Assert.state(false, "根据售后订单号:{} 没有查询到OFS售后订单", refOrderCode);
        return null;
    }

    /**
     * 根据售后订单号，查询售后订单
     *
     * @param queryOfsSoSaleOutVo             查询条件
     * @param rerturnGoodsOrderSearchDataList 需要填充原始的数组集合
     * @param pageNo                          页码
     * @author liuyang
     */
    public void getBatchOfsRertunOrder(QueryOfsSoSaleOutVo queryOfsSoSaleOutVo, List<RerturnGoodsOrderSearchData> rerturnGoodsOrderSearchDataList, Long pageNo) throws Exception {
        Assert.notNull(queryOfsSoSaleOutVo, "queryOfsSoSaleOutVo不能为空");
        Assert.notNull(rerturnGoodsOrderSearchDataList, "rerturnGoodsOrderSearchDataList不能为空");
        Assert.notNull(pageNo, "pageNo不能为空");

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

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

        InterfaceParamDto interfaceParamDto = new InterfaceParamDto();
        interfaceParamDto.setApi("ofs.returnOrder.search");
        interfaceParamDto.setData(JSON.toJSONString(queryOfsSoSaleOutVo));
        RerturnGoodsOrderSearchJsonRootBean rerturnGoodsOrderSearchJsonRootBean = (RerturnGoodsOrderSearchJsonRootBean) ofsUnifiedService.unified(interfaceParamDto);
        if (rerturnGoodsOrderSearchJsonRootBean != null) {
            if ("false".equals(rerturnGoodsOrderSearchJsonRootBean.getError()) && "0".equals(rerturnGoodsOrderSearchJsonRootBean.getCode()) && "Success".equals(rerturnGoodsOrderSearchJsonRootBean.getMsg())) {
                List<RerturnGoodsOrderSearchData> rerturnGoodsOrderSearchJsonRootBeanData = rerturnGoodsOrderSearchJsonRootBean.getData();
                if (rerturnGoodsOrderSearchJsonRootBeanData != null && rerturnGoodsOrderSearchJsonRootBeanData.size() > 0) {
                    rerturnGoodsOrderSearchDataList.addAll(rerturnGoodsOrderSearchJsonRootBeanData);
                    getBatchOfsRertunOrder(queryOfsSoSaleOutVo, rerturnGoodsOrderSearchDataList, ++pageNo);
                }
            } else {
                logger.error("查询售后订单失败，接口返回结果:{}", JSON.toJSON(rerturnGoodsOrderSearchJsonRootBean));
            }
        } else {
            logger.error("rerturnGoodsOrderSearchJsonRootBean为空（返回结果为空），查询入参条件：{}！", JSON.toJSON(queryOfsSoSaleOutVo));
        }
    }

    /**
     * 查询OFS销售订单
     *
     * @param code OFS销售订单编码
     * @author liuyang
     */
    public SaleOrderMessageDto getOfsOrder(String code) throws Exception {
        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.salesOrder.search");
        interfaceParamDto.setData(JSON.toJSONString(queryOfsSoSaleOutVo));
        SaleOrderMessageDto saleOrderMessageDto = (SaleOrderMessageDto) ofsUnifiedService.unified(interfaceParamDto);
        return saleOrderMessageDto;
    }

}
