feat(修改): 多语言修改

This commit is contained in:
Bunny 2024-09-29 23:46:44 +08:00
parent 0472efb607
commit c1d4eb235e
12 changed files with 172 additions and 36 deletions

View File

@ -52,7 +52,7 @@ public class GlobalExceptionHandler {
// 匹配到内容 // 匹配到内容
String patternString = "Request method '(\\w+)' is not supported"; String patternString = "Request method '(\\w+)' is not supported";
Matcher matcher = Pattern.compile(patternString).matcher(message); Matcher matcher = Pattern.compile(patternString).matcher(message);
if (matcher.find()) return Result.error(null, 500, "请求方法错误,不是 " + matcher.group(1)); if (matcher.find()) return Result.error(null, 500, "请求方法错误,不是 " + matcher.group(1) + "类型请求");
// 请求API不存在 // 请求API不存在
String noStaticResource = "No static resource (.*)\\."; String noStaticResource = "No static resource (.*)\\.";

View File

@ -0,0 +1,29 @@
package cn.bunny.dao.dto.i18n;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Schema(name = "I18nAddDto对象", title = "多语言添加", description = "多语言添加")
public class I18nAddDto {
@Schema(name = "keyName", title = "多语言key")
@NotBlank(message = "多语言key不能为空")
private String keyName;
@Schema(name = "translation", title = "多语言翻译名称")
@NotBlank(message = "多语言翻译名称不能为空")
private String translation;
@Schema(name = "typeId", title = "多语言类型id")
private Long typeId;
}

View File

@ -14,9 +14,10 @@ import lombok.NoArgsConstructor;
public class I18nDto { public class I18nDto {
@Schema(name = "keyName", title = "多语言key") @Schema(name = "keyName", title = "多语言key")
private Integer keyName; private String keyName;
@Schema(name = "translation", title = "多语言翻译名称") @Schema(name = "translation", title = "多语言翻译名称")
private String translation; private String translation;
} }

View File

@ -0,0 +1,33 @@
package cn.bunny.dao.dto.i18n;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Schema(name = "I18nUpdateDto对象", title = "多语言更新", description = "多语言更新")
public class I18nUpdateDto {
@Schema(name = "id", title = "主键")
@NotBlank(message = "id不能为空")
private Long id;
@Schema(name = "keyName", title = "多语言key")
@NotBlank(message = "多语言key不能为空")
private String keyName;
@Schema(name = "translation", title = "多语言翻译名称")
@NotBlank(message = "多语言翻译名称不能为空")
private String translation;
@Schema(name = "typeId", title = "多语言类型id")
private Long typeId;
}

View File

@ -1,20 +1,19 @@
package cn.bunny.dao.vo.i18n; package cn.bunny.dao.vo.i18n;
import cn.bunny.dao.vo.BaseVo;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@EqualsAndHashCode(callSuper = true)
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@Builder @Builder
@Schema(name = "I18nVo对象", title = "多语言返回内容", description = "多语言返回内容") @Schema(name = "I18nVo对象", title = "多语言返回内容", description = "多语言返回内容")
public class I18nVo { public class I18nVo extends BaseVo {
@Schema(name = "keyName", title = "多语言key") @Schema(name = "keyName", title = "多语言key")
private Integer keyName; private String keyName;
@Schema(name = "translation", title = "多语言翻译名称") @Schema(name = "translation", title = "多语言翻译名称")
private String translation; private String translation;

View File

@ -1,6 +1,8 @@
package cn.bunny.services.controller; package cn.bunny.services.controller;
import cn.bunny.dao.dto.i18n.I18nAddDto;
import cn.bunny.dao.dto.i18n.I18nDto; import cn.bunny.dao.dto.i18n.I18nDto;
import cn.bunny.dao.dto.i18n.I18nUpdateDto;
import cn.bunny.dao.entity.system.MenuIcon; import cn.bunny.dao.entity.system.MenuIcon;
import cn.bunny.dao.pojo.result.PageResult; import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.pojo.result.Result; import cn.bunny.dao.pojo.result.Result;
@ -37,8 +39,8 @@ public class I18nController {
@Operation(summary = "获取多语言内容", description = "获取多语言内容") @Operation(summary = "获取多语言内容", description = "获取多语言内容")
@GetMapping("getI18n") @GetMapping("getI18n")
public Mono<Result<Map<String, Map<String, String>>>> getI18n() { public Mono<Result<Map<String, Object>>> getI18n() {
Map<String, Map<String, String>> vo = i18nService.getI18n(); Map<String, Object> vo = i18nService.getI18n();
return Mono.just(Result.success(vo)); return Mono.just(Result.success(vo));
} }
@ -57,20 +59,20 @@ public class I18nController {
@Operation(summary = "添加多语言", description = "添加多语言") @Operation(summary = "添加多语言", description = "添加多语言")
@PostMapping("addI18n") @PostMapping("addI18n")
public Mono<Result<String>> addI18n(@Valid @RequestBody I18nDto dto) { public Mono<Result<String>> addI18n(@Valid @RequestBody I18nAddDto dto) {
i18nService.addI18n(dto); i18nService.addI18n(dto);
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS)); return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS));
} }
@Operation(summary = "更新多语言", description = "更新多语言") @Operation(summary = "更新多语言", description = "更新多语言")
@PostMapping("updateI18n") @PutMapping("updateI18n")
public Mono<Result<String>> updateI18n(@Valid @RequestBody I18nDto dto) { public Mono<Result<String>> updateI18n(@Valid @RequestBody I18nUpdateDto dto) {
i18nService.updateI18n(dto); i18nService.updateI18n(dto);
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS)); return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS));
} }
@Operation(summary = "删除多语言类型", description = "删除多语言类型") @Operation(summary = "删除多语言类型", description = "删除多语言类型")
@PostMapping("deleteI18n") @DeleteMapping("deleteI18n")
public Mono<Result<String>> deleteI18n(@RequestBody List<Long> ids) { public Mono<Result<String>> deleteI18n(@RequestBody List<Long> ids) {
i18nService.deleteI18n(ids); i18nService.deleteI18n(ids);
return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS)); return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS));

