package com.steering.mes.mcp.coup;

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

import com.steering.comm.db.DBCommon;

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

public class FrmMachine extends DBCommon {
	/***
	 * 工序点
	 * 
	 * @return
	 */
	public CoreReturnObject getQueryGx(String gxName) {
		return this.getDataBySqlID("FrmMachine.getQueryGx",
				new Object[] { gxName });
	}

	/***
	 * 工序点
	 * 
	 * @return
	 */
	public CoreReturnObject getQuerySoue(String gxName) {
		return this.getDataBySqlID("FrmMachine.getQuerySoue",
				new Object[] { gxName });
	}

	/***
	 * 产线
	 * 
	 * @return
	 */
	public CoreReturnObject getQueryPline(String gxName) {
		return this.getDataBySqlID("FrmMachine.getQueryPline",
				new Object[] { gxName });
	}

	/***
	 * 查询机床维护
	 * 
	 * @return
	 */
	public CoreReturnObject getQueryPhosphateItem(ArrayList<String> pram) {
		return this.getDataBySqlID("FrmMachine.getQueryPhosphateItem",
				pram.toArray());
	}

	public CoreReturnObject queryMachine(ArrayList<String> pram) {
		return this.getDataBySqlID("FrmMachine.queryMachine", pram.toArray());
	}

	/***
	 * 查询机床操作人员维护
	 * 
	 * @return
	 */
	public CoreReturnObject getQueryOperation(String plineCode,
			String machineNo, String proessCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMachine.getQueryOperation");
		return this.getData(sql, new Object[] { plineCode, machineNo,
				proessCode });
	}

	/***
	 * 新增机床对应用户
	 * 
	 * @return
	 */
	public CoreReturnObject getAddPhosphateItem(
			ArrayList<ArrayList<String>> pram,
			ArrayList<ArrayList<String>> pram1, 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;
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String machineNo = list.get(0);
				String machineNm = list.get(1);
				String userName = list.get(2);
				String userTime = list.get(3);
				String proessCode = list.get(4);
				// 新增机床信息
				String addMach = CoreXmlSqlParsersFactory
						.getSql("FrmMachine.getAddPhosphateItem");
				ps = conn.prepareStatement(addMach);
				ps.setObject(1, machineNo);
				ps.setObject(2, machineNm);
				ps.setObject(3, userName);
				ps.setObject(4, userTime);
				ps.setObject(5, proessCode);
				ps.setObject(6, "C041");
				ps.executeUpdate();
				ps.close();

			}
			for (int i = 0; i < pram1.size(); i++) {

				ArrayList<String> list = (ArrayList<String>) pram1.get(i);
				String machineNo = list.get(0);
				String proessCode = list.get(1);
				String machine_user1 = list.get(2);
				String machine_user2 = list.get(3);
				String bz = list.get(4);
				String userName = list.get(5);
				String userTime = list.get(6);

				int count = 0;
				String machineNm = "";
				String sql = "SELECT T.MACHINE_NAME FROM COUPLING_MACHINE_USER T WHERE T.MACHINE_NO=? ";
				ps = conn.prepareStatement(sql);
				ps.setObject(1, machineNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					count = 1;
					machineNm = rs.getString("MACHINE_NAME");
				}
				rs.close();
				ps.close();
				if (count == 0) {
					cro.setV_errMsg("未新增机床信息!");
					return cro;
				}
				// 新增车丝机床操作工信息
				String addOp = CoreXmlSqlParsersFactory
						.getSql("FrmMachine.getAddOperation");
				ps = conn.prepareStatement(addOp);
				ps.setObject(1, machineNo);
				ps.setObject(2, machineNm);
				ps.setObject(3, "C041");
				ps.setObject(4, machine_user1);
				ps.setObject(5, machine_user2);
				ps.setObject(6, "");
				ps.setObject(7, bz);
				ps.setObject(8, proessCode);
				ps.setObject(9, userName);
				ps.setObject(10, userTime);
				ps.executeUpdate();
				ps.close();
			}

			cro.setV_errMsg("新增成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;

	}

