|
@@ -1,5 +1,6 @@
|
|
|
package com.zcxk.rmcp.core.dao.mongo;
|
|
|
|
|
|
+import com.zcxk.core.common.exception.BusinessException;
|
|
|
import com.zcxk.core.common.pojo.PageResult;
|
|
|
import com.zcxk.core.common.pojo.UserCondition;
|
|
|
import com.zcxk.core.mongo.base.BaseDao;
|
|
@@ -11,12 +12,16 @@ import com.zcxk.core.utils.ZoniotIntegerUtils;
|
|
|
import com.zcxk.core.utils.ZoniotStringUtils;
|
|
|
import com.zcxk.rmcp.api.dto.meterreadrecord.MeterReadRecordDto;
|
|
|
import com.zcxk.rmcp.api.dto.meterreadrecord.MeterReadRecordUpdateDto;
|
|
|
+import com.zcxk.rmcp.api.dto.readmeter.MeterData;
|
|
|
import com.zcxk.rmcp.api.enums.ReadStatusEnum;
|
|
|
+import com.zcxk.rmcp.api.enums.RmcpErrorEnum;
|
|
|
import com.zcxk.rmcp.api.vo.MeterReadRecordAggregationVo;
|
|
|
import com.zcxk.rmcp.api.vo.MeterReadRecordVo;
|
|
|
+import com.zcxk.rmcp.core.dao.DeviceMapper;
|
|
|
import com.zcxk.rmcp.core.entity.Device;
|
|
|
import com.zcxk.rmcp.core.mongo.MeterReadRecord;
|
|
|
import io.swagger.models.auth.In;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.data.mongodb.core.aggregation.*;
|
|
@@ -26,6 +31,7 @@ import org.springframework.data.mongodb.core.query.Update;
|
|
|
import org.springframework.stereotype.Repository;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
@@ -43,6 +49,9 @@ import java.util.Map;
|
|
|
@Repository
|
|
|
public class MeterReadRecordDao extends BaseDao<MeterReadRecord, String> implements BaseMongoDao {
|
|
|
|
|
|
+ @Resource
|
|
|
+ private DeviceMapper deviceMapper;
|
|
|
+
|
|
|
/**
|
|
|
* @author Andy
|
|
|
* @description 根据日期和ID获取当前日期的抄表数据
|
|
@@ -66,6 +75,25 @@ public class MeterReadRecordDao extends BaseDao<MeterReadRecord, String> impleme
|
|
|
return dataList.get(0);
|
|
|
}
|
|
|
|
|
|
+ public MeterReadRecordVo findMeterReadRecord(Long id, Criteria childCriteria, String sortKey, Sort.Direction direction){
|
|
|
+ AggregationOptions aggregationOptions = AggregationOptions.builder().allowDiskUse(true).build();
|
|
|
+ Aggregation agg = Aggregation.newAggregation(
|
|
|
+ Aggregation.match(Criteria.where("id").is(id)),
|
|
|
+ Aggregation.unwind("data"),
|
|
|
+ Aggregation.match(childCriteria),
|
|
|
+ Aggregation.sort(Sort.by(direction, sortKey))
|
|
|
+ ).withOptions(aggregationOptions);
|
|
|
+ AggregationResults<MeterReadRecordVo> durationData =
|
|
|
+ mongoTemplate.aggregate(agg, MeterReadRecord.class, MeterReadRecordVo.class);
|
|
|
+ List<MeterReadRecordVo> dataList = durationData.getMappedResults();
|
|
|
+ if (CollectionUtils.isEmpty(dataList)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return dataList.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* @author Andy
|
|
|
* @description 保存抄表记录
|
|
@@ -77,34 +105,43 @@ public class MeterReadRecordDao extends BaseDao<MeterReadRecord, String> impleme
|
|
|
super.save(readRecord);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
+ * 更新读数和哟用水量
|
|
|
* @author Andy
|
|
|
- * @description 更新读数
|
|
|
- * @date 15:27 2021/7/23
|
|
|
- * @param dto
|
|
|
+ * @date 10:35 2021/8/30
|
|
|
+ * @param id:
|
|
|
+ * @param readDate:
|
|
|
+ * @param readData:
|
|
|
+ * @param lastCost:
|
|
|
* @return long
|
|
|
**/
|
|
|
- public long updateReadData(MeterReadRecordUpdateDto dto){
|
|
|
- MeterReadRecordVo meterReadRecordVo = findMeterReadRecord(dto.getId(), dto.getReadDate());
|
|
|
- if (meterReadRecordVo == null) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ public long updateReadData(long id, int readDate, BigDecimal readData, BigDecimal lastCost){
|
|
|
Query query = new Query();
|
|
|
- query.addCriteria(Criteria.where("id").is(dto.getId()));
|
|
|
- query.addCriteria(Criteria.where("data.readDate").is(Integer.parseInt(dto.getReadDate().replaceAll("-",""))));
|
|
|
+ query.addCriteria(Criteria.where("id").is(id));
|
|
|
+ query.addCriteria(Criteria.where("data.readDate").is(readDate));
|
|
|
Update update = new Update();
|
|
|
- update.set("data.$.readData",dto.getReadData());
|
|
|
+ update.set("data.$.readData" , readData.doubleValue());
|
|
|
update.set("data.$.readStatus", ReadStatusEnum.READ.getCode());
|
|
|
update.set("data.$.readTime", System.currentTimeMillis());
|
|
|
- Double lastValid = meterReadRecordVo.getData().getLastValid();
|
|
|
- if (lastValid == null) {
|
|
|
- update.set("data.$.lastCost", new BigDecimal(dto.getReadData()));
|
|
|
- } else {
|
|
|
- update.set("data.$.lastCost", BigDecimalUtils.subtract(meterReadRecordVo.getData().getLastValid(), new BigDecimal(dto.getReadData())));
|
|
|
- }
|
|
|
+ update.set("data.$.lastCost", lastCost.doubleValue());
|
|
|
update.set("data.$.createBy", UserUtil.getCurrentUser().getUsername());
|
|
|
- return upsert(query,update).getModifiedCount();
|
|
|
+ long total = upsert(query,update).getModifiedCount();
|
|
|
+ if (total > 0 ) {
|
|
|
+ Device device = deviceMapper.findById(id);
|
|
|
+ if (device != null) {
|
|
|
+ LocalDateTime localDateTime = device.getLastReceiveTime();
|
|
|
+ int monthLength = (localDateTime.getMonthValue() + "").length();
|
|
|
+ String monthValue = monthLength == 1 ? "0" + localDateTime.getMonthValue() : localDateTime.getMonthValue() + "";
|
|
|
+ int eqDate = Integer.parseInt(localDateTime.getYear() + "" + monthValue + "" + localDateTime.getDayOfMonth());
|
|
|
+ if (readDate >= eqDate) {
|
|
|
+ Device deviceUpdate = new Device();
|
|
|
+ deviceUpdate.setId(id);
|
|
|
+ deviceUpdate.setReadData(readData.toPlainString());
|
|
|
+ deviceMapper.updateByDeviceData(deviceUpdate);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return total;
|
|
|
}
|
|
|
|
|
|
/**
|