Commit 165b3da1 authored by fangxinjiang's avatar fangxinjiang

代码生成器调整

parent 5a718ac9
...@@ -14,6 +14,9 @@ import java.time.LocalDateTime; ...@@ -14,6 +14,9 @@ import java.time.LocalDateTime;
*/ */
@Data @Data
public class EmpBadRecordSearchVo extends TEmpBadRecord { public class EmpBadRecordSearchVo extends TEmpBadRecord {
/**
* 多选导出或删除等操作
*/
private String ids; private String ids;
/** /**
......
...@@ -53,7 +53,6 @@ import javax.servlet.http.HttpServletResponse; ...@@ -53,7 +53,6 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
......
...@@ -65,6 +65,10 @@ public class CodeGenKits { ...@@ -65,6 +65,10 @@ public class CodeGenKits {
private final String ENTITY_JAVA_VM = "Entity.java.vm"; private final String ENTITY_JAVA_VM = "Entity.java.vm";
private final String ENTITY_VO_JAVA_VM = "EntityVo.java.vm";
private final String ENTITY_SEARCH_VO_JAVA_VM = "EntitySearchVo.java.vm";
private final String MAPPER_JAVA_VM = "Mapper.java.vm"; private final String MAPPER_JAVA_VM = "Mapper.java.vm";
private final String SERVICE_JAVA_VM = "Service.java.vm"; private final String SERVICE_JAVA_VM = "Service.java.vm";
...@@ -94,6 +98,8 @@ public class CodeGenKits { ...@@ -94,6 +98,8 @@ public class CodeGenKits {
*/ */
private List<String> getTemplates(GenConfig config) { private List<String> getTemplates(GenConfig config) {
List<String> templates = new ArrayList<>(); List<String> templates = new ArrayList<>();
templates.add("template/EntityVo.java.vm");
templates.add("template/EntitySearchVo.java.vm");
templates.add("template/Entity.java.vm"); templates.add("template/Entity.java.vm");
templates.add("template/Mapper.java.vm"); templates.add("template/Mapper.java.vm");
templates.add("template/Mapper.xml.vm"); templates.add("template/Mapper.xml.vm");
...@@ -358,6 +364,14 @@ public class CodeGenKits { ...@@ -358,6 +364,14 @@ public class CodeGenKits {
return packagePath + "entity" + File.separator + className + ".java"; return packagePath + "entity" + File.separator + className + ".java";
} }
if (template.contains(ENTITY_VO_JAVA_VM)) {
return packagePath + "entity" + File.separator + className + "Vo.java";
}
if (template.contains(ENTITY_SEARCH_VO_JAVA_VM)) {
return packagePath + "entity" + File.separator + className + "SearchVo.java";
}
if (template.contains(MAPPER_JAVA_VM)) { if (template.contains(MAPPER_JAVA_VM)) {
return packagePath + "mapper" + File.separator + className + "Mapper.java"; return packagePath + "mapper" + File.separator + className + "Mapper.java";
} }
......
...@@ -134,4 +134,30 @@ public class ${className}Controller { ...@@ -134,4 +134,30 @@ public class ${className}Controller {
return R.ok(${classname}Service.removeById(${pk.lowerAttrName})); return R.ok(${classname}Service.removeById(${pk.lowerAttrName}));
} }
/**
* ${comments} 批量导入
*
* @author ${author}
* @date ${datetime}
**/
@SneakyThrows
@Operation(description = "批量新增${comments} hasPermission('${moduleName}_${pathName}-batch-import')")
@SysLog("批量新增${comments}")
@PostMapping("/importListAdd")
@PreAuthorize("@pms.hasPermission('${moduleName}_${pathName}-batch-import')")
public R<List<ErrorMessage>> importListAdd(@RequestBody MultipartFile file){
return ${classname}Service.importDiy(file.getInputStream());
}
/**
* ${comments} 批量导出
* @author ${author}
* @date ${datetime}
**/
@Operation(description = "导出${comments} hasPermission('${moduleName}_${pathName}-export')")
@PostMapping("/export")
@PreAuthorize("@pms.hasPermission('${moduleName}_${pathName}-export')")
public void export(HttpServletResponse response, @RequestBody ${className}SearchVo searchVo) {
${classname}Service.listExport(response,searchVo);
}
} }
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the yifu4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package ${package}.${moduleName}.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* ${comments}
*
* @author ${author}
* @date ${datetime}
*/
@Data
public class ${className}SearchVo extends ${className}{
/**
* 多选导出或删除等操作
*/
@Schema(description = "选中ID,多个逗号分割")
private String ids;
/**
* 创建时间区间 [开始时间,结束时间]
*/
@Schema(description = "创建时间区间")
private LocalDateTime[] createTimes;
/**
* @Author fxj
* 查询数据起
**/
@Schema(description = "查询limit 开始")
private int limitStart;
/**
* @Author fxj
* 查询数据止
**/
@Schema(description = "查询limit 数据条数")
private int limitEnd;
}
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the yifu4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
#set($excludeColumns = ["create_time","update_time","create_by","update_by"])
package ${package}.${moduleName}.vo;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yifu.cloud.plus.v1.yifu.common.mybatis.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
#if(${hasBigDecimal})
import java.math.BigDecimal;
#end
/**
* ${comments}
*
* @author ${author}
* @date ${datetime}
*/
@Data
public class ${className}Vo extends RowIndex implements Serializable{
#foreach ($column in $columns)
## 排除部分字段
#if(!$excludeColumns.contains($column.columnName))
/**
* $column.comments
*/
#if($column.columnName == $pk.columnName)
@TableId(type = IdType.ASSIGN_ID)
#end
#end
#if($column.nullable)
@NotBlank(message = "$column.comments 不能为空")
#end
#if($column.maxLength && $column.attrType =='String')
@Length(max=$column.maxLength,message = "$column.comments 不能超过$column.maxLength 个字符")
#end
@ExcelAttribute(name = "$column.comments"#if($column.nullable), isNotEmpty = true,errorInfo = "$column.comments 不能为空" #end #if($column.attrType =='LocalDate' || $column.attrType =='LocalDateTime') ,isDate = true #end #if($column.maxLength), maxLength = $column.maxLength #end)
@Schema(description ="$column.comments"#if($column.hidden),hidden=$column.hidden#end)
@ExcelProperty("$column.comments")
#if($column.attrType =='LocalDate' || $column.attrType =='LocalDateTime' || $column.attrType =='Date')
private Date $column.lowerAttrName;
#else
private $column.attrType $column.lowerAttrName;
#end
#end
}
...@@ -33,4 +33,10 @@ public interface ${className}Service extends IService<${className}> { ...@@ -33,4 +33,10 @@ public interface ${className}Service extends IService<${className}> {
* @return * @return
*/ */
IPage<${className}> get${className}Page(Page<${className}> page, ${className} ${classname}); IPage<${className}> get${className}Page(Page<${className}> page, ${className} ${classname});
R<List<ErrorMessage>> importDiy(InputStream inputStream);
void listExport(HttpServletResponse response, ${className}SearchVo searchVo);
List<${className}> noPageDiy(${className}SearchVo searchVo)
} }
...@@ -39,4 +39,189 @@ public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${c ...@@ -39,4 +39,189 @@ public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${c
public IPage<${className}> get${className}Page(Page<${className}> page, ${className} ${classname}){ public IPage<${className}> get${className}Page(Page<${className}> page, ${className} ${classname}){
return baseMapper.get${className}Page(page,${classname}); return baseMapper.get${className}Page(page,${classname});
} }
/**
* ${comments}批量导入
*/
@Override
R<List<ErrorMessage>> importDiy(InputStream inputStream){
}
/**
* ${comments}批量导出
* @param ${classname} ${comments}
* @return
*/
@Override
void listExport(HttpServletResponse response, ${className}SearchVo searchVo){
String fileName = "不良记录批量导出" + DateUtil.getThisTime() + ".xlsx";
//获取要导出的列表
List<${className}> list = new ArrayList<>();
long count = noPageCountDiy(searchVo);
ServletOutputStream out = null;
try {
out = response.getOutputStream();
response.setContentType("multipart/form-data");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName , "UTF-8"));
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,然后文件流会自动关闭
//EasyExcel.write(out, TEmpBadRecord.class).sheet("不良记录").doWrite(list);
ExcelWriter excelWriter = EasyExcel.write(out, ${className}.class).build();
int index = 0;
if (count > CommonConstants.ZERO_INT){
for (int i = 0; i <= count; ) {
// 获取实际记录
searchVo.setLimitStart(i);
searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
list = noPageDiy(searchVo);
if (Common.isNotNull(list)){
ExcelUtil<${className}> util = new ExcelUtil<>(${className}.class);
for (${className} vo:list){
util.convertEntity(vo,null,null,null);
}
}
if (Common.isNotNull(list)){
WriteSheet writeSheet = EasyExcel.writerSheet(${comments}+index).build();
excelWriter.write(list,writeSheet);
index++;
}
i = i + CommonConstants.EXCEL_EXPORT_LIMIT;
if (Common.isNotNull(list)){
list.clear();
}
}
}else {
WriteSheet writeSheet = EasyExcel.writerSheet(${comments}+index).build();
excelWriter.write(list,writeSheet);
}
if (Common.isNotNull(list)){
list.clear();
}
out.flush();
excelWriter.finish();
}catch (Exception e){
log.error("执行异常" ,e);
}finally {
try {
if (null != out) {
out.close();
}
} catch (IOException e) {
log.error("执行异常", e);
}
}
}
@Override
public List<${className}> noPageDiy(${className}SearchVo searchVo) {
LambdaQueryWrapper<${className}> wrapper = buildQueryWrapper(searchVo);
List<String> idList = Common.getList(searchVo.getIds());
if (Common.isNotNull(idList)){
wrapper.in(TEmpBadRecord::getId,idList);
}
if (searchVo.getLimitStart() >= 0 && searchVo.getLimitEnd() > 0){
wrapper.last(" limit "+ searchVo.getLimitStart() +","+ searchVo.getLimitEnd());
}
wrapper.orderByDesc(BaseEntity::getCreateTime);
return baseMapper.selectList(wrapper);
}
private Long noPageCountDiy(${className}SearchVo searchVo) {
LambdaQueryWrapper<${className}> wrapper = buildQueryWrapper(searchVo);
List<String> idList = Common.getList(searchVo.getIds());
if (Common.isNotNull(idList)){
wrapper.in(${className}::getId,idList);
}
return baseMapper.selectCount(wrapper);
}
private LambdaQueryWrapper buildQueryWrapper(${className}SearchVo entity){
LambdaQueryWrapper<${className}> wrapper = Wrappers.lambdaQuery();
if (ArrayUtil.isNotEmpty(entity.getCreateTimes())) {
wrapper.ge(${className}::getCreateTime, entity.getCreateTimes()[0])
.le(${className}::getCreateTime,
entity.getCreateTimes()[1]);
}
if (Common.isNotNull(entity.getCreateName())){
wrapper.eq(${className}::getCreateName,entity.getCreateName());
}
return wrapper;
}
@Override
public R<List<ErrorMessage>> importDiy(InputStream inputStream) {
List<ErrorMessage> errorMessageList = new ArrayList<>();
ExcelUtil<${className}Vo> util1 = new ExcelUtil<>(${className}Vo.class);;
// 写法2
// 匿名内部类 不用额外写一个DemoDataListener
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
try {
EasyExcel.read(inputStream, ${className}Vo.class, new ReadListener<${className}Vo>() {
/**
* 单次缓存的数据量
*/
public static final int BATCH_COUNT = CommonConstants.BATCH_COUNT;
/**
*临时存储
*/
private List<${className}Vo> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
@Override
public void invoke(${className}Vo data, AnalysisContext context) {
ReadRowHolder readRowHolder = context.readRowHolder();
Integer rowIndex = readRowHolder.getRowIndex();
data.setRowIndex(rowIndex+1);
ErrorMessage errorMessage = util1.checkEntity(data, data.getRowIndex());
if (Common.isNotNull(errorMessage)){
errorMessageList.add(errorMessage);
}else {
cachedDataList.add(data);
}
if (cachedDataList.size() >= BATCH_COUNT) {
saveData();
// 存储完成清理 list
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
saveData();
}
/**
* 加上存储数据库
*/
private void saveData() {
log.info("{}条数据,开始存储数据库!", cachedDataList.size());
import${className}(cachedDataList,errorMessageList);
log.info("存储数据库成功!");
}
}).sheet().doRead();
}catch (Exception e){
log.error(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR,e);
return R.failed(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR);
}
return R.ok(errorMessageList);
}
private void import${className}(List<${className}Vo> excelVOList, List<ErrorMessage> errorMessageList) {
// 个性化校验逻辑
ErrorMessage errorMsg;
// 执行数据插入操作 组装
for (int i = 0; i < excelVOList.size(); i++) {
${className}Vo excel = excelVOList.get(i);
// 数据合法情况 TODO
// 插入
insertExcel(excel,emp);
errorMessageList.add(new ErrorMessage(excel.getRowIndex(),CommonConstants.SAVE_SUCCESS));
}
}
/**
* 插入excel bad record
*/
private void insertExcel(${className}Vo excel) {
${className} insert = new ${className}();
BeanUtil.copyProperties(excel, insert);
this.save(insert);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment