Browse Source

perf 开发公厕对接用水用电量设备接口

master
yingsl 3 years ago
parent
commit
5107fb984a
  1. 14
      src/main/java/com/example/common/Constant.java
  2. 69
      src/main/java/com/example/modular/api/controller/DeviceDataPushController.java
  3. 40
      src/main/java/com/example/modular/api/service/DeviceDataPushService.java
  4. 204
      src/main/java/com/example/modular/api/service/impl/DeviceDataPushServiceImpl.java
  5. 1
      src/main/java/com/example/modular/device/mapper/mapping/BaseDeviceDeliveryMapper.xml
  6. 5
      src/main/java/com/example/modular/device/po/BaseDeviceDelivery.java
  7. 10
      src/main/java/com/example/modular/energyconsumption/mapper/ToiletElectricityConsumptionMapper.java
  8. 9
      src/main/java/com/example/modular/energyconsumption/mapper/ToiletWaterConsumptionMapper.java
  9. 6
      src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletElectricityConsumptionMapper.xml
  10. 7
      src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletWaterConsumptionMapper.xml
  11. 8
      src/main/java/com/example/modular/energyconsumption/service/IToiletElectricityConsumptionService.java
  12. 9
      src/main/java/com/example/modular/energyconsumption/service/IToiletWaterConsumptionService.java
  13. 6
      src/main/java/com/example/modular/energyconsumption/service/impl/ToiletElectricityConsumptionServiceImpl.java
  14. 6
      src/main/java/com/example/modular/energyconsumption/service/impl/ToiletWaterConsumptionServiceImpl.java

14
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;
}

69
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;
}
}

40
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);
}

204
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<BaseDeviceDelivery> 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<BaseDeviceDelivery> 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<ToiletWaterConsumption> todayToiletWaterConsumptionQueryWrapper = new QueryWrapper<>();
todayToiletWaterConsumptionQueryWrapper.eq("toilet_id",toiletId);
todayToiletWaterConsumptionQueryWrapper.ge("create_time",todayStartTime);
todayToiletWaterConsumptionQueryWrapper.le("create_time",todayEndTime);
List<ToiletWaterConsumption> 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<BaseDeviceDelivery> 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<BaseDeviceDelivery> 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<ToiletElectricityConsumption> toiletElectricityConsumptionQueryWrapper = new QueryWrapper<>();
toiletElectricityConsumptionQueryWrapper.eq("toilet_id",toiletId);
toiletElectricityConsumptionQueryWrapper.ge("create_time",todayStartTime);
toiletElectricityConsumptionQueryWrapper.le("create_time",todayEndTime);
List<ToiletElectricityConsumption> 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<String,Object> 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);
}
}

1
src/main/java/com/example/modular/device/mapper/mapping/BaseDeviceDeliveryMapper.xml

@ -13,6 +13,7 @@
<result column="create_by" property="createBy" />
<result column="update_time" property="updateTime" />
<result column="update_by" property="updateBy" />
<result column="delete_status" property="deleteStatus" />
</resultMap>
<!-- 通用查询结果列 -->

5
src/main/java/com/example/modular/device/po/BaseDeviceDelivery.java

@ -69,5 +69,10 @@ public class BaseDeviceDelivery extends Model<BaseDeviceDelivery> {
@TableField("update_by")
private String updateBy;
/**
* 是否删除0 未删除 1 已删除
*/
@TableField("delete_status")
private Integer deleteStatus;
}

10
src/main/java/com/example/modular/energyconsumption/mapper/ToiletElectricityConsumptionMapper.java

@ -64,4 +64,14 @@ public interface ToiletElectricityConsumptionMapper extends BaseMapper<ToiletEle
* @author ysl
*/
Double sumLastMonthUseElectricityByToiletIdListAndTime(@Param(value = "toiletIdList") List<Long> 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);
}

9
src/main/java/com/example/modular/energyconsumption/mapper/ToiletWaterConsumptionMapper.java

@ -87,4 +87,13 @@ public interface ToiletWaterConsumptionMapper extends BaseMapper<ToiletWaterCons
* @author ysl
*/
Double sumLastMonthUseWaterByToiletIdListAndTime (@Param(value = "toiletIdList") List<Long> 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);
}

6
src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletElectricityConsumptionMapper.xml

@ -84,4 +84,10 @@
and create_time &lt; #{endTime}
</if>
</select>
<select id="sumElectricityBeforeDate" resultType="java.lang.Double">
select ifnull(sum(use_electricity),0) as use_electricity
from toilet_electricity_consumption where toilet_id = #{toiletId}
and create_time &lt; #{dateTime}
</select>
</mapper>

7
src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletWaterConsumptionMapper.xml

@ -125,4 +125,11 @@
and create_time &lt; #{endTime}
</if>
</select>
<select id="sumWaterBeforeDate" resultType="java.lang.Double">
select ifnull(sum(use_water),0) as use_water
from toilet_water_consumption where toilet_id = #{toiletId}
and create_time &lt; #{dateTime}
</select>
</mapper>

8
src/main/java/com/example/modular/energyconsumption/service/IToiletElectricityConsumptionService.java

@ -82,4 +82,12 @@ public interface IToiletElectricityConsumptionService extends IService<ToiletEle
*/
Result exportPeopleElectricityConsumption(String userToken,PassengerChartParamVO passengerChartParamVO,HttpServletResponse response,Integer type);
/**
* 获取日期前的用电量总和接口
* @param toiletId
* @param dateTime
* @return
* @author ysl
*/
Double sumElectricityBeforeDate(Long toiletId,String dateTime);
}

9
src/main/java/com/example/modular/energyconsumption/service/IToiletWaterConsumptionService.java

@ -81,4 +81,13 @@ public interface IToiletWaterConsumptionService extends IService<ToiletWaterCons
* @author ysl
*/
Result exportPeopleWaterConsumptionList(String userToken,PassengerChartParamVO passengerChartParamVO,HttpServletResponse response,Integer type);
/**
* 获取日期前的用水量总和接口
* @param toiletId
* @param dateTime
* @return
* @author ysl
*/
Double sumWaterBeforeDate(Long toiletId,String dateTime);
}

6
src/main/java/com/example/modular/energyconsumption/service/impl/ToiletElectricityConsumptionServiceImpl.java

@ -557,4 +557,10 @@ public class ToiletElectricityConsumptionServiceImpl extends ServiceImpl<ToiletE
}
return new Result();
}
@Override
public Double sumElectricityBeforeDate(Long toiletId, String dateTime) {
Double electricity = this.baseMapper.sumElectricityBeforeDate(toiletId,dateTime);
return electricity;
}
}

6
src/main/java/com/example/modular/energyconsumption/service/impl/ToiletWaterConsumptionServiceImpl.java

@ -562,5 +562,11 @@ public class ToiletWaterConsumptionServiceImpl extends ServiceImpl<ToiletWaterCo
return new Result();
}
@Override
public Double sumWaterBeforeDate(Long toiletId,String dateTime) {
Double water = this.baseMapper.sumWaterBeforeDate(toiletId,dateTime);
return water;
}
}
Loading…
Cancel
Save