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

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

 /**
     * 通用查询
     */
    public CoreReturnObject Query(String agreementCode){
    	SqlBuilder b = new SqlBuilder("ComBasePurchaseProviderDAL.Query");
    	b.Fill(new String[]{agreementCode});
    	
    	return this.getData(b.toString());
    }
    
    /**
     * 查询供应商基础数据
     * @return
     */
    public CoreReturnObject QueryProviderBaseData(){
    	String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseProviderDAL.QueryProviderBaseData");
        return this.getData(sqlQuery);
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String agreementCode, String suppCode){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseProviderDAL.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){
                ComBasePurchaseProviderEntity comBasePurchaseProviderEntity = EntityTool.getEntityByGson(json, ComBasePurchaseProviderEntity.class);
                String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseProviderDAL.GetMaxPk");
                sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, comBasePurchaseProviderEntity);
                maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                //comBasePurchaseProviderEntity.setPk(maxPk.getMaxValue());
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseProviderDAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, comBasePurchaseProviderEntity);
                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){
                ComBasePurchaseProviderEntity comBasePurchaseProviderEntity = EntityTool.getEntityByGson(json, ComBasePurchaseProviderEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseProviderDAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, comBasePurchaseProviderEntity);
                if(!transction.ExcuteTransction(sqlUpdate)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
}
