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.ComBasePurchaseAgreementEntity;
import com.steering.pss.qcm.model.ComBasePurchaseProviderEntity;
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_AGREEMENT所对应的Java类（生成工具：代码生成工具4.0
 * 访问地址：http://172.16.2.128/tool/）
 * 
 * @author tgcx-test date 2016-07-26
 */
public class ComBasePurchaseAgreementDAL extends DBCommon {

	/**
	 * 通用查询
	 */
	public CoreReturnObject Query(String agreementDesc, String validflag) {
		SqlBuilder b = new SqlBuilder("ComBasePurchaseAgreementDAL.Query");
		b.BuildLike("AGREEMENT_DESC", agreementDesc, SqlBuilder.Empty);
		b.Fill("VALIDFLAG", validflag);
		return this.getData(b.toString());
	}

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

	/**
	 * 保存
	 */
	public CoreReturnObject Save(String json, ArrayList<String> jsonProviders, ArrayList<String> jsonStdIdxs) {
		TransctionTool transction = new TransctionTool();
		try {
			Connection con = transction.getCon();
			GenericEntity maxPk = null;
			
			if(!json.equals("")){
				ComBasePurchaseAgreementEntity comBasePurchaseAgreementEntity = EntityTool.getEntityByGson(json,
						ComBasePurchaseAgreementEntity.class);
				
				String sqlSave = "";
				if (comBasePurchaseAgreementEntity.getValidflag().equals("")) {
					sqlSave = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseAgreementDAL.Insert");
					String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseAgreementDAL.GetMaxPk");
					sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, comBasePurchaseAgreementEntity);
					maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
					comBasePurchaseAgreementEntity.setAgreementCode(maxPk.getMaxValue());
				} else {
					//如果内控标准发生改变，需要清除向下数据
					String sqlGetPurchaseAgreement = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseAgreementDAL.QueryByPk");
					sqlGetPurchaseAgreement = EntityTool.fillSqlByEntity(sqlGetPurchaseAgreement, comBasePurchaseAgreementEntity);
					ComBasePurchaseAgreementEntity purchaseAgreement = EntityTool.getEntityBySqlNoClsCon(
							sqlGetPurchaseAgreement, con, ComBasePurchaseAgreementEntity.class);
					if(!comBasePurchaseAgreementEntity.getStdCodeNk().equals(purchaseAgreement.getStdCodeNk())){
						String sqlClear = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseAgreementDAL.Clear");
						sqlClear = EntityTool.fillSqlByEntity(sqlClear, comBasePurchaseAgreementEntity);
						if (!transction.ExcuteTransction(sqlClear)) {
							return transction.rollBack();
						}
					}
					
					sqlSave = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseAgreementDAL.Update");
				}
				sqlSave = EntityTool.fillSqlByEntity(sqlSave, comBasePurchaseAgreementEntity);
				if (!transction.ExcuteTransction(sqlSave)) {
					return transction.rollBack();
				}
				
			}
			
			if(jsonProviders.size() > 0)
			{
				ComBasePurchaseProviderEntity provider = EntityTool.getEntityByJSONStr(jsonProviders.get(0), ComBasePurchaseProviderEntity.class);
				String sqlDelProvider = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseProviderDAL.Delete2");
				sqlDelProvider = EntityTool.fillSqlByEntity(sqlDelProvider, provider);
				if(!transction.ExcuteTransction(sqlDelProvider)){
	                return transction.rollBack();
	            }
			}
			for(String jsonProvider : jsonProviders){
				ComBasePurchaseProviderEntity provider = EntityTool.getEntityByJSONStr(jsonProvider, ComBasePurchaseProviderEntity.class);
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseProviderDAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, provider);
                if(!transction.ExcuteTransction(sqlInsert)){
                    return transction.rollBack();
                }
            }
			
			if(jsonStdIdxs.size() > 0){
				ComBasePurchaseStdIdxEntity comBasePurchaseStdIdxEntity = EntityTool.getEntityByGson(jsonStdIdxs.get(0),
						ComBasePurchaseStdIdxEntity.class);
				String sqlDelStdIdx = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseStdIdxDAL.Delete2");
				sqlDelStdIdx = EntityTool.fillSqlByEntity(sqlDelStdIdx, comBasePurchaseStdIdxEntity);
				if (!transction.ExcuteTransction(sqlDelStdIdx)) {
					return transction.rollBack();
				}
			}
			
			for (String jsonStdIdx : jsonStdIdxs) {
				ComBasePurchaseStdIdxEntity comBasePurchaseStdIdxEntity = EntityTool.getEntityByGson(jsonStdIdx,
						ComBasePurchaseStdIdxEntity.class);
				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 UpdateValidflag(String json, String validflag) {
		TransctionTool transction = new TransctionTool();
		try {
			ComBasePurchaseAgreementEntity comBasePurchaseAgreementEntity = EntityTool.getEntityByGson(json,
					ComBasePurchaseAgreementEntity.class);
			String sqlUpdateValidflag = "";
			if (validflag.equals("0")) {
				sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseAgreementDAL.Delete");
				
				String deleteStdIdx = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseStdIdxDAL.DeleteByAgreementCode");
				deleteStdIdx = EntityTool.fillSqlByEntity(deleteStdIdx, comBasePurchaseAgreementEntity);
				if (!transction.ExcuteTransction(deleteStdIdx)) {
					return transction.rollBack();
				}
			} else {
				sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseAgreementDAL.Resume");
			}
			sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, comBasePurchaseAgreementEntity);
			if (!transction.ExcuteTransction(sqlUpdateValidflag)) {
				return transction.rollBack();
			}
		} catch (Exception ex) {
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		return transction.commit();
	}
}
