feat(mqtt): 新增MQTT服务配置功能

添加MQTT服务配置相关功能,包括实体类、DTO、Mapper、Service、Controller等。支持MQTT服务的增删改查操作,并集成到智能柜系统中,以便管理MQTT服务器的连接配置。
This commit is contained in:
dzq 2025-05-08 10:51:01 +08:00
parent fe5a19157a
commit 42df4996e4
18 changed files with 485 additions and 1 deletions

View File

@ -0,0 +1,68 @@
package com.agileboot.admin.controller.cabinet;
import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.domain.cabinet.mqtt.MqttServerApplicationService;
import com.agileboot.domain.cabinet.mqtt.command.AddMqttServerCommand;
import com.agileboot.domain.cabinet.mqtt.command.UpdateMqttServerCommand;
import com.agileboot.domain.cabinet.mqtt.db.MqttServerEntity;
import com.agileboot.domain.cabinet.mqtt.dto.MqttServerDTO;
import com.agileboot.domain.cabinet.mqtt.query.SearchMqttServerQuery;
import com.agileboot.domain.common.command.BulkOperationCommand;
import io.swagger.v3.oas.annotations.Operation;
import java.util.List;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/cabinet/mqttServer")
@RequiredArgsConstructor
@Validated
public class MqttServerController extends BaseController {
private final MqttServerApplicationService mqttServerApplicationService;
@Operation(summary = "MQTT服务列表")
@GetMapping
public ResponseDTO<PageDTO<MqttServerDTO>> list(SearchMqttServerQuery<MqttServerEntity> query) {
PageDTO<MqttServerDTO> page = mqttServerApplicationService.getMqttServerList(query);
return ResponseDTO.ok(page);
}
@Operation(summary = "新增MQTT服务")
@AccessLog(title = "MQTT服务管理", businessType = BusinessTypeEnum.ADD)
@PostMapping
public ResponseDTO<Void> add(@Validated @RequestBody AddMqttServerCommand command) {
mqttServerApplicationService.addMqttServer(command);
return ResponseDTO.ok();
}
@Operation(summary = "修改MQTT服务")
@AccessLog(title = "MQTT服务管理", businessType = BusinessTypeEnum.MODIFY)
@PutMapping("/{serverId}")
public ResponseDTO<Void> edit(@PathVariable Long serverId, @Validated @RequestBody UpdateMqttServerCommand command) {
command.setMqttServerId(serverId);
mqttServerApplicationService.updateMqttServer(command);
return ResponseDTO.ok();
}
@Operation(summary = "删除MQTT服务")
@AccessLog(title = "MQTT服务管理", businessType = BusinessTypeEnum.DELETE)
@DeleteMapping("/{ids}")
public ResponseDTO<Void> remove(@PathVariable @NotNull List<Long> ids) {
mqttServerApplicationService.deleteMqttServer(new BulkOperationCommand<>(ids));
return ResponseDTO.ok();
}
}

View File

@ -0,0 +1,58 @@
package com.agileboot.domain.cabinet.mqtt;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.cabinet.mqtt.command.AddMqttServerCommand;
import com.agileboot.domain.cabinet.mqtt.command.UpdateMqttServerCommand;
import com.agileboot.domain.cabinet.mqtt.db.MqttServerEntity;
import com.agileboot.domain.cabinet.mqtt.db.MqttServerService;
import com.agileboot.domain.cabinet.mqtt.dto.MqttServerDTO;
import com.agileboot.domain.cabinet.mqtt.model.MqttServerModel;
import com.agileboot.domain.cabinet.mqtt.model.MqttServerModelFactory;
import com.agileboot.domain.cabinet.mqtt.query.SearchMqttServerQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.cache.annotation.Cacheable;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@RequiredArgsConstructor
public class MqttServerApplicationService {
private final MqttServerService mqttServerService;
private final MqttServerModelFactory mqttServerModelFactory;
public PageDTO<MqttServerDTO> getMqttServerList(SearchMqttServerQuery<MqttServerEntity> query) {
Page<MqttServerEntity> page = mqttServerService.getServerList(query);
List<MqttServerDTO> dtoList = page.getRecords().stream()
.map(MqttServerDTO::new)
.collect(Collectors.toList());
return new PageDTO<>(dtoList, page.getTotal());
}
public MqttServerEntity getByServerId(Long serverId) {
return mqttServerService.getById(serverId);
}
public void addMqttServer(AddMqttServerCommand command) {
MqttServerModel model = mqttServerModelFactory.create();
model.loadAddCommand(command);
model.insert();
}
public void updateMqttServer(UpdateMqttServerCommand command) {
MqttServerModel model = mqttServerModelFactory.loadById(command.getMqttServerId());
model.loadUpdateCommand(command);
model.updateById();
}
public void deleteMqttServer(BulkOperationCommand<Long> command) {
for (Long id : command.getIds()) {
MqttServerModel model = mqttServerModelFactory.loadById(id);
model.deleteById();
}
}
}

