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

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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.wmspackage.storage.StorageServiceProxy;
import com.cim.idm.wmspackage.storage.management.data.Storage;
import com.cim.idm.wmspackage.storage.management.data.StorageKey;
import com.cim.idm.wmspackage.storage.management.info.MakeEmptyInfo;

public class StorageServiceImpl extends CommonServiceDAO<StorageKey, Storage> {

	private static Log log = LogFactory.getLog(StorageServiceImpl.class);
	
	public void changeUserState(EventInfo eventInfo,String siteName,String storageName,String useState){
		StorageKey key = new StorageKey();
		key.setSiteName(siteName);
		key.setStorageName(storageName);
		MakeEmptyInfo makeEmptyInfo = new MakeEmptyInfo();
		
        Map<String, Object> bindMap = new HashMap<String, Object>();
		
		bindMap.put("useState", useState);
		makeEmptyInfo.setUserColumns(bindMap);
		StorageServiceProxy.getStorageService().makeEmpty(key, eventInfo, makeEmptyInfo);
		
	}
	
	public void changeUserStateByBoxList(EventInfo eventInfo,String siteName,String storageName){
		
		if(storageName.isEmpty())
		{
			return;
		}
		
		String sql = 	" SELECT COUNT( MATERIALPACKINGNAME) " +
						"  FROM MATERIALPACKING " +
						"   WHERE LOCATIONNAME=? AND STOCKSTATE='Stocked'" ;
		
    	Object[] args = {storageName};

    	String result = String.valueOf(IDMFrameServiceProxy.getSqlTemplate().queryForInt(sql, args));
    	StorageKey key = new StorageKey();
    	key.setSiteName(siteName);
    	key.setStorageName(storageName);
    	Storage dataInfo=selectByKey(key);
    	
//		key.setSiteName(siteName);
//		key.setStorageName(storageName);
    	
		//Map<String, Object> bindMap = new HashMap<String, Object>();
    	if ("0".equals(result)){
    		//bindMap.put("useState", "Empty");
    		dataInfo.setUseState("Empty");
		} else {
			//bindMap.put("useState", "NotFull");
			dataInfo.setUseState("NotFull");
		}
		//SetEventInfo setEventInfo = new SetEventInfo();

		//bindMap.put("useState", useState);
		//setEventInfo.setUserColumns(bindMap);
    	StorageServiceProxy.getStorageService().update(dataInfo);
		//StorageServiceProxy.getStorageService().setEvent(key, eventInfo, setEventInfo);
		
	}
	public void changeStorageStateByBoxList(String siteName,String storageName)
	{
		//V_开头的库位为虚拟库位  
		if(storageName.isEmpty() || storageName.equalsIgnoreCase("V_") )
		{
			return;
		}
		
	    String sql =" SELECT COUNT( MATERIALPACKINGNAME) " +
					" FROM MATERIALPACKING " +
					" WHERE LOCATIONNAME=? " ;
		
    	Object[] args = {storageName.startsWith("V_")?storageName.substring(2):storageName};

    	String result = String.valueOf(IDMFrameServiceProxy.getSqlTemplate().queryForInt(sql, args));
    	StorageKey key = new StorageKey();
    	key.setSiteName(siteName);
    	key.setStorageName(storageName);
    	Storage dataInfo=selectByKey(key);
    	if ("0".equals(result)){
    		dataInfo.setUseState("Empty");
		} else {
			dataInfo.setUseState("NotFull");
		}
    	StorageServiceProxy.getStorageService().update(dataInfo);
		
	}
	
}
