package com.cim.idm.mwmsextend.materialshiprequest.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jdom.Element;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.orm.service.CommonServiceDAO;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import com.cim.idm.wmspackage.materialshiprequestdetail.MaterialShipRequestDetailServiceProxy;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.data.MaterialShipRequestDetail;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.data.MaterialShipRequestDetailKey;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.info.CreateMaterialShipRequestDetailInfo;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.info.MakeShipDetailDecreaseAssignedInfo;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.info.MakeShipDetailIncreaseAssignedInfo;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.info.NonPrimaryKeyValueInfo;

public class MaterialShipRequestDetailServiceImpl extends CommonServiceDAO<MaterialShipRequestDetailKey, MaterialShipRequestDetail>{
	
	
	public MaterialShipRequestDetail getShipRequestDetail(String siteName, String invoiceNo, String invoiceDetailNo) 
	{
		MaterialShipRequestDetailKey keyInfo = new MaterialShipRequestDetailKey(siteName, invoiceNo,invoiceDetailNo);
		MaterialShipRequestDetail selectByKey = selectByKey(keyInfo);
		return selectByKey; 
	}
	

	/**
	 * 2024.09.26
	 */
    // 新增shipRequestDetailImpl.create4WMS25
    public void create4WMS25(EventInfo eventInfo, String siteName, String shipRequestName, String shipRequestDetailName,
            String materialSpecName, String grade,
            String domesticExport, String requestQuantity, String assignedQuantity, String charge, String erpLocation,
            String erpFactoryName, String erpReceiveLocation, String remark, String loadPosition, String POSID) {
        CreateMaterialShipRequestDetailInfo info = new CreateMaterialShipRequestDetailInfo();

        info.setSiteName(siteName);
        info.setShipRequestName(shipRequestName);
        info.setShipRequestDetailName(shipRequestDetailName);
        info.setMaterialSpecName(materialSpecName);
        info.setGrade(grade);
        info.setDomesticExport(domesticExport);

        double requestQty = Double.parseDouble(requestQuantity);

		double assignedQty = Double.parseDouble(assignedQuantity);

        info.setRequestQuantity(requestQty);
        info.setAssignedQuantity(assignedQty);

        Map<String, Object> bindMap = new HashMap<String, Object>();
        bindMap.put("charge", charge);
        bindMap.put("erpLocation", erpLocation);
        bindMap.put("erpFactoryName", erpFactoryName);
        bindMap.put("erpReceiveLocation", erpReceiveLocation);
        bindMap.put("remark", remark);
        bindMap.put("loadPosition", loadPosition);
        bindMap.put("posid", POSID);
        info.setUserColumns(bindMap);

        eventInfo.setEventName("Create");

        MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().create(info);

    }
    
    // 2024.09.26
    public void create(EventInfo eventInfo, String siteName, String shipRequestName, String shipRequestDetailName,
            String materialSpecName, String grade,
            String domesticExport, String requestQuantity, String assignedQuantity, String charge, String erpLocation,
            String erpFactoryName, String erpReceiveLocation, String remark, String loadPosition,
            String seq, String wbs_O, String wbs_N, String meIns, String erfMe, String xloEk, String kzEar,
            String xwaOk, String liFnr, String name1, String bwtar, String sobkz) {
        CreateMaterialShipRequestDetailInfo info = new CreateMaterialShipRequestDetailInfo();

        info.setSiteName(siteName);
        info.setShipRequestName(shipRequestName);
        info.setShipRequestDetailName(shipRequestDetailName);
        info.setMaterialSpecName(materialSpecName);
        info.setGrade(grade);
        info.setDomesticExport(domesticExport);

		double requestQty = Double.parseDouble(requestQuantity);

		double assignedQty = Double.parseDouble(assignedQuantity);

        info.setRequestQuantity(requestQty);
        info.setAssignedQuantity(assignedQty);

        Map<String, Object> bindMap = new HashMap<String, Object>();
        bindMap.put("charge", charge);
        bindMap.put("erpLocation", erpLocation);
        bindMap.put("erpFactoryName", erpFactoryName);
        bindMap.put("erpReceiveLocation", erpReceiveLocation);
        bindMap.put("remark", remark);
        bindMap.put("loadPosition", loadPosition);
        bindMap.put("seq", seq);
        bindMap.put("wbs_O", wbs_O);
        bindMap.put("wbs_N", wbs_N);
        bindMap.put("meIns", meIns);
        bindMap.put("erfMe", erfMe);
        bindMap.put("xloEk", xloEk);
        bindMap.put("kzEar", kzEar);
        bindMap.put("xwaOk", xwaOk);
        bindMap.put("liFnr", liFnr);
        bindMap.put("name1", name1);
        bindMap.put("bwtar", bwtar);
        bindMap.put("sobkz", sobkz);
        info.setUserColumns(bindMap);

        eventInfo.setEventName("Create");

        MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().create(info);

    }
    

