package com.steering.pss.qcm;

 
import java.util.ArrayList;

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.SqlBuilder;

public class CoreStandard extends DBCommon {

	/**
	 * 保存
	 * @param spbAddParams
	 * @param spbUpdateParams
	 * @param spbSupplementAddParams
	 * @param spbSupplementUpdateParams
	 * @return
	 */
	public CoreReturnObject doSave(ArrayList<ArrayList<Object>> spbAddParams,
			ArrayList<ArrayList<Object>> spbUpdateParams,
			ArrayList<ArrayList<Object>> spbSupplementAddParams,
			ArrayList<ArrayList<Object>> spbSupplementUpdateParams) {

		CoreReturnObject spbCro = new CoreReturnObject();

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

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

		String spbSupplementAddSql = CoreXmlSqlParsersFactory
				.getSql("CoreStandardPsc.ADD");

		String spbSupplementUpdateSql = CoreXmlSqlParsersFactory
				.getSql("CoreStandardPsc.MODIFY");

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

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

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

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

		if (spbUpdateParams.size() > 0) {
			for (int j = 0; j < spbUpdateParams.size(); j++) {
				spbSqlList.add(spbUpdateSql);
				spbParmList.add(spbUpdateParams.get(j).toArray());
			}
		}
		// 构建从表新增sql语句和参数。

		if (spbSupplementAddParams.size() > 0) {
			for (int k = 0; k < spbSupplementAddParams.size(); k++) {
				spbSqlList.add(spbSupplementAddSql);
				spbParmList.add(spbSupplementAddParams.get(k).toArray());
			}
		}

		// 构建从表修改sql语句和参数。
		if (spbSupplementUpdateParams.size() > 0) {
			for (int g = 0; g < spbSupplementUpdateParams.size(); g++) {
				spbSqlList.add(spbSupplementUpdateSql);
				spbParmList.add(spbSupplementUpdateParams.get(g).toArray());
			}
		}

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

		return spbCro;

	}

	/**
	 * 标准号管理查询
	 * 
	 * @param 是否包含已删除数据
	 * @param 产品规格描述
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject getLineList(Boolean includeDeleted, String name) {
		String deletedFlag = includeDeleted ? "0" : "";
		Object[] params = new Object[] { deletedFlag, name };
		return this.getDataBySqlID("CoreStandard.QUERY", params);
	}

	/**
	 * 标准号管理从表查询
	 * 
	 * @param 是否包含已删除数据
	 * @param 产品规格描述
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject getLineListPsc(Boolean includeDeleted) {
		String deletedFlag = includeDeleted ? "0" : "";
		Object[] params = new Object[] { deletedFlag };
		return this.getDataBySqlID("CoreStandardPsc.QUERY", params);
	}
	
	/**
	 * 标准号管理删除或恢复
	 */
	public CoreReturnObject deleteLineInfo(ArrayList<Object> plineCodes,
			String operator, Boolean isDelete) {
		ArrayList<String> sqlIDList = new ArrayList<String>();
		ArrayList<Object[]> params = new ArrayList<Object[]>();
		for (int i = 0; i < plineCodes.size(); i++) {
			if (isDelete) {
				sqlIDList.add("CoreStandard.DELETE");
				params.add(new Object[] { operator,
						plineCodes.get(i).toString() });
			} else {
				sqlIDList.add("CoreStandard.RESUME");
				params.add(new Object[] { plineCodes.get(i).toString() });
			}
		}
		return this.setDataById(sqlIDList, params);
	}

	
	/**
	 * 冶金规范表删除或恢复
	 */
	public CoreReturnObject deleteLineInfoPsc(ArrayList<Object> plineCodes,
			String operator, Boolean isDelete) {
		ArrayList<String> sqlIDList = new ArrayList<String>();
		ArrayList<Object[]> params = new ArrayList<Object[]>();
		for (int i = 0; i < plineCodes.size(); i++) {
			if (isDelete) {
				sqlIDList.add("CoreStandardPsc.DELETE");
				params.add(new Object[] { operator,
						plineCodes.get(i).toString() });
			} else {
				sqlIDList.add("CoreStandardPsc.RESUME");
				params.add(new Object[] { plineCodes.get(i).toString() });
			}
		}
		return this.setDataById(sqlIDList, params);
	}
	/**
	 * 绑定标准号
	 */
	public CoreReturnObject bandCmbStd() {
		return this.getDataBySqlID("CoreStandard.BANDSTD");
	}
	
	public CoreReturnObject bandCmbMaxStdXh()
	{
		return this.getDataBySqlID("CoreStandard.BANDSTDXH");
	}
	
	public CoreReturnObject queryMaxStdCode(){
	    return this.getDataBySqlID("CoreStandard.queryMaxStdCode");
	}
	
	public CoreReturnObject queryMaxPscCode(){
	    return this.getDataBySqlID("CoreStandard.queryMaxPscCode");
	}
	
	public CoreReturnObject getProducName(){
        return this.getDataBySqlID("CoreStandard.GetProducName");
    }
	
	public CoreReturnObject getSteel(){
        return this.getDataBySqlID("CoreStandard.GetSteel");
    }
	
	public CoreReturnObject getStd_Style(){
        return this.getDataBySqlID("CoreStandard.GetStd_Style");
    }
	
	public CoreReturnObject isExistStd(ArrayList<String> list){
	    return this.getDataBySqlID("CoreStandard.IsExistStd", list.toArray());
	}
	
	public CoreReturnObject isExistPsc(ArrayList<String> list){
        return this.getDataBySqlID("CoreStandard.IsExistPsc", list.toArray());
    }
	
	public CoreReturnObject getPsc(String code){
	    Object []obj =new Object[]{code};
        return this.getDataBySqlID("CoreStandard.GETPSCUPDATE",  obj);
    }
	
	/**
	 * 查询内控索引码
	 * @param stdNkName
	 * @param gradeName
	 * @return
	 */
	public CoreReturnObject getStdNkIdnex(String stdNkName, String gradeName){
		SqlBuilder b = new SqlBuilder("CoreStandard.getStdNkIndex");
		b.Fill(new String[]{ stdNkName, gradeName });
		
		return this.getData(b.toString());
	}
}
