diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java index 42573213f..48ee62ddf 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java @@ -181,5 +181,11 @@ public interface ErrorCodeConstants { // ========== 除尘系统信息 TODO 补充编号 ========== ErrorCode DUST_EQUIPINFO_NOT_EXISTS = new ErrorCode(1_002_029_006, "除尘系统信息不存在"); - + // ========== 粉尘种类 TODO 补充编号 ========== + ErrorCode DUST_TYPE_NOT_EXISTS = new ErrorCode(1_002_030_001, "粉尘种类不存在"); + ErrorCode DUST_TYPE_EXITS_CHILDREN = new ErrorCode(1_002_030_002, "存在存在子粉尘种类,无法删除"); + ErrorCode DUST_TYPE_PARENT_NOT_EXITS = new ErrorCode(1_002_030_003,"父级粉尘种类不存在"); + ErrorCode DUST_TYPE_PARENT_ERROR = new ErrorCode(1_002_030_004, "不能设置自己为父粉尘种类"); + ErrorCode DUST_TYPE_NAME_DUPLICATE = new ErrorCode(1_002_030_005, "已经存在该名称的粉尘种类"); + ErrorCode DUST_TYPE_PARENT_IS_CHILD = new ErrorCode(1_002_030_006, "不能设置自己的子DustType为父DustType"); } diff --git a/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/DustTypeController.java b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/DustTypeController.java new file mode 100644 index 000000000..380b74309 --- /dev/null +++ b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/DustTypeController.java @@ -0,0 +1,95 @@ +package cn.iocoder.yudao.module.system.controller.admin.dusttype; + +import org.springframework.web.bind.annotation.*; +import jakarta.annotation.Resource; +import org.springframework.validation.annotation.Validated; +import org.springframework.security.access.prepost.PreAuthorize; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Operation; + +import jakarta.validation.constraints.*; +import jakarta.validation.*; +import jakarta.servlet.http.*; +import java.util.*; +import java.io.IOException; + +import cn.iocoder.yudao.framework.common.pojo.PageParam; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; + +import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; + +import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog; +import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*; + +import cn.iocoder.yudao.module.system.controller.admin.dusttype.vo.*; +import cn.iocoder.yudao.module.system.dal.dataobject.dusttype.DustTypeDO; +import cn.iocoder.yudao.module.system.service.dusttype.DustTypeService; + +@Tag(name = "管理后台 - 粉尘种类") +@RestController +@RequestMapping("/system/dust-type") +@Validated +public class DustTypeController { + + @Resource + private DustTypeService dustTypeService; + + @PostMapping("/create") + @Operation(summary = "创建粉尘种类") + @PreAuthorize("@ss.hasPermission('system:dust-type:create')") + public CommonResult createDustType(@Valid @RequestBody DustTypeSaveReqVO createReqVO) { + return success(dustTypeService.createDustType(createReqVO)); + } + + @PutMapping("/update") + @Operation(summary = "更新粉尘种类") + @PreAuthorize("@ss.hasPermission('system:dust-type:update')") + public CommonResult updateDustType(@Valid @RequestBody DustTypeSaveReqVO updateReqVO) { + dustTypeService.updateDustType(updateReqVO); + return success(true); + } + + @DeleteMapping("/delete") + @Operation(summary = "删除粉尘种类") + @Parameter(name = "id", description = "编号", required = true) + @PreAuthorize("@ss.hasPermission('system:dust-type:delete')") + public CommonResult deleteDustType(@RequestParam("id") String id) { + dustTypeService.deleteDustType(id); + return success(true); + } + + + @GetMapping("/get") + @Operation(summary = "获得粉尘种类") + @Parameter(name = "id", description = "编号", required = true, example = "1024") + @PreAuthorize("@ss.hasPermission('system:dust-type:query')") + public CommonResult getDustType(@RequestParam("id") String id) { + DustTypeDO dustType = dustTypeService.getDustType(id); + return success(BeanUtils.toBean(dustType, DustTypeRespVO.class)); + } + + @GetMapping("/list") + @Operation(summary = "获得粉尘种类列表") + @PreAuthorize("@ss.hasPermission('system:dust-type:query')") + public CommonResult> getDustTypeList(@Valid DustTypeListReqVO listReqVO) { + List list = dustTypeService.getDustTypeList(listReqVO); + return success(BeanUtils.toBean(list, DustTypeRespVO.class)); + } + + @GetMapping("/export-excel") + @Operation(summary = "导出粉尘种类 Excel") + @PreAuthorize("@ss.hasPermission('system:dust-type:export')") + @ApiAccessLog(operateType = EXPORT) + public void exportDustTypeExcel(@Valid DustTypeListReqVO listReqVO, + HttpServletResponse response) throws IOException { + List list = dustTypeService.getDustTypeList(listReqVO); + // 导出 Excel + ExcelUtils.write(response, "粉尘种类.xls", "数据", DustTypeRespVO.class, + BeanUtils.toBean(list, DustTypeRespVO.class)); + } + +} \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/vo/DustTypeListReqVO.java b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/vo/DustTypeListReqVO.java new file mode 100644 index 000000000..2a49f5573 --- /dev/null +++ b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/vo/DustTypeListReqVO.java @@ -0,0 +1,26 @@ +package cn.iocoder.yudao.module.system.controller.admin.dusttype.vo; + +import lombok.*; +import java.util.*; +import io.swagger.v3.oas.annotations.media.Schema; +import cn.iocoder.yudao.framework.common.pojo.PageParam; +import java.time.LocalDateTime; +import org.springframework.format.annotation.DateTimeFormat; + +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; + +@Schema(description = "管理后台 - 粉尘种类列表 Request VO") +@Data +public class DustTypeListReqVO { + + @Schema(description = "名称", example = "李四") + private String name; + + @Schema(description = "父级", example = "20841") + private String parentId; + + @Schema(description = "创建时间") + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + private LocalDateTime[] createTime; + +} \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/vo/DustTypeRespVO.java b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/vo/DustTypeRespVO.java new file mode 100644 index 000000000..a8cca1ed1 --- /dev/null +++ b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/vo/DustTypeRespVO.java @@ -0,0 +1,33 @@ +package cn.iocoder.yudao.module.system.controller.admin.dusttype.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.*; +import java.util.*; +import org.springframework.format.annotation.DateTimeFormat; +import java.time.LocalDateTime; +import cn.idev.excel.annotation.*; + +@Schema(description = "管理后台 - 粉尘种类 Response VO") +@Data +@ExcelIgnoreUnannotated +public class DustTypeRespVO { + + @Schema(description = "编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "A") + @ExcelProperty("编码") + private String id; + + @Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "李四") + @ExcelProperty("名称") + private String name; + + @Schema(description = "父级", example = "20841") + @ExcelProperty("父级") + private String parentId; + + @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) + @ExcelProperty("创建时间") + private LocalDateTime createTime; + @Schema(description = "排序", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + @ExcelProperty("排序") + private Integer sort; +} diff --git a/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/vo/DustTypeSaveReqVO.java b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/vo/DustTypeSaveReqVO.java new file mode 100644 index 000000000..e0fc343fd --- /dev/null +++ b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dusttype/vo/DustTypeSaveReqVO.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.module.system.controller.admin.dusttype.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.*; +import java.util.*; +import jakarta.validation.constraints.*; + +@Schema(description = "管理后台 - 粉尘种类新增/修改 Request VO") +@Data +public class DustTypeSaveReqVO { + + @Schema(description = "编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "A") + private String id; + + @Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "李四") + @NotEmpty(message = "名称不能为空") + private String name; + + @Schema(description = "父级", example = "20841") + private String parentId; + + @Schema(description = "排序", example = "1") + private Integer sort; + +} \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/dusttype/DustTypeDO.java b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/dusttype/DustTypeDO.java new file mode 100644 index 000000000..6eeea70f4 --- /dev/null +++ b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/dusttype/DustTypeDO.java @@ -0,0 +1,47 @@ +package cn.iocoder.yudao.module.system.dal.dataobject.dusttype; + +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; + +/** + * 粉尘种类 DO + * + * @author 超级管理员 + */ +@TableName("hazard_dust_type") +@KeySequence("hazard_dust_type_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。 +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class DustTypeDO extends BaseDO { + + public static final String PARENT_ID_ROOT = "0"; + + /** + * 编码 + */ + @TableId(type = IdType.INPUT) + private String id; + /** + * 名称 + */ + private String name; + /** + * 父级 + */ + private String parentId; + + /** + * 排序 + */ + private Integer sort; + + +} diff --git a/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dusttype/DustTypeMapper.java b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dusttype/DustTypeMapper.java new file mode 100644 index 000000000..1657fd95c --- /dev/null +++ b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dusttype/DustTypeMapper.java @@ -0,0 +1,36 @@ +package cn.iocoder.yudao.module.system.dal.mysql.dusttype; + +import java.util.*; + +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.module.system.dal.dataobject.dusttype.DustTypeDO; +import org.apache.ibatis.annotations.Mapper; +import cn.iocoder.yudao.module.system.controller.admin.dusttype.vo.*; + +/** + * 粉尘种类 Mapper + * + * @author 超级管理员 + */ +@Mapper +public interface DustTypeMapper extends BaseMapperX { + + default List selectList(DustTypeListReqVO reqVO) { + return selectList(new LambdaQueryWrapperX() + .likeIfPresent(DustTypeDO::getName, reqVO.getName()) + .eqIfPresent(DustTypeDO::getParentId, reqVO.getParentId()) + .betweenIfPresent(DustTypeDO::getCreateTime, reqVO.getCreateTime()) + .orderByAsc(DustTypeDO::getSort)); + } + + default DustTypeDO selectByParentIdAndName(String parentId, String name) { + return selectOne(DustTypeDO::getParentId, parentId, DustTypeDO::getName, name); + } + + default Long selectCountByParentId(String parentId) { + return selectCount(DustTypeDO::getParentId, parentId); + } + +} \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/service/dusttype/DustTypeService.java b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/service/dusttype/DustTypeService.java new file mode 100644 index 000000000..193bc49e9 --- /dev/null +++ b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/service/dusttype/DustTypeService.java @@ -0,0 +1,56 @@ +package cn.iocoder.yudao.module.system.service.dusttype; + +import java.util.*; +import jakarta.validation.*; +import cn.iocoder.yudao.module.system.controller.admin.dusttype.vo.*; +import cn.iocoder.yudao.module.system.dal.dataobject.dusttype.DustTypeDO; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.pojo.PageParam; + +/** + * 粉尘种类 Service 接口 + * + * @author 超级管理员 + */ +public interface DustTypeService { + + /** + * 创建粉尘种类 + * + * @param createReqVO 创建信息 + * @return 编号 + */ + String createDustType(@Valid DustTypeSaveReqVO createReqVO); + + /** + * 更新粉尘种类 + * + * @param updateReqVO 更新信息 + */ + void updateDustType(@Valid DustTypeSaveReqVO updateReqVO); + + /** + * 删除粉尘种类 + * + * @param id 编号 + */ + void deleteDustType(String id); + + + /** + * 获得粉尘种类 + * + * @param id 编号 + * @return 粉尘种类 + */ + DustTypeDO getDustType(String id); + + /** + * 获得粉尘种类列表 + * + * @param listReqVO 查询条件 + * @return 粉尘种类列表 + */ + List getDustTypeList(DustTypeListReqVO listReqVO); + +} \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/service/dusttype/DustTypeServiceImpl.java b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/service/dusttype/DustTypeServiceImpl.java new file mode 100644 index 000000000..990aac65f --- /dev/null +++ b/yudao-module-system/yudao-module-system-server/src/main/java/cn/iocoder/yudao/module/system/service/dusttype/DustTypeServiceImpl.java @@ -0,0 +1,141 @@ +package cn.iocoder.yudao.module.system.service.dusttype; + +import cn.hutool.core.collection.CollUtil; +import org.springframework.stereotype.Service; +import jakarta.annotation.Resource; +import org.springframework.validation.annotation.Validated; +import org.springframework.transaction.annotation.Transactional; + +import java.util.*; +import cn.iocoder.yudao.module.system.controller.admin.dusttype.vo.*; +import cn.iocoder.yudao.module.system.dal.dataobject.dusttype.DustTypeDO; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.pojo.PageParam; +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; + +import cn.iocoder.yudao.module.system.dal.mysql.dusttype.DustTypeMapper; + +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; + +/** + * 粉尘种类 Service 实现类 + * + * @author 超级管理员 + */ +@Service +@Validated +public class DustTypeServiceImpl implements DustTypeService { + + @Resource + private DustTypeMapper dustTypeMapper; + + @Override + public String createDustType(DustTypeSaveReqVO createReqVO) { + // 校验父级的有效性 + validateParentDustType(null, createReqVO.getParentId()); + // 校验名称的唯一性 + validateDustTypeNameUnique(null, createReqVO.getParentId(), createReqVO.getName()); + + // 插入 + DustTypeDO dustType = BeanUtils.toBean(createReqVO, DustTypeDO.class); + dustTypeMapper.insert(dustType); + + // 返回 + return dustType.getId(); + } + + @Override + public void updateDustType(DustTypeSaveReqVO updateReqVO) { + // 校验存在 + validateDustTypeExists(updateReqVO.getId()); + // 校验父级的有效性 + validateParentDustType(updateReqVO.getId(), updateReqVO.getParentId()); + // 校验名称的唯一性 + validateDustTypeNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName()); + + // 更新 + DustTypeDO updateObj = BeanUtils.toBean(updateReqVO, DustTypeDO.class); + dustTypeMapper.updateById(updateObj); + } + + @Override + public void deleteDustType(String id) { + // 校验存在 + validateDustTypeExists(id); + // 校验是否有子粉尘种类 + if (dustTypeMapper.selectCountByParentId(id) > 0) { + throw exception(DUST_TYPE_EXITS_CHILDREN); + } + // 删除 + dustTypeMapper.deleteById(id); + } + + + private void validateDustTypeExists(String id) { + if (dustTypeMapper.selectById(id) == null) { + throw exception(DUST_TYPE_NOT_EXISTS); + } + } + + private void validateParentDustType(String id, String parentId) { + if (parentId == null || DustTypeDO.PARENT_ID_ROOT.equals(parentId)) { + return; + } + // 1. 不能设置自己为父粉尘种类 + if (Objects.equals(id, parentId)) { + throw exception(DUST_TYPE_PARENT_ERROR); + } + // 2. 父粉尘种类不存在 + DustTypeDO parentDustType = dustTypeMapper.selectById(parentId); + if (parentDustType == null) { + throw exception(DUST_TYPE_PARENT_NOT_EXITS); + } + // 3. 递归校验父粉尘种类,如果父粉尘种类是自己的子粉尘种类,则报错,避免形成环路 + if (id == null) { // id 为空,说明新增,不需要考虑环路 + return; + } + for (int i = 0; i < Short.MAX_VALUE; i++) { + // 3.1 校验环路 + parentId = parentDustType.getParentId(); + if (Objects.equals(id, parentId)) { + throw exception(DUST_TYPE_PARENT_IS_CHILD); + } + // 3.2 继续递归下一级父粉尘种类 + if (parentId == null || DustTypeDO.PARENT_ID_ROOT.equals(parentId)) { + break; + } + parentDustType = dustTypeMapper.selectById(parentId); + if (parentDustType == null) { + break; + } + } + } + + private void validateDustTypeNameUnique(String id, String parentId, String name) { + DustTypeDO dustType = dustTypeMapper.selectByParentIdAndName(parentId, name); + if (dustType == null) { + return; + } + // 如果 id 为空,说明不用比较是否为相同 id 的粉尘种类 + if (id == null) { + throw exception(DUST_TYPE_NAME_DUPLICATE); + } + if (!Objects.equals(dustType.getId(), id)) { + throw exception(DUST_TYPE_NAME_DUPLICATE); + } + } + + @Override + public DustTypeDO getDustType(String id) { + return dustTypeMapper.selectById(id); + } + + @Override + public List getDustTypeList(DustTypeListReqVO listReqVO) { + return dustTypeMapper.selectList(listReqVO); + } + +} \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-server/src/main/resources/mapper/dusttype/DustTypeMapper.xml b/yudao-module-system/yudao-module-system-server/src/main/resources/mapper/dusttype/DustTypeMapper.xml new file mode 100644 index 000000000..a2d84e8db --- /dev/null +++ b/yudao-module-system/yudao-module-system-server/src/main/resources/mapper/dusttype/DustTypeMapper.xml @@ -0,0 +1,12 @@ + + + + + + + \ No newline at end of file