package com.steering.mes.zgmil.coup;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

import com.steering.comm.db.DBCommon;

/**
 * 文件上传（探伤报告、质检报告）
 * 
 * @author Administrator
 * 
 */
public class FileUpdate extends DBCommon {

	/**
	 * 生成探伤报告的报告编号
	 * 
	 * @param heatno
	 * @param plineCode
	 * @param bc
	 * @param bz
	 * @return
	 * @throws SQLException
	 */
	public CoreReturnObject getReportNo(String heatno, String plineCode,
			String bc, String bz) throws SQLException {
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		ResultSet rs = null;

		String sql = CoreXmlSqlParsersFactory
				.getSql("FileUpdae.selectReportNo");// 查询该炉号的探伤报告是否已存在
		String sql2 = CoreXmlSqlParsersFactory.getSql("FileUpdae.getReportNo");// 生成探伤报告编号
		psta = connection.prepareStatement(sql);
		psta.setString(1, heatno);
		psta.setString(2, bc);
		psta.setString(3, bz);
		psta.setString(4, plineCode);
		rs = psta.executeQuery();
		if (rs.next()) {
			return this
					.getData(sql, new Object[] { heatno, bc, bz, plineCode });
		} else {
			return this.getData(sql2, new Object[] { plineCode, plineCode });
		}

	}

	/**
	 * 上传探伤报告
	 * 
	 * @param heatno
	 * @param shift
	 * @param group
	 * @param plineCode
	 * @param user
	 * @param filePathNew
	 * @return
	 */
	public CoreReturnObject updateInspectionReport(String heatno, String shift,
			String group, String plineCode, String user, String filePathNew,
			String proPlanId, String gxPlanNo) {

		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			PreparedStatement st = null;
			String updateLenSql = CoreXmlSqlParsersFactory
					.getSql("FileUpdate.updateInspectionReport");
			st = connection.prepareStatement(updateLenSql);
			st.setString(1, heatno);
			st.setString(2, plineCode);
			st.setString(3, shift);
			st.setString(4, group);
			st.setString(5, user);
			st.setString(6, filePathNew);
			st.setString(7, proPlanId);
			st.setString(8, gxPlanNo);
			st.addBatch();

			st.executeBatch();
			connection.commit();
			cro.setV_errMsg("上传成功!");
		} catch (Exception e) {
			System.out.println(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 plineCode
	 * @return
	 * @throws SQLException
	 */
	public CoreReturnObject getReportNo2(String heatno, String plineCode,
			String bc, String bz, String follow) throws SQLException {
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		ResultSet rs = null;

		String sql = CoreXmlSqlParsersFactory
				.getSql("FileUpdae.selectReportNo2");// 查询该炉号的表检报告是否已存在
		String sql2 = CoreXmlSqlParsersFactory.getSql("FileUpdae.getReportNo2");// 生成表检报告编号
		psta = connection.prepareStatement(sql);
		psta.setString(1, heatno);
		psta.setString(2, bc);
		psta.setString(3, bz);
		psta.setString(4, plineCode);
		psta.setString(5, follow);
		rs = psta.executeQuery();
		if (rs.next()) {
			return this.getData(sql, new Object[] { heatno, bc, bz, plineCode,
					follow });
		} else {
			return this.getData(sql2, new Object[] { plineCode, plineCode });
		}
	}

	/**
	 * 上传表检报告
	 * 
	 * @param heatno
	 * @param shift
	 * @param group
	 * @param plineCode
	 * @param user
	 * @param filePathNew
	 * @param follow
	 * @return
	 */
	public CoreReturnObject updateSurfaceReport(String heatno, String shift,
			String group, String plineCode, String user, String filePathNew,
			String follow, String proPlanId, String gxPlanNo) {

		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			PreparedStatement st = null;
			String updateLenSql = CoreXmlSqlParsersFactory
					.getSql("FileUpdate.updateSurfaceReport");
			st = connection.prepareStatement(updateLenSql);
			st.setString(1, heatno);
			st.setString(2, plineCode);
			st.setString(3, shift);
			st.setString(4, group);
			st.setString(5, user);
			st.setString(6, filePathNew);
			st.setString(7, follow);
			st.setString(8, proPlanId);
			st.setString(9, gxPlanNo);
			st.addBatch();

			st.executeBatch();
			connection.commit();
			cro.setV_errMsg("上传成功!");
		} catch (Exception e) {
			System.out.println(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;
	}

}
