From edd6232433157a497d41b66c8e27ba1e34f4146e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E5=A4=A7=E5=8F=94?= <494979559@qq.com> Date: Fri, 6 Sep 2024 17:36:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E4=B8=8A=E7=94=B5=E6=81=A2?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/iot/enums/ReceiveStatus.java | 21 +++++ .../iot/service/IotReceiveServiceImpl.java | 83 ++++++++++++++++--- .../domain/vo/TransactionBillVO.java | 3 + .../mapper/TransactionBillMapper.xml | 1 + .../service/TransactionAssembler.java | 4 + .../impl/TransactionAssemblerImpl.java | 35 ++++++++ 6 files changed, 135 insertions(+), 12 deletions(-) create mode 100644 smart-switch-service/src/main/java/com/ruoyi/iot/enums/ReceiveStatus.java diff --git a/smart-switch-service/src/main/java/com/ruoyi/iot/enums/ReceiveStatus.java b/smart-switch-service/src/main/java/com/ruoyi/iot/enums/ReceiveStatus.java new file mode 100644 index 00000000..3726aa6f --- /dev/null +++ b/smart-switch-service/src/main/java/com/ruoyi/iot/enums/ReceiveStatus.java @@ -0,0 +1,21 @@ +package com.ruoyi.iot.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * 数据推送状态 + * @author wjh + * 2024/9/6 + */ +@Getter +@AllArgsConstructor +public enum ReceiveStatus { + + ONLINE(1, "在线"), + OFFLINE(0, "离线"); + + private final Integer status; + private final String msg; + +} diff --git a/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotReceiveServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotReceiveServiceImpl.java index dd31f5c8..6dcb1f7a 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotReceiveServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotReceiveServiceImpl.java @@ -1,19 +1,30 @@ package com.ruoyi.iot.service; import com.ruoyi.common.utils.NumberUtils; import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.collection.CollectionUtils; import com.ruoyi.iot.constants.ReceiveConstants; import com.ruoyi.iot.domain.IotDeviceDetail; import com.ruoyi.iot.domain.ReceiveMsg; +import com.ruoyi.iot.enums.ReceiveStatus; import com.ruoyi.iot.enums.ReceiveType; import com.ruoyi.ss.device.domain.Device; import com.ruoyi.ss.device.domain.enums.DeviceOutageWay; +import com.ruoyi.ss.device.domain.vo.DeviceVO; import com.ruoyi.ss.device.service.DeviceService; +import com.ruoyi.ss.suit.domain.enums.SuitFeeType; +import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery; +import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillStatus; +import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillType; +import com.ruoyi.ss.transactionBill.domain.vo.TransactionBillVO; +import com.ruoyi.ss.transactionBill.service.TransactionAssembler; +import com.ruoyi.ss.transactionBill.service.TransactionBillService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.math.RoundingMode; +import java.util.List; /** * @author wjh @@ -26,25 +37,73 @@ public class IotReceiveServiceImpl implements IotReceiveService{ @Autowired private DeviceService deviceService; + @Autowired + private TransactionBillService transactionBillService; + + @Autowired + private TransactionAssembler transactionAssembler; + + @Autowired + private IotService iotService; + @Override public void handleReceive(ReceiveMsg msg) { - log.info("handleReceive {}", msg.toString()); + if (msg == null) { + return; + } // 数据点推送 if (ReceiveType.DATA_POINT.getType().equals(msg.getType())) { - if (ReceiveConstants.DS_SSID.equals(msg.getDsId()) - && ReceiveConstants.DSV_SSID_CT.equals((String) msg.getValue())) { - // 当数据点推送ssid的值为ChangteA时,录入设备 - Device device = this.parseToDevice(msg); - device.setDeviceName("未命名"); -// deviceService.addInitDevice(device); - } else { - // 其他情况,更新设备信息 - deviceService.updateByIot(this.parseToDevice(msg)); - } + // 更新设备信息 +// deviceService.updateByIot(this.parseToDevice(msg)); } // 生命周期 else if (ReceiveType.DEVICE_STATUS.getType().equals(msg.getType())) { - deviceService.updateByIot(this.parseToDeviceByLife(msg)); +// deviceService.updateByIot(this.parseToDeviceByLife(msg)); + if (msg.getStatus().equals(ReceiveStatus.ONLINE.getStatus())) { + this.recoverBalance(msg); + } + } + } + + // 恢复设备的余额 + private void recoverBalance(ReceiveMsg msg) { + // 查询设备 + DeviceVO device = deviceService.selectByMac(msg.getDevName()); + if (device == null) { + return; + } + + // 查询未结束的订单 + TransactionBillQuery query = new TransactionBillQuery(); + query.setDeviceId(device.getDeviceId()); + query.setIsFinished(false); + query.setStatus(TransactionBillStatus.SUCCESS.getStatus()); + query.setSuitFeeTypes(SuitFeeType.singleList()); + List billList = transactionBillService.selectSmTransactionBillList(query); + + if (CollectionUtils.isNotEmptyElement(billList)) { + // 拼接剩余时长(秒)/电量(度) + transactionAssembler.assembleSuitSurplus(billList); + + long seconds = 0; // 待恢复的时长(秒) + BigDecimal ele = BigDecimal.ZERO; // 待恢复的电量(度) + + for (TransactionBillVO bill : billList) { + if (bill.getSuitSurplus().compareTo(BigDecimal.ZERO) > 0) { + if (SuitFeeType.rechargeTimeList().contains(bill.getSuitFeeType())) { + seconds += bill.getSuitSurplus().longValue(); + } else if (SuitFeeType.rechargeCountList().contains(bill.getSuitFeeType())) { + ele = ele.add(bill.getSuitSurplus()); + } + } + } + + if (seconds > 0) { + iotService.setTime(device.getMac(), seconds, device.getModelProductId()); + } + if (ele.compareTo(BigDecimal.ZERO) > 0) { + iotService.setEle(device.getMac(), ele, device.getModelProductId()); + } } } diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/domain/vo/TransactionBillVO.java b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/domain/vo/TransactionBillVO.java index 1178a1d8..8a76852c 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/domain/vo/TransactionBillVO.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/domain/vo/TransactionBillVO.java @@ -49,6 +49,9 @@ public class TransactionBillVO extends TransactionBill { @JsonView(JsonViewProfile.App.class) private Boolean isFinished; + @ApiModelProperty("套餐剩余时长(毫秒)/电量(度)") + private BigDecimal suitSurplus; + /** * 获取套餐时长(秒) */ diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/mapper/TransactionBillMapper.xml b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/mapper/TransactionBillMapper.xml index 570a781b..654342e7 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/mapper/TransactionBillMapper.xml +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/mapper/TransactionBillMapper.xml @@ -141,6 +141,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and su.user_name like concat('%', #{query.userName}, '%') and su1.user_name like concat('%', #{query.mchName}, '%') and stb.device_name like concat('%', #{query.deviceName}, '%') + and stb.device_mac = #{query.deviceMac} and stb.create_time = #{query.createTime} and date(stb.create_time) = date(#{query.createDate}) and year(stb.create_time) = #{query.year} diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/TransactionAssembler.java b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/TransactionAssembler.java index 744cc57b..5b14b0bd 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/TransactionAssembler.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/TransactionAssembler.java @@ -16,4 +16,8 @@ public interface TransactionAssembler { */ void assembleChannelName(List list); + /** + * 拼接套餐剩余时长/度数 + */ + void assembleSuitSurplus(List list); } diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionAssemblerImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionAssemblerImpl.java index e25d713b..130e64a3 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionAssemblerImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionAssemblerImpl.java @@ -6,11 +6,16 @@ import com.ruoyi.ss.channel.domain.ChannelQuery; import com.ruoyi.ss.channel.domain.ChannelVO; import com.ruoyi.ss.channel.service.ChannelService; import com.ruoyi.ss.channelWithdraw.service.ChannelWithdrawService; +import com.ruoyi.ss.suit.domain.enums.SuitFeeMode; +import com.ruoyi.ss.suit.domain.enums.SuitFeeType; import com.ruoyi.ss.transactionBill.domain.vo.TransactionBillVO; import com.ruoyi.ss.transactionBill.service.TransactionAssembler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.math.BigDecimal; +import java.time.Duration; +import java.time.LocalDateTime; import java.util.List; import java.util.Map; import java.util.Objects; @@ -47,4 +52,34 @@ public class TransactionAssemblerImpl implements TransactionAssembler { } } } + + @Override + public void assembleSuitSurplus(List list) { + if (CollectionUtils.isEmptyElement(list)) { + return; + } + + for (TransactionBillVO bill : list) { + // 充值时间 + if (SuitFeeType.rechargeTimeList().contains(bill.getSuitFeeType())) { + LocalDateTime endTime = bill.getSuitEndTime() == null ? bill.getSuitExpireTime() : bill.getSuitEndTime(); + if (endTime == null) { + bill.setSuitSurplus(BigDecimal.ZERO); + } else { + LocalDateTime now = LocalDateTime.now(); + Duration between = Duration.between(now, endTime); + bill.setSuitSurplus(BigDecimal.valueOf(between.getSeconds())); + } + } + // 充值时长 + else if (SuitFeeType.rechargeCountList().contains(bill.getSuitFeeType())) { + BigDecimal endEle = bill.getSuitEndEle() == null ? bill.getDeviceTotalEle() : bill.getSuitEndEle(); + if (endEle == null) { + bill.setSuitSurplus(BigDecimal.ZERO); + } else { + bill.setSuitSurplus(endEle.subtract(bill.getDeviceTotalEle())); + } + } + } + } }