package cn.net.cpzslibs.prot.handset;

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

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

public class Prot19OptLabel extends ProtBase {
	/**
	 * 19号协议
	 */
	private short iTaskCode19 = 19;
	/**
	 * 云南汽配模式 1901
	 */
	private short iTaskCode_Car1901 = 1901;
	/**
	 * 酒1902
	 */
	private short iTaskCode_Wine1902 = 1902;
	/**
	 * 出库
	 */
	public static final short optAction_out = 1;
	/**
	 * 入库
	 */
	public static final short optAction_in = 2;
	/**
	 * 销售
	 */
	public static final short optAction_sale = 3;
	/**
	 * 返库
	 */
	public static final short optAction_back = 4;

	/**
	 * 19.标签操作.操作类型 1- 出库 2- 入库 3- 销售 4- 返库
	 * 
	 * @author zwj<br/>
	 *         2015-5-6 下午10:51:03<br/>
	 * @param dos
	 * @param dis
	 * @param iLabelId
	 * @param iCompanyId
	 * @param iOptAction
	 *            操作类型
	 * @param iOptAddr
	 *            操作地址
	 * @param iOptPerson
	 *            操作人员
	 * @param iOptMemoStr
	 *            操作说明
	 * @param vecicleNumber
	 *            车牌号
	 * @param iSendTaskCode
	 *            发送协议
	 * @throws IOException
	 */
	private void dealOptLabel(DataOutputStream dos, DataInputStream dis, long iLabelId, int iCompanyId,
			short iOptAction, int iOptAddr, int iOptPerson, String iOptMemoStr, String vecicleNumber,
			short iSendTaskCode) throws IOException {
		int iOptMemSize = iOptMemoStr.getBytes("utf-8").length;
		if (iOptMemSize >= 255) {
			errLog(iSendTaskCode, "描述超过" + iOptMemSize);
			return;
		}
		if (iSendTaskCode == iTaskCode_Car1901) {
			send1901Prot(dos, iLabelId, iCompanyId, iOptAction, (short) iOptAddr, iOptPerson, (short) iOptMemSize,
					iOptMemoStr, vecicleNumber);
		} else {
			send19Prot(dos, iLabelId, iCompanyId, iOptAction, (short) iOptAddr, iOptPerson, (short) iOptMemSize,
					iOptMemoStr);
		}
		recHeader(dis);
		recFFFF(dis, iSendTaskCode);
	}

	/**
	 * 1901标签操作
	 * <p>
	 * 2014-10-29上午10:11:41
	 * </p>
	 * 
	 * @param dos
	 * @param dis
	 * @param iLabelId
	 * @param iOptAction
	 * @param iOptAddr
	 * @param iOptPerson
	 * @param iOptMemoStr
	 * @param vecicleNumber
	 *            车牌号32位
	 * @return
	 * @throws IOException
	 */
	public void dealOptSaleCar(SocketCreate create, long iLabelId, String iOptMemoStr, String vecicleNumber)
			throws IOException {
		short iOptAction = optAction_sale;
		int iOptAddr = 0;
		int iOptPerson = 0;
		dealOptLabel(create.getDos(), create.getDis(), iLabelId, create.getiCompanyId(), iOptAction, iOptAddr,
				iOptPerson, iOptMemoStr, vecicleNumber, iTaskCode_Car1901);
	}
	/**
	 * 入库
	 * 
	 * @date 2015-10-26上午10:42:02
	 * @param create
	 * @param iLabelId
	 * @param iOptMemoStr
	 * @param vecicleNumber
	 * @throws IOException
	 */
	public void dealOptInCar(SocketCreate create, long iLabelId, String iOptMemoStr, String vecicleNumber)
			throws IOException {
		short iOptAction = optAction_in;
		int iOptAddr = 0;
		int iOptPerson = 0;
		dealOptLabel(create.getDos(), create.getDis(), iLabelId, create.getiCompanyId(), iOptAction, iOptAddr,
				iOptPerson, iOptMemoStr, vecicleNumber, iTaskCode_Car1901);
	}
	/**
	 * 出库
	 * 
	 * @date 2015-10-26上午10:40:52
	 * @param create
	 * @param iLabelId
	 * @param iOptMemoStr
	 * @param vecicleNumber
	 */
	public void dealOptOutCar(SocketCreate create, long iLabelId, String iOptMemoStr, String vecicleNumber)
			throws IOException {
		short iOptAction = optAction_out;
		int iOptAddr = 0;
		int iOptPerson = 0;
		dealOptLabel(create.getDos(), create.getDis(), iLabelId, create.getiCompanyId(), iOptAction, iOptAddr,
				iOptPerson, iOptMemoStr, vecicleNumber, iTaskCode_Car1901);
	}

	/**
	 * 1901标签操作--> 返库
	 * <p>
	 * 2014-10-29上午10:11:41
	 * </p>
	 * 
	 * @param dos
	 * @param dis
	 * @param iLabelId
	 * @param iOptAction
	 * @param iOptAddr
	 * @param iOptPerson
	 * @param iOptMemoStr
	 * @param vecicleNumber
	 *            车牌号 不能超过32位
	 * @return
	 * @throws IOException
	 */
	public void dealOptBackCar(SocketCreate create, long iLabelId, String iOptMemoStr, String vecicleNumber)
			throws IOException {
		short iOptAction = optAction_back;
		int iOptAddr = 0;
		int iOptPerson = 0;
		dealOptLabel(create.getDos(), create.getDis(), iLabelId, create.getiCompanyId(), iOptAction, iOptAddr,
				iOptPerson, iOptMemoStr, vecicleNumber, iTaskCode_Car1901);
	}