	/***
	 * 修改机床对应用户
	 * 
	 * @return
	 */
	public CoreReturnObject getUpdatePhosphateItem(
			ArrayList<ArrayList<String>> pram,
			ArrayList<ArrayList<String>> pram1, 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;
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String machineNo = list.get(3);
				String machineNm = list.get(0);
				String userName = list.get(1);
				String userTime = list.get(2);
				String machineNo1 = list.get(4);
				// 新增机床信息
				String updateMach = CoreXmlSqlParsersFactory
						.getSql("FrmMachine.getUpdatePhosphateItem");
				ps = conn.prepareStatement(updateMach);				
				ps.setObject(1, machineNm);
				ps.setObject(2, userName);
				ps.setObject(3, userTime);
				ps.setObject(4, machineNo);
				ps.setObject(5, "C041");
				ps.setObject(6, machineNo1);
				ps.setObject(7, "C041");
				ps.executeUpdate();
				ps.close();

			}
			for (int i = 0; i < pram1.size(); i++) {

				ArrayList<String> list = (ArrayList<String>) pram1.get(i);
				String machineNo = list.get(0);
				String proessCode = list.get(1);
				String machine_user1 = list.get(2);
				String machine_user2 = list.get(3);
				String bz = list.get(4);
				String userName = list.get(5);
				String userTime = list.get(6);
				String id = list.get(7);

				int count = 0;
				String machineNm = "";
				String sql = "SELECT T.MACHINE_NAME FROM COUPLING_MACHINE_USER T WHERE T.MACHINE_NO=? ";
				ps = conn.prepareStatement(sql);
				ps.setObject(1, machineNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					count = 1;
					machineNm = rs.getString("MACHINE_NAME");
				}
				rs.close();
				ps.close();
				if (count == 0) {
					cro.setV_errMsg("未新增机床信息!");
					return cro;
				}
				// 新增车丝机床操作工信息
				String updateOp = CoreXmlSqlParsersFactory
						.getSql("FrmMachine.getUpdateOperation");
				ps = conn.prepareStatement(updateOp);
				ps.setObject(1, machineNo);
				ps.setObject(2, machineNm);
				ps.setObject(3, "C041");
				ps.setObject(4, machine_user1);
				ps.setObject(5, machine_user2);
				ps.setObject(6, "");
				ps.setObject(7, bz);
				ps.setObject(8, userName);
				ps.setObject(9, userTime);
				ps.setObject(10, id);
				ps.executeUpdate();
				ps.close();
			}

			cro.setV_errMsg("修改成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/***
	 * 删除机床对应用户
	 * 
	 * @return
	 */
	public CoreReturnObject getDeletePhosphateItem(
			ArrayList<ArrayList<String>> pram,
			ArrayList<ArrayList<String>> pram1, String flag) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			if (flag.equals("1")) {
				for (int i = 0; i < pram.size(); i++) {
					ArrayList<String> list = (ArrayList<String>) pram.get(i);
					String machineNo = list.get(0);
					String proessCode = list.get(1);
					// 删除车丝机床信息
					String updateMach = CoreXmlSqlParsersFactory
							.getSql("FrmMachine.getDeletePhosphateItem");
					ps = conn.prepareStatement(updateMach);
					ps.setObject(1, machineNo);
					ps.executeUpdate();
					ps.close();
					// 删除车丝机床操作工信息
					String updateMachOp = CoreXmlSqlParsersFactory
							.getSql("FrmMachine.getDeleteOper");
					ps = conn.prepareStatement(updateMachOp);
					ps.setObject(1, machineNo);
					ps.setObject(2, proessCode);
					ps.executeUpdate();
					ps.close();

				}
			} else if (flag.equals("2")) {
				for (int i = 0; i < pram1.size(); i++) {

					ArrayList<String> list = (ArrayList<String>) pram1.get(i);
					String id = list.get(0);
					// 删除车丝机床操作工信息
					String updateMachOp = "DELETE COUPLING_OPERATION WHERE ID = ? ";
					ps = conn.prepareStatement(updateMachOp);
					ps.setObject(1, id);
					ps.executeUpdate();
					ps.close();
				}
			}
			cro.setV_errMsg("删除成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}
}
