国际化

yanzili 3 years ago
parent 191d72f8ea
commit 485e592591

@ -0,0 +1,100 @@
package com.anjiplus.template.gaea.business.modules.gaeaUiI18n.controller;
import com.anji.plus.gaea.annotation.AccessKey;
import com.anji.plus.gaea.bean.ResponseBean;
import com.anji.plus.gaea.curd.service.GaeaBaseService;
import com.anji.plus.gaea.utils.GaeaBeanUtils;
import com.anji.plus.gaea.utils.GaeaUtils;
import com.anjiplus.template.gaea.business.base.BaseController;
import com.anjiplus.template.gaea.business.modules.gaeaUiI18n.controller.dto.GaeaUiI18nDto;
import com.anjiplus.template.gaea.business.modules.gaeaUiI18n.controller.param.GaeaUiI18nParam;
import com.anjiplus.template.gaea.business.modules.gaeaUiI18n.dao.entity.GaeaUiI18n;
import com.anjiplus.template.gaea.business.modules.gaeaUiI18n.service.GaeaUiI18nService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @desc ui controller
* @website https://gitee.com/anji-plus/gaea
* @author
* @date 2021-03-25 15:30:59.286
**/
@RestController
@Api(tags = "ui文字国际化处理管理")
@RequestMapping("/gaeaUiI18n")
public class GaeaUiI18nController extends BaseController<GaeaUiI18nParam, GaeaUiI18n, GaeaUiI18nDto> {
@Autowired
private GaeaUiI18nService gaeaUiI18nService;
@Override
public GaeaBaseService<GaeaUiI18nParam, GaeaUiI18n> getService() {
return gaeaUiI18nService;
}
@Override
public GaeaUiI18n getEntity() {
return new GaeaUiI18n();
}
@Override
public GaeaUiI18nDto getDTO() {
return new GaeaUiI18nDto();
}
@GetMapping({"/{id}"})
@AccessKey
@Override
public ResponseBean detail(@PathVariable("id") Long id) {
this.logger.info("{}根据ID查询服务开始id为{}", this.getClass().getSimpleName(), id);
GaeaUiI18n result = gaeaUiI18nService.getDetail(id);
GaeaUiI18nDto dto = this.getDTO();
GaeaBeanUtils.copyAndFormatter(result, dto);
ResponseBean responseBean = this.responseSuccessWithData(this.resultDtoHandle(dto));
this.logger.info("{}根据ID查询结束结果{}", this.getClass().getSimpleName(), GaeaUtils.toJSONString(responseBean));
return responseBean;
}
@PostMapping(value = "/scan")
public ResponseBean scan(@RequestParam String table){
gaeaUiI18nService.scan(table);
return responseSuccess();
}
@GetMapping(value = "/getTables")
public ResponseBean getTables(){
List<String> t = gaeaUiI18nService.getUi18nTables();
return responseSuccessWithData(t);
}
@PostMapping(value = "/listI18nFields")
public ResponseBean getI18nFields(@RequestBody GaeaUiI18nDto q){
GaeaUiI18n qry = new GaeaUiI18n();
GaeaBeanUtils.copyAndFormatter(q,qry);
// 客户端测试时,以前端参数为准
if(StringUtils.isEmpty(qry.getLocale())) {
qry.setLocale(getI18nLang());
}
Map ret = new HashMap();
for(String table : q.getRefer().split(",")) {
qry.setRefer(table);
Map t = gaeaUiI18nService.getI18nFields(qry);
ret.putIfAbsent(qry.getModule(),new HashMap<>());
// 合并module子节点
((Map)ret.get(qry.getModule())).putAll((Map)t.get(qry.getModule()));
}
ret.put("locale",q.getLocale());
return responseSuccessWithData(ret);
}
}

@ -0,0 +1,50 @@
package com.anjiplus.template.gaea.business.modules.gaeaUiI18n.controller.dto;
import java.io.Serializable;
import com.anji.plus.gaea.curd.dto.GaeaBaseDTO;
import com.anji.plus.gaea.annotation.Formatter;
import lombok.Data;
import java.sql.Timestamp;
/**
*
* @description ui dto
* @author
* @date 2021-03-25 15:30:59.286
**/
@Data
public class GaeaUiI18nDto extends GaeaBaseDTO implements Serializable {
/** 语言标识 */
@Formatter(dictCode = "LOCALE",targetField = "localeCn")
private String locale;
private String localeCn;
/** 行业标识 */
@Formatter(dictCode = "SYS_CATA_TYPE",targetField = "cataTypeCn")
private String cataType;
private String cataTypeCn;
/** 所属系统 */
private String system;
/** 所属菜单编号 */
private String module;
/** 字段编码 */
private String code;
/** 字段名称 */
private String name;
/** 业务描述 */
private String remark;
private String refer;
/** 启用状态 */
@Formatter(dictCode = "ENABLE_FLAG",targetField = "enabledCn")
private Integer enabled;
private String enabledCn;
}

