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

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

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

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscDAL.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){
                QcmZbsTemplateRMscEntity qcmZbsTemplateRMscEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateRMscEntity.class);
                String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscDAL.GetMaxPk");
                sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, qcmZbsTemplateRMscEntity);
                maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                //QcmZbsTemplateRMscEntity.setPk(maxPk.getMaxValue());
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscDAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, qcmZbsTemplateRMscEntity);
                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){
                QcmZbsTemplateRMscEntity qcmZbsTemplateRMscEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateRMscEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscDAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, qcmZbsTemplateRMscEntity);
                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, ArrayList<String> mscSpecJsons){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            GenericEntity maxPk = null;
            for(String json : jsons){
            	QcmZbsTemplateRMscEntity qcmZbsTemplateRMscEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateRMscEntity.class);
                String sqlSave = "";
                if(qcmZbsTemplateRMscEntity.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscDAL.GetMaxPk");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, qcmZbsTemplateRMscEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    qcmZbsTemplateRMscEntity.setPrimaryKey(maxPk.getMaxValue());
                }else{
                	sqlSave = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscDAL.Update");
                }
                	sqlSave = EntityTool.fillSqlByEntity(sqlSave, qcmZbsTemplateRMscEntity);
                if(!transction.ExcuteTransction(sqlSave)){
                    return transction.rollBack();
                }
            }
            
            for(String json : mscSpecJsons){
                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.setPrimaryKey(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, ArrayList<String> mscSpecJsons, String validflag){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            for(String json : jsons){
                QcmZbsTemplateRMscEntity qcmZbsTemplateRMscEntity = EntityTool.getEntityByGson(json, QcmZbsTemplateRMscEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscDAL.Delete");
                    String sqlDeleteRSpec = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscSpecDAL.DeleteByParentPk");
                    sqlDeleteRSpec = EntityTool.fillSqlByEntity(sqlDeleteRSpec, qcmZbsTemplateRMscEntity);
                    if(!transction.ExcuteTransction(sqlDeleteRSpec)){
                        return transction.rollBack();
                    }
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, qcmZbsTemplateRMscEntity);
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    return transction.rollBack();
                }
            }
            
            for(String json : mscSpecJsons){
                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();
    }
    
    public CoreReturnObject GetZbsRMscCnt(String msc, String tbh){
    	String sql = CoreXmlSqlParsersFactory.getSql("QcmZbsTemplateRMscDAL.GetZbsRMscCnt");
    	sql = StringTool.ReplaceAll(sql, "#MSC#", msc);
    	sql = StringTool.ReplaceAll(sql, "#TBH#", tbh);
    	return this.getData(sql);
    }
}
