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.QcmZbsTemplateRMscSpecEntity;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

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

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

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscSpecDAL.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){
                QcmZbsTemplateRMscSpecEntity qcmZbsTemplateRMscSpecEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateRMscSpecEntity.class);
                String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscSpecDAL.GetMaxPk");
                sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, qcmZbsTemplateRMscSpecEntity);
                maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                //QcmZbsTemplateRMscSpecEntity.setPk(maxPk.getMaxValue());
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscSpecDAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, qcmZbsTemplateRMscSpecEntity);
                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){
                QcmZbsTemplateRMscSpecEntity qcmZbsTemplateRMscSpecEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateRMscSpecEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscSpecDAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, qcmZbsTemplateRMscSpecEntity);
                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){
                QcmZbsTemplateRMscSpecEntity qcmZbsTemplateRMscSpecEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateRMscSpecEntity.class);
                String sqlSave = "";
                if(qcmZbsTemplateRMscSpecEntity.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscSpecDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscSpecDAL.GetMaxPk");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, qcmZbsTemplateRMscSpecEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    //qcmZbsTemplateRMscSpecEntity.setPk(maxPk.getMaxValue());
                }else{
                sqlSave = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscSpecDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, qcmZbsTemplateRMscSpecEntity);
                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){
                QcmZbsTemplateRMscSpecEntity qcmZbsTemplateRMscSpecEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateRMscSpecEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscSpecDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscSpecDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, qcmZbsTemplateRMscSpecEntity);
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
}
