package com.sw.mes.sap.verify.closeOrderState;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.poi.wp.usermodel.Paragraph;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sw.mes.sap.dto.WarehouseStatisticsDto;
import com.sw.mes.sap.dto.jip.JipCloseOrderVerifyResult;
import com.sw.mes.sap.entity.LogSapStock;
import com.sw.mes.sap.entity.ReportSapGoodsmvt;
import com.sw.mes.sap.service.LogSapStockService;
import com.sw.mes.sap.service.ReportSapGoodsmvtService;
import com.sw.mes.sap.verify.VerifyWorkOrderState;

/**
 * 线边仓库存验证
 * 
 * @author liru
 * @date 2024-04-17
 */
@Component("storeVerify")
public class StoreVerify implements VerifyWorkOrderState {

	@Autowired
	private ReportSapGoodsmvtService reportSapGoodsmvtService;
	@Autowired
	private LogSapStockService logSapStockService;

	@Override
	public void executeVerify(JipCloseOrderVerifyResult result, String AUFNR, String WERKS) {
		
		List<ReportSapGoodsmvt> allMatnrChargLgorts = reportSapGoodsmvtService.findAllOrderMatnrChargLgort(AUFNR);
		
		for (ReportSapGoodsmvt reportSapGoodsmvt : allMatnrChargLgorts) {
			String aufnr = reportSapGoodsmvt.getAufnr();
			String matnr = reportSapGoodsmvt.getMatnr();
			String charg = reportSapGoodsmvt.getCharg();
			String lgort = reportSapGoodsmvt.getLgort();
			
			//1. 查询SAP库存 查询线边仓库存 比对结果
			WarehouseStatisticsDto param = new WarehouseStatisticsDto();
			param.setMatnr(matnr);
			param.setLgorts(Arrays.asList(lgort));
			param.setChargs(Arrays.asList(charg));
			List<Map<Object, Object>> comparisonResults = logSapStockService.realTimeReconciliationOfInventory(param);

			if(comparisonResults!=null) {
				for (Map<Object, Object> comparisonResult : comparisonResults) {
					Object _cy = comparisonResult.get("库存差异");
					if(Double.valueOf(_cy.toString()) != 0) {
						result.setStatus(false);
			            result.setMsg("库存差异:"+_cy+" 工单["+AUFNR+"] 物料["+matnr+"] 批次["+charg+"] 库位["+lgort+"],不允许关单");
			            break;
					}
				}
			}
		}
	}

}
