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 Prot34LabelAddCarNo extends ProtBase {

	private final short iTaskCode34 = 34;

	private final short iSale = 1;// 销售
	private final short iOut = 2;// 出库
	// 为包标签设置车牌时，还没有子标签设置车牌号
	private final int notFindChild = 0xFFB9;
	// 要操作的标签还没有激活
	private final int notBind = 0xFFB8;
	// 包标签做了出库或 子标签不能做销售
	private final int childCanNotSale = 0xFFB7;
	/**
	 * 销售
	 * 
	 * @date 2015-10-30下午3:22:48
	 * @param create
	 * @param iLabel
	 * @param carNo
	 * @param tel
	 * @throws SocketTimeoutException
	 * @throws IOException
	 */
	public void dealProtSale(SocketCreate create, long iLabel, String carNo, String tel) throws SocketTimeoutException,
			IOException {
		if (tel == null || "".equals(tel)) {
			tel = "0";
		}
		sendProt(create.getDos(), iTaskCode34, iLabel, iSale, carNo, Long.valueOf(tel));
		recProt(create.getDis(), iTaskCode34);
	}
	/**
	 * 出库
	 * 
	 * @date 2015-10-30下午3:22:42
	 * @param create
	 * @param iLabel
	 * @param carNo
	 * @throws SocketTimeoutException
	 * @throws IOException
	 */
	public void dealProtOut(SocketCreate create, long iLabel, String carNo) throws SocketTimeoutException, IOException {
		sendProt(create.getDos(), iTaskCode34, iLabel, iOut, carNo, 0);
		recProt(create.getDis(), iTaskCode34);
	}
	/**
	 * 没有发现有相同车牌号的子标签
	 * 
	 * @date 2015-10-29下午3:15:33
	 * @return
	 */
	public boolean isNotFindChild() {
		return getiRecErrcode() == notFindChild;
	}
	/**
	 * 子标签不能做销售
	 * 
	 * @date 2015-10-30下午3:21:50
	 * @return
	 */
	public boolean isChildCanNotSale() {
		return getiRecErrcode() == childCanNotSale;
	}

	private void recProt(DataInputStream dis, short iTaskCode) throws IOException {
		recHeader(dis);
		recFFFF(dis, iTaskCode);
	}
	/**
	 * @date 2015-10-23上午10:59:34
	 * @param dos
	 * @param isPackage
	 *            1:包标签，2:子标签
	 * @param iTaskCode
	 * @param iLabel
	 * @param carNum
	 * @param iTel
	 */
	private void sendProt(DataOutputStream dos, short iTaskCode, long iLabel, short isPackage, String carNum, long iTel)
			throws SocketTimeoutException, IOException {
		byte[] sendCarNums = string2bytes(carNum);

		short sendCarNumSize = (short) sendCarNums.length;
		short iBodySize = (short) (countBodySize(iLabel, sendCarNumSize, isPackage, iTel) + sendCarNumSize);
		sendHeader(dos, iTaskCode, iBodySize);
		sendiLabel(dos, iLabel);
		dos.writeShort(isPackage);
		dos.writeShort(sendCarNumSize);
		dos.write(sendCarNums);
		dos.writeLong(iTel);
		sendCheckCode(dos);
	}
}
