package com.steering.pss.plnsaleord.processOrder;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.base.PlnHelper;
import com.steering.pss.plnsaleord.processOrder.entity.PlnPassmodelEntity;

/**
 * 孔型计划
 * 
 * @author peng
 * 
 */
public class FrmPlnPassmodel extends DBCommon {
	/**
	 * 初始化产线
	 * 
	 * @return
	 */
	public CoreReturnObject initPline() {
		return this.getDataBySqlID("FrmPlnPassmodel.initPline");
	}

	/**
	 * 初始化孔型
	 * 
	 * @return
	 */
	public CoreReturnObject initPassModel(String plineCode) {
		Object[] params=new Object[]{plineCode};
		return this.getDataBySqlID("FrmPlnPassmodel.initPassModel",params);
	}

	/**
	 * 查询
	 * 
	 * @param plineCode
	 * @param year
	 * @return
	 */
	public CoreReturnObject queryModel(String plineCode, String year) {
		List<String> params = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory.getSql("FrmPlnPassmodel.query");
		if (plineCode.length() > 0) {
			params.add(plineCode);
			sql += " and t.pline_code=? ";
		}
		if (year.length() > 0) {
			params.add(year);
			sql += " and t.PRO_MONTH=? ";
		}
		sql += " order by t.PLINE_CODE,t.GX_PRO_SEQ";
		return this.getData(sql, params.toArray());
	}

	/**
	 * 新增
	 * 
	 * @param jsonStr
	 * @return
	 */
	public CoreReturnObject add(String jsonStr) {
		PlnPassmodelEntity entity = PlnHelper.jsonToEntity(
				PlnPassmodelEntity.class, jsonStr);
		String sql = "";
		try {
			Connection con = this.getDao(CoreConstantData.sysbeanstr)
					.getConnection();
			String gxProSeq = PlnHelper.getParam(con,
					"FrmPlnPassmodel.getMaxGxProSeq",
					new String[] { entity.getPlineCode() }, "GX_PRO_SEQ");
			entity.setGxProSeq(gxProSeq);
			Date now = new Date();
			SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
			String year = sf.format(now);
			String id = entity.getPlineCode() + year;
			String passPlanId = PlnHelper.getParam(con,
					"FrmPlnPassmodel.getMaxPassPlanId",
					new String[] { id, id }, "PASSM_PLAN_ID");
			entity.setPassmPlanId(passPlanId);
			sql = CoreXmlSqlParsersFactory.getSql("FrmPlnPassmodel.add");

			sql = EntityTool.fillSqlByEntity(sql, entity);
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return this.setData(sql);
	}

	/**
	 * 修改
	 * 
	 * @param jsonStr
	 * @return
	 */
	public CoreReturnObject update(String jsonStr) {
		CoreReturnObject core = new CoreReturnObject();
		core.setResult(false);
		Connection con = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			PlnPassmodelEntity entity = PlnHelper.jsonToEntity(
					PlnPassmodelEntity.class, jsonStr);
			int gxProSeq = Integer.parseInt(entity.getGxProSeq());
			String planTimeE = PlnHelper.getParam(
					con,
					"FrmPlnPassmodel.getTimeBySeq",
					new String[] { entity.getPlineCode(),
							String.valueOf(gxProSeq - 1) }, "END_TIME");
			if (!planTimeE.equals("")) {
				if (planTimeE.compareTo(entity.getBeginTime()) > 0) {
					core.setV_errMsg("开始时间不能大于前生产顺序的结束时间!");
					return core;
				}
			}
			String planTimeB = PlnHelper.getParam(
					con,
					"FrmPlnPassmodel.getTimeBySeq",
					new String[] { entity.getPlineCode(),
							String.valueOf(gxProSeq + 1) }, "BEGIN_TIME");
			if (!planTimeB.equals("")) {
				if (entity.getEndTime().compareTo(planTimeB) > 0) {
					core.setV_errMsg("结束时间不能小于后生产顺序的开始时间!");
					return core;
				}
			}
			PlnHelper.deleteOrUpdateByEntity(con, "FrmPlnPassmodel.update",
					entity);

			core.setV_errMsg("修改成功!");
			core.setResult(true);
			con.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return core;
	}

	/**
	 * 删除
	 * 
	 * @param jsonStr
	 * @return
	 */
	public CoreReturnObject delete(String jsonStr) {
		CoreReturnObject core = new CoreReturnObject();
		core.setResult(false);
		Connection con = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			PlnPassmodelEntity entity = PlnHelper.jsonToEntity(
					PlnPassmodelEntity.class, jsonStr);
			PlnHelper.deleteOrUpdate(con, "FrmPlnPassmodel.delete",
					new Object[] { entity.getPassmPlanId() });
			PlnHelper.deleteOrUpdate(
					con,
					"FrmPlnPassmodel.updateSeq",
					new Object[] { -1, entity.getPlineCode(),
							entity.getGxProSeq() });
			core.setV_errMsg("删除成功!");
			core.setResult(true);
			con.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return core;
	}

	/**
	 * 查询最大结束时间
	 * 
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject getMaxEndTime(String plineCode, String yearMonth) {
		Object[] params = new Object[] { plineCode,yearMonth };
		return this.getDataBySqlID("FrmPlnPassmodel.getMaxEndTime", params);
	}
}
