package com.steering.mes.mcp.Mch;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;

public class FrmMchControlLog extends DBCommon {

	/***
	 * 查询加工日志表
	 * 
	 * @param parm
	 * @param plineCode
	 * @param startTim
	 *            当天
	 * @param endTim
	 *            前一天
	 * @return
	 */
	public CoreReturnObject doQueryPlan(String startTim, String endTim,
			String bc, String bz, String plineCode, String conFalg) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMchControlLog.doQueryPlan");
		if (!bc.equals("")) {
			sql += " AND T.PRO_BC='" + bc + "' AND T.PROC_DATE = '" + startTim
					+ "'";
		}

		if (!plineCode.equals("")) {
			sql += "  AND T.PLINE_CODE ='" + plineCode + "'";
		}
		if (!conFalg.equals("")) {
			sql += " AND T.CONFIRM_FLAG ='" + conFalg + "'";
		}
		sql += " ORDER BY T.LOGS_BEG,T.LOGS_END ASC";
		return this.getData(sql);
	}

	/***
	 * 查询加工日志表停机记录
	 * 
	 * @param parm
	 * @param plineCode
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doStopQuery(String startTim, String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMchControlLog.doStopQuery");
		if (!startTim.equals("")) {
			sql += " AND T.PROC_DATE='" + startTim + "'";
		}
		if (!plineCode.equals("")) {
			sql += "  AND T.PLINE_CODE ='" + plineCode + "'";
		}
		sql += " ORDER BY T.LOGS_BEG ASC";
		return this.getData(sql);
	}

	/***
	 * 查询调度生产运行停机时间
	 * 
	 * @param parm
	 * @param plineCode
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doMinutesQuery(String startTim, String days) {
		String sql = "";
		try {
			int day = Integer.parseInt(days);
			sql += "SELECT";
			for (int i = 0; i < day; i++) {
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
				Date strat;
				String systime = "";

				strat = dateFormat.parse(startTim + String.valueOf(i + 1));
				systime = dateFormat.format(strat);
				String dayd = "DAY" + String.valueOf(i + 1);
				sql += "  (SELECT NVL(SUM(CEIL((TO_DATE(T.LOGS_END, 'YYYY-MM-DD HH24:MI') - "
						+ "TO_DATE(T.LOGS_BEG, 'YYYY-MM-DD HH24:MI')) * 24*60)),0) SUMMINUTES "
						+ "FROM VRP_CONTROL_LOG T WHERE T.PROC_DATE = '"
						+ systime
						+ "'"
						+ " AND T.PLINE_CODE = T1.PLINE_CODE "
						+ " AND T.CONFIRM_FLAG = '1'  "
						+ " AND T.PRO_TYPE = '2') " + dayd + ",";
			}
			sql += " (SELECT T2.PLINE_NAME FROM COM_BASE_PLINE T2 "
					+ " WHERE T2.PLINE_CODE = T1.PLINE_CODE  "
					+ " AND T2.VALIDFLAG = '1') PLINE_NAME";
			sql += " FROM VRP_CONTROL_LOG T1  WHERE T1.BAL_YEAR_MONTH = '"
					+ startTim + "' "
					+ " AND T1.CONFIRM_FLAG = '1'  GROUP BY T1.PLINE_CODE";

			sql += " ORDER BY PLINE_NAME ASC";
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return this.getData(sql);
	}

	/***
	 * 查询生产详情
	 * 
	 * @param startTim
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryPlanPro(String startTim, String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMchControlLog.doQueryPlanPro");
		if (!startTim.equals("")) {
			sql += "  AND T.PROC_DATE = '" + startTim + "'";
		}
		if (!plineCode.equals("")) {
			sql += "  AND T.PLINE_CODE ='" + plineCode + "'";
		}
		sql += " ORDER BY T.LOGS_BEG,T.LOGS_END ASC";
		return this.getData(sql);
	}

	/***
	 * 查询月生产详情
	 * 
	 * @param startTim
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doMonthQueryPlanPro(String startTim,
			String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMchControlLog.doQueryPlanPro");
		if (!startTim.equals("")) {
			sql += "  AND T.BAL_YEAR_MONTH = '" + startTim + "'";
		}
		if (!plineCode.equals("")) {
			sql += "  AND T.PLINE_CODE ='" + plineCode + "'";
		}
		sql += " ORDER BY T.LOGS_BEG,T.LOGS_END ASC";
		return this.getData(sql);
	}

	/***
	 * 统计生产班实绩
	 * 
	 * @param parm
	 * @param plineCode
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doQueryActual(String startTim, String bc,
			String plineCode) {
		String sql = "";
		sql = CoreXmlSqlParsersFactory.getSql("FrmMchControlLog.doQueryActual");
		return this.getData(sql, new Object[] { startTim, bc, startTim, bc,
				plineCode, startTim, bc, plineCode, startTim, bc, plineCode,
				startTim, bc, plineCode, startTim, bc, plineCode });
	}

	/***
	 * 统计生产日实绩
	 * 
	 * @param parm
	 * @param plineCode
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doQueryDayActual(String startTim, String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMchControlLog.doQueryDayActual");
		return this.getData(sql, new Object[] { startTim, startTim, plineCode,
				startTim, plineCode, startTim, plineCode, startTim, plineCode,
				startTim, plineCode });
	}

	/***
	 * 统计生产月实绩
	 * 
	 * @param parm
	 * @param plineCode
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doQueryMonthActual(String startTim, String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMchControlLog.doQueryMonthActual");
		return this.getData(sql, new Object[] { startTim, startTim, plineCode,
				startTim, plineCode, startTim, plineCode, startTim, plineCode,
				startTim, plineCode });
	}

	/**
	 * 查询停机分类
	 * 
	 * @param sortCode
	 * @return
	 */
	public CoreReturnObject getComBaseInfo(String sortCode) {
		return this.getDataBySqlID("FrmMchControlLog.getComBaseInfo",
				new Object[] { sortCode });
	}

	/***
	 * 新增加工日志
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject addMilControlLog(ArrayList<String> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String systime = dateFormat.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String addsql = CoreXmlSqlParsersFactory
					.getSql("FrmMchControlLog.addMilControlLog");// 新增日志
			PreparedStatement pstaControl = connection.prepareStatement(addsql);

			String stratime = parm.get(0);
			String endtime = parm.get(1);
			String name = parm.get(2);
			String bc = parm.get(3);
			String bz = parm.get(4);
			// int count = 0;
			// String sqlloading = CoreXmlSqlParsersFactory
			// .getSql("FrmMchControlLog.queryPlanJg");
			// PreparedStatement psta = connection.prepareStatement(sqlloading);
			// psta.setObject(1, stratime);
			// // psta.setObject(1, endtime);
			// ResultSet rs = psta.executeQuery();
			// if (rs.next()) {
			// count = Integer.parseInt(rs.getString("COUNTS"));
			// }
			// rs.close();
			// psta.close();
			// if (count != 0) {
			// cro.setV_errMsg("该新增的记录已存在！");
			// return cro;
			// }
			pstaControl.setObject(1, stratime);
			pstaControl.setObject(2, endtime);
			pstaControl.setObject(3, systime);
			pstaControl.setObject(4, name);
			pstaControl.setObject(5, bc);
			pstaControl.setObject(6, bz);
			pstaControl.setObject(7, parm.get(5));
			pstaControl.addBatch();
			pstaControl.executeBatch();
			pstaControl.close();

			// 修改MIL_CONTROL_LOG :Proc_Date
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmMchControlLog.UpdataPROCDATE");
			PreparedStatement ps = connection.prepareStatement(updateSql);
			ps.executeUpdate();
			ps.close();

			cro.setV_errMsg("新增成功!");
			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/***
	 * 一键处理新增加工日志
	 * 
	 * @param parm
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject oneKeyProcesMilControlLog(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String systime = dateFormat.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String addsql = CoreXmlSqlParsersFactory
					.getSql("FrmMchControlLog.OneKeyProcesMilControlLog");// 一键处理新增加工日志
			PreparedStatement pstaControl = connection.prepareStatement(addsql);
			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String stratime = list.get(0);
				String endtime = list.get(1);
				String name = list.get(2);
				String bc = list.get(3);
				String bz = list.get(4);
				// int count = 0;
				// String sqlloading = CoreXmlSqlParsersFactory
				// .getSql("FrmMchControlLog.queryPlanJg");
				// PreparedStatement psta = connection
				// .prepareStatement(sqlloading);
				// psta.setObject(1, stratime);
				// // psta.setObject(1, endtime);
				// ResultSet rs = psta.executeQuery();
				// if (rs.next()) {
				// count = Integer.parseInt(rs.getString("COUNTS"));
				// }
				// rs.close();
				// psta.close();
				// if (count != 0) {
				// cro.setV_errMsg("该新增的记录已存在！");
				// return cro;
				// }
				pstaControl.setObject(1, stratime);
				pstaControl.setObject(2, endtime);
				pstaControl.setObject(3, systime);
				pstaControl.setObject(4, name);
				pstaControl.setObject(5, bc);
				pstaControl.setObject(6, bz);
				pstaControl.setObject(7, list.get(5));
				pstaControl.setObject(8, "2");
				pstaControl.setObject(9, "500603");
				pstaControl.setObject(10, "50060302");
				pstaControl.setObject(11, "5006030201");
				pstaControl.addBatch();
			}
			pstaControl.executeBatch();
			pstaControl.close();

			// 修改MIL_CONTROL_LOG :Proc_Date
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmMchControlLog.UpdataPROCDATE");
			PreparedStatement ps = connection.prepareStatement(updateSql);
			ps.executeUpdate();
			ps.close();

			cro.setV_errMsg("一键处理成功!");
			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/***
	 * 一键处理修改加工日志
	 * 
	 * @param parm
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject oneKeyUpdateMilControlLog(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		// Date now = new Date();
		// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		// String systime = dateFormat.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String addsql = CoreXmlSqlParsersFactory
					.getSql("FrmMchControlLog.OneKeyUpdateMilControlLog");// 一键处理修改加工日志
			PreparedStatement pstaControl = connection.prepareStatement(addsql);
			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				// String stratime = list.get(0);
				String endtime = list.get(2);
				// String name = list.get(3);
				// String bc = list.get(4);
				// String bz = list.get(5);
				//               
				pstaControl.setObject(1, endtime);
				pstaControl.setObject(2, list.get(7));
				pstaControl.addBatch();
			}
			pstaControl.executeBatch();

			cro.setV_errMsg("一键处理成功!");
			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 修改调度日志维护第一条数据时间
	 * 
	 * @param logsEnd
	 * @param ControlId
	 * @return
	 */
	public CoreReturnObject updateOneDate(String logsEnd, String ControlId) {
		String sql = "";
		sql = "UPDATE VRP_CONTROL_LOG T SET T.LOGS_BEG = '" + logsEnd
				+ "' WHERE T.CONTROL_ID = '" + ControlId + "'";
		return this.setData(sql);
	}

	/***
	 * 修改调度日志
	 * 
	 * @param parm
	 * @param stationId
	 * @param colGroup
	 * @param colOrder
	 * @return
	 */
	public CoreReturnObject updateMilControlLog(
			ArrayList<ArrayList<String>> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHmm");
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmMchControlLog.updateControlLog");// 修改调度日志
			PreparedStatement updatePsta = connection
					.prepareStatement(updateSql);
			// String updateSqlUp = CoreXmlSqlParsersFactory
			// .getSql("FrmMchControlLog.updateControlLogUp");// 修改选中行上一条数据
			// PreparedStatement updatePstaUp = connection
			// .prepareStatement(updateSqlUp);
			// String updateSqlDown = CoreXmlSqlParsersFactory
			// .getSql("FrmMchControlLog.updateControlLogDown");// 修改选中行下一条数据
			// PreparedStatement updatePstaDown = connection
			// .prepareStatement(updateSqlDown);
			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String stratTime = list.get(0);
				String endTime = list.get(1);
				String plineNo = list.get(2);
				String stopType = list.get(3);
				String stopReasno = list.get(4);
				String qualifiedNum = list.get(5);
				String qualifiedWt = list.get(6);
				String scrapNum = list.get(7);
				String scrapWt = list.get(8);
				String bc = list.get(9);
				String bz = list.get(10);
				String userId = list.get(11);
				String stopSamll = list.get(12);
				String memo = list.get(13);
				// 查询确认状态
				String confiem = "";
				String sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmMchControlLog.queryContfirm");
				PreparedStatement psta = connection
						.prepareStatement(sqlloading);
				psta.setObject(1, userId);
				ResultSet rs = psta.executeQuery();
				if (rs.next()) {
					confiem = rs.getString("CONFIRM_FLAG");
				}
				rs.close();
				psta.close();

				if (confiem.equals("1")) {
					cro.setV_errMsg("已确认不能进行此操作!");
					return cro;
				}
				// 查询修改行记录
				// String logsbeg = "";
				// String logsend = "";
				// sqlloading = CoreXmlSqlParsersFactory
				// .getSql("FrmMchControlLog.queryTime");
				// psta = connection
				// .prepareStatement(sqlloading);
				// psta.setObject(1, userId);
				// rs = psta.executeQuery();
				// if (rs.next()) {
				// logsbeg = rs.getString("LOGS_BEG");
				// logsend = rs.getString("LOGS_END");
				// }
				// rs.close();
				// psta.close();
				// int stratday = 0;
				// int strathours = 0;
				// int stratminutes = 0;
				// long[] time = getDistanceTimes(logsbeg, stratTime);
				// if (time.length != 0) {
				// stratday = (int) time[0];
				// strathours = (int) time[1];
				// stratminutes = (int) time[2];
				// }

				// int endday = 0;
				// int endhours = 0;
				// int endminutes = 0;
				// long[] time1 = getDistanceTimes(logsend, endTime);
				// if (time1.length != 0) {
				// endday = (int) time1[0];
				// endhours = (int) time1[1];
				// endminutes = (int) time1[2];
				// }
				// 查询修改行上一行记录
				// String stratlogsbeg = "";
				// String stratlogsend = "";
				// String stratUserId = "";
				// sqlloading = CoreXmlSqlParsersFactory
				// .getSql("FrmMchControlLog.queryStratTime");
				// psta = connection.prepareStatement(sqlloading);
				// psta.setObject(1, userId);
				// psta.setObject(2, bc);
				// psta.setObject(3, bz);
				// rs = psta.executeQuery();
				// if (rs.next()) {
				// stratlogsbeg = rs.getString("LOGS_BEG");
				// stratlogsend = rs.getString("LOGS_END");
				// stratUserId = rs.getString("CONTROL_ID");
				//
				// Date dt = dateAddSecond(sdf.parse(stratlogsend), stratday,
				// strathours, stratminutes);
				// boolean flag = dt.before(sdf.parse(stratlogsbeg));
				// if (flag) {
				// cro.setV_errMsg("选中行上一行开始时间大于等于结束时间，请先修改上一行数据!");
				// return cro;
				// }
				// updatePstaUp.setObject(1, sd.format(dt));
				// updatePstaUp.setObject(2, stratUserId);
				// updatePstaUp.addBatch();
				// updatePstaUp.executeBatch();
				// }
				// rs.close();
				// psta.close();

				// 查询修改行下一行记录
				// String endlogsbeg = "";
				// String endlogsend = "";
				// String endUserId = "";
				// sqlloading = CoreXmlSqlParsersFactory
				// .getSql("FrmMchControlLog.queryEndTime");
				// psta = connection.prepareStatement(sqlloading);
				// psta.setObject(1, userId);
				// psta.setObject(2, bc);
				// psta.setObject(3, bz);
				// rs = psta.executeQuery();
				// if (rs.next()) {
				// endlogsbeg = rs.getString("LOGS_BEG");
				// endlogsend = rs.getString("LOGS_END");
				// endUserId = rs.getString("CONTROL_ID");
				//
				// Date dt = dateAddSecond(sdf.parse(endlogsbeg), endday,
				// endhours, endminutes);
				// boolean flag = dt.before(sdf.parse(endlogsend));
				// if (!flag) {
				// cro.setV_errMsg("选中行下一行开始时间大于等于结束时间，请先修改下一行数据!");
				// return cro;
				// }
				// updatePstaDown.setObject(1, sd.format(dt));
				// updatePstaDown.setObject(2, endUserId);
				// updatePstaDown.addBatch();
				// updatePstaDown.executeBatch();
				// }
				// rs.close();
				// psta.close();

				updatePsta.setObject(1, sd.format(sdf.parse(stratTime)));
				updatePsta.setObject(2, sd.format(sdf.parse(endTime)));
				updatePsta.setObject(3, plineNo);
				updatePsta.setObject(4, stopType);
				updatePsta.setObject(5, stopReasno);
				updatePsta.setObject(6, qualifiedNum);
				updatePsta.setObject(7, qualifiedWt);
				updatePsta.setObject(8, scrapNum);
				updatePsta.setObject(9, scrapWt);
				updatePsta.setObject(10, bc);
				updatePsta.setObject(11, bz);
				updatePsta.setObject(12, stopSamll);
				updatePsta.setObject(13, memo);
				updatePsta.setObject(14, sd.format(sdf.parse(stratTime)));
				updatePsta.setObject(15, sd.format(sdf.parse(stratTime)));

				updatePsta.setObject(16, sd.format(sdf.parse(stratTime)));
				updatePsta.setObject(17, sd.format(sdf.parse(stratTime)));
				updatePsta.setObject(18, sd.format(sdf.parse(stratTime)));

				updatePsta.setObject(19, sd.format(sdf.parse(stratTime)));
				updatePsta.setObject(20, sd.format(sdf.parse(stratTime)));
				updatePsta.setObject(21, sd.format(sdf.parse(stratTime)));
				updatePsta.setObject(22, sd.format(sdf.parse(stratTime)));

				updatePsta.setObject(23, sd.format(sdf.parse(stratTime)));
				updatePsta.setObject(24, sd.format(sdf.parse(stratTime)));
				updatePsta.setObject(25, sd.format(sdf.parse(stratTime)));

				updatePsta.setObject(26, sd.format(sdf.parse(stratTime)));
				updatePsta.setObject(27, sd.format(sdf.parse(stratTime)));
				updatePsta.setObject(28, userId);

				updatePsta.addBatch();
				updatePsta.executeBatch();
			}

			cro.setV_errMsg("修改成功!");
			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/***
	 * 删除停机调度日志
	 * 
	 * @param parm
	 * @param stationId
	 * @param colGroup
	 * @param colOrder
	 * @return
	 */
	public CoreReturnObject deleteMilControlLog(
			ArrayList<ArrayList<String>> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmMchControlLog.DeleteControlLog");// 删除调度日志
			PreparedStatement updatePsta = connection
					.prepareStatement(updateSql);
			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String userId = list.get(0);

				// 查询确认状态
				String confiem = "";
				String sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmMchControlLog.queryContfirm");
				PreparedStatement psta = connection
						.prepareStatement(sqlloading);
				psta.setObject(1, userId);
				ResultSet rs = psta.executeQuery();
				if (rs.next()) {
					confiem = rs.getString("CONFIRM_FLAG");
				}
				rs.close();
				psta.close();

				if (confiem.equals("1")) {
					cro.setV_errMsg("已确认不能进行此操作!");
					return cro;
				}
				updatePsta.setObject(1, userId);
				updatePsta.addBatch();
			}
			updatePsta.executeBatch();
			cro.setV_errMsg("删除成功!");
			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/***
	 * 确认
	 * 
	 * @param parm
	 * @param count
	 * @param list1
	 * @return
	 */
	public CoreReturnObject confirmMilControlLog(
			ArrayList<ArrayList<String>> parm, String intCount) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmMchControlLog.confirmMilControlLog");// 修改确认
			PreparedStatement updatePsta = connection
					.prepareStatement(updateSql);

			// if (conCount != Integer.parseInt(count)) {
			// cro.setV_errMsg("当前班次班组数据不符，请刷新！");
			// return cro;
			// }
			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String name = list.get(1);
				String time = list.get(2);
				String bc = list.get(3);
				String bz = list.get(4);
				String plinCoed = list.get(5);
				String logbes = list.get(6);
				String stattime = list.get(7);
				String endtime = list.get(8);
				String userId = list.get(0);
				if (intCount.equals("0")) {
					String contId = "";
					String sql = CoreXmlSqlParsersFactory
							.getSql("FrmMchControlLog.queryCountLoag");
					PreparedStatement psta1 = connection.prepareStatement(sql);
					psta1.setObject(1, logbes);
					psta1.setObject(2, plinCoed);
					psta1.setObject(3, bc);
					psta1.setObject(4, bz);
					psta1.setObject(5, stattime);
					psta1.setObject(6, endtime);
					ResultSet rs1 = psta1.executeQuery();
					while (rs1.next()) {
						contId = rs1.getString("CONTROL_ID");

						updatePsta.setObject(1, name);
						updatePsta.setObject(2, time);
						updatePsta.setObject(3, contId);
						updatePsta.addBatch();
					}
					rs1.close();
					psta1.close();
				} else {
					// 查询确认状态
					String confiem = "";
					String sqlloading = CoreXmlSqlParsersFactory
							.getSql("FrmMchControlLog.queryContfirm");
					PreparedStatement psta = connection
							.prepareStatement(sqlloading);
					psta.setObject(1, userId);
					ResultSet rs = psta.executeQuery();
					if (rs.next()) {
						confiem = rs.getString("CONFIRM_FLAG");
					}
					rs.close();
					psta.close();

					if (confiem.equals("1")) {
						cro.setV_errMsg("已确认不能进行此操作!");
						return cro;
					}
					updatePsta.setObject(1, name);
					updatePsta.setObject(2, time);
					updatePsta.setObject(3, userId);
					updatePsta.addBatch();

				}
			}
			updatePsta.executeBatch();

			cro.setV_errMsg("确认成功!");
			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/***
	 * 取消确认
	 * 
	 * @param parm
	 * @param count
	 * @param list1
	 * @return
	 */
	public CoreReturnObject upConfirmMilControlLog(
			ArrayList<ArrayList<String>> parm, String intCount) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmMchControlLog.UpConfirmMilControlLog");// 取消确认
			PreparedStatement updatePsta = connection
					.prepareStatement(updateSql);
			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String userId = list.get(0);
				String bc = list.get(1);
				String bz = list.get(2);
				String plinCode = list.get(3);
				String logbes = list.get(4);
				String stattime = list.get(5);
				String endtime = list.get(6);
				if (intCount.equals("0")) {
					String usCount = "";
					String sql = CoreXmlSqlParsersFactory
							.getSql("FrmMchControlLog.queryCountLoag1");
					PreparedStatement psta1 = connection.prepareStatement(sql);
					psta1.setObject(1, logbes);
					psta1.setObject(2, plinCode);
					psta1.setObject(3, bc);
					psta1.setObject(4, bz);
					psta1.setObject(5, stattime);
					psta1.setObject(6, endtime);
					ResultSet rs1 = psta1.executeQuery();
					while (rs1.next()) {
						usCount = rs1.getString("CONTROL_ID");

						updatePsta.setObject(1, usCount);
						updatePsta.addBatch();
					}
					rs1.close();
					psta1.close();
				} else {
					// 查询确认状态
					String confiem = "";
					String sqlloading = CoreXmlSqlParsersFactory
							.getSql("FrmMchControlLog.queryContfirm");
					PreparedStatement psta = connection
							.prepareStatement(sqlloading);
					psta.setObject(1, userId);
					ResultSet rs = psta.executeQuery();
					if (rs.next()) {
						confiem = rs.getString("CONFIRM_FLAG");
					}
					rs.close();
					psta.close();

					if (confiem.equals("0")) {
						cro.setV_errMsg("未确认不能进行此操作!");
						return cro;
					}

					updatePsta.setObject(1, userId);
					updatePsta.addBatch();
				}
			}
			updatePsta.executeBatch();

			cro.setV_errMsg("取消确认成功!");
			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 获取修改后的时间
	 * 
	 * @param beginTime
	 * @param day
	 * @param hours
	 * @param minutes
	 * @return
	 */
	public static Date dateAddSecond(Date beginTime, int day, int hours,
			int minutes) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(beginTime);
		rightNow.add(Calendar.DAY_OF_YEAR, day);
		rightNow.add(Calendar.HOUR, hours);
		rightNow.add(Calendar.MINUTE, minutes);
		Date dt = rightNow.getTime();
		return dt;
	}

	/**
	 * 两个时间相差距离多少天多少小时多少分
	 * 
	 * @param str1
	 *            时间参数 1 格式：1990-01-01 12:00
	 * @param str2
	 *            时间参数 2 格式：2009-01-01 12:00
	 * @return long[] 返回值为：{天, 时, 分}
	 */
	public static long[] getDistanceTimes(String str1, String str2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date one;
		Date two;
		long day = 0;
		long hour = 0;
		long min = 0;
		// long sec = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			// if (time1 < time2) {
			// diff = time2 - time1;
			// }
			// else {
			diff = time2 - time1;
			// }
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			// sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long[] times = { day, hour, min };
		return times;
	}

	/**
	 * 查询连轧待上料信息
	 * 
	 * @param MilPlan
	 *            作业计划实体类
	 * @param ProcessNo
	 *            工序号 1-锯切 2-环形加热炉 3-穿孔 4-连轧 5-再加热炉 6-定径 7-管排锯 8-矫直 9-ET探伤...
	 *            11-涡流探伤 12-人工质检
	 * @return CoreReturnObject
	 * @Return 0-未开始 1-工序已开始
	 */
	public CoreReturnObject getSingleMainPlan(String plineCode) {

		// String Judge_Stove_No = "Judge_Stove_No"; MIL_ROLLING_Result


		String Sql = "SELECT b.CUT_NUM,a.ID,a.SEQ,a.STOVE_NO,a.JUDGE_STOVE_NO,substr(a.JUDGE_STOVE_NO，0,6) as JUDGE_NO,a.LOT_NO,a.CONTRACT_NO,a.IMP_PROCESS,a.PLINE_CODE,a.BACK_PLAN_FLAG,";
		Sql += "CASE a.STATUS WHEN ";
		Sql += "'00' THEN '上料' WHEN '05' THEN '开始锯切' WHEN  '10'	THEN '锯切完成' WHEN  '15' THEN '入炉' WHEN '20'	THEN '出炉' WHEN '25' THEN '开始穿孔' WHEN '30'	THEN '穿孔完成' WHEN ";
		Sql += " '35' THEN '开始连轧' WHEN  '40'	THEN '连轧完成' WHEN '45' THEN '入再加热炉' WHEN '50'	THEN '出再加热炉' WHEN '55' THEN '开始定径' WHEN '60'	THEN '定径完成' WHEN ";
		Sql += " '65' THEN '开始分切' WHEN '70'	THEN '分切完成' WHEN '75' THEN '开始矫直' WHEN '80'	THEN '矫直完成 ' WHEN '85' THEN '开始探伤' WHEN '90'	THEN '探伤完成' WHEN	 '95' THEN '人工质检开始' WHEN ";
		Sql += "  '99'	THEN '入库' END AS STATUS,";
		Sql += " a.PRODUCER, a.PROCESS_STATUS, a.DB_LENGTH, a.DB_HEIGHT, a.ROLL_LENGTH, a.SAW_NUM, a.TARGET_LENGTH, a.FINISH_USER, a.TARGET_LIABRARY, a.VARIETIES, a.PIPE_SIZE, ";
		Sql += " a.BUCKLE_TYPE, a.LENGTH_RANGE, a.ROLL_STANDRAD,a.PIPE_SECTION, a.STEELCODE, a.STEELNAME, a.GRADECODE,  a.GRADENAME, a.STEEL_STANDRAD, a.TARGET, BRANCH,";
		Sql += "a.TON, a.CUT_TARGET,a.CUT_LENGTH, a.CUT_WEIGHT, a.DB_NUM, CASE a.NODE_STATUS WHEN  '00' THEN '' WHEN '10'	THEN '锯切完成' WHEN '15' THEN '入炉' WHEN '20'	THEN '出炉' WHEN ";
		Sql += " '25' THEN '开始穿孔' WHEN '30'	THEN '穿孔完成' WHEN '35' THEN '开始连轧' WHEN '40'	THEN '连轧完成' WHEN '45' THEN '入再加热炉' WHEN '50'	THEN '出再加热炉' WHEN '55' THEN '开始定径' WHEN ";
		Sql += " '60'	THEN '定径完成' WHEN '65' THEN '开始分切' WHEN '70'	THEN '分切完成' WHEN '75' THEN '开始矫直' WHEN '80'	THEN '矫直完成 ' WHEN '85' THEN '开始探伤' WHEN '90'	THEN '探伤完成' WHEN	 '96' THEN '人工质检完成' WHEN '99' THEN '交库'";
		Sql += " END AS NODE_STATUS,";
		Sql += " Nvl((select  b.NODE_FUR_FLAG from MIL_ROLLING_Result b where b.judge_stove_no = a.judge_Stove_no) ,3)AS IS_START ,";

		Sql += " TO_CHAR(a.START_TIME, 'YYYY-MM-DD HH24:MI:SS') START_TIME, TO_CHAR(a.END_TIME, 'YYYY-MM-DD HH24:MI:SS') END_TIME, TO_CHAR(a.TOM, 'YYYY-MM-DD HH24:MI:SS') TOM, TO_CHAR(a.TOC, 'YYYY-MM-DD HH24:MI:SS') TOC ";
		Sql += "   FROM MIL_PLAN   a  ,MIL_FEEDSAW_RESULT b";

		Sql += "  WHERE 1=1  ";
		Sql += "  and  a.NODE_STATUS between '00' and '30' ";
		Sql += " AND a.PLINE_CODE = '" + plineCode + "'";

		Sql += " and a.judge_stove_no = b.judge_stove_no(+)";
		Sql += " and a.id = b.Result_NO(+)";
		Sql += " AND STATUS>'00'   ";
		Sql += " AND Nvl((select b.NODE_FUR_FLAG from MIL_ROLLING_Result b where b.judge_stove_no = a.judge_Stove_no),3) = '3' ";
		Sql += " AND a.rolling_status in ('0','1')";
		Sql += "  order by   a.FEED_TIME  ,a.START_TIME";
		return this.getData(Sql);
	}
}