    // 新增shipRequestDetailImpl.modify4WMS25
    public void modify4WMS25(EventInfo eventInfo, String siteName, String shipRequestName, String shipRequestDetailName,
            String materialSpecName, String grade,
            String domesticExport, String requestQuantity, String assignedQuantity, String erpLocation,
            String erpFactoryName, String charge, String remark, String POSID) {

        MaterialShipRequestDetailKey key = new MaterialShipRequestDetailKey();

        key.setSiteName(siteName);
        key.setShipRequestName(shipRequestName);
        key.setShipRequestDetailName(shipRequestDetailName);

        Map<String, Object> bindMap = new HashMap<String, Object>();
        bindMap.put("materialSpecName", materialSpecName);
        bindMap.put("grade", grade);
        bindMap.put("domesticExport", domesticExport);
        bindMap.put("requestQuantity", Double.parseDouble(requestQuantity));
        bindMap.put("assignedQuantity", Double.parseDouble(assignedQuantity));

        bindMap.put("charge", charge);
        bindMap.put("erpLocation", erpLocation);
        bindMap.put("erpFactoryName", erpFactoryName);
        bindMap.put("remark", remark);
        bindMap.put("posid", POSID);

        // SetUserColumnsInfo setEventInfo = new SetUserColumnsInfo();
        // setEventInfo.setUserColumns(bindMap);
        NonPrimaryKeyValueInfo nonPrimaryKeyValueInfo = new NonPrimaryKeyValueInfo();
        nonPrimaryKeyValueInfo.setUserColumns(bindMap);

        eventInfo.setEventName("Modify");

        MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().setEvent(key,
                nonPrimaryKeyValueInfo);

    }

	
	public void updateShipRequestDetailAssignedQty(String siteName, String invoiceNo, String invoiceDetailNo, String boxQty,String flag){
		MaterialShipRequestDetailKey keyInfo = new MaterialShipRequestDetailKey(siteName, invoiceNo,invoiceDetailNo);
		MaterialShipRequestDetail selectByKey = selectByKey(keyInfo);
		
		MakeShipDetailIncreaseAssignedInfo makeShipDetailIncreaseAssignedInfo=new MakeShipDetailIncreaseAssignedInfo();
		MakeShipDetailDecreaseAssignedInfo makeShipDetailDecreaseAssignedInfo=new MakeShipDetailDecreaseAssignedInfo();
		double qty = Double.parseDouble(boxQty);
		if("Assign".equals(flag)){
			makeShipDetailIncreaseAssignedInfo.setAssignedQuantity(qty);
			MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().increaseAssignedQuantity(keyInfo, makeShipDetailIncreaseAssignedInfo);
		} else if("Deassign".equals(flag)){
			makeShipDetailDecreaseAssignedInfo.setAssignedQuantity(qty);
			MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().decreaseAssignedQuantity(keyInfo, makeShipDetailDecreaseAssignedInfo);
		}

		
	}
	public String getShipDetailName(String shipRequestName,String materialSpecName,String materialQuantity)throws Exception {
		String shipRequestDetailName="";
		String sql = "SELECT a.SHIPREQUESTDETAILNAME, a.REQUESTQUANTITY-a.ASSIGNEDQUANTITY as QUANTITY FROM MATERIALSHIPREQUESTDETAIL a WHERE SHIPREQUESTNAME=:shipRequestName and MATERIALSPECNAME=:materialSpecName  ";
		Map<String, String> bindMap = new HashMap<String, String>();
		bindMap.put("shipRequestName", shipRequestName);
		bindMap.put("materialSpecName", materialSpecName);
		List<Map<String, Object>> sqlResult = IDMFrameServiceProxy
				.getSqlTemplate()
				.queryForList(sql, bindMap);
		if(sqlResult.size()>0)
		{
			for (int i = 0; i < sqlResult.size(); i++) {
				if((Double.parseDouble(sqlResult.get(i).get("QUANTITY").toString())-(Double.parseDouble(materialQuantity))>=0))
				{
					shipRequestDetailName= sqlResult.get(i).get("SHIPREQUESTDETAILNAME").toString();
					break;
				}
			}
		}
		if(shipRequestDetailName.equals(""))
			throw new CustomException("FindDetailError","","");
		return shipRequestDetailName;
	}
	public void updateShipRequestDetailAssignedQty(org.jdom.Document doc,
			String flag) {

		flag = "Assign";

		Element root = doc.getDocument().getRootElement();

		Element element = root.getChild("Body"); // .getChild("MATERIALPACKING");

		for (Iterator<?> iterator = element.getChildren()
				.iterator(); iterator.hasNext();) {
			Element mpE = (Element) iterator.next();

			if (mpE.getName().equals("MATERIALPACKING")) {
			String siteName = mpE.getChild("SITENAME").getText();
			String invoiceNo = mpE.getChild("SHIPREQUESTNAME").getText();
			String invoiceDetailNo = mpE.getChild("SHIPREQUESTDETAILNAME")
					.getText();
			String boxQty = mpE.getChild("MATERIALQUANTITY").getText();

			MaterialShipRequestDetailKey keyInfo = new MaterialShipRequestDetailKey(
					siteName, invoiceNo, invoiceDetailNo);
			MaterialShipRequestDetail selectByKey = selectByKey(keyInfo);

			MakeShipDetailIncreaseAssignedInfo makeShipDetailIncreaseAssignedInfo = new MakeShipDetailIncreaseAssignedInfo();
			MakeShipDetailDecreaseAssignedInfo makeShipDetailDecreaseAssignedInfo = new MakeShipDetailDecreaseAssignedInfo();
				double qty = Double.parseDouble(boxQty);
			if ("Assign".equals(flag)) {
				makeShipDetailIncreaseAssignedInfo.setAssignedQuantity(qty);
				MaterialShipRequestDetailServiceProxy
						.getMaterialShipRequestDetailService()
						.increaseAssignedQuantity(keyInfo,
								makeShipDetailIncreaseAssignedInfo);
			} else if ("Deassign".equals(flag)) {
				makeShipDetailDecreaseAssignedInfo.setAssignedQuantity(qty);
				MaterialShipRequestDetailServiceProxy
						.getMaterialShipRequestDetailService()
						.decreaseAssignedQuantity(keyInfo,
								makeShipDetailDecreaseAssignedInfo);
			}
			}
		
		}

		
	}

	public void create(EventInfo eventInfo,String siteName,String shipRequestName,String shipRequestDetailName,String materialSpecName,String grade,
			String domesticExport,String requestQuantity,String assignedQuantity, String charge, String erpLocation,String erpFactoryName, String erpReceiveLocation,String remark,String loadPosition)
	{
		CreateMaterialShipRequestDetailInfo info = new CreateMaterialShipRequestDetailInfo();
		
		info.setSiteName(siteName);
		info.setShipRequestName(shipRequestName);
		info.setShipRequestDetailName(shipRequestDetailName);
		info.setMaterialSpecName(materialSpecName);
		info.setGrade(grade);
		info.setDomesticExport(domesticExport);

		double requestQty = Double.parseDouble(requestQuantity);

		double assignedQty = Double.parseDouble(assignedQuantity);
		
		info.setRequestQuantity(requestQty);
		info.setAssignedQuantity(assignedQty);
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("charge",charge);
		bindMap.put("erpLocation",erpLocation);
		bindMap.put("erpFactoryName",erpFactoryName);
		bindMap.put("erpReceiveLocation",erpReceiveLocation);
		bindMap.put("remark", remark);
		bindMap.put("loadPosition",loadPosition);
		info.setUserColumns(bindMap);
		
		eventInfo.setEventName("Create");
						
		MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().create(info);
		
	}
	
	
	public void create1(EventInfo eventInfo,String siteName,String shipRequestName,String shipRequestDetailName,String materialSpecName,String grade,
			String domesticExport,String requestQuantity,String assignedQuantity, String erpLocation,String erpFactoryName,String charge)
	{
		CreateMaterialShipRequestDetailInfo info = new CreateMaterialShipRequestDetailInfo();
		
		info.setSiteName(siteName);
		info.setShipRequestName(shipRequestName);
		info.setShipRequestDetailName(shipRequestDetailName);
		info.setMaterialSpecName(materialSpecName);
		info.setGrade(grade);
		info.setDomesticExport(domesticExport);
			
		double requestQty = Double.parseDouble(requestQuantity);

		double assignedQty = Double.parseDouble(assignedQuantity);
		
		info.setRequestQuantity(requestQty);
		info.setAssignedQuantity(assignedQty);
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("charge",charge);
		bindMap.put("erpLocation",erpLocation);
		bindMap.put("erpFactoryName",erpFactoryName);

		info.setUserColumns(bindMap);
		
		eventInfo.setEventName("Create");
						
		MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().create(info);
		
	}
	public void BMDTcreate(EventInfo eventInfo,String siteName,String shipRequestName,String shipRequestDetailName,String materialSpecName,
			String requestQuantity, String erpLocation,String erpFactoryName,String charge,String erpReceiveLocation)
	{
		CreateMaterialShipRequestDetailInfo info = new CreateMaterialShipRequestDetailInfo();
		
		info.setSiteName(siteName);
		info.setShipRequestName(shipRequestName);
		info.setShipRequestDetailName(shipRequestDetailName);
		info.setMaterialSpecName(materialSpecName);
			
		double requestQty = Double.parseDouble(requestQuantity);
				
		info.setRequestQuantity(requestQty);
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("charge",charge);
		bindMap.put("erpLocation",erpLocation);
		bindMap.put("erpFactoryName",erpFactoryName);
		bindMap.put("erpReceiveLocation",erpReceiveLocation);
		info.setUserColumns(bindMap);
		
		eventInfo.setEventName("Create");
						
		MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().create(info);
		
	}
	
	
	public void modify(EventInfo eventInfo,String siteName,String shipRequestName,String shipRequestDetailName,String materialSpecName,String grade,
			String domesticExport,String requestQuantity,String assignedQuantity)
	{
			
		MaterialShipRequestDetailKey key = new MaterialShipRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);
		key.setShipRequestDetailName(shipRequestDetailName);
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("materialSpecName", materialSpecName);
		bindMap.put("grade", grade);
		bindMap.put("domesticExport", domesticExport);
		bindMap.put("requestQuantity", Double.parseDouble(requestQuantity));
		bindMap.put("assignedQuantity", Double.parseDouble(assignedQuantity));
		
		
//		SetUserColumnsInfo setEventInfo = new SetUserColumnsInfo();
//		setEventInfo.setUserColumns(bindMap);
		NonPrimaryKeyValueInfo nonPrimaryKeyValueInfo = new NonPrimaryKeyValueInfo();
		nonPrimaryKeyValueInfo.setUserColumns(bindMap);
		
