package com.steering.pss.sale.order;

import CoreFS.SA01.CoreIComponent;
import CoreFS.SA06.CoreReturnObject;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.db.DBCommon;

public class CoreDlgTransitPlan extends DBCommon {
	/**
	 * 保存
	 * @param spbAddParams
	 * @param spbUpdateParams
	 * @param spbSupplementAddParams
	 * @param spbSupplementUpdateParams
	 * @return
	 */
	public CoreReturnObject doOk(ArrayList<ArrayList<Object>> AddParms,
			ArrayList<ArrayList<Object>> UpdateParms) {

		if(AddParms.size()>0)
		{
			ArrayList<Object> maxParms=AddParms.get(0);
			int index_seqMax=GetMaxIndexSeq(maxParms.get(0).toString(),maxParms.get(1).toString(),maxParms.get(2).toString());
			for(int i=0;i<AddParms.size();i++)
			{
				AddParms.get(i).add(index_seqMax+=1);
			}
		
		}
		CoreReturnObject spbCro = new CoreReturnObject();

		String spbAddSql = CoreXmlSqlParsersFactory.getSql("CoreDlgTransitPlan.ADD");

		String spbUpdateSql = CoreXmlSqlParsersFactory
				.getSql("CoreDlgTransitPlan.MODIFY");

		

		ArrayList<Object> spbSqlList = new ArrayList<Object>();
		ArrayList<Object[]> spbParmList = new ArrayList<Object[]>();

		// 构建主表addsql语句和参数。

		if (AddParms.size() > 0) {
			for (int i = 0; i < AddParms.size(); i++) {
				spbSqlList.add(spbAddSql);
				spbParmList.add(AddParms.get(i).toArray());
			}
		}

		// 构建主表修改sql语句和参数。

		if (UpdateParms.size() > 0) {
			for (int j = 0; j < UpdateParms.size(); j++) {
				spbSqlList.add(spbUpdateSql);
				spbParmList.add(UpdateParms.get(j).toArray());
			}
		}
		

		try {
			spbCro = this.getDao("TgDao").ExcuteTransction(spbSqlList,
					spbParmList);
		} catch (Exception e) {
			spbCro = spbCro.SetErrInfo(e.getMessage());
		}

		return spbCro;

	}
	
	private int GetMaxIndexSeq(String manageNo, String orderNo, String orderSeq) {
		
		String GetMaxIndexSeqSql = CoreXmlSqlParsersFactory.getSql("CoreDlgTransitPlan.GetMaxIndexSeq");
		Object[] obj=new Object[]{manageNo,orderNo,orderSeq};
		int indexSeq=0;
		try{
			ResultSet rs = this.getDao("TgDao")
	                .ExceuteQueryForResultSet(GetMaxIndexSeqSql, obj);
		
			if(rs.next())
			{
				indexSeq=rs.getInt(1);
			}
		}
		catch (SQLException e) {
            // TODO Auto-generated catch block
            CoreIComponent.logAdapter.filelogger.logError(e
                    .getMessage());// 将服务端的错误信息写入日志文件
        }
		return indexSeq;
	}

	/**
	 * 批量删除
	 * @param spbAddParams
	 * @param spbUpdateParams
	 * @param spbSupplementAddParams
	 * @param spbSupplementUpdateParams
	 * @return
	 */
	public CoreReturnObject doDelete(ArrayList<ArrayList<Object>> deleteParms
			) {

		CoreReturnObject spbCro = new CoreReturnObject();

		String spbDeleteSql = CoreXmlSqlParsersFactory.getSql("CoreDlgTransitPlan.doDelete");

		ArrayList<Object> spbSqlList = new ArrayList<Object>();
		ArrayList<Object[]> spbParmList = new ArrayList<Object[]>();

		// 构建主表addsql语句和参数。

		if (deleteParms.size() > 0) {
			for (int i = 0; i < deleteParms.size(); i++) {
				spbSqlList.add(spbDeleteSql);
				spbParmList.add(deleteParms.get(i).toArray());
			}
		}

		try {
			spbCro = this.getDao("TgDao").ExcuteTransction(spbSqlList,
					spbParmList);
		} catch (Exception e) {
			spbCro = spbCro.SetErrInfo(e.getMessage());
		}

		return spbCro;

	}
	
	/**
	 * 查询功能
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject doQuery(String ManageNo,String OrderNo) {
		Object[] obj = new Object[] { ManageNo,OrderNo };
		return this.getDataBySqlID("CoreDlgTransitPlan.doQuery", obj);
	}
	
	/**
	 * 查询功能ComBaseInfo
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject doQueryComBaseInfo(String code) {
		Object[] obj = new Object[] { code };
		return this.getDataBySqlID("CoreDlgTransitPlan.doQueryComBaseInfo", obj);
	}
	
	/**
	 * 查询功能
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject doLineLnQuery(String ManageNo,String OrderNo) {
		Object[] obj = new Object[] { ManageNo,OrderNo };
		return this.getDataBySqlID("CoreDlgTransitPlan.GetMaxordlndlypk", obj);
	}
}
