设备上电恢复

This commit is contained in:
墨大叔 2024-09-06 17:36:41 +08:00
parent 15b7f77fb5
commit edd6232433
6 changed files with 135 additions and 12 deletions

View File

@ -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;
}

View File

@ -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<TransactionBillVO> 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());
}
}
}

View File

@ -49,6 +49,9 @@ public class TransactionBillVO extends TransactionBill {
@JsonView(JsonViewProfile.App.class)
private Boolean isFinished;
@ApiModelProperty("套餐剩余时长(毫秒)/电量(度)")
private BigDecimal suitSurplus;
/**
* 获取套餐时长
*/

View File

@ -141,6 +141,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="query.userName != null "> and su.user_name like concat('%', #{query.userName}, '%')</if>
<if test="query.mchName != null "> and su1.user_name like concat('%', #{query.mchName}, '%')</if>
<if test="query.deviceName != null "> and stb.device_name like concat('%', #{query.deviceName}, '%')</if>
<if test="query.deviceMac != null and query.deviceMac != ''"> and stb.device_mac = #{query.deviceMac}</if>
<if test="query.createTime != null"> and stb.create_time = #{query.createTime}</if>
<if test="query.createDate != null"> and date(stb.create_time) = date(#{query.createDate})</if>
<if test="query.year != null "> and year(stb.create_time) = #{query.year}</if>

View File

@ -16,4 +16,8 @@ public interface TransactionAssembler {
*/
void assembleChannelName(List<TransactionBillVO> list);
/**
* 拼接套餐剩余时长/度数
*/
void assembleSuitSurplus(List<TransactionBillVO> list);
}

View File

@ -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<TransactionBillVO> 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()));
}
}
}
}
}