View File

@ -0,0 +1,11 @@
package com.agileboot.domain.cabinet.mqtt.command;
import com.agileboot.domain.cabinet.mqtt.db.MqttServerEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class AddMqttServerCommand extends MqttServerEntity {
}

View File

@ -0,0 +1,16 @@
package com.agileboot.domain.cabinet.mqtt.command;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.PositiveOrZero;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class UpdateMqttServerCommand extends AddMqttServerCommand {
@NotNull
@PositiveOrZero
private Long mqttServerId;
}

View File

@ -0,0 +1,60 @@
package com.agileboot.domain.cabinet.mqtt.db;
import com.agileboot.common.core.base.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
/**
* <p>
* MQTT服务配置表
* </p>
*
* @author valarchie
* @since 2025-05-08
*/
@Getter
@Setter
@TableName("mqtt_server")
@ApiModel(value = "MqttServerEntity对象", description = "MQTT服务配置表")
public class MqttServerEntity extends BaseEntity<MqttServerEntity> {
private static final long serialVersionUID = 1L;
@ApiModelProperty("主键ID")
@TableId(value = "mqtt_server_id", type = IdType.AUTO)
private Long mqttServerId;
@ApiModelProperty("MQTT服务器地址")
@TableField("server_url")
private String serverUrl;
@ApiModelProperty("连接账号")
@TableField("username")
private String username;
@ApiModelProperty("连接密码")
@TableField("`password`")
private String password;
@ApiModelProperty("订阅主题过滤器")
@TableField("topic_filter")
private String topicFilter;
@ApiModelProperty("发布主题")
@TableField("publish_topic")
private String publishTopic;
@Override
public Serializable pkVal() {
return this.mqttServerId;
}
}

View File

@ -0,0 +1,35 @@
package com.agileboot.domain.cabinet.mqtt.db;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* <p>
* MQTT服务配置表 Mapper 接口
* </p>
*
* @author valarchie
* @since 2025-05-08
*/
public interface MqttServerMapper extends BaseMapper<MqttServerEntity> {
@Select("SELECT mqtt_server_id, server_url, username, password, topic_filter, publish_topic " +
"FROM mqtt_server " +
"${ew.customSqlSegment}")
Page<MqttServerEntity> getMqttServerList(
Page<MqttServerEntity> page,
@Param(Constants.WRAPPER) Wrapper<MqttServerEntity> queryWrapper
);
@Select("SELECT * FROM mqtt_server WHERE server_url = #{serverUrl} LIMIT 1")
MqttServerEntity selectByServerUrl(String serverUrl);
@Select("SELECT * FROM mqtt_server WHERE mqtt_server_id = #{id} LIMIT 1")
MqttServerEntity selectById(Long id);
}

View File

@ -0,0 +1,21 @@
package com.agileboot.domain.cabinet.mqtt.db;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* MQTT服务配置表 服务类
* </p>
*
* @author valarchie
* @since 2025-05-08
*/
import com.agileboot.common.core.page.AbstractPageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
public interface MqttServerService extends IService<MqttServerEntity> {
Page<MqttServerEntity> getServerList(AbstractPageQuery<MqttServerEntity> query);
List<MqttServerEntity> selectAll();
}

View File

@ -0,0 +1,34 @@
package com.agileboot.domain.cabinet.mqtt.db;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* MQTT服务配置表 服务实现类
* </p>
*
* @author valarchie
* @since 2025-05-08
*/
import com.agileboot.common.core.page.AbstractPageQuery;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
@Service
public class MqttServerServiceImpl extends ServiceImpl<MqttServerMapper, MqttServerEntity> implements MqttServerService {
@Override
public Page<MqttServerEntity> getServerList(AbstractPageQuery<MqttServerEntity> query) {
return this.page(query.toPage(), query.toQueryWrapper());
}
@Override
public List<MqttServerEntity> selectAll() {
LambdaQueryWrapper<MqttServerEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(MqttServerEntity::getDeleted, false)
.orderByDesc(MqttServerEntity::getCreateTime);
return this.list(wrapper);
}
}

