diff --git a/src/main/java/org/yangliu/codegenerate/Main.java b/src/main/java/org/yangliu/codegenerate/Main.java index 8029cd4..a7eaa07 100644 --- a/src/main/java/org/yangliu/codegenerate/Main.java +++ b/src/main/java/org/yangliu/codegenerate/Main.java @@ -35,6 +35,8 @@ public class Main { //实体类模板 private static String bussi_entity_template = "XXXEntity.javai"; + private static String bussi_entity_excel_template = "XXXEntityExcel.javai"; + private static String bussi_entity_dto_template = "XXXEntityDTO.javai"; private static String bussi_entity_vo_template = "XXXEntityVO.javai"; private static String bussi_entity_add_vo_template = "XXXEntityAddPVO.javai"; @@ -66,9 +68,9 @@ public class Main { String user = "enjoy"; String password = "Nomi1234"; //2.选择表配置 - String table= "oqc_lnspection_template"; - String entityName="OqcLnspectionTemplate";//实体类 - String description = "检验模板表";//备注 + String table= "oqc_report_template"; + String entityName="OqcReportTemplate";//实体类 + String description = "报告模板";//备注 String primaryKeyField = "id";//主键 List tableEntityVoList = printTableStructure(driver, url, user, password, table); //3.生成 @@ -88,6 +90,7 @@ public class Main { Map dataMap = new HashMap<>(); dataMap.put("bussiPackage", bussiPackage);//包 dataMap.put("entityPackage", "domain");//实体类包名 domain/entity + dataMap.put("excelPackage","oqcExcel");//导出Excel类的包 dataMap.put("tableName", tableName);//表名称 dataMap.put("tableVo", tableVo);//表属性 //主键 @@ -109,6 +112,11 @@ public class Main { MarkerGenerate.generate(templatepath, bussi_entity_template, entityParsePath, entityName + ".java", dataMap); String dtoParsePath = parsePath + "dto" + File.separator; MarkerGenerate.generate(templatepath, bussi_entity_dto_template, dtoParsePath, entityName + "DTO.java", dataMap); + + //excel + String excelParsePath=parsePath +(String) dataMap.get("excelPackage")+ File.separator;; + MarkerGenerate.generate(templatepath, bussi_entity_excel_template, excelParsePath, entityName + "Excel.java", dataMap); + String voParsePath = parsePath + "vo" + File.separator; //MarkerGenerate.generate(templatepath, bussi_entity_vo_template, voParsePath, entityName + "VO.java", dataMap); //add_pvo diff --git a/src/main/resources/yangliu/XXXController.javai b/src/main/resources/yangliu/XXXController.javai index 95f0973..9057f60 100644 --- a/src/main/resources/yangliu/XXXController.javai +++ b/src/main/resources/yangliu/XXXController.javai @@ -26,6 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.springframework.web.multipart.MultipartFile; /** * @Description: ${tableVo.ftlDescription} @@ -117,4 +118,22 @@ public class ${entityName}Controller extends BaseController { return this.${entityName?uncap_first}Service.delete${entityName}(Arrays.asList(ids.split(","))); } + + /** + * 导出 + */ + @ApiOperation( value ="导出", notes ="导出", httpMethod ="POST") + @PostMapping("/export") + public void export(HttpServletResponse response,Query${entityName}VO query${entityName}VO){ + ${entityName?uncap_first}Service.export(response,query${entityName}VO); + } + + /** + * 导入 + */ + @ApiOperation( value ="导入", notes ="导入", httpMethod ="POST") + @PostMapping("/import") + public AjaxResult import${entityName}(@RequestPart("file") MultipartFile file) throws Exception { + return ${entityName?uncap_first}Service.import${entityName}(file); + } } diff --git a/src/main/resources/yangliu/XXXEntityExcel.javai b/src/main/resources/yangliu/XXXEntityExcel.javai new file mode 100644 index 0000000..1cea1c9 --- /dev/null +++ b/src/main/resources/yangliu/XXXEntityExcel.javai @@ -0,0 +1,62 @@ +package ${bussiPackage}.${excelPackage}; + +import com.enjoy.common.core.annotation.Excel; +import com.enjoy.common.core.utils.iqc.ExcelImport; +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; +import org.springframework.format.annotation.DateTimeFormat; +import java.util.Date; + +/** + * @Description: ${tableVo.ftlDescription} + * @Author: ${author} + * @Date: ${.now?string["yyyy-MM-dd"]} + * @Version: V1.0 + */ +@Data +@ToString +@AllArgsConstructor +@NoArgsConstructor +public class ${entityName}Excel{ + private static final long serialVersionUID = 1L; +<#list originalColumns as po> + <#if po.fieldName =='id'|| po.fieldName=='delFlag' || po.fieldName == 'creatorId' || po.fieldName == 'createBy' || po.fieldName == 'createTime' || po.fieldName == 'updaterId' || po.fieldName == 'updateBy' || po.fieldName == 'updateTime' || po.fieldName == 'tenantId'><#else> + /**${po.filedComment}*/ + <#if po.fieldName == primaryKeyField> + @TableId(type = IdType.ASSIGN_ID) + <#else> + <#if po.fieldType =='Date'> + <#if po.fieldDbType =='date'> + @Excel(name = "${po.filedComment}", width = 15) + @ExcelImport("${po.filedComment}") + @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd") + @DateTimeFormat(pattern="yyyy-MM-dd") + <#elseif po.fieldDbType =='datetime'> + @Excel(name = "${po.filedComment}", width = 20) + @ExcelImport("${po.filedComment}") + @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss") + @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") + <#elseif po.fieldDbType =='timestamp'> + @Excel(name = "${po.filedComment}", width = 20) + @ExcelImport("${po.filedComment}") + @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss") + @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") + + <#else> + @Excel(name = "${po.filedComment}", width = 15) + @ExcelImport("${po.filedComment}") + + + @ApiModelProperty(value = "${po.filedComment}",example = "${po.filedComment}") + <#if po.fieldName == 'delFlag'> + @TableLogic + + private <#if po.fieldType=='java.sql.Blob'>byte[]<#else>${po.fieldType} ${po.fieldName}; + + + +} diff --git a/src/main/resources/yangliu/XXXService.javai b/src/main/resources/yangliu/XXXService.javai index 6e8f0e7..d8a0e99 100644 --- a/src/main/resources/yangliu/XXXService.javai +++ b/src/main/resources/yangliu/XXXService.javai @@ -8,6 +8,8 @@ import ${bussiPackage}.vo.Add${entityName}PVO; import ${bussiPackage}.vo.Edit${entityName}PVO; import ${bussiPackage}.${entityPackage}.${entityName}; import com.baomidou.mybatisplus.extension.service.IService; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; import java.util.List; /** @@ -51,4 +53,14 @@ public interface I${entityName}Service extends IService<${entityName}> { * 批量删除 */ AjaxResult delete${entityName}(List ids); + + /** + * 导出 + */ + void export(HttpServletResponse response, Query${entityName}VO query${entityName}VO); + + /** + * 导入 + */ + AjaxResult import${entityName}(MultipartFile file); } \ No newline at end of file diff --git a/src/main/resources/yangliu/XXXServiceImpl.javai b/src/main/resources/yangliu/XXXServiceImpl.javai index da01529..be99236 100644 --- a/src/main/resources/yangliu/XXXServiceImpl.javai +++ b/src/main/resources/yangliu/XXXServiceImpl.javai @@ -1,5 +1,9 @@ package ${bussiPackage}.service.impl; +import cn.hutool.core.bean.BeanUtil; +import com.enjoy.common.core.utils.iqc.IQCExcelUtils; +import com.enjoy.common.core.web.domain.AjaxResult; +import com.enjoy.common.security.utils.ExcelUtil; import com.enjoy.common.core.web.domain.AjaxResult; import com.enjoy.oqc.utils.BeanHkUtils; import com.enjoy.oqc.utils.CollectionUtil; @@ -13,6 +17,8 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import javax.servlet.http.HttpServletResponse; +import org.springframework.web.multipart.MultipartFile; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; @@ -23,6 +29,7 @@ import ${bussiPackage}.mapper.${entityName}Mapper; import ${bussiPackage}.service.I${entityName}Service; import ${bussiPackage}.vo.Add${entityName}PVO; import ${bussiPackage}.vo.Edit${entityName}PVO; +import ${bussiPackage}.oqcExcel.${entityName}Excel; /** @@ -55,7 +62,7 @@ public class ${entityName}ServiceImpl extends ServiceImpl<${entityName}Mapper, $ if (CollectionUtil.isNotEmpty(dbEntityList)) { dbEntityList.stream().forEach(entity->{ ${entityName}DTO ${uncap_first}DTO=new ${entityName}DTO(); - BeanHkUtils.copyProperties(entity,${uncap_first}DTO); + ${uncap_first}DTO = (${entityName}DTO) BeanHkUtils.copyProperties(entity,${uncap_first}DTO); list.add(${uncap_first}DTO); }); } @@ -83,7 +90,7 @@ public class ${entityName}ServiceImpl extends ServiceImpl<${entityName}Mapper, $ if (CollectionUtil.isNotEmpty(dbEntityList)) { dbEntityList.stream().forEach(entity->{ ${entityName}DTO ${uncap_first}DTO=new ${entityName}DTO(); - BeanHkUtils.copyProperties(entity,${uncap_first}DTO); + ${uncap_first}DTO = (${entityName}DTO) BeanHkUtils.copyProperties(entity,${uncap_first}DTO); list.add(${uncap_first}DTO); }); } @@ -145,4 +152,38 @@ public class ${entityName}ServiceImpl extends ServiceImpl<${entityName}Mapper, $ this.update(updateWrapper); return AjaxResult.success("删除成功!"); } + /** + * 导出 + */ + @Override + public void export(HttpServletResponse response, Query${entityName}VO query${entityName}VO){ + List<${entityName}Excel> list=new ArrayList(); + QueryWrapper<${entityName}> queryWrapper=new QueryWrapper(); + List<${entityName}> dbEntityList = this.list(queryWrapper); + if (CollectionUtil.isNotEmpty(dbEntityList)) { + dbEntityList.stream().forEach(entity->{ + ${entityName}Excel ${uncap_first}Excel=new ${entityName}Excel(); + ${uncap_first}Excel = (${entityName}Excel) BeanHkUtils.copyProperties(entity,${uncap_first}Excel); + list.add(${uncap_first}Excel); + }); + } + ExcelUtil<${entityName}Excel> util = new ExcelUtil<${entityName}Excel>(${entityName}Excel.class); + util.exportExcel(response, list, "${tableVo.ftlDescription}"); + } + + /** + * 导入 + */ + @Override + public AjaxResult import${entityName}(MultipartFile file){ + try { + List<${entityName}Excel> excelList = IQCExcelUtils.readMultipartFile(file, ${entityName}Excel.class); + List<${entityName}> entityList = BeanUtil.copyToList(excelList, ${entityName}.class); + this.saveBatch(entityList); + }catch (Exception e){ + log.error("导入 异常:",e); + return AjaxResult.error(e.getMessage()); + } + return AjaxResult.success("导入成功"); + } }