package com.steering.pss.qcm;

import java.sql.SQLException;
import java.util.ArrayList;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.db.DBCommon;

/**
 * 化学元素基础信息管理类
 * 
 * @author WQ 2014/10/16
 * @version v1.0
 * @modify
 */
public class CoreChemistryElements extends DBCommon {
	
	/**
	 * 判断描述信息是否重复
	 * @param name
	 * @return
	 */
	public CoreReturnObject GetName(String name) {
		Object[] obj = new Object[] { name };
		return this.getDataBySqlID("CoreChemistryElements.GetName", obj);
	}
	
	
	/**
	 * 获取化学元素代码最大值
	 * @param name
	 * @return
	 */
	public CoreReturnObject GetMaxCode() {
		return this.getDataBySqlID("CoreChemistryElements.GetMaxCode");
	}
	
	/**
	 * 化学元素基础信息查询
	 * 
	 * @param includeDeleted
	 *            是否包含已删除数据
	 * @param name
	 *            化学元素描述
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject getLineList(Boolean includeDeleted, String name) {
		String deletedFlag = includeDeleted ? "0" : "";
		Object[] params = new Object[] { deletedFlag, name };
		return this.getDataBySqlID("CoreChemistryElements.QUERY", params);
	}

	/**
	 * 化学元素基础信息新增
	 * 
	 * @param parm
	 *            参数
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject doAdd(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();

		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreChemistryElements.ADD");
		
		try {
				cro = this.getDao("TgDao").ExcuteNonQuery(strSql,
						parm.toArray());
		} catch (SQLException e) {
			cro.setV_errCode(-2);
			cro = cro.SetErrInfo(e.getMessage());
		}

		return cro;
	}

	/**
	 * 化学元素基础信息修改
	 * 
	 * @param parm
	 *            参数
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject doModify(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();

		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreChemistryElements.MODIFY");

		try {
				cro = this.getDao("TgDao").ExcuteNonQuery(strSql,
						parm.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			cro.setV_errCode(-2);
			cro = cro.SetErrInfo(e.getMessage());
		}

		return cro;
	}

	/**
	 * 化学元素基础信息删除或恢复
	 * 
	 * @param plineCodes
	 *            产线代码集合
	 * @param operator
	 *            操作人
	 * @param isDelete
	 *            true删除 false恢复
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	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("CoreChemistryElements.DELETE");
				params.add(new Object[] { operator,
						plineCodes.get(i).toString() });
			} else {
				sqlIDList.add("CoreChemistryElements.RESUME");
				params.add(new Object[] { plineCodes.get(i).toString() });
			}
		}
		return this.setDataById(sqlIDList, params);
	}
	
	/**
	 * 是否复合公式是否由基本化学元素组成。
	 * @param elements
	 * @return
	 */
	public CoreReturnObject complexIsValid(String elements){
		String sql = CoreXmlSqlParsersFactory.getSql("CoreChemistryElements.isComplex");
		sql = sql.replace("#ELEMENTS#", elements);
		
		return this.getData(sql);
	}
	
	/**
	 * 质量免检基础信息新增
	 * 
	 * @param parm
	 *            参数
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject doFreeTestAdd(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();

		String strSql = CoreXmlSqlParsersFactory
				.getSql("FreeTest.ADD");
		
		try {
				cro = this.getDao("TgDao").ExcuteNonQuery(strSql,
						parm.toArray());
		} catch (SQLException e) {
			cro.setV_errCode(-2);
			cro = cro.SetErrInfo(e.getMessage());
		}

		return cro;
	}
	
	/**
	 * 质量免检基础信息修改
	 * 
	 * @param parm
	 *            参数
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject doFreeTestModify(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();

		String strSql = CoreXmlSqlParsersFactory
				.getSql("FreeTest.MODIFY");

		try {
				cro = this.getDao("TgDao").ExcuteNonQuery(strSql,
						parm.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			cro.setV_errCode(-2);
			cro = cro.SetErrInfo(e.getMessage());
		}

		return cro;
	}
	/**
	 * 质量免检基础信息查询
	 * 
	 * @param includeDeleted
	 *            是否包含已删除数据
	 * @param name
	 *            化学元素描述
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject getFreeTest(String name) {
		Object[] params = new Object[] { name };
		return this.getDataBySqlID("FreeTest.QUERY", params);
	}
	
	/**
	 * 质量免检基础信息删除或恢复
	 * 
	 * @param plineCodes
	 *            产线代码集合
	 * @param operator
	 *            操作人
	 * @param isDelete
	 *            true删除 false恢复
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject deleteFreeTest(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("FreeTest.DELETE");
				params.add(new Object[] { operator,
						plineCodes.get(i).toString() });
			} else {
				sqlIDList.add("FreeTest.RESUME");
				params.add(new Object[] { plineCodes.get(i).toString() });
			}
		}
		return this.setDataById(sqlIDList, params);
	}
}
