package cn.net.cpzslibs.prot;

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

import cn.net.cpzslibs.address.AddressUtil;

public class Prot11DownConflictInfo extends ProtBase {
	// private final short iTaskCode11 = 11;
	private final short iTaskCode1101 = 1101;
	private final short iTaskCode1102 = 1102;

	private short iPageSize = 12;// 设置每页显示12条数据
	private final short iStatus = 0;// 读取未处理的信息
	private String jsonData;// 获取到的json数据

	// --------------废弃-----
	/**
	 * 总页数
	 */
	@Deprecated
	private int iRecPageCount;
	/**
	 * 当前页码
	 */
	@Deprecated
	private short iRecPageNo;
	/**
	 * 冲突信息
	 */
	@Deprecated
	private List<Map<String, String>> conflictInfoMaps;
	// --------------key-----
	/**
	 * 序号
	 */
	public final static String kId = "id";
	/**
	 * 追溯码
	 */
	public final static String kLabelId = "iLabelId";
	/**
	 * 产品Id
	 */
	public final static String kProductId = "iProductId";
	/**
	 * 销售区域码码
	 */
	public final static String kSaleAreaCode = "iSaleArea";
	/**
	 * 冲突区域码
	 */
	public final static String kConllisionAreaCode = "iConllisionArea";
	/**
	 * 销售区域名称
	 */
	public final static String kSaleAreaName = "iSaleAreaName";
	/**
	 * 冲突区域名称
	 */
	public final static String kConllisionAreaName = "iConllisionAreaName";
	/**
	 * 冲突时间
	 */
	public final static String kConllisionTime = "iConllisionTime";
	/**
	 * 冲突类型
	 */
	public final static String kConllisionType = "iConllisionType";
	/**
	 * 处理状态
	 */
	public final static String kStatus = "iStatus";

	/**
	 * 下载指定页码的串货信息 <br/>
	 * 2015-6-2下午4:05:17
	 * 
	 * @param socketCreate
	 * @param iPageNo
	 *            申请显示页码
	 * @throws IOException
	 */
	public void dealDownConflictInfo(SocketCreate socketCreate, int iPageNo)
			throws IOException {
		recProt1101(socketCreate, iTaskCode1101, (short)iPageNo);
		
	}
	/**
	 * 下载指定页码的串货信息 <br/>
	 * 2015-6-2下午4:05:17
	 * 
	 * @param socketCreate
	 * @param iPageNo
	 *            申请显示页码
	 * @throws IOException
	 */
	public void dealAllConflictInfo(SocketCreate socketCreate, int iPageSize)
			throws IOException {
		this.iPageSize = (short)iPageSize;
		recProt1101(socketCreate, iTaskCode1101, (short)1);
		
	}
	
	public void dealConflictInfoByPage(SocketCreate socketCreate, int iPageSize, int iPageNo)
			throws IOException {
		this.iPageSize = (short)iPageSize;
		recProt1101(socketCreate, iTaskCode1101, (short)iPageNo);
		
	}
	
	public void dealNewConflictInfoByPage(SocketCreate socketCreate, int iPageSize, int iPageNo)
			throws IOException {
		this.iPageSize = (short)iPageSize;
		recProt1101(socketCreate, iTaskCode1102, (short)iPageNo);
		
	}
	
	

	/**
	 * <br/>
	 * 2015-6-2下午3:59:25
	 * 
	 * @param create
	 * @param iTaskCode
	 * @param iPageNo
	 * @throws IOException
	 */
	private void recProt1101(SocketCreate create, short iTaskCode, short iPageNo)
			throws IOException {
		this.iSendTaskIndex = create.getiTaskIndex();
		DataInputStream dis = create.getDis();
//		System.err.println("   iPageSize:" + iPageSize);
		sendProt(create.getDos(), iTaskCode, create.getiCompanyId(), iPageNo,
				iPageSize, iStatus);
		recHeader(dis);
		if (isTaskCode(iTaskCode)) {
			int iJsonSize = getUnsignedByte(dis.readShort());
//			System.err.println("iJsonSize--bodySize:" + iJsonSize); 
			if (iJsonSize > 0) {
				setSuccess(true);
				jsonData = new String(recString(dis, iJsonSize), "utf-8");
				System.err.println("jsonLength:" + jsonData.length());
			} else {
				setSuccess(false);
				jsonData = null;
			}
		} else {
			jsonData = null;
			recFFFF(dis, iTaskCode);
		}
	}

	/**
	 * 得到的json数据 <br/>
	 * 
	 * { <br/>
	 * "PageCount":"10", // 总页数 <br/>
	 * "PageNo":"3", // 当前页号 <br/>
	 * "PageSize":"20", // 每页大小 <br/>
	 * "acInfo":[ // 串货信息数组 <br/>
	 * { <br/>
	 * 		"id":"XXX", // 序号 <br/>
	 * 		"LabelId":"XXX", // 标签号 <br/>
	 * 		"ProductId":"XXX", // 产品ID <br/>
	 * 		"SaleArea":"XXX", // 销售区域 <br/>
	 * 		"ConllisionArea":"XXX", // 冲突区域 <br/>
	 * 		"ConllisionType":"XXX", // 冲突类型 <br/>
	 * 		"Status":"XXX" // 处理状态 <br/>
	 * }, <br/>
	 * ... <br/>
	 * ] <br/>
	 * } <br/>
	 * 
	 * 2015-6-2下午3:59:14
	 * 
	 * @return
	 */
	public String getJsonData() {
		return jsonData;
	}