View File

@ -0,0 +1,38 @@
package com.agileboot.domain.cabinet.mqtt.dto;
import cn.hutool.core.bean.BeanUtil;
import com.agileboot.common.annotation.ExcelColumn;
import com.agileboot.common.annotation.ExcelSheet;
import com.agileboot.domain.cabinet.mqtt.db.MqttServerEntity;
import com.agileboot.domain.common.cache.CacheCenter;
import com.agileboot.domain.system.user.db.SysUserEntity;
import lombok.Data;
@ExcelSheet(name = "MQTT服务配置列表")
@Data
public class MqttServerDTO {
public MqttServerDTO(MqttServerEntity entity) {
if (entity != null) {
BeanUtil.copyProperties(entity, this);
}
}
@ExcelColumn(name = "服务ID")
private Long mqttServerId;
@ExcelColumn(name = "服务器地址")
private String serverUrl;
@ExcelColumn(name = "用户名")
private String username;
@ExcelColumn(name = "密码")
private String password;
@ExcelColumn(name = "订阅主题")
private String topicFilter;
@ExcelColumn(name = "发布主题")
private String publishTopic;
}

View File

@ -0,0 +1,39 @@
package com.agileboot.domain.cabinet.mqtt.model;
import cn.hutool.core.bean.BeanUtil;
import com.agileboot.domain.cabinet.mqtt.db.MqttServerEntity;
import com.agileboot.domain.cabinet.mqtt.command.AddMqttServerCommand;
import com.agileboot.domain.cabinet.mqtt.command.UpdateMqttServerCommand;
import com.agileboot.domain.cabinet.mqtt.db.MqttServerService;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class MqttServerModel extends MqttServerEntity {
private MqttServerService mqttServerService;
public MqttServerModel(MqttServerEntity entity, MqttServerService mqttServerService) {
this(mqttServerService);
if (entity != null) {
BeanUtil.copyProperties(entity, this);
}
}
public MqttServerModel(MqttServerService mqttServerService) {
this.mqttServerService = mqttServerService;
}
public void loadAddCommand(AddMqttServerCommand command) {
if (command != null) {
BeanUtil.copyProperties(command, this, "mqttServerId");
}
}
public void loadUpdateCommand(UpdateMqttServerCommand command) {
if (command != null) {
loadAddCommand(command);
}
}
}

View File

@ -0,0 +1,27 @@
package com.agileboot.domain.cabinet.mqtt.model;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import com.agileboot.domain.cabinet.mqtt.db.MqttServerEntity;
import com.agileboot.domain.cabinet.mqtt.db.MqttServerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class MqttServerModelFactory {
private final MqttServerService mqttServerService;
public MqttServerModel loadById(Long mqttServerId) {
MqttServerEntity entity = mqttServerService.getById(mqttServerId);
if (entity == null) {
throw new ApiException(ErrorCode.Business.COMMON_OBJECT_NOT_FOUND, mqttServerId, "MQTT服务器配置");
}
return new MqttServerModel(entity, mqttServerService);
}
public MqttServerModel create() {
return new MqttServerModel(mqttServerService);
}
}

View File

@ -0,0 +1,40 @@
package com.agileboot.domain.cabinet.mqtt.query;
import cn.hutool.core.util.StrUtil;
import com.agileboot.common.core.page.AbstractPageQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class SearchMqttServerQuery<T> extends AbstractPageQuery<T> {
private Long mqttServerId;
private String serverUrl;
private String username;
private String password;
private String topicFilter;
private String publishTopic;
private Date startTime;
private Date endTime;
@Override
public QueryWrapper<T> addQueryCondition() {
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
queryWrapper
.eq(mqttServerId != null, "mqtt_server_id", mqttServerId)
.like(StrUtil.isNotEmpty(serverUrl), "server_url", serverUrl)
.eq(StrUtil.isNotEmpty(username), "username", username)
.eq(StrUtil.isNotEmpty(password), "password", password)
.like(StrUtil.isNotEmpty(topicFilter), "topic_filter", topicFilter)
.like(StrUtil.isNotEmpty(publishTopic), "publish_topic", publishTopic)
.between(startTime != null && endTime != null, "create_time", startTime, endTime);
this.timeRangeColumn = "create_time";
return queryWrapper;
}
}

View File

