package com.lhd.dao.xmgl.jcxx;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
  *****
public class ConfigDictionaryDAO {

 private JdbcTemplate jdbcTemplate;
 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  this.jdbcTemplate = jdbcTemplate;
 }
 //查询
 public List list(){
  final List list = new ArrayList();
   String sql = "select * from config order by code" ;
   jdbcTemplate.query(sql,new RowCallbackHandler(){
   public void proce***ow(ResultSet rs) throws SQLException {
    final ConfigDictionaryModel configDictionaryModel = new ConfigDictionaryModel();
    configDictionaryModel.setCode(rs.getString(1));
    configDictionaryModel.setName(Util.IsoToGB2312(rs.getString(2)));
    configDictionaryModel.setValue(rs.getString(3));
    list.add(configDictionaryModel);
   }
  });
  return list ;
 }
 //插入
 public void insert(final ConfigDictionaryModel configDictionaryModel){
 
   String sql_insert = "insert into config(code,name,value) values(?,?,?) " ; 
   jdbcTemplate.update(sql_insert,
   new PreparedStatementSetter() {
    public void setValues(PreparedStatement ps) throws SQLException {
     ps.setString(1, configDictionaryModel.getCode().trim()); //编号
     ps.setString(2, configDictionaryModel.getName().trim()); //名称
     ps.setString(3, configDictionaryModel.getValue().trim()); //简码
    }
  });
 }
 //修改
 public void update(final ConfigDictionaryModel configDictionaryModel){
   String sql_update = "update config set name = ?,value = ? where code = ? " ;
   jdbcTemplate.update(sql_update,
    new PreparedStatementSetter() {
     public void setValues(PreparedStatement ps) throws SQLException {
      ps.setString(1, configDictionaryModel.getName()); //
      ps.setString(2, configDictionaryModel.getValue()); //
      ps.setString(3, configDictionaryModel.getCode()); //
     }
  });

 }
  //删除
 public int delete(String code){
   String sql = "delete from config where code = '" + code + "'" ;
  
   int count = jdbcTemplate.update(sql) ;
  return count ;
 }
 public String autoGetBh(){
   String sql = "select isnull(max(code),'0000') from config";
  return Util.addOne((String)jdbcTemplate.queryForObject(sql, String.class));
 }
 
 public int getCount(String code){
  String sql = "select count(*) from config where code like '"+code+"%'";
  int count = jdbcTemplate.queryForInt(sql);
  return count;
 }
 //检查系统是否存在编号
 public boolean isExist(String code,String type){
  boolean exist = false ;
  String sql = null ;
  if(type.equals("insert")){ //增加
   sql = "select count(*) from config "
    + "where code = '"+code+"'";
  }else if(type.equals("update")){ //修改
   sql = "select count(*) from config "
    + "where code = '"+code+"'";
  }
  
  int count = jdbcTemplate.queryForInt(sql);
  if(count > 0){
   exist = true ;
  }
  
  return exist ;  
 }

 //修改查name段
 public List updateWithCode(String code){
  final List list = new ArrayList();
  String sql = "select * from config where code = '" + code + "'";
  jdbcTemplate.query(sql,new RowCallbackHandler(){
   public void proce***ow(ResultSet rs) throws SQLException {
    final ConfigDictionaryModel configDictionaryModel = new ConfigDictionaryModel();
    configDictionaryModel.setCode(rs.getString(1));
    configDictionaryModel.setName(Util.IsoToGB2312(rs.getString(2)));
    configDictionaryModel.setValue(rs.getString(3));
    list.add(configDictionaryModel);
   }
  });
  return list ;
 }

}