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 CoreOrderPrice 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);
			
			String managNo=maxParms.get(0).toString();
			String orderNo=maxParms.get(1).toString();
			
			int index_seqMax=GetMaxIndexSeq(managNo,orderNo);
			
			for(int i=0;i<AddParms.size();i++)
			{
				AddParms.get(i).add(index_seqMax+=1);
			}
		}
		CoreReturnObject spbCro = new CoreReturnObject();

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

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

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

		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 GetMaxIndexSeqSql = CoreXmlSqlParsersFactory.getSql("CoreOrderPrice.GetMaxIndexSeq");
		Object[] obj=new Object[]{manageNo,orderNo};
		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("CoreOrderPrice.doDelete");
		ArrayList<Object> spbSqlList = new ArrayList<Object>();
		ArrayList<Object[]> spbParmList = new ArrayList<Object[]>();
		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("CoreOrderPrice.doQuery", obj);
	}
	
	
	
	/**
	 * 合同行状态变更
	 * @return
	 */
	public CoreReturnObject UpdateOrderSts(String ManagNo,String OrderNo
			) {

		Object[] obj = new Object[] { ManagNo,OrderNo };
		return this.setDataBySqlID("CoreOrderPrice.UpdateOrderSts", obj);

	}
}
