package com.steering.pss.qcm.DAL;

import java.sql.Connection;
import java.util.ArrayList;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.ComBaseSuppAptitudeEntity;
import com.steering.pss.qcm.model.GenericEntity;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

/**
 * 数据库表COM_BASE_SUPP_APTITUDE所对应的Java类（生成工具：代码生成工具4.0
 * 访问地址：http://172.16.2.128/tool/）
 * 
 * @author tgcx-test date 2016-08-16
 */
public class ComBaseSuppAptitudeDAL extends DBCommon {

	/**
	 * 通用查询
	 */
	public CoreReturnObject Query(String suppCode) {
		SqlBuilder b = new SqlBuilder("ComBaseSuppAptitudeDAL.Query");
		b.AppendLn("AND SUPP_CODE = {0}");
		b.Fill(new String[] { suppCode });

		return this.getData(b.toString());
	}

	/**
	 * 通过主键查询
	 */
	public CoreReturnObject QueryByPk(String id) {
		String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBaseSuppAptitudeDAL.QueryByPk");
		return this.getData(sqlQuery);
	}

	/**
	 * 新增
	 */
	public CoreReturnObject Insert(String json) {
		TransctionTool transction = new TransctionTool();
		String id = "";
		try {
			Connection con = transction.getCon();
			GenericEntity maxId = EntityTool.getEntityBySqlNoClsCon(
					CoreXmlSqlParsersFactory.getSql("ComBaseSuppAptitudeDAL.GetMaxPk"), con, GenericEntity.class);
			ComBaseSuppAptitudeEntity comBaseSuppAptitudeEntity = EntityTool.getEntityByGson(json,
					ComBaseSuppAptitudeEntity.class);
			comBaseSuppAptitudeEntity.setId(maxId.getMaxValue());
			id = comBaseSuppAptitudeEntity.getId();
			String sqlInsert = CoreXmlSqlParsersFactory.getSql("ComBaseSuppAptitudeDAL.Insert");
			sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, comBaseSuppAptitudeEntity);
			if (!transction.ExcuteTransction(sqlInsert)) {
				return transction.rollBack();
			}
		} catch (Exception ex) {
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		CoreReturnObject cro = transction.commit();
		cro.setResult(id);
		return cro;
	}

	/**
	 * 修改
	 */
	public CoreReturnObject Update(String json) {
		TransctionTool transction = new TransctionTool();
		try {
			ComBaseSuppAptitudeEntity comBaseSuppAptitudeEntity = EntityTool.getEntityByGson(json,
					ComBaseSuppAptitudeEntity.class);
			String sqlUpdate = CoreXmlSqlParsersFactory.getSql("ComBaseSuppAptitudeDAL.Update");
			sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, comBaseSuppAptitudeEntity);
			if (!transction.ExcuteTransction(sqlUpdate)) {
				return transction.rollBack();
			}
		} catch (Exception ex) {
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		return transction.commit();
	}

	/**
	 * 删除
	 */
	public CoreReturnObject Delete(String json) {
		TransctionTool transction = new TransctionTool();
		try {
			ComBaseSuppAptitudeEntity comBaseSuppAptitudeEntity = EntityTool.getEntityByGson(json,
					ComBaseSuppAptitudeEntity.class);
			String sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseSuppAptitudeDAL.Delete");
			sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, comBaseSuppAptitudeEntity);
			if (!transction.ExcuteTransction(sqlUpdateValidflag)) {
				return transction.rollBack();
			}
		} catch (Exception ex) {
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		return transction.commit();
	}
	
	public CoreReturnObject IsRepeat(String certNo, String id){
		SqlBuilder b = new SqlBuilder("ComBaseSuppAptitudeDAL.IsRepeat");
		b.Fill(new String[]{certNo, id});
		return this.getData(b.toString());
	}
}
