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

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

	/**
	 * 通用查询
	 */
	public CoreReturnObject Query(String stdIdxType, String agreementCode) {
		SqlBuilder b = new SqlBuilder("ComBasePurchaseStdIdxDAL.Query");
		b.Fill("AGREEMENT_CODE", agreementCode);
		b.Fill("STD_IDX_TYPE", stdIdxType);
		return this.getData(b.toString());
	}

	/**
	 * 通过主键查询
	 */
	public CoreReturnObject QueryByPk(String agreementCode, String stdIdxType, String stdIdx) {
		String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseStdIdxDAL.QueryByPk");
		return this.getData(sqlQuery);
	}

	/**
	 * 新增
	 */
	public CoreReturnObject Insert(ArrayList<String> jsons) {
		TransctionTool transction = new TransctionTool();
		try {
			Connection con = transction.getCon();
			GenericEntity maxPk = null;
			for (String json : jsons) {
				ComBasePurchaseStdIdxEntity comBasePurchaseStdIdxEntity = EntityTool.getEntityByGson(json,
						ComBasePurchaseStdIdxEntity.class);
				String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseStdIdxDAL.GetMaxPk");
				sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, comBasePurchaseStdIdxEntity);
				maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
				// comBasePurchaseStdIdxEntity.setPk(maxPk.getMaxValue());
				String sqlInsert = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseStdIdxDAL.Insert");
				sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, comBasePurchaseStdIdxEntity);
				if (!transction.ExcuteTransction(sqlInsert)) {
					return transction.rollBack();
				}
			}
		} catch (Exception ex) {
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		return transction.commit();
	}

	/**
	 * 修改
	 */
	public CoreReturnObject Update(ArrayList<String> jsons) {
		TransctionTool transction = new TransctionTool();
		try {
			Connection con = transction.getCon();
			for (String json : jsons) {
				ComBasePurchaseStdIdxEntity comBasePurchaseStdIdxEntity = EntityTool.getEntityByGson(json,
						ComBasePurchaseStdIdxEntity.class);
				String sqlUpdate = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseStdIdxDAL.Update");
				sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, comBasePurchaseStdIdxEntity);
				if (!transction.ExcuteTransction(sqlUpdate)) {
					return transction.rollBack();
				}
			}
		} catch (Exception ex) {
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		return transction.commit();
	}
}
