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

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

    /**
     * 通用查询
     */
    public CoreReturnObject Query(String sortCode, String baseName, String validflag){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBaseInfoDAL.Query");
        sqlQuery = StringTool.ReplaceAll(sqlQuery, "#SORTCODE#", sortCode);
        sqlQuery = StringTool.ReplaceAll(sqlQuery, "#BASENAME#", baseName);
        sqlQuery = StringTool.ReplaceAll(sqlQuery, "#VALIDFLAG#", validflag);
        return this.getData(sqlQuery);
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String basecode){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBaseInfoDAL.QueryByPk");
        return this.getData(sqlQuery);
    }
    
    /**
     * 查询基础数据名称是否存在
     */
    public CoreReturnObject QueryBaseNameCnt(String sortCode, String baseName, String baseCode){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBaseInfoDAL.QueryBaseNameCnt");
        sqlQuery = StringTool.ReplaceAll(sqlQuery, "#SORTCODE#", sortCode);
        sqlQuery = StringTool.ReplaceAll(sqlQuery, "#BASENAME#", baseName);
        sqlQuery = StringTool.ReplaceAll(sqlQuery, "#BASECODE#", baseCode);
        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){
                ComBaseInfoEntity comBaseInfoEntity = EntityTool.getEntityByGson(json, ComBaseInfoEntity.class);
                String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBaseInfoDAL.GetMaxPk");
                sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, comBaseInfoEntity);
                maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                comBaseInfoEntity.setBasecode(maxPk.getMaxValue());
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("ComBaseInfoDAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, comBaseInfoEntity);
                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){
                ComBaseInfoEntity comBaseInfoEntity = EntityTool.getEntityByGson(json, ComBaseInfoEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("ComBaseInfoDAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, comBaseInfoEntity);
                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){
                ComBaseInfoEntity comBaseInfoEntity = EntityTool.getEntityByGson(json, ComBaseInfoEntity.class);
                String sqlSave = "";
                if(comBaseInfoEntity.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseInfoDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBaseInfoDAL.GetMaxPk");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, comBaseInfoEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    comBaseInfoEntity.setBasecode(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseInfoDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, comBaseInfoEntity);
                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){
                ComBaseInfoEntity comBaseInfoEntity = EntityTool.getEntityByGson(json, ComBaseInfoEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseInfoDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseInfoDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, comBaseInfoEntity);
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
}