		eventInfo.setEventName("Modify");
						
		MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().setEvent(key, nonPrimaryKeyValueInfo);
		
	}
	
	public void modify1(EventInfo eventInfo,String siteName,String shipRequestName,String shipRequestDetailName,String materialSpecName,String grade,
			String domesticExport,String requestQuantity,String assignedQuantity,String erpLocation,String erpFactoryName,String charge)
	{
			
		MaterialShipRequestDetailKey key = new MaterialShipRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);
		key.setShipRequestDetailName(shipRequestDetailName);
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("materialSpecName", materialSpecName);
		bindMap.put("grade", grade);
		bindMap.put("domesticExport", domesticExport);
		bindMap.put("requestQuantity", Double.parseDouble(requestQuantity));
		bindMap.put("assignedQuantity", Double.parseDouble(assignedQuantity));
		
		bindMap.put("charge", charge);
		bindMap.put("erpLocation", erpLocation);
		bindMap.put("erpFactoryName", erpFactoryName);
		
		
//		SetUserColumnsInfo setEventInfo = new SetUserColumnsInfo();
//		setEventInfo.setUserColumns(bindMap);
		NonPrimaryKeyValueInfo nonPrimaryKeyValueInfo = new NonPrimaryKeyValueInfo();
		nonPrimaryKeyValueInfo.setUserColumns(bindMap);
		
		eventInfo.setEventName("Modify");
						
		MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().setEvent(key, nonPrimaryKeyValueInfo);
		
	}
	
	public void modifyForB17(EventInfo eventInfo,String siteName,String shipRequestName,String shipRequestDetailName,String materialSpecName,String grade,
			String domesticExport,String requestQuantity,String assignedQuantity,String erpLocation,String erpFactoryName,String charge,String remark)
	{
			
		MaterialShipRequestDetailKey key = new MaterialShipRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);
		key.setShipRequestDetailName(shipRequestDetailName);
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("materialSpecName", materialSpecName);
		bindMap.put("grade", grade);
		bindMap.put("domesticExport", domesticExport);
		bindMap.put("requestQuantity", Double.parseDouble(requestQuantity));
		bindMap.put("assignedQuantity", Double.parseDouble(assignedQuantity));
		
		bindMap.put("charge", charge);
		bindMap.put("erpLocation", erpLocation);
		bindMap.put("erpFactoryName", erpFactoryName);
		bindMap.put("remark", remark);
		
		
