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

import java.util.ArrayList;
import java.util.List;

import com.cim.idm.framework.util.time.TimeStampUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cim.idm.mwmsextend.transport.TransportServiceProxy;
import com.cim.idm.mwmsextend.transport.management.data.TransportCommand;
import com.cim.idm.mwmsextend.transport.management.data.TransportCommandDetail;
import com.cim.idm.mwmsextend.transport.management.data.TransportCommandDetailKey;
import com.cim.idm.mwmsextend.transport.management.data.TransportCommandKey;
import com.cim.idm.framework.exception.ext.NotFoundSignal;
import com.cim.idm.framework.util.time.TimeUtils;


public class TransportJobServiceUtil {
	
	private static Log log = LogFactory.getLog(TransportJobServiceUtil.class);
	
	
	public static String generateTransportCommandID(){
		return "TRANSCMD-"+ TimeStampUtil.getCurrentEventTimeKey();
	}
	
	public TransportCommand getTransportCommand(String transportCommandID){
		
		TransportCommandKey key = new TransportCommandKey();
		key.setTransportCommandID(transportCommandID);
		TransportCommand transportCommand = null;

		transportCommand = TransportServiceProxy.getTransportCommandService().selectByKey(key);
		
		return transportCommand;
		
	}
	/**
	 * 
	 * @param transportCommandID
	 * @return A list of TransportCommandDetail
	 */
	public List<TransportCommandDetail> getTransportCommandDetails(String transportCommandID){
		String condition = "transportCommandID = ?";
		Object[] bindSet = {transportCommandID};
		
		List<TransportCommandDetail> details = TransportServiceProxy.getTransportCommandDetailService().select(condition, bindSet, TransportCommandDetail.class);
		return details;
	}
	
	public List<TransportCommandDetail> getNotCompletedTransportCommandDetails(String transportCommandID){
		
		String condition = "transportCommandID = ? and state in ('CREATED','STARTED')";
		Object[] bindSet = {transportCommandID};
		
		List<TransportCommandDetail> details = null;
		
		try{
			details = TransportServiceProxy.getTransportCommandDetailService().select(condition, bindSet, TransportCommandDetail.class);
		}catch(NotFoundSignal e){
			
		}
		return details;
		
	}
	
	public List<TransportCommandDetail> getNotEndTransportCommandDetailsByTray(String trayName){
		String condition = "durableName = ? and state in ('CREATED','STARTED','REQUESTED')";
		Object[] bindSet = {trayName};
		
		List<TransportCommandDetail> details = null;
		
		try{
			details = TransportServiceProxy.getTransportCommandDetailService().select(condition, bindSet, TransportCommandDetail.class);
		}catch(NotFoundSignal e){
			
		}
		return details;
	}
	
	/**
	 * 
	 * @param transportCommandID
	 * @param trayName
	 * @return
	 */
	public TransportCommandDetail getTransportCommandDetail(String transportCommandID, String trayName ){
		
		TransportCommandDetailKey key = new TransportCommandDetailKey();
		key.setTransportCommandID(transportCommandID);
		key.setDurableName(trayName);
		TransportCommandDetail transportCommandDetail = null;

		transportCommandDetail = TransportServiceProxy.getTransportCommandDetailService().selectByKey(key);
		
		return transportCommandDetail;
		
	}
	/**
	 * 删除参数 delDestination中的搬送目的地
	 * @param oldDestination 之前的搬送目的地
	 * @param delDestination 需要删除的搬送目的地
	 * @return deletedDestination 删除了需要删除的搬送目的地
	 */
	public String deletedDestination(String oldDestination , String delDestination){
		String[] oldDests = StringUtils.split(oldDestination, ",");
		String[] delDests = StringUtils.split(delDestination, ",");
		
		List<String> deletedDests = new ArrayList<String>();
		for(String port : oldDests){
			boolean isDel = false;
			for(String del : delDests){
				if(del.trim().equals(port.trim())){
					isDel = true;
					break;
				}
			}
			if(!isDel)
				deletedDests.add(port.trim());
		}
		StringBuilder builder = new StringBuilder();
		
		for(String dest :deletedDests){
			builder.append(dest).append(",");
		}
		builder.deleteCharAt(builder.length()-1);
		return builder.toString();
	}
}
