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.ComBaseSuppBankEntity;
import com.steering.pss.qcm.model.GenericEntity;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

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

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

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

	/**
	 * 通过主键查询
	 */
	public CoreReturnObject QueryByPk(String suppCode, String bankAccount) {
		String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBaseSuppBankDAL.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);
			ComBaseSuppBankEntity comBaseSuppBankEntity = EntityTool.getEntityByGson(json, ComBaseSuppBankEntity.class);
			comBaseSuppBankEntity.setId(maxId.getMaxValue());
			id = comBaseSuppBankEntity.getId();
			String sqlInsert = CoreXmlSqlParsersFactory.getSql("ComBaseSuppBankDAL.Insert");
			sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, comBaseSuppBankEntity);
			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 {
			ComBaseSuppBankEntity comBaseSuppBankEntity = EntityTool.getEntityByGson(json, ComBaseSuppBankEntity.class);
			String sqlUpdate = CoreXmlSqlParsersFactory.getSql("ComBaseSuppBankDAL.Update");
			sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, comBaseSuppBankEntity);
			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 {
			ComBaseSuppBankEntity comBaseSuppBankEntity = EntityTool.getEntityByGson(json, ComBaseSuppBankEntity.class);
			String sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseSuppBankDAL.Delete");
			sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, comBaseSuppBankEntity);
			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 bankAccount, String id){
		SqlBuilder b = new SqlBuilder("ComBaseSuppBankDAL.IsRepeat");
		b.Fill(new String[]{bankAccount, id});
		return this.getData(b.toString());
	}
}
