Merge remote-tracking branch 'origin/master'

This commit is contained in:
SjS 2025-04-29 18:20:34 +08:00
commit fcd24047c0
19 changed files with 186 additions and 68 deletions

View File

@ -1,5 +1,12 @@
package com.ruoyi.common.pay.tm;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.exception.ServiceException;
@ -14,12 +21,6 @@ import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.http.HttpUtils;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
/**
* 太米支付
@ -73,7 +74,7 @@ public class TmPayService implements PayApi {
ServiceUtil.assertion(refundInfo.getInteger("refundStatus") != 1, "退款失败:%s", refundInfo.getString("refundMessage"));
return refundInfo.toJavaObject(RefundInfo.class);
} else {
throw new ServiceException("退款失败: " + jsonResponse.getString("errMsg"));
throw new ServiceException("退款" + refundAble.refundAmountYuan() +"元,退款失败: " + jsonResponse.getString("errMsg"));
}
}
throw new ServiceException("退款数据为空");

View File

@ -1,6 +1,7 @@
package com.ruoyi.common.utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* @author wjh
@ -160,7 +161,7 @@ public class MathUtils {
if (a == null || b == null) {
return false;
}
return equals(a.setScale(precision, BigDecimal.ROUND_HALF_UP), b.setScale(precision, BigDecimal.ROUND_HALF_UP));
return equals(a.setScale(precision, RoundingMode.HALF_UP), b.setScale(precision, RoundingMode.HALF_UP));
}
// 获取最大值

View File

@ -1,8 +1,11 @@
package com.ruoyi.bst.device.domain;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.format.annotation.DateTimeFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ -14,7 +17,7 @@ public class DeviceQuery extends DeviceVO {
@ApiModelProperty("是否刷新物联网信息")
private Boolean refresh;
@ApiModelProperty("设备ID列表")
private List<Long> ids;
@ -47,4 +50,13 @@ public class DeviceQuery extends DeviceVO {
@ApiModelProperty("关键词")
private String keyword;
@ApiModelProperty("最后上报时间(结束)")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime lastTimeEnd;
@ApiModelProperty("最后上报时间(起始)")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime lastTimeStart;
}

View File

@ -160,4 +160,12 @@ public interface DeviceMapper
*/
List<DeviceMacSnVO> selectMacSnList(@Param("query") DeviceQuery query);
/**
* 条件更新简单版
* @param data
* @param query
* @return
*/
int updateByQuerySimple(@Param("data") Device data, @Param("query") DeviceQuery query);
}

View File

