| 
					
				 | 
			
			
				@@ -0,0 +1,186 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.huaxu.service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.huaxu.dto.AmountDayThirtyDto; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.huaxu.dto.AmountTotalDto; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+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.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.getLatestValue()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Calendar begin = Calendar.getInstance();// 得到一个Calendar的实例 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        begin.setTime(new Date()); // 设置时间为当前时间 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<AmountDayThirtyDto> listC = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (int i = 1; i <= 31; 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(maps.get(df.format(d))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                amountDayThirtyDto.setAmountTime(d); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            listC.add(amountDayThirtyDto); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<AmountDayThirtyDto> listD = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (int m = 0; m < 30; m++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            AmountDayThirtyDto amountDayThirtyDto = new AmountDayThirtyDto(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            amountDayThirtyDto.setAmountTime(listC.get(m).getAmountTime()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (listC.get(m + 1).getAmount() != null && listC.get(m).getAmount() != null) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                amountDayThirtyDto.setAmount((double) Math.round((listC.get(m).getAmount() - listC.get(m + 1).getAmount()) * 1000) / 1000); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                amountDayThirtyDto.setAmount(0d); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            listD.add(amountDayThirtyDto); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return listD; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 查询供水及瞬时流量 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @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 : 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())) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    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(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        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; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |