package com.dev.swms.server.telnet.demo;

import java.io.IOException;
import java.util.Date;

import net.wimpi.telnetd.net.Connection;

import com.dev.energy.server.exception.BusinessException;
import com.dev.kangaroo.shell.BreakException;
import com.dev.kangaroo.shell.ContinueException;
import com.dev.kangaroo.shell.Energy4BaseShell;
import com.dev.swms.server.model.base.Item;
import com.dev.swms.server.model.base.Location;
import com.dev.swms.server.model.base.LotInfo;
import com.dev.swms.server.model.base.PackageUnit;
import com.dev.swms.server.model.base.Warehouse;
import com.dev.swms.server.model.receiving.ASN;
import com.dev.swms.server.model.receiving.ASNDetail;
import com.dev.swms.server.web.filter.WarehouseHolder;

public class ASNShell extends Energy4BaseShell {

	@Override
	protected void mainProcess(Connection connection) throws BreakException,
			ContinueException, IOException {
		WarehouseHolder.setWarehouse(getWarehouse());
		getTextField("asnDetail.item");
		hide("asnDetail.item");
		ASNDetail asnDetail = getASNDetail();
		Item item = getItem(asnDetail.getItem().getId());
		//输入文本内容： 如 单号: 12345
		output("asnDetail.item.code", item.getCode());
		output("asnDetail.item.name", item.getName());
		PackageUnit pu =  getPackageUnit(asnDetail.getPackageUnit().getId());
		output("asnDetail.expectedQuantity", asnDetail.getExpectedQuantity());
		output("asnDetail.item.unit", pu.getUnit());
		
		getTextField("location");
		//构造一个数值输入控件，并获取输入的VALUE
		int receiverQuantity = getIntegerField("asnDetail.receivedQuantity");
		String itemStatus = getTextField("asnDetail.item.status");
		
		
		Location l = getLocation();
		confirmReceiving(asnDetail.getId(),asnDetail.getLotInfo(),receiverQuantity,asnDetail.getPackageUnit().getId(),
				new Date(),itemStatus,1L,l.getId());
		setShellMessage("收货成功!");
		forward("asnListShell");
	}
	
	
	public Warehouse getWarehouse(){
		try {
			return invoke("asnManager", "load",Warehouse.class,1L);
		} catch (BusinessException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void confirmReceiving(Long asnDetailId, LotInfo lotInfo, double quantity, 
			Long receivingPackId, Date receiveDate, String qualityCode, Long workerId, Long locationId){
		Object[] rowData = (Object[])getParentValue("rowData");
		try {
			invoke("asnManager", "confirmReceiving",(Long)rowData[0] ,asnDetailId,lotInfo,quantity,
					receivingPackId,receiveDate,qualityCode,workerId,locationId);
		} catch (BusinessException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public Location getLocation() {
		try {
			return invoke("locationManager", "getLocation",get("location"));
		} catch (BusinessException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ASNDetail getASNDetail() {
		Object[] rowData = (Object[])getParentValue("rowData");
		try {
			int type = 0;
			return invoke("asnManager", "getASNDetailByItem",(Long)rowData[0] ,get("asnDetail.item"),type);
		} catch (BusinessException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public PackageUnit getPackageUnit(Long unitId){

		try {
			return invoke("itemManager", "load",PackageUnit.class ,unitId);
		} catch (BusinessException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	
	}
	
	public Item getItem(Long itemId) {
		try {
			return invoke("itemManager", "load",Item.class ,itemId);
		} catch (BusinessException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ASN getWmsASN() throws BusinessException, Exception{
		Object[] rowData = (Object[])getParentValue("rowData");
		/**
		 * 
		 * @param <T>返回类型
		 * @param beanId   对象BeanId
		 * @param methodName 方法名
		 * @param objects  可变数组参数
		 * @return
		 * @throws BusinessException
		 * @throws Exception
		 */
		return invoke("asnManager", "load", ASN.class,(Long)rowData[0]);
	}

	@Override
	protected void setShellMessage(String message) {

	}

	@Override
	protected String getShellMessage() {
		return null;
	}
}
