package cn.net.cpzslibs.prot.handset;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.SocketTimeoutException;

import cn.net.cpzslibs.prot.ProtBase;
import cn.net.cpzslibs.prot.SocketCreate;

public class Prot25OptTray extends ProtBase {

	private final short iTaskCode25 = 25;
	private final short iOptOut = 1;

	/**
	 * 托盘出库 <br/>
	 * 2015-5-29上午11:09:57
	 * 
	 * @param create
	 * @param iTrayId
	 *            托盘ID
	 * @param iOptAction
	 *            操作类型<br/>
	 *            1-出库
	 * @param boxs
	 *            出库的箱IDs
	 * @param iOptAddr
	 *            操作地区
	 * @param iOptPerson
	 *            操作人
	 * @param optMemoStr
	 *            操作说明
	 * @param iSaleArea
	 *            销售区域
	 * @throws SocketTimeoutException
	 * @throws IOException
	 */
	public void dealOptOutTray(SocketCreate create, long iTrayId, long[] boxs,
                               int iOptAddr, int iOptPerson, String optMemoStr, int iSaleArea)
			throws SocketTimeoutException, IOException {
		dealProt(create, iTaskCode25, iTrayId, iOptOut, boxs, iOptAddr,
				iOptPerson, optMemoStr, iSaleArea);
	}

	public void dealOptOutTray(SocketCreate create, long iTrayId, long[] boxs,
                               String iOptAddr, String iOptPerson, String optMemoStr,
                               String iSaleArea) throws SocketTimeoutException, IOException {
		this.dealProt(create, iTaskCode25, iTrayId, iOptOut, boxs,
				Integer.valueOf(iOptAddr), Integer.valueOf(iOptPerson),
				optMemoStr, Integer.valueOf(iSaleArea));
	}

	private void dealProt(SocketCreate create, short iTaskCode, long iTrayId,
                          short iOptAction, long[] boxs, int iOptAddr, int iOptPerson,
                          String optMemoStr, int iSaleArea) throws SocketTimeoutException,
			IOException {
		this.iSendTaskIndex = create.getiTaskIndex();
		recProt(create.getDos(), create.getDis(), iTaskCode, iTrayId,
				iOptAction, boxs, iOptAddr, iOptPerson, optMemoStr, iSaleArea);
	}

	/**
	 * 
	 * <br/>
	 * 2015-5-29上午11:03:39
	 * 
	 * @param dos
	 * @param iTaskCode
	 * @param iTrayId
	 *            托盘ID
	 * @param iOptAction
	 *            操作类型<br/>
	 *            1-出库
	 * @param boxs
	 *            箱IDs
	 * @param iOptAddr
	 *            操作地区
	 * @param iOptPerson
	 *            操作人
	 * @param optMemoStr
	 *            操作说明
	 * @param iSaleArea
	 *            销售区域
	 * @throws SocketTimeoutException
	 * @throws IOException
	 */
	private void recProt(DataOutputStream dos, DataInputStream dis,
			short iTaskCode, long iTrayId, short iOptAction, long[] boxs,
			int iOptAddr, int iOptPerson, String optMemoStr, int iSaleArea)
			throws SocketTimeoutException, IOException {
		sendProt(dos, iTaskCode, iTrayId, iOptAction, boxs, iOptAddr,
				iOptPerson, optMemoStr, iSaleArea);
		recHeader(dis);
		recFFFF(dis, iTaskCode);
	}

	/**
	 * 
	 * <br/>
	 * 2015-5-29上午11:03:39
	 * 
	 * @param dos
	 * @param iTaskCode
	 * @param iTrayId
	 *            托盘ID
	 * @param iOptAction
	 *            操作类型<br/>
	 *            1-出库
	 * @param boxs
	 *            箱IDs
	 * @param iOptAddr
	 *            操作地区
	 * @param iOptPerson
	 *            操作人
	 * @param optMemoStr
	 *            操作说明
	 * @param iSaleArea
	 *            销售区域
	 * @throws SocketTimeoutException
	 * @throws IOException
	 */
	private void sendProt(DataOutputStream dos, short iTaskCode, long iTrayId,
			short iOptAction, long[] boxs, int iOptAddr, int iOptPerson,
			String optMemoStr, int iSaleArea) throws SocketTimeoutException,
			IOException {
		byte[] iSendOptMemoStr = optMemoStr.getBytes();
		short iOptMemSize = (short) iSendOptMemoStr.length;
		/* 箱数量; 0 托盘下所有箱; Num指定箱的数量 */
		short boxsLength = (short) boxs.length;
		short iBodySize = (short) (countBodySize(iTrayId, iOptAction,
				boxsLength, iOptAddr, iOptPerson, iOptMemSize, iSaleArea)
				+ boxsLength * 8 + iOptMemSize);
		sendHeader(dos, iTaskCode, iBodySize);
		sendiLabel(dos, iTrayId);// 托盘ID
		dos.writeShort(iOptAction);// 操作类型1-出库
		dos.writeShort(boxsLength);// 箱数量
		for (int i = 0; i < boxsLength; i++) {
			sendiLabel(dos, boxs[i]);// 箱ID1
		}
		dos.writeInt(iOptAddr);// 操作地区
		dos.writeInt(iOptPerson);// 操作人
		dos.writeShort(iOptMemSize);// 操作说明长度
		dos.write(iSendOptMemoStr);// 操作说明
		sendiAreaCode(dos, iSaleArea);// 销售区域
		sendCheckCode(dos);
	}
}