@ -127,6 +127,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="query.lastUserPhone != null and query.lastUserPhone != ''">and bul.user_name like concat('%', #{query.lastUserPhone}, '%')</if>
<if test="query.mchName != null and query.mchName != ''">and mch.nick_name like concat('%', #{query.mchName}, '%')</if>
<if test="query.modelName != null and query.modelName != ''">and bm.name like concat('%', #{query.modelName}, '%')</if>
<if test="query.lastTimeEnd != null">and bd.last_location_time &lt;= #{query.lastTimeEnd}</if>
<if test="query.lastTimeStart != null">and bd.last_location_time &gt;= #{query.lastTimeStart}</if>
<if test="query.keyword != null and query.keyword != ''">
and (
bd.sn like concat('%', #{query.keyword}, '%')
@ -474,4 +476,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</where>
</select>
<!-- updateByQuerySimple -->
<update id="updateByQuerySimple">
update bst_device bd
<trim prefix="SET" suffixOverrides=",">
<include refid="updateColumns"/>
</trim>
<where>
<include refid="searchCondition"/>
</where>
</update>
</mapper>

View File

@ -3,6 +3,7 @@ package com.ruoyi.bst.device.service;
import java.util.Collections;
import java.util.List;
import com.ruoyi.bst.device.domain.Device;
import com.ruoyi.bst.device.domain.DeviceVO;
import com.ruoyi.bst.device.domain.enums.DeviceUnLockType;
import com.ruoyi.bst.device.domain.vo.DeviceIotVO;
@ -138,4 +139,11 @@ public interface DeviceIotService {
*/
DeviceIotVO setMusic(DeviceVO device, String music, String reason, boolean requiredIot);
/**
* 更新物联网信息
* @param device
* @return
*/
int updateIot(Device device);
}

View File

@ -202,5 +202,13 @@ public interface DeviceService
*/
public List<DeviceMacSnVO> listSnByMac(List<String> macList);
/**
* 条件更新
* @param data
* @param query
* @return
*/
public int updateByQuery(Device data, DeviceQuery query);
}

View File

@ -26,7 +26,6 @@ import com.ruoyi.bst.device.utils.DeviceUtil;
import com.ruoyi.bst.orderDevice.domain.enums.OrderDeviceStatus;
import com.ruoyi.common.constants.ServiceCode;
import com.ruoyi.common.core.redis.RedisLock;
import com.ruoyi.common.core.redis.enums.RedisLockKey;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.StringUtils;
@ -275,25 +274,15 @@ public class DeviceIotServiceImpl implements DeviceIotService {
DeviceUtil.setIotInfo(device, iot);
}
// 异步更新数据库
// 异步发送命令强制设备上报数据
scheduledExecutorService.schedule(() -> {
for (DeviceVO device : deviceList) {
try {
// 加锁控制更新频率
boolean lock = redisLock.lock(RedisLockKey.DEVICE_IOT_REFRESH, device.getMac(), 10);
if (!lock) {
continue;
if (onlineType != null) {
iotService.getOnlineStatus(device, onlineType);
}
// 获取在线状态
if (StringUtils.hasText(onlineType)) {
String onlineStatus = iotService.getOnlineStatus(device, onlineType);
this.setOnlineStatus(device, onlineStatus);
}
// 更新物联网信息
this.updateIot(device);
} catch (Exception e) {
log.error("更新设备{}物联网信息失败", device.getSn(), e);
log.error("强制设备{}上报数据失败", device.getSn(), e);
}
}
}, 0, TimeUnit.SECONDS);
@ -308,11 +297,13 @@ public class DeviceIotServiceImpl implements DeviceIotService {
}
public int updateIot(Device device) {
if (device == null || device.getId() == null) {
if (device == null) {
return 0;
}
if (StringUtils.isBlank(device.getMac()) && device.getId() == null) {
return 0;
}
Device data = new Device();
data.setId(device.getId());
data.setVoltage(device.getVoltage());
data.setSignalStrength(device.getSignalStrength());
data.setQuality(device.getQuality());
@ -326,7 +317,10 @@ public class DeviceIotServiceImpl implements DeviceIotService {
data.setOnlineStatus(device.getOnlineStatus());
data.setLastOnlineTime(device.getLastOnlineTime());
data.setSoftwareVersion(device.getSoftwareVersion());
return deviceMapper.updateDevice(data);
DeviceQuery query = new DeviceQuery();
query.setId(device.getId());
query.setEqMac(device.getMac());
return deviceMapper.updateByQuerySimple(data, query);
}
@Override

View File

@ -580,6 +580,12 @@ public class DeviceServiceImpl implements DeviceService
DeviceQuery query = new DeviceQuery();
query.setMacList(macList);
return deviceMapper.selectMacSnList(query);
}
@Override
public int updateByQuery(Device data, DeviceQuery query) {
return deviceMapper.updateByQuery(data, query);
}
}

View File

@ -37,7 +37,8 @@ public class DeviceUtil {
}
}
}
device.setLastTime(at);
device.setVoltage(sys.getBat());
device.setSignalStrength(sys.getCsq());
device.setQuality(sys.getQ());
@ -61,7 +62,6 @@ public class DeviceUtil {
setIotSysInfo(device, sys, iot.getAt());
}
device.setSoftwareVersion(iot.getVer());
device.setLastTime(iot.getAt());
}
/**

View File

@ -4,7 +4,6 @@ import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ -68,7 +67,6 @@ import com.ruoyi.bst.pay.service.PayConverter;
import com.ruoyi.bst.pay.service.PayService;
import com.ruoyi.bst.refund.domain.enums.RefundType;
import com.ruoyi.bst.sms.service.SmsService;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.core.redis.RedisLock;
import com.ruoyi.common.core.redis.enums.RedisLockKey;
import com.ruoyi.common.utils.DateUtils;
@ -251,12 +249,11 @@ public class OrderServiceImpl implements OrderService
// 校验参数
orderValidator.validate(bo);
// 若设备当前订单未支付且是当前用户的订单则将其取消
// 若设备当前订单未支付则将其取消
DeviceVO device = bo.getDevice();
UserVO user = bo.getUser();
if (device.getOrderId() != null && OrderStatus.WAIT_PAY.getCode().equals(device.getOrderStatus()) && Objects.equals(device.getOrderUserId(), user.getUserId())) {
int cancel = this.cancelOrder(device.getOrderId(), "取消用户重复订单");
ServiceUtil.assertion(cancel != 1, "取消用户重复订单失败");
if (device.getOrderId() != null && OrderStatus.WAIT_PAY.getCode().equals(device.getOrderStatus())) {
int cancel = this.cancelOrder(device.getOrderId(), "取消上一个未支付订单");
ServiceUtil.assertion(cancel != 1, "取消上一个未支付订单失败");
}
return transactionTemplate.execute(status -> {

View File

@ -2,6 +2,7 @@ package com.ruoyi.bst.refund.service.impl;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -160,7 +161,9 @@ public class RefundServiceImpl implements RefundService
// 判断是否同步通知若是则直接处理支付成功
if (channelApiType.getIsRefundSync() != null && channelApiType.getIsRefundSync()) {
this.handleRefundSuccess(refund.getNo());
scheduledExecutorService.schedule(() -> {
this.handleRefundSuccess(refund.getNo());
}, 10, TimeUnit.SECONDS);
}
return insertRefund;

View File

@ -1,12 +1,12 @@
package com.ruoyi.iot.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author wjh
* 2024/7/11

View File

@ -65,6 +65,7 @@ public class ReceiveController {
ObjBody obj = IotUtil.resolveBody(body, false);
if (obj != null){
Object msg = obj.getMsg();
log.info("收到receive数据{}", msg);
// 接收到msg
if (msg instanceof String) {
iotReceiveService.handleReceive(JSON.parseObject((String) msg, ReceiveMsg.class));

View File

@ -1,6 +1,7 @@
package com.ruoyi.iot.service.impl;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
@ -83,37 +84,33 @@ public class IotReceiveServiceImpl implements IotReceiveService {
if (device == null) {
return;
}
BigDecimal oldLon = device.getLongitude();
BigDecimal oldLat = device.getLatitude();
// 转为设备信息
log.info("收到sys数据" + msg.getValue());
log.info("收到sys数据{}", msg.getValue());
IotDeviceSysInfo sys = IotUtil.toSysInfo(msg.getValue());
LocalDateTime at = DateUtils.toLocalDateTime(msg.getAt());
// 0,0定位不处理
if (MathUtils.equals(sys.getLon(), BigDecimal.ZERO) && MathUtils.equals(sys.getLat(), BigDecimal.ZERO)) {
log.info("设备{}定位异常:{},{},不处理", msg.getDevName(), sys.getLon(), sys.getLat());
return;
}
DeviceUtil.setIotSysInfo(device, sys, at);
// 处理设备定位BUG若出现BUG则重启设备
int handle = this.handleDeviceLocationBug(device, sys, at);
int handle = this.handleDeviceLocationBug(device, at, oldLon, oldLat);
if (handle == 1) {
return;
}
// 设置设备信息
DeviceUtil.setIotSysInfo(device, sys, at);
device.setLastTime(at);
device.setLastLocationTime(at);
// 卫星数量小于5不处理设备操作
if (device.getSatellites() == null || device.getSatellites() < 5) {
log.error("卫星数量小于5不处理: {}", device.getMac());
return;
// 更新设备信息
device.setOnlineStatus(DeviceOnlineStatus.ONLINE.getStatus());
device.setLastOnlineTime(at);
int update = deviceIotService.updateIot(device);
if (update != 1) {
log.error("更新设备信息失败: {}", msg.getDevName());
}
// 处理运营区
this.handleArea(device);
// 处理定位日志
this.handleLocationLog(device);
}
}
@ -138,7 +135,7 @@ public class IotReceiveServiceImpl implements IotReceiveService {
* 处理设备定位BUG
* @param device
*/
private int handleDeviceLocationBug(DeviceVO device, IotDeviceSysInfo sys, LocalDateTime at) {
private int handleDeviceLocationBug(DeviceVO device, LocalDateTime at, BigDecimal oldLon, BigDecimal oldLat) {
if (device == null || StringUtils.isBlank(device.getMac()) || device.getLastLocationTime() == null || at == null) {
return 0;
}
@ -147,13 +144,23 @@ public class IotReceiveServiceImpl implements IotReceiveService {
if (duration.getSeconds() > 60) {
return 0;
}
boolean isOpen = DeviceQuality.OPEN.getCode().equals(sys.getQ()); // 电门是否开启
// 0,0定位不处理
if (MathUtils.equals(device.getLongitude(), BigDecimal.ZERO) && MathUtils.equals(device.getLatitude(), BigDecimal.ZERO)) {
log.info("设备{}定位异常:{},{},不处理", device.getMac(), device.getLongitude(), device.getLatitude());
return 0;
}
boolean isOpen = DeviceQuality.OPEN.getCode().equals(device.getQuality()); // 电门是否开启
// 需要在设备未启动的时候做否则可能有安全问题
if (!isOpen) {
// 若当前数据点的上报时间和设备内上次上报的时间不一样但是定位一样则重启设备
log.info("当前定位:{},{},上次定位:{},{}", sys.getLon().setScale(8), sys.getLat().setScale(8), device.getLongitude().setScale(8), device.getLatitude().setScale(8));
if (at.isAfter(device.getLastLocationTime()) && MathUtils.equalsFixed(device.getLongitude(), sys.getLon(), 8) && MathUtils.equalsFixed(device.getLatitude(), sys.getLat(), 8)) {
String reason = String.format("设备不同时间的两次定位一样,重启设备。新定位:%s,%s旧定位%s,%s", sys.getLon(), sys.getLat(), device.getLongitude(), device.getLatitude());
log.info("当前定位:{},{},上次定位:{},{}",
device.getLongitude().setScale(8, RoundingMode.HALF_UP),
device.getLatitude().setScale(8, RoundingMode.HALF_UP),
oldLon.setScale(8, RoundingMode.HALF_UP),
oldLat.setScale(8, RoundingMode.HALF_UP)
);
if (at.isAfter(device.getLastLocationTime()) && MathUtils.equalsFixed(device.getLongitude(), oldLon, 8) && MathUtils.equalsFixed(device.getLatitude(), oldLat, 8)) {
String reason = String.format("设备不同时间的两次定位一样,重启设备。新定位:%s,%s旧定位%s,%s", device.getLongitude(), device.getLatitude(), oldLon, oldLat);
deviceIotService.reboot(device, reason, true);
return 1;
}
@ -174,6 +181,16 @@ public class IotReceiveServiceImpl implements IotReceiveService {
if (duration.getSeconds() > 60) {
return;
}
// 卫星数量小于5不处理设备操作
if (device.getSatellites() == null || device.getSatellites() < 5) {
log.error("卫星数量小于5不处理: {}", device.getMac());
return;
}
// 0,0定位不处理
if (MathUtils.equals(device.getLongitude(), BigDecimal.ZERO) && MathUtils.equals(device.getLatitude(), BigDecimal.ZERO)) {
log.info("设备{}定位异常:{},{},不处理", device.getMac(), device.getLongitude(), device.getLatitude());
return;
}
// 若处理过则不处理
String key = CacheConstants.DEVICE_AREA_HANDLE_KEY + device.getMac();
Integer isHandle = redisCache.getCacheObject(key);
@ -275,6 +292,17 @@ public class IotReceiveServiceImpl implements IotReceiveService {
// 定位日志处理
private void handleLocationLog(DeviceVO device) {
// 卫星数量小于5不处理设备操作
if (device.getSatellites() == null || device.getSatellites() < 5) {
log.error("卫星数量小于5不处理: {}", device.getMac());
return;
}
// 0,0定位不处理
if (MathUtils.equals(device.getLongitude(), BigDecimal.ZERO) && MathUtils.equals(device.getLatitude(), BigDecimal.ZERO)) {
log.info("设备{}定位异常:{},{},不处理", device.getMac(), device.getLongitude(), device.getLatitude());
return;
}
// 转换定位日志
LocationLog po = locationLogConverter.toPo(device);
if (po == null) {

View File

@ -280,9 +280,9 @@ public class IotServiceImpl implements IotService {
// 记录成功日志
this.addCommandLog(device, command, res.getMsg(), reason, res.getCode(), result);
// 转为在线状态并缓存结果30秒
// 转为在线状态并缓存结果10秒
String status = this.parseToOnlineStatus(res, deviceName, true);
redisCache.setCacheObject(this.getOnlineCacheKey(deviceName), status, 30, TimeUnit.SECONDS);
redisCache.setCacheObject(this.getOnlineCacheKey(deviceName), status, 10, TimeUnit.SECONDS);
// 异步更新设备在线状态
scheduledExecutorService.schedule(() -> {
int update = deviceService.updateOnlineStatusByMac(deviceName, status);

View File

@ -31,6 +31,7 @@ import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.iot.domain.IotDeviceSysInfo;
import com.ruoyi.iot.domain.ObjBody;
import com.ruoyi.iot.domain.response.CommandResponse;
import com.ruoyi.iot.enums.IotHttpStatus;
import lombok.extern.slf4j.Slf4j;
@ -214,7 +215,8 @@ public class IotUtil {
if (result == null) {
return "未知错误";
}
return result.getMsg();
IotHttpStatus status = IotHttpStatus.convertByCode(result.getCode());
return status.getMsg();
}
public static IotDeviceSysInfo toSysInfo(Object value) {

View File

@ -1,14 +1,19 @@
package com.ruoyi.task.device;
import java.time.LocalDateTime;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.bst.device.domain.Device;
import com.ruoyi.bst.device.domain.DeviceQuery;
import com.ruoyi.bst.device.service.DeviceIotService;
import com.ruoyi.bst.device.service.DeviceService;
import com.ruoyi.iot.service.impl.DeviceOnlineStatus;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
@Component
@Slf4j
public class DeviceTask {
@ -16,9 +21,27 @@ public class DeviceTask {
@Autowired
private DeviceIotService deviceIotService;
@Autowired
private DeviceService deviceService;
// 监控所有设备
public void monitorAll() {
deviceIotService.monitor(new ArrayList<>());
}
/**
* 将过久未上报数据的设备设置为离线
*/
public void monitorOffline(Integer seconds) {
// 条件在线且最后上报时间小于当前时间 - seconds秒
DeviceQuery query = new DeviceQuery();
query.setOnlineStatus(DeviceOnlineStatus.ONLINE.getStatus());
query.setLastTimeEnd(LocalDateTime.now().plusSeconds(-seconds));
Device data = new Device();
data.setOnlineStatus(DeviceOnlineStatus.OFFLINE.getStatus());
int update = deviceService.updateByQuery(data, query);
log.info("更新了{}条离线的设备数据", update);
}
}

View File

@ -0,0 +1,12 @@
package com.ruoyi.web.app;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
@RestController
@RequestMapping("/app/locationLog")
public class AppLocationLogController extends BaseController {
}