mirror of https://gitee.com/stylefeng/roses
【Translation】多语言模块方法调整以及修改字典类型字段
parent
25735b77a9
commit
4632548c9f
|
@ -1,5 +1,6 @@
|
|||
package cn.stylefeng.roses.kernel.i18n.api.enums;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -17,24 +18,24 @@ public enum TranslationEnum {
|
|||
/**
|
||||
* 中文
|
||||
*/
|
||||
CHINESE(1, "中文"),
|
||||
CHINESE("chinese", "中文"),
|
||||
|
||||
/**
|
||||
* 英文
|
||||
*/
|
||||
ENGLISH(2, "English");
|
||||
ENGLISH("english", "English");
|
||||
|
||||
/**
|
||||
* 语种编码
|
||||
*/
|
||||
private final Integer code;
|
||||
private final String code;
|
||||
|
||||
/**
|
||||
* 语种的中文描述
|
||||
*/
|
||||
private final String description;
|
||||
|
||||
TranslationEnum(Integer code, String description) {
|
||||
TranslationEnum(String code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
@ -59,8 +60,8 @@ public enum TranslationEnum {
|
|||
* @author fengshuonan
|
||||
* @date 2019/10/18 10:33
|
||||
*/
|
||||
public static TranslationEnum valueOf(Integer value) {
|
||||
if (value != null) {
|
||||
public static TranslationEnum getValue(String value) {
|
||||
if (StrUtil.isNotBlank(value)) {
|
||||
for (TranslationEnum translationLanguages : TranslationEnum.values()) {
|
||||
if (translationLanguages.getCode().equals(value)) {
|
||||
return translationLanguages;
|
||||
|
|
|
@ -38,10 +38,10 @@ public class TranslationRequest extends BaseRequest {
|
|||
private String tranName;
|
||||
|
||||
/**
|
||||
* 1:中文 2:英语
|
||||
* 语种字典
|
||||
*/
|
||||
@NotBlank(message = "language不能为空", groups = {add.class, edit.class})
|
||||
private Integer language;
|
||||
private String tranLanguageCode;
|
||||
|
||||
/**
|
||||
* 翻译的值
|
||||
|
|
|
@ -36,7 +36,7 @@ public class TranslationController {
|
|||
* @date 2021/1/24 19:17
|
||||
*/
|
||||
@PostResource(name = "新增多语言配置", path = "/i18n/add")
|
||||
public ResponseData addItem(@RequestBody @Validated(TranslationRequest.add.class) TranslationRequest translationRequest) {
|
||||
public ResponseData add(@RequestBody @Validated(TranslationRequest.add.class) TranslationRequest translationRequest) {
|
||||
this.translationService.add(translationRequest);
|
||||
return new SuccessResponseData();
|
||||
}
|
||||
|
@ -48,8 +48,8 @@ public class TranslationController {
|
|||
* @date 2021/1/24 19:17
|
||||
*/
|
||||
@PostResource(name = "新增多语言配置", path = "/i18n/edit")
|
||||
public ResponseData editItem(@RequestBody @Validated(BaseRequest.edit.class) TranslationRequest translationRequest) {
|
||||
this.translationService.update(translationRequest);
|
||||
public ResponseData edit(@RequestBody @Validated(BaseRequest.edit.class) TranslationRequest translationRequest) {
|
||||
this.translationService.edit(translationRequest);
|
||||
return new SuccessResponseData();
|
||||
}
|
||||
|
||||
|
@ -61,7 +61,7 @@ public class TranslationController {
|
|||
*/
|
||||
@PostResource(name = "新增多语言配置", path = "/i18n/delete")
|
||||
public ResponseData delete(@RequestBody @Validated(BaseRequest.delete.class) TranslationRequest translationRequest) {
|
||||
this.translationService.delete(translationRequest);
|
||||
this.translationService.del(translationRequest);
|
||||
return new SuccessResponseData();
|
||||
}
|
||||
|
||||
|
@ -73,7 +73,7 @@ public class TranslationController {
|
|||
*/
|
||||
@GetResource(name = "新增多语言配置", path = "/i18n/detail")
|
||||
public ResponseData detail(@Validated(BaseRequest.detail.class) TranslationRequest translationRequest) {
|
||||
Translation detail = this.translationService.findDetail(translationRequest);
|
||||
Translation detail = this.translationService.detail(translationRequest);
|
||||
return new SuccessResponseData(detail);
|
||||
}
|
||||
|
||||
|
@ -84,8 +84,8 @@ public class TranslationController {
|
|||
* @date 2021/1/24 19:20
|
||||
*/
|
||||
@GetResource(name = "新增多语言配置", path = "/i18n/page")
|
||||
public ResponseData list(TranslationRequest translationRequest) {
|
||||
PageResult<Translation> page = this.translationService.findPage(translationRequest);
|
||||
public ResponseData page(TranslationRequest translationRequest) {
|
||||
PageResult<Translation> page = this.translationService.getPage(translationRequest);
|
||||
return new SuccessResponseData(page);
|
||||
}
|
||||
|
||||
|
|
|
@ -40,10 +40,10 @@ public class Translation extends BaseEntity {
|
|||
private String tranName;
|
||||
|
||||
/**
|
||||
* 1:中文 2:英语
|
||||
* 语种字典
|
||||
*/
|
||||
@TableField("language")
|
||||
private Integer language;
|
||||
@TableField("tran_language_code")
|
||||
private String tranLanguageCode;
|
||||
|
||||
/**
|
||||
* 翻译的值
|
||||
|
|
|
@ -6,6 +6,8 @@ import cn.stylefeng.roses.kernel.i18n.api.pojo.request.TranslationRequest;
|
|||
import cn.stylefeng.roses.kernel.i18n.modular.entity.Translation;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 多语言表 服务类
|
||||
*
|
||||
|
@ -15,43 +17,67 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||
public interface TranslationService extends IService<Translation>, TranslationPersistenceApi {
|
||||
|
||||
/**
|
||||
* 新增翻译项
|
||||
* 新增
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/24 19:27
|
||||
* @param translationRequest 参数对象
|
||||
* @author chenjinlong
|
||||
* @date 2021/1/26 12:52
|
||||
*/
|
||||
void add(TranslationRequest param);
|
||||
void add(TranslationRequest translationRequest);
|
||||
|
||||
/**
|
||||
* 更新翻译项
|
||||
* 删除
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/24 19:27
|
||||
* @param translationRequest 参数对象
|
||||
* @author chenjinlong
|
||||
* @date 2021/1/26 12:52
|
||||
*/
|
||||
void update(TranslationRequest param);
|
||||
void del(TranslationRequest translationRequest);
|
||||
|
||||
/**
|
||||
* 删除翻译项
|
||||
* 修改
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/24 19:27
|
||||
* @param translationRequest 参数对象
|
||||
* @author chenjinlong
|
||||
* @date 2021/1/26 12:52
|
||||
*/
|
||||
void delete(TranslationRequest param);
|
||||
void edit(TranslationRequest translationRequest);
|
||||
|
||||
/**
|
||||
* 查询详情
|
||||
* 查询-详情-根据主键id
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/24 19:28
|
||||
* @param translationRequest 参数对象
|
||||
* @author chenjinlong
|
||||
* @date 2021/1/26 12:52
|
||||
*/
|
||||
Translation findDetail(TranslationRequest param);
|
||||
Translation detail(TranslationRequest translationRequest);
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
* 查询-详情-按实体对象
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/24 19:28
|
||||
* @param translationRequest 参数对象
|
||||
* @author chenjinlong
|
||||
* @date 2021/1/26 12:52
|
||||
*/
|
||||
PageResult<Translation> findPage(TranslationRequest param);
|
||||
Translation detailBy(TranslationRequest translationRequest);
|
||||
|
||||
/**
|
||||
* 查询-列表-按实体对象
|
||||
*
|
||||
* @param translationRequest 参数对象
|
||||
* @author chenjinlong
|
||||
* @date 2021/1/26 12:52
|
||||
*/
|
||||
List<Translation> listBy(TranslationRequest translationRequest);
|
||||
|
||||
/**
|
||||
* 查询-列表-分页-按实体对象
|
||||
*
|
||||
* @param translationRequest 参数对象
|
||||
* @author chenjinlong
|
||||
* @date 2021/1/26 12:52
|
||||
*/
|
||||
PageResult<Translation> getPage(TranslationRequest translationRequest);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -33,61 +33,55 @@ import java.util.List;
|
|||
public class TranslationServiceImpl extends ServiceImpl<TranslationMapper, Translation> implements TranslationService {
|
||||
|
||||
@Override
|
||||
public void add(TranslationRequest param) {
|
||||
|
||||
// 1.构造实体
|
||||
public void add(TranslationRequest translationRequest) {
|
||||
Translation translation = new Translation();
|
||||
BeanUtil.copyProperties(param, translation);
|
||||
|
||||
// 2.保存到库中
|
||||
BeanUtil.copyProperties(translationRequest, translation);
|
||||
this.save(translation);
|
||||
|
||||
// 3.添加对应context
|
||||
TranslationEnum translationEnum = TranslationEnum.valueOf(param.getLanguage());
|
||||
TranslationDict translationDict = TranslationDictFactory.createTranslationDict(translationEnum, translation);
|
||||
TranslationContext.me().addTranslationDict(translationDict);
|
||||
// 更新对应常量
|
||||
this.saveContext(translation);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update(TranslationRequest param) {
|
||||
|
||||
// 1.根据id获取信息
|
||||
Translation translation = this.queryTranslation(param);
|
||||
|
||||
// 2.请求参数转化为实体
|
||||
BeanUtil.copyProperties(param, translation);
|
||||
|
||||
// 3.更新记录
|
||||
public void edit(TranslationRequest translationRequest) {
|
||||
Translation translation = this.queryTranslation(translationRequest);
|
||||
BeanUtil.copyProperties(translationRequest, translation);
|
||||
this.updateById(translation);
|
||||
|
||||
// 4.更新对应常量context
|
||||
TranslationEnum translationEnum = TranslationEnum.valueOf(param.getLanguage());
|
||||
TranslationDict translationDict = TranslationDictFactory.createTranslationDict(translationEnum, translation);
|
||||
TranslationContext.me().addTranslationDict(translationDict);
|
||||
// 更新对应常量
|
||||
this.saveContext(translation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(TranslationRequest param) {
|
||||
|
||||
// 1.根据id获取实体
|
||||
Translation translation = this.queryTranslation(param);
|
||||
|
||||
// 2.删除该记录
|
||||
this.removeById(param.getTranId());
|
||||
|
||||
// 3.删除对应context
|
||||
TranslationEnum translationEnum = TranslationEnum.valueOf(translation.getLanguage());
|
||||
TranslationContext.me().deleteTranslationDict(translationEnum, translation.getTranCode());
|
||||
public void del(TranslationRequest translationRequest) {
|
||||
Translation translation = this.queryTranslation(translationRequest);
|
||||
this.removeById(translationRequest.getTranId());
|
||||
// 更新对应常量
|
||||
this.saveContext(translation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Translation findDetail(TranslationRequest param) {
|
||||
return queryTranslation(param);
|
||||
public Translation detail(TranslationRequest translationRequest) {
|
||||
return this.queryTranslation(translationRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<Translation> findPage(TranslationRequest param) {
|
||||
LambdaQueryWrapper<Translation> wrapper = createWrapper(param);
|
||||
public Translation detailBy(TranslationRequest translationRequest) {
|
||||
List<Translation> list = this.listBy(translationRequest);
|
||||
if (list.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return list.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Translation> listBy(TranslationRequest translationRequest) {
|
||||
LambdaQueryWrapper<Translation> queryWrapper = this.createWrapper(translationRequest);
|
||||
return this.list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<Translation> getPage(TranslationRequest translationRequest) {
|
||||
LambdaQueryWrapper<Translation> wrapper = createWrapper(translationRequest);
|
||||
Page<Translation> page = this.page(PageFactory.defaultPage(), wrapper);
|
||||
return PageResultFactory.createPageResult(page);
|
||||
}
|
||||
|
@ -97,7 +91,7 @@ public class TranslationServiceImpl extends ServiceImpl<TranslationMapper, Trans
|
|||
List<Translation> list = this.list();
|
||||
ArrayList<TranslationDict> translationDictList = new ArrayList<>();
|
||||
for (Translation translation : list) {
|
||||
TranslationEnum translationEnum = TranslationEnum.valueOf(translation.getLanguage());
|
||||
TranslationEnum translationEnum = TranslationEnum.getValue(translation.getTranLanguageCode());
|
||||
TranslationDict translationDict = TranslationDictFactory.createTranslationDict(translationEnum, translation);
|
||||
translationDictList.add(translationDict);
|
||||
}
|
||||
|
@ -105,45 +99,55 @@ public class TranslationServiceImpl extends ServiceImpl<TranslationMapper, Trans
|
|||
}
|
||||
|
||||
/**
|
||||
* 获取字典项的对象
|
||||
* 根据主键id获取对象
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/24 21:54
|
||||
* @param
|
||||
* @return
|
||||
* @author chenjinlong
|
||||
* @date 2021/1/26 13:28
|
||||
*/
|
||||
private Translation queryTranslation(TranslationRequest param) {
|
||||
Translation translation = this.getById(param.getTranId());
|
||||
private Translation queryTranslation(TranslationRequest translationRequest) {
|
||||
Translation translation = this.getById(translationRequest.getTranId());
|
||||
if (ObjectUtil.isEmpty(translation)) {
|
||||
throw new TranslationException(TranslationExceptionEnum.NOT_EXISTED, param.getTranId());
|
||||
throw new TranslationException(TranslationExceptionEnum.NOT_EXISTED, translationRequest.getTranId());
|
||||
}
|
||||
return translation;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建多语言的wrapper
|
||||
* 实体构建queryWrapper
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/24 22:03
|
||||
*/
|
||||
private LambdaQueryWrapper<Translation> createWrapper(TranslationRequest param) {
|
||||
private LambdaQueryWrapper<Translation> createWrapper(TranslationRequest translationRequest) {
|
||||
LambdaQueryWrapper<Translation> queryWrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
if (ObjectUtil.isNotNull(param)) {
|
||||
|
||||
// 如果编码不为空,则带上名称搜素搜条件
|
||||
if (ObjectUtil.isNotEmpty(param.getTranCode())) {
|
||||
queryWrapper.like(Translation::getTranCode, param.getTranCode());
|
||||
}
|
||||
|
||||
// 如果翻译名称不为空,则带上翻译名称
|
||||
if (ObjectUtil.isNotEmpty(param.getTranName())) {
|
||||
queryWrapper.like(Translation::getTranName, param.getTranName());
|
||||
}
|
||||
}
|
||||
|
||||
// 按时间倒序
|
||||
queryWrapper.orderByDesc(Translation::getCreateTime);
|
||||
String tranCode = translationRequest.getTranCode();
|
||||
String tranName = translationRequest.getTranName();
|
||||
String tranLanguageCode = translationRequest.getTranLanguageCode();
|
||||
// SQL条件拼接
|
||||
queryWrapper.like(ObjectUtil.isNotEmpty(tranCode), Translation::getTranCode, tranCode);
|
||||
queryWrapper.like(ObjectUtil.isNotEmpty(tranName), Translation::getTranName, tranName);
|
||||
queryWrapper.eq(ObjectUtil.isNotEmpty(tranLanguageCode), Translation::getTranLanguageCode, tranLanguageCode);
|
||||
// 排序
|
||||
queryWrapper.orderByDesc(Translation::getTranCode);
|
||||
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新对应常量
|
||||
*
|
||||
* @param translation
|
||||
* @author chenjinlong
|
||||
* @date 2021/1/26 13:45
|
||||
*/
|
||||
private void saveContext(Translation translation) {
|
||||
TranslationEnum translationEnum = TranslationEnum.getValue(translation.getTranLanguageCode());
|
||||
TranslationDict translationDict = TranslationDictFactory.createTranslationDict(translationEnum, translation);
|
||||
TranslationContext.me().addTranslationDict(translationDict);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue