package cn.net.cpzslibs.prot.handset;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

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

/**
 * 下载正品保证单
 * 
 * @author zwj
 * 
 *         2015-7-17
 */
public class Prot26GoodsId extends ProtBase {
	private final short iTaskCode = 26;
	private String jsonData;

	public void dealGoodsId(SocketCreate create) throws IOException {
		recProt(create.getDos(), create.getDis(), iTaskCode);
	}

	private void recProt(DataOutputStream dos, DataInputStream dis,
			short iTaskCode) throws IOException {
		sendProt(dos, iTaskCode);
		recHeader(dis);
		if (isFFFF()) {
			recFFFF(dis, iTaskCode);
		} else {
			int iJsonSize = getUnsignedByte(dis.readShort());
			jsonData = iJsonSize <= 0 ? null : getUTF8(dis, iJsonSize);
			recCheckCode(dis);
		}
	}

	private void sendProt(DataOutputStream dos, short iTaskCode)
			throws IOException {
		short bodySize = countBodySize();
		sendHeader(dos, iTaskCode, bodySize);
		sendCheckCode(dos);
	}

	/**
	 * json数据，[{ "id": "XXX", "name": "xxx" },{}] <br/>
	 * 2015-7-15下午4:45:39
	 * 
	 * @return 无数据返回null;
	 */
	public String getJsonData() {
		return jsonData;
	}

	/**
	 * 
	 * <br/>
	 * 2015-7-15下午4:46:36
	 * 
	 * @return 无数据返回null;key:id,name
	 */
	public List<Map<String, String>> parseJson() {
		return jsonData == null ? null : JsonToMap.toListMap(jsonData);
	}

}
