Merge remote-tracking branch 'origin/main'

This commit is contained in:
达富斌
2025-09-09 15:12:43 +08:00
10 changed files with 582 additions and 1 deletions

View File

@@ -0,0 +1,109 @@
package cn.iocoder.yudao.module.farm.controller.admin.deviceclassify;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo.DeviceClassifyPageReqVO;
import cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo.DeviceClassifyRespVO;
import cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo.DeviceClassifySaveReqVO;
import cn.iocoder.yudao.module.farm.dal.dataobject.deviceclassify.DeviceClassifyDO;
import cn.iocoder.yudao.module.farm.service.deviceclassify.DeviceClassifyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - 设备分类")
@RestController
@RequestMapping("/farm/device-classify")
@Validated
public class DeviceClassifyController {
@Resource
private DeviceClassifyService deviceClassifyService;
@PostMapping("/create")
@Operation(summary = "创建设备分类")
@PreAuthorize("@ss.hasPermission('farm:device-classify:create')")
public CommonResult<Long> createDeviceClassify(@Valid @RequestBody DeviceClassifySaveReqVO createReqVO) {
return success(deviceClassifyService.createDeviceClassify(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新设备分类")
@PreAuthorize("@ss.hasPermission('farm:device-classify:update')")
public CommonResult<Boolean> updateDeviceClassify(@Valid @RequestBody DeviceClassifySaveReqVO updateReqVO) {
deviceClassifyService.updateDeviceClassify(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除设备分类")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('farm:device-classify:delete')")
public CommonResult<Boolean> deleteDeviceClassify(@RequestParam("id") Long id) {
deviceClassifyService.deleteDeviceClassify(id);
return success(true);
}
@DeleteMapping("/delete-list")
@Parameter(name = "ids", description = "编号", required = true)
@Operation(summary = "批量删除设备分类")
@PreAuthorize("@ss.hasPermission('farm:device-classify:delete')")
public CommonResult<Boolean> deleteDeviceClassifyList(@RequestParam("ids") List<Long> ids) {
deviceClassifyService.deleteDeviceClassifyListByIds(ids);
return success(true);
}
@GetMapping("/get")
@Operation(summary = "获得设备分类")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('farm:device-classify:query')")
public CommonResult<DeviceClassifyRespVO> getDeviceClassify(@RequestParam("id") Long id) {
DeviceClassifyDO deviceClassify = deviceClassifyService.getDeviceClassify(id);
return success(BeanUtils.toBean(deviceClassify, DeviceClassifyRespVO.class));
}
@GetMapping("/page")
@Operation(summary = "获得设备分类分页")
@PreAuthorize("@ss.hasPermission('farm:device-classify:query')")
public CommonResult<PageResult<DeviceClassifyRespVO>> getDeviceClassifyPage(@Valid DeviceClassifyPageReqVO pageReqVO) {
PageResult<DeviceClassifyDO> pageResult = deviceClassifyService.getDeviceClassifyPage(pageReqVO);
return success(BeanUtils.toBean(pageResult, DeviceClassifyRespVO.class));
}
@GetMapping("/list")
@Operation(summary = "获得设备分类全量")
public CommonResult<List<DeviceClassifyRespVO>> getDeviceClassifyList() {
List<DeviceClassifyDO> pageResult = deviceClassifyService.getDeviceClassifyList();
return success(BeanUtils.toBean(pageResult, DeviceClassifyRespVO.class));
}
@GetMapping("/export-excel")
@Operation(summary = "导出设备分类 Excel")
@PreAuthorize("@ss.hasPermission('farm:device-classify:export')")
@ApiAccessLog(operateType = EXPORT)
public void exportDeviceClassifyExcel(@Valid DeviceClassifyPageReqVO pageReqVO,
HttpServletResponse response) throws IOException {
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
List<DeviceClassifyDO> list = deviceClassifyService.getDeviceClassifyPage(pageReqVO).getList();
// 导出 Excel
ExcelUtils.write(response, "设备分类.xls", "数据", DeviceClassifyRespVO.class,
BeanUtils.toBean(list, DeviceClassifyRespVO.class));
}
}

View File

@@ -0,0 +1,32 @@
package cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - 设备分类分页 Request VO")
@Data
public class DeviceClassifyPageReqVO extends PageParam {
@Schema(description = "父id", example = "8103")
private Long parentId;
@Schema(description = "名称", example = "赵六")
private String name;
@Schema(description = "图片")
private String image;
@Schema(description = "描述", example = "你说的对")
private String description;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
private String tenantId;
}

View File

@@ -0,0 +1,42 @@
package cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo;
import cn.idev.excel.annotation.ExcelIgnoreUnannotated;
import cn.idev.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
@Schema(description = "管理后台 - 设备分类 Response VO")
@Data
@ExcelIgnoreUnannotated
public class DeviceClassifyRespVO {
@Schema(description = "主键ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "16263")
@ExcelProperty("主键ID")
private Long id;
@Schema(description = "父id", example = "8103")
@ExcelProperty("父id")
private Long parentId;
@Schema(description = "名称", example = "赵六")
@ExcelProperty("名称")
private String name;
@Schema(description = "图片(单个图片URL,兼容旧接口)")
@ExcelProperty("图片")
private String image;
@Schema(description = "图片集合(支持多张图片)")
private List<String> imageList;
@Schema(description = "描述", example = "你说的对")
@ExcelProperty("描述")
private String description;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("创建时间")
private LocalDateTime createTime;
}

View File

@@ -0,0 +1,31 @@
package cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.List;
import jakarta.validation.constraints.NotBlank;
@Schema(description = "管理后台 - 设备分类新增/修改 Request VO")
@Data
public class DeviceClassifySaveReqVO {
@Schema(description = "主键ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "16263")
private Long id;
@Schema(description = "父id", example = "8103")
private Long parentId;
@Schema(description = "名称", example = "赵六")
@NotBlank(message = "设备分类名称不能为空")
private String name;
@Schema(description = "图片(单个图片URL,兼容旧接口)")
private String image;
@Schema(description = "图片集合(支持多张图片)")
private List<String> imageList;
@Schema(description = "描述", example = "你说的对")
private String description;
}

View File

@@ -0,0 +1,75 @@
package cn.iocoder.yudao.module.farm.dal.dataobject.deviceclassify;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.farm.framework.util.ImageCollectionUtil;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import java.util.List;
/**
* 设备分类 DO
*
* @author 超级管理
*/
@TableName("farm_device_classify")
@KeySequence("farm_device_classify_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DeviceClassifyDO extends BaseDO {
/**
* 主键ID
*/
@TableId
private Long id;
/**
* 父id
*/
private Long parentId;
/**
* 名称
*/
private String name;
/**
* 图片
*/
private String image;
/**
* 描述
*/
private String description;
private String tenantId;
/**
* 设备图片集合(业务层使用)
*/
@TableField(exist = false)
private List<String> imageList;
/**
* 获取设备图片集合
*/
public List<String> getImageList() {
if (imageList == null) {
imageList = ImageCollectionUtil.toImageList(this.image);
}
return imageList;
}
/**
* 设置设备图片集合
*/
public void setImageList(List<String> imageList) {
this.imageList = imageList;
this.image = ImageCollectionUtil.toDatabaseString(imageList);
}
}

View File

@@ -0,0 +1,30 @@
package cn.iocoder.yudao.module.farm.dal.mysql.deviceclassify;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo.DeviceClassifyPageReqVO;
import cn.iocoder.yudao.module.farm.dal.dataobject.deviceclassify.DeviceClassifyDO;
import org.apache.ibatis.annotations.Mapper;
/**
* 设备分类 Mapper
*
* @author 超级管理
*/
@Mapper
public interface DeviceClassifyMapper extends BaseMapperX<DeviceClassifyDO> {
default PageResult<DeviceClassifyDO> selectPage(DeviceClassifyPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<DeviceClassifyDO>()
.eqIfPresent(DeviceClassifyDO::getParentId, reqVO.getParentId())
.likeIfPresent(DeviceClassifyDO::getName, reqVO.getName())
.eqIfPresent(DeviceClassifyDO::getImage, reqVO.getImage())
.eqIfPresent(DeviceClassifyDO::getDescription, reqVO.getDescription())
.eqIfPresent(DeviceClassifyDO::getTenantId, reqVO.getTenantId())
.betweenIfPresent(DeviceClassifyDO::getCreateTime, reqVO.getCreateTime())
.orderByDesc(DeviceClassifyDO::getId));
}
void deleteByClassifyId(Long id);
}

View File

@@ -132,5 +132,10 @@ public interface ErrorCodeConstants {
ErrorCode DUPLICATE_SPECIES_NAMES_ERROR = new ErrorCode(1_050_041_000, "气象灾害名称重复");
ErrorCode NOT_EXIST = new ErrorCode(1_050_042_000, "节点不存在或已被删除");
ErrorCode DEVICE_CLASSIFY_NOT_EXISTS = new ErrorCode(1_050_045_000, "设备分类不存在或已被删除");
// ========== 图片集合相关 1_050_046_000 ==========
ErrorCode IMAGE_COLLECTION_INVALID = new ErrorCode(1_050_046_000, "图片集合格式无效");
ErrorCode IMAGE_URL_INVALID = new ErrorCode(1_050_046_001, "图片URL格式无效");
ErrorCode IMAGE_COLLECTION_TOO_LARGE = new ErrorCode(1_050_046_002, "图片集合过大,最多支持10张图片");
}

View File

@@ -0,0 +1,64 @@
package cn.iocoder.yudao.module.farm.service.deviceclassify;
import java.util.*;
import jakarta.validation.*;
import cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo.*;
import cn.iocoder.yudao.module.farm.dal.dataobject.deviceclassify.DeviceClassifyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
/**
* 设备分类 Service 接口
*
* @author 超级管理
*/
public interface DeviceClassifyService {
/**
* 创建设备分类
*
* @param createReqVO 创建信息
* @return 编号
*/
Long createDeviceClassify(@Valid DeviceClassifySaveReqVO createReqVO);
/**
* 更新设备分类
*
* @param updateReqVO 更新信息
*/
void updateDeviceClassify(@Valid DeviceClassifySaveReqVO updateReqVO);
/**
* 删除设备分类
*
* @param id 编号
*/
void deleteDeviceClassify(Long id);
/**
* 批量删除设备分类
*
* @param ids 编号
*/
void deleteDeviceClassifyListByIds(List<Long> ids);
/**
* 获得设备分类
*
* @param id 编号
* @return 设备分类
*/
DeviceClassifyDO getDeviceClassify(Long id);
/**
* 获得设备分类分页
*
* @param pageReqVO 分页查询
* @return 设备分类分页
*/
PageResult<DeviceClassifyDO> getDeviceClassifyPage(DeviceClassifyPageReqVO pageReqVO);
List<DeviceClassifyDO> getDeviceClassifyList();
}

View File

@@ -0,0 +1,176 @@
package cn.iocoder.yudao.module.farm.service.deviceclassify;
import cn.hutool.core.text.CharSequenceUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo.DeviceClassifyPageReqVO;
import cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo.DeviceClassifySaveReqVO;
import cn.iocoder.yudao.module.farm.dal.dataobject.deviceclassify.DeviceClassifyDO;
import cn.iocoder.yudao.module.farm.dal.mysql.deviceclassify.DeviceClassifyMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static cn.iocoder.yudao.module.farm.enums.ErrorCodeConstants.*;
/**
* 设备分类 Service 实现类
*
* @author 超级管理
*/
@Service
@Validated
@Slf4j
public class DeviceClassifyServiceImpl implements DeviceClassifyService {
@Resource
private DeviceClassifyMapper deviceClassifyMapper;
@Override
public Long createDeviceClassify(DeviceClassifySaveReqVO createReqVO) {
// 1. 处理图片集合转换
List<String> finalImageList = processImageCollection(createReqVO.getImageList(), createReqVO.getImage());
// 2. 插入
DeviceClassifyDO deviceClassify = BeanUtils.toBean(createReqVO, DeviceClassifyDO.class);
deviceClassify.setTenantId(getLoginUser().getTenantId().toString());
// 3. 设置处理后的图片集合
deviceClassify.setImageList(finalImageList);
deviceClassifyMapper.insert(deviceClassify);
// 返回
return deviceClassify.getId();
}
@Override
public void updateDeviceClassify(DeviceClassifySaveReqVO updateReqVO) {
// 校验存在
validateDeviceClassifyExists(updateReqVO.getId());
// 处理图片集合转换
List<String> finalImageList = processImageCollection(updateReqVO.getImageList(), updateReqVO.getImage());
// 更新
DeviceClassifyDO updateObj = BeanUtils.toBean(updateReqVO, DeviceClassifyDO.class);
updateObj.setImageList(finalImageList);
deviceClassifyMapper.updateById(updateObj);
}
@Override
public void deleteDeviceClassify(Long id) {
// 校验存在
validateDeviceClassifyExists(id);
// 删除
deviceClassifyMapper.deleteByClassifyId(id);
}
@Override
public void deleteDeviceClassifyListByIds(List<Long> ids) {
// 删除
deviceClassifyMapper.deleteByIds(ids);
}
private void validateDeviceClassifyExists(Long id) {
if (deviceClassifyMapper.selectById(id) == null) {
throw exception(DEVICE_CLASSIFY_NOT_EXISTS);
}
}
@Override
public DeviceClassifyDO getDeviceClassify(Long id) {
DeviceClassifyDO deviceClassifyDO = deviceClassifyMapper.selectById(id);
if (deviceClassifyDO != null) {
deviceClassifyDO.getImageList();
}
return deviceClassifyDO;
}
@Override
public PageResult<DeviceClassifyDO> getDeviceClassifyPage(DeviceClassifyPageReqVO pageReqVO) {
pageReqVO.setTenantId(getLoginUser().getTenantId().toString());
return deviceClassifyMapper.selectPage(pageReqVO);
}
@Override
public List<DeviceClassifyDO> getDeviceClassifyList() {
List<DeviceClassifyDO> deviceClassifyDOS = deviceClassifyMapper.selectList(new LambdaQueryWrapperX<DeviceClassifyDO>().eq(DeviceClassifyDO::getTenantId, getLoginUser().getTenantId()));
deviceClassifyDOS.forEach(DeviceClassifyDO::getImageList);
return deviceClassifyDOS;
}
/**
* 处理图片集合转换
* 支持从 imageList 或 image 字段获取图片,优先使用 imageList
*
* @param imageList 图片集合
* @param singleImage 单个图片URL(兼容旧接口)
* @return 处理后的图片集合
*/
private List<String> processImageCollection(List<String> imageList, String singleImage) {
List<String> finalImageList = new ArrayList<>();
// 优先使用 imageList
if (imageList != null && !imageList.isEmpty()) {
// 验证图片集合大小
if (imageList.size() > 10) {
throw exception(IMAGE_COLLECTION_TOO_LARGE);
}
// 验证并过滤图片URL
for (String imageUrl : imageList) {
if (CharSequenceUtil.isNotBlank(imageUrl)) {
if (!isValidImageUrl(imageUrl)) {
log.warn("无效的图片URL: {}", imageUrl);
continue;
}
if (!finalImageList.contains(imageUrl)) {
finalImageList.add(imageUrl);
}
}
}
log.debug("处理图片集合,原始数量: {}, 有效数量: {}", imageList.size(), finalImageList.size());
return finalImageList;
}
// 如果 imageList 为空,尝试使用单个图片
if (CharSequenceUtil.isNotBlank(singleImage)) {
if (!isValidImageUrl(singleImage)) {
log.warn("无效的单个图片URL: {}", singleImage);
return new ArrayList<>();
}
finalImageList.add(singleImage);
return finalImageList;
}
// 都为空,返回空集合
return finalImageList;
}
/**
* 验证图片URL是否有效
*
* @param imageUrl 图片URL
* @return 是否有效
*/
private boolean isValidImageUrl(String imageUrl) {
if (CharSequenceUtil.isBlank(imageUrl)) {
return false;
}
// 基本的URL格式验证
return imageUrl.startsWith("http://") || imageUrl.startsWith("https://") ||
imageUrl.startsWith("/") || imageUrl.startsWith("data:image/");
}
}

View File

@@ -0,0 +1,17 @@
<?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="cn.iocoder.yudao.module.farm.dal.mysql.deviceclassify.DeviceClassifyMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
-->
<delete id="deleteByClassifyId" parameterType="Long">
delete
from farm_device_classify
where id = #{id}
</delete>
</mapper>