@ -39,6 +39,10 @@ public class SmartCabinetEntity extends BaseEntity<SmartCabinetEntity> {
@TableField("cabinet_type") @TableField("cabinet_type")
private Integer cabinetType; private Integer cabinetType;
@ApiModelProperty("MQTT服务ID")
@TableField("mqtt_server_id")
private Long mqttServerId;
@ApiModelProperty("柜机模版编号") @ApiModelProperty("柜机模版编号")
@TableField("template_no") @TableField("template_no")
private String templateNo; private String templateNo;

View File

@ -6,6 +6,8 @@ import com.agileboot.common.annotation.ExcelSheet;
import com.agileboot.domain.common.cache.CacheCenter; import com.agileboot.domain.common.cache.CacheCenter;
import com.agileboot.domain.cabinet.smartCabinet.db.SmartCabinetEntity; import com.agileboot.domain.cabinet.smartCabinet.db.SmartCabinetEntity;
import com.agileboot.domain.system.user.db.SysUserEntity; import com.agileboot.domain.system.user.db.SysUserEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@ExcelSheet(name = "智能柜信息表") @ExcelSheet(name = "智能柜信息表")
@ -32,6 +34,9 @@ public class SmartCabinetDTO {
@ExcelColumn(name = "柜机类型0主柜 1副柜") @ExcelColumn(name = "柜机类型0主柜 1副柜")
private Integer cabinetType; private Integer cabinetType;
@ExcelColumn(name = "MQTT服务ID")
private Long mqttServerId;
@ExcelColumn(name = "柜机模版编号") @ExcelColumn(name = "柜机模版编号")
private String templateNo; private String templateNo;

View File

@ -16,6 +16,7 @@ public class SearchSmartCabinetQuery<T> extends AbstractPageQuery<T> {
private String templateNo; private String templateNo;
private Date startTime; private Date startTime;
private Date endTime; private Date endTime;
private Long mqttServerId;
@Override @Override
public QueryWrapper<T> addQueryCondition() { public QueryWrapper<T> addQueryCondition() {
@ -24,6 +25,7 @@ public class SearchSmartCabinetQuery<T> extends AbstractPageQuery<T> {
queryWrapper queryWrapper
.like(StrUtil.isNotEmpty(cabinetName), "cabinet_name", cabinetName) .like(StrUtil.isNotEmpty(cabinetName), "cabinet_name", cabinetName)
.eq(cabinetType != null, "cabinet_type", cabinetType) .eq(cabinetType != null, "cabinet_type", cabinetType)
.eq(mqttServerId!= null, "mqtt_server_id", mqttServerId)
.eq(StrUtil.isNotEmpty(templateNo), "template_no", templateNo) .eq(StrUtil.isNotEmpty(templateNo), "template_no", templateNo)
.eq("deleted", false) .eq("deleted", false)
.between(startTime != null && endTime != null, "create_time", startTime, endTime); .between(startTime != null && endTime != null, "create_time", startTime, endTime);

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.agileboot.domain.cabinet.mqtt.db.MqttServerMapper">
</mapper>

View File

@ -61,7 +61,7 @@ public class CodeGenerator {
//生成的类 放在orm子模块下的/target/generated-code目录底下 //生成的类 放在orm子模块下的/target/generated-code目录底下
.module("/agileboot-orm/target/generated-code") .module("/agileboot-orm/target/generated-code")
.parentPackage("com.agileboot") .parentPackage("com.agileboot")
.tableName("payment_operation_log") .tableName("mqtt_server")
// 决定是否继承基类 // 决定是否继承基类
.isExtendsFromBaseEntity(true) .isExtendsFromBaseEntity(true)
.build(); .build();

View File

@ -0,0 +1,21 @@
DROP TABLE IF EXISTS `mqtt_server`;
CREATE TABLE `mqtt_server` (
`mqtt_server_id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`server_url` VARCHAR(200) NOT NULL COMMENT 'MQTT服务器地址',
`username` VARCHAR(100) NOT NULL COMMENT '连接账号',
`password` VARCHAR(200) NOT NULL COMMENT '连接密码',
`topic_filter` VARCHAR(200) NOT NULL COMMENT '订阅主题过滤器',
`publish_topic` VARCHAR(200) NOT NULL COMMENT '发布主题',
`creator_id` BIGINT NULL DEFAULT 0 COMMENT '创建者ID',
`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater_id` BIGINT NULL DEFAULT 0 COMMENT '更新者ID',
`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '删除标志0存在 1删除',
PRIMARY KEY (`mqtt_server_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='MQTT服务配置表';
ALTER TABLE `smart_cabinet`
ADD COLUMN `mqtt_server_id` BIGINT NULL COMMENT 'MQTT服务ID'
AFTER `cabinet_type`;