package cn.net.cpzslibs.prot;

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

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

/**
 * 20号，获取标签产品信息<br>
 * 2001号协议获取产品信息（Json文件）
 * <p>
 * 使用情况：云南汽配模式使用
 * </p>
 * 
 * @author zwj 2014-11-6
 */
public class Prot20ProductDesc extends ProtBase {
	/**
	 * 2001汽配模式
	 */
	private final short iTaskCode2001 = 2001;
	/**
	 * 20汽配模式
	 */
	private final short iTaskCode20 = 20;
	/**
	 * json数据
	 */
	private String jsonData;

	/**
	 * 任务20：无效标签，iErrorCode=0xFFEE
	 */
	private final int INVALID_20_LABEL_ID = 0xFFEE;

	/**
	 * 自动选择模式，酒和通用
	 * <br/>2015-5-25下午4:03:53
	 * @param socketCreate
	 * @throws IOException
	 */
	public void dealProdInfoSwitch(SocketCreate socketCreate) throws IOException{
		switch (socketCreate.getiWorkStyle()) {
		case WorkType.Car:
			dealYunCarProdInfo(socketCreate);
			break;
		default:
			dealProdInfo(socketCreate);
			break;
		}
	}
	
	/*public void dealProdInfoSwitch(SocketCreate socketCreate, int workStle) throws IOException{
		switch (workStle) {
		case WorkType.Car:
			dealYunCarProdInfo(socketCreate);
			break;
		default:
			dealProdInfo(socketCreate);
			break;
		}
	}*/
	
	/**
	 * 2001汽配模式使用 2014-12-1上午10:38:12
	 * 
	 * @throws IOException
	 */
	public void dealYunCarProdInfo(SocketCreate socketCreate)
			throws IOException {
		iSendTaskIndex = socketCreate.getiTaskIndex();
		sendProt(socketCreate.getDos(), socketCreate.getiLabel(),
				socketCreate.getiCompanyId(), socketCreate.getiProductId(),
				iTaskCode2001);
		recProt(socketCreate.getDis(), iTaskCode2001);
	}

	/**
	 * 普通20号协议发送
	 * <p>
	 * 2014-12-1上午10:38:02
	 * </p>
	 * 
	 * @throws IOException
	 */
	public void dealProdInfo(SocketCreate socketCreate) throws IOException {
		iSendTaskIndex = socketCreate.getiTaskIndex();
		sendProt(socketCreate.getDos(), socketCreate.getiLabel(),
				socketCreate.getiCompanyId(), socketCreate.getiProductId(),
				iTaskCode20);
		recProt(socketCreate.getDis(), iTaskCode20);
	}
	/*public void dealProdInfo(SocketCreate socketCreate, long labelID, Prot10001LoginResult loginBean) throws IOException {
		iSendTaskIndex = socketCreate.getiTaskIndex();
		sendProt(socketCreate.getDos(), labelID,
				Integer.parseInt(loginBean.getCompanyID()), Long.parseLong(loginBean.getProductID()),
				iTaskCode20);
		recProt(socketCreate.getDis(), iTaskCode20);
	}
	*/
	
	/**
	 * 接收并处理数据<br/>
	 * 2014-11-6下午1:59:24
	 * 
	 * @param dis
	 * @param iTaskCode
	 * @throws IOException
	 */
	private void recProt(DataInputStream dis, short iTaskCode)
			throws IOException {
		recHeader(dis);
		int jsonSize = getiRecBodySize() - 2;
		if (jsonSize <= 0) {
			// 接收到了少于2个字节的数据
			setSuccess(false);
			errLog(iTaskCode, "json is empty");
			return;
		}
		int sendTaskCode = getUnsignedByte(iTaskCode);

		String jsonData = null;// json数据
		if (isTaskCode(iTaskCode)) {
			byte[] jsonBytes = recString(dis, jsonSize);
			recCheckCode(dis);
			jsonData = new String(jsonBytes, "UTF-8");
//			System.err.println("   jsonData:" + jsonData);
		} else {
			recFFFF(dis, iTaskCode);
		}

		if (!isResult() || jsonData == null || jsonData.length() <= 0) {
			setSuccess(false);
			if (getiRecErrcode() == (short) INVALID_20_LABEL_ID) {
				errLog(sendTaskCode, "非法追溯码");
			} else {
				errLog(sendTaskCode, "无数据");
			}
			return;
		} else {
			setSuccess(true);
			this.jsonData = jsonData;
		}
	}

