package com.steering.pss.qcm.DAL;

import java.sql.Connection;
import java.util.ArrayList;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
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.ComBaseImpactCEntity;
import com.steering.pss.qcm.model.GenericEntity;

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

 /**
     * 通用查询
     */
    public CoreReturnObject Query(String id, String validflag){
    	SqlBuilder b = new SqlBuilder("ComBaseImpactCDAL.Query");
    	b.Fill(new String[]{ id, validflag });
    	return this.getData(b.toString());
    }

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

    /**
     * 保存
     */
    public CoreReturnObject Save(ArrayList<String> jsons){
    TransctionTool transction = new TransctionTool();
    try{
            Connection con = transction.getCon();
            GenericEntity maxPk = null;
            for(String json : jsons){
                ComBaseImpactCEntity comBaseImpactCEntity = EntityTool.getEntityByGson(json, ComBaseImpactCEntity.class);
                String sqlSave = "";
                if(comBaseImpactCEntity.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseImpactCDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBaseImpactCDAL.GetMaxPk");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, comBaseImpactCEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    //comBaseImpactCEntity.setPk(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseImpactCDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, comBaseImpactCEntity);
                if(!transction.ExcuteTransction(sqlSave)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }

    /**
     * 修改有效标志
     */
    public CoreReturnObject UpdateValidflag(ArrayList<String> jsons, String validflag){
        TransctionTool transction = new TransctionTool();
    try{
            Connection con = transction.getCon();
            for(String json : jsons){
                ComBaseImpactCEntity comBaseImpactCEntity = EntityTool.getEntityByGson(json, ComBaseImpactCEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseImpactCDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseImpactCDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, comBaseImpactCEntity);
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
}
