package com.cim.idm.wmsextend.generic.master;

import com.cim.idm.framework.util.sys.SystemPropHelper;
import com.cim.idm.wmsextend.generic.util.ObjectUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

@Service("ntConstantMap")
public class ConstantMap extends com.cim.idm.wmspackage.generic.master.ConstantMap
		implements ApplicationContextAware, InitializingBean
{
    public static final String FLAG_N = "N";
	public static final String FLAG_Y = "Y";
	public static final CharSequence RECORD_MESSAGE_LOG_BY_SERVER = "RECORD_MESSAGE_LOG_BY_SERVER";
	public static final String MESSAGECOUNT =  "MESSAGECOUNT";
	private static Log				log								= LogFactory.getLog(ConstantMap.class);

	public static final String ConstantDef = "select constantName, constantValue" + SystemPropHelper.CR +
			" from ConstantDef where cacheFlag = 'Y' ORDER BY constantName" ;


	public String					Spec_CheckedOut					= "CheckedOut";
	public String					Spec_CheckedIn					= "CheckedIn";
	public String					Spec_NotActive					= "NotActive";
	public String					Spec_Active						= "Active";

	// StateModel
	public String					Stml_Resource					= "ResourceState";
	public String					Stml_Communication				= "MachineCommunicationState";
	public String					Stml_PortAccessMode				= "PortAccessMode";
	public String					Stml_PortTransferState			= "PortTransferState";
	public String					Stml_LotState					= "LotState";
	public String					Stml_LotProcessState			= "LotProcessState";
	public String					Stml_LotHoldState				= "LotHoldState";
	public String					Stml_ProductState				= "ProductState";
	public String					Stml_ProductProcessState		= "ProductProcessState";
	public String					Stml_ProductHoldState			= "ProductHoldState";
	public String					Stml_DurableState				= "DurableState";
	public String					Stml_DurableCleanState			= "DurableCleanState";
	public String					Stml_ConsumableState			= "ConsumableState";

	// 2. tracking
	// 2.1. Lot
	public String					Lot_Created						= "Created";
	public String					Lot_Released					= "Released";
	public String					Lot_Completed					= "Completed";
	public String					Lot_Shipped						= "Shipped";
	public String					Lot_Scrapped					= "Scrapped";
	public String					Lot_Emptied						= "Emptied";
	public String					Lot_InitialState				= "Created";
	public String					Lot_WaitingToLogin				= "WaitingToLogin";
	public String					Lot_LoggedIn					= "LoggedIn";
	public String					Lot_LoggedOut					= "LoggedOut";
	public String					Lot_InitialProcessState			= "LoggedIn";
	public String					Lot_NotOnHold					= "NotOnHold";
	public String					Lot_OnHold						= "OnHold";
	public String					Lot_InitialHoldState			= "NotOnHold";
	public String					Lot_InRework					= "InRework";
	public String					Lot_NotInRework					= "NotInRework";
	public String					Lot_InitialReworkState			= "NotInRework";

	// //ksk ///////////// => ʹ������Lot_Rework
	// public String Lot_InRework = "InRework";

	// 2.2. Product
	public String					Prod_NotAllocated				= "NotAllocated";
	public String					Prod_Allocated					= "Allocated";
	public String					Prod_InProduction				= "InProduction";
	public String					Prod_Completed					= "Completed";
	public String					Prod_Shipped					= "Shipped";
	public String					Prod_Scrapped					= "Scrapped";
	public String					Prod_Consumed					= "Consumed";
	public String					Prod_InitialState				= "Allocated";
	public String					Prod_Traveling					= "Traveling";
	public String					Prod_Idle						= "Idle";
	public String					Prod_Processing					= "Processing";
	public String					Prod_InitialProcessState		= "Idle";
	public String					Prod_NotOnHold					= "NotOnHold";
	public String					Prod_OnHold						= "OnHold";
	public String					Prod_InitialHoldState			= "NotOnHold";
	public String					Prod_InRework					= "InRework";
	public String					Prod_NotInRework				= "NotInRework";
	public String					Prod_InitialReworkState			= "NotInRework";

	// 2.3. Durable
	public String					Dur_NotAvailable				= "NotAvailable";
	public String					Dur_Available					= "Available";
	public String					Dur_InUse						= "InUse";
	public String					Dur_Scrapped					= "Scrapped";
	public String					Dur_Clean						= "Clean";
	public String					Dur_Dirty						= "Dirty";
	public String					Dur_Repairing					= "Repairing";
	public String					Dur_InitialState				= "Available";
	public String					Dur_InitialCleanState			= "Clean";

	// 2.4. Consumable
	public String					Cons_NotAvailable				= "NotAvailable";
	public String					Cons_Available					= "Available";
	public String					Cons_InitialState				= "Available";

	// 2.5. MaterialGroup
	public String					Mgp_Lot							= "Lot";
	public String					Mgp_Product						= "Product";

	// 2.6. ProcessGroup
	public String					ProcessGroup_OnHold				= "OnHold";
	public String					ProcessGroup_NotOnHold			= "NotOnHold";
	public String					ProcessGroup_Stocked			= "Stocked";
	public String					ProcessGroup_PreStocked			= "PreStocked";
	public String					ProcessGroup_Shipped			= "Shipped";
	public String					ProcessGroup_OnPause			= "OnPause";
	public String					ProcessGroup_NotOnPause			= "NotOnPause";

	// 3. ProcessFlow
	// 3.1 Node Type
	public String					Node_Start						= "Start";
	public String					Node_End						= "End";
	public String					Node_ProcessOperation			= "ProcessOperation";
	public String					Node_ProcessFlow				= "ProcessFlow";
	public String					Node_ConditionalDivergence		= "ConditionalDivergence";
	public String					Node_ConditionalConvergence		= "ConditionalConvergence";
	public String					Node_ReworkDivergence			= "ReworkDivergence";
	public String					Node_ReworkConvergence			= "ReworkConvergence";

	// 3.2 Arc Type
	public String					Arc_Normal						= "Normal";
	public String					Arc_Conditional					= "Conditional";
	public String					Arc_Rework						= "Rework";

	// 4. DataCollection
	public String					Dcs_Item						= "N";

	// 5. ProductRequest
	public String					Prq_Created						= "Created";
	public String					Prq_Released					= "Released";
	public String					Prq_Completed					= "Completed";
	public String					Prq_NotOnHold					= "NotOnHold";
	public String					Prq_OnHold						= "OnHold";

	public String					Spar_Available					= "Available";
	public String					Spar_NotAvailable				= "NotAvailable";
	public String					Spar_InitialState				= "Available";

	public String					PMJob_Queued					= "Queued";
	public String					PMJob_Paused					= "Paused";
	public String					PMJob_Executing					= "Executing";
	public String					PMJob_Aborted					= "Aborted";
	public String					PMJob_Canceled					= "Canceled";
	public String					PMJob_Completed					= "Completed";
	public String					PMJob_InitialState				= "Queued";
	public String					PMJob_Stopped					= "Stopped";

	// 6. Machine
	// Resource State
	public String					Rsc_OutOfService				= "OutOfService";						// Default
	public String					Rsc_InService					= "InService";

	public String					Rsc_InitialResourceState		= "InService";

	// Machine Type
	public String					Mac_ProductionMachine			= "ProductionMachine";
	public String					Mac_StorageMachine				= "StorageMachine";
	public String					Mac_TransportMachine			= "TransportMachine";
	public String					Mac_ProductionResource			= "ProductionResource";
	public String					Mac_StorageResource				= "StorageResource";
	public String					Mac_TransportResource			= "TransportResource";
	public String					Mac_Port						= "Port";

	// E10 State
	public String					Mac_StandBy						= "StandBy";
	public String					Mac_Productive					= "Productive";
	public String					Mac_Engineering					= "Engineering";
	public String					Mac_ScheduledDown				= "ScheduledDown";
	public String					Mac_UnscheduledDown				= "UnScheduledDown";
	public String					Mac_NonScheduled				= "NonScheduled";

	public String					Mac_InitialE10State				= "StandBy";

	// OnLine State
	public String					Mac_OffLine						= "OffLine";
	public String					Mac_OnLineLocal					= "OnLineLocal";
	public String					Mac_OnLineRemote				= "OnLineRemote";

	public String					Mac_OnHold						= "OnHold";
	public String					Mac_NotOnHold					= "NotOnHold";

	public String					Mac_InitialCommunicationState	= "OffLine";

	// Port Access Mode
	public String					Port_Manual						= "Manual";
	public String					Port_Auto						= "Auto";

	public String					Port_InitialAccessMode			= "Auto";

	// Port Transfer State
	public String					Port_ReadyToLoad				= "ReadyToLoad";
	public String					Port_ReservedToLoad				= "ReservedToLoad";
	public String					Port_ReadyToProcess				= "ReadyToProcess";
	public String					Port_Processing					= "Processing";
	public String					Port_ReadyToUnload				= "ReadyToUnLoad";
	public String					Port_ReservedToUnload			= "ReservedToUnLoad";

	public String					Port_InitialTransferState		= "ReadyToLoad";

	// Location State
	public String					LocationState_Empty				= "Empty";
	public String					LocationState_Full				= "Full";

	/*
	 * Maximum
	 */
	public long						Max_Priority					= 1000;
	public long						Max_ConsumableQuantity			= 1000000;
	public double					Max_DurableTimeUsed				= 1000000;
	public double					Max_DurableDurationUsed			= 1000000;
	public long						Max_CarrierCapacity				= 1000;
	public long						Max_ProductPosition				= 1000;
	public double					Max_ProductQuantity				= 1000;
	public double					Max_SubProductUnitQuantity		= 1000;
	public double					Max_SubProductQuantity			= 1000;
	public long						Max_MaterialQuantity;

	// SubjectName
	public String                   Subject_PEX						= "PEX";
	public String					Subject_CNX						= "CNX";
	public String					Subject_MCS						= "MCS";
	public String					Subject_TEX						= "TEX";

	// StockIn & Module Interface
	public String					StockInInterfaceFlag			= "Y";
	public String					CancelStockInInterfaceFlag		= "N";
	public String					StockInEvent					= "StockIn";

	// Ship Reqeust & Invoice
	public String					ShipRequestStateConfirmed		= "Confirmed";

	// Step
	public String                   STEP_F1000               = "F1000";
	public String                   STEP_F1500               = "F1500";	//Step for ReturningReceive Boxing
	public String                   STEP_F1800               = "F1800";	//Step for ReturningReceive Palletizing
	public String                   STEP_F2000               = "F2000";

	// Panel Type
	public String					Panel_Type_Returning     		= "RG";

	// Material Type
	public String                   TYPE_LOT                        = "Lot";
	public String                   TYPE_PANEL                      = "Panel";
	public String                   TYPE_BOX                        = "Box";
	public String                   TYPE_PALLET                     = "Pallet";
	public String                   TYPE_PROCESSGROUP               = "ProcessGroup";

	// SELECT DISTINCT PANELTYPE FROM PRODUCTSPEC
	public String					PanelType_Normal				= "Normal";
	public String					PanelType_TVSet					= "TV Set";
	public String					PanelType_Open_Cell				= "Open Cell";

	//Site Name
	public String					SiteName_B20					= "B20";
	//PackingGroupType
	public String					PackingGroupType_Pallet			= "Pallet";
	public String					PackingGroupType_Box			= "Box";

	//StorageType
	public String					StorageUseType_Storage			= "Storage";
	public String					StorageUseType_Port				= "Port";
	public String					StorageUseType_Transport		= "Transport";
	public String					StorageOperationType_Manual		= "Manual";
	public String					StorageOperationType_Auto		= "Auto";

	//OQA Info
	public String					OQA_Result_OK					= "OK";
	public String					OQA_Result_NG					= "NG";
	public String					OQA_State_Pre					= "PREOQA";
	public String					OQA_State_Continue				= "CONTINUE";
	public String					OQA_State_End				    = "END";
	public String					OQA_State_NO                    ="NO";
	//B5&TM1 INT Info
	public String					B5_MaterialSpec					= "INT_WMS06";
	public String					B5_POInfo						= "INT_WMS09";
	public String					B5_ManualCount					= "INT_WMS19";
	public String					B5_MakeUndo						= "INT_WMS22";
	public String					B5_ShipRequestCount				= "INT_WMS15";
	public String					New_ShipRequestWMS15Count		= "14633";
	public String					B5_POOem						= "INT_WMS17";
	public String					B5_ShipConfirmCount				= "INT_WMS05";
	public String					B5_ScrapInfo					= "INT_WMS25";
	public String					B5_POCount						= "INT_WMS13";
	public String					B5_ShipRequestInfo				= "INT_WMS10";
	public String					B5_CustomInfo					= "INT_WMS03";
	public String					B5_DeliveryInfo					= "INT_WMS04";
	public String					B5_WorkOrderInfo				= "INT_WMS01";
	public String					B5_WorkOrderCount				= "INT_WMS02";
	public String					B5_OQA							= "INT_WMS27";
	public String					InboundDeliveryInfo				= "INT_PI045";
	public String					InboundDeliveryCount			= "INT_PI046";
	//B10&B7 INT Info
	public String					New_MaterialSpec				= "INT_PI050";
	public String					New_POInfo						= "INT_PI051";
	public String					New_ManualCount					= "14980";  // INT_PI056
	public String					New_MakeUndo					= "14517";  //"INT_PI058";
	public String					New_ShipRequestCount			= "14633";  // INT_PI054
	//public String					New_POOem						= "INT_PI055";
	public String New_POOem = "14073";// 组件外发接口由INT_PI055修改为14073    2024.09.26
	public String					New_ShipConfirmCount			= "14727";
	public String					New_ScrapInfo					= "INT_PI059";
	public String					New_POCount						= "14141";  // 原INT_PI053
	public String					New_ShipRequestInfo				= "INT_PI052";
	public String					New_CustomInfo					= "14217"; // INT_PI047
	public String					New_DeliveryInfo				= "INT_PI048";
	public String					New_WorkOrderInfo				= "INT_PI061";
	public String					New_WorkOrderCount				= "INT_PI062";
	public String					New_BMDTOEMPOInfo				= "INT_PI064";
	public String					New_BMDTOEMReserveInfo			= "INT_PI065";
	public String					New_OQAResult					= "INT_PI060";

	//GERP
	public String					New_ScrapManualStockOutCount	= "14980";
	public String					New_POPostingCount				= "14789";


	//PMS
	public String					New_PartsStockInOutReceived		= "Parts_MWMS_Received";
	public String					New_PartsStockInOutRequest		= "Parts_MWMS_Request";


	public String                   BMDT_ManualStockIn_KOSTL        = "2000PA0500";
	public CharSequence Factory_All = "ALL";
	private Map<String, String>		constantDefsMap					= new HashMap<String, String>();

	private ApplicationContext		ac;

	public ConstantMap()
	{

	}


	/*
	 * Name : getEventName
	 * Desc : This function is getEventName
	 * Author : AIM Systems, Inc
	 * Date : 2011.01.18
	 */
	public String getEventName(String eventNameConst){
		try {
			Field filed = ConstantMap.class.getDeclaredField(eventNameConst);
			filed.setAccessible(true);
			return filed.get(this).toString();
		} catch (SecurityException e) {
			return "";
		} catch (NoSuchFieldException e) {
			return "";
		} catch (IllegalArgumentException e) {
			return "";
		} catch (IllegalAccessException e) {
			return "";
		}

	}

	/*
	 * Name : getConstantDefsMap
	 * Desc : This function is getConstantDefsMap
	 * Author : AIM Systems, Inc
	 * Date : 2011.01.18
	 */
	public Map<String, String> getConstantDefsMap()
	{
		return constantDefsMap;
	}

	/*
	 * Name : setApplicationContext
	 * Desc : This function is setApplicationContext
	 * Author : AIM Systems, Inc
	 * Date : 2011.01.18
	 */
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException
	{
		this.ac = arg0;
	}

	/*
	 * Name : afterPropertiesSet
	 * Desc : This function is afterPropertiesSet
	 * Author : AIM Systems, Inc
	 * Date : 2011.01.18
	 */
	public void afterPropertiesSet()
			throws Exception
	{
		load();
	}

	/*
	 * Name : load
	 * Desc : This function is load
	 * Author : AIM Systems, Inc
	 * Date : 2011.01.18
	 */
	@Override
	public void load()
	{
		super.load();
		constantDefsMap.clear();

//		constantDefsMap =  com.cim.idm.wmspackage.generic.GenericServiceProxy.getConstantMap().getConstantDefsMap();

		log.info("ConstantDef load resultList :" + constantDefsMap.size());

		Field[] fields = ConstantMap.class.getDeclaredFields();
		Field[] superFidles = this.getClass().getSuperclass().getDeclaredFields();
		for ( Field field : fields )
		{
			setValue(constantDefsMap, field);
		}

		for ( Field field : superFidles )
		{
			setValue(constantDefsMap, field);
		}

	}

	/*
	 * Name : setValue
	 * Desc : This function is setValue
	 * Author : AIM Systems, Inc
	 * Date : 2011.01.18
	 */
	private void setValue(Map<String, String> constantDefs, Field field)
	{
		String constantValue = constantDefs.get(field.getName());

		if ( constantValue != null )
		{
			try
			{
				ObjectUtil.copyFieldValue(field, this, constantValue);
			}
			catch ( IllegalArgumentException e )
			{
				log.error(e, e);
			}
			catch ( Exception e )
			{
				log.error(e, e);
			}
		}
	}
	@Override
	public synchronized void reload(){
		this.getConstantDefsMap().clear();
		this.load();
		log.info("Reload ConstantMap successfully.");
	}
}