	/**
	 * 2001 云南汽配模式中使用的。获取产品信息 20号协议
	 * <p>
	 * 2014-11-7下午3:44:16
	 * </p>
	 * 
	 * @param dos
	 * @param iLabelId
	 * @param iCompanyId
	 * @param iProductId
	 * @param iTaskCode
	 *            任务号，这里支持20和2001
	 * @throws IOException
	 */
	private void sendProt(DataOutputStream dos, long iLabelId, int iCompanyId,
			long iProductId, short iTaskCode) throws IOException {
		short bodySize = countBodySize(iLabelId, iCompanyId, iProductId);
		sendHeader(dos, iTaskCode, bodySize);
		dos.writeLong(iLabelId);
		dos.writeInt(iCompanyId);
		dos.writeLong(iProductId);
		sendCheckCode(dos);
	}

	// ----通用字段
	/**
	 * 产品的id
	 */
	public final static String kId = "id";
	/**
	 * 配件产地
	 */
	public final static String kOrigin = "origin";
	/**
	 * 配件厂商
	 */
	public final static String kProducer = "producer";
	/**
	 * 查询描述
	 */
	public final static String kQuery_des = "query_des";
	/**
	 * 公司电话
	 */
	public final static String kTEL = "companyTel";

	/* --------------------------2001汽配模式专用字段-------------------------- */
	/**
	 * 适用车型
	 */
	public final static String kCarType = "carType";
	/**
	 * 配件型号
	 */
	public final static String kNumberID = "numberID";
	/**
	 * 配件描述2001
	 */
	public final static String kDescrip = "descrip";
	/**
	 * 配件名称
	 */
	public final static String kName = "name";
	/* --------------------------20号协议特殊字段-------------------------- */
	/**
	 * 消费码
	 */
	public final static String kCode = "code";
	/**
	 * 公司名称
	 */
	public final static String kCompanyname = "companyname";
	/**
	 * 产品描述20
	 */
	public final static String kDescription = "description";
	/**
	 * 产品名称
	 */
	public final static String kProductname = "name";
	/**
	 * 产品url
	 */
	public final static String kUrl = "url";
	/**
	 * 公司url
	 */
	public final static String kCompanyUrl = "companyUrl";

	/**
	 * [{"carType":"一汽大众","descrip":"","id":"100053","name":"滚针轴承","numberID":
	 * "002 311 125 A","origin":"","producer":"上海上汽大众汽车销售有限公司","query_des":""}]
	 * 
	 * 
	 * @param strResult
	 */
	private Map<String, String> parse2001Json(String strResult) {
		return JsonToMap.toListFirstMap(strResult);
	}

	/**
	 * 20.
	 * [{"code":"8208","companyname":"云南大理泛亚汽车城有限责任公司","description":"","id":
	 * "100053","origin":"","producer":"上海上汽大众汽车销售有限公司","productname":"滚针轴承",
	 * "query_des":"","url":"",companyUrl”:”XXX”}] <br/>
	 * 
	 * @param strResult
	 */
	private Map<String, String> parse20Json(String strResult) {
		return JsonToMap.toListFirstMap(strResult);
	}

	/**
	 * 
	 * 2015-3-13下午2:14:14
	 * [{"code":"8208","companyname":"云南大理泛亚汽车城有限责任公司","description":"","id":<br/>
	 * "100053","origin":"","producer":"上海上汽大众汽车销售有限公司","productname":"滚针轴承",<br/>
	 * "query_des":"","url":"",companyUrl”:”XXX”}] <br/>
	 * 
	 * @return
	 */
	public Map<String, String> getMap20() {
		return JsonToMap.toListFirstMap(this.jsonData);
	}

	/**
	 * 汽配返回的结果 <br/>
	 * 2001号协议json解析为map的结果 <br/>
	 * [{"carType":"一汽大众","descrip":"","id":"100053","name":"滚针轴承","numberID":<br/>
	 * "002 311 125 A","origin":"","producer":"上海上汽大众汽车销售有限公司","query_des":""}]<br/>
	 * 2015-4-2下午4:47:38
	 * 
	 * @return
	 */
	public Map<String, String> getMap2001() {
		return JsonToMap.toListFirstMap(this.jsonData);
	}
	/**
	 * 通用的Map
	 * <br/>2015-5-25下午3:58:31
	 * @return
	 */
	public  Map<String, String> getCommonMap(){
		return JsonToMap.toListFirstMap(this.jsonData);
	}

	/**
	 * 服务器获取到的json数据 <br/>
	 * 2015-4-27下午1:31:48
	 * 
	 * @return
	 */
	public String getJsonData() {
		return jsonData;
	}

}
