From 5107fb984a9ffd9cae0d1ff3737b7efa46fd140f Mon Sep 17 00:00:00 2001 From: yingsl Date: Thu, 6 Jan 2022 16:23:46 +0800 Subject: [PATCH] =?UTF-8?q?perf=20=E5=BC=80=E5=8F=91=E5=85=AC=E5=8E=95?= =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E7=94=A8=E6=B0=B4=E7=94=A8=E7=94=B5=E9=87=8F?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/common/Constant.java | 14 ++ .../api/controller/DeviceDataPushController.java | 69 +++++++ .../modular/api/service/DeviceDataPushService.java | 40 ++++ .../service/impl/DeviceDataPushServiceImpl.java | 204 +++++++++++++++++++++ .../mapper/mapping/BaseDeviceDeliveryMapper.xml | 1 + .../modular/device/po/BaseDeviceDelivery.java | 5 + .../mapper/ToiletElectricityConsumptionMapper.java | 10 + .../mapper/ToiletWaterConsumptionMapper.java | 9 + .../mapping/ToiletElectricityConsumptionMapper.xml | 6 + .../mapping/ToiletWaterConsumptionMapper.xml | 7 + .../IToiletElectricityConsumptionService.java | 8 + .../service/IToiletWaterConsumptionService.java | 9 + .../ToiletElectricityConsumptionServiceImpl.java | 6 + .../impl/ToiletWaterConsumptionServiceImpl.java | 6 + 14 files changed, 394 insertions(+) create mode 100644 src/main/java/com/example/modular/api/controller/DeviceDataPushController.java create mode 100644 src/main/java/com/example/modular/api/service/DeviceDataPushService.java create mode 100644 src/main/java/com/example/modular/api/service/impl/DeviceDataPushServiceImpl.java diff --git a/src/main/java/com/example/common/Constant.java b/src/main/java/com/example/common/Constant.java index c2d1bfd..5a5d828 100644 --- a/src/main/java/com/example/common/Constant.java +++ b/src/main/java/com/example/common/Constant.java @@ -647,4 +647,18 @@ public class Constant { */ public static final Integer WARNING_STATUS_DEAL = 1; + + //———————————————————————————————— 设备模块常量 ———————————————————————————— + /** + * 设备类型:电表 + */ + public static final Integer DEVICE_ELECTRICITY_TYPE = 1; + /** + * 设备类型:水表 + */ + public static final Integer DEVICE_WATER_TYPE = 2; + /** + * 设备类型:气表 + */ + public static final Integer DEVICE_AIR_TYPE = 3; } diff --git a/src/main/java/com/example/modular/api/controller/DeviceDataPushController.java b/src/main/java/com/example/modular/api/controller/DeviceDataPushController.java new file mode 100644 index 0000000..7feae0c --- /dev/null +++ b/src/main/java/com/example/modular/api/controller/DeviceDataPushController.java @@ -0,0 +1,69 @@ +package com.example.modular.api.controller; + +import com.example.modular.api.service.DeviceDataPushService; +import com.example.util.Result; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +/** + * 水电表数据推送 controller + * @author ysl + * @since 2022-01-06 + */ +@RestController +@RequestMapping("/device-data") +public class DeviceDataPushController { + + @Autowired + private DeviceDataPushService deviceDataPushService; + + /** + * 从设备编号获取用水量数据 + * @param base64 照片base64 + * @param imgUrl 照片下载地址 + * @param devId 设备ID + * @param devVol 设备电压 + * @param isPointerMeter 是否是指针表 + * @param createTime 上传时间 yyyy-MM-dd HH:mm:ss + * @param result 识别结果JSON字符串 + * @return + * @author ysl + */ + @PostMapping("/water") + @CrossOrigin + public String deviceWaterSave (@RequestParam(value = "base64",required = false)String base64, + @RequestParam(value = "imgUrl",required = false)String imgUrl, + @RequestParam(value = "devId",required = false)String devId, + @RequestParam(value = "devVol",required = false)Integer devVol, + @RequestParam(value = "isPointerMeter",required = false,defaultValue = "false")boolean isPointerMeter, + @RequestParam(value = "createTime",required = false)String createTime, + @RequestParam(value = "result",required = false)String result) { + String resultStr = deviceDataPushService.deviceWaterSave(base64,imgUrl,devId,devVol,isPointerMeter,createTime,result); + return resultStr; + } + + /** + * 从设备编号获取用电量数据 + * @param base64 照片base64 + * @param imgUrl 照片下载地址 + * @param devId 设备ID + * @param devVol 设备电压 + * @param isPointerMeter 是否是指针表 + * @param createTime 上传时间 yyyy-MM-dd HH:mm:ss + * @param result 识别结果JSON字符串 + * @return + * @author ysl + */ + @PostMapping("/electricity") + @CrossOrigin + public String deviceElectricitySave (@RequestParam(value = "base64",required = false)String base64, + @RequestParam(value = "imgUrl",required = false)String imgUrl, + @RequestParam(value = "devId",required = false)String devId, + @RequestParam(value = "devVol",required = false)Integer devVol, + @RequestParam(value = "isPointerMeter",required = false,defaultValue = "false")boolean isPointerMeter, + @RequestParam(value = "createTime",required = false)String createTime, + @RequestParam(value = "result",required = false)String result) { + String resultStr = deviceDataPushService.deviceElectricitySave(base64,imgUrl,devId,devVol,isPointerMeter,createTime,result); + return resultStr; + } +} diff --git a/src/main/java/com/example/modular/api/service/DeviceDataPushService.java b/src/main/java/com/example/modular/api/service/DeviceDataPushService.java new file mode 100644 index 0000000..878c730 --- /dev/null +++ b/src/main/java/com/example/modular/api/service/DeviceDataPushService.java @@ -0,0 +1,40 @@ +package com.example.modular.api.service; + +import org.springframework.web.bind.annotation.RequestParam; + +/** + * 水电表数据推送 service + * @author ysl + * @since 2022-01-06 + */ +public interface DeviceDataPushService { + + /** + * 从设备编号获取用水量数据 + * @param base64 照片base64 + * @param imgUrl 照片下载地址 + * @param devId 设备ID + * @param devVol 设备电压 + * @param isPointerMeter 是否是指针表 + * @param createTime 上传时间 yyyy-MM-dd HH:mm:ss + * @param result 识别结果JSON字符串 + * @return + * @author ysl + */ + String deviceWaterSave(String base64,String imgUrl,String devId,Integer devVol,boolean isPointerMeter,String createTime,String result); + + + /** + * 从设备编号获取用电量数据 + * @param base64 照片base64 + * @param imgUrl 照片下载地址 + * @param devId 设备ID + * @param devVol 设备电压 + * @param isPointerMeter 是否是指针表 + * @param createTime 上传时间 yyyy-MM-dd HH:mm:ss + * @param result 识别结果JSON字符串 + * @return + * @author ysl + */ + String deviceElectricitySave(String base64,String imgUrl,String devId,Integer devVol,boolean isPointerMeter,String createTime,String result); +} diff --git a/src/main/java/com/example/modular/api/service/impl/DeviceDataPushServiceImpl.java b/src/main/java/com/example/modular/api/service/impl/DeviceDataPushServiceImpl.java new file mode 100644 index 0000000..34a5028 --- /dev/null +++ b/src/main/java/com/example/modular/api/service/impl/DeviceDataPushServiceImpl.java @@ -0,0 +1,204 @@ +package com.example.modular.api.service.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.example.common.Constant; +import com.example.modular.api.service.DeviceDataPushService; +import com.example.modular.device.po.BaseDeviceDelivery; +import com.example.modular.device.service.IBaseDeviceDeliveryService; +import com.example.modular.energyconsumption.po.ToiletElectricityConsumption; +import com.example.modular.energyconsumption.po.ToiletWaterConsumption; +import com.example.modular.energyconsumption.service.IToiletElectricityConsumptionService; +import com.example.modular.energyconsumption.service.IToiletWaterConsumptionService; +import com.example.modular.toilet.po.Toilet; +import com.example.modular.toilet.service.IToiletService; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 水电表数据推送 实现类 + * @author ysl + * @since 2022-01-06 + */ +@Service +public class DeviceDataPushServiceImpl implements DeviceDataPushService { + + @Autowired + private IBaseDeviceDeliveryService baseDeviceDeliveryService; + @Autowired + private IToiletService toiletService; + @Autowired + private IToiletWaterConsumptionService toiletWaterConsumptionService; + @Autowired + private IToiletElectricityConsumptionService toiletElectricityConsumptionService; + + @Override + public String deviceWaterSave(String base64, String imgUrl, String devId, Integer devVol, boolean isPointerMeter, String createTime, String result) { + //校验参数是否正确 + if (StringUtils.isBlank(devId) || StringUtils.isBlank(createTime) || StringUtils.isBlank(result)) { + return "PARAMS ERROR"; + } + //校验设备ID是否存在 + QueryWrapper baseDeviceDeliveryQueryWrapper = new QueryWrapper<>(); + baseDeviceDeliveryQueryWrapper.eq("device_id",devId); + baseDeviceDeliveryQueryWrapper.eq("delete_status", Constant.TOILET_DEL_STATUS_NO); + baseDeviceDeliveryQueryWrapper.eq("device_type", Constant.DEVICE_WATER_TYPE); + List baseDeviceDeliveryList = baseDeviceDeliveryService.list(baseDeviceDeliveryQueryWrapper); + if (CollectionUtils.isEmpty(baseDeviceDeliveryList)) { + return "DEVICE NOT EXIST"; + } + //获取公厕ID + Long toiletId = baseDeviceDeliveryList.get(0).getToiletId(); + //校验公厕是否存在 + Toilet toilet = toiletService.getById(toiletId); + if (toilet == null) { + return "TOILET NOT EXIST"; + } + //resule JSON解析 + JSONObject parseResult = JSON.parseObject(result); + Integer outputState = parseResult.getInteger("outputState"); + if (!outputState.equals(0)) { + return "outputState:" + outputState; + } + Double resultValue; + //获取数据值 + if (!isPointerMeter) { + //非指针表 + String outputStrDec = parseResult.getString("outputStrDec"); + String outputStrInt = parseResult.getString("outputStrInt"); + resultValue = Double.parseDouble(outputStrInt + "." + outputStrDec); + } else { + resultValue = parseResult.getDoubleValue("outputReadingValue"); + } + //获取上传时间当天的开始和结束时间 以及 前一天的开始时间和结束时间 + String todayStartTime = createTime.split(" ")[0] + " 00:00:00"; + String todayEndTime = createTime.split(" ")[0] + " 23:59:59"; + //获取用水量创建时间当天的数据和当天之前的用水量总和 + Double useWaterBeforeDate = toiletWaterConsumptionService.sumWaterBeforeDate(toiletId,todayStartTime); + if (resultValue < useWaterBeforeDate) { + return "USE WATER LESS THAN BEFORE"; + } + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date createDateTime; + try { + createDateTime = dateFormat.parse(createTime); + } catch (ParseException e) { + return "CREATETIME FORMAT ERROR"; + } + QueryWrapper todayToiletWaterConsumptionQueryWrapper = new QueryWrapper<>(); + todayToiletWaterConsumptionQueryWrapper.eq("toilet_id",toiletId); + todayToiletWaterConsumptionQueryWrapper.ge("create_time",todayStartTime); + todayToiletWaterConsumptionQueryWrapper.le("create_time",todayEndTime); + List todayToiletWaterList = toiletWaterConsumptionService.list(todayToiletWaterConsumptionQueryWrapper); + Double dateUseWater = resultValue - useWaterBeforeDate; + if (CollectionUtils.isEmpty(todayToiletWaterList)) { + ToiletWaterConsumption toiletWaterConsumption = new ToiletWaterConsumption(); + toiletWaterConsumption.setToiletId(toiletId); + toiletWaterConsumption.setUseWater(dateUseWater); + toiletWaterConsumption.setCreateTime(createDateTime); + toiletWaterConsumption.setUpdateTime(createDateTime); + toiletWaterConsumptionService.save(toiletWaterConsumption); + } else { + ToiletWaterConsumption toiletWaterConsumption = todayToiletWaterList.get(0); + toiletWaterConsumption.setUseWater(dateUseWater); + toiletWaterConsumption.setUpdateTime(createDateTime); + toiletWaterConsumptionService.updateById(toiletWaterConsumption); + } + return "SUCCESS"; + } + + @Override + public String deviceElectricitySave(String base64, String imgUrl, String devId, Integer devVol, boolean isPointerMeter, String createTime, String result) { + //校验参数是否正确 + if (StringUtils.isBlank(devId) || StringUtils.isBlank(createTime) || StringUtils.isBlank(result)) { + return "PARAMS ERROR"; + } + //校验设备ID是否存在 + QueryWrapper baseDeviceDeliveryQueryWrapper = new QueryWrapper<>(); + baseDeviceDeliveryQueryWrapper.eq("device_id",devId); + baseDeviceDeliveryQueryWrapper.eq("delete_status", Constant.TOILET_DEL_STATUS_NO); + baseDeviceDeliveryQueryWrapper.eq("device_type", Constant.DEVICE_ELECTRICITY_TYPE); + List baseDeviceDeliveryList = baseDeviceDeliveryService.list(baseDeviceDeliveryQueryWrapper); + if (CollectionUtils.isEmpty(baseDeviceDeliveryList)) { + return "DEVICE NOT EXIST"; + } + //获取公厕ID + Long toiletId = baseDeviceDeliveryList.get(0).getToiletId(); + //校验公厕是否存在 + Toilet toilet = toiletService.getById(toiletId); + if (toilet == null) { + return "TOILET NOT EXIST"; + } + //resule JSON解析 + JSONObject parseResult = JSON.parseObject(result); + Integer outputState = parseResult.getInteger("outputState"); + if (!outputState.equals(0)) { + return "outputState:" + outputState; + } + Double resultValue; + //获取数据值 + if (!isPointerMeter) { + //非指针表 + String outputStrDec = parseResult.getString("outputStrDec"); + String outputStrInt = parseResult.getString("outputStrInt"); + resultValue = Double.parseDouble(outputStrInt + "." + outputStrDec); + } else { + resultValue = parseResult.getDoubleValue("outputReadingValue"); + } + //获取上传时间当天的开始和结束时间 以及 前一天的开始时间和结束时间 + String todayStartTime = createTime.split(" ")[0] + " 00:00:00"; + String todayEndTime = createTime.split(" ")[0] + " 23:59:59"; + //获取用电量创建时间当天的数据和当天之前的用水量总和 + Double useElectricityBeforeDate = toiletElectricityConsumptionService.sumElectricityBeforeDate(toiletId,todayStartTime); + if (resultValue < useElectricityBeforeDate) { + return "USE WATER LESS THAN BEFORE"; + } + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date createDateTime; + try { + createDateTime = dateFormat.parse(createTime); + } catch (ParseException e) { + return "CREATETIME FORMAT ERROR"; + } + QueryWrapper toiletElectricityConsumptionQueryWrapper = new QueryWrapper<>(); + toiletElectricityConsumptionQueryWrapper.eq("toilet_id",toiletId); + toiletElectricityConsumptionQueryWrapper.ge("create_time",todayStartTime); + toiletElectricityConsumptionQueryWrapper.le("create_time",todayEndTime); + List toiletElectricityConsumptionList = toiletElectricityConsumptionService.list(toiletElectricityConsumptionQueryWrapper); + Double dateUseElectricity = resultValue - useElectricityBeforeDate; + if (CollectionUtils.isEmpty(toiletElectricityConsumptionList)) { + ToiletElectricityConsumption toiletElectricityConsumption = new ToiletElectricityConsumption(); + toiletElectricityConsumption.setToiletId(toiletId); + toiletElectricityConsumption.setUseElectricity(dateUseElectricity); + toiletElectricityConsumption.setCreateTime(createDateTime); + toiletElectricityConsumption.setUpdateTime(createDateTime); + toiletElectricityConsumptionService.save(toiletElectricityConsumption); + } else { + ToiletElectricityConsumption toiletElectricityConsumption = toiletElectricityConsumptionList.get(0); + toiletElectricityConsumption.setUseElectricity(dateUseElectricity); + toiletElectricityConsumption.setUpdateTime(createDateTime); + toiletElectricityConsumptionService.updateById(toiletElectricityConsumption); + } + return "SUCCESS"; + } + + public static void main (String[] args) { + Map map = new HashMap<>(); + map.put("outputState",0); + map.put("outputReadingValue",15); + map.put("outputStrDec",12); + map.put("outputStrInt",13); + String s = JSON.toJSONString(map); + System.out.println(s); + } +} diff --git a/src/main/java/com/example/modular/device/mapper/mapping/BaseDeviceDeliveryMapper.xml b/src/main/java/com/example/modular/device/mapper/mapping/BaseDeviceDeliveryMapper.xml index e40132c..284966e 100644 --- a/src/main/java/com/example/modular/device/mapper/mapping/BaseDeviceDeliveryMapper.xml +++ b/src/main/java/com/example/modular/device/mapper/mapping/BaseDeviceDeliveryMapper.xml @@ -13,6 +13,7 @@ + diff --git a/src/main/java/com/example/modular/device/po/BaseDeviceDelivery.java b/src/main/java/com/example/modular/device/po/BaseDeviceDelivery.java index d2553f2..cd889ae 100644 --- a/src/main/java/com/example/modular/device/po/BaseDeviceDelivery.java +++ b/src/main/java/com/example/modular/device/po/BaseDeviceDelivery.java @@ -69,5 +69,10 @@ public class BaseDeviceDelivery extends Model { @TableField("update_by") private String updateBy; + /** + * 是否删除:0 未删除 1 已删除 + */ + @TableField("delete_status") + private Integer deleteStatus; } diff --git a/src/main/java/com/example/modular/energyconsumption/mapper/ToiletElectricityConsumptionMapper.java b/src/main/java/com/example/modular/energyconsumption/mapper/ToiletElectricityConsumptionMapper.java index 8c5e830..52baadd 100644 --- a/src/main/java/com/example/modular/energyconsumption/mapper/ToiletElectricityConsumptionMapper.java +++ b/src/main/java/com/example/modular/energyconsumption/mapper/ToiletElectricityConsumptionMapper.java @@ -64,4 +64,14 @@ public interface ToiletElectricityConsumptionMapper extends BaseMapper toiletIdList, @Param(value = "startTime") Date startTime, @Param(value = "endTime") Date endTime); + + + /** + * 获取日期前的用电量总和接口 + * @param toiletId + * @param dateTime + * @return + * @author ysl + */ + Double sumElectricityBeforeDate(@Param(value = "toiletId")Long toiletId,@Param(value = "dateTime") String dateTime); } diff --git a/src/main/java/com/example/modular/energyconsumption/mapper/ToiletWaterConsumptionMapper.java b/src/main/java/com/example/modular/energyconsumption/mapper/ToiletWaterConsumptionMapper.java index 8d917e0..05e92d0 100644 --- a/src/main/java/com/example/modular/energyconsumption/mapper/ToiletWaterConsumptionMapper.java +++ b/src/main/java/com/example/modular/energyconsumption/mapper/ToiletWaterConsumptionMapper.java @@ -87,4 +87,13 @@ public interface ToiletWaterConsumptionMapper extends BaseMapper toiletIdList,@Param(value = "startTime") Date startTime,@Param(value = "endTime") Date endTime); + + /** + * 获取日期前的用水量总和接口 + * @param toiletId + * @param dateTime + * @return + * @author ysl + */ + Double sumWaterBeforeDate(@Param(value = "toiletId")Long toiletId,@Param(value = "dateTime") String dateTime); } diff --git a/src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletElectricityConsumptionMapper.xml b/src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletElectricityConsumptionMapper.xml index 8ea7837..e1e9ef9 100644 --- a/src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletElectricityConsumptionMapper.xml +++ b/src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletElectricityConsumptionMapper.xml @@ -84,4 +84,10 @@ and create_time < #{endTime} + + diff --git a/src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletWaterConsumptionMapper.xml b/src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletWaterConsumptionMapper.xml index 5e35c43..23453db 100644 --- a/src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletWaterConsumptionMapper.xml +++ b/src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletWaterConsumptionMapper.xml @@ -125,4 +125,11 @@ and create_time < #{endTime} + + + diff --git a/src/main/java/com/example/modular/energyconsumption/service/IToiletElectricityConsumptionService.java b/src/main/java/com/example/modular/energyconsumption/service/IToiletElectricityConsumptionService.java index c1a923f..3e37087 100644 --- a/src/main/java/com/example/modular/energyconsumption/service/IToiletElectricityConsumptionService.java +++ b/src/main/java/com/example/modular/energyconsumption/service/IToiletElectricityConsumptionService.java @@ -82,4 +82,12 @@ public interface IToiletElectricityConsumptionService extends IService