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.StringTool;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.qcm.model.QcmZbsTemplateHeadEntity;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

/**
 * 数据库表QCM_ZBS_TEMPLATE_HEAD所对应的Java类（生成工具：代码生成器）
 * @author tgcx-test   date 2016-05-13
 */
public class QcmZbsTemplateHeadDAL extends DBCommon{

    /**
     * 通用查询
     */
    public CoreReturnObject Query(String decription, String validflag){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateHeadDAL.Query");
        sqlQuery = StringTool.ReplaceAll(sqlQuery, "#DESCRIPTION#", decription);
        sqlQuery = StringTool.ReplaceAll(sqlQuery, "#VALIDFLAG#", validflag);
        return this.getData(sqlQuery);
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String tbh){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateHeadDAL.QueryByPk");
        sqlQuery = StringTool.ReplaceAll(sqlQuery, "#TBH#", tbh);
        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){
                QcmZbsTemplateHeadEntity qcmZbsTemplateHeadEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateHeadEntity.class);
                String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateHeadDAL.GetMaxPk");
                sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, qcmZbsTemplateHeadEntity);
                maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                qcmZbsTemplateHeadEntity.setTbh(maxPk.getMaxValue());
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateHeadDAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, qcmZbsTemplateHeadEntity);
                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){
                QcmZbsTemplateHeadEntity qcmZbsTemplateHeadEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateHeadEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateHeadDAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, qcmZbsTemplateHeadEntity);
                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 Save(ArrayList<String> jsons){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            GenericEntity maxPk = null;
            for(String json : jsons){
                QcmZbsTemplateHeadEntity qcmZbsTemplateHeadEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateHeadEntity.class);
                String sqlSave = "";
                if(qcmZbsTemplateHeadEntity.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateHeadDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateHeadDAL.GetMaxPk");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, qcmZbsTemplateHeadEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    qcmZbsTemplateHeadEntity.setTbh(maxPk.getMaxValue());
                }else{
                sqlSave = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateHeadDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, qcmZbsTemplateHeadEntity);
                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){
                QcmZbsTemplateHeadEntity qcmZbsTemplateHeadEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateHeadEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateHeadDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateHeadDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, qcmZbsTemplateHeadEntity);
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
    
    /**
     * 修改xls
     */
    public CoreReturnObject UpdateXls(String json){
        TransctionTool transction = new TransctionTool();
        try{
            QcmZbsTemplateHeadEntity qcmZbsTemplateHeadEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateHeadEntity.class);
            String sqlUpdate = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateHeadDAL.UpdateXls");
            sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, qcmZbsTemplateHeadEntity);
            if(!transction.ExcuteTransction(sqlUpdate)){
                return transction.rollBack();
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
}
