package com.steering.pss.kaohe.server;

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.kaohe.entity.RpKaoheItemquanzhongEntity;
import com.steering.pss.qcm.model.GenericEntity;

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

    /**
     * 通用查询
     */
    public CoreReturnObject Query(String ITEM_NAME, String VALIDFLAG){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("RpKaoheItemquanzhongDAL.Query");
        
        
       
        sqlQuery = sqlQuery.replaceAll("#ITEM_NAME#", ITEM_NAME);
        sqlQuery = sqlQuery.replaceAll("#VALIDFLAG#", VALIDFLAG);
        
        
        return this.getData(sqlQuery);
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("RpKaoheItemquanzhongDAL.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){
                RpKaoheItemquanzhongEntity rpKaoheItemquanzhongEntity = EntityTool.getEntityByGson(json, RpKaoheItemquanzhongEntity.class);
                String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("RpKaoheItemquanzhongDAL.GetMaxPk");
                sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, rpKaoheItemquanzhongEntity);
                maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                //rpKaoheItemquanzhongEntity.setPk(maxPk.getMaxValue());
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("RpKaoheItemquanzhongDAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, rpKaoheItemquanzhongEntity);
                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){
                RpKaoheItemquanzhongEntity rpKaoheItemquanzhongEntity = EntityTool.getEntityByGson(json, RpKaoheItemquanzhongEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("RpKaoheItemquanzhongDAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, rpKaoheItemquanzhongEntity);
                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{
                    
            for(String json : jsons){
                RpKaoheItemquanzhongEntity rpKaoheItemquanzhongEntity = EntityTool.getEntityByGson(json, RpKaoheItemquanzhongEntity.class);
                String sqlSave = "";
                if(rpKaoheItemquanzhongEntity.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("RpKaoheItemquanzhongDAL.Insert");
                    //rpKaoheItemquanzhongEntity.setPk(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("RpKaoheItemquanzhongDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, rpKaoheItemquanzhongEntity);
                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{
            
            for(String json : jsons){
                RpKaoheItemquanzhongEntity rpKaoheItemquanzhongEntity = EntityTool.getEntityByGson(json, RpKaoheItemquanzhongEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("True")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("RpKaoheItemquanzhongDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("RpKaoheItemquanzhongDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, rpKaoheItemquanzhongEntity);
                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 queryItemtype() {
        return this.getDataBySqlID("RpKaoheItemquanzhongDAL.QueryItemtype");
    }
}
