123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- package com.huaxu.service;
- import com.huaxu.dto.*;
- import com.huaxu.entity.*;
- import com.huaxu.util.ByteArrayUtils;
- import com.huaxu.util.RedisUtil;
- import org.bouncycastle.crypto.engines.AESLightEngine;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.text.DateFormat;
- import java.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.*;
- @Service
- public class SecSupplyService {
- @Autowired
- private SceneService sceneService;
- @Autowired
- private MonthReportService monthReportService;
- @Autowired
- private DeviceParmService deviceParmService;
- @Autowired
- private RedisUtil redisUtil;
- @Autowired
- private DayReportService dayReportService;
- @Autowired
- private YearReportService yearReportService;
- /**
- * 查询30天供水量
- * @param sceneEntity
- * @return
- */
- public List<AmountDayThirtyDto> getAmountDayThirty(SceneEntity sceneEntity) {
- List<SceneEntity> sceneEntities = sceneService.selectByTypeName(sceneEntity);
- if (sceneEntities.size() == 0)
- return null;
- MonthReportEntity monthReportEntity = new MonthReportEntity();
- monthReportEntity.setParentSceneLists(sceneEntities);
- List<MonthReportEntity> monthReportEntities = monthReportService.findAmountBySceneIds(monthReportEntity);
- Map<String, Double> maps = new LinkedHashMap<>();
- for (MonthReportEntity item : monthReportEntities) {
- maps.put(item.getYear() + "-" + String.format("%02d", item.getMonth()) + "-" + String.format("%02d", item.getDay()), item.getSumValue());
- }
- Calendar begin = Calendar.getInstance();// 得到一个Calendar的实例
- begin.setTime(new Date()); // 设置时间为当前时间
- List<AmountDayThirtyDto> listC = new ArrayList<>();
- for (int i = 1; i <= 30; i++) {
- begin.add(Calendar.DATE, -1);// 日期加1
- Date d = new Date(begin.getTimeInMillis());
- DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- AmountDayThirtyDto amountDayThirtyDto = new AmountDayThirtyDto();
- if (maps.containsKey(df.format(d).toString())) {
- amountDayThirtyDto.setAmountTime(d);
- amountDayThirtyDto.setAmount((double)Math.round(maps.get(df.format(d))*1000)/1000);
- } else {
- amountDayThirtyDto.setAmountTime(d);
- amountDayThirtyDto.setAmount(0d);
- }
- listC.add(amountDayThirtyDto);
- }
- return listC;
- }
- /**
- * 查询供水及瞬时流量
- * @param sceneEntity
- * @return
- */
- public AmountTotalDto findAmountTotalByTypeName(SceneEntity sceneEntity) {
- AmountTotalDto amountTotalDto = new AmountTotalDto();
- List<SceneEntity> sceneEntities = sceneService.selectByTypeName(sceneEntity);
- if (sceneEntities.size() == 0)
- return null;
- Calendar begin = Calendar.getInstance();// 得到一个Calendar的实例
- begin.setTime(new Date()); // 设置时间为当前时间
- begin.add(Calendar.DATE, -1);// 日期加1
- //查询所有的关联设备及测点信息
- DeviceParmEntity deviceParmEntity = new DeviceParmEntity();
- deviceParmEntity.setSceneEntities(sceneEntities);
- deviceParmEntity.setParmType(14);//查询瞬时流量
- List<DeviceParmEntity> deviceParmEntities = deviceParmService.selectDeviceBySceneIdAndType(deviceParmEntity);
- //瞬时流量
- double instantFlow = 0d;
- for (DeviceParmEntity item : deviceParmEntities) {
- //取缓存里的数据
- byte[] bytes = redisUtil.get(("sms_water_" + item.getDeviceCode()).getBytes());
- if (bytes != null && bytes.length > 0) {
- MonitorDataEntity monitorDataEntity = (MonitorDataEntity) ByteArrayUtils.bytesToObject(bytes).get();
- //筛选该设备相同属性的值
- Map<Long, MonitorDataValueEntity> map = new HashMap<>();
- //将缓存中的实时数据放到map中方便进行遍历
- for (MonitorDataValueEntity dateValue : monitorDataEntity.getDataValues()) {
- map.put(dateValue.getAttributeId(), dateValue);
- }
- if (map.containsKey(item.getAttributeId().longValue()))
- instantFlow += map.get(item.getAttributeId().longValue()).getDataValue();
- }
- }
- amountTotalDto.setInstantFlow((double) Math.round(instantFlow * 1000) / 1000);
- //本日供水量
- deviceParmEntity.setParmType(3);//查询供水量
- List<DeviceParmEntity> deviceParms = deviceParmService.selectDeviceBySceneIdAndType(deviceParmEntity);
- double dayAmount = 0d;
- for (DeviceParmEntity item : deviceParms) {
- //取缓存里的数据
- byte[] bytes = redisUtil.get(("sms_water_" + item.getDeviceCode()).getBytes());
- if (bytes != null && bytes.length > 0) {
- MonitorDataEntity monitorDataEntity = (MonitorDataEntity) ByteArrayUtils.bytesToObject(bytes).get();
- //筛选该设备相同属性的值
- Map<Long, MonitorDataValueEntity> map = new HashMap<>();
- //将缓存中的实时数据放到map中方便进行遍历
- for (MonitorDataValueEntity dateValue : monitorDataEntity.getDataValues()) {
- map.put(dateValue.getAttributeId(), dateValue);
- }
- if (map.containsKey(item.getAttributeId().longValue())) {
- DayReportEntity dayReportEntity = new DayReportEntity();
- dayReportEntity.setYear(begin.get(Calendar.YEAR));
- dayReportEntity.setMonth(begin.get(Calendar.MONTH) + 1);
- dayReportEntity.setDay(begin.get(Calendar.DAY_OF_MONTH));
- dayReportEntity.setDeviceId(item.getDeviceId().longValue());
- dayReportEntity.setAttributeId(item.getAttributeId().longValue());
- List<DayReportEntity> dayReportEntities = dayReportService.findDeviceLastDayValue(dayReportEntity);
- if (dayReportEntities.size() > 0) {
- dayAmount += map.get(item.getAttributeId().longValue()).getDataValue();
- System.out.println("item.getAttributeId()"+item.getAttributeId()+"--"+map.get(item.getAttributeId().longValue()));
- dayAmount = dayAmount - dayReportEntities.get(0).getLatestValue();
- }
- }
- }
- }
- amountTotalDto.setDayAmount((double) Math.round(dayAmount * 1000) / 1000);
- //本月供水量
- double monthAmount = 0d;
- begin.add(Calendar.DATE, 1);// 恢复到当前日期
- MonthReportEntity monthReportEntity = new MonthReportEntity();
- monthReportEntity.setYear(begin.get(Calendar.YEAR));
- monthReportEntity.setMonth(begin.get(Calendar.MONTH) + 1);
- monthReportEntity.setParentSceneLists(sceneEntities);
- List<MonthReportEntity> monthReportNew = monthReportService.findAmountTotalBySceneIds(monthReportEntity);
- begin.add(Calendar.MONTH, -1);// 恢复到当前日期
- MonthReportEntity monthReportLast = new MonthReportEntity();
- monthReportLast.setYear(begin.get(Calendar.YEAR));
- monthReportLast.setMonth(begin.get(Calendar.MONTH) + 1);
- monthReportLast.setParentSceneLists(sceneEntities);
- List<MonthReportEntity> monthReportsLast = monthReportService.findAmountTotalBySceneIds(monthReportLast);
- if (monthReportNew.size() > 0 && monthReportsLast.size() > 0 && monthReportNew.get(0) != null && monthReportsLast.get(0) != null) {
- monthAmount = monthReportNew.get(0).getLatestValue() - monthReportsLast.get(0).getLatestValue() + dayAmount;
- } else if (monthReportNew.size() > 0 && monthReportNew.get(0) != null) {
- monthAmount = monthReportNew.get(0).getLatestValue() + dayAmount;
- } else {
- monthAmount = dayAmount;
- }
- amountTotalDto.setMonthAmount((double) Math.round(monthAmount * 1000) / 1000);
- //本年供水量
- double yearAmount = 0d;
- begin.add(Calendar.MONTH, 1);// 恢复到当前日期
- YearReportEntity yearReportEntity = new YearReportEntity();
- yearReportEntity.setYear(begin.get(Calendar.YEAR));
- yearReportEntity.setParentSceneLists(sceneEntities);
- List<YearReportEntity> yearReportsNew = yearReportService.findAmountTotalBySceneIds(yearReportEntity);
- yearReportEntity.setYear(begin.get(Calendar.YEAR) - 1);
- List<YearReportEntity> yearReportsLast = yearReportService.findAmountTotalBySceneIds(yearReportEntity);
- if (yearReportsNew.size() > 0 && yearReportsLast.size() > 0 && yearReportsNew.get(0) != null && yearReportsLast.get(0) != null) {
- yearAmount = yearReportsNew.get(0).getLatestValue() - yearReportsLast.get(0).getLatestValue() + monthAmount;
- } else if (yearReportsNew.size() > 0 && yearReportsNew.get(0) != null) {
- yearAmount = yearReportsNew.get(0).getLatestValue() + monthAmount;
- } else {
- yearAmount = monthAmount;
- }
- amountTotalDto.setYearAmount((double) Math.round(yearAmount * 1000) / 1000);
- return amountTotalDto;
- }
- public WaterPieDto selectWaterQualityByTypeName(SceneEntity sceneEntity) {
- List<SceneEntity> sceneEntities = sceneService.selectByTypeName(sceneEntity);
- if (sceneEntities.size() == 0)
- return null;
- WaterPieDto waterPieDto = new WaterPieDto();
- DeviceParmEntity deviceParmEntity = new DeviceParmEntity();
- deviceParmEntity.setSceneEntities(sceneEntities);
- List<ParmTypeCountDto> listTotal = deviceParmService.findAlarmTotalCount(deviceParmEntity);
- List<ParmTypeCountDto> listAlarmTotal =deviceParmService.findAlarmCountTotalCount(deviceParmEntity);
- WaterPieStateDto waterPieStateDto4 = new WaterPieStateDto();
- waterPieStateDto4.setNormalCount(listTotal.get(0).getTotalCount()- listAlarmTotal.get(0).getNbnormalCount());
- waterPieStateDto4.setNbnormalCount(listTotal.get(0).getNbnormalCount());
- waterPieStateDto4.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- listAlarmTotal.get(0).getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
- waterPieDto.setWaterQuality(waterPieStateDto4);
- List<ParmTypeCountDto> list = deviceParmService.findAlarmCount(deviceParmEntity);
- //余氯11 浊度9 PH8 cod 15 15,8,9,11,18,19
- for(ParmTypeCountDto item : list)
- {
- switch(item.getParmType())
- {
- case 8:
- WaterPieStateDto waterPieStateDto1 = new WaterPieStateDto();
- waterPieStateDto1.setNormalCount(listTotal.get(0).getTotalCount()- item.getNbnormalCount());
- waterPieStateDto1.setNbnormalCount(item.getNbnormalCount());
- DecimalFormat df1 = new DecimalFormat("#.00");
- waterPieStateDto1.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- item.getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
- waterPieDto.setPh(waterPieStateDto1);
- break;
- case 9:
- WaterPieStateDto waterPieStateDto2 = new WaterPieStateDto();
- waterPieStateDto2.setNormalCount(listTotal.get(0).getTotalCount()- item.getNbnormalCount());
- waterPieStateDto2.setNbnormalCount(item.getNbnormalCount());
- DecimalFormat df2 = new DecimalFormat("#.00");
- waterPieStateDto2.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- item.getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
- waterPieDto.setTurbidity(waterPieStateDto2);
- break;
- case 11:
- WaterPieStateDto waterPieStateDto3 = new WaterPieStateDto();
- waterPieStateDto3.setNormalCount(listTotal.get(0).getTotalCount()- item.getNbnormalCount());
- waterPieStateDto3.setNbnormalCount(item.getNbnormalCount());
- waterPieStateDto3.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- item.getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
- waterPieDto.setResidualChlorine(waterPieStateDto3);
- break;
- }
- }
- return waterPieDto;
- }
- }
|