package cn.net.cpzslibs.prot;

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

/**
 * 6.基于标签号读取销售区域
 * 
 * @author zwj
 * 
 *         2015-04-01
 * @version 1
 */
public class Prot06GetAreaCode extends ProtBase {
	private final short iTaskCode = 6;
	/**
	 * 销售区域，取值参考区域定义
	 */
	private int iAread;
	/**
	 * 产品Id(未启用)
	 */
	private long iProductId;

	/**
	 * @param socketCreate
	 * @throws IOException
	 */
	public void dealGetAreaCode(SocketCreate socketCreate) throws IOException {

		this.iSendTaskIndex = socketCreate.getiTaskIndex();
		sendProt(socketCreate.getDos(), socketCreate.getiLabel());
		recProt(socketCreate.getDis(), iTaskCode);
	}
	/*public void dealGetAreaCode(SocketCreate socketCreate, long labelId) throws IOException {

		this.iSendTaskIndex = socketCreate.getiTaskIndex();
		sendProt(socketCreate.getDos(), labelId);
		recProt(socketCreate.getDis(), iTaskCode);
	}*/

	/**
	 * 这个需要验证是否正确 <br/>
	 * 2015-4-1下午4:35:29
	 * 
	 * @return
	 */
	public int getiAread() {
		return iAread;
	}

	/**
	 * 产品id <br/>
	 * 2015-4-1下午4:17:06
	 * 
	 * @return
	 */
	public long getiProductId() {
		return iProductId;
	}

	/**
	 * 发送协议
	 * <p>
	 * 2014-12-24下午1:23:04
	 * </p>
	 * 
	 * @param dos
	 * @param iLabel
	 * @throws IOException
	 */
	private void sendProt(DataOutputStream dos, long iLabel) throws IOException {
		short bodySize = countBodySize(iLabel);
		sendHeader(dos, (short) 6, bodySize);
		sendiLabel(dos, iLabel);
		sendCheckCode(dos);
	}

	/**
	 * 接收数据，并适当的处理下<br>
	 * Handler返回obj销售地区
	 * <p>
	 * 2014-12-24下午1:47:48
	 * </p>
	 * 
	 * @param dis
	 * @param iTaskCode
	 * @throws IOException
	 */
	private void recProt(DataInputStream dis, short iTaskCode)
			throws IOException {
		recHeader(dis);
		if (isTaskCode(iTaskCode)) {
			iAread = reciAreadCode(dis);
			iProductId = reciProductId(dis);
			recCheckCode(dis);
			if (iAread > 0) {
				// 简单验证是否获取到了地址
				setSuccess(true);
			}
			debugLog(iTaskCode, ";iAread:" + iAread);
		} else {
			// 错误
			recFFFF(dis, iTaskCode);
		}
	}

}
