feature: tag功能.

This commit is contained in:
lijiahang
2023-09-05 17:30:37 +08:00
parent 8035396995
commit db9afe3866
25 changed files with 741 additions and 30 deletions

View File

@@ -28,4 +28,6 @@ public class Const implements com.orion.lang.constant.Const {
public static final Integer DEFAULT_SORT = 10;
public static final Long NONE_ID = -1L;
}

View File

@@ -15,6 +15,7 @@ import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* redis 工具类
@@ -23,6 +24,7 @@ import java.util.function.Function;
* @version 1.0.0
* @since 2021/11/6 11:09
*/
@SuppressWarnings("unchecked")
public class RedisUtils {
private static RedisTemplate<String, String> redisTemplate;
@@ -80,7 +82,6 @@ public class RedisUtils {
* @param processor processor
* @param <T> type
*/
@SuppressWarnings("unchecked")
public static <T> void processSetJson(String key, CacheKeyDefine define, Consumer<T> processor) {
String value = redisTemplate.opsForValue().get(key);
if (value == null) {
@@ -167,6 +168,24 @@ public class RedisUtils {
return Lists.map(elements, mapper);
}
/**
* 查询 list 区间
*
* @param key key
* @param define define
* @return list
*/
public static <T> List<T> listRangeJson(String key, CacheKeyDefine define) {
// 查询列表
List<String> elements = redisTemplate.opsForList().range(key, 0, -1);
if (elements == null) {
return Lists.newList();
}
return (List<T>) elements.stream()
.map(s -> JSON.parseObject(s, define.getType()))
.collect(Collectors.toList());
}
/**
* list 添加元素
*
@@ -179,6 +198,66 @@ public class RedisUtils {
redisTemplate.opsForList().rightPushAll(key, Lists.map(list, mapper));
}
/**
* list 添加元素
*
* @param key key
* @param list list
* @param <T> T
*/
public static <T> void listPushAllJson(String key, List<T> list) {
List<String> values = list.stream()
.map(JSON::toJSONString)
.collect(Collectors.toList());
redisTemplate.opsForList().rightPushAll(key, values);
}
/**
* list 添加元素
*
* @param key key
* @param value value
* @param mapper mapper
* @param <T> T
*/
public static <T> void listPush(String key, T value, Function<T, String> mapper) {
redisTemplate.opsForList().rightPush(key, mapper.apply(value));
}
/**
* list 添加元素
*
* @param key key
* @param value value
* @param <T> T
*/
public static <T> void listPushJson(String key, T value) {
redisTemplate.opsForList().rightPush(key, JSON.toJSONString(value));
}
/**
* list 删除元素
*
* @param key key
* @param value value
* @param mapper mapper
* @param <T> T
*/
public static <T> void listRemove(String key, T value, Function<T, String> mapper) {
redisTemplate.opsForList().remove(key, 1, mapper.apply(value));
}
/**
* list 删除元素
*
* @param key key
* @param value value
* @param <T> T
*/
public static <T> void listRemoveJson(String key, T value) {
redisTemplate.opsForList().remove(key, 1, JSON.toJSONString(value));
}
/**
* 设置过期时间
*

View File

@@ -44,7 +44,7 @@ public class CodeGenerator {
// new GenTable("system_user", "用户", "user")
// .vue("user", "user")
// .enums(UserStatusEnum.class),
new GenTable("favorite", "收藏", "favorite"),
new GenTable("tag", "标签枚举", "tag"),
};
// jdbc 配置 - 使用配置文件
File yamlFile = new File("orion-ops-launch/src/main/resources/application-dev.yaml");

View File

@@ -59,7 +59,7 @@ public class ${table.controllerName} {
@PutMapping("/update")
@Operation(summary = "${apiComment.updateById}")
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:update')")
public Integer update${type}(@Validated @RequestBody(Id.class) ${type}UpdateRequest request) {
public Integer update${type}(@Validated(Id.class) @RequestBody ${type}UpdateRequest request) {
return ${typeLower}Service.update${type}ById(request);
}

View File

@@ -29,7 +29,7 @@ public interface ${type}Convert {
${type}VO to(${type}DO domain);
${type}Export export(${type}DO domain);
${type}Export toExport(${type}DO domain);
List<${type}VO> to(List<${type}DO> list);

View File

@@ -1,8 +1,8 @@
package ${package.Mapper};
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import ${package.Entity}.${entity};
import ${superMapperClassPackage};
import ${package.Entity}.${entity};
#if(${mapperAnnotationClass})
import ${mapperAnnotationClass.name};
#end

View File

@@ -41,6 +41,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
@Override
public Long create${type}(${type}CreateRequest request) {
log.info("${type}Service-create${type} record: {}", JSON.toJSONString(record));
// 转换
${type}DO record = ${type}Convert.MAPPER.to(request);
record.setId(null);
@@ -48,12 +49,13 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
this.check${type}Present(record);
// 插入
int effect = ${typeLower}DAO.insert(record);
log.info("${type}Service-create${type} effect: {}, record: {}", effect, JSON.toJSONString(record));
log.info("${type}Service-create${type} effect: {}", effect);
return record.getId();
}
@Override
public Integer update${type}ById(${type}UpdateRequest request) {
log.info("${type}Service-update${type}ById updateRecord: {}", JSON.toJSONString(updateRecord));
// 查询
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
${type}DO record = ${typeLower}DAO.selectById(id);
@@ -64,7 +66,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
this.check${type}Present(updateRecord);
// 更新
int effect = ${typeLower}DAO.updateById(updateRecord);
log.info("${type}Service-update${type}ById effect: {}, updateRecord: {}", effect, JSON.toJSONString(updateRecord));
log.info("${type}Service-update${type}ById effect: {}", effect);
return effect;
}
@@ -132,15 +134,17 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
@Override
public Integer delete${type}ById(Long id) {
log.info("${type}Service-delete${type}ById id: {}", id);
int effect = ${typeLower}DAO.deleteById(id);
log.info("${type}Service-delete${type}ById id: {}, effect: {}", id, effect);
log.info("${type}Service-delete${type}ById effect: {}", effect);
return effect;
}
@Override
public Integer batchDelete${type}ByIdList(List<Long> idList) {
log.info("${type}Service-batchDelete${type}ByIdList idList: {}", idList);
int effect = ${typeLower}DAO.deleteBatchIds(idList);
log.info("${type}Service-batchDelete${type}ByIdList idList: {}, effect: {}", idList, effect);
log.info("${type}Service-batchDelete${type}ByIdList effect: {}", effect);
return effect;
}
@@ -157,12 +161,14 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
@Override
public void export${type}(${type}QueryRequest request, HttpServletResponse response) throws IOException {
log.info("${type}Service.export${type} request: {}", JSON.toJSONString(request));
// 条件
LambdaQueryWrapper<${type}DO> wrapper = this.buildQueryWrapper(request);
// 查询
List<${type}Export> rows = ${typeLower}DAO.of()
.wrapper(wrapper)
.list(${type}Convert.MAPPER::export);
.list(${type}Convert.MAPPER::toExport);
log.info("${type}Service.export${type} size: {}", rows.size());
// 导出
ByteArrayOutputStream out = new ByteArrayOutputStream();
ExcelExport.create(${type}Export.class)

View File

@@ -1,4 +1,4 @@
package ${package.ServiceImpl};
package ${currentPackage};
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -126,7 +126,9 @@ public class ${type}ApiImpl implements ${type}Api {
log.info("${type}Api.delete${type}ById id: {}", id);
Valid.notNull(id, ErrorMessage.ID_MISSING);
// 删除
return ${typeLower}DAO.deleteById(id);
int effect = ${typeLower}DAO.deleteById(id);
log.info("${type}Api.delete${type}ById effect: {}", effect);
return effect;
}
@Override
@@ -134,7 +136,9 @@ public class ${type}ApiImpl implements ${type}Api {
log.info("${type}Api.batchDelete${type}ByIdList idList: {}", idList);
Valid.notEmpty(idList, ErrorMessage.ID_MISSING);
// 删除
return ${typeLower}DAO.deleteBatchIds(idList);
int effect = ${typeLower}DAO.deleteBatchIds(idList);
log.info("${type}Api.batchDelete${type}ByIdList effect: {}", effect);
return effect;
}
@Override
@@ -144,7 +148,9 @@ public class ${type}ApiImpl implements ${type}Api {
// 条件
LambdaQueryWrapper<${type}DO> wrapper = this.buildQueryWrapper(dto);
// 删除
return ${typeLower}DAO.delete(wrapper);
int effect = ${typeLower}DAO.delete(wrapper);
log.info("${type}Api.delete${type} effect: {}", effect);
return effect;
}
/**

View File

@@ -1,6 +1,5 @@
package ${currentPackage};
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.test.core.base.BaseUnitTest;
import com.orion.ops.framework.test.core.utils.EntityRandoms;
@@ -53,44 +52,63 @@ public class ${type}ApiImplTests extends BaseUnitTest {
@Test
@Order(3)
public void update${type}Test() {
${type}QueryDTO query = new ${type}QueryDTO();
query.setId(lastId);
${type}UpdateDTO req = EntityRandoms.random(${type}UpdateDTO.class);
req.setId(null);
Integer effect = ${typeLower}Api.update${type}(query, req);
assertEquals(effect, 1);
}
@Test
@Order(4)
public void get${type}ByIdTest() {
${type}DTO row = ${typeLower}Api.get${type}ById(lastId);
assertNotNull(row);
}
@Test
@Order(4)
@Order(5)
public void get${type}ByIdListTest() {
List<${type}DTO> rows = ${typeLower}Api.get${type}ByIdList(Lists.of(lastId));
assertFalse(rows.isEmpty());
}
@Test
@Order(5)
@Order(6)
public void get${type}ListTest() {
List<${type}DTO> rows = ${typeLower}Api.get${type}List(new ${type}QueryDTO());
assertFalse(rows.isEmpty());
}
@Test
@Order(6)
@Order(7)
public void get${type}CountTest() {
Long count = ${typeLower}Api.get${type}Count(new ${type}QueryDTO());
assertEquals(count, 1L);
}
@Test
@Order(7)
@Order(8)
public void delete${type}ByIdTest() {
Integer effect = ${typeLower}Api.delete${type}ById(lastId);
assertEquals(effect, 1);
}
@Test
@Order(8)
@Order(9)
public void batchDelete${type}ByIdListTest() {
Integer effect = ${typeLower}Api.batchDelete${type}ByIdList(Lists.of(lastId));
assertEquals(effect, 0);
}
@Test
@Order(10)
public void delete${type}Test() {
${type}QueryDTO dto = new ${type}QueryDTO();
Integer effect = ${typeLower}Api.delete${type}(dto);
assertEquals(effect, 0);
}
}

View File

@@ -53,34 +53,45 @@ public class ${type}ServiceImplTests extends BaseUnitTest {
@Test
@Order(3)
public void update${type}Test() {
${type}QueryRequest query = new ${type}QueryRequest();
query.setId(lastId);
${type}UpdateRequest req = EntityRandoms.random(${type}UpdateRequest.class);
req.setId(null);
Integer effect = ${typeLower}Service.update${type}(query, req);
assertEquals(effect, 1);
}
@Test
@Order(4)
public void get${type}ByIdTest() {
${type}VO row = ${typeLower}Service.get${type}ById(lastId);
assertNotNull(row);
}
@Test
@Order(4)
@Order(5)
public void get${type}ByIdListTest() {
List<${type}VO> rows = ${typeLower}Service.get${type}ByIdList(Lists.of(lastId));
assertFalse(rows.isEmpty());
}
@Test
@Order(5)
@Order(6)
public void get${type}ListTest() {
List<${type}VO> rows = ${typeLower}Service.get${type}List(new ${type}QueryRequest());
assertFalse(rows.isEmpty());
}
@Test
@Order(6)
@Order(7)
public void get${type}CountTest() {
Long count = ${typeLower}Service.get${type}Count(new ${type}QueryRequest());
assertEquals(count, 1L);
}
@Test
@Order(7)
@Order(8)
public void get${type}PageTest() {
${type}QueryRequest request = new ${type}QueryRequest();
request.setPage(1);
@@ -90,17 +101,25 @@ public class ${type}ServiceImplTests extends BaseUnitTest {
}
@Test
@Order(8)
@Order(9)
public void delete${type}ByIdTest() {
Integer effect = ${typeLower}Service.delete${type}ById(lastId);
assertEquals(effect, 1);
}
@Test
@Order(9)
@Order(10)
public void batchDelete${type}ByIdListTest() {
Integer effect = ${typeLower}Service.batchDelete${type}ByIdList(Lists.of(lastId));
assertEquals(effect, 0);
}
@Test
@Order(11)
public void delete${type}Test() {
${type}QueryRequest query = new ${type}QueryRequest();
Integer effect = ${typeLower}Service.delete${type}(query);
assertEquals(effect, 0);
}
}

View File

@@ -69,17 +69,17 @@ public class FavoriteApiImpl implements FavoriteApi {
request.setUserId(userId);
request.setType(typeName);
cacheRelIdList = favoriteService.getFavoriteRelIdList(request);
// 设置 -1 到缓存防止穿透
// 添加默认值 防止穿透
if (cacheRelIdList.isEmpty()) {
cacheRelIdList.add(Const.L_N_1);
cacheRelIdList.add(Const.NONE_ID);
}
// 设置缓存
RedisUtils.listPushAll(key, cacheRelIdList, String::valueOf);
// 设置过期时间
RedisUtils.setExpire(key, FavoriteCacheKeyDefine.FAVORITE);
}
// 尝试删除防止穿透的 key
cacheRelIdList.remove(Const.L_N_1);
// 删除防止穿透的 key
cacheRelIdList.remove(Const.NONE_ID);
return CompletableFuture.completedFuture(cacheRelIdList);
}

View File

@@ -0,0 +1,23 @@
### 创建标签枚举
POST {{baseUrl}}/infra/tag/create
Content-Type: application/json
Authorization: {{token}}
{
"name": "TAG1",
"type": "HOST"
}
### 查询标签枚举
GET {{baseUrl}}/infra/tag/list?type=HOST
Content-Type: application/json
Authorization: {{token}}
### 通过 id 删除标签枚举
DELETE {{baseUrl}}/infra/tag/delete?id=2
Authorization: {{token}}
###

View File

@@ -0,0 +1,64 @@
package com.orion.ops.module.infra.controller;
import com.orion.ops.framework.common.annotation.IgnoreLog;
import com.orion.ops.framework.common.annotation.RestWrapper;
import com.orion.ops.framework.common.constant.IgnoreLogMode;
import com.orion.ops.module.infra.entity.request.tag.TagCreateRequest;
import com.orion.ops.module.infra.entity.vo.TagVO;
import com.orion.ops.module.infra.service.TagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 标签枚举 api
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-9-5 11:58
*/
@Tag(name = "infra - 标签枚举服务")
@Slf4j
@Validated
@RestWrapper
@RestController
@RequestMapping("/infra/tag")
@SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
public class TagController {
@Resource
private TagService tagService;
@PostMapping("/create")
@Operation(summary = "创建标签枚举")
@PreAuthorize("@ss.hasPermission('infra:tag:create')")
public Long createTag(@Validated @RequestBody TagCreateRequest request) {
return tagService.createTag(request);
}
@IgnoreLog(IgnoreLogMode.RET)
@GetMapping("/list")
@Operation(summary = "查询标签枚举")
@Parameter(name = "type", description = "type", required = true)
@PreAuthorize("@ss.hasPermission('infra:tag:query')")
public List<TagVO> getTagListAll(@RequestParam("type") String type) {
return tagService.getTagList(type);
}
@DeleteMapping("/delete")
@Operation(summary = "通过 id 删除标签枚举")
@Parameter(name = "id", description = "id", required = true)
@PreAuthorize("@ss.hasPermission('infra:tag:delete')")
public Integer deleteTag(@RequestParam("id") Long id) {
return tagService.deleteTagById(id);
}
}

View File

@@ -0,0 +1,37 @@
package com.orion.ops.module.infra.convert;
import com.orion.ops.module.infra.entity.domain.TagDO;
import com.orion.ops.module.infra.entity.dto.TagCacheDTO;
import com.orion.ops.module.infra.entity.request.tag.TagCreateRequest;
import com.orion.ops.module.infra.entity.request.tag.TagQueryRequest;
import com.orion.ops.module.infra.entity.vo.TagVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 标签枚举 内部对象转换器
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-9-5 11:58
*/
@Mapper
public interface TagConvert {
TagConvert MAPPER = Mappers.getMapper(TagConvert.class);
TagDO to(TagCreateRequest request);
TagDO to(TagQueryRequest request);
TagVO to(TagDO domain);
TagCacheDTO toCache(TagDO domain);
List<TagVO> to(List<TagDO> list);
List<TagVO> toList(List<TagCacheDTO> cache);
}

View File

@@ -0,0 +1,31 @@
package com.orion.ops.module.infra.dao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.ops.framework.mybatis.core.mapper.IMapper;
import com.orion.ops.module.infra.entity.domain.TagDO;
import org.apache.ibatis.annotations.Mapper;
/**
* 标签枚举 Mapper 接口
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-9-5 11:58
*/
@Mapper
public interface TagDAO extends IMapper<TagDO> {
/**
* 获取查询条件
*
* @param entity entity
* @return 查询条件
*/
default LambdaQueryWrapper<TagDO> queryCondition(TagDO entity) {
return this.wrapper()
.eq(TagDO::getId, entity.getId())
.eq(TagDO::getName, entity.getName())
.eq(TagDO::getType, entity.getType());
}
}

View File

@@ -0,0 +1,25 @@
package com.orion.ops.module.infra.define;
import com.orion.lang.define.cache.CacheKeyBuilder;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.ops.module.infra.entity.dto.TagCacheDTO;
import java.util.concurrent.TimeUnit;
/**
* tag 服务缓存 key
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/9/5 15:36
*/
public interface TagCacheKeyDefine {
CacheKeyDefine TAG_NAME = new CacheKeyBuilder()
.key("tag:{}")
.desc("tag名称 ${type}")
.type(TagCacheDTO.class)
.timeout(3, TimeUnit.DAYS)
.build();
}

View File

@@ -0,0 +1,41 @@
package com.orion.ops.module.infra.entity.domain;
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 com.orion.ops.framework.mybatis.core.domain.BaseDO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
/**
* 标签枚举 实体对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-9-5 11:58
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName(value = "tag", autoResultMap = true)
@Schema(name = "TagDO", description = "标签枚举 实体对象")
public class TagDO extends BaseDO {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@Schema(description = "标签名称")
@TableField("name")
private String name;
@Schema(description = "标签类型")
@TableField("type")
private String type;
}

View File

@@ -0,0 +1,31 @@
package com.orion.ops.module.infra.entity.dto;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
/**
* @author Jiahang Li
* @version 1.0.0
* @since 2023/9/5 15:37
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Schema(name = "TagCacheDTO", description = "菜单 缓存业务对象")
public class TagCacheDTO {
@EqualsAndHashCode.Include
@Schema(description = "id")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@Schema(description = "标签名称")
@TableField("name")
private String name;
}

View File

@@ -0,0 +1,37 @@
package com.orion.ops.module.infra.entity.request.tag;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 标签枚举 创建请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-9-5 11:58
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "TagCreateRequest", description = "标签枚举 创建请求对象")
public class TagCreateRequest implements Serializable {
@NotBlank
@Size(max = 32)
@Schema(description = "标签名称")
private String name;
@NotBlank
@Size(max = 12)
@Schema(description = "标签类型")
private String type;
}

View File

@@ -0,0 +1,28 @@
package com.orion.ops.module.infra.entity.request.tag;
import com.orion.ops.framework.common.entity.PageRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import javax.validation.constraints.Size;
/**
* 标签枚举 查询请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-9-5 11:58
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Schema(name = "TagQueryRequest", description = "标签枚举 查询请求对象")
public class TagQueryRequest extends PageRequest {
@Size(max = 12)
@Schema(description = "标签类型")
private String type;
}

View File

@@ -0,0 +1,42 @@
package com.orion.ops.module.infra.entity.request.tag;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 标签枚举 更新请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-9-5 11:58
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "TagUpdateRequest", description = "标签枚举 更新请求对象")
public class TagUpdateRequest implements Serializable {
@NotNull
@Schema(description = "id")
private Long id;
@NotBlank
@Size(max = 32)
@Schema(description = "标签名称")
private String name;
@NotBlank
@Size(max = 12)
@Schema(description = "标签类型")
private String type;
}

View File

@@ -0,0 +1,33 @@
package com.orion.ops.module.infra.entity.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 标签枚举 视图响应对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-9-5 11:58
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "TagVO", description = "标签枚举 视图响应对象")
public class TagVO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "标签名称")
private String name;
}

View File

@@ -0,0 +1,49 @@
package com.orion.ops.module.infra.service;
import com.orion.ops.module.infra.entity.request.tag.TagCreateRequest;
import com.orion.ops.module.infra.entity.vo.TagVO;
import java.util.List;
/**
* 标签枚举 服务类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-9-5 11:58
*/
public interface TagService {
/**
* 创建标签枚举
*
* @param request request
* @return id
*/
Long createTag(TagCreateRequest request);
/**
* 查询标签枚举
*
* @param type type
* @return rows
*/
List<TagVO> getTagList(String type);
/**
* 通过 id 删除标签枚举
*
* @param id id
* @return effect
*/
Integer deleteTagById(Long id);
/**
* 通过 id 删除标签枚举
*
* @param idList idList
* @return effect
*/
Integer deleteTagByIdList(List<Long> idList);
}

View File

@@ -0,0 +1,118 @@
package com.orion.ops.module.infra.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.mybatis.core.query.Conditions;
import com.orion.ops.framework.redis.core.utils.RedisUtils;
import com.orion.ops.module.infra.convert.TagConvert;
import com.orion.ops.module.infra.dao.TagDAO;
import com.orion.ops.module.infra.define.TagCacheKeyDefine;
import com.orion.ops.module.infra.entity.domain.TagDO;
import com.orion.ops.module.infra.entity.dto.TagCacheDTO;
import com.orion.ops.module.infra.entity.request.tag.TagCreateRequest;
import com.orion.ops.module.infra.entity.vo.TagVO;
import com.orion.ops.module.infra.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* 标签枚举 服务实现类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-9-5 11:58
*/
@Slf4j
@Service
public class TagServiceImpl implements TagService {
@Resource
private TagDAO tagDAO;
@Override
public Long createTag(TagCreateRequest request) {
// 转换
TagDO record = TagConvert.MAPPER.to(request);
record.setId(null);
// 查询 tag 是否存在
String type = record.getType();
LambdaQueryWrapper<TagDO> wrapper = tagDAO.wrapper()
.eq(TagDO::getName, record.getName())
.eq(TagDO::getType, type);
TagDO checkTag = tagDAO.of(wrapper)
.only()
.get();
if (checkTag != null) {
return checkTag.getId();
}
// 插入
int effect = tagDAO.insert(record);
log.info("TagService-createTag effect: {}, record: {}", effect, JSON.toJSONString(record));
// 设置缓存
String cacheKey = TagCacheKeyDefine.TAG_NAME.format(type);
TagCacheDTO cache = TagConvert.MAPPER.toCache(record);
RedisUtils.listPushJson(cacheKey, cache);
RedisUtils.setExpire(cacheKey, TagCacheKeyDefine.TAG_NAME);
return record.getId();
}
@Override
public List<TagVO> getTagList(String type) {
// 查询缓存
String cacheKey = TagCacheKeyDefine.TAG_NAME.format(type);
List<TagCacheDTO> cacheValues = RedisUtils.listRangeJson(cacheKey, TagCacheKeyDefine.TAG_NAME);
if (cacheValues.isEmpty()) {
// 为空则需要查询缓存
LambdaQueryWrapper<TagDO> wrapper = Conditions.eq(TagDO::getType, type);
cacheValues = tagDAO.of(wrapper).list(TagConvert.MAPPER::toCache);
// 添加默认值 防止穿透
if (cacheValues.isEmpty()) {
cacheValues.add(TagCacheDTO.builder().id(Const.NONE_ID).build());
}
// 设置到缓存
RedisUtils.listPushAllJson(cacheKey, cacheValues);
RedisUtils.setExpire(cacheKey, TagCacheKeyDefine.TAG_NAME);
}
// 删除防止穿透的 key
cacheValues.removeIf(s -> Const.NONE_ID.equals(s.getId()));
// 转换
return TagConvert.MAPPER.toList(cacheValues);
}
@Override
public Integer deleteTagById(Long id) {
TagDO tag = tagDAO.selectById(id);
if (tag == null) {
return Const.N_0;
}
// 删除数据库
int effect = tagDAO.deleteById(id);
log.info("TagService-deleteTagById id: {}, effect: {}", id, effect);
// 删除缓存
String cacheKey = TagCacheKeyDefine.TAG_NAME.format(tag.getType());
RedisUtils.listRemoveJson(cacheKey, TagConvert.MAPPER.toCache(tag));
return effect;
}
@Override
public Integer deleteTagByIdList(List<Long> idList) {
List<TagDO> tagList = tagDAO.selectBatchIds(idList);
if (tagList.isEmpty()) {
return Const.N_0;
}
// 删除数据库
int effect = tagDAO.deleteBatchIds(idList);
log.info("TagService-deleteTagByIdList idList: {}, effect: {}", idList, effect);
// 删除缓存
for (TagDO tag : tagList) {
String cacheKey = TagCacheKeyDefine.TAG_NAME.format(tag.getType());
RedisUtils.listRemoveJson(cacheKey, TagConvert.MAPPER.toCache(tag));
}
return effect;
}
}

View File

@@ -0,0 +1,22 @@
<?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.orion.ops.module.infra.dao.TagDAO">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.orion.ops.module.infra.entity.domain.TagDO">
<id column="id" property="id"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<result column="creator" property="creator"/>
<result column="updater" property="updater"/>
<result column="deleted" property="deleted"/>
<result column="name" property="name"/>
<result column="type" property="type"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, name, type, create_time, update_time, creator, updater, deleted
</sql>
</mapper>