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

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

	/**
	 * 通用查询
	 */
	public CoreReturnObject Query(String suppCode, String suppName, String suppLinkMan, String validflag) {
		SqlBuilder b = new SqlBuilder("ComBaseSuppDAL.Query");
		b.AppendLn("AND T.SUPP_CODE LIKE UPPER('%'||{0}||'%')", !suppCode.equals(""));
		b.AppendLn("AND T.SUPP_NAME LIKE UPPER('%'||{1}||'%')", !suppName.equals(""));
		b.AppendLn("AND T.SUPPLIER_LINK_MAN LIKE UPPER('%'||{2}||'%')", !suppLinkMan.equals(""));
		b.AppendLn("AND T.VALIDFLAG = '1'", validflag.equals("1"));
		b.Fill(new String[] { suppCode, suppName, suppLinkMan });

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

	/**
	 * 通过主键查询
	 */
	public CoreReturnObject QueryByPk(String suppCode) {
		SqlBuilder b = new SqlBuilder("ComBaseSuppDAL.QueryByPk");
		b.Fill(new String[]{ suppCode });
		return this.getData(b.toString());
	}

	/**
	 * 新增
	 */
	public CoreReturnObject Insert(String jsonSupp) {
		TransctionTool transction = new TransctionTool();
		String suppCode = "";
		try {
			Connection con = transction.getCon();
			GenericEntity maxSuppCode = EntityTool.getEntityBySqlNoClsCon(
					CoreXmlSqlParsersFactory.getSql("ComBaseSuppDAL.GetMaxPk"), con, GenericEntity.class);
			ComBaseSuppEntity comBaseSuppEntity = EntityTool.getEntityByGson(jsonSupp, ComBaseSuppEntity.class);
			comBaseSuppEntity.setSuppCode(maxSuppCode.getMaxValue());
			suppCode = comBaseSuppEntity.getSuppCode();
			String sqlInsert = CoreXmlSqlParsersFactory.getSql("ComBaseSuppDAL.Insert");
			sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, comBaseSuppEntity);
			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(suppCode);
		return cro;
	}

	/**
	 * 修改
	 */
	public CoreReturnObject Update(String jsonSupp) {
		TransctionTool transction = new TransctionTool();
		try {
			ComBaseSuppEntity comBaseSuppEntity = EntityTool.getEntityByGson(jsonSupp, ComBaseSuppEntity.class);
			String sqlUpdate = CoreXmlSqlParsersFactory.getSql("ComBaseSuppDAL.Update");
			sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, comBaseSuppEntity);
			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 UpdateValidflag(String json, String validflag) {
		TransctionTool transction = new TransctionTool();
		try {
			ComBaseSuppEntity comBaseSuppEntity = EntityTool.getEntityByGson(json, ComBaseSuppEntity.class);
			String sqlUpdateValidflag = "";
			if (validflag.equals("0")) {
				sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseSuppDAL.Delete");
			} else {
				sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseSuppDAL.Resume");
			}
			sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, comBaseSuppEntity);
			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 QueryRegion(){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseSuppDAL.QueryRegion");
		return this.getData(sql);
	}
	
	public CoreReturnObject IsRepeat(String suppName, String suppCode){
		SqlBuilder b = new SqlBuilder("ComBaseSuppDAL.IsRepeat");
		b.Fill(new String[]{suppName, suppCode});
		return this.getData(b.toString());
	}
}