View File

@ -8,6 +8,7 @@ import cn.bunny.dao.vo.i18n.I18nTypeVo;
import cn.bunny.services.service.I18nTypeService; import cn.bunny.services.service.I18nTypeService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -39,20 +40,20 @@ public class I18nTypeController {
@Operation(summary = "添加多语言类型", description = "添加多语言类型") @Operation(summary = "添加多语言类型", description = "添加多语言类型")
@PostMapping("addI18nType") @PostMapping("addI18nType")
public Mono<Result<String>> addI18nType(@RequestBody I18nTypeAddDto dto) { public Mono<Result<String>> addI18nType(@Valid @RequestBody I18nTypeAddDto dto) {
i18nTypeService.addI18nType(dto); i18nTypeService.addI18nType(dto);
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS)); return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS));
} }
@Operation(summary = "更新多语言类型", description = "更新多语言类型") @Operation(summary = "更新多语言类型", description = "更新多语言类型")
@PostMapping("updateI18nType") @PutMapping("updateI18nType")
public Mono<Result<String>> updateI18nType(@RequestBody I18nTypeUpdateDto dto) { public Mono<Result<String>> updateI18nType(@Valid @RequestBody I18nTypeUpdateDto dto) {
i18nTypeService.updateI18nType(dto); i18nTypeService.updateI18nType(dto);
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS)); return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS));
} }
@Operation(summary = "删除多语言类型", description = "删除多语言类型") @Operation(summary = "删除多语言类型", description = "删除多语言类型")
@PostMapping("deleteI18nType") @DeleteMapping("deleteI18nType")
public Mono<Result<String>> deleteI18nType(@RequestBody List<Long> ids) { public Mono<Result<String>> deleteI18nType(@RequestBody List<Long> ids) {
i18nTypeService.deleteI18nType(ids); i18nTypeService.deleteI18nType(ids);
return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS)); return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS));

