设备分类
This commit is contained in:
@@ -87,7 +87,7 @@ public class DeviceClassifyController {
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "获得设备分类分页")
|
||||
@Operation(summary = "获得设备分类全量")
|
||||
public CommonResult<List<DeviceClassifyRespVO>> getDeviceClassifyList() {
|
||||
List<DeviceClassifyDO> pageResult = deviceClassifyService.getDeviceClassifyList();
|
||||
return success(BeanUtils.toBean(pageResult, DeviceClassifyRespVO.class));
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
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.*;
|
||||
import java.util.*;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import cn.idev.excel.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
@Schema(description = "管理后台 - 设备分类 Response VO")
|
||||
@Data
|
||||
@@ -24,10 +25,12 @@ public class DeviceClassifyRespVO {
|
||||
@ExcelProperty("名称")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "图片")
|
||||
@Schema(description = "图片(单个图片URL,兼容旧接口)")
|
||||
@ExcelProperty("图片")
|
||||
private String image;
|
||||
|
||||
@Schema(description = "图片集合(支持多张图片)")
|
||||
private List<String> imageList;
|
||||
@Schema(description = "描述", example = "你说的对")
|
||||
@ExcelProperty("描述")
|
||||
private String description;
|
||||
|
||||
@@ -2,8 +2,8 @@ package cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
import java.util.*;
|
||||
import jakarta.validation.constraints.*;
|
||||
import java.util.List;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
@Schema(description = "管理后台 - 设备分类新增/修改 Request VO")
|
||||
@Data
|
||||
@@ -16,11 +16,15 @@ public class DeviceClassifySaveReqVO {
|
||||
private Long parentId;
|
||||
|
||||
@Schema(description = "名称", example = "赵六")
|
||||
@NotBlank(message = "设备分类名称不能为空")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "图片")
|
||||
@Schema(description = "图片(单个图片URL,兼容旧接口)")
|
||||
private String image;
|
||||
|
||||
@Schema(description = "图片集合(支持多张图片)")
|
||||
private List<String> imageList;
|
||||
|
||||
@Schema(description = "描述", example = "你说的对")
|
||||
private String description;
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
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.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 设备分类 DO
|
||||
@@ -47,4 +48,28 @@ public class DeviceClassifyDO extends BaseDO {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,4 +133,9 @@ public interface ErrorCodeConstants {
|
||||
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张图片");
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
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;
|
||||
@@ -8,14 +9,16 @@ import cn.iocoder.yudao.module.farm.controller.admin.deviceclassify.vo.DeviceCla
|
||||
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.DEVICE_CLASSIFY_NOT_EXISTS;
|
||||
import static cn.iocoder.yudao.module.farm.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
* 设备分类 Service 实现类
|
||||
@@ -24,6 +27,7 @@ import static cn.iocoder.yudao.module.farm.enums.ErrorCodeConstants.DEVICE_CLASS
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class DeviceClassifyServiceImpl implements DeviceClassifyService {
|
||||
|
||||
@Resource
|
||||
@@ -31,11 +35,16 @@ public class DeviceClassifyServiceImpl implements DeviceClassifyService {
|
||||
|
||||
@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());
|
||||
deviceClassifyMapper.insert(deviceClassify);
|
||||
|
||||
// 3. 设置处理后的图片集合
|
||||
deviceClassify.setImageList(finalImageList);
|
||||
deviceClassifyMapper.insert(deviceClassify);
|
||||
// 返回
|
||||
return deviceClassify.getId();
|
||||
}
|
||||
@@ -44,9 +53,16 @@ public class DeviceClassifyServiceImpl implements DeviceClassifyService {
|
||||
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
|
||||
@@ -72,7 +88,11 @@ public class DeviceClassifyServiceImpl implements DeviceClassifyService {
|
||||
|
||||
@Override
|
||||
public DeviceClassifyDO getDeviceClassify(Long id) {
|
||||
return deviceClassifyMapper.selectById(id);
|
||||
DeviceClassifyDO deviceClassifyDO = deviceClassifyMapper.selectById(id);
|
||||
if (deviceClassifyDO != null) {
|
||||
deviceClassifyDO.getImageList();
|
||||
}
|
||||
return deviceClassifyDO;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -83,7 +103,74 @@ public class DeviceClassifyServiceImpl implements DeviceClassifyService {
|
||||
|
||||
@Override
|
||||
public List<DeviceClassifyDO> getDeviceClassifyList() {
|
||||
return deviceClassifyMapper.selectList(new LambdaQueryWrapperX<DeviceClassifyDO>().eq(DeviceClassifyDO::getTenantId, getLoginUser().getTenantId()));
|
||||
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/");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user