package cn.net.cpzslibs.prot;

import com.google.gson.Gson;

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

import cn.net.cpzslibs.prot.bean.Prot105LabelHistoryBean;

/**
 * 查询记录
 * @author zwj
 *
 * 2015-9-8
 */
public class Prot105LabelHistory extends ProtBase {
	private short iTaskCode105 = 105;
	private String jsonData;

	public void dealProt(SocketCreate create, String userid) throws SocketTimeoutException, IOException {
		sendProt(create.getDos(), iTaskCode105, userid);
		recProt(create.getDis(), iTaskCode105);
	}

	public String getJsonData() {
		return jsonData;
	}

	public Prot105LabelHistoryBean getLabelHistoryBean() {
		if (jsonData == null || jsonData.isEmpty()) {
			return null;
		}
		Gson gson = new Gson();
		Prot105LabelHistoryBean historyBean = gson.fromJson(jsonData, Prot105LabelHistoryBean.class);
		return historyBean;
	}

	private void recProt(DataInputStream dis, short iTaskCode) throws IOException {
		recHeader(dis);
		if(isFFFF()){
			setSuccess(false);
		}else{
			setSuccess(true);
			int jsonLength = getiRecBodySize() - 2;
			jsonData = getUTF8(dis, jsonLength);
		}
		recCheckCode(dis);
	}
	private void sendProt(DataOutputStream dos, short iTaskCode, String userid) throws SocketTimeoutException,
			IOException {
		byte[] sendUserIds = new byte[12];// 发送用户名
		System.arraycopy(string2bytes(userid), 0, sendUserIds, 12 - string2bytes(userid).length,
				string2bytes(userid).length);
		short iBodySize = countBodySize(sendUserIds);
		sendHeader(dos, iTaskCode, iBodySize);
		dos.write(sendUserIds);
		sendCheckCode(dos);
	}
}