View File

@ -38,4 +38,11 @@ public interface I18nMapper extends BaseMapper<I18n> {
* @return 分页结果 * @return 分页结果
*/ */
IPage<I18n> selectListByPage(@Param("page") Page<MenuIcon> pageParams, @Param("dto") I18nDto dto); IPage<I18n> selectListByPage(@Param("page") Page<MenuIcon> pageParams, @Param("dto") I18nDto dto);
/**
* 物理删除多语言
*
* @param ids 删除 id 列表
*/
void deleteBatchIdsWithPhysics(List<Long> ids);
} }

View File

@ -1,15 +1,18 @@
package cn.bunny.services.service; package cn.bunny.services.service;
import cn.bunny.dao.dto.i18n.I18nAddDto;
import cn.bunny.dao.dto.i18n.I18nDto; import cn.bunny.dao.dto.i18n.I18nDto;
import cn.bunny.dao.dto.i18n.I18nUpdateDto;
import cn.bunny.dao.entity.i18n.I18n; import cn.bunny.dao.entity.i18n.I18n;
import cn.bunny.dao.entity.system.MenuIcon; import cn.bunny.dao.entity.system.MenuIcon;
import cn.bunny.dao.pojo.result.PageResult; import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.vo.i18n.I18nVo; import cn.bunny.dao.vo.i18n.I18nVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import jakarta.validation.Valid;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* <p> * <p>
@ -26,7 +29,7 @@ public interface I18nService extends IService<I18n> {
* *
* @return 多语言返回内容 * @return 多语言返回内容
*/ */
Map<String, Map<String, String>> getI18n(); HashMap<String, Object> getI18n();
/** /**
* * 获取管理多语言列表 * * 获取管理多语言列表
@ -40,14 +43,14 @@ public interface I18nService extends IService<I18n> {
* *
* @param dto 添加表单 * @param dto 添加表单
*/ */
void addI18n(I18nDto dto); void addI18n(@Valid I18nAddDto dto);
/** /**
* * 更新多语言 * * 更新多语言
* *
* @param dto 更新表单 * @param dto 更新表单
*/ */
void updateI18n(I18nDto dto); void updateI18n(@Valid I18nUpdateDto dto);
/** /**
* * 删除多语言类型 * * 删除多语言类型

View File

@ -1,21 +1,33 @@
package cn.bunny.services.service.impl; package cn.bunny.services.service.impl;
import cn.bunny.common.service.exception.BunnyException;
import cn.bunny.dao.dto.i18n.I18nAddDto;
import cn.bunny.dao.dto.i18n.I18nDto; import cn.bunny.dao.dto.i18n.I18nDto;
import cn.bunny.dao.dto.i18n.I18nUpdateDto;
import cn.bunny.dao.entity.i18n.I18n; import cn.bunny.dao.entity.i18n.I18n;
import cn.bunny.dao.entity.i18n.I18nType;
import cn.bunny.dao.entity.i18n.I18nWithI18nType; import cn.bunny.dao.entity.i18n.I18nWithI18nType;
import cn.bunny.dao.entity.system.MenuIcon; import cn.bunny.dao.entity.system.MenuIcon;
import cn.bunny.dao.pojo.result.PageResult; import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.pojo.result.ResultCodeEnum;
import cn.bunny.dao.vo.i18n.I18nVo; import cn.bunny.dao.vo.i18n.I18nVo;
import cn.bunny.services.mapper.I18nMapper; import cn.bunny.services.mapper.I18nMapper;
import cn.bunny.services.mapper.I18nTypeMapper;
import cn.bunny.services.service.I18nService; import cn.bunny.services.service.I18nService;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -27,18 +39,37 @@ import java.util.stream.Collectors;
* @since 2024-09-28 * @since 2024-09-28
*/ */
@Service @Service
@Transactional
public class I18nServiceImpl extends ServiceImpl<I18nMapper, I18n> implements I18nService { public class I18nServiceImpl extends ServiceImpl<I18nMapper, I18n> implements I18nService {
@Autowired
private I18nTypeMapper i18nTypeMapper;
/** /**
* * 获取多语言内容 * * 获取多语言内容
* *
* @return 多语言返回内容 * @return 多语言返回内容
*/ */
@Override @Override
public Map<String, Map<String, String>> getI18n() { public HashMap<String, Object> getI18n() {
// 查找默认语言内容
I18nType i18nType = i18nTypeMapper.selectOne(Wrappers.<I18nType>lambdaQuery().eq(I18nType::getIsDefault, true));
List<I18nWithI18nType> i18nWithI18nTypes = baseMapper.selectListWithI18nType(); List<I18nWithI18nType> i18nWithI18nTypes = baseMapper.selectListWithI18nType();
return i18nWithI18nTypes.stream()
.collect(Collectors.groupingBy(I18nWithI18nType::getTypeName, Collectors.toMap(I18nWithI18nType::getKeyName, I18nWithI18nType::getSummary))); // 整理集合
Map<String, Map<String, String>> map = i18nWithI18nTypes.stream()
.collect(Collectors.groupingBy(
I18nWithI18nType::getTypeName,
Collectors.toMap(
I18nWithI18nType::getKeyName,
I18nWithI18nType::getTranslation
)
));
// 返回集合
HashMap<String, Object> hashMap = new HashMap<>(map);
hashMap.put("local", Objects.requireNonNull(i18nType.getTypeName(), "zh"));
return hashMap;
} }
/** /**
@ -69,8 +100,18 @@ public class I18nServiceImpl extends ServiceImpl<I18nMapper, I18n> implements I1
* @param dto 添加表单 * @param dto 添加表单
*/ */
@Override @Override
public void addI18n(I18nDto dto) { public void addI18n(@Valid I18nAddDto dto) {
String keyName = dto.getKeyName();
Long typeId = dto.getTypeId();
// 查询数据是否存在
List<I18n> i18nList = list(Wrappers.<I18n>lambdaQuery().eq(I18n::getKeyName, keyName).eq(I18n::getTypeId, typeId));
if (!i18nList.isEmpty()) throw new BunnyException(ResultCodeEnum.DATA_EXIST);
// 保存内容
I18n i18n = new I18n();
BeanUtils.copyProperties(dto, i18n);
save(i18n);
} }
/** /**
@ -79,8 +120,17 @@ public class I18nServiceImpl extends ServiceImpl<I18nMapper, I18n> implements I1
* @param dto 更新表单 * @param dto 更新表单
*/ */
@Override @Override
public void updateI18n(I18nDto dto) { public void updateI18n(@Valid I18nUpdateDto dto) {
Long id = dto.getId();
// 查询数据是否存在
List<I18n> i18nList = list(Wrappers.<I18n>lambdaQuery().eq(I18n::getId, id));
if (i18nList.isEmpty()) throw new BunnyException(ResultCodeEnum.DATA_NOT_EXIST);
// 保存内容
I18n i18n = new I18n();
BeanUtils.copyProperties(dto, i18n);
updateById(i18n);
} }
/** /**
@ -90,6 +140,6 @@ public class I18nServiceImpl extends ServiceImpl<I18nMapper, I18n> implements I1
*/ */
@Override @Override
public void deleteI18n(List<Long> ids) { public void deleteI18n(List<Long> ids) {
baseMapper.deleteBatchIdsWithPhysics(ids);
} }
} }

View File

@ -8,7 +8,6 @@ import cn.bunny.dao.pojo.result.ResultCodeEnum;
import cn.bunny.dao.vo.i18n.I18nTypeVo; import cn.bunny.dao.vo.i18n.I18nTypeVo;
import cn.bunny.services.mapper.I18nTypeMapper; import cn.bunny.services.mapper.I18nTypeMapper;
import cn.bunny.services.service.I18nTypeService; import cn.bunny.services.service.I18nTypeService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -52,6 +51,7 @@ public class I18nTypeServiceImpl extends ServiceImpl<I18nTypeMapper, I18nType> i
public void addI18nType(I18nTypeAddDto dto) { public void addI18nType(I18nTypeAddDto dto) {
String typeName = dto.getTypeName(); String typeName = dto.getTypeName();
Boolean isDefault = dto.getIsDefault(); Boolean isDefault = dto.getIsDefault();
I18nType i18nType = new I18nType();
// 查询添加的数据是否之前添加过 // 查询添加的数据是否之前添加过
List<I18nType> i18nTypeList = list(Wrappers.<I18nType>lambdaQuery().eq(I18nType::getTypeName, typeName)); List<I18nType> i18nTypeList = list(Wrappers.<I18nType>lambdaQuery().eq(I18nType::getTypeName, typeName));
@ -59,12 +59,12 @@ public class I18nTypeServiceImpl extends ServiceImpl<I18nTypeMapper, I18nType> i
// 如果是默认将其它内容设为false // 如果是默认将其它内容设为false
if (isDefault) { if (isDefault) {
LambdaUpdateWrapper<I18nType> lambdaUpdateWrapper = Wrappers.<I18nType>update().lambda().set(I18nType::getIsDefault, false); i18nType.setIsDefault(false);
update(null, lambdaUpdateWrapper); update(i18nType, Wrappers.<I18nType>lambdaUpdate().eq(I18nType::getIsDefault, true));
} }
// 保存数据 // 保存数据
I18nType i18nType = new I18nType(); i18nType = new I18nType();
BeanUtils.copyProperties(dto, i18nType); BeanUtils.copyProperties(dto, i18nType);
save(i18nType); save(i18nType);
} }
@ -78,6 +78,7 @@ public class I18nTypeServiceImpl extends ServiceImpl<I18nTypeMapper, I18nType> i
public void updateI18nType(I18nTypeUpdateDto dto) { public void updateI18nType(I18nTypeUpdateDto dto) {
Long id = dto.getId(); Long id = dto.getId();
Boolean isDefault = dto.getIsDefault(); Boolean isDefault = dto.getIsDefault();
I18nType i18nType = new I18nType();
// 查询更新的内容是否存在 // 查询更新的内容是否存在
List<I18nType> i18nTypeList = list(Wrappers.<I18nType>lambdaQuery().eq(I18nType::getId, id)); List<I18nType> i18nTypeList = list(Wrappers.<I18nType>lambdaQuery().eq(I18nType::getId, id));
@ -85,12 +86,12 @@ public class I18nTypeServiceImpl extends ServiceImpl<I18nTypeMapper, I18nType> i
// 如果是默认将其它内容设为false // 如果是默认将其它内容设为false
if (isDefault) { if (isDefault) {
LambdaUpdateWrapper<I18nType> lambdaUpdateWrapper = Wrappers.<I18nType>update().lambda().set(I18nType::getIsDefault, false); i18nType.setIsDefault(false);
update(null, lambdaUpdateWrapper); update(i18nType, Wrappers.<I18nType>lambdaUpdate().eq(I18nType::getIsDefault, true));
} }
// 更新内容 // 更新内容
I18nType i18nType = new I18nType(); i18nType = new I18nType();
BeanUtils.copyProperties(dto, i18nType); BeanUtils.copyProperties(dto, i18nType);
updateById(i18nType); updateById(i18nType);
} }

View File

@ -20,6 +20,16 @@
id, key_name, translation, type_id, create_user, update_user, update_time, create_time, is_deleted id, key_name, translation, type_id, create_user, update_user, update_time, create_time, is_deleted
</sql> </sql>
<!-- 物理删除多语言 -->
<delete id="deleteBatchIdsWithPhysics">
delete
from sys_i18n
where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
<!-- 多语言和多语言类型 --> <!-- 多语言和多语言类型 -->
<select id="selectListWithI18nType" resultType="cn.bunny.dao.entity.i18n.I18nWithI18nType"> <select id="selectListWithI18nType" resultType="cn.bunny.dao.entity.i18n.I18nWithI18nType">
select i18n.*, i18n_type.type_name select i18n.*, i18n_type.type_name