package cn.net.cpzslibs.prot;

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

import cn.net.cpzslibs.address.AddressUtil;

public class Prot10UpdateConflict extends ProtBase {

	/**
	 * 10．报告串货信息
	 */
	private final short iTaskCode10 = 10;
	private String sInspectorId;
	private String errMsg;

	/**
	 * 消费者app<br/>
	 * 2015年4月19日 23:20:09
	 * 
	 * @param socketCreate
	 * @param iSale_Area
	 *            预设值销售地区 Code
	 * @param iConflict_Area
	 *            当前销售地区，即串货地区 Code
	 * @throws IOException
	 */
	public void dealUpdateCnflictCpzs(SocketCreate socketCreate,
                                      int iSale_Area, int iConflict_Area) throws IOException {
		this.sInspectorId = socketCreate.getsInspectorId();
		this.iSendTaskIndex = socketCreate.getiTaskIndex();
		short iCollisionType = 0;// 消费者为0
		recProt(socketCreate.getDos(), socketCreate.getDis(), iTaskCode10,
				socketCreate.getiLabel(), socketCreate.getiCompanyId(),
				socketCreate.getiProductId(), iSale_Area, iConflict_Area,
				iCollisionType);
	}

	/**
	 * 稽查app<br/>
	 * 2015年4月19日 23:21:50
	 * 
	 * @param socketCreate
	 * @param iSale_Area
	 *            预设值销售地区 Code
	 * @param iConflict_Area
	 *            当前销售地区，即串货地区 Code
	 * @throws IOException
	 */
	public void dealUpdateCnflictCpzsCheck(SocketCreate socketCreate,
                                           int iSale_Area, int iConflict_Area) throws IOException {

		this.iSendTaskIndex = socketCreate.getiTaskIndex();
		this.sInspectorId = socketCreate.getsInspectorId();
		short iCollisionType = 1;// 稽查为1
		recProt(socketCreate.getDos(), socketCreate.getDis(), iTaskCode10,
				socketCreate.getiLabel(), socketCreate.getiCompanyId(),
				socketCreate.getiProductId(), iSale_Area, iConflict_Area,
				iCollisionType);
	}
	public void dealUpdateCnflictCpzsCheck(SocketCreate socketCreate,
                                           int iSale_Area, int iConflict_Area, long labelId, int companyId, int productId) throws IOException {

		this.iSendTaskIndex = socketCreate.getiTaskIndex();
		short iCollisionType = 1;// 稽查为1
		recProt(socketCreate.getDos(), socketCreate.getDis(), iTaskCode10,
				labelId, companyId,
				productId, iSale_Area, iConflict_Area,
				iCollisionType);
	}

	/**
	 * 10．报告串货信息
	 * 
	 * @param dos
	 * @param iType
	 *            0: 消费者APP;1：稽查APP
	 * @param iLabel
	 *            标签号
	 * @param iCompanyId
	 *            企业Id
	 * @param iProductId
	 *            产品Id
	 * @param iSale_Area
	 *            销售区域
	 * @param iConflict_Area
	 *            冲突区域
	 * @throws IOException
	 */
	private void sendProt(DataOutputStream dos, long iLabel, int iCompanyId,
			long iProductId, int iSale_Area, int iConflict_Area, short iType,
			String sUserTag) throws IOException {
		byte[] sInspectorId = sUserTag.getBytes();
		byte[] sendInspectorId = new byte[32];
		// 后面补零，服务器遇0认为是空
		System.arraycopy(sInspectorId, 0, sendInspectorId, 0,
				sInspectorId.length);
		short bodySize = countBodySize(iLabel, iCompanyId, iProductId,
				iSale_Area, iConflict_Area, iType, sendInspectorId);

		sendHeader(dos, (short) 10, bodySize);
		sendiLabel(dos, iLabel);
		sendiCompanyId(dos, iCompanyId);
		sendiProductId(dos, iProductId);
		sendiAreaCode(dos, iSale_Area);
		sendiAreaCode(dos, iConflict_Area);
		dos.writeShort(iType);// 0: 消费者APP;1：稽查APP
		dos.write(sendInspectorId);
		sendCheckCode(dos);
	}

	/**
	 * 10.报告串货信息
	 * <p>
	 * 2014-12-24下午2:46:00
	 * </p>
	 * 
	 * @param dos
	 * @param dis
	 * @param iTaskCode
	 *            任务号
	 * @param iLabelID
	 *            标签id
	 * @param iCompanyId
	 *            公司id
	 * @param iProductId
	 *            产品id
	 * @param iSale_Area
	 *            预设值销售地区 Code
	 * @param iConflict_Area
	 *            当前销售地区，即串货地区 Code
	 * @param iCollisionType
	 *            0: 消费者APP,1：稽查APP
	 * @throws IOException
	 */
	private void recProt(DataOutputStream dos, DataInputStream dis,
			short iTaskCode, long iLabelID, int iCompanyId, long iProductId,
			int iSale_Area, int iConflict_Area, short iCollisionType)
			throws IOException {
		if (AddressUtil.isSame(iSale_Area, iConflict_Area)) {
			errMsg = AddressUtil.errMsg;
			this.setSuccess(false);
			return;
		}
		sendProt(dos, iLabelID, iCompanyId, iProductId, iSale_Area,
				iConflict_Area, iCollisionType, sInspectorId);
		recHeader(dis);
		recFFFF(dis, iTaskCode);
	}

	/**
	 * 提示错误信息
	 * 
	 * @author zwj<br/>
	 *         2015-4-24 下午10:39:18<br/>
	 * @return
	 */
	public String getErrMsg() {
		return errMsg;
	}
}