@ -0,0 +1,54 @@
/**/
package com.anjiplus.template.gaea.business.modules.gaeaUiI18n.controller.param;
import lombok.Data;
import java.io.Serializable;
import com.anji.plus.gaea.annotation.Query;
import com.anji.plus.gaea.constant.QueryEnum;
import com.anji.plus.gaea.curd.params.PageParam;
import java.util.List;
/**
* @desc GaeaUiI18n ui
* @author
* @date 2021-03-25 15:30:59.286
**/
@Data
public class GaeaUiI18nParam extends PageParam implements Serializable{
/** 精确查询 */
@Query
private String locale;
/** 精确查询 */
@Query
private String cataType;
/** 精确查询 */
@Query
private String system;
/** 模糊查询 */
@Query(value = QueryEnum.LIKE)
private String code;
/** 模糊查询 */
@Query(value = QueryEnum.LIKE)
private String name;
/** 模糊查询 */
@Query(value = QueryEnum.LIKE)
private String remark;
@Query(value = QueryEnum.EQ)
private String refer;
@Query(value = QueryEnum.EQ)
private String module;
/** 精确查询 */
@Query
private Integer enabled;
}

@ -0,0 +1,33 @@
package com.anjiplus.template.gaea.business.modules.gaeaUiI18n.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import com.anji.plus.gaea.curd.mapper.GaeaBaseMapper;
import com.anjiplus.template.gaea.business.modules.gaeaUiI18n.dao.entity.GaeaUiI18n;
import com.anjiplus.template.gaea.business.modules.gaeaUiI18n.service.impl.ColumnDesc;
/**
* GaeaUiI18n Mapper
* @author
* @date 2021-03-25 15:30:59.286
**/
@Mapper
public interface GaeaUiI18nMapper extends GaeaBaseMapper<GaeaUiI18n> {
/**
*
* @param tableName
* @return
*/
List<ColumnDesc> queryColumns(@Param(value = "tableName") String tableName);
/**
*
* @param s
* @return
*/
List<String> queryTables(@Param(value = "tableName") String s);
}

@ -0,0 +1,46 @@
package com.anjiplus.template.gaea.business.modules.gaeaUiI18n.dao.entity;
import lombok.Data;
import io.swagger.annotations.ApiModelProperty;
import com.anji.plus.gaea.curd.entity.GaeaBaseEntity;
import com.baomidou.mybatisplus.annotation.TableName;
/**
* @description ui entity
* @author
* @date 2021-03-25 15:30:59.286
**/
@TableName(value="gaea_ui_i18n")
@Data
public class GaeaUiI18n extends GaeaBaseEntity {
@ApiModelProperty(value = "语言标识")
private String locale;
@ApiModelProperty(value = "行业标识")
private String cataType;
@ApiModelProperty(value = "所属系统")
private String system;
@ApiModelProperty(value = "所属模块")
private String module;
@ApiModelProperty(value = "字段编码")
private String code;
@ApiModelProperty(value = "字段名称")
private String name;
@ApiModelProperty(value = "业务描述")
private String remark;
@ApiModelProperty(value = "关联表名")
private String refer;
@ApiModelProperty(value = "启用状态")
private Integer enabled;
}

@ -0,0 +1,46 @@
package com.anjiplus.template.gaea.business.modules.gaeaUiI18n.service;
import com.anjiplus.template.gaea.business.base.BaseService;
import com.anjiplus.template.gaea.business.modules.gaeaUiI18n.dao.entity.GaeaUiI18n;
import com.anjiplus.template.gaea.business.modules.gaeaUiI18n.controller.param.GaeaUiI18nParam;
import java.util.List;
import java.util.Map;
/**
* @desc GaeaUiI18n ui
* @author
* @date 2021-03-25 15:30:59.286
**/
public interface GaeaUiI18nService extends BaseService<GaeaUiI18nParam, GaeaUiI18n> {
/***
*
*
* @param id
* @return
*/
GaeaUiI18n getDetail(Long id);
/**
* i18n,
*
*
* @param table
*/
void scan(String table);
/**
*
* @return
*/
List<String> getUi18nTables();
/**
*
* @param initQry
* @return map
*/
Map getI18nFields(GaeaUiI18n initQry);
}

@ -0,0 +1,11 @@
package com.anjiplus.template.gaea.business.modules.gaeaUiI18n.service.impl;
import com.anjiplus.template.gaea.generator.domain.Column;
/**
* @author WongBin
* @date 2021/3/26
*/
public class ColumnDesc extends Column {
}

