DataviewServiceImpl.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /**
  2. *
  3. */
  4. package com.xcgl.dataview.service.impl;
  5. import java.text.DecimalFormat;
  6. import java.text.ParseException;
  7. import java.util.ArrayList;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import org.activiti.engine.impl.util.json.JSONObject;
  13. import org.apache.log4j.Logger;
  14. import org.jeecgframework.core.common.exception.BusinessException;
  15. import org.jeecgframework.core.util.DateUtils;
  16. import org.jeecgframework.p3.core.common.utils.DateUtil;
  17. import org.jeecgframework.p3.core.utils.common.StringUtils;
  18. import org.jeecgframework.web.system.service.SystemService;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. //import com.alibaba.druid.sql.dialect.oracle.ast.clause.SubqueryFactoringClause.Entry;
  22. import com.xcgl.dataview.entity.DeviceStatus;
  23. import com.xcgl.dataview.entity.DeviceStatusSumDto;
  24. import com.xcgl.dataview.entity.DeviceType;
  25. import com.xcgl.dataview.entity.GonglvDto;
  26. import com.xcgl.dataview.entity.NenghaoDto;
  27. import com.xcgl.dataview.entity.NenghaoType;
  28. import com.xcgl.dataview.entity.SensorLiveTextDto;
  29. import com.xcgl.dataview.entity.WarningSumDto;
  30. import com.xcgl.dataview.service.DataviewServiceI;
  31. import com.xcgl.utils.XcglDateUtils;
  32. import cn.com.lzt.accessory.controller.AccessoryController;
  33. /**
  34. * @author xzx
  35. *
  36. */
  37. @Service("dataviewService")
  38. public class DataviewServiceImpl implements DataviewServiceI {
  39. private static final Logger logger = Logger.getLogger(AccessoryController.class);
  40. @Autowired
  41. private SystemService systemService;
  42. /**
  43. * 获取最新的全部设备瞬时功率数据
  44. * @param valuetime 取值时间戳
  45. * @return
  46. * @throws Exception
  47. */
  48. @Override
  49. public List<GonglvDto> getGonglvData(Date valuetime) throws BusinessException{
  50. List<GonglvDto> list = new ArrayList<>();
  51. StringBuilder gvSql = new StringBuilder();
  52. gvSql.append(" SELECT ");
  53. gvSql.append(" id,code,name,value,recordtime");
  54. gvSql.append(" FROM (\r\n" +
  55. "select deviceid as id ,devicename as name, de.code as code,param_value as value,record.recordtime from p_sensor_record record \r\n" +
  56. "left join p_device de on record.deviceid = de.id\r\n" +
  57. "where record.typeunit = \"KW\" \r\n" + //只有单位是千瓦的设备才是可以监测瞬时功率
  58. "order by record.recordtime desc) a ");
  59. gvSql.append(" group by id ");
  60. List<Map<String, Object>> result = systemService.findForJdbc(gvSql.toString());
  61. if(result != null && result.size() > 0) {
  62. for(Map<String, Object> res : result)
  63. {
  64. GonglvDto gldto = new GonglvDto();
  65. gldto.setId(res.get("id").toString());
  66. gldto.setCode(res.get("code").toString());
  67. gldto.setName(res.get("name").toString());
  68. gldto.setValue(Double.valueOf(res.get("value").toString()));
  69. list.add(gldto);
  70. }
  71. }else {
  72. list.add(GonglvDto.getDemoInstance("1#变压器"));
  73. list.add(GonglvDto.getDemoInstance("2#变压器"));
  74. list.add(GonglvDto.getDemoInstance("舞台照明1"));
  75. list.add(GonglvDto.getDemoInstance("舞台照明2"));
  76. list.add(GonglvDto.getDemoInstance("主楼照明"));
  77. list.add(GonglvDto.getDemoInstance("热泵1"));
  78. list.add(GonglvDto.getDemoInstance("热泵2"));
  79. list.add(GonglvDto.getDemoInstance("热泵3"));
  80. list.add(GonglvDto.getDemoInstance("热泵4"));
  81. list.add(GonglvDto.getDemoInstance("变电所动力照明柜"));
  82. list.add(GonglvDto.getDemoInstance("北区办公及改造楼照明及空调"));
  83. list.add(GonglvDto.getDemoInstance("两栋小楼办公室电源"));
  84. list.add(GonglvDto.getDemoInstance("VRV室外空调机"));
  85. }
  86. return list;
  87. }
  88. /**
  89. * 获取过去monthcount个月连续的月能耗数据,当前月份为7, 获取1-6月份的月能耗
  90. * 算法概述:
  91. * 1、先查询每个设备的平均功率(算数平均值)
  92. * 2、按月计算所有设备平均功率合计
  93. * 3、累计平均功率 * 24 * 30(31)= 月能耗
  94. * @param monthcount
  95. * @return
  96. * @throws BusinessException
  97. */
  98. @Override
  99. public List<NenghaoDto> getLastContinuousNenghaoData(int monthcount)throws BusinessException{
  100. List<NenghaoDto> list = new ArrayList<>();
  101. StringBuilder nhSql = new StringBuilder();
  102. Date qryDate = DateUtil.alterMonty(DateUtil.getCurrDate(), 0- monthcount);
  103. nhSql.append("select sum(value) as total ,month from ");
  104. nhSql.append(" ( SELECT ");
  105. nhSql.append(" pointid, pointname, pointcode,AVG(param_value) as value ,left(recordtime,7) as month");
  106. nhSql.append(" FROM p_sensor_record ");
  107. //查询条件为当前月份往前退n个月,本月不查询
  108. nhSql.append(" where typeunit = \"KW\" and left(recordtime,7) >= \"").append(DateUtils.formatDate(qryDate, "yyyy-MM")).append("\" ");
  109. nhSql.append(" and left(recordtime,7) < \"").append( DateUtils.formatDate(DateUtil.getCurrDate(), "yyyy-MM")).append("\"");
  110. nhSql.append(" group by id ) tmp ");
  111. nhSql.append(" group by month");
  112. List<Map<String, Object>> result = systemService.findForJdbc(nhSql.toString());
  113. if(result != null && result.size() > 0) {
  114. for(Map<String, Object> res : result)
  115. {
  116. int year = Integer.parseInt(res.get("month").toString().substring(0,4));
  117. int month = Integer.parseInt(res.get("month").toString().substring(5,7));
  118. double totleGV = Double.parseDouble( res.get("total").toString());
  119. double gh = (month == 1 ||month == 3 || month == 5|| month == 7 || month == 8 ||month == 10 ||month == 12) ? totleGV * 31
  120. : ((month == 2 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? totleGV * 29
  121. :( month == 2 ? totleGV * 28: totleGV * 30));
  122. list.add(NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO,
  123. year,
  124. month,
  125. 1,
  126. gh));
  127. }
  128. }else {
  129. NenghaoDto month_1 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 1, 1, 21230.0);
  130. NenghaoDto month_2 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 2, 1, 18911.7);
  131. NenghaoDto month_3 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 3, 1, 21033.3);
  132. NenghaoDto month_4 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 4, 1, 18122.8);
  133. NenghaoDto month_5 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 5, 1, 18278.9);
  134. NenghaoDto month_6 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 6, 1, 32330.6);
  135. list.add(month_1);
  136. list.add(month_2);
  137. list.add(month_3);
  138. list.add(month_4);
  139. list.add(month_5);
  140. list.add(month_6);
  141. }
  142. return list;
  143. }
  144. @Override
  145. public HashMap<String,String> getNenghaoSumData(boolean needUnit)throws BusinessException{
  146. HashMap<String,String> nengHaoRiYueJi = new HashMap<String,String> ();
  147. StringBuilder nhSql = new StringBuilder();
  148. //本季度第一天
  149. Date curQuarterDate = XcglDateUtils.getCurrentQuarterStartTime();
  150. nhSql.append("select sum(value) as total ,day ,month from \r\n" +
  151. " ( SELECT \r\n" +
  152. " pointid, pointname, pointcode,AVG(param_value) as value ,left(recordtime,10) as day,left(recordtime,7) as month\r\n" +
  153. " FROM p_sensor_record \r\n" +
  154. " where typeunit = 'KW' and left(recordtime,10) >= '").append(DateUtils.formatDate(curQuarterDate, "yyyy-MM-dd")).append("'\r\n");
  155. nhSql.append(" and left(recordtime,10) <= '").append(DateUtils.formatDate(new Date(), "yyyy-MM-dd")).append("'\r\n");
  156. nhSql.append(" group by pointid,day ) tmp \r\n" +
  157. " group by day");
  158. List<Map<String, Object>> result = systemService.findForJdbc(nhSql.toString());
  159. HashMap<String,Double> monthNenghao = new HashMap<String,Double> ();
  160. DecimalFormat df = new DecimalFormat("#");
  161. if(result != null && result.size() > 0) {
  162. for(Map<String, Object> res : result)
  163. {
  164. String month = res.get("month").toString();
  165. String day = res.get("day").toString();
  166. //每日功耗(根据每日功率平均值*24小时计算所得)
  167. Double dayTotleGV = new Double(res.get("total").toString()) * 24;
  168. if(day.equals(DateUtils.formatDate( new Date(), "yyyy-MM-dd")))
  169. {
  170. nengHaoRiYueJi.put("rinenghao", df.format(dayTotleGV)+ (needUnit ? "/kwh":""));
  171. }
  172. if(monthNenghao.containsKey(month))
  173. {
  174. monthNenghao.put(month, monthNenghao.get(month).doubleValue()+dayTotleGV.doubleValue());
  175. }else
  176. {
  177. monthNenghao.put(month, dayTotleGV);
  178. }
  179. }
  180. //计算月能耗
  181. if(monthNenghao.containsKey(DateUtils.formatDate(new Date(), "yyyy-MM")))
  182. {
  183. nengHaoRiYueJi.put("yuenenghao", df.format(monthNenghao.get(DateUtils.formatDate(new Date(), "yyyy-MM")))+ (needUnit ? "/kwh":""));
  184. }
  185. //计算季能耗
  186. double jinenghao = 0;
  187. for (java.util.Map.Entry<String,Double> entry : monthNenghao.entrySet()) {
  188. jinenghao += entry.getValue();
  189. }
  190. nengHaoRiYueJi.put("jinenghao", df.format(jinenghao)+ (needUnit ? "/kwh":""));
  191. }
  192. if(!nengHaoRiYueJi.containsKey("rinenghao"))
  193. nengHaoRiYueJi.put("rinenghao", "0.00"+ (needUnit ? "/kwh":""));
  194. if(!nengHaoRiYueJi.containsKey("yuenenghao"))
  195. nengHaoRiYueJi.put("yuenenghao", "0.00"+ (needUnit ? "/kwh":""));
  196. if(!nengHaoRiYueJi.containsKey("jinenghao"))
  197. nengHaoRiYueJi.put("jinenghao", "0.00" +(needUnit ? "/kwh":""));
  198. return nengHaoRiYueJi;
  199. }
  200. @Override
  201. public HashMap<String,String> getNenghaoSumDataByProject(boolean needUnit,String projectid)throws BusinessException{
  202. HashMap<String,String> nengHaoRiYueJi = new HashMap<String,String> ();
  203. StringBuilder nhSql = new StringBuilder();
  204. //本季度第一天
  205. Date curQuarterDate = XcglDateUtils.getCurrentQuarterStartTime();
  206. nhSql.append("select sum(value) as total ,day ,month from \r\n" +
  207. " ( SELECT \r\n" +
  208. " pointid, pointname, pointcode,AVG(param_value) as value ,left(recordtime,10) as day,left(recordtime,7) as month\r\n" +
  209. " FROM p_sensor_record \r\n" +
  210. " where projectid = '"+projectid+"' "
  211. + " and typeunit = 'KW' and left(recordtime,10) >= '").append(DateUtils.formatDate(curQuarterDate, "yyyy-MM-dd")).append("'\r\n");
  212. nhSql.append(" and left(recordtime,10) <= '").append(DateUtils.formatDate(new Date(), "yyyy-MM-dd")).append("'\r\n");
  213. nhSql.append(" group by pointid,day ) tmp \r\n" +
  214. " group by day");
  215. List<Map<String, Object>> result = systemService.findForJdbc(nhSql.toString());
  216. HashMap<String,Double> monthNenghao = new HashMap<String,Double> ();
  217. DecimalFormat df = new DecimalFormat("#");
  218. if(result != null && result.size() > 0) {
  219. for(Map<String, Object> res : result)
  220. {
  221. String month = res.get("month").toString();
  222. String day = res.get("day").toString();
  223. //每日功耗(根据每日功率平均值*24小时计算所得)
  224. Double dayTotleGV = new Double(res.get("total").toString()) * 24;
  225. if(day.equals(DateUtils.formatDate( new Date(), "yyyy-MM-dd")))
  226. {
  227. nengHaoRiYueJi.put("rinenghao", df.format(dayTotleGV)+ (needUnit ? "/kwh":""));
  228. }
  229. if(monthNenghao.containsKey(month))
  230. {
  231. monthNenghao.put(month, monthNenghao.get(month).doubleValue()+dayTotleGV.doubleValue());
  232. }else
  233. {
  234. monthNenghao.put(month, dayTotleGV);
  235. }
  236. }
  237. //计算月能耗
  238. if(monthNenghao.containsKey(DateUtils.formatDate(new Date(), "yyyy-MM")))
  239. {
  240. nengHaoRiYueJi.put("yuenenghao", df.format(monthNenghao.get(DateUtils.formatDate(new Date(), "yyyy-MM")))+ (needUnit ? "/kwh":""));
  241. }
  242. //计算季能耗
  243. double jinenghao = 0;
  244. for (java.util.Map.Entry<String,Double> entry : monthNenghao.entrySet()) {
  245. jinenghao += entry.getValue();
  246. }
  247. nengHaoRiYueJi.put("jinenghao", df.format(jinenghao)+ (needUnit ? "/kwh":""));
  248. }
  249. if(!nengHaoRiYueJi.containsKey("rinenghao"))
  250. nengHaoRiYueJi.put("rinenghao", "0.00"+ (needUnit ? "/kwh":""));
  251. if(!nengHaoRiYueJi.containsKey("yuenenghao"))
  252. nengHaoRiYueJi.put("yuenenghao", "0.00"+ (needUnit ? "/kwh":""));
  253. if(!nengHaoRiYueJi.containsKey("jinenghao"))
  254. nengHaoRiYueJi.put("jinenghao", "0.00" +(needUnit ? "/kwh":""));
  255. return nengHaoRiYueJi;
  256. }
  257. /**
  258. * 设备状态日期汇总,查询时间段内,每天正常设备数、离线设备数、报警设备数
  259. * ps、以监测点为查询维度,不以设备为查询维度
  260. * */
  261. @Override
  262. public List<DeviceStatusSumDto> getDeviceStatusSumData(String startDate, String endDate) throws BusinessException{
  263. // String startDay ,endDay;
  264. // try {
  265. // startDay = DateUtils.parseDate(endDate, "yyyy-MM-dd").toString();
  266. // endDay = DateUtils.parseDate(endDate, "yyyy-MM-dd").toString();
  267. // }catch(ParseException e)
  268. // {
  269. // logger.error(e);
  270. // throw new BusinessException(e.getMessage());
  271. // }
  272. List<DeviceStatusSumDto> list = new ArrayList<>();
  273. StringBuilder devStatusSql = new StringBuilder();
  274. devStatusSql.append(" select count(distinct monitor_point_id) total ,left(abn.create_date,10 ) as dataday,state");
  275. devStatusSql.append(" from p_sensor_abnormal abn");
  276. devStatusSql.append(" where abn.state = '").append(DeviceStatus.OFFLINE.getIndex()).append("'");
  277. devStatusSql.append(" and left(abn.create_date,10 ) >= '").append(startDate).append("'");
  278. devStatusSql.append(" and left(abn.create_date,10 ) <= '").append(endDate).append("'");
  279. devStatusSql.append(" group by dataday");
  280. devStatusSql.append(" union all ");
  281. devStatusSql.append(" select count(distinct pointid) total ,left(recordtime,10 ) as dataday,qcdstate as state ");
  282. devStatusSql.append(" from p_sensor_record record ");
  283. devStatusSql.append(" where left(recordtime,10 ) >= '").append(startDate).append("'");
  284. devStatusSql.append(" and left(recordtime,10 ) <= '").append(endDate).append("'");
  285. devStatusSql.append(" group by dataday,qcdstate");
  286. List<Map<String, Object>> result = systemService.findForJdbc(devStatusSql.toString());
  287. HashMap<String, DeviceStatusSumDto> value = new HashMap<String, DeviceStatusSumDto>();
  288. if (result != null && result.size() > 0) {
  289. for (Map<String, Object> res : result) {
  290. DeviceStatusSumDto sumDto ;
  291. if(value.containsKey(res.get("dataday").toString())) {
  292. sumDto = value.get(res.get("dataday").toString());
  293. }else {
  294. sumDto = new DeviceStatusSumDto();
  295. }
  296. sumDto.setValueTime(new Date());
  297. sumDto.setSumDate(res.get("dataday").toString());
  298. if(res.get("state").toString().equals(String.valueOf(DeviceStatus.OFFLINE.getIndex())))
  299. {
  300. sumDto.setOfflineCount(Integer.parseInt(res.get("total").toString()));
  301. }else if (res.get("state").toString().equals(String.valueOf(DeviceStatus.NORMAL.getIndex()))){
  302. sumDto.setNormalCount(Integer.parseInt(res.get("total").toString()));
  303. }else
  304. {//超上限和超下限都要sum到一起
  305. sumDto.setWarningCount(sumDto.getWarningCount() + Integer.parseInt(res.get("total").toString()));
  306. }
  307. value.put(res.get("dataday").toString(),sumDto);
  308. }
  309. for (DeviceStatusSumDto sumDto : value.values()) {
  310. list.add(sumDto) ;
  311. }
  312. } else {
  313. list.add(DeviceStatusSumDto.getDemoInstance("2018-07-01"));
  314. list.add(DeviceStatusSumDto.getDemoInstance("2018-07-02"));
  315. list.add(DeviceStatusSumDto.getDemoInstance("2018-07-03"));
  316. list.add(DeviceStatusSumDto.getDemoInstance("2018-07-04"));
  317. list.add(DeviceStatusSumDto.getDemoInstance("2018-07-05"));
  318. list.add(DeviceStatusSumDto.getDemoInstance("2018-07-06"));
  319. list.add(DeviceStatusSumDto.getDemoInstance("2018-07-07"));
  320. list.add(DeviceStatusSumDto.getDemoInstance("2018-07-08"));
  321. list.add(DeviceStatusSumDto.getDemoInstance("2018-07-09"));
  322. list.add(DeviceStatusSumDto.getDemoInstance("2018-07-10"));
  323. list.add(DeviceStatusSumDto.getDemoInstance("2018-07-11"));
  324. }
  325. return list;
  326. }
  327. @Override
  328. public List<WarningSumDto> getWarningSumData(String date) throws BusinessException{
  329. List<WarningSumDto> list = new ArrayList<>();
  330. //查询一个月内的数据
  331. Date qryDate = DateUtil.alterMonty(new Date(), -1);
  332. StringBuilder devStatusSql = new StringBuilder();
  333. devStatusSql.append(" select count(abnormal_cnt) as count ,type.type_code as type ");
  334. devStatusSql.append(" from p_sensor_abnormal abn");
  335. devStatusSql.append(" left join p_sensor_monitor_point point on abn.monitor_point_id = point.id");
  336. devStatusSql.append(" left join p_device dev on point.deviceid = dev.id");
  337. devStatusSql.append(" left join p_device_type type on dev.type_id = type.id");
  338. devStatusSql.append(" where type_code in ('1','2','3','4','5','6')");
  339. devStatusSql.append(" and left(abn.create_date,10 ) > \"").append(DateUtils.formatDate(qryDate, "yyyy-MM-dd")).append("\"");
  340. devStatusSql.append(" group by type.type_code");
  341. List<Map<String, Object>> result = systemService.findForJdbc(devStatusSql.toString());
  342. if (result != null && result.size() > 0) {
  343. for (Map<String, Object> res : result) {
  344. WarningSumDto sumDto = new WarningSumDto();
  345. sumDto.setSum(Integer.parseInt( res.get("count").toString()));
  346. sumDto.setDeviceType(DeviceType.values()[Integer.parseInt(res.get("type").toString())-1]);
  347. list.add(sumDto);
  348. }
  349. } else {
  350. list.add(WarningSumDto.getDemoInstance(DeviceType.FENGJIFANG));
  351. list.add(WarningSumDto.getDemoInstance(DeviceType.JISHUIJING));
  352. list.add(WarningSumDto.getDemoInstance(DeviceType.KONGTIAO));
  353. list.add(WarningSumDto.getDemoInstance(DeviceType.PENLINBENG));
  354. list.add(WarningSumDto.getDemoInstance(DeviceType.SHENGHUOBENG));
  355. list.add(WarningSumDto.getDemoInstance(DeviceType.XIAOFANGBENG));
  356. }
  357. return list;
  358. }
  359. @Override
  360. public List<SensorLiveTextDto> getSensorLiveTextDto() throws BusinessException{
  361. List<SensorLiveTextDto> list = new ArrayList<>();
  362. // 只查询当天数据
  363. //先查出最新的(table: p_sensor_record),正常以及超上下限的数据
  364. StringBuilder sensorLiveSql = new StringBuilder();
  365. sensorLiveSql.append("select deviceid,devicecode,devicename,pointid,pointcode,pointname,value,recordtime,qcdstate,value from(");
  366. sensorLiveSql.append(" select record.deviceid as deviceid ,record.devicename as devicename,de.code as devicecode,pointid,pointcode,pointname,param_value as value,recordtime,record.qcdstate as qcdstate");
  367. sensorLiveSql.append(" from p_sensor_record record ");
  368. sensorLiveSql.append(" left join p_device de on record.deviceid = de.id");
  369. // sensorLiveSql.append(" left join p_device de on record.deviceid = de.id");
  370. sensorLiveSql.append(" where left(recordtime,10 ) = '").append(DateUtils.formatDate(new Date(),"yyyy-MM-dd")).append("'");
  371. sensorLiveSql.append(" order by recordtime desc) tmp");
  372. sensorLiveSql.append(" group by pointid");
  373. List<Map<String, Object>> result = systemService.findForJdbc(sensorLiveSql.toString());
  374. HashMap<String,String> pointids = new HashMap<String,String> ();
  375. if (result != null && result.size() > 0) {
  376. for (Map<String, Object> res : result) {
  377. SensorLiveTextDto sliveDto = new SensorLiveTextDto();
  378. sliveDto.setDevice_code(res.get("devicecode").toString());
  379. sliveDto.setDevice_id(res.get("deviceid").toString());
  380. sliveDto.setDevice_name(res.get("devicename").toString());
  381. sliveDto.setSensor_id(res.get("pointid").toString());
  382. sliveDto.setSensor_code(res.get("pointcode").toString());
  383. sliveDto.setSensor_name(res.get("pointname").toString());
  384. if(StringUtils.isNotEmpty(res.get("value").toString())) {
  385. sliveDto.setValue(Double.valueOf(res.get("value").toString()));
  386. }else {
  387. continue;
  388. }
  389. sliveDto.setStatus(DeviceStatus.getDeviceStatus(Integer.parseInt(res.get("qcdstate").toString())));
  390. try {
  391. String recordtime = res.get("recordtime").toString();
  392. // if(recordtime.length()>19) {
  393. // recordtime = recordtime.substring(0, 19);
  394. // }
  395. sliveDto.setValueTime(DateUtils.parseDate(recordtime, "yyyy-MM-dd HH:mm:ss"));
  396. } catch (ParseException e) {
  397. logger.error(e);
  398. }
  399. list.add(sliveDto);
  400. pointids.put(res.get("pointid").toString(),res.get("pointid").toString());
  401. }
  402. }
  403. // 查询离线数据
  404. sensorLiveSql = new StringBuilder();
  405. sensorLiveSql.append(
  406. "select deviceid,devicecode,devicename,pointid,pointcode,pointname,value,recordtime,qcdstate,value from(");
  407. sensorLiveSql.append(
  408. " select point.deviceid as deviceid ,dev.name as devicename,dev.code as devicecode,monitor_point_id as pointid,");
  409. sensorLiveSql.append(
  410. " point.code as pointcode, point.name as pointname, '' as value,abn.create_date as recordtime,abn.state as qcdstate");
  411. sensorLiveSql.append(" from p_sensor_abnormal abn");
  412. sensorLiveSql.append(" left join p_sensor_monitor_point point on abn.monitor_point_id = point.id");
  413. sensorLiveSql.append(" left join p_device dev on point.deviceid = dev.id");
  414. sensorLiveSql.append(" where abn.state = '").append(DeviceStatus.OFFLINE.getIndex()).append("'");
  415. sensorLiveSql.append(" and left(abn.create_date,10 ) = '").append(DateUtils.formatDate(new Date(),"yyyy-MM-dd")).append("'");
  416. sensorLiveSql.append(" order by abn.create_date desc) tmp");
  417. sensorLiveSql.append(" group by pointid");
  418. result = systemService.findForJdbc(sensorLiveSql.toString());
  419. if (result != null && result.size() > 0) {
  420. for (Map<String, Object> res : result) {
  421. if(pointids.containsKey(res.get("pointid").toString()))
  422. continue;
  423. SensorLiveTextDto sliveDto = new SensorLiveTextDto();
  424. sliveDto.setDevice_code(res.get("devicecode").toString());
  425. sliveDto.setDevice_id(res.get("deviceid").toString());
  426. sliveDto.setDevice_name(res.get("devicename").toString());
  427. sliveDto.setSensor_id(res.get("pointid").toString());
  428. sliveDto.setSensor_code(res.get("pointcode").toString());
  429. sliveDto.setSensor_name(res.get("pointname").toString());
  430. if(StringUtils.isNotEmpty(res.get("value").toString())) {
  431. sliveDto.setValue(Double.valueOf(res.get("value").toString()));
  432. }else {
  433. continue;
  434. }
  435. sliveDto.setStatus(DeviceStatus.values()[Integer.parseInt(res.get("qcdstate").toString())-1]);
  436. try {
  437. sliveDto.setValueTime(DateUtils.parseDate(res.get("recordtime").toString(), "yyyy-MM-dd HH:mm:ss"));
  438. } catch (ParseException e) {
  439. logger.error(e);
  440. }
  441. list.add(sliveDto);
  442. }
  443. }
  444. return list;
  445. }
  446. @Override
  447. public Map<String, Object> getDeviceCode2ValueMap() throws BusinessException{
  448. List<SensorLiveTextDto> list = getSensorLiveTextDto();
  449. if(list != null && list.size()>0) {
  450. Map<String,Object> map = new HashMap<String, Object>();
  451. for(SensorLiveTextDto dto : list) {
  452. String deviceCode = dto.getSensor_code();
  453. Double value = dto.getValue();
  454. map.put(deviceCode, JSONObject.doubleToString(value));
  455. }
  456. return map;
  457. }
  458. return null;
  459. }
  460. }