package cn.net.cpzslibs.prot;

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

/**
 * 获取配置文件指定数据<br/>
 * 1.用于商城模式，获取2个字段。A.商城广告语和URL;B.商铺描述和URL(这个URL是商铺URL)
 * 
 * @author zwj 2014-12-9
 */
public class Prot23ConfigData extends ProtBase {

	private short iTaskCode23 = 23;
	/**
	 * 配置数据
	 */
	private String sConfigData;

	/**
	 * 获取配置文件指定数据23<br/>
	 * 2014-12-9下午3:53:31
	 * 
	 * @param sKeyLevel_1第一层关键词的长度
	 *            "display"
	 * @param sKeyLevel_2第二层关键词的长度
	 *            "企业id"
	 * @throws IOException
	 */
	public void dealConfigData(SocketCreate socketCreate, String sKeyLevel_1,
                               String sKeyLevel_2) throws IOException {
		recProt(socketCreate.getDis(), socketCreate.getDos(), iTaskCode23,
				sKeyLevel_1, sKeyLevel_2);
	}

	/**
	 * 23. 获取配置文件指定数据
	 * <p>
	 * 2014-12-9下午2:51:07
	 * </p>
	 * 
	 * @param dos
	 * @param iTaskCode
	 * @param sKeyLevel_1第一层关键词的长度
	 * @param sKeyLevel_2第二层关键词的长度
	 * @throws IOException
	 */
	private void sendProt(DataOutputStream dos, short iTaskCode,
			String sKeyLevel_1, String sKeyLevel_2) throws IOException {
		byte[] sendSKey1 = sKeyLevel_1.getBytes();
		short iKeyLevelSize_1 = (short) sendSKey1.length;
		byte[] sendSKey2 = sKeyLevel_2.getBytes();
		short iKeyLevelSize_2 = (short) sendSKey2.length;
		short bodySize = countBodySize(iKeyLevelSize_1, sendSKey1,
				iKeyLevelSize_2, sendSKey2);
		sendHeader(dos, iTaskCode, bodySize);
		dos.writeShort(iKeyLevelSize_1);
		dos.write(sendSKey1);
		dos.writeShort(iKeyLevelSize_2);
		dos.write(sendSKey2);
		sendCheckCode(dos);
	}

	/**
	 * 
	 * <p>
	 * 2014-12-22上午10:51:51
	 * </p>
	 * 
	 * @param socket
	 * @param dis
	 * @param dos
	 * @param iTaskCode
	 * @param sKeyLevel_1
	 * @param sKeyLevel_2
	 * @throws IOException
	 */
	private void recProt(DataInputStream dis, DataOutputStream dos,
			short iTaskCode, String sKeyLevel_1, String sKeyLevel_2)
			throws IOException {

		sendProt(dos, iTaskCode, sKeyLevel_1, sKeyLevel_2);
		recHeader(dis);
		if (isTaskCode(iTaskCode)) {
			short sContentSize = dis.readShort();
			byte[] bsContent = new byte[getUnsignedByte(sContentSize)];
			for (int i = 0; i < bsContent.length; i++) {
				bsContent[i] = dis.readByte();
			}
			String sContent = new String(bsContent, "UTF-8");
			this.sConfigData = sContent;
			recCheckCode(dis);
			debugLog(iTaskCode, sContent);
			setSuccess(true);
		} else {
			recFFFF(dis, iTaskCode);
		}
	}

	/**
	 * 配置文件的内容，json根式的String <br/>
	 * 2015-3-25上午11:22:31
	 * 
	 * @return
	 */
	public String getsConfigData() {
		return sConfigData;
	}

}
