package com.steering.pss.plnsaleord.processOrder.base;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

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

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.pss.plnsaleord.base.PlnHelper;

/**
 * 前后工序信息
 * 
 * @author peng
 * 
 */
public class PreGxInfo extends DBCommon {
	public CoreReturnObject queryGxInfo(String proPlanId, String processCode,
			String flag) {
		Connection con = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			List<String[]> arrList = null;
			if (processCode.equals("D")) {
				arrList = PlnHelper.getArrays(con, "PreGxInfo.getGxInfoD",
						"   and T.PRO_PLAN_ID = ? ",
						new String[] { proPlanId }, new String[] {
								"PREVIOUS_GX", "PREV_GXPLAN_ID", "NEXT_GX",
								"NEXT_PRO_PLAN_ID" });

			} else if (processCode.equals("E")) {
				arrList = PlnHelper.getArrays(con, "PreGxInfo.getGxInfoE",
						"   and T.PRO_PLAN_ID = ? ",
						new String[] { proPlanId }, new String[] {
								"PREVIOUS_GX", "PREV_GXPLAN_ID", "NEXT_GX",
								"NEXT_PRO_PLAN_ID" });

			} else if (processCode.equals("F")) {
				arrList = PlnHelper.getArrays(con, "PreGxInfo.getGxInfoF",
						"   and T.PRO_PLAN_ID = ? ",
						new String[] { proPlanId }, new String[] {
								"PREVIOUS_GX", "PREV_GXPLAN_ID", "NEXT_GX",
								"NEXT_PRO_PLAN_ID" });
			} else if (processCode.equals("G")) {
				arrList = PlnHelper.getArrays(con, "PreGxInfo.getGxInfoG",
						"   and T.PRO_PLAN_ID = ? ",
						new String[] { proPlanId }, new String[] {
								"PREVIOUS_GX", "PREV_GXPLAN_ID", "NEXT_GX",
								"NEXT_PRO_PLAN_ID" });
			}
			// ORDER BY TO_NUMBER(T.GX_PLAN_NO)
			if (arrList == null || arrList.size() == 0) {
				return new CoreReturnObject();
			}
			String[] proPlanIdList = new String[arrList.size()];
			String sql = "";
			for (int i = 0; i < arrList.size(); i++) {
				String[] arr = arrList.get(i);
				if (flag.equals("Pr")) {
					if (arr[0] != null) {
						if (arr[0].equals("D")) {
							sql = CoreXmlSqlParsersFactory
									.getSql("PreGxInfo.getGxInfoD");
						} else if (arr[0].equals("E")) {
							sql = CoreXmlSqlParsersFactory
									.getSql("PreGxInfo.getGxInfoE");
						} else if (arr[0].equals("F")) {
							sql = CoreXmlSqlParsersFactory
									.getSql("PreGxInfo.getGxInfoF");
						} else {

						}
					}
					proPlanIdList[i]=arr[1];
				} else {
					if (arr[2] != null) {
						if (arr[2].equals("E")) {
							sql = CoreXmlSqlParsersFactory
									.getSql("PreGxInfo.getGxInfoE");
						} else if (arr[2].equals("F")) {
							sql = CoreXmlSqlParsersFactory
									.getSql("PreGxInfo.getGxInfoF");
						} else if (arr[2].equals("G")) {
							sql = CoreXmlSqlParsersFactory
									.getSql("PreGxInfo.getGxInfoG");
						} else {
							sql = CoreXmlSqlParsersFactory
									.getSql("PreGxInfo.getGxInfoH");
						}
					}
					proPlanIdList[i]=arr[3];
				}
			}

			if (!sql.equals("") && proPlanIdList.length> 0) {
				sql += CoreConverter.toSqlConditionString("T.PRO_PLAN_ID",
						proPlanIdList);
				sql += "  ORDER BY TO_NUMBER(T.GX_PLAN_NO)";
				return this.getData(sql);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return new CoreReturnObject();
	}
}
