This commit is contained in:
墨大叔 2024-05-22 15:50:24 +08:00
parent b66a5f3e15
commit 765a795e36
23 changed files with 222 additions and 63 deletions

View File

@ -1,27 +0,0 @@
package com.ruoyi.common.utils;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author wjh
* 2024/4/29
*/
public class CollectionUtils extends org.springframework.util.CollectionUtils {
/**
* 是否为空包括空集合空元素
* @param list
* @return
*/
public static boolean isEmptyElement(List<?> list) {
if (list == null) {
return true;
}
// 过滤空元素
List<?> collect = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
return org.springframework.util.CollectionUtils.isEmpty(collect);
}
}

View File

@ -16,7 +16,7 @@ import static org.apache.commons.lang3.time.DateFormatUtils.format;
/**
* 时间工具类
*
*
* @author ruoyi
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
@ -32,13 +32,13 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
/**
* 获取当前Date型日期
*
*
* @return Date() 当前日期
*/
public static Date getNowDate()
@ -48,7 +48,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/**
* 获取当前日期, 默认格式为yyyy-MM-dd
*
*
* @return String
*/
public static String getDate()
@ -242,4 +242,35 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
public static String getYYYY_MM(Date date) {
return format(date, YYYY_MM);
}
/**
* 获取指定月的最后一天
* @param year
* @param month
*/
public static Date getMonthLastDate(Integer year, Integer month) {
// 创建一个Calendar实例并设置为指定的日期
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1); // 设置为该月的第一天
// 获取该月的最后一天
calendar.add(Calendar.MONTH, 1); // 移动到下个月的第一天
calendar.add(Calendar.DATE, -1); // 回退一天回到上个月的最后一天
// 获取最后一天的日期
return calendar.getTime();
}
/**
* 获取指定月份的第一天
* @param year
* @param month
*/
public static Date getMonthFirstDate(Integer year, Integer month) {
Date firstDay = new Date();
firstDay.setYear(year - 1900);
firstDay.setMonth(month - 1);
firstDay.setDate(1);
return firstDay;
}
}

View File

