|
@@ -1,4 +1,12 @@
|
|
|
package com.bz.rmcp.dap.service.impl;
|
|
|
+import com.bz.rmcp.dap.common.Constants;
|
|
|
+import com.bz.rmcp.dap.model.MeasureInfo;
|
|
|
+import com.bz.rmcp.dap.model.MeterData;
|
|
|
+import com.bz.rmcp.dap.service.ProductService;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.Date;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
@@ -10,18 +18,22 @@ import com.bz.rmcp.dap.service.DeviceDataService;
|
|
|
import com.zcxk.core.common.util.SnowflakeIdWorker;
|
|
|
import com.zcxk.core.utils.DateUtil;
|
|
|
import com.zcxk.rmcp.api.enums.DeviceStatusEnum;
|
|
|
+import com.zcxk.rmcp.api.enums.ValveStatusEnum;
|
|
|
+import com.zcxk.rmcp.api.vo.DeviceDetailVo;
|
|
|
import com.zcxk.rmcp.core.dao.DeviceMapper;
|
|
|
import com.zcxk.rmcp.core.dao.ProductMapper;
|
|
|
import com.zcxk.rmcp.core.entity.Device;
|
|
|
import com.zcxk.rmcp.core.entity.Product;
|
|
|
import com.zcxk.rmcp.core.mongo.DeviceData;
|
|
|
import com.zcxk.rmcp.core.mongo.DeviceDataItem;
|
|
|
+import com.zcxk.rmcp.core.mongo.MeterReadRecord;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.data.mongodb.core.query.Update;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
@@ -42,33 +54,173 @@ public class DeviceDataServiceImpl implements DeviceDataService {
|
|
|
@Autowired
|
|
|
private MongoTemplate mongoTemplate;
|
|
|
@Autowired
|
|
|
- private ProductMapper productMapper;
|
|
|
+ private ProductService productService;
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
|
|
|
@Override
|
|
|
public int saveDeviceData(DeviceOrigDataDTO data) {
|
|
|
- log.info("begin saveDeviceData,device = {} ,data = {}" , JSON.toJSONString(data));
|
|
|
+ log.info("begin saveDeviceData ,data = {}" , JSON.toJSONString(data));
|
|
|
Device device = deviceMapper.findByDeviceNo(data.getDeviceNo());
|
|
|
if (device != null) {
|
|
|
// 1,保存设备测点数据
|
|
|
- Map<String,Object> measureMap = saveDeviceMeasuringDatas(device,data);
|
|
|
+ MeasureInfo measureInfo = saveDeviceMeasuringData(device,data);
|
|
|
|
|
|
- // 2,更新设备
|
|
|
- updateDevice(device,measureMap);
|
|
|
+ // 2,更新设备信息
|
|
|
+ MeterData meterData = updateDevice(device,measureInfo);
|
|
|
+
|
|
|
+ // 3,保存抄表数据
|
|
|
+ saveReadingData(meterData);
|
|
|
|
|
|
}
|
|
|
log.info("end saveDeviceData");
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ * @description 保存抄表数据
|
|
|
+ * @param updateDevice
|
|
|
+ * @return void
|
|
|
+ * @author linqingwei
|
|
|
+ **/
|
|
|
+ private void saveReadingData(MeterData meterData) {
|
|
|
+ log.info("begin saveReadingData ,meterData = {}" , JSON.toJSONString(meterData));
|
|
|
+ MeterReadRecord.MeterReadInfo meterReadInfo = buildMeterReadInfo(meterData);
|
|
|
+
|
|
|
+ //更新数据项
|
|
|
+ Query updateQuery = new Query();
|
|
|
+ updateQuery.addCriteria(Criteria.where("deviceId").is(meterData.getDeviceId()));
|
|
|
+ updateQuery.addCriteria(Criteria.where("data.readDate").is(DateUtil.getDate(new Date())));
|
|
|
+ Update update = new Update();
|
|
|
+ update.set("data.$.readTime",meterData.getReceiveDate());
|
|
|
+ update.set("data.$.readData",meterData.getReadData());
|
|
|
+ update.set("data.$.valveStatus",meterData.getValveStatus());
|
|
|
+ update.set("data.$.lastValid",meterReadInfo.getLastValid());
|
|
|
+ update.inc("data.$.lastCost", meterReadInfo.getLastCost());
|
|
|
+ long updateResult = mongoTemplate.updateMulti(updateQuery,update,Constants.METER_READ_RECORD_TABLE).getModifiedCount();
|
|
|
+ if(updateResult == 0){
|
|
|
+ //更新失败推送数据
|
|
|
+ Query pushQuery = new Query();
|
|
|
+ pushQuery.addCriteria(Criteria.where("deviceId").is(meterData.getDeviceId()));
|
|
|
+ Update push = new Update();
|
|
|
+ push.push("data",meterReadInfo);
|
|
|
+ long pushResult = mongoTemplate.updateMulti(pushQuery,push,Constants.METER_READ_RECORD_TABLE).getModifiedCount();
|
|
|
+ if(pushResult == 0){
|
|
|
+ //推送失败插入数据
|
|
|
+ MeterReadRecord meterReadRecord = buildMeterReadRecord(meterData,meterReadInfo);
|
|
|
+ mongoTemplate.insert(meterReadRecord,Constants.METER_READ_RECORD_TABLE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //保存最新抄表信息到缓存中
|
|
|
+ redisTemplate.opsForValue().set(Constants.PREFIX_CACHE_FLAG+meterData.getDeviceId(),meterData);
|
|
|
+ log.info("end saveReadingData");
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * @description 构建MeterReadInfo
|
|
|
+ * @param device
|
|
|
+ * @return com.zcxk.rmcp.core.mongo.MeterReadRecord.MeterReadInfo
|
|
|
+ * @author linqingwei
|
|
|
+ **/
|
|
|
+ private MeterReadRecord.MeterReadInfo buildMeterReadInfo(MeterData meterData) {
|
|
|
+ // 上次表数据
|
|
|
+ MeterData lastMeterData = getMeterLastData(meterData);
|
|
|
+ // 计算今日用水量
|
|
|
+ BigDecimal todayCost = calculateTodayCost(meterData.getReadData(), lastMeterData.getReadData());
|
|
|
+
|
|
|
+ MeterReadRecord.MeterReadInfo readInfo = new MeterReadRecord.MeterReadInfo();
|
|
|
+ readInfo.setReadDate(DateUtil.getDate(new Date()));
|
|
|
+ readInfo.setReadTime(meterData.getReceiveDate());
|
|
|
+ readInfo.setValveStatus(meterData.getValveStatus());
|
|
|
+ readInfo.setReadData(meterData.getReadData());
|
|
|
+ readInfo.setLastValid(lastMeterData.getReadData());
|
|
|
+ readInfo.setLastCost(todayCost.doubleValue());//用水量
|
|
|
+ return readInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ private BigDecimal calculateTodayCost(Double currentReading, Double lastValid) {
|
|
|
+ BigDecimal currentValidDecimal = new BigDecimal(currentReading);
|
|
|
+ BigDecimal lastValidDecimal = new BigDecimal(lastValid);
|
|
|
+ return currentValidDecimal.subtract(lastValidDecimal);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*
|
|
|
+ * @description 获取最后抄表数据
|
|
|
+ * @param waterMeter
|
|
|
+ * @param readDate
|
|
|
+ * @return java.lang.String
|
|
|
+ * @author linqingwei
|
|
|
+ **/
|
|
|
+ private MeterData getMeterLastData(MeterData meterData){
|
|
|
+ MeterData data = null;
|
|
|
+ data = getMeterLastDataFromCache(meterData.getDeviceId());
|
|
|
+ if(data==null){
|
|
|
+ data = new MeterData();
|
|
|
+ }
|
|
|
+
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ private MeterData getMeterLastDataFromCache(Long deviceId) {
|
|
|
+ return (MeterData) redisTemplate.opsForValue().get(Constants.PREFIX_CACHE_FLAG+deviceId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * @description 构建MeterReadRecord
|
|
|
+ * @param device
|
|
|
+ * @return com.zcxk.rmcp.core.mongo.MeterReadRecord
|
|
|
+ * @author linqingwei
|
|
|
+ **/
|
|
|
+ private MeterReadRecord buildMeterReadRecord(MeterData meterData,MeterReadRecord.MeterReadInfo meterReadInfo) {
|
|
|
+ DeviceDetailVo vo = deviceMapper.findDetail(meterData.getDeviceId());
|
|
|
+
|
|
|
+ List<MeterReadRecord.MeterReadInfo> list = new ArrayList<>();
|
|
|
+ list.add(meterReadInfo);
|
|
|
+
|
|
|
+ MeterReadRecord readRecord = new MeterReadRecord();
|
|
|
+ readRecord.setYear(DateUtil.getYear(new Date()));
|
|
|
+ readRecord.setTenantId(vo.getTenantId());
|
|
|
+ readRecord.setCategoryId(vo.getCategoryId());
|
|
|
+ readRecord.setCompanyOrgId(vo.getCompanyOrgId());
|
|
|
+ readRecord.setCompanyOrgName(vo.getCompanyName());
|
|
|
+ readRecord.setDeptOrgId(vo.getDeptOrgId());
|
|
|
+ readRecord.setDeptOrgName(vo.getDeptName());
|
|
|
+ readRecord.setCommunityId(vo.getCommunityId());
|
|
|
+ readRecord.setCommunityName(vo.getCommunityName());
|
|
|
+ //readRecord.setConcentratorId(0);
|
|
|
+ //readRecord.setConcentratorCode("");
|
|
|
+ //readRecord.setCollectorId(0);
|
|
|
+ //readRecord.setCollectorCode("");
|
|
|
+ //readRecord.setChannelNumber("");
|
|
|
+ readRecord.setDeviceId(vo.getId());
|
|
|
+ readRecord.setDeviceNo(vo.getDeviceNo());
|
|
|
+ readRecord.setLocation(vo.getDeviceNo());
|
|
|
+ readRecord.setProductId(vo.getProductId());
|
|
|
+ readRecord.setDeviceModel(vo.getProductModel());
|
|
|
+ readRecord.setMeterNo(vo.getMeterNo());
|
|
|
+ readRecord.setFileNo(vo.getFileNo());
|
|
|
+ readRecord.setCreateTime(new Date());
|
|
|
+ readRecord.setCreatorName(Constants.SYS_FLAG);
|
|
|
+ readRecord.setUpdateTime(new Date());
|
|
|
+ readRecord.setData(list);
|
|
|
+
|
|
|
+ return readRecord;
|
|
|
+ }
|
|
|
+
|
|
|
/*
|
|
|
* @description 更新设备
|
|
|
* @param device
|
|
|
* @param measureMap
|
|
|
- * @return void
|
|
|
+ * @return MeterData
|
|
|
* @author linqingwei
|
|
|
**/
|
|
|
- private void updateDevice(Device device, Map<String, Object> measureMap) {
|
|
|
- Product product = productMapper.findById(device.getProductId());
|
|
|
+ private MeterData updateDevice(Device device, MeasureInfo measureInfo) {
|
|
|
+ log.info("begin updateDevice ,device = {},measureInfo = {}" , JSON.toJSONString(device),JSON.toJSONString(measureInfo));
|
|
|
+ Map<String, Object> measureMap = measureInfo.getMeasureMap();
|
|
|
+
|
|
|
+ Product product = productService.findProductByCache(device.getProductId());
|
|
|
Device update = new Device();
|
|
|
update.setId(device.getId());
|
|
|
update.setDeviceStatus(DeviceStatusEnum.NORMAL.getCode());
|
|
@@ -77,8 +229,24 @@ public class DeviceDataServiceImpl implements DeviceDataService {
|
|
|
update.setLastReceiveTime(LocalDateTime.now());
|
|
|
update.setUpdateDate(LocalDateTime.now());
|
|
|
deviceMapper.updateByPrimaryKeySelective(update);
|
|
|
+
|
|
|
+ MeterData meterData = new MeterData();
|
|
|
+ meterData.setDeviceId(device.getId());
|
|
|
+ if(update.getReadData()!=null)meterData.setReadData(Double.valueOf(update.getReadData()));
|
|
|
+ if(update.getValveStatus()!=null)meterData.setValveStatus(update.getValveStatus());
|
|
|
+ meterData.setReceiveDate(measureInfo.getReceiveDate());
|
|
|
+
|
|
|
+ log.info("end updateDevice");
|
|
|
+ return meterData;
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ * @description 按抄表code获取读数
|
|
|
+ * @param measureMap
|
|
|
+ * @param product
|
|
|
+ * @return java.lang.String
|
|
|
+ * @author linqingwei
|
|
|
+ **/
|
|
|
private String convertReadData(Map<String, Object> measureMap,Product product){
|
|
|
Object temp = measureMap.get(product.getReadingMeasuringCode());
|
|
|
if (temp != null) {
|
|
@@ -86,10 +254,21 @@ public class DeviceDataServiceImpl implements DeviceDataService {
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ /*
|
|
|
+ * @description 按阀门code获取阀门状态
|
|
|
+ * @param measureMap
|
|
|
+ * @param product
|
|
|
+ * @return java.lang.Integer
|
|
|
+ * @author linqingwei
|
|
|
+ **/
|
|
|
private Integer convertValveStatus(Map<String, Object> measureMap,Product product){
|
|
|
Object temp = measureMap.get(product.getValveMeasuringCode());
|
|
|
if (temp != null) {
|
|
|
- return (Integer) measureMap.get(product.getValveMeasuringCode());
|
|
|
+ Integer valve = Integer.valueOf((String) measureMap.get(product.getValveMeasuringCode()));
|
|
|
+ if(valve == 0) return ValveStatusEnum.ON.getCode();
|
|
|
+ if(valve == 1) return ValveStatusEnum.OFF.getCode();
|
|
|
+ return ValveStatusEnum.ABNORMAL.getCode();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
@@ -98,20 +277,25 @@ public class DeviceDataServiceImpl implements DeviceDataService {
|
|
|
* @description 保存设备测点数据
|
|
|
* @param device
|
|
|
* @param data
|
|
|
- * @return void
|
|
|
+ * @return MeasureInfo
|
|
|
* @author linqingwei
|
|
|
**/
|
|
|
- private Map<String, Object> saveDeviceMeasuringDatas(Device device, DeviceOrigDataDTO data) {
|
|
|
- Map<String, Object> measureMap = new HashMap<>();
|
|
|
- // 3.保存数据
|
|
|
+ private MeasureInfo saveDeviceMeasuringData(Device device, DeviceOrigDataDTO data) {
|
|
|
+ MeasureInfo measureInfo = new MeasureInfo();
|
|
|
+ // 保存数据
|
|
|
for (Map.Entry<Date, String> entry : data.getParsedData().entrySet()) {
|
|
|
|
|
|
Date date = entry.getKey();
|
|
|
- String value = entry.getValue();
|
|
|
+ String json = entry.getValue();
|
|
|
+
|
|
|
+
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(json);
|
|
|
+ Map<String, Object> measureMap = (Map<String, Object>)jsonObject;
|
|
|
+
|
|
|
+ measureInfo.setReceiveDate(date);
|
|
|
+ measureInfo.setMeasureMap(measureMap);
|
|
|
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(value);
|
|
|
- measureMap = (Map<String, Object>)jsonObject;
|
|
|
- DeviceDataItem deviceDataItem = buildDeviceDataItem(date ,measureMap);
|
|
|
+ DeviceDataItem deviceDataItem = buildDeviceDataItem(measureInfo);
|
|
|
|
|
|
|
|
|
//更新数据项
|
|
@@ -119,15 +303,15 @@ public class DeviceDataServiceImpl implements DeviceDataService {
|
|
|
query.addCriteria(Criteria.where("deviceId").is(device.getId()));
|
|
|
Update update = new Update();
|
|
|
update.push("data", deviceDataItem);
|
|
|
- long result = mongoTemplate.updateMulti(query,update,"rmcp_device_data").getModifiedCount();
|
|
|
+ long updateResult = mongoTemplate.updateMulti(query,update,Constants.DEVICE_DATA_TABLE).getModifiedCount();
|
|
|
|
|
|
- if(result == 0){
|
|
|
+ if(updateResult == 0){
|
|
|
//更新失败插入数据
|
|
|
- DeviceData deviceData = buildDeviceData(device,date ,measureMap);
|
|
|
- mongoTemplate.insert(deviceData,"rmcp_device_data");
|
|
|
+ DeviceData deviceData = buildDeviceData(device,measureInfo);
|
|
|
+ mongoTemplate.insert(deviceData,Constants.DEVICE_DATA_TABLE);
|
|
|
}
|
|
|
}
|
|
|
- return measureMap;
|
|
|
+ return measureInfo;
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -137,11 +321,11 @@ public class DeviceDataServiceImpl implements DeviceDataService {
|
|
|
* @return com.zcxk.rmcp.core.mongo.DeviceDataItem
|
|
|
* @author linqingwei
|
|
|
**/
|
|
|
- private DeviceDataItem buildDeviceDataItem(Date date, Map<String, Object> measureMap) {
|
|
|
+ private DeviceDataItem buildDeviceDataItem(MeasureInfo measureInfo) {
|
|
|
DeviceDataItem dataItem = new DeviceDataItem();
|
|
|
- dataItem.setReceiveDate(DateUtil.getDate(date));
|
|
|
- dataItem.setReceiveTime(date.getTime());
|
|
|
- dataItem.setMeasureData(measureMap);
|
|
|
+ dataItem.setReceiveDate(DateUtil.getDate(measureInfo.getReceiveDate()));
|
|
|
+ dataItem.setReceiveTime(measureInfo.getReceiveDate().getTime());
|
|
|
+ dataItem.setMeasureData(measureInfo.getMeasureMap());
|
|
|
return dataItem;
|
|
|
}
|
|
|
|
|
@@ -153,18 +337,18 @@ public class DeviceDataServiceImpl implements DeviceDataService {
|
|
|
* @return com.zcxk.rmcp.core.mongo.DeviceData
|
|
|
* @author linqingwei
|
|
|
**/
|
|
|
- private DeviceData buildDeviceData(Device device, Date date, Map<String, Object> measureMap) {
|
|
|
+ private DeviceData buildDeviceData(Device device, MeasureInfo measureInfo) {
|
|
|
DeviceData deviceData = new DeviceData();
|
|
|
deviceData.setId(idWorker.nextId());
|
|
|
deviceData.setDeviceId(device.getId());
|
|
|
deviceData.setYear(DateUtil.getYear(new Date()));
|
|
|
deviceData.setStatus(1);
|
|
|
- deviceData.setCreateBy("system");
|
|
|
+ deviceData.setCreateBy(Constants.SYS_FLAG);
|
|
|
deviceData.setCreateDate(new Date());
|
|
|
|
|
|
List<DeviceDataItem> list = new ArrayList<>();
|
|
|
|
|
|
- DeviceDataItem dataItem = buildDeviceDataItem(date,measureMap);
|
|
|
+ DeviceDataItem dataItem = buildDeviceDataItem(measureInfo);
|
|
|
list.add(dataItem);
|
|
|
|
|
|
deviceData.setData(list);
|