package com.steering.pss.ydm.base;

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

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

public class OverstockPipe {

	/**
	 * 查询出库单号
	 * @param conn
	 * @param strlist
	 * @param plineCode
	 * @return
	 * @throws Exception
	 */
	public static String querySendOut(Connection conn, String strlist,
			String plineCode) throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("OverstockPipe.queryOutList");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		psta.setString(2, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("OUTSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 余材转压库综合判定增加产权出库记录
	 * @param connection
	 * @param core
	 * @param JugdeApplyCode
	 * @return
	 * @throws Exception
	 */
	public CoreReturnObject addOutstockDoc(Connection connection,
			CoreReturnObject core, String JugdeApplyCode) throws Exception {
		PreparedStatement psta = null;
		ResultSet rs = null;
		String sql = "";
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String yearMoth = df.format(now);

		// 生成产权出库单号
		String outCqNO = OverstockPipe.querySendOut(connection, "800809", "");

		String updateOff = CoreXmlSqlParsersFactory
				.getSql("OverstockPipe.addOutstockDoc");
		PreparedStatement pstaupdateOff = connection
				.prepareStatement(updateOff);
		pstaupdateOff.setString(1, JugdeApplyCode);
		pstaupdateOff.setString(2, yearMoth);
		pstaupdateOff.setString(3, outCqNO);
		pstaupdateOff.addBatch();
		pstaupdateOff.executeBatch();
		return core;
	}
	
	/**
	 * 撤销综合判定调用
	 * @param connection
	 * @param core
	 * @param JugdeApplyCode
	 * @return
	 * @throws Exception
	 */
	public CoreReturnObject deleteOutstockDoc(Connection connection,
			CoreReturnObject core, String JugdeApplyCode) throws Exception {
		String updateOff = CoreXmlSqlParsersFactory
				.getSql("OverstockPipe.deleteOutstockDoc");
		PreparedStatement pstaupdateOff = connection
				.prepareStatement(updateOff);
		pstaupdateOff.setString(1, JugdeApplyCode);
		pstaupdateOff.addBatch();
		pstaupdateOff.executeBatch();
		return core;
	}
}
