用户年用水量/剩余用水量计算逻辑优化

This commit is contained in:
蒾酒 2025-01-01 19:44:03 +08:00
parent 84ed8dc31f
commit 40cf501ddf
2 changed files with 17 additions and 12 deletions

View File

@ -1,5 +1,6 @@
package com.fastbee.data.controller.userRecharge;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
@ -195,25 +196,29 @@ public class UserIrrigationRecordController extends BaseController
return toAjax(userIrrigationRecordService.deleteUserIrrigationRecordByIds(ids));
}
/**
* 统计用户的年度用水量年度剩余用水量
*/
@GetMapping("/getYearWater")
@ApiOperation("统计用户的年度用水量,年度剩余用水量")
public AjaxResult getYearWater(@Param("userId") Long userId) {
// 获取当前年份的第一天
LocalDate startOfYear = LocalDate.now().withDayOfYear(1);
// 统计从今年第一天开始以后的用水量
Long count = new LambdaQueryChainWrapper<>(userIrrigationRecordMapper)
// 获取从今年第一天开始以后的所有用水量记录
List<UserIrrigationRecord> records = new LambdaQueryChainWrapper<>(userIrrigationRecordMapper)
.select(UserIrrigationRecord::getCurFlow)
.eq(UserIrrigationRecord::getUserId, userId)
.ge(UserIrrigationRecord::getCreateTime, startOfYear)
.count();
.list();
// 手动累加CurFlow字段的值CurFlow字段是BigDecimal类型
BigDecimal totalWaterUsed = records.stream()
.map(UserIrrigationRecord::getCurFlow) // 将记录转换为BigDecimal流
.reduce(BigDecimal.ZERO, BigDecimal::add); // 使用reduce累加BigDecimal值
Map<String,Object> resp = new HashMap<>();
resp.put("yearUserWater", count);
resp.put("yearUserWaterLeft", 1000 - count); // 假设年度总用水量为1000单位
resp.put("yearUserWater", totalWaterUsed);
resp.put("yearUserWaterLeft", BigDecimal.valueOf(1000).subtract(totalWaterUsed)); // 假设年度总用水量为1000单位
return AjaxResult.success(resp);
}

View File

@ -75,13 +75,13 @@ public class UserIrrigationRecord extends BaseEntity
/** 开泵时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Excel(name = "开泵时间", width = 30, dateFormat = "yyyy-MM-dd")
@Excel(name = "开泵时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty("开泵时间")
private Date startTime;
/** 关泵时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Excel(name = "关泵时间", width = 30, dateFormat = "yyyy-MM-dd")
@Excel(name = "关泵时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty("关泵时间")
private Date endTime;
@ -96,8 +96,8 @@ public class UserIrrigationRecord extends BaseEntity
private Integer unit;
/** 灌溉上报时间 */
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "灌溉上报时间", width = 30, dateFormat = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Excel(name = "灌溉上报时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty("灌溉上报时间")
private Date lastTime;