package com.steering.pss.judge.DAL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;

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

import com.steering.comm.db.DBCommon;
import com.steering.pss.judge.model.YdmZcOutlistEntity;

public class QcmJyQueryFrm extends DBCommon {

	/**
	 * 查询合同信息
	 * 
	 * @param params
	 * @return
	 */
	public CoreReturnObject getOrderInfo(ArrayList<Object> params) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("QcmJyQueryFrm.getOrderInfo");
		String sql1 = " and T.OUTSTOCK_TIME BETWEEN TO_DATE('" + params.get(1)
				+ "', 'YYYY-MM-DD HH24:MI:SS')  and TO_DATE('" + params.get(2)
				+ "', 'YYYY-MM-DD HH24:MI:SS')";
		if (!params.get(0).equals("")) {
			sql1 += " and T.ORDER_NO = '" + params.get(0) + "'";
		}
		sql = sql.replace("#something#", sql1);
		return this.getData(sql);
	}
	
	/**
	 * 查询合同信息(虚单)
	 * 
	 * @param params
	 * @return
	 */
	public CoreReturnObject getOrderInfoXD(ArrayList<Object> params) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("QcmJyQueryFrm.getOrderInfoXD");
		String sql1 = " and T.CREATE_TIME BETWEEN TO_DATE('" + params.get(1)
				+ "', 'YYYY-MM-DD HH24:MI:SS')  and TO_DATE('" + params.get(2)
				+ "', 'YYYY-MM-DD HH24:MI:SS')";
		if (!params.get(0).equals("")) {
			sql1 += " and T.ORDER_NO = '" + params.get(0) + "'";
			//sql1 += " and T.ORDER_NO = SUBSTR('"+ params.get(0) +"',1,INSTR('"+ params.get(0) +"','/')-1)" ;
			//sql1 += " and T.ORDER_SEQ = SUBSTR('"+ params.get(0) +"', INSTR('"+ params.get(0) +"', '/')+1)" ;
		}
		sql = sql.replace("#something#", sql1);
		return this.getData(sql);
	}

	/**
	 * 生成检验报告编号
	 * 
	 * @param year
	 * @return
	 */
	public CoreReturnObject doQueryJyYearSeq(String year) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("QcmJyQueryFrm.doQueryJyYearSeq");
		return this.getData(sql, new Object[] { year });
	}

	/**
	 * 查询炉批信息
	 * 
	 * @param params
	 * @return
	 */
	public CoreReturnObject getJustStoveNo(ArrayList<Object> params) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("QcmJyQueryFrm.getJustStoveNo");
		String sql1 = " AND T.ORDER_NO = SUBSTR('" + params.get(0)
				+ "', 1, INSTR('" + params.get(0) + "', '/')-1)";
		sql1 += " AND T.ORDER_SEQ = SUBSTR('" + params.get(0) + "',  INSTR('"
				+ params.get(0) + "', '/')+1)";
		sql1 += " AND T.OUTSTOCK_TIME BETWEEN TO_DATE('" + params.get(1)
				+ "', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + params.get(2)
				+ "', 'YYYY-MM-DD HH24:MI:SS')";
		sql = sql.replace("#something#", sql1);
		return this.getData(sql);
	}
	
	/**
	 * 查询炉批信息(虚单)
	 * 
	 * @param params
	 * @return
	 */
	public CoreReturnObject getJustStoveNoXD(ArrayList<Object> params) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("QcmJyQueryFrm.getJustStoveNoXD");
		String sql1 = " AND T.ORDER_NO = SUBSTR('" + params.get(0)
				+ "', 1, INSTR('" + params.get(0) + "', '/')-1)";
		sql1 += " AND T.ORDER_SEQ = SUBSTR('" + params.get(0) + "',  INSTR('"
				+ params.get(0) + "', '/')+1)";
		sql1 += " AND T.CREATE_TIME BETWEEN TO_DATE('" + params.get(1)
				+ "', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + params.get(2)
				+ "', 'YYYY-MM-DD HH24:MI:SS')";
		sql = sql.replace("#something#", sql1);
		return this.getData(sql);
	}

	/**
	 * 查询检验报告
	 * 
	 * @param params
	 * @return
	 */
	public CoreReturnObject getQcmJyPath(ArrayList<Object> params) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("QcmJyQueryFrm.getQcmJyPath");
		String sql1 = " and T.CREATE_TIME BETWEEN TO_DATE('" + params.get(1)
				+ "', 'YYYY-MM-DD HH24:MI:SS')  and TO_DATE('" + params.get(2)
				+ "', 'YYYY-MM-DD HH24:MI:SS')";
		if (!params.get(0).equals("")) {
			sql1 += " and T.ORDER_NO = '" + params.get(0) + "'";
		}
		sql = sql.replace("#something#", sql1);
		return this.getData(sql);
	}

	/**
	 * 生成检验报告
	 * 
	 * @param list
	 * @param parm
	 * @param flag
	 *            (1-检验报告打印预览,2-生成检验报告)
	 * 
	 * @return
	 * @throws ParseException
	 */
	public CoreReturnObject doAddJyReport(ArrayList<String> list,
			ArrayList<String> parm, String flag) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;

			String year = list.get(0);
			String seq = list.get(1);
			String orderNo = list.get(2);

			if (flag.equals("2")) {
				String sqlLangZbs = CoreXmlSqlParsersFactory
						.getSql("QcmJyQueryFrm.AddJyReport");
				ps = conn.prepareStatement(sqlLangZbs);
				ps.setObject(1, year);
				ps.setObject(2, seq);
				ps.setObject(3, orderNo);
				ps.setObject(4, list.get(3));
				ps.setObject(5, list.get(4));
				ps.setObject(6, list.get(5));
				ps.executeUpdate();
				ps.close();
			}
			// 清空检验报告编号
			String sqlUpdateYearSeqNull = CoreXmlSqlParsersFactory
					.getSql("QcmJyQueryFrm.updateYearSeqNull");
			ps = conn.prepareStatement(sqlUpdateYearSeqNull);
			ps.setObject(1, year.toString() + seq.toString());
			ps.executeUpdate();
			ps.close();
			// 标记出库表检验报告编号
			for (String str : parm) {
				YdmZcOutlistEntity ydmZcTity = com.steering.comm.tools.EntityTool
						.getEntityByJSONStr(str, YdmZcOutlistEntity.class);
				String sqlYdmZc = CoreXmlSqlParsersFactory
						.getSql("QcmJyQueryFrm.updateYdmZcYearSeq");
				ps = conn.prepareStatement(sqlYdmZc);
				ps.setObject(1, year.toString() + seq.toString());
				ps.setObject(2, orderNo);
				ps.setObject(3, orderNo);
				ps.setObject(4, orderNo);
				ps.setObject(5, orderNo);
				ps.setObject(6, ydmZcTity.getJudgeStoveNo());
				ps.setObject(7, ydmZcTity.getBatchNo());
				ps.setObject(8, ydmZcTity.getBillNo());
				ps.setObject(9, ydmZcTity.getActCount());
				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_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}
	
	/**
	 * 生成检验报告(虚单)
	 * 
	 * @param list
	 * @param parm
	 * @param flag
	 *            (1-检验报告打印预览,2-生成检验报告)
	 * 
	 * @return
	 * @throws ParseException
	 */
	public CoreReturnObject doAddJyReportXD(ArrayList<String> list,
			ArrayList<String> parm, String flag) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;

			String year = list.get(0);
			String seq = list.get(1);
			String orderNo = list.get(2);

			if (flag.equals("2")) {
				String sqlLangZbs = CoreXmlSqlParsersFactory
						.getSql("QcmJyQueryFrm.AddJyReport");
				ps = conn.prepareStatement(sqlLangZbs);
				ps.setObject(1, year);
				ps.setObject(2, seq);
				ps.setObject(3, orderNo);
				ps.setObject(4, list.get(3));
				ps.setObject(5, list.get(4));
				ps.setObject(6, list.get(5));
				ps.executeUpdate();
				ps.close();
			}
			// 清空检验报告编号
			String sqlUpdateYearSeqNull = CoreXmlSqlParsersFactory
					.getSql("QcmJyQueryFrm.updateYearSeqNullXD");
			ps = conn.prepareStatement(sqlUpdateYearSeqNull);
			ps.setObject(1, year.toString() + seq.toString());
			ps.executeUpdate();
			ps.close();
			// 标记出库表检验报告编号
			for (String str : parm) {
				YdmZcOutlistEntity ydmZcTity = com.steering.comm.tools.EntityTool
						.getEntityByJSONStr(str, YdmZcOutlistEntity.class);
				String sqlYdmZc = CoreXmlSqlParsersFactory
						.getSql("QcmJyQueryFrm.updateYdmZcYearSeqXD");
				ps = conn.prepareStatement(sqlYdmZc);
				ps.setObject(1, year.toString() + seq.toString());
				ps.setObject(2, orderNo);
				ps.setObject(3, orderNo);
				ps.setObject(4, orderNo);
				ps.setObject(5, orderNo);
				ps.setObject(6, ydmZcTity.getJudgeStoveNo());
				ps.setObject(7, ydmZcTity.getBatchNo());
				ps.setObject(8, ydmZcTity.getBillNo());
				ps.setObject(9, ydmZcTity.getActCount());
				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_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 修改打印标识，次数
	 * 
	 * @param year
	 * @param seq
	 * @param userName
	 * @return
	 * @throws ParseException
	 */
	public CoreReturnObject doUpdatePrintData(String year, String seq,
			String userName) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;

			String updateLan = "UPDATE QCM_JY_PATH SET PRINT_FLAG= '1', PRINT_NUM=TO_NUMBER(NVL(PRINT_NUM,0))+1,PRINT_TIME=SYSDATE,PRINT_NAME=?  WHERE YEAR = ? AND SEQ = ?";
			ps = conn.prepareStatement(updateLan);
			ps.setObject(1, userName);
			ps.setObject(2, year);
			ps.setObject(3, seq);
			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_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

}