@ -0,0 +1,93 @@
package com.ruoyi.common.utils.collection;
import com.ruoyi.common.utils.DateUtils;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author wjh
* 2024/4/29
*/
public class CollectionUtils extends org.springframework.util.CollectionUtils {
/**
* 是否为空包括空集合空元素
* @param list
* @return
*/
public static boolean isEmptyElement(List<?> list) {
if (list == null) {
return true;
}
// 过滤空元素
List<?> collect = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
return org.springframework.util.CollectionUtils.isEmpty(collect);
}
/**
* 填充列表中的元素
* @param oldList 旧列表
* @param startDate 起始日期
* @param endDate 结束日期
* @param clazz 填充元素的类
*/
public static List<? super FillAble> fillElement(List<? extends FillAble> oldList, Date startDate, Date endDate,Class<? extends FillAble> clazz ) throws InstantiationException, IllegalAccessException {
List<FillAble> newList = new ArrayList<>();
while (startDate.compareTo(endDate) <= 0) {
Date finalI = startDate;
FillAble fillable = oldList.stream().filter(item -> DateUtils.getYYYY_MM_DD(item.fillDate()).equals(DateUtils.getYYYY_MM_DD(finalI))).findFirst().orElse(null);
if (fillable == null) {
fillable = clazz.newInstance().fillElement(finalI);
}
newList.add(fillable);
startDate = DateUtils.addDays(startDate,1);
}
return newList;
}
// TODO 未经验证
/**
* 补全列表中的日期数据对于缺失的日期使用提供的工厂方法生成对象
*
* @param dataList 原始数据列表元素需有getDateMethod返回Date类型属性的方法
* @param dateGetter 获取元素日期属性的方法引用
* @param customFactory 当日期缺失时用于生成对应元素的对象工厂方法
* @param <T> 数据列表中元素的类型
* @return 完整日期序列下的数据列表缺失的日期已由customFactory填充
*/
public static <T> List<T> completeMissingDates(List<T> dataList,
Function<T, Date> dateGetter,
Function<Date, T> customFactory) {
if (dataList == null || dataList.isEmpty()) {
return Collections.emptyList();
}
List<Date> dates = dataList.stream()
.map(dateGetter)
.sorted()
.collect(Collectors.toList());
Date startDate = dates.get(0);
Date endDate = dates.get(dates.size() - 1);
List<Date> fullDates = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
while (!calendar.getTime().after(endDate)) {
fullDates.add(calendar.getTime());
calendar.add(Calendar.DATE, 1);
}
Map<Date, T> dateToObjectMap = dataList.stream()
.collect(Collectors.toMap(dateGetter, Function.identity()));
return fullDates.stream()
.map(date -> dateToObjectMap.getOrDefault(date, customFactory.apply(date)))
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,17 @@
package com.ruoyi.common.utils.collection;
import java.util.Date;
/**
* 允许填充的元素
* @author wjh
* 2024/5/22
*/
public interface FillAble {
// 获取当前时间
Date fillDate();
// 用于填充的元素
FillAble fillElement(Date currentDate);
}

View File

@ -34,7 +34,9 @@ public class IotReceiveServiceImpl implements IotReceiveService{
if (ReceiveConstants.DS_SSID.equals(msg.getDsId())
&& ReceiveConstants.DSV_SSID_CT.equals((String) msg.getValue())) {
// 当数据点推送ssid的值为ChangteA时录入设备
deviceService.addInitDevice(this.parseToDevice(msg));
SmDevice device = this.parseToDevice(msg);
device.setDeviceName("未命名");
deviceService.addInitDevice(device);
} else {
// 其他情况更新设备信息
deviceService.updateByIot(this.parseToDevice(msg));

View File

@ -1,6 +1,6 @@
package com.ruoyi.ss.channel.service;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.channel.domain.SmChannel;
import com.ruoyi.ss.channel.domain.SmChannelQuery;
import com.ruoyi.ss.channel.mapper.SmChannelMapper;

View File

@ -64,6 +64,7 @@ public class SmDevice extends BaseEntity
/** 设备Mac号 */
@Excel(name = "设备Mac号")
@ApiModelProperty("设备Mac号")
@JsonView(JsonViewProfile.App.class)
private String mac;
/** 激活时间 */

View File

@ -1,6 +1,6 @@
package com.ruoyi.ss.device.service.impl;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.dashboard.BillCountVo;
import com.ruoyi.ss.device.domain.vo.SmDeviceVo;
import com.ruoyi.ss.device.service.DeviceAssembler;

View File

@ -2,7 +2,7 @@ package com.ruoyi.ss.device.service.impl;
import com.ruoyi.common.core.domain.BaseValidator;
import com.ruoyi.common.core.domain.ValidateResult;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ss.device.domain.SmDeviceBO;
import com.ruoyi.ss.device.domain.SmDeviceQuery;

View File

@ -3,6 +3,7 @@ package com.ruoyi.ss.device.service.impl;
import com.ruoyi.common.enums.LoginType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.iot.domain.IotDeviceInfo;
import com.ruoyi.iot.service.IotService;
import com.ruoyi.ss.device.domain.SmDevice;

View File

@ -1,6 +1,8 @@
package com.ruoyi.ss.meterReadingRecord.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.collection.FillAble;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ -12,7 +14,7 @@ import java.util.Date;
* 2024/3/28
*/
@Data
public class SmMeterReadingRecordVo extends SmMeterReadingRecord {
public class SmMeterReadingRecordVo extends SmMeterReadingRecord implements FillAble {
@ApiModelProperty("创建时间")
@JsonFormat(pattern = "yyyy-MM-dd")
@ -30,4 +32,20 @@ public class SmMeterReadingRecordVo extends SmMeterReadingRecord {
@ApiModelProperty("分组内已使用的电量统计")
private BigDecimal groupUsedElectriQuantity;
@Override
public Date fillDate() {
return this.createDate;
}
@Override
public FillAble fillElement(Date currentDate) {
SmMeterReadingRecordVo el = new SmMeterReadingRecordVo();
el.setCreateDate(currentDate);
el.setCreateMonth(DateUtils.getMonth(currentDate));
el.setCreateDay(DateUtils.getDay(currentDate));
el.setGroupUsedElectriQuantity(BigDecimal.ZERO);
el.setTotalElectriQuantity(BigDecimal.ZERO);
el.setUsedElectriQuantity(BigDecimal.ZERO);
return el;
}
}

View File

@ -1,5 +1,6 @@
package com.ruoyi.ss.meterReadingRecord.service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.iot.domain.IotDeviceInfo;
@ -10,12 +11,13 @@ import com.ruoyi.ss.device.mapper.SmDeviceMapper;
import com.ruoyi.ss.meterReadingRecord.domain.SmMeterReadingRecord;
import com.ruoyi.ss.meterReadingRecord.domain.SmMeterReadingRecordQuery;
import com.ruoyi.ss.meterReadingRecord.domain.SmMeterReadingRecordVo;
import com.ruoyi.ss.meterReadingRecord.domain.meterReadingRecord.MeterReadingRecordGroupBy;
import com.ruoyi.ss.meterReadingRecord.mapper.SmMeterReadingRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import java.util.ArrayList;
import java.util.Date;
@ -174,6 +176,21 @@ public class SmMeterReadingRecordServiceImpl implements ISmMeterReadingRecordSer
@Override
public List<SmMeterReadingRecordVo> selectCount(SmMeterReadingRecordQuery dto) {
return smMeterReadingRecordMapper.selectCount(dto);
List<SmMeterReadingRecordVo> list = smMeterReadingRecordMapper.selectCount(dto);
// 补全中间的日期
if (dto.getYear() != null && dto.getMonth() != null && MeterReadingRecordGroupBy.create_date.name().equals(dto.getGroupBy())) {
try {
list = CollectionUtils.fillElement(list,
DateUtils.getMonthFirstDate(dto.getYear(), dto.getMonth()),
DateUtils.getMonthLastDate(dto.getYear(), dto.getMonth()),
SmMeterReadingRecordVo.class)
.stream().map(item -> (SmMeterReadingRecordVo)item).collect(Collectors.toList());
} catch (Exception e) {
throw new ServiceException(e.getMessage());
}
}
return list;
}
}

View File

@ -1,6 +1,6 @@
package com.ruoyi.ss.store.service.impl;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ss.dashboard.BillCountVo;
import com.ruoyi.ss.device.domain.SmDeviceCountVO;

View File

@ -1,6 +1,6 @@
package com.ruoyi.ss.store.service.impl;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;

View File

@ -2,7 +2,7 @@ package com.ruoyi.ss.store.service.impl;
import com.ruoyi.common.core.domain.BaseValidator;
import com.ruoyi.common.core.domain.ValidateResult;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ss.device.domain.SmDeviceQuery;
import com.ruoyi.ss.device.service.ISmDeviceService;

View File

@ -1,6 +1,6 @@
package com.ruoyi.ss.suit.service.impl;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ss.suit.domain.Suit;
import com.ruoyi.ss.suit.domain.SuitQuery;

View File

@ -2,7 +2,7 @@ package com.ruoyi.ss.suit.service.impl;
import com.ruoyi.common.core.domain.BaseValidator;
import com.ruoyi.common.core.domain.ValidateResult;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ss.device.service.DeviceValidator;
import com.ruoyi.ss.suit.domain.SuitBO;

View File

@ -1,6 +1,6 @@
package com.ruoyi.ss.transactionBill.service.impl;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.channel.domain.SmChannel;
import com.ruoyi.ss.channel.domain.SmChannelQuery;
import com.ruoyi.ss.channel.service.ISmChannelService;

View File

@ -5,6 +5,7 @@ import com.ruoyi.common.core.redis.RedisLock;
import com.ruoyi.common.core.redis.enums.RedisLockKey;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.account.domain.SmAccountVo;
import com.ruoyi.ss.account.service.ISmAccountService;
import com.ruoyi.ss.channel.domain.SmChannel;

View File

@ -1,6 +1,6 @@
package com.ruoyi.ss.user.service.impl;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.device.domain.SmDeviceCountVO;
import com.ruoyi.ss.device.domain.SmDeviceQuery;
import com.ruoyi.ss.device.domain.enums.DeviceGroupBy;

View File

@ -1,6 +1,6 @@
package com.ruoyi.task.device;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.device.domain.SmDeviceQuery;
import com.ruoyi.ss.device.domain.enums.DeviceStatus;
import com.ruoyi.ss.device.domain.vo.SmDeviceVo;
@ -11,7 +11,6 @@ import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.List;
/**

View File

@ -178,6 +178,7 @@ public class AppDeviceController extends BaseController {
@ApiOperation("通过SN码获取设备信息")
@GetMapping("/{deviceNo}/bySn")
@JsonView(JsonViewProfile.App.class)
public AjaxResult getByDeviceNo(@PathVariable @ApiParam("设备编号(SN)") String deviceNo) {
return success(smDeviceService.selectByDeviceNo(deviceNo));
}

View File

@ -29,6 +29,7 @@ import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@ -122,22 +123,25 @@ public class AppTransactionBillController extends BaseController
if (dto.getEndDate() == null) {
dto.setEndDate(now);
}
// 如果按日查询查询的月份是当前月份并且当前日期小于7日则查询最近7天的数据否则查询当月第一天开始的数据
// 如果按日查询
if (Objects.equals(dto.getGroupBy(), TransactionBillGroupBy.create_date.name())) {
if (Objects.equals(dto.getYear(), DateUtils.getYear(now))
&& Objects.equals(dto.getMonth(), DateUtils.getMonth(now))
&& DateUtils.getDay(now) < 7) {
// 七天前
dto.setStartDate(DateUtils.addDays(dto.getEndDate(), -6));
dto.setYear(null);
dto.setMonth(null);
} else {
Date firstDay = new Date(dto.getEndDate().getTime());
firstDay.setYear(dto.getYear() - 1900);
firstDay.setMonth(dto.getMonth() - 1);
firstDay.setDate(1);
dto.setStartDate(firstDay);
}
// 查询的月份是当前月份并且当前日期小于7日则查询最近7天的数据否则查询当月第一天开始的数据
// if (Objects.equals(dto.getYear(), DateUtils.getYear(now))
// && Objects.equals(dto.getMonth(), DateUtils.getMonth(now))
// && DateUtils.getDay(now) < 7) {
// // 七天前
// dto.setStartDate(DateUtils.addDays(dto.getEndDate(), -6));
// dto.setYear(null);
// dto.setMonth(null);
// } else {
// dto.setStartDate(DateUtils.getMonthFirstDate(dto.getYear(), dto.getMonth()));
// dto.setEndDate(DateUtils.getMonthLastDate(dto.getYear(), dto.getMonth()));
// }
// 以上是旧的需求防止要改回去
// 以下是新的需求如果按日分组则查询当月第一天到最后一天的数据并补全中间缺少的数据
dto.setStartDate(DateUtils.getMonthFirstDate(dto.getYear(), dto.getMonth()));
dto.setEndDate(DateUtils.getMonthLastDate(dto.getYear(), dto.getMonth()));
}
dto.setMchId(getUserId());
dto.setStatus(TransactionBillStatus.SUCCESS.getStatus());
@ -150,7 +154,8 @@ public class AppTransactionBillController extends BaseController
if (CollectionUtils.isEmpty(totalList)) {
ajax.put("totalRecharge", 0);
} else {
ajax.put("totalRecharge", totalList.get(0).getRecharge());
BigDecimal total = totalList.get(0).getRecharge();
ajax.put("totalRecharge", total == null ? 0 : total);
}
return ajax;