@ -0,0 +1,162 @@
package com.anjiplus.template.gaea.business.modules.gaeaUiI18n.service.impl;
import cn.hutool.core.util.StrUtil;
import com.anji.plus.gaea.constant.BaseOperationEnum;
import com.anji.plus.gaea.constant.Enabled;
import com.anji.plus.gaea.curd.mapper.GaeaBaseMapper;
import com.anji.plus.gaea.exception.BusinessException;
import com.anjiplus.template.gaea.generator.domain.Column;
import com.anjiplus.template.gaea.generator.service.GeneratorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.anjiplus.template.gaea.business.modules.gaeaUiI18n.dao.entity.GaeaUiI18n;
import com.anjiplus.template.gaea.business.modules.gaeaUiI18n.service.GaeaUiI18nService;
import com.anjiplus.template.gaea.business.modules.gaeaUiI18n.dao.GaeaUiI18nMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @desc GaeaUiI18n ui
* @author
* @date 2021-03-25 15:30:59.286
**/
@Service
@Slf4j
public class GaeaUiI18nServiceImpl implements GaeaUiI18nService {
@Autowired
private GaeaUiI18nMapper gaeaUiI18nMapper;
@Override
public GaeaBaseMapper<GaeaUiI18n> getMapper() {
return gaeaUiI18nMapper;
}
@Override
public GaeaUiI18n getDetail(Long id) {
GaeaUiI18n gaeaUiI18n = this.selectOne(id);
return gaeaUiI18n;
}
@Autowired(required = false)
GeneratorService generatorService;
@Value("${spring.application.name}")
private String applicationName;
@Override
public void processBeforeOperation(GaeaUiI18n entity,
BaseOperationEnum type) throws BusinessException {
if(BaseOperationEnum.INSERT.equals(type)){
entity.setSystem(applicationName);
//entity.setCataType("");
}
}
@Override
public void scan(String table) {
assert table!=null;
LambdaQueryWrapper<GaeaUiI18n> wrapper = Wrappers.lambdaQuery();
wrapper.eq(GaeaUiI18n::getRefer,table);
if(gaeaUiI18nMapper.selectCount(wrapper) > 0){
log.warn("{} exist,ignore",table);
throw new BusinessException("6001",new String[]{"该表的字段已维护国际化配置"});
}
List<Column> columnDescList = generatorService.getColumns(table);
//List<ColumnDesc> columnDescList = gaeaUiI18nMapper.queryColumns(table);
if(!CollectionUtils.isEmpty(columnDescList)){
List<GaeaUiI18n> list = columnDescList.stream().map(item->{
if(item.getRemark()!=null && item.getRemark().length()>20){
item.setRemark(item.getRemark().substring(0,20));
}
GaeaUiI18n it = new GaeaUiI18n();
it.setCode(StrUtil.toCamelCase(item.getColumnName()));
it.setName(item.getRemark());
it.setLocale(getI18nLang());
it.setSystem(applicationName);
it.setRemark(item.getRemark());
it.setEnabled(Enabled.YES.getValue());
it.setRefer(item.getTableName());
return it;
}).collect(Collectors.toList());
int ret = gaeaUiI18nMapper.insertBatch(list);
log.info("insert-batch-for:{},size:{}",table,ret);
}
}
private String getI18nLang(){
return LocaleContextHolder.getLocale().getLanguage();
}
@Override
public List<String> getUi18nTables() {
return gaeaUiI18nMapper.queryTables("");
}
@Override
public Map getI18nFields(GaeaUiI18n q) {
String table = q.getRefer();
String tableAlias = "";
// 提取表名称和别名
if(q.getRefer().contains(":")) {
table = q.getRefer().split(":")[0];
tableAlias = Optional.ofNullable(q.getRefer().substring(table.length() + 1)).orElse("");
q.setRefer(table);
}
LambdaQueryWrapper<GaeaUiI18n> qry = Wrappers.lambdaQuery();
qry.eq(GaeaUiI18n::getLocale,q.getLocale())
.and(t->t.eq(GaeaUiI18n::getEnabled,Enabled.YES.getValue()))
// 所属行业
.and(t->t.eq(GaeaUiI18n::getCataType,q.getCataType()).or().isNull(GaeaUiI18n::getCataType))
// 同一个表 或同一个菜单
.and(t->t.eq(GaeaUiI18n::getRefer,q.getRefer()).or().eq(GaeaUiI18n::getModule,q.getModule()))
;
List<GaeaUiI18n> list = gaeaUiI18nMapper.selectList(qry);
HashMap kv = new HashMap();
// 表级别 字段默认设置
list.stream().filter(i->i.getRefer()!=null && i.getCataType()==null).forEach(i->{
kv.put(i.getCode(),i.getName());
});
// 表级别 字段行业属性,覆盖默认设置
list.stream().filter(i->i.getRefer()!=null && i.getCataType()!=null).forEach(i->{
kv.put(i.getCode(),i.getName());
});
Map result = new HashMap();
// 挂载在module根节点
if(tableAlias.length()<1) {
result.put(q.getModule(), kv);
}else {
// 作为module的子节点
result.putIfAbsent(q.getModule(),new HashMap<>());
((Map)result.get(q.getModule())).put(tableAlias,kv);
}
// 设置模块级别的字段配置
HashMap m = new HashMap();
list.stream().filter(i->i.getRefer()==null && i.getModule().equals(q.getModule())).forEach(item->{
m.put(item.getCode(),item.getName());
});
if(!m.isEmpty()) {
((Map)result.get(q.getModule())).putAll(m);
}
return result;
}
}
Loading…
Cancel
Save