package com.sw.mes.sap.reconciliation.warehouse;

import com.sw.mes.sap.entity.ReportSapGoodsmvt;
import com.sw.mes.sap.reconciliation.IntermediateResultEnum;
import com.sw.mes.sap.reconciliation.framework.*;
import com.sw.mes.sap.reconciliation.warehouse.data.SAPReportData;
import com.sw.mes.sap.service.ReportSapGoodsmvtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 线边仓对账逻辑
 *
 * @Author：wanjia
 * @Date：2024/2/26 13:45
 */
@Service
public class InventoryReconciliation {
    @Autowired
    private Reconciliation reconciliation;
    @Autowired
    private LineWarehouseData lineWarehouseData;

    //用于扫sap报工数据
    @Autowired
    private ReportSapGoodsmvtService reportSapGoodsmvtService;

    private final static Logger logger = LoggerFactory.getLogger(InventoryReconciliation.class);

    /**
     * 对齐库存：入库单-出库单 = 库存
     * fetch1:入库单总量
     * fetch2：库存量
     * fetch3：出库单总量
     *
     * @param code 物料料号
     */
    public ReconResult checkInventory(String bizName, String code) {
        //这里可以增加扫表程序
        return reconciliation.checkById(bizName, code, (data, intermediateResultList) -> {
            if (null == intermediateResultList || intermediateResultList.length != 3) {
                return null;
            }
            //这边等待组合多个结果进行交叉对比
            BigDecimal quantity = new BigDecimal(0);
            BigDecimal inputQuantity = new BigDecimal(0);
            BigDecimal outPutQuantity = new BigDecimal(0);
            for (IntermediateResult<WarehouseInterResult> intermediateResult : intermediateResultList) {
                if (intermediateResult.getCode() == IntermediateResultEnum.INPUT.getCode()) {
                    inputQuantity = intermediateResult.getData().getQuantity();
                }
                if (intermediateResult.getCode() == IntermediateResultEnum.OUTPUT.getCode()) {
                    outPutQuantity = intermediateResult.getData().getQuantity();
                }
                if (intermediateResult.getCode() == IntermediateResultEnum.INVENTORY.getCode()) {
                    quantity = intermediateResult.getData().getQuantity();
                }
            }
            ReconResult result = new ReconResult();
            if (quantity.compareTo(inputQuantity.subtract(outPutQuantity)) == 0) {
                result.setId(code);
                result.setEqual(true);
                result.setGap(new BigDecimal(0));
            } else {
                result.setId(code);
                result.setEqual(false);
                result.setGap(inputQuantity.subtract(outPutQuantity.add(quantity)));//计算公式优化
            }
            return result;
        }, id -> {
            //获取线边仓入库数据
            return lineWarehouseData.getWarehouseInputBillByCode((String) id);
        }, id -> {
            //获取线边仓库存数据
            return lineWarehouseData.getWarehouseInventoryByCode((String) id);
        }, id -> {
            //获取线边仓出库数据
            return lineWarehouseData.getWarehouseOutputBillByCode((String) id);
        });
    }


    /**
     * 入库单和wms进行对账，按天进行对账
     * 1、扫wms入库数据
     * 2、根据code获取input明细
     * 3、对账
     *
     * @param current 如果不传，则对全量，如果传入，则对时间所在的当天
     */
    public void inputAndWMSReconciliation(Date current) {
        reconciliation.batchScan("wms->mes", ((data, intermediateResult) -> {
            return null;
        }), queue -> {//扫wms的记录

        }, id -> {
            return null;
        });
    }


    /**
     * 线边仓出库和sap上报记录进行全量对账:
     * 1、扫表：sap上报记录
     * 2、fetch1：查询线边仓出库单
     * 3、交叉对账：
     */
    public void outputAndSAPReconciliation() {
        reconciliation.batchScan("mes->sap", (data, list) -> {
            SAPReportData reportData = (SAPReportData) data;
            BigDecimal quantity = reportData.getQuantity();
            BigDecimal outPutQuantity = new BigDecimal(0);
            for (IntermediateResult<WarehouseInterResult> intermediateResult : list) {
                //和出库记录进行对比
                if (null != intermediateResult && intermediateResult.getCode() == 200) {
                    outPutQuantity = intermediateResult.getData().getQuantity();
                }
            }
            ReconResult result = new ReconResult();
            if (quantity.compareTo(outPutQuantity) == 0) {
                result.setId(reportData.getQueryKey());
                result.setEqual(true);
                result.setGap(new BigDecimal(0));
            } else {
                result.setId(reportData.getQueryKey());
                result.setEqual(false);
                result.setGap(quantity.subtract(outPutQuantity));
            }
            return result;
        }, queue -> {//扫表逻辑
            List<ReportSapGoodsmvt> list = reportSapGoodsmvtService.groupMaterialCostAtSap();
            for (ReportSapGoodsmvt goodsmvt : list) {
                SAPReportData reportData = new SAPReportData();
                reportData.setId(goodsmvt.getId());
                reportData.setMaterialCode(goodsmvt.getMatnr());
                reportData.setQueryKey(goodsmvt.getMatnr());
                reportData.setQuantity(new BigDecimal(goodsmvt.getMenge()));
                reportData.setProcessName(goodsmvt.getProcName());
                queue.submit(reportData); //提交处理透传给下游进行对比
            }
        }, id -> {
            //获取线边仓出库数据
            return lineWarehouseData.getWarehouseOutputBillByCode((String) id);
        });
    }
}
