package com.steering.pss.plnsaleord.jggxpln;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.pss.plnsaleord.steelMarkingPlan.FrmSteelMakingPlanChange;

/**
 * 接箍合同二级工艺
 * 
 * @author 2018.10.15 tjg
 * 
 */
public class FrmJgSecondProcess extends DBCommon {

	/**
	 * 查询接箍合同
	 * 
	 * @return
	 */
	public CoreReturnObject queryOrder(String jgOrderNo, String orderNo,
			String codeJg, String[] jgStart, String[] pcStart) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmJgSecondProcess.queryJgOrder");
		if (!jgOrderNo.equals("")) {
			sql += " AND T2.JG_ORDERNO LIKE '%" + jgOrderNo + "%'";
		}
		if (!orderNo.equals("")) {
			sql += " AND T2.ORDER_NO||'/'||T2.ORDER_SEQ LIKE '%" + orderNo
					+ "%'";
		}
		if (!codeJg.equals("")) {
			sql += " AND T2.CODE_JG='" + codeJg + "'";
		}
		if (jgStart.length > 0) {
			sql += CoreConverter.toSqlConditionString("T2.STATUS", jgStart);
		}
		if (pcStart.length > 0) {
			sql += CoreConverter.toSqlConditionString("T2.EXE_STATUS", pcStart);
		}
		sql += " ORDER BY T2.TIME_CP ASC";
		return this.getData(sql);
	}

	/**
	 * 根据订单号，排产序号查询炉计划
	 * 
	 * @param heatPlanNo
	 * @return
	 */
	public CoreReturnObject queryPlanStovePlanId(String proPlanId) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmJgSecondProcess.queryPlanStoveReadyH");
		sql += "  AND T.PRO_PLAN_ID ='" + proPlanId + "'";
		sql += " ORDER BY T.GX_PRO_SEQ,T.HEAT_PLAN_NO ASC";
		return this.getData(sql);
	}
	/**
	 * 查询接箍二级工艺备注
	 * 
	 * @param heatPlanNo
	 * @return
	 */
	public CoreReturnObject initMemo() {
		String sql ="SELECT T.BASECODE,T.BASENAME FROM COM_BASE_INFO T WHERE T.SORTCODE='7017'";
		return this.getData(sql);
	}
	/**
	 * 查询接箍订单(7+4)与二级工艺关系
	 * 
	 * @param key
	 * @param validflag
	 * @return
	 */
	public CoreReturnObject doQueryCraftSeconed(String key, String validflag) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmJgSecondProcess.doQueryCraftSeconed");
		// if(!key.equals("")){
		//			
		// }
		return this.getData(sql, new Object[] { key });
	}
	/**
	 * 查询合同接箍一级备注
	 * @return
	 */
	public CoreReturnObject doQueryOrderJgMemo(String orderNoSeq) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmJgSecondProcess.doQueryOrderJgMemo");
		return this.getData(sql, new Object[] { orderNoSeq });
	}
	/**
	 * 查询最大编号
	 * @return
	 */
	public CoreReturnObject doQueryNumBeiMax(String numberJg) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmJgSecondProcess.doQueryNumBeiMax");
		return this.getData(sql,new Object[] { numberJg,numberJg });
	}
	/**
	 * 查询接箍订单(7+4)与二级工艺关系最大序号
	 * 
	 * @param key
	 * @param validflag
	 * @return
	 */
	public CoreReturnObject doMaxCraftSeq(String key) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmJgSecondProcess.doMaxCraftSeq");
		return this.getData(sql, new Object[] { key });
	}

	/***
	 * 新增
	 * 
	 * @param jslist
	 * @param userName
	 * @return
	 */
	public CoreReturnObject doAddCraft(ArrayList<ArrayList<String>> jslist,
			String userName) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			// ResultSet rs = null;
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
			// 循环炉计划信息
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String key = list.get(0);
				String craftNo2 = list.get(1);
				String craftSeq = list.get(2);
				String craftPath = list.get(3);
				String Memo = list.get(4);
				String Memo1 = list.get(5);
				String content = list.get(6);
				String Memo2 = list.get(7);
				String numberJg = list.get(8);
				if (craftNo2.equals("")) {
					cro.setV_errMsg("二级工艺文件不能为空!");
					return cro;
				}
				// 新增接箍二级工艺与排产订单(7+4)对应关系数据
				String updateMatBut = CoreXmlSqlParsersFactory
						.getSql("FrmJgSecondProcess.doAddCraft");
				ps = conn.prepareStatement(updateMatBut);
				ps.setObject(1, key);
				ps.setObject(2, craftNo2);
				ps.setObject(3, craftSeq);
				ps.setObject(4, craftPath);
				ps.setObject(5, Memo);
				ps.setObject(6, userName);
				ps.setObject(7, dateTime);
				ps.setObject(8, Memo1);
				ps.setObject(9, content);
				ps.setObject(10, Memo2);
				ps.setObject(11, numberJg);
				ps.executeUpdate();
				ps.close();

				// 作废接箍二级工艺与排产订单(7+4)对应关系数据
				String update = "UPDATE CRAFT_FILE_JG2 SET DELETE_NAME  = ?,DELETE_TIME= TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),VALIDFLAG='0' WHERE CRAFT_SEQ2 = ? AND KEY=?";
				ps = conn.prepareStatement(update);
				ps.setObject(1, userName);
				ps.setObject(2, dateTime);
				ps.setObject(3, Integer.parseInt(craftSeq) - 1);
				ps.setObject(4, key);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("新增成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/***
	 * 修改
	 * 
	 * @param jslist
	 * @param userName
	 * @return
	 */
	public CoreReturnObject doUpdate(ArrayList<ArrayList<String>> jslist,
			String userName) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			// ResultSet rs = null;
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
			// 循环炉计划信息
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);

				String craftNo2 = list.get(0);
				String craftPath = list.get(1);
				String Memo = list.get(2);
				String Memo1 = list.get(3);
				String content = list.get(4);
				String pk = list.get(5);
				String Memo2 = list.get(6);
				String numberJg = list.get(7);
				if (craftNo2.equals("")) {
					cro.setV_errMsg("二级工艺文件不能为空!");
					return cro;
				}
				// 修改接箍二级工艺与排产订单(7+4)对应关系数据
				String updateMatBut = "UPDATE CRAFT_FILE_JG2 SET CRAFT_NO_JG2 = ?,UPDATE_NAME  = ?,UPDATE_TIME= TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),CRAFT_PATH= ?, MEMO= ?,MEMO1= ?,CONTENT=?,MEMO2=?,NUMBERS=? WHERE PK = ?";
				ps = conn.prepareStatement(updateMatBut);
				ps.setObject(1, craftNo2);
				ps.setObject(2, userName);
				ps.setObject(3, dateTime);
				ps.setObject(4, craftPath);
				ps.setObject(5, Memo);
				ps.setObject(6, Memo1);
				ps.setObject(7, content);
				ps.setObject(8, Memo2);
				ps.setObject(9, numberJg);
				ps.setObject(10, pk);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("修改成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/***
	 * 作废/恢复
	 * 
	 * @param jslist
	 * @param userName
	 * @return
	 */
	public CoreReturnObject doDelete(ArrayList<ArrayList<String>> jslist,
			String userName, String falg) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			// ResultSet rs = null;
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
			// 循环炉计划信息
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String pk = list.get(0);
				if (falg.equals("1")) {
					userName = "";
					dateTime = "";
				}
				// 作废/恢复接箍二级工艺与排产订单(7+4)对应关系数据
				String updateMatBut = "UPDATE CRAFT_FILE_JG2 SET DELETE_NAME  = ?,DELETE_TIME= TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),VALIDFLAG=? WHERE PK = ?";
				ps = conn.prepareStatement(updateMatBut);
				ps.setObject(1, userName);
				ps.setObject(2, dateTime);
				ps.setObject(3, falg);
				ps.setObject(4, pk);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/***
	 * 确认/取消工艺
	 * 
	 * @param jslist
	 * @param userName
	 * @return
	 */
	public CoreReturnObject doConfirm(ArrayList<ArrayList<String>> jslist,
			String userName, String flag) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			// ResultSet rs = null;
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
			// 循环炉计划信息
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String proPlanId = list.get(0);

				if (proPlanId.equals("")) {
					cro.setV_errMsg("该接箍合同未生成接箍排产订单,不能确认/取消工艺!");
					return cro;
				}

				int coutNum = 0;// 接箍排产订单7+4循环次数

				// 查询接箍排产订单7+4记录
				String sql = "SELECT C.KEY FROM PLN_GX_JG_PROP_C C WHERE C.PRO_PLAN_ID=?";
				ps = conn.prepareStatement(sql);
				ps.setObject(1, proPlanId);
				rs = ps.executeQuery();
				while (rs.next()) {
					String key = rs.getString("KEY");

					coutNum++;

					int count = 0;
					// 查询接箍排产订单7+4是否存在有效工艺
					String sqlCraft = "SELECT COUNT(*) COUNT FROM CRAFT_FILE_JG2 T WHERE T.KEY =? AND T.VALIDFLAG='1'";
					PreparedStatement ps1 = conn.prepareStatement(sqlCraft);
					ps1.setObject(1, key);
					ResultSet rs1 = ps1.executeQuery();
					if (rs1.next()) {
						count = rs1.getInt("COUNT");
					}
					rs1.close();
					ps1.close();

					if (count == 0) {
						cro
								.setV_errMsg("该接箍合同对应接箍排产订单7+4,有未生成二级工艺或二级工艺无效,不能确认/取消确认!");
						return cro;
					}
				}
				rs.close();
				ps.close();

				// if(coutNum>1){
				// cro.setV_errMsg("该接箍合同未生成接箍排产订单,不能确认/取消工艺");
				// }
				if (flag.equals("1")) {
					// 修改接箍排产订单确认标志
					String updatePro = "UPDATE PLN_GX_JG_PROP_S T SET T.IS_PLAN_OK='1',T.ISSUED_MAN=?,T.ISSUED_TIME=TO_DATE(?,'YYYY-MM-DD HH24:MI;SS') WHERE T.PRO_PLAN_ID=?";
					ps = conn.prepareStatement(updatePro);
					ps.setObject(1, userName);
					ps.setObject(2, dateTime);
					ps.setObject(3, proPlanId);
					ps.executeUpdate();
					ps.close();
				} else {
					// 取消确认接箍排产订单确认标志
					String updatePro = "UPDATE PLN_GX_JG_PROP_S T SET T.IS_PLAN_OK='0',T.LAST_UNDOWN_MAN=?,T.LAST_UNDOWN_TIME=TO_DATE(?,'YYYY-MM-DD HH24:MI;SS') WHERE T.PRO_PLAN_ID=?";
					ps = conn.prepareStatement(updatePro);
					ps.setObject(1, userName);
					ps.setObject(2, dateTime);
					ps.setObject(3, proPlanId);
					ps.executeUpdate();
					ps.close();
				}
			}
			cro.setV_errMsg("成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}
}