package com.steering.mes.mcp.heatTreatment;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

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

import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.FrmMchPersonentity;

public class HttSetPline extends DBCommon {
	public CoreReturnObject doQuery(String heatno, String pline) {

		String sql = "";
		sql = CoreXmlSqlParsersFactory.getSql("HttOfficeD.doQuery");

		if (!heatno.equals("")) {
			sql += " AND T.HEAT_PLAN_NO ='" + heatno + "'";
		}
		if (!pline.equals("")) {
			sql += " AND T.PLINE_NAME ='" + pline + "'";
		}
		sql += " ORDER BY T.PLINE_NAME";
		return this.getData(sql);
	}

	// 查询产线对应的工序
	public CoreReturnObject query(String pline) {

		String sql = "";
		sql = CoreXmlSqlParsersFactory.getSql("HttSetPline.Query");
		if (!pline.equals("")) {
			sql += " AND T1.PLINE_CODE ='" + pline + "'";
		}
		return this.getData(sql);
	}

	/**
	 * 查询离线去向
	 * 
	 * @param resultNo
	 * @return
	 */
	public CoreReturnObject getQuerydirect(ArrayList<String> list) {
		return this.getDataBySqlID("HttSetPline.getQuerydirect", new Object[] {
				list.get(0) });
	}

	/**
	 * 新增工序点
	 * 
	 * @param load
	 * @return
	 */
	public CoreReturnObject addPlineGx(ArrayList<Object> pram) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat matter2 = new SimpleDateFormat("yyyyMMddHHmm");
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("HttSetPline.addOfficeD");// 工序点新增
			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			String sqlDET = CoreXmlSqlParsersFactory
					.getSql("HttSetPline.detOfficeD");// 删除相同的产线
			PreparedStatement patDET = connection.prepareStatement(sqlDET);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				// 删除
				patDET.setObject(1, list.get(0));
				patDET.addBatch();
				patDET.executeBatch();
				// 新增
				patAdd.setObject(1, list.get(0));
				patAdd.setObject(2, list.get(1));
				patAdd.setObject(3, list.get(2));
				patAdd.setObject(4, list.get(3));
				patAdd.addBatch();
				patAdd.executeBatch();
			}
			core.setV_errMsg("新增成功！");
			core.setResult(1);
			connection.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return core;
	}

	/**
	 * 删除
	 * 
	 * @param load
	 * @return
	 */
	public CoreReturnObject detPlineGx(ArrayList<Object> pram) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat matter2 = new SimpleDateFormat("yyyyMMddHHmm");
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String sqlDET = CoreXmlSqlParsersFactory
					.getSql("HttSetPline.detOfficeDirect");// 删除相同的产线
			PreparedStatement patDET = connection.prepareStatement(sqlDET);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				// 删除
				patDET.setObject(1, list.get(0));
				patDET.addBatch();
				patDET.executeBatch();
			}
			core.setV_errMsg("删除成功！");
			core.setResult(1);
			connection.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return core;
	}
}