//		SetUserColumnsInfo setEventInfo = new SetUserColumnsInfo();
//		setEventInfo.setUserColumns(bindMap);
		NonPrimaryKeyValueInfo nonPrimaryKeyValueInfo = new NonPrimaryKeyValueInfo();
		nonPrimaryKeyValueInfo.setUserColumns(bindMap);
		
		eventInfo.setEventName("Modify");
						
		MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().setEvent(key, nonPrimaryKeyValueInfo);
		
	}
	public void BMDTmodify(EventInfo eventInfo,String siteName,String shipRequestName,String shipRequestDetailName,String materialSpecName,
			String requestQuantity, String erpLocation,String erpFactoryName,String charge,String erpReceiveLocation)
	{
			
		MaterialShipRequestDetailKey key = new MaterialShipRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);
		key.setShipRequestDetailName(shipRequestDetailName);
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("materialSpecName", materialSpecName);
		bindMap.put("requestQuantity", Double.parseDouble(requestQuantity));

		
		bindMap.put("charge",charge);
		bindMap.put("erpLocation",erpLocation);
		bindMap.put("erpFactoryName",erpFactoryName);
		bindMap.put("erpReceiveLocation",erpReceiveLocation);
		
//		SetUserColumnsInfo setEventInfo = new SetUserColumnsInfo();
//		setEventInfo.setUserColumns(bindMap);
		NonPrimaryKeyValueInfo nonPrimaryKeyValueInfo = new NonPrimaryKeyValueInfo();
		nonPrimaryKeyValueInfo.setUserColumns(bindMap);
		
		eventInfo.setEventName("Modify");
						
		MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().setEvent(key, nonPrimaryKeyValueInfo);
		
	}
	public void TM1modify(EventInfo eventInfo,String siteName,String shipRequestName,String shipRequestDetailName,String erpReceiveLocation)
	{
			
		MaterialShipRequestDetailKey key = new MaterialShipRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);
		key.setShipRequestDetailName(shipRequestDetailName);
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("erpReceiveLocation",erpReceiveLocation);
		
//		SetUserColumnsInfo setEventInfo = new SetUserColumnsInfo();
//		setEventInfo.setUserColumns(bindMap);
		NonPrimaryKeyValueInfo nonPrimaryKeyValueInfo = new NonPrimaryKeyValueInfo();
		nonPrimaryKeyValueInfo.setUserColumns(bindMap);
		
		eventInfo.setEventName("Modify");
						
		MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().setEvent(key, nonPrimaryKeyValueInfo);
		
	}
	public void remove(EventInfo eventInfo,String siteName,String shipRequestName,String shipRequestDetailName)
	{
			
		MaterialShipRequestDetailKey key = new MaterialShipRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);
		key.setShipRequestDetailName(shipRequestDetailName);
		
		eventInfo.setEventName("Remove");
								
		MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().remove(key);
		
	}	
	
}