	/**
	 * 19
	 * <p>
	 * 2014-10-29上午10:27:18
	 * </p>
	 * 
	 * @param dos
	 * @param iLabel
	 * @param iCompanyId企业Id
	 * @param iOptAction操作类型
	 *            1- 出库 2- 入库 3- 销售 4- 返库
	 * @param iOptAddr地区号
	 * @param iOptPerson操作人员
	 * @param iOptMemSize操作说明大小
	 * @param iOptMemoStr操作说明
	 *            ,操作说明长度(最大=255字节)
	 * @param vecicleNumber
	 *            车牌号 不能超过32位
	 * @param model
	 *            模型
	 * @throws IOException
	 */
	private void send19Prot(DataOutputStream dos, long iLabel, int iCompanyId, short iOptAction, short iOptAddr,
			int iOptPerson, short iOptMemSize, String iOptMemoStr) throws IOException {
		// 基本模式
		short bodySize = (short) (countBodySize(iLabel, iCompanyId, iOptAction, iOptAddr, iOptPerson, iOptMemSize) + iOptMemSize);
		sendHeader(dos, iTaskCode19, bodySize);
		sendiLabel(dos, iLabel);
		sendiCompanyId(dos, iCompanyId);
		dos.writeShort(iOptAction);
		dos.writeShort(iOptAddr);
		dos.writeInt(iOptPerson);
		dos.writeShort(iOptMemSize);
		dos.write(iOptMemoStr.getBytes("UTF-8"));
		sendCheckCode(dos);
	}

	/**
	 * 1901
	 * <p>
	 * 2014-12-30下午2:08:54
	 * </p>
	 * 
	 * @param dos
	 * @param iLabel
	 * @param iCompanyId
	 * @param iOptAction
	 * @param iOptAddr
	 * @param iOptPerson
	 * @param iOptMemSize
	 * @param iOptMemoStr
	 * @param vecicleNumber
	 *            车牌号 不能超过32位
	 * @throws IOException
	 */
	private void send1901Prot(DataOutputStream dos, long iLabel, int iCompanyId, short iOptAction, short iOptAddr,
			int iOptPerson, short iOptMemSize, String iOptMemoStr, String vecicleNumber) throws IOException {

		byte[] sendNums = new byte[32];
		byte[] num = vecicleNumber.getBytes("utf-8");
		int numSize = num.length;
		for (int i = 0; i < numSize; i++) {
			sendNums[i] = num[i];
		}
		short bodySize = (short) (countBodySize(iLabel, iCompanyId, iOptAction, iOptAddr, iOptPerson, iOptMemSize,
				sendNums) + iOptMemSize);//
		sendHeader(dos, iTaskCode_Car1901, bodySize);
		sendiLabel(dos, iLabel);
		sendiCompanyId(dos, iCompanyId);
		dos.writeShort(iOptAction);
		dos.writeShort(iOptAddr);
		dos.writeInt(iOptPerson);
		dos.writeShort(iOptMemSize);
		dos.write(iOptMemoStr.getBytes("UTF-8"));
		dos.write(sendNums);
		sendCheckCode(dos);
	}

	/**
	 * 19号协议 <br/>
	 * 2015-3-24上午10:38:36
	 * 
	 * @param dos
	 * @param dis
	 * @param iLabelId
	 * @param iOptAction
	 * @param iCompanyId
	 * @param iOptMemoStr
	 * @throws IOException
	 */
	public void dealOpt(SocketCreate create, long iLabelId, short iOptAction, String iOptMemoStr) throws IOException {
		int iOptAddr = 0;
		int iOptPerson = 0;
		String vecicleNumber = "";
		dealOptLabel(create.getDos(), create.getDis(), iLabelId, create.getiCompanyId(), iOptAction, iOptAddr,
				iOptPerson, iOptMemoStr, vecicleNumber, iTaskCode19);
	}

	// 酒类操作
	private void dealOptWine(SocketCreate create, long iLabelId, short iOptAction, String iOptMemoStr)
			throws IOException {
		int iOptAddr = 0;
		int iOptPerson = 0;
		String vecicleNumber = "";
		dealOptLabel(create.getDos(), create.getDis(), iLabelId, create.getiCompanyId(), iOptAction, iOptAddr,
				iOptPerson, iOptMemoStr, vecicleNumber, iTaskCode_Wine1902);
	}

	/**
	 * 酒类-箱-出库操作
	 * 
	 * @author zwj<br/>
	 *         2015-5-3 下午10:02:30<br/>
	 * @param create
	 * @param iBoxId
	 * @param iOptMemoStr
	 * @throws IOException
	 */
	public void dealOptWineOut(SocketCreate create, long iBoxId, String iOptMemoStr) throws IOException {
		dealOptWine(create, iBoxId, optAction_out, iOptMemoStr);
	}

	/**
	 * 酒类-箱-入库操作
	 * 
	 * @author zwj<br/>
	 *         2015-5-3 下午10:02:30<br/>
	 * @param create
	 * @param iBoxId
	 * @param iOptMemoStr
	 * @throws IOException
	 */
	public void dealOptWineIn(SocketCreate create, long iBoxId, String iOptMemoStr) throws IOException {
		dealOptWine(create, iBoxId, optAction_in, iOptMemoStr);
	}
}
