diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/controller/GaeaUiI18nController.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/controller/GaeaUiI18nController.java new file mode 100644 index 00000000..98a8ae72 --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/controller/GaeaUiI18nController.java @@ -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 { + + @Autowired + private GaeaUiI18nService gaeaUiI18nService; + + @Override + public GaeaBaseService 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 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); + } + +} diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/controller/dto/GaeaUiI18nDto.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/controller/dto/GaeaUiI18nDto.java new file mode 100644 index 00000000..2d5e5dca --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/controller/dto/GaeaUiI18nDto.java @@ -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; + +} \ No newline at end of file diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/controller/param/GaeaUiI18nParam.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/controller/param/GaeaUiI18nParam.java new file mode 100644 index 00000000..f36d5b7b --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/controller/param/GaeaUiI18nParam.java @@ -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; +} \ No newline at end of file diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/dao/GaeaUiI18nMapper.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/dao/GaeaUiI18nMapper.java new file mode 100644 index 00000000..7abfe917 --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/dao/GaeaUiI18nMapper.java @@ -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 { + + /** + * 查询表的定义,字段中文描述等信息 + * @param tableName + * @return + */ + List queryColumns(@Param(value = "tableName") String tableName); + + /** + * 查询项目所有的表名 + * @param s + * @return + */ + List queryTables(@Param(value = "tableName") String s); +} \ No newline at end of file diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/dao/entity/GaeaUiI18n.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/dao/entity/GaeaUiI18n.java new file mode 100644 index 00000000..6381e5b2 --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/dao/entity/GaeaUiI18n.java @@ -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; + + +} \ No newline at end of file diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/service/GaeaUiI18nService.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/service/GaeaUiI18nService.java new file mode 100644 index 00000000..b9ca6b5a --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/service/GaeaUiI18nService.java @@ -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 { + + /*** + * 查询详情 + * + * @param id + * @return + */ + GaeaUiI18n getDetail(Long id); + + /** + * 新加功能菜单时,i18n同步、初始化,以单表为单位处理 + * 优先从 自动生成处理表查询,没有则从源表查询,减少重复维护 + * + * @param table 表名称 + */ + void scan(String table); + + /** + * 查询项目所有的表 + * @return + */ + List getUi18nTables(); + + /** + * 菜单初始化时,查询该菜单需要的国际化列表 + * @param initQry + * @return map + */ + Map getI18nFields(GaeaUiI18n initQry); +} \ No newline at end of file diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/service/impl/ColumnDesc.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/service/impl/ColumnDesc.java new file mode 100644 index 00000000..77d44336 --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/service/impl/ColumnDesc.java @@ -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 { + +} diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/service/impl/GaeaUiI18nServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/service/impl/GaeaUiI18nServiceImpl.java new file mode 100644 index 00000000..ac49537e --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/gaeaUiI18n/service/impl/GaeaUiI18nServiceImpl.java @@ -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 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 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 columnDescList = generatorService.getColumns(table); + //List columnDescList = gaeaUiI18nMapper.queryColumns(table); + if(!CollectionUtils.isEmpty(columnDescList)){ + List 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 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 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 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; + } +} \ No newline at end of file