package com.fgkj.dao.impl; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStream; import java.sql.Blob; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import sun.misc.BASE64Encoder; import com.fgkj.dao.BaseDAO; import com.fgkj.dao.CallBack; import com.fgkj.dao.DAOHelper; import com.fgkj.db.DBUtil; import com.fgkj.dto.User_Chart; import com.fgkj.dto.User_inf; public class User_ChartImpl implements BaseDAO,CallBack{ public List getResults(ResultSet rs) { List list=new ArrayList(); try { while(rs.next()){ User_Chart uchart=new User_Chart(); uchart.setNum(rs.getInt("num")); uchart.setChart_name(rs.getString("chart_name")); uchart.setChart_file(rs.getString("chart_file")); uchart.setChart_logo_blob(rs.getBlob("chart_logo")); uchart.setChart_normal_blob(rs.getBlob("chart_normal")); uchart.setChart_behind_blob(rs.getBlob("chart_behind")); uchart.setChart_warn_blob(rs.getBlob("chart_warn")); uchart.setChart_timeout_blob(rs.getBlob("chart_timeout")); list.add(uchart); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } //添加图标 public boolean add(Object obj) { User_Chart uchart=(User_Chart) obj; List list=new ArrayList(); list.add(uchart.getChart_name()); list.add(uchart.getChart_file()); list.add(uchart.getChart_logo()); list.add(uchart.getChart_normal()); list.add(uchart.getChart_behind()); list.add(uchart.getChart_warn()); list.add(uchart.getChart_timeout()); list.add(uchart.getChart_nuclear_cap()); list.add(uchart.getChart_dev_alarm()); list.add(uchart.getChart_pre_charge()); list.add(uchart.getChart_online_charge()); list.add(uchart.getChart_poff()); list.add(uchart.getChart_install()); list.add(uchart.getChart_xuhang()); String sql="insert into web_site.tb_user_Chart(chart_name,chart_file,Chart_logo,Chart_normal,Chart_behind,Chart_warn,Chart_timeout,Chart_nuclear_cap,Chart_dev_alarm,Chart_pre_charge,Chart_online_charge,Chart_poff,Chart_install,Chart_xuhang) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; boolean bl=DAOHelper.executeUpdatePicture(DBUtil.getConn(), sql, list); return bl; } //修改图标信息 public boolean update(Object obj) { User_Chart uchart=(User_Chart) obj; List list=new ArrayList(); String sql="update web_site.tb_user_Chart SET"; //logo图片 String logoSql=" Chart_logo=? "; if(uchart.getChart_logo()!=null&&uchart.getChart_logo().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_logo()); sql+=logoSql; } //normal图片 String normalSql=" Chart_normal=? "; if(uchart.getChart_normal()!=null&&uchart.getChart_normal().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_normal()); sql+=normalSql; } //behind图片 String behindSql=" Chart_behind=? "; if(uchart.getChart_behind()!=null&&uchart.getChart_behind().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_behind()); sql+=behindSql; } //warn图片 String warnSql=" Chart_warn=? "; if(uchart.getChart_warn()!=null&&uchart.getChart_warn().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_warn()); sql+=warnSql; } //time_out图片 String time_outSql=" Chart_timeout=? "; if(uchart.getChart_timeout()!=null&&uchart.getChart_timeout().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_timeout()); sql+=time_outSql; } //nuclear_cap图片 String nuclear_capSql=" Chart_nuclear_cap=? "; if(uchart.getChart_nuclear_cap()!=null&&uchart.getChart_nuclear_cap().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_nuclear_cap()); sql+=nuclear_capSql; } //dev_alarm图片 String dev_alarmSql=" Chart_dev_alarm=? "; if(uchart.getChart_dev_alarm()!=null&&uchart.getChart_dev_alarm().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_dev_alarm()); sql+=dev_alarmSql; } //pre_charge图片 String pre_chargeSql=" Chart_pre_charge=? "; if(uchart.getChart_pre_charge()!=null&&uchart.getChart_pre_charge().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_pre_charge()); sql+=pre_chargeSql; } //online_charge图片 String online_chargeSql=" Chart_online_charge=? "; if(uchart.getChart_online_charge()!=null&&uchart.getChart_online_charge().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_online_charge()); sql+=online_chargeSql; } //Chart_poff图片 String poffSql=" Chart_poff=? "; if(uchart.getChart_poff()!=null&&uchart.getChart_poff().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_poff()); sql+=poffSql; } //Chart_install图片 String installSql=" Chart_install=? "; if(uchart.getChart_install()!=null&&uchart.getChart_install().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_install()); sql+=installSql; } //Chart_xuhang图片 String xuhangSql=" Chart_xuhang=? "; if(uchart.getChart_xuhang()!=null&&uchart.getChart_xuhang().length()>0){ if(list.size()>0){ sql+=","; } list.add(uchart.getChart_xuhang()); sql+=xuhangSql; } //endSql String endSql=" where chart_file=? "; list.add(uchart.getChart_file()); sql+=endSql; boolean bl=DAOHelper.executeUpdatePicture(DBUtil.getConn(), sql, list); return bl; } //删除图标 public boolean del(Object obj) { User_Chart uchart=(User_Chart) obj; List list=new ArrayList(); list.add(uchart.getChart_file()); String sql="delete from web_site.tb_user_Chart where chart_file=?"; boolean bl=DAOHelper.executeUpdatePicture(DBUtil.getConn(), sql, list); return bl; } //查询所有chart_file public List searchAll() { String sql="select num,chart_name,chart_file from web_site.tb_user_Chart order by num "; List list=DAOHelper.executeQueryPicture(sql, DBUtil.getConn(), null, new CallBack() { public List getResults(ResultSet rs) { List list=new ArrayList(); try { while(rs.next()){ User_Chart uchart=new User_Chart(); uchart.setNum(rs.getInt("num")); uchart.setChart_name(rs.getString("chart_name")); uchart.setChart_file(rs.getString("chart_file")); list.add(uchart); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }); return list; } //根据用户id查图表 public List serchByCondition(Object obj) { User_inf uinf=(User_inf) obj; String sql="select distinct(chart_file) from web_site.tb_user_Chart,db_user.tb_user_inf " + " where web_site.tb_user_Chart.chart_name=db_user.tb_user_inf.ujobgroup " + " and db_user.tb_user_inf.uname=?" ; List list=DAOHelper.executeQuery(sql, DBUtil.getConn(),new Object[]{uinf.getUName()},new CallBack() { public List getResults(ResultSet rs) { List list=new ArrayList(); try { while(rs.next()){ User_Chart uchart=new User_Chart(); uchart.setChart_file(rs.getString("chart_file")); list.add(uchart); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }); return list; } //根据chart_file和列名查询图片(Chart_name放列名字) public List serchByInfo(Object obj){ final User_Chart uchart=(User_Chart) obj; String sql="select "+uchart.getChart_name()+" from web_site.tb_user_Chart where chart_file=? "; List list=DAOHelper.executeQueryPicture(sql, DBUtil.getConn(), new Object[]{uchart.getChart_file()}, new CallBack() { public List getResults(ResultSet rs) { List list=new ArrayList(); try { while(rs.next()){ Blob blob=rs.getBlob(uchart.getChart_name()); list.add(blob); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }); return list; } //根据chart_file和列名查询图片(Chart_name放列名字) public List serchByInfo1(Object obj){ User_Chart uchart=(User_Chart) obj; String sql="select Chart_logo,Chart_normal,Chart_behind,Chart_warn,Chart_timeout,Chart_poff,Chart_install" + ",Chart_nuclear_cap,Chart_dev_alarm,Chart_pre_charge,Chart_online_charge,Chart_xuhang " + "from web_site.tb_user_Chart where chart_file=? "; List list=DAOHelper.executeQueryPicture(sql, DBUtil.getConn(), new Object[]{uchart.getChart_file()}, new CallBack() { public List getResults(ResultSet rs) { List list=new ArrayList(); try { while(rs.next()){ User_Chart u=new User_Chart(); u.setChart_logo_str(convertBlobToBase64String(rs.getBlob("Chart_logo"))); u.setChart_normal_str(convertBlobToBase64String(rs.getBlob("Chart_normal"))); u.setChart_behind_str(convertBlobToBase64String(rs.getBlob("Chart_behind"))); u.setChart_warn_str(convertBlobToBase64String(rs.getBlob("Chart_warn"))); u.setChart_timeout_str(convertBlobToBase64String(rs.getBlob("Chart_timeout"))); u.setChart_poff_str(convertBlobToBase64String(rs.getBlob("Chart_poff"))); u.setChart_install_str(convertBlobToBase64String(rs.getBlob("Chart_install"))); u.setChart_nuclear_cap_str(convertBlobToBase64String(rs.getBlob("Chart_nuclear_cap"))); u.setChart_dev_alarm_str(convertBlobToBase64String(rs.getBlob("Chart_dev_alarm"))); u.setChart_pre_charge_str(convertBlobToBase64String(rs.getBlob("Chart_pre_charge"))); u.setChart_online_charge_str(convertBlobToBase64String(rs.getBlob("Chart_online_charge"))); u.setChart_xuhang_str(convertBlobToBase64String(rs.getBlob("Chart_xuhang"))); list.add(u); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }); return list; } public static String convertBlobToBase64String(Blob blob) { String result = ""; if(null != blob) { try { InputStream msgContent = blob.getBinaryStream(); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[100]; int n = 0; while (-1 != (n = msgContent.read(buffer))) { output.write(buffer, 0, n); } result =new BASE64Encoder().encode(output.toByteArray()) ; output.close(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return "data:text/html;base64,"+result; }else { return null; } } public static void main(String[] args) { User_ChartImpl uimpl=new User_ChartImpl(); User_Chart uchart=new User_Chart(); uchart.setChart_file("whyc"); uimpl.serchByInfo1(uchart); } }