From 0d6ef45c31c2e4b1fd92e00fa6b4bdd5313f997f Mon Sep 17 00:00:00 2001 From: gejunhao <1421207006@qq.com> Date: Fri, 10 Jan 2025 15:23:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit b59c68d115f67c782d44a51fca8b6023a4e691ed) --- pom.xml | 6 + ruoyi-admin/pom.xml | 5 + ruoyi-modules/guoyan-platform/pom.xml | 114 ++++++++++++++++++ .../platform/controller/TestController.java | 77 ++++++++++++ .../domain/entity/ProjectManager.java | 45 +++++++ .../listener/ProjectManagerListener.java | 63 ++++++++++ .../platform/mapper/ProjectManagerMapper.java | 29 +++++ .../service/ProjectManagerService.java | 28 +++++ .../impl/ProjectManagerServiceImpl.java | 38 ++++++ ruoyi-modules/pom.xml | 1 + 10 files changed, 406 insertions(+) create mode 100644 ruoyi-modules/guoyan-platform/pom.xml create mode 100644 ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/controller/TestController.java create mode 100644 ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/domain/entity/ProjectManager.java create mode 100644 ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/listener/ProjectManagerListener.java create mode 100644 ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/mapper/ProjectManagerMapper.java create mode 100644 ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/service/ProjectManagerService.java create mode 100644 ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/service/impl/ProjectManagerServiceImpl.java diff --git a/pom.xml b/pom.xml index 7b92aa7..5f6ab86 100644 --- a/pom.xml +++ b/pom.xml @@ -346,6 +346,12 @@ ${revision} + + org.dromara + guoyan-platform + ${revision} + + org.dromara diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml index 9e97804..89ab0e7 100644 --- a/ruoyi-admin/pom.xml +++ b/ruoyi-admin/pom.xml @@ -86,6 +86,11 @@ org.dromara ruoyi-demo + + + org.dromara + guoyan-platform + diff --git a/ruoyi-modules/guoyan-platform/pom.xml b/ruoyi-modules/guoyan-platform/pom.xml new file mode 100644 index 0000000..b24aeb9 --- /dev/null +++ b/ruoyi-modules/guoyan-platform/pom.xml @@ -0,0 +1,114 @@ + + + + ruoyi-modules + org.dromara + 5.2.3 + + 4.0.0 + + guoyan-platform + + + + + + org.dromara + ruoyi-common-core + + + + org.dromara + ruoyi-common-doc + + + + org.dromara + ruoyi-common-sms + + + + org.dromara + ruoyi-common-mail + + + + org.dromara + ruoyi-common-redis + + + + org.dromara + ruoyi-common-idempotent + + + + org.dromara + ruoyi-common-mybatis + + + + org.dromara + ruoyi-common-log + + + + org.dromara + ruoyi-common-excel + + + + org.dromara + ruoyi-common-security + + + + org.dromara + ruoyi-common-web + + + + org.dromara + ruoyi-common-ratelimiter + + + + org.dromara + ruoyi-common-translation + + + + org.dromara + ruoyi-common-sensitive + + + + org.dromara + ruoyi-common-encrypt + + + + org.dromara + ruoyi-common-tenant + + + + org.dromara + ruoyi-common-websocket + + + + org.dromara + ruoyi-demo + + + + + + 17 + 17 + + + diff --git a/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/controller/TestController.java b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/controller/TestController.java new file mode 100644 index 0000000..6768543 --- /dev/null +++ b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/controller/TestController.java @@ -0,0 +1,77 @@ +package org.dromara.platform.controller; + +import com.alibaba.excel.EasyExcel; +import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.dromara.common.core.domain.R; +import org.dromara.common.core.validate.QueryGroup; +import org.dromara.common.mybatis.core.page.PageQuery; +import org.dromara.common.mybatis.core.page.TableDataInfo; +import org.dromara.demo.domain.bo.TestDemoBo; +import org.dromara.demo.domain.vo.TestDemoVo; +import org.dromara.platform.domain.entity.ProjectManager; +import org.dromara.platform.listener.ProjectManagerListener; +import org.dromara.platform.service.ProjectManagerService; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.net.URLEncoder; +import java.time.LocalDate; + +/** + * @Author gejunhao + * @Date 2025/1/10 14:11 + * @Description: 测试接口 + */ +@RequiredArgsConstructor +@RestController +@Slf4j +@RequestMapping("/test") +public class TestController { + + @GetMapping("/hello") + public void test(HttpServletResponse response) throws IOException { + log.warn("hello word!"); + response.setContentType("text/html;charset=utf8"); + response.getWriter().println("调用接口成功!"); + } + + @Resource + private ProjectManagerService projectManagerService; + @PostMapping("/uploadProjectManager") + public void upload(MultipartFile file, HttpServletResponse response) throws IOException { + long t1 = System.currentTimeMillis(); + + EasyExcel.read(file.getInputStream(), ProjectManager.class, new ProjectManagerListener(projectManagerService)).sheet().doRead(); + + response.setContentType("text/html;charset=utf8"); + long t2 = System.currentTimeMillis(); + response.getWriter().println("导入数据成功!,共用时:"+(t2-t1)); + log.info("导入项目负责人数据成功! 共用时:{}ms",(t2-t1)); + } + + + @GetMapping("/downloadProjectManager") + public void download(HttpServletResponse response) throws IOException { + // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setCharacterEncoding("utf-8"); + String flag = "项目负责人信息"; + // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 + LocalDate currentDate = LocalDate.now(); + System.out.println("当前日期: " + currentDate); + String fileName = URLEncoder.encode( flag+currentDate, "UTF-8") + .replaceAll("\\+", "%20"); + response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); + EasyExcel.write(response.getOutputStream(), ProjectManager.class) + .sheet("项目联系人信息"+currentDate) + .doWrite(projectManagerService.getData()); + } +} diff --git a/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/domain/entity/ProjectManager.java b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/domain/entity/ProjectManager.java new file mode 100644 index 0000000..86bda69 --- /dev/null +++ b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/domain/entity/ProjectManager.java @@ -0,0 +1,45 @@ +package org.dromara.platform.domain.entity; + +import com.alibaba.excel.annotation.ExcelIgnore; +import com.alibaba.excel.annotation.ExcelProperty; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.io.Serializable; + +/** + * + * @author 14212 + * @TableName project_manager + */ +@TableName(value ="project_manager") +@Data +public class ProjectManager implements Serializable { + + @ExcelProperty(value = "序号") + //@TableId(type = IdType.AUTO) + @TableField(exist = false) + private Integer id; + + + @ExcelProperty(value = "项目名称") + private String projectName; + + /** + * 可以有多个项目负责人 + */ + @ExcelProperty(value = "项目负责人") + private String manager; + + @ExcelProperty(value = "联系方式") + private String phone; + + @ExcelIgnore + private String tenantId; + + @TableField(exist = false) + private static final long serialVersionUID = 1L; +} diff --git a/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/listener/ProjectManagerListener.java b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/listener/ProjectManagerListener.java new file mode 100644 index 0000000..c83c9f0 --- /dev/null +++ b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/listener/ProjectManagerListener.java @@ -0,0 +1,63 @@ +package org.dromara.platform.listener; + +import com.alibaba.excel.context.AnalysisContext; + + +import com.alibaba.excel.read.listener.ReadListener; +import lombok.extern.slf4j.Slf4j; +import org.dromara.platform.domain.entity.ProjectManager; +import org.dromara.platform.service.ProjectManagerService; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.ArrayList; +import java.util.List; + +/** + * 自定义监听器读数据 + * @author gjh + */ +@Slf4j +public class ProjectManagerListener implements ReadListener { + + private List list = new ArrayList<>(); + /** + * 自己定义一个缓冲量 + */ + private static final int BATCH_COUNT = 20; + @Autowired + private ProjectManagerService projectManagerService; + + public ProjectManagerListener(ProjectManagerService projectManagerService) { + this.projectManagerService = projectManagerService; + } + + /** + * 每读一行数据都会调用这个方法 + * + * @param projectManager + * @param analysisContext + */ + @Override + public void invoke(ProjectManager projectManager, AnalysisContext analysisContext) { + // 读取一行数据就添加到集合 + list.add(projectManager); + // 判断是否到达缓存量了 + if (list.size() >= BATCH_COUNT){ + // 操作数据库 + projectManagerService.addData(list); + list = new ArrayList<>(BATCH_COUNT); + } + } + + /** + * 读完整个excel之后再调用这个方法 + * + * @param analysisContext + */ + @Override + public void doAfterAllAnalysed(AnalysisContext analysisContext) { + if (list.size()>0){ + projectManagerService.addData(list); + } + } +} diff --git a/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/mapper/ProjectManagerMapper.java b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/mapper/ProjectManagerMapper.java new file mode 100644 index 0000000..d01b5af --- /dev/null +++ b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/mapper/ProjectManagerMapper.java @@ -0,0 +1,29 @@ +package org.dromara.platform.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +import org.apache.ibatis.annotations.ResultType; +import org.apache.ibatis.annotations.Select; +import org.dromara.platform.domain.entity.ProjectManager; + +import java.util.List; + +/** +* @author 14212 +* @description 针对表【project_manager】的数据库操作Mapper +* @createDate 2025-01-08 09:22:14 +* @Entity com.gejunhao.pojo.ProjectManager +*/ +public interface ProjectManagerMapper extends BaseMapper { + /** + *从数据库中获取数据 + * @return + */ + @Select("select * from project_manager") + @ResultType(ProjectManager.class) + List getDate(); +} + + + + diff --git a/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/service/ProjectManagerService.java b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/service/ProjectManagerService.java new file mode 100644 index 0000000..b5ac916 --- /dev/null +++ b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/service/ProjectManagerService.java @@ -0,0 +1,28 @@ +package org.dromara.platform.service; + + + +import com.baomidou.mybatisplus.extension.service.IService; +import org.dromara.platform.domain.entity.ProjectManager; + +import java.util.List; + +/** +* @author 14212 +* @description 针对表【project_manager】的数据库操作Service +* @createDate 2025-01-08 09:22:14 +*/ +public interface ProjectManagerService extends IService { + /** + * 批量插入数据 + * @param list + */ + public void addData(List list); + + + /** + * 从数据库中获取数据 + * @return + */ + public List getData(); +} diff --git a/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/service/impl/ProjectManagerServiceImpl.java b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/service/impl/ProjectManagerServiceImpl.java new file mode 100644 index 0000000..963226a --- /dev/null +++ b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/service/impl/ProjectManagerServiceImpl.java @@ -0,0 +1,38 @@ +package org.dromara.platform.service.impl; + + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import jakarta.annotation.Resource; +import org.dromara.platform.domain.entity.ProjectManager; +import org.dromara.platform.mapper.ProjectManagerMapper; +import org.dromara.platform.service.ProjectManagerService; +import org.springframework.stereotype.Service; +import java.util.List; + +/** +* @author 14212 +* @description 针对表【project_manager】的数据库操作Service实现 +* @createDate 2025-01-08 09:22:14 + */ +@Service +public class ProjectManagerServiceImpl extends ServiceImpl + implements ProjectManagerService { + @Resource + private ProjectManagerMapper projectManagerMapper; + + @Override + public void addData(List list) { + this.saveBatch(list); + } + + @Override + public List getData() { + List projectManagers = projectManagerMapper.getDate(); + return projectManagers; + } + +} + + + + diff --git a/ruoyi-modules/pom.xml b/ruoyi-modules/pom.xml index daff497..b8286ce 100644 --- a/ruoyi-modules/pom.xml +++ b/ruoyi-modules/pom.xml @@ -15,6 +15,7 @@ ruoyi-job ruoyi-system ruoyi-workflow + guoyan-platform ruoyi-modules