package cn.net.cpzslibs.prot.handset;

import com.google.gson.Gson;

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

import cn.net.cpzslibs.prot.ProtBase;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.Prot10048BreedManagerBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10048MachManagerBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10048PlantBreedMangerBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10048PlantMachMangerBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10048PutStuffBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10048SendBreedBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10048SendMachBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10048SendPlantBreedBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10048SendPlantMachBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10048SendPutStuffBean;
import cn.net.cpzslibs.prot.utils.StrUtils;

public class Prot10048BreedAndMach extends ProtBase {

	private final short iTaskCode10048 = 10048;

	private Prot10048BreedManagerBean resBreedBean;// 养殖计划的接受的json

	private Prot10048MachManagerBean resMachBean;// 养殖加工计划的接受的json

	private Prot10048PlantBreedMangerBean resPlantBreed; // 种植生产计划接受的json

	private Prot10048PlantMachMangerBean resPlantMach;// 种植加工计划

	private Prot10048PutStuffBean putStuff;// 添加投入品表

	private Gson gson;

	public Prot10048BreedAndMach() {
		gson = new Gson();
	}

	public Prot10048BreedManagerBean getResBreedBean() {
		return resBreedBean;
	}

	public void setResBreedBean(Prot10048BreedManagerBean resBreedBean) {
		this.resBreedBean = resBreedBean;
	}

	public Prot10048MachManagerBean getResMachBean() {
		return resMachBean;
	}

	public void setResMachBean(Prot10048MachManagerBean resMachBean) {
		this.resMachBean = resMachBean;
	}

	public Prot10048PlantBreedMangerBean getResPlantBreed() {
		return resPlantBreed;
	}

	public void setResPlantBreed(Prot10048PlantBreedMangerBean resPlantBreed) {
		this.resPlantBreed = resPlantBreed;
	}

	public Prot10048PlantMachMangerBean getResPlantMach() {
		return resPlantMach;
	}

	public void setResPlantMach(Prot10048PlantMachMangerBean resPlantMach) {
		this.resPlantMach = resPlantMach;
	}

	public Prot10048PutStuffBean getPutStuff() {
		return putStuff;
	}

	public void setPutStuff(Prot10048PutStuffBean putStuff) {
		this.putStuff = putStuff;
	}

	public void dealBreedManagerList(SocketCreate create,
                                     Prot10048SendBreedBean labelBean) throws IOException {
		recProt(create.getSocket(), create.getDos(), create.getDis(), labelBean);
	}

