标签管理完善

This commit is contained in:
温文静WWW 2025-07-18 15:18:45 +08:00
parent 698fcc90c1
commit 593c534cfc
7 changed files with 82 additions and 67 deletions

View File

@ -1,6 +1,7 @@
package com.aircraft.modules.article.controller;
import com.aircraft.modules.article.domain.CpLabel;
import com.aircraft.modules.article.domain.dto.CpLabelDTO;
import com.aircraft.modules.article.domain.dto.CpLabelVo;
import com.aircraft.modules.article.mapper.CpLabelMapper;
import com.aircraft.modules.article.service.CpLabelService;
@ -52,17 +53,16 @@ public class CpLabelController {
@Autowired
private CpModuleService cpModuleService;
@ApiOperation(value = "分页查询标签", notes = "分页查询标签")
@ApiOperation(value = "分页查询标签(name模糊查询)", notes = "分页查询标签")
@RequestMapping(method = RequestMethod.GET)
@ApiImplicitParams({
@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
@ApiImplicitParam(name = "current", value = "当前页面从1开始", paramType = "query")
@ApiImplicitParam(name = "current", value = "当前页面从1开始", paramType = "query"),
@ApiImplicitParam(name = "name", value = "name进行模糊查询", paramType = "query")
})
public ResponseEntity<IPage<CpLabel>> findByPage(final CpLabel example, final Page page) {
public ResponseEntity<IPage<CpLabelVo>> findByPage(final Page<CpLabelVo> page, @RequestParam(required = false) String name) {
try {
QueryWrapper<CpLabel> wrapper = new QueryWrapper<>(example);
// 正确调用page方法(分页对象, 查询条件)
IPage<CpLabel> records = entityService.page(page, wrapper);
IPage<CpLabelVo> records = entityService.findByPage(page, name);
return new ResponseEntity<>(records, HttpStatus.OK);
} catch (Exception e) {
LOG.error("分页查询标签时出错", e);
@ -86,7 +86,6 @@ public class CpLabelController {
success.put("code", 200);
success.put("msg", "删除成功");
return ResponseEntity.ok(success);
// return ResponseEntity.noContent().build();
} catch (DataIntegrityViolationException e) {
LOG.error("删除标签失败: 数据完整性冲突", e);
return ResponseEntity.status(HttpStatus.CONFLICT).build();
@ -114,7 +113,7 @@ public class CpLabelController {
@ApiOperation(value = "添加标签")
@RequestMapping(method = {RequestMethod.POST})
public ResponseEntity<?> add(@Valid @RequestBody final CpLabelVo cpLabelVo, final BindingResult result) {
public ResponseEntity<?> add(@Valid @RequestBody final CpLabelDTO cpLabelDTO, final BindingResult result) {
try {
if (result.hasErrors()) {
String errorMsg = result.getFieldErrors().stream()
@ -125,14 +124,14 @@ public class CpLabelController {
}
// 检查模块是否存在
Integer moduleId = cpLabelVo.getModuleId();
Integer moduleId = cpLabelDTO.getModuleId();
if (moduleId == null || !cpModuleService.existsById(moduleId)) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(Map.of("message", "不存在该模块"));
}
CpLabel cpLabel = new CpLabel();
BeanUtils.copyProperties(cpLabelVo, cpLabel);
BeanUtils.copyProperties(cpLabelDTO, cpLabel);
// 设置默认值
cpLabel.setCreateTime(LocalDateTime.now());
cpLabel.setDelFlag(0);
@ -143,7 +142,7 @@ public class CpLabelController {
// 返回201 Created状态和创建的资源
return ResponseEntity
.created(URI.create("/cpLabel/" + cpLabel.getId()))
.body(cpLabelVo);
.body(cpLabelDTO);
} catch (Exception e) {
LOG.error("添加标签失败", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
@ -185,7 +184,7 @@ public class CpLabelController {
entityService.updateById(entity);
// 成功响应
return ResponseEntity.ok(Map.of("code", 200, "msg", "修改标签成功"));
return ResponseEntity.ok(Map.of("code", 200, "msg", "修改标签成功"));
// return ResponseEntity.ok(entity);
} catch (Exception e) {

View File

@ -0,0 +1,33 @@
package com.aircraft.modules.article.domain.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
@ApiModel
public class CpLabelDTO {
@ApiModelProperty("模块id")
private Integer moduleId;
@ApiModelProperty("标签")
private String name;
@ApiModelProperty("标签id")
private Integer id;
@ApiModelProperty(value = "标签备注")
private String remark;
@ApiModelProperty(value = "排序号")
private Integer orderNum;
@ApiModelProperty("父标签名称,可为null")
private Integer parentId;
@ApiModelProperty("子标签列表")
private List<CpLabelDTO> children;
}

View File

@ -1,34 +1,9 @@
package com.aircraft.modules.article.domain.dto;
import com.aircraft.modules.article.domain.CpLabel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
@ApiModel
public class CpLabelVo {
@ApiModelProperty("模块id")
private Integer moduleId;
@ApiModelProperty("标签")
private String name;
@ApiModelProperty("标签id")
private Integer id;
@ApiModelProperty(value = "标签备注")
private String remark;
@ApiModelProperty(value = "排序号")
private Integer orderNum;
@ApiModelProperty("父标签名称,可为null")
private Integer parentId;
@ApiModelProperty("子标签列表")
private List<CpLabelVo> children;
public class CpLabelVo extends CpLabel {
private String moduleName;//模块名
}

View File

@ -1,8 +1,11 @@
package com.aircraft.modules.article.mapper;
import com.aircraft.modules.article.domain.CpLabel;
import com.aircraft.modules.article.domain.dto.CpLabelDTO;
import com.aircraft.modules.article.domain.dto.CpLabelVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@ -19,7 +22,7 @@ import java.util.List;
@Mapper
public interface CpLabelMapper extends BaseMapper<CpLabel> {
@Select("SELECT * FROM cp_label WHERE name = #{labelName}")
CpLabelVo findByName(String labelName);
CpLabelDTO findByName(String labelName);
List<CpLabel> selectLabelTreeByModuleId(Integer moduleId);
@ -27,4 +30,6 @@ public interface CpLabelMapper extends BaseMapper<CpLabel> {
@Select("SELECT * FROM cp_label WHERE id = #{labelId}")
CpLabel findById(int labelId);
IPage<CpLabelVo> selectVoPage(Page<CpLabelVo> page, String name);
}

View File

@ -2,10 +2,10 @@ package com.aircraft.modules.article.service;
import com.aircraft.modules.article.domain.CpLabel;
import com.aircraft.modules.article.domain.dto.CpLabelVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* <p>
* 标签表 服务类
@ -15,15 +15,7 @@ import java.util.List;
* @since 2025-07-10
*/
public interface CpLabelService extends IService<CpLabel> {
/**
* 检查标签名称在模块内是否唯一
* @param moduleId 模块ID
* @param labelName 标签名称
* @param excludeId 排除的标签ID更新时使用
* @return 是否唯一
*/
boolean isLabelNameUnique(Integer moduleId, String labelName, Integer excludeId);
void updateDelFlagById(Integer id, Integer delFlag);
IPage<CpLabelVo> findByPage(Page<CpLabelVo> page, String name);
}

View File

@ -1,18 +1,17 @@
package com.aircraft.modules.article.service.impl;
import com.aircraft.modules.article.domain.CpLabel;
import com.aircraft.modules.article.domain.dto.CpArticleVo;
import com.aircraft.modules.article.domain.dto.CpLabelVo;
import com.aircraft.modules.article.mapper.CpLabelMapper;
import com.aircraft.modules.article.service.CpLabelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* <p>
* 标签表 服务实现类
@ -23,19 +22,17 @@ import java.util.Map;
*/
@Service
public class CpLabelServiceImpl extends ServiceImpl<CpLabelMapper, CpLabel> implements CpLabelService {
@Override
public boolean isLabelNameUnique(Integer moduleId, String labelName, Integer excludeId) {
CpLabel label = baseMapper.selectOne(new QueryWrapper<CpLabel>()
.eq("module_id", moduleId)
.eq("name", labelName)
.eq("del_flag", 0)
.ne(excludeId != null, "id", excludeId));
return label == null;
}
@Autowired
private CpLabelMapper cpLabelMapper;
@Override
public void updateDelFlagById(Integer id, Integer delFlag) {
baseMapper.updateDelFlagById(id, delFlag);
}
@Override
public IPage<CpLabelVo> findByPage(Page<CpLabelVo> page, String name) {
IPage<CpLabelVo> cpLabelVoIPage = cpLabelMapper.selectVoPage(page, name);
return cpLabelVoIPage;
}
}

View File

@ -14,4 +14,18 @@
WHERE id = #{id}
AND del_flag = 0 <!-- 确保只更新未删除的记录 -->
</update>
<!--分页查询标签可进行name模糊查询-->
<select id="selectVoPage" resultType="com.aircraft.modules.article.domain.dto.CpLabelVo">
SELECT
l.*,
m.id AS moduleId, -- 映射模块ID
m.module_name AS moduleName -- 映射模块名称
FROM cp_label l
LEFT JOIN cp_module m ON l.module_id = m.id -- 添加模块表关联
WHERE l.del_flag = 0
<if test="name != null and name != ''">
AND l.name LIKE CONCAT('%', #{name}, '%')
</if>
ORDER BY create_time DESC
</select>
</mapper>