b97355b4a4b5ec1f1b8a965378d2786984cda972.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package com.sinosoft.cm.common.impl;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import nl.justobjects.pushlet.util.Sys;
  9. import org.apache.log4j.Logger;
  10. import com.persistence.DBdll.SysOperator;
  11. import com.sinosoft.cm.CMHandlerResultSet;
  12. import com.sinosoft.cm.CMTemplate;
  13. import com.sinosoft.cm.common.DateUtil;
  14. import com.sinosoft.cm.common.StringUtils;
  15. import com.sinosoft.cm.common.dao.HeightChartDao;
  16. import com.sinosoft.cm.common.vo.HeightChart;
  17. import com.sinosoft.cm.ex.SQLStringException;
  18. import com.sinosoft.common.excel.JsonPluginsUtil;
  19. import com.sun.org.apache.bcel.internal.generic.NEW;
  20. import flex.messaging.io.ArrayList;
  21. public class HeightChartDaoImpl implements HeightChartDao {
  22. protected Logger log = Logger.getLogger(this.getClass());
  23. protected CMTemplate cmt=null;
  24. protected Map<String,String> map=null;
  25. public HeightChartDaoImpl() {
  26. cmt=new CMTemplate();
  27. map=new HashMap<String, String>();
  28. /*获取历史端记录的ID和时间*/
  29. map.put("getRecords", "SELECT APPEAR_TIME,REPORT_ID FROM REPORT_RECORDS "
  30. + " WHERE APPEAR_TIME BETWEEN TO_DATE(?, 'YYYY-MM-DD HH:MI:SS') "
  31. + " AND TO_DATE(?,'YYYY-MM-DD HH:MI:SS') AND COMP_ID =? AND REPORT_TYPE=? ORDER BY APPEAR_TIME ");
  32. map.put("getColumn", "select COMP_NAME from report_comp_dictionary where comp_id in (?)");
  33. /*应急演练*/
  34. map.put("YJYL", "SELECT DRILL_TYPE, COUNT(*) AS AMOUNT FROM EMC_AM_EMERGENCY_DRILL "
  35. + "WHERE IS_DEL='0' AND SPONSOR_COMP "
  36. + "IN ( select DEPT_ID from sys_department where instr(dept_path,?)>0 ) GROUP BY DRILL_TYPE;");
  37. /*应急人员*/
  38. map.put("YJRY", "SELECT TEAM_COMP_ID,TEAM_NAME, TEAM_MEMBER_NUMBER FROM ECM_AM_EMERGENCY_TEAM "
  39. + "WHERE IS_DEL='0' AND TEAM_COMP_ID "
  40. + "IN ( select DEPT_ID from sys_department where instr(dept_path,?)>0 )");
  41. /*应急预案*/
  42. map.put("YJYA", "SELECT PLAN_TYPE, COUNT( *) AMOUNT FROM ECM_AM_YJYASERVER "
  43. + " WHERE IS_DEL='0' AND UNITS IN ( "
  44. + "select DEPT_ID from sys_department where instr(dept_path,?)>0 ) GROUP BY PLAN_TYPE; ");
  45. /*发电车*/
  46. map.put("CARNUM","SELECT SUM(CASE WHEN CAR_NUM IS NULL THEN 0 WHEN CAR_NUM='' THEN 0 ELSE TO_NUMBER(CAR_NUM)END) AS AMOUNT "
  47. + " FROM EMC_AM_GENERATOR_CAR WHERE COMP_ID IN( "
  48. + " select DEPT_ID from sys_department where instr(dept_path,?)>0)");
  49. /*发电车*/
  50. map.put("NUM", "SELECT SUM(CASE WHEN NUM IS NULL THEN 0 WHEN NUM='' THEN 0 ELSE TO_NUMBER(NUM)END) AS AMOUNT "
  51. + "FROM EMC_AM_GENERATOR WHERE COMP_ID IN( "
  52. + " select DEPT_ID from sys_department where instr(dept_path,?)>0)");
  53. }
  54. public List<Map<String,String>> findAllBySqlName(String sqlName,List<String> sqlLabel,Object...args) throws Exception{
  55. List<Map<String,String>> list=new ArrayList();
  56. log.info(HeightChartDaoImpl.class+":findAllBySqlName(String sql,List<String> sqlLabel,Object...args) sql:"+map.get(sqlName));
  57. if(sqlLabel==null||0==sqlLabel.size())throw new Exception("查询接受字段异常");
  58. return cmt.queryToMap(map.get(sqlName), sqlLabel, args);
  59. }
  60. /**
  61. * 通过sql语句和标签查询多个字段
  62. * @param sql
  63. * @param sqlLabel
  64. * @param args
  65. * @return
  66. * @throws SQLStringException
  67. */
  68. public List<Map<String,String>> findAll(String sql,List<String> sqlLabel,Object...args) throws SQLStringException{
  69. //检查sql语句
  70. log.info(HeightChartDaoImpl.class+"sql:--------------------"+sql);
  71. if(sql==null && "".equals(sql.trim())) {
  72. throw new SQLStringException(HeightChartDaoImpl.class+"查询语句异常:--------------------"+sql);};
  73. //检查参数
  74. log.info(HeightChartDaoImpl.class+"参数:--------------------"+args);
  75. if(args==null && 0==args.length){
  76. throw new SQLStringException(HeightChartDaoImpl.class+"查询参数异常"+args);};
  77. //检查接受查询对象
  78. log.info(HeightChartDaoImpl.class+"参数:--------------------"+sqlLabel);
  79. if(args==null&& 0== sqlLabel.size()){
  80. throw new SQLStringException(HeightChartDaoImpl.class+"接受对象异常"+sqlLabel);};
  81. return (List<Map<String, String>>) cmt.queryToMap(sql, sqlLabel, args);
  82. }
  83. /**
  84. * 重载
  85. * 通过sql语句查询单个字段
  86. */
  87. public List<String> findAll(String sql,Object...args) throws SQLStringException{
  88. //检查sql语句
  89. log.info(HeightChartDaoImpl.class+"sql:--------------------"+sql);
  90. if(sql==null && "".equals(sql.trim())) {
  91. throw new SQLStringException(HeightChartDaoImpl.class+"查询语句异常:--------------------"+sql);};
  92. //检查参数
  93. log.info(HeightChartDaoImpl.class+"参数:--------------------"+args);
  94. if(args==null && 0==args.length){
  95. throw new SQLStringException(HeightChartDaoImpl.class+"查询参数异常"+args);};
  96. return (List<String>) cmt.queryToList(sql, args);
  97. }
  98. /**
  99. * 通过sql语句名查询IN语句
  100. * @param sqlName
  101. * @param list
  102. * @return
  103. * @throws SQLStringException
  104. */
  105. public List<String> findAllInId(String sqlName,List<String> list) throws SQLStringException{
  106. //检查sql语句
  107. log.info(HeightChartDaoImpl.class+"sql:--------------------"+map.get(sqlName));
  108. if(map.get(sqlName)==null && "".equals(map.get(sqlName).trim())) {
  109. throw new SQLStringException(HeightChartDaoImpl.class+"查询语句异常:--------------------"+map.get(sqlName));};
  110. //检查参数
  111. log.info(HeightChartDaoImpl.class+"参数:--------------------"+map.get(sqlName));
  112. if(list==null && 0==list.size()){
  113. throw new SQLStringException(HeightChartDaoImpl.class+"查询参数异常"+list);};
  114. StringBuffer str=new StringBuffer();
  115. for(int i=0;i<list.size();i++ ){
  116. if(0==i){
  117. str.append("'"+list.get(i)+"' ");
  118. }else{
  119. str.append(", '"+list.get(i)+"' ");
  120. }
  121. }
  122. System.out.println("str###########################"+str);
  123. String sql=map.get(sqlName).replace("?", str);
  124. System.out.println("====================="+sql);
  125. return (List<String>) cmt.queryToList(sql);
  126. }
  127. /**
  128. * 查询数据字典查询需要查询的字段
  129. * @param labels 需要查询的字段集合
  130. * @param args 1.公司编号,2,高字段是否启用,3该字段是否统计
  131. * @return
  132. */
  133. public List<Map<String,String>> getSelectLable(List<String> labels,Object...args){
  134. try {
  135. StringBuffer select=new StringBuffer("select");
  136. Object[] arrStr=labels.toArray();
  137. for(int index=0,len=arrStr.length;index<len;index++){
  138. if(0==index) select.append(" "+new StringBuffer(arrStr[index].toString()));
  139. select.append(", "+new StringBuffer(arrStr[index].toString()));
  140. }
  141. select.append(" FROM EMC_AM_REPORT_DICTIONARY WHERE REPORT_TYPE=? AND IS_USED= ? AND IS_COUNT=?;");
  142. List<Map<String,String>> list=findAll(select.toString(),labels, args);
  143. return list;
  144. } catch (SQLStringException e) {
  145. e.printStackTrace();
  146. log.error(e.getMessage());
  147. }
  148. return null;
  149. }
  150. /**
  151. * 拼接求和字段(聚合查询活单条记录查询<SUM,DETAIL,AVG……>)
  152. * @param list
  153. * @param columnName 需要增加的辅助字段
  154. * @return
  155. */
  156. public List<String> appendSql(List<Map<String,String>> list,String columnName,String olymerization){
  157. List<String> sumList=new ArrayList();
  158. for(Map<String,String> map:list){
  159. //String str="SUM ( CASE WHEN "+ map.get(columnName)+" IS NULL THEN 0 WHEN "+ map.get(columnName)+" ='' THEN 0 ELSE TO_NUMBER( "+map.get(columnName)+" ) END ) AS "+map.get(columnName)+"_SUM";
  160. StringBuffer str=new StringBuffer();
  161. if("COMP_ID".equals(map.get(columnName)) && ! "DETAIL".equals(olymerization)){
  162. break;
  163. }
  164. if("COMP_ID".equals(map.get(columnName))){
  165. str.append(" "+ map.get(columnName)+" " );
  166. }else{
  167. str.append("CASE WHEN "+ map.get(columnName)+" IS NULL THEN 0 WHEN "+ map.get(columnName)+" ='' THEN 0 ELSE TO_NUMBER( "+map.get(columnName)+" ) END");
  168. }
  169. if(!"DETAIL".equals(olymerization)){
  170. str=new StringBuffer(olymerization+" ( ").append(str).append(" ) ");
  171. }
  172. str.append(" AS "+map.get(columnName)+"_SUM");
  173. sumList.add(str.toString());
  174. }
  175. return sumList;
  176. }
  177. /**
  178. * 通过数据字典拼接sql语句
  179. * @param listColumn 需要查询的集合
  180. * @param tableName 表名
  181. * @param args 查询
  182. * @return
  183. */
  184. public String getSql(List<String> listColumn,String tableName,Object...args){
  185. StringBuffer select=new StringBuffer("select ");
  186. Object[] arrStr=listColumn.toArray();
  187. for(int index=0,len=arrStr.length;index<len;index++){
  188. if(0==index) select.append(" "+new StringBuffer(arrStr[index].toString()));
  189. select.append(", "+new StringBuffer(arrStr[index].toString()));
  190. }
  191. select.append(" FROM "+new StringBuffer(tableName)+" WHERE REPORT_ID IN "
  192. + "( SELECT FD_OBJECTID FROM REPORT_RECORDS WHERE APPEAR_TIME IN (SELECT MAX(APPEAR_TIME) FROM REPORT_RECORDS "
  193. + " WHERE COMP_ID=? AND REPORT_TYPE=? AND APPEAR_TIME<TO_DATE(?,'YYYY-MM-DD HH:MI:SS')))");
  194. System.out.println("查询语句__________________:"+select.toString());
  195. return select.toString();
  196. }
  197. /* AND APPEAR_TIME<TO_DATE(?,''YYYY-MM-DD HH:MI:SS'')*/
  198. /**
  199. * 将list集合转换为map集合
  200. * @param list
  201. * @return
  202. * @throws Exception
  203. */
  204. public Map<String,List<String>> listMapToMapList(List<Map<String,String>> list,Object...columNames) throws Exception{
  205. Map<String,List<String>> map=new HashMap<String, List<String>>();
  206. if(null==list||0==list.size())throw new Exception("listMapToMapList(List<Map<String,String>> list):参数为空或者长度为0;");
  207. for(String listName : list.get(0).keySet()){
  208. map.put(listName, new ArrayList());
  209. for(int i=0;i<columNames.length;i++){
  210. if(listName==columNames[i]){
  211. map.put(listName+"_SUM", new ArrayList());
  212. }
  213. }
  214. }
  215. for(Map<String,String> listItem : list){
  216. for(String listItemKey:listItem.keySet() ){
  217. map.get(listItemKey).add(listItem.get(listItemKey));
  218. for(int i=0;i<columNames.length;i++){
  219. if(listItemKey==columNames[i]){
  220. map.get(listItemKey+"_SUM").add(listItem.get(listItemKey)+"_SUM");
  221. }
  222. }
  223. }
  224. }
  225. return map;
  226. }
  227. /**
  228. * 查询数据字典表中的单条属性
  229. * @param columnName 数据字典的字段名 对应表的属性(表名,表的字段。表对应的中文名)
  230. * @param args
  231. * @return
  232. * @throws Exception
  233. */
  234. public Map<String,List<String>> queryLabel(String columnName,Object ...args) throws Exception{
  235. //数据表对应查询字段集合
  236. List<String> bb=new ArrayList();
  237. bb.add("REPORT_TABLE_NAME");
  238. bb.add("TABLE_COLUMN");
  239. bb.add("COLUMN_NAME");
  240. bb.add(columnName);
  241. //获取数据字典需要查询的字段
  242. List<Map<String,String>> label=this.getSelectLable(bb,args);
  243. return listMapToMapList(label,"TABLE_COLUMN");
  244. }
  245. /**
  246. * 通过公司Id查询报表记录
  247. * @param compId
  248. * @param Null
  249. * @param agrs
  250. * @return
  251. */
  252. public List<Map<String,String>> findAllById(String compId,String Null,String dateString,Object...agrs){
  253. //数据表对应查询字段集合
  254. List<String> bb=new ArrayList();
  255. bb.add("REPORT_TABLE_NAME");
  256. bb.add("TABLE_COLUMN");
  257. bb.add("COLUMN_NAME");
  258. //获取数据字典需要查询的字段
  259. List<Map<String,String>> label=this.getSelectLable(bb,agrs);
  260. Map<String,String> compID=new HashMap<String, String>();
  261. compID.put("TABLE_COLUMN", "COMP_ID");
  262. compID.put("REPORT_TABLE_NAME", label.get(0).get("REPORT_TABLE_NAME"));
  263. label.add(compID);
  264. try {
  265. //将map集合转换为list集合,并添加字段
  266. Map<String,List<String>> map=listMapToMapList(label,"TABLE_COLUMN");
  267. //拼接统计字段
  268. List<String> ll=this.appendSql(label, "TABLE_COLUMN",Null);
  269. //获取sql语句
  270. if(!"DETAIL".equals(Null)){
  271. map.get("TABLE_COLUMN_SUM").remove("COMP_ID_SUM");
  272. }
  273. String sql=this.getSql(ll,map.get("REPORT_TABLE_NAME").get(0), agrs);
  274. System.out.println("aa"+map.get("TABLE_COLUMN_SUM"));
  275. return cmt.queryToMap(sql,map.get("TABLE_COLUMN_SUM") ,compId,agrs[0],dateString);
  276. } catch (Exception e) {
  277. e.printStackTrace();
  278. }
  279. return null;
  280. }
  281. public static void main(String[] args) throws Exception {
  282. HeightChartDaoImpl dao=new HeightChartDaoImpl();
  283. Logger log= Logger.getLogger(dao.getClass());
  284. try {
  285. /*List<Map<String,String>> li=dao.findAllById("11","DETAIL","8","0","0");
  286. List list=dao.findAllInId("getColumn", dao.listMapToMapList(li, "COMP_ID_SUM").get("COMP_ID_SUM"));
  287. System.out.println("bbbb"+list);
  288. System.out.println("CUOREN__________"+dao.listMapToMapList(li, "COMP_ID_SUM"));
  289. System.out.println("zuihoujieguo"+JsonPluginsUtil.beanListToJson(li));
  290. //System.out.println(dao.queryLabel("COLUMN_NAME", "8","0","0"));
  291. System.out.println("_____________"+li.size());*/
  292. /*List<String> list=new ArrayList();
  293. list.add("AMOUNT");
  294. List<Map<String,String>> li=dao.findAllBySqlName("NUM", list, "11");
  295. System.out.println(li);*/
  296. String date="2015-11-12 12:5:20";
  297. List<Map<String,String>> list=new ArrayList();
  298. List<String> xlist=new ArrayList();
  299. for(int index=0;index<Integer.valueOf(10);index++){
  300. Date temDate=DateUtil.toData(date);
  301. temDate.setSeconds(DateUtil.toData(date).getSeconds()-index*Integer.valueOf(30));
  302. xlist.add(DateUtil.toString(temDate));
  303. list.addAll(dao.findAllById("11","DETAIL",DateUtil.toString(temDate),"5" ,"0","0"));
  304. }
  305. System.out.println("________________+++++++"+dao.listMapToMapList(list, "COMP_ID_SUM").get("COMP_ID_SUM"));
  306. System.out.println(dao.listMapToMapList(list, "COMP_ID_SUM").get("COMP_ID_SUM").size());
  307. List<String> xAxisCategories=dao.findAllInId("getColumn", dao.listMapToMapList(list, "COMP_ID_SUM").get("COMP_ID_SUM"));
  308. System.out.println();
  309. System.out.println(list.size());
  310. System.out.println(JsonPluginsUtil.beanListToJson(list));
  311. } catch (Exception e) {
  312. e.printStackTrace();
  313. }
  314. }
  315. }