	// /**
	// *
	// * 下载未处理的窜货信息。<br/>
	// *
	// * @param socketCreate
	// * @param iPageNo下载页码
	// * @throws IOException
	// */
	// @Deprecated
	// public void dealDownConflictInfo(SocketCreate socketCreate, short
	// iPageNo)
	// throws IOException {
	//
	// this.iSendTaskIndex = socketCreate.getiTaskIndex();
	// short iPageSize = 12;// 设置每页显示12条数据
	// short iStatus = 0;// 读取未处理的信息
	// recProt(socketCreate.getDos(), socketCreate.getDis(), iTaskCode11,
	// socketCreate.getiCompanyId(), iPageNo, iPageSize, iStatus);
	// }

	/**
	 * 下载串货信息<br/>
	 * 2014-12-31上午10:00:33
	 * 
	 * @param dos
	 * @param iCompanyId
	 *            企业ID
	 * @param iPageNo
	 *            页号，从第一页开始
	 * @param iPageSize
	 *            每页记录数
	 * @param iStatus
	 *            0:未处理 1:已处理 0xFFFF:全部 目前只是请求未处理的
	 * @throws IOException
	 */
	private void sendProt(DataOutputStream dos, short iTaskCode,
			int iCompanyId, short iPageNo, short iPageSize, short iStatus)
			throws IOException {
		short bodySize = countBodySize(iCompanyId, iPageNo, iPageSize, iStatus);
		sendHeader(dos, iTaskCode, bodySize);
		sendiCompanyId(dos, iCompanyId);
		dos.writeShort(iPageNo);
		dos.writeShort(iPageSize);
		dos.writeShort(iStatus);
		sendCheckCode(dos);
	}

	/**
	 * 下载串货信息<br/>
	 * 2014-12-31上午10:02:39
	 * 
	 * @param dos
	 * @param dis
	 * @param iCompanyId
	 *            企业ID
	 * @param iPageNo
	 *            页号，从第一页开始
	 * @param iPageSize
	 *            每页记录数
	 * @param iStatus
	 *            0:未处理 1:已处理 0xFFFF:全部 目前只是请求未处理的
	 * @throws IOException
	 */
	@Deprecated
	private void recProt(DataOutputStream dos, DataInputStream dis,
			short iTaskCode, int iCompanyId, short iPageNo, short iPageSize,
			short iStatus) throws IOException {

		sendProt(dos, iTaskCode, iCompanyId, iPageNo, iPageSize, iStatus);

		recHeader(dis);
		if (isTaskCode(iTaskCode)) {
			conflictInfoMaps = new ArrayList<Map<String, String>>();
			short iPageCount = dis.readShort();// 总页数
			iRecPageCount = iPageCount;
			if (iPageCount > 0) {
				iRecPageNo = dis.readShort();
				short pageSize = dis.readShort();// 当前页总记录数

				for (int j = 0; j < pageSize; j++) {
					Map<String, String> map = new HashMap<String, String>();
					long id = dis.readLong();// 序号
					long iLabelId = reciLabel(dis);// 追溯码
					long iProductId = reciProductId(dis);// 产品Id
					int iSaleArea = reciAreadCode(dis);// 销售区域
					int iConllisionArea = reciAreadCode(dis);// 冲突区域
					byte[] iConllisionTime = recString(dis, 16);// 冲突时间
					// YYYYMMDD-HHMMSS
					short iConllisionType = dis.readShort();
					short iRecStatus = dis.readShort();// 处理状态
					map.put(kId, String.valueOf(id));
					map.put(kLabelId, String.valueOf(iLabelId));
					map.put(kProductId, String.valueOf(iProductId));
					map.put(kSaleAreaCode, String.valueOf(iSaleArea));
					map.put(kConllisionAreaCode,
							String.valueOf(iConllisionArea));
					map.put(kConllisionType, String.valueOf(iConllisionType));
					map.put(kStatus, String.valueOf(iRecStatus));

					String strSaleAreaName = AddressUtil
							.searchCityNameByNum(iSaleArea);
					String strConllisionAreaName = AddressUtil
							.searchCityNameByNum(iConllisionArea);
					if (strSaleAreaName.trim().length() <= 0) {
						strSaleAreaName = "无";
					}
					if (strConllisionAreaName.trim().length() <= 0) {
						strConllisionAreaName = "无";
					}
					map.put(kSaleAreaName, strSaleAreaName);
					map.put(kConllisionAreaName, strConllisionAreaName);
					conflictInfoMaps.add(map);// 获取全部数据
				}
				recCheckCode(dis);
				setSuccess(true);
			} else {
				setSuccess(false);
			}
		} else {
			recFFFF(dis, iTaskCode);
		}
	}

	/**
	 * 服务器返回总共页数<br/>
	 * 2014-12-31上午11:15:31
	 * 
	 * @return
	 */
	@Deprecated
	public int getiRecPageCount() {
		return iRecPageCount;
	}

	/**
	 * 冲突信息
	 * 
	 * @return
	 */
	@Deprecated
	public List<Map<String, String>> getConflictInfoMaps() {
		return conflictInfoMaps;
	}

	/**
	 * 当前页码
	 * 
	 * @return
	 */
	@Deprecated
	public short getiRecPageNo() {
		return iRecPageNo;
	}
}
