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

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.exception.IDMFrameDBErrorSignal;
import com.cim.idm.framework.orm.service.CommonServiceDAO;
import com.cim.idm.framework.util.xml.JdomUtils;
import com.cim.idm.mwmsextend.checkplan.CheckPlanServiceProxy;
import com.cim.idm.mwmsextend.checkplan.management.data.CheckPlan;
import com.cim.idm.mwmsextend.checkplan.management.data.CheckPlanKey;
import com.cim.idm.mwmsextend.checkplan.management.info.CreateInfo;
import com.cim.idm.mwmsextend.checkplan.management.info.SetEventInfo;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking;

public class CheckPlanSolutionServiceImpl extends CommonServiceDAO<CheckPlanKey,CheckPlan>{
	
	public void OICCreateCheckPlan(EventInfo eventInfo,Document doc)throws Exception{
		Element root = doc.getDocument().getRootElement();
		Element CheckPlanListElement = root.getChild("Body");
		
		try {
			
			SetEventInfo setEventInfo = new SetEventInfo();
			List<CheckPlanKey> keyList = new ArrayList<CheckPlanKey>();
			List<CreateInfo> createInfoList = new ArrayList<CreateInfo>();
			if (CheckPlanListElement != null)
			{
				for (Iterator<?> iterator = CheckPlanListElement.getChildren().iterator();iterator.hasNext();)
				{
					Element panelE = (Element) iterator.next();
					CreateInfo createInfo = new CreateInfo();
					
					Map<String, Object> bindMap = new HashMap<String, Object>();
					
					createInfo.setPlanDate(panelE.getChild("PLANDATE").getText());
					createInfo.setCheckPlanName(panelE.getChild("CHECKPLANNAME").getText());
					createInfo.setSiteName(panelE.getChild("SITENAME").getText());
					createInfo.setMaterialPackingName(panelE.getChild("MATERIALPACKINGNAME").getText());
					
					bindMap.put("sysLocation", panelE.getChild("SYSLOCATION").getText());
					bindMap.put("planType", panelE.getChild("PLANTYPE").getText());
					bindMap.put("planTypeName", panelE.getChild("PLANTYPENAME").getText());
					bindMap.put("planCondition", panelE.getChild("PLANCONDITION").getText());
					/*
					 * bindMap.put("checkMaterialType",
					 * panelE.getChild("CHECKMATERIALTYPE").getText());
					 * bindMap.put("checkPackingType",
					 * panelE.getChild("CHECKPACKINGTYPE").getText());
					 */
					bindMap.put("erpLocation", panelE.getChild("ERPLOCATION").getText());
					bindMap.put("materialQuantity", Double.parseDouble(panelE.getChild("MATERIALQUANTITY").getText()));
					bindMap.put("materialSpecName", panelE.getChild("MATERIALSPECNAME").getText());					
					bindMap.put("boxState", panelE.getChild("BOXSTATE").getText());
					bindMap.put("moveFlag", panelE.getChild("MOVEFLAG").getText());
					bindMap.put("checkPlanState", panelE.getChild("CHECKPLANSTATE").getText());
					bindMap.put("lastAction", panelE.getChild("LASTACTION").getText());
					bindMap.put("lastEventName", "OICCreateCheckPlan");
					bindMap.put("lastEventUser", eventInfo.getEventUser());
					bindMap.put("lastEventComment", eventInfo.getEventComment());
					createInfo.setUserColumns(bindMap);
					
					createInfoList.add(createInfo);
				}	
			}
			CheckPlanServiceProxy.getCheckPlanService().create(eventInfo, createInfoList);
		}
		catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		}
	}
	public void OICCheckPlanComplete (String planDate, String checkPlanName){
		String sqlPlan =" update CHECKPLAN set CHECKPLANSTATE = 'Completed' where PLANDATE=:planDate and CHECKPLANNAME=:checkPlanName";
		String sqlRecord =" update CHECKRECORD set CHECKSTATE = 'Completed' where PLANDATE=:planDate and CHECKPLANNAME=:checkPlanName";
		Map<String, String> bindMap = new HashMap<String, String>();
		bindMap.put("planDate", planDate);
		bindMap.put("checkPlanName", checkPlanName);	
		IDMFrameServiceProxy.getSqlTemplate().update(sqlPlan, bindMap);
		IDMFrameServiceProxy.getSqlTemplate().update(sqlRecord, bindMap);
	}
	
	public Document OICSearchWipByCondition(Document doc,String siteName,String wipTime,String conditionName,String condition)throws Exception
	{
		try{
			
			StringBuffer sql = new StringBuffer();
			sql.append("select * from WIPHISTORY where");
			Map<String, String> bindMap = new HashMap<String, String>();
			sql.append(" SITENAME =" +"'" + siteName +"'" + " and PACKINGSTATE='Released' and STOCKSTATE='Stocked' and ");
            sql.append(" WIPTIME like " +"'" + wipTime + "%'" + " and ");
			sql.append(" "+conditionName+"");
			if (conditionName.equalsIgnoreCase("ERPLOCATION")  || conditionName.equalsIgnoreCase("MATERIALSPECNAME")){
				
				sql.append(" in "+condition);			
			}
			else if (conditionName.equalsIgnoreCase("LOCATIONNAME")){
				sql.append(" between "+condition);
			}
			List<Map<String, Object>> wiplist = 
		 IDMFrameServiceProxy.getSqlTemplate().queryForList(sql.toString(), bindMap);
			doc.getRootElement().removeChild("Body");
			JdomUtils.addElement(doc.getRootElement(), "Body", null);

			for(Map<String, Object> wip : wiplist)
			{
				Element  wipHistory= JdomUtils.createElement("WIPHISTORY");
				for (String key : wip.keySet()){
					JdomUtils.addElement(wipHistory, key.toUpperCase(),(wip.get(key)==null? "":wip.get(key).toString()));					
				}	
				JdomUtils.appendChildElement(doc.getRootElement().getChild("Body"),wipHistory);
			}
			return null;
		}
		catch(Exception e){e.printStackTrace();
		throw e;}
	}
	
//	public Document OICSearchWip (Document doc)throws Exception 
//	{
//		
//		Element root = doc.getDocument().getRootElement();
//		Element body = root.getChild("Body");
//		String conditionName ="";
//		String condition ="";
//		String wipTime ="";
//	  try {
//		  wipTime = body.getChildText("TODATE");
//		  conditionName=body.getChildText("AREANAME");
//		  condition=body.getChildText("ETCSTATE");
//		  OICSearchWipByCondition(wipTime,conditionName,condition);
//	     return doc;
//	    }
//	  catch(Exception e){e.printStackTrace();
//		throw e;}
//	}
}
