package com.steering.pss.sale.order;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

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

import com.steering.comm.db.DBCommon;

/**
 * 调整交货期
 * 
 * @author Administrator
 * 
 */
public class FrmOrderDelverEdate extends DBCommon {

	/**
	 * 查询
	 * 
	 * @param orderNo
	 * @param SALE_ORG
	 * @return
	 */
	public CoreReturnObject doQuery(String orderNo, String SALE_ORG) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOrderDelverEdate.doQuery");
		if (!orderNo.equals("")) {
			sql += "  AND T.ORDER_NO LIKE '%" + orderNo + "%' ";
		}
		if (SALE_ORG.equals("100102")) {
			sql += " AND T.SALE_ORG = '100102'";
		} else {
			sql += " AND T.SALE_ORG <> '100102'";
		}
		sql += " ORDER BY ORDER_NO,ORDER_SEQ,DELIVERY_NO ";
		return this.getData(sql);
	}
	
	/**
	 * 查询交货期调整记录
	 * @param ordPk
	 * @return
	 */
	public CoreReturnObject doQueryEdateRecord(String ordPk) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOrderDelverEdate.doQueryEdateRecord");
		if (!ordPk.equals("")) {
			sql += "  AND T.ORD_PK = '" + ordPk + "' ";
		}
		sql += " ORDER BY ORDER_NO,ORDER_SEQ,DELIVERY_NO,COL_TIME ";
		return this.getData(sql);
	}

	/**
	 * 调整交货期
	 * 
	 * @param parm
	 * @param userName
	 *            操作人
	 * @param userID
	 * @param flag
	 *            0-前调，1-后调
	 * @param tian
	 *            天数
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject updateDelvryEdate(ArrayList<Object> pram,
			String userName, String userID, String flag, String tian) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String flagLine = "0";
		String ORD_LN_PK_TEST = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String scrapeSql = CoreXmlSqlParsersFactory
					.getSql("FrmOrderDelverEdate.updateDelvryEdate");
			PreparedStatement scrapePsta = connection
					.prepareStatement(scrapeSql);
			String edateLineSql = CoreXmlSqlParsersFactory
					.getSql("FrmOrderDelverEdate.updateDelvryEdateLine");
			PreparedStatement edateLinePsta = connection
					.prepareStatement(edateLineSql);

			for (int j = 0; j < pram.size(); j++) {
				ArrayList<Object> list = (ArrayList<Object>) pram.get(j);
				String ORD_PK = list.get(0).toString();
				String ORD_LN_PK = list.get(1).toString();
				String ORD_LN_DLY_PK = list.get(2).toString();
				String ORDER_NO = list.get(3).toString();
				String ORDER_SEQ = list.get(4).toString();
				String DELIVERY_NO = list.get(5).toString();

				scrapePsta.setString(1, ORD_PK);
				scrapePsta.setString(2, ORD_LN_PK);
				scrapePsta.setString(3, ORD_LN_DLY_PK);
				scrapePsta.setString(4, ORDER_NO);
				scrapePsta.setString(5, ORDER_SEQ);
				scrapePsta.setString(6, DELIVERY_NO);
				scrapePsta.setString(7, userName);
				scrapePsta.setString(8, userID);
				scrapePsta.setString(9, flag);
				scrapePsta.setString(10, tian);
				scrapePsta.addBatch();
				
				if(!ORD_LN_PK_TEST.equals(ORD_LN_PK)){
					edateLinePsta.setString(1, ORD_PK);
					edateLinePsta.setString(2, ORD_LN_PK);
					edateLinePsta.setString(3, ORD_LN_DLY_PK);
					edateLinePsta.setString(4, ORDER_NO);
					edateLinePsta.setString(5, ORDER_SEQ);
					edateLinePsta.setString(6, DELIVERY_NO);
					edateLinePsta.setString(7, userName);
					edateLinePsta.setString(8, userID);
					edateLinePsta.setString(9, flag);
					edateLinePsta.setString(10, tian);
					edateLinePsta.setString(11, flagLine);
					edateLinePsta.addBatch();
					ORD_LN_PK_TEST = ORD_LN_PK;
				}
				
			}
			scrapePsta.executeBatch();
			edateLinePsta.executeBatch();
			cro.setV_errMsg("调整交货期成功!");
			connection.commit();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 仅调合同行交货期
	 * 
	 * @param pram
	 * @param userName
	 * @param userID
	 * @param flag
	 * @param tian
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject updateDelvryEdateLine(ArrayList<Object> pram,
			String userName, String userID, String flag, String tian) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String flagLine = "1";
		String ORD_LN_PK_TEST = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String edateLineSql = CoreXmlSqlParsersFactory
			.getSql("FrmOrderDelverEdate.updateDelvryEdateLine");
	PreparedStatement edateLinePsta = connection
			.prepareStatement(edateLineSql);

			for (int j = 0; j < pram.size(); j++) {
				ArrayList<Object> list = (ArrayList<Object>) pram.get(j);
				String ORD_PK = list.get(0).toString();
				String ORD_LN_PK = list.get(1).toString();
				String ORD_LN_DLY_PK = list.get(2).toString();
				String ORDER_NO = list.get(3).toString();
				String ORDER_SEQ = list.get(4).toString();
				String DELIVERY_NO = list.get(5).toString();

				if(!ORD_LN_PK_TEST.equals(ORD_LN_PK)){
					edateLinePsta.setString(1, ORD_PK);
					edateLinePsta.setString(2, ORD_LN_PK);
					edateLinePsta.setString(3, ORD_LN_DLY_PK);
					edateLinePsta.setString(4, ORDER_NO);
					edateLinePsta.setString(5, ORDER_SEQ);
					edateLinePsta.setString(6, DELIVERY_NO);
					edateLinePsta.setString(7, userName);
					edateLinePsta.setString(8, userID);
					edateLinePsta.setString(9, flag);
					edateLinePsta.setString(10, tian);
					edateLinePsta.setString(11, flagLine);
					edateLinePsta.addBatch();
					ORD_LN_PK_TEST = ORD_LN_PK;
				}
				
			}
			edateLinePsta.executeBatch();
			cro.setV_errMsg("仅调合同行交货期成功!");
			connection.commit();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
}