	private void recProt(Socket socket, DataOutputStream dos,
			DataInputStream dis, Prot10048SendBreedBean labelBean)
			throws IOException {
		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {
				// 发送登录信息
				debugLog(iTaskCode10048, "开始请求产品列表");
				sendProt(dos, labelBean);
			}
		}
		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				recJson(dis);
			}
		}
	}

	/**
	 * 发送JSon数据到服务器
	 */
	private void sendProt(DataOutputStream dos, Prot10048SendBreedBean labelBean)
			throws IOException {
		String bodyContent = gson.toJson(labelBean).toString();
		System.out.println("String" + bodyContent);
		short bodySize = countBodySize(bodyContent);
		sendHeader(dos, iTaskCode10048, bodySize);// 84
		dos.write(bodyContent.getBytes("UTF-8"));

		sendCheckCoded(dos, bodyContent.getBytes("UTF-8"));
	}

	/**
	 * 读取JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recJson(DataInputStream dis) throws IOException {
		debugLog(iTaskCode10048, "获取接收的数据");
		recHeader(dis);
		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);
		debugLog(iTaskCode10048, jsonData);

		if (!StrUtils.isEmpty(jsonData)) {

			resBreedBean = gson.fromJson(jsonData,
					Prot10048BreedManagerBean.class);
		}
		recCheckCode(dis);
	}

	// 发送加工管理计划

	public void dealMachManagerList(SocketCreate create,
                                    Prot10048SendMachBean labelBean) throws IOException {
		recMachProt(create.getSocket(), create.getDos(), create.getDis(),
				labelBean);
	}

	private void recMachProt(Socket socket, DataOutputStream dos,
			DataInputStream dis, Prot10048SendMachBean labelBean)
			throws IOException {
		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {
				// 发送登录信息
				debugLog(iTaskCode10048, "开始请求产品列表");
				sendMachProt(dos, labelBean);
			}
		}
		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				recMachJson(dis);
			}
		}
	}

	/**
	 * 发送JSon数据到服务器
	 */
	private void sendMachProt(DataOutputStream dos,
			Prot10048SendMachBean labelBean) throws IOException {
		String bodyContent = gson.toJson(labelBean).toString();
		short bodySize = countBodySize(bodyContent);
		sendHeader(dos, iTaskCode10048, bodySize);// 84
		dos.write(bodyContent.getBytes("UTF-8"));

		sendCheckCoded(dos, bodyContent.getBytes("UTF-8"));
	}

	/**
	 * 读取JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recMachJson(DataInputStream dis) throws IOException {
		debugLog(iTaskCode10048, "获取接收的数据");
		recHeader(dis);
		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);
		debugLog(iTaskCode10048, jsonData);

		if (!StrUtils.isEmpty(jsonData)) {

			resMachBean = gson.fromJson(jsonData,
					Prot10048MachManagerBean.class);
		}
		recCheckCode(dis);
	}

	public void dealPlantBreedMangerList(SocketCreate create,
                                         Prot10048SendPlantBreedBean labelBean) throws IOException {
		recPlantBreedProt(create.getSocket(), create.getDos(), create.getDis(),
				labelBean);
	}

	private void recPlantBreedProt(Socket socket, DataOutputStream dos,
			DataInputStream dis, Prot10048SendPlantBreedBean labelBean)
			throws IOException {
		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {
				// 发送登录信息
				debugLog(iTaskCode10048, "开始请求产品列表");
				sendPlantBreedProt(dos, labelBean);
			}
		}
		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				recPlantBreedJson(dis);
			}
		}
	}

	/**
	 * 发送种植的生产管理JSon数据到服务器
	 */
	private void sendPlantBreedProt(DataOutputStream dos,
			Prot10048SendPlantBreedBean labelBean) throws IOException {
		String bodyContent = gson.toJson(labelBean).toString();
		System.out.println("String--json内容" + bodyContent);
		short bodySize = countBodySize(bodyContent);
		sendHeader(dos, iTaskCode10048, bodySize);// 84
		dos.write(bodyContent.getBytes("UTF-8"));

		sendCheckCoded(dos, bodyContent.getBytes("UTF-8"));
	}

	/**
	 * 读取种植JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recPlantBreedJson(DataInputStream dis) throws IOException {
		debugLog(iTaskCode10048, "获取接收的数据");
		recHeader(dis);
		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);
		debugLog(iTaskCode10048, jsonData);

		if (!StrUtils.isEmpty(jsonData)) {

			resPlantBreed = gson.fromJson(jsonData,
					Prot10048PlantBreedMangerBean.class);
		}
		recCheckCode(dis);
	}

	// 发送种植加工管理计划

	public void dealPlantMachManagerList(SocketCreate create,
                                         Prot10048SendPlantMachBean labelBean) throws IOException {
		recPlantMachProt(create.getSocket(), create.getDos(), create.getDis(),
				labelBean);
	}

	private void recPlantMachProt(Socket socket, DataOutputStream dos,
			DataInputStream dis, Prot10048SendPlantMachBean labelBean)
			throws IOException {
		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {
				// 发送登录信息
				debugLog(iTaskCode10048, "开始发送信息");
				sendPlantMachProt(dos, labelBean);
			}
		}
		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				recPlantMachJson(dis);
			}
		}
	}

	/**
	 * 发送种植的加工管理JSon数据到服务器
	 */
	private void sendPlantMachProt(DataOutputStream dos,
			Prot10048SendPlantMachBean labelBean) throws IOException {
		String bodyContent = gson.toJson(labelBean).toString();
		System.out.println("String--json内容" + bodyContent);
		short bodySize = countBodySize(bodyContent);
		sendHeader(dos, iTaskCode10048, bodySize);// 84
		dos.write(bodyContent.getBytes("UTF-8"));

		sendCheckCoded(dos, bodyContent.getBytes("UTF-8"));
	}

	/**
	 * 读取种植加工JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recPlantMachJson(DataInputStream dis) throws IOException {
		debugLog(iTaskCode10048, "获取接收的数据");
		recHeader(dis);
		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);
		debugLog(iTaskCode10048, jsonData);

		if (!StrUtils.isEmpty(jsonData)) {
			resPlantMach = gson.fromJson(jsonData,
					Prot10048PlantMachMangerBean.class);
		}
		recCheckCode(dis);
	}

	// 发送种植加工管理计划

	public void dealPutStuffList(SocketCreate create,
                                 Prot10048SendPutStuffBean labelBean) throws IOException {
		recPutStuffProt(create.getSocket(), create.getDos(), create.getDis(),
				labelBean);
	}

	private void recPutStuffProt(Socket socket, DataOutputStream dos,
			DataInputStream dis, Prot10048SendPutStuffBean labelBean)
			throws IOException {
		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {
				// 发送登录信息
				debugLog(iTaskCode10048, "开始发送信息");
				sendPutStuffProt(dos, labelBean);
			}
		}
		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				recPutStuffJson(dis);
			}
		}
	}

	/**
	 * 发送种植的加工管理JSon数据到服务器
	 */
	private void sendPutStuffProt(DataOutputStream dos,
			Prot10048SendPutStuffBean labelBean) throws IOException {
		String bodyContent = gson.toJson(labelBean).toString();
		System.out.println("String--json内容" + bodyContent);
		short bodySize = countBodySize(bodyContent);
		sendHeader(dos, iTaskCode10048, bodySize);// 84
		dos.write(bodyContent.getBytes("UTF-8"));

		sendCheckCoded(dos, bodyContent.getBytes("UTF-8"));
	}

	/**
	 * 读取投入品JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recPutStuffJson(DataInputStream dis) throws IOException {
		debugLog(iTaskCode10048, "获取接收的数据");
		recHeader(dis);
		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);
		debugLog(iTaskCode10048, jsonData);

		if (!StrUtils.isEmpty(jsonData)) {
			putStuff = gson.fromJson(jsonData, Prot10048PutStuffBean.class);
		}
		recCheckCode(dis);
	}

}
