62c12cf65f39d55d2c6cb44417ae6c8255b5e57f.svn-base 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. *
  3. */
  4. package com.sinosoft.am.resource.ups_generator.dao;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.apache.log4j.Logger;
  8. import com.persistence.service.PersistenceFactory;
  9. import com.persistence.service.SysPersistence;
  10. import com.persistence.service.exception.PersistenceException;
  11. import com.sinosoft.am.resource.ups_generator.vo.UPSGeneratorNum;
  12. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  13. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  14. /**
  15. * @author 蒋云涛
  16. *
  17. */
  18. public class UPSGeneratorDao{
  19. private Logger log = Logger.getLogger(this.getClass());
  20. SysModel sysmodel = ModelFactory.getSysmodel();
  21. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  22. StringBuffer json = new StringBuffer();
  23. private List<UPSGeneratorNum>numList=new ArrayList<UPSGeneratorNum>();
  24. /**
  25. * 根据传过来的ID进行检测,有子节点且子节点在明细表中有数据的,继续查询该节点下面的数据
  26. * @param id
  27. * @return
  28. */
  29. public String UPSGeneratorNumInit(String id){
  30. try{
  31. if(checkId(id)){
  32. String sql="select id from sys_department_0827 where parent_id='"+id+"'";
  33. List<String[]>tempList= persistence.getSearchResult(99, sql.toString());
  34. log.info("carNum.tempList.size===="+tempList.size());
  35. for(int i=0;i<tempList.size();i++){
  36. List<String>list=getCmpList(tempList.get(i)[0], new ArrayList<String>());
  37. getUPSGeneratorNum(list, tempList.get(i)[0]);
  38. }
  39. }else{
  40. List<String>list=new ArrayList<String>();
  41. list.add(id);
  42. getNoChildUPSGeneratorNum(list, id);
  43. }}catch(Exception e){
  44. this.log.error(e.getMessage(), e);
  45. }
  46. // log.info("最终JSON==="+json.toString()); 统计栏
  47. /*if (json.lastIndexOf(",") > -1) {
  48. json.deleteCharAt(json.lastIndexOf(","));
  49. }*/
  50. UPSGeneratorNum numBean=new UPSGeneratorNum();
  51. for(int i=0;i<numList.size();i++){
  52. numBean.setContent_min(numBean.getContent_min()+numList.get(i).getContent_min());
  53. numBean.setContent_mid(numBean.getContent_mid()+numList.get(i).getContent_mid());
  54. numBean.setContent_max(numBean.getContent_max()+numList.get(i).getContent_max());
  55. numBean.setNum(numBean.getNum()+numList.get(i).getNum());
  56. }
  57. json.append("{ ");
  58. json.append(" \"content_min\":\"" + numBean.getContent_min() + "\",");
  59. json.append(" \"content_mid\":\"" + numBean.getContent_mid() + "\",");
  60. json.append(" \"content_max\":\"" + numBean.getContent_max() + "\",");
  61. json.append(" \"num\":\"" + numBean.getNum() + "\",");
  62. json.append(" \"name\":\"" +"总计" + "\",");
  63. json.append(" \"dept_id\":\"" + "11111" + "\",");
  64. json.append(" \"isParent\":false,");
  65. json.append(" \"open\":false");
  66. json.append("} ");
  67. String result="{\"rows\":["+json.toString()+"]}";
  68. // List<String>list=getCmpList(id, new ArrayList<String>());
  69. log.info("result===="+result);
  70. return result;
  71. }
  72. public String UPSGeneratorNum(String id){
  73. try{
  74. if(checkId(id)){
  75. String sql="select id from sys_department_0827 where parent_id='"+id+"'";
  76. List<String[]>tempList= persistence.getSearchResult(99, sql.toString());
  77. log.info("generatorNum.tempList.size===="+tempList.size());
  78. for(int i=0;i<tempList.size();i++){
  79. List<String>list=getCmpList(tempList.get(i)[0], new ArrayList<String>());
  80. getUPSGeneratorNum(list, tempList.get(i)[0]);
  81. }
  82. }else{
  83. List<String>list=new ArrayList<String>();
  84. list.add(id);
  85. getNoChildUPSGeneratorNum(list, id);
  86. }}catch(Exception e){
  87. this.log.error(e.getMessage(), e);
  88. }
  89. // log.info("最终JSON==="+json.toString());
  90. if (json.lastIndexOf(",") > -1) {
  91. json.deleteCharAt(json.lastIndexOf(","));
  92. }
  93. String result="{\"rows\":["+json.toString()+"]}";
  94. // List<String>list=getCmpList(id, new ArrayList<String>());
  95. log.info("result===="+result);
  96. return result;
  97. }
  98. public List<String> getCmpList(String id,List<String>list) {
  99. try{
  100. if(checkId(id)){//存在子节点且子节点在明细表中有记录
  101. list.add(id);
  102. String sql="select id from sys_department_0827 where parent_id='"+id+"'";
  103. List<String[]>tempList= persistence.getSearchResult(99, sql.toString());
  104. log.info("getCmpList.tempList.size===="+tempList.size());
  105. for(int i=0;i<tempList.size();i++){
  106. getCmpList(tempList.get(i)[0],list);
  107. }
  108. }
  109. else{//如果不存在自己点或者子节点在明细表中无记录,则返回该ID
  110. list.add(id);
  111. }}catch(Exception e){
  112. this.log.error(e.getMessage(), e);
  113. }
  114. return list;
  115. }
  116. public boolean checkId(String id) {
  117. try{
  118. String sql="SELECT * FROM EMC_AM_UPS_GENERATOR WHERE COMP_ID IN(SELECT ID FROM SYS_DEPARTMENT_0827 WHERE PARENT_ID='"+id+"')";
  119. List<String[]>tempList= persistence.getSearchResult(99, sql.toString());
  120. if(tempList==null||tempList.size()==0){
  121. return false;
  122. }else{
  123. return true;
  124. }}catch(Exception e){
  125. this.log.error(e.getMessage(), e);
  126. return false;
  127. }
  128. }
  129. public void getUPSGeneratorNum(List<String>list,String id) throws PersistenceException{
  130. UPSGeneratorNum cn=new UPSGeneratorNum();
  131. try{
  132. for(int i=0;i<list.size();i++){
  133. // String sql="select comp_id,sum(case when car_content<250 then car_num else 0 end) "
  134. // + "as content_min,sum(case when car_content>250 and car_content<500 then car_num"
  135. // + " else 0 end) as content_mid,sum(case when car_content>501 then car_num else 0"
  136. // + " end) as content_max,car_comp,sum(case car_level when '380V' then car_num else 0 end)as"
  137. // + " level_max,sum(case car_level when '10kV' then car_num else 0 end)as level_min,sum(case "
  138. // + "STORED_ENERGY_TYPE when '发电机' then car_num else 0 end)as generator,sum(case STORED_ENERGY_TYPE "
  139. // + "when 'UPS' then car_num else 0 end)as ups,sum(case STORED_ENERGY_TYPE when '磁飞轮' then car_num"
  140. // + " else 0 end)as fly_circle ,sum(car_num)as num ,is_del from emc_am_generator_car group by car_comp,comp_id "
  141. // + ",is_del having is_del='0' and comp_id='"+list.get(i)+"'";
  142. String sql ="select comp_id , sum(case when content<10 then num else 0 end) as content_min, sum(case when content>11 and content<50 then num else 0 end) as content_mid,"
  143. + "sum(case when content>51 then num else 0 end) as content_max,comp,sum(num) as num,is_del from emc_am_ups_generator group by comp,comp_id,is_del having is_del='0' and "
  144. + "comp_id='"+list.get(i)+"'";
  145. log.info("getUPSGeneratorNum.sql===="+sql);
  146. List<String[]>numList= persistence.getSearchResult(99, sql.toString());
  147. if(numList.size()>0){
  148. cn.setContent_min(cn.getContent_min()+Integer.parseInt(numList.get(0)[1]));
  149. cn.setContent_mid(cn.getContent_mid()+Integer.parseInt(numList.get(0)[2]));
  150. cn.setContent_max(cn.getContent_max()+Integer.parseInt(numList.get(0)[3]));
  151. cn.setNum(cn.getNum()+Integer.parseInt(numList.get(0)[5]));
  152. }
  153. }}catch(Exception e){
  154. this.log.error(e.getMessage(), e);
  155. }
  156. // StringBuffer json=new StringBuffer();
  157. if(cn.getNum()>0){
  158. if(checkId(id)){
  159. json.append("{ ");
  160. json.append(" \"content_min\":\"" + cn.getContent_min() + "\",");
  161. json.append(" \"content_mid\":\"" + cn.getContent_mid() + "\",");
  162. json.append(" \"content_max\":\"" + cn.getContent_max() + "\",");
  163. json.append(" \"num\":\"" + cn.getNum() + "\",");
  164. json.append(" \"name\":\"" + getNameById(id) + "\",");
  165. json.append(" \"dept_id\":\"" + id + "\",");
  166. json.append(" \"isParent\":true,");
  167. json.append(" \"open\":false");
  168. json.append("}, ");
  169. }else{
  170. json.append("{ ");
  171. json.append(" \"content_min\":\"" + cn.getContent_min() + "\",");
  172. json.append(" \"content_mid\":\"" + cn.getContent_mid() + "\",");
  173. json.append(" \"content_max\":\"" + cn.getContent_max() + "\",");
  174. json.append(" \"num\":\"" + cn.getNum() + "\",");
  175. json.append(" \"name\":\"" + getNameById(id) + "\",");
  176. json.append(" \"dept_id\":\"" + id + "\",");
  177. json.append(" \"isParent\":false,");
  178. json.append(" \"open\":false");
  179. json.append("}, ");
  180. }
  181. }
  182. numList.add(cn);
  183. }
  184. public void getNoChildUPSGeneratorNum(List<String>list,String id) throws PersistenceException{
  185. UPSGeneratorNum cn=new UPSGeneratorNum();
  186. try{
  187. for(int i=0;i<list.size();i++){
  188. String sql ="select comp_id , sum(case when content<10 then num else 0 end) as content_min, sum(case when content>11 and content<50 then num else 0 end) as content_mid,"
  189. + "sum(case when content>51 then num else 0 end) as content_max,comp,sum(num) as num,is_del from emc_am_ups_generator group by comp,comp_id,is_del having is_del='0' and "
  190. + "comp_id='"+list.get(i)+"'";
  191. log.info("getNoChildUPSGeneratorNum.sql===="+sql);
  192. List<String[]>numList= persistence.getSearchResult(99, sql.toString());
  193. if(numList.size()>0){
  194. cn.setContent_min(cn.getContent_min()+Integer.parseInt(numList.get(0)[1]));
  195. cn.setContent_mid(cn.getContent_mid()+Integer.parseInt(numList.get(0)[2]));
  196. cn.setContent_max(cn.getContent_max()+Integer.parseInt(numList.get(0)[3]));
  197. cn.setNum(cn.getNum()+Integer.parseInt(numList.get(0)[5]));
  198. }
  199. }}catch(Exception e){
  200. this.log.error(e.getMessage(), e);
  201. }
  202. // StringBuffer json=new StringBuffer();
  203. if(cn.getNum()>0){
  204. json.append("{ ");
  205. json.append(" \"content_min\":\"" + cn.getContent_min() + "\",");
  206. json.append(" \"content_mid\":\"" + cn.getContent_mid() + "\",");
  207. json.append(" \"content_max\":\"" + cn.getContent_max() + "\",");
  208. json.append(" \"num\":\"" + cn.getNum() + "\",");
  209. json.append(" \"name\":\"" + getNameById(id) + "\",");
  210. json.append(" \"dept_id\":\"" + id + "\",");
  211. json.append(" \"isParent\":false,");
  212. json.append(" \"open\":true");
  213. json.append("}, ");
  214. numList.add(cn);
  215. }
  216. }
  217. public String getNameById(String id){
  218. String sql="select name from sys_department_0827 where id='"+id+"'";
  219. try {
  220. List<String[]>tempList= persistence.getSearchResult(99, sql.toString());
  221. if(tempList==null||tempList.size()==0){
  222. return "";
  223. }else{
  224. String name=tempList.get(0)[0];
  225. log.info("name===="+name);
  226. return name;
  227. }
  228. } catch (PersistenceException e) {
  229. e.printStackTrace();
  230. return "";
  231. }
  232. }
  233. }