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

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

View File

@ -75,13 +75,13 @@ public class UserIrrigationRecord extends BaseEntity
/** 开泵时间 */ /** 开泵时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @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("开泵时间") @ApiModelProperty("开泵时间")
private Date startTime; private Date startTime;
/** 关泵时间 */ /** 关泵时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @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("关泵时间") @ApiModelProperty("关泵时间")
private Date endTime; private Date endTime;
@ -96,8 +96,8 @@ public class UserIrrigationRecord extends BaseEntity
private Integer unit; private Integer unit;
/** 灌溉上报时间 */ /** 灌溉上报时间 */
@JsonFormat(pattern = "yyyy-MM-dd") @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("灌溉上报时间") @ApiModelProperty("灌溉上报时间")
private Date lastTime; private Date lastTime;