日志更新

This commit is contained in:
磷叶 2025-04-22 16:42:22 +08:00
parent 248a6c0a7d
commit 76dc3c6612
9 changed files with 93 additions and 37 deletions

View File

@ -68,4 +68,12 @@ public class CommandLog extends BaseEntity
@Excel(name = "纬度")
@ApiModelProperty("纬度")
private BigDecimal latitude;
@Excel(name = "订单ID")
@ApiModelProperty("订单ID")
private Long orderId;
@Excel(name = "原始响应")
@ApiModelProperty("原始响应")
private String originResponse;
}

View File

@ -19,7 +19,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
bcl.opera_time,
bcl.iot_code,
bcl.longitude,
bcl.latitude
bcl.latitude,
bcl.order_id,
bcl.origin_response
from bst_command_log bcl
</sql>
@ -34,6 +36,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="query.userId != null "> and bcl.user_id = #{query.userId}</if>
<if test="query.userName != null and query.userName != ''"> and bcl.user_name like concat('%', #{query.userName}, '%')</if>
<if test="query.iotCode != null "> and bcl.iot_code = #{query.iotCode}</if>
<if test="query.orderId != null "> and bcl.order_id = #{query.orderId}</if>
${query.params.dataScope}
</sql>
@ -62,7 +65,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
opera_time,
iot_code,
longitude,
latitude
latitude,
order_id,
origin_response
</trim>
values
<foreach collection="list" item="i" separator=",">
@ -89,6 +94,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="i.longitude == null ">default,</if>
<if test="i.latitude != null ">#{i.latitude},</if>
<if test="i.latitude == null ">default,</if>
<if test="i.orderId != null ">#{i.orderId},</if>
<if test="i.orderId == null ">default,</if>
<if test="i.originResponse != null ">#{i.originResponse},</if>
<if test="i.originResponse == null ">default,</if>
</trim>
</foreach>
</insert>
@ -107,6 +116,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="iotCode != null">iot_code,</if>
<if test="longitude != null">longitude,</if>
<if test="latitude != null">latitude,</if>
<if test="orderId != null">order_id,</if>
<if test="originResponse != null">origin_response,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="type != null and type != ''">#{type},</if>
@ -120,6 +131,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="iotCode != null">#{iotCode},</if>
<if test="longitude != null">#{longitude},</if>
<if test="latitude != null">#{latitude},</if>
<if test="orderId != null">#{orderId},</if>
<if test="originResponse != null">#{originResponse},</if>
</trim>
</insert>
@ -143,6 +156,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="data.iotCode != null">iot_code = #{data.iotCode},</if>
<if test="data.longitude != null">longitude = #{data.longitude},</if>
<if test="data.latitude != null">latitude = #{data.latitude},</if>
<if test="data.orderId != null">order_id = #{data.orderId},</if>
<if test="data.originResponse != null">origin_response = #{data.originResponse},</if>
</sql>
<delete id="deleteCommandLogById" parameterType="Long">

View File

@ -7,7 +7,6 @@ import com.ruoyi.bst.commandLog.domain.CommandLog;
import com.ruoyi.bst.commandLog.domain.CommandLogQuery;
import com.ruoyi.bst.commandLog.domain.CommandLogVO;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.iot.interfaces.IotDevice;
/**
* 命令日志Service接口
@ -65,18 +64,6 @@ public interface CommandLogService
*/
public int deleteCommandLogById(Long id);
/**
* 插入一条API日志
*
* @param mac MAC
* @param command 命令
* @param result 结果
* @param reason 原因
* @param loginUser
* @param iotCode
*/
int addApiLog(IotDevice device, String command, String result, String reason, LoginUser loginUser, Integer iotCode);
/**
* 插入一条蓝牙命令日志
*/
@ -91,4 +78,9 @@ public interface CommandLogService
* 清除指定日期之前的日志
*/
int clearBeforeDateLater(LocalDate date);
/**
* 新增命令日志
*/
int addLog(CommandLog po, LoginUser loginUser);
}

View File

@ -13,12 +13,12 @@ import com.ruoyi.bst.commandLog.domain.CommandLogVO;
import com.ruoyi.bst.commandLog.domain.enums.CommandLogType;
import com.ruoyi.bst.commandLog.mapper.CommandLogMapper;
import com.ruoyi.bst.commandLog.service.CommandLogService;
import com.ruoyi.bst.device.service.DeviceService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.iot.interfaces.IotDevice;
/**
* 命令日志Service业务层处理
@ -35,6 +35,9 @@ public class CommandLogServiceImpl implements CommandLogService
@Autowired
private RedisCache redisCache;
@Autowired
private DeviceService deviceService;
/**
* 查询命令日志
*
@ -107,21 +110,6 @@ public class CommandLogServiceImpl implements CommandLogService
return commandLogMapper.deleteCommandLogById(id);
}
@Override
public int addApiLog(IotDevice device, String command, String result, String reason, LoginUser loginUser, Integer iotCode) {
CommandLog po = new CommandLog();
po.setType(CommandLogType.API.getType());
po.setMac(device.mac());
po.setCommand(command);
po.setResult(result);
po.setReason(reason);
po.setIotCode(iotCode);
po.setLongitude(device.longitude());
po.setLatitude(device.latitude());
return this.addLog(po, loginUser);
}
@Override
public int addBluetoothLog(CommandLog po, LoginUser loginUser) {
po.setType(CommandLogType.BLUETOOTH.getType());
@ -144,18 +132,26 @@ public class CommandLogServiceImpl implements CommandLogService
return commandLogMapper.clearBeforeDateLater(date);
}
private int addLog(CommandLog po, LoginUser loginUser) {
@Override
public int addLog(CommandLog po, LoginUser loginUser) {
if (po == null || StringUtils.isBlank(po.getMac())) {
return 0;
}
po.setOperaTime(LocalDateTime.now());
// 设置操作人
if (loginUser != null) {
po.setUserId(loginUser.getUserId());
po.setUserName(loginUser.getNickName());
po.setUserId(loginUser.getUserId());
}
// 查询订单ID
if (po.getMac() != null) {
Long orderId = deviceService.selectOrderIdByMac(po.getMac());
po.setOrderId(orderId);
}
// 添加到缓存中
redisCache.rightPush(CacheConstants.INSERT_COMMAND_LOG, po);

View File

@ -144,4 +144,11 @@ public interface DeviceMapper
* @return
*/
int unbindArea(@Param("ids") List<Long> ids);
/**
* 根据mac查询订单ID
* @param mac
* @return
*/
Long selectOrderIdByMac(@Param("mac") String mac);
}

View File

@ -431,4 +431,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{id}
</foreach>
</update>
<!-- selectOrderIdByMac -->
<select id="selectOrderIdByMac">
select bod.order_id
from bst_device bd
left join bst_order_device bod on bod.id = bd.order_device_id
where bd.mac = #{mac}
</select>
</mapper>

View File

@ -187,5 +187,12 @@ public interface DeviceService
*/
public int updateLastUserId(Long id, Long lastUserId);
/**
* 根据mac查询订单ID
* @param mac
* @return
*/
public Long selectOrderIdByMac(String mac);
}

View File

@ -562,4 +562,12 @@ public class DeviceServiceImpl implements DeviceService
data.setLastUserId(lastUserId);
return deviceMapper.updateDevice(data);
}
@Override
public Long selectOrderIdByMac(String mac) {
if (StringUtils.isBlank(mac)) {
return null;
}
return deviceMapper.selectOrderIdByMac(mac);
}
}

View File

@ -14,6 +14,8 @@ import org.springframework.stereotype.Service;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.bst.commandLog.domain.CommandLog;
import com.ruoyi.bst.commandLog.domain.enums.CommandLogType;
import com.ruoyi.bst.commandLog.service.CommandLogService;
import com.ruoyi.bst.device.service.DeviceService;
import com.ruoyi.common.constant.CacheConstants;
@ -268,14 +270,15 @@ public class IotServiceImpl implements IotService {
String param = "device_name=" + deviceName + "&product_id=" + productId +"&timeout=" + timeout;
sendUrl = sendUrl + "?" + param;
String token = TokenUtil.getToken();
String result = null;
try {
// 发送命令
String result = HttpUtils.sendPostWithToken(sendUrl, command, token);
result = HttpUtils.sendPostWithToken(sendUrl, command, token);
ServiceUtil.assertion(!StringUtils.hasText(result), "与OneNet通信异常");
CommandResponse res = JSON.parseObject(result, CommandResponse.class);
// 记录成功日志
this.addCommandLog(device, command, res.getMsg(), reason, res.getCode());
this.addCommandLog(device, command, res.getMsg(), reason, res.getCode(), result);
// 转为在线状态并缓存结果30秒
String status = this.parseToOnlineStatus(res, deviceName, true);
@ -290,20 +293,30 @@ public class IotServiceImpl implements IotService {
return res;
} catch (Exception e) {
this.addCommandLog(device, command, "操作失败:" + e.getMessage(), reason, null);
this.addCommandLog(device, command, "操作失败:" + e.getMessage(), reason, null, result);
throw e;
}
}
// 新增命令日志
private void addCommandLog(IotDevice device, String command, String result, String reason, Integer iotCode) {
private void addCommandLog(IotDevice device, String command, String result, String reason, Integer iotCode, String originResponse) {
LoginUser loginUser = null;
try {
loginUser = SecurityUtils.getLoginUser();
} catch (Exception e) {
log.error(e.getMessage());
}
commandLogService.addApiLog(device, command, result, reason, loginUser, iotCode);
CommandLog po = new CommandLog();
po.setType(CommandLogType.API.getType());
po.setMac(device.mac());
po.setCommand(command);
po.setResult(result);
po.setReason(reason);
po.setIotCode(iotCode);
po.setLongitude(device.longitude());
po.setLatitude(device.latitude());
po.setOriginResponse(originResponse);
commandLogService.addLog(po, loginUser);
}
private CommandResponse uploadData(IotDevice device, String reason) {