Browse Source

学习资料 文件上传功能完善

jingyuanchao 2 năm trước cách đây
mục cha
commit
8c76055870

+ 0 - 5
pom.xml

@@ -229,11 +229,6 @@
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-bootstrap</artifactId>
         </dependency>
-        <dependency>
-            <groupId>org.projectlombok</groupId>
-            <artifactId>lombok</artifactId>
-            <version>${lombok.version}</version>
-        </dependency>
     </dependencies>
 
 

+ 13 - 16
soc-api/soc-api-system/src/main/java/com/xunmei/system/api/domain/SysFile.java

@@ -5,11 +5,10 @@ import org.apache.commons.lang3.builder.ToStringStyle;
 
 /**
  * 文件信息
- * 
+ *
  * @author xunmei
  */
-public class SysFile
-{
+public class SysFile {
     /**
      * 文件名称
      */
@@ -26,23 +25,21 @@ public class SysFile
      */
     private String code;
 
-    public String getName()
-    {
+    private String realName;
+
+    public String getName() {
         return name;
     }
 
-    public void setName(String name)
-    {
+    public void setName(String name) {
         this.name = name;
     }
 
-    public String getUrl()
-    {
+    public String getUrl() {
         return url;
     }
 
-    public void setUrl(String url)
-    {
+    public void setUrl(String url) {
         this.url = url;
     }
 
@@ -56,10 +53,10 @@ public class SysFile
 
     @Override
     public String toString() {
-        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
-            .append("name", getName())
-            .append("url", getUrl())
-            .append("code", getCode())
-            .toString();
+        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
+                .append("name", getName())
+                .append("url", getUrl())
+                .append("code", getCode())
+                .toString();
     }
 }

+ 5 - 0
soc-common/soc-common-core/pom.xml

@@ -128,5 +128,10 @@
             <groupId>io.netty</groupId>
             <artifactId>netty-common</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 1 - 1
soc-common/soc-common-core/src/main/java/com/xunmei/common/core/utils/file/MimeTypeUtils.java

@@ -30,7 +30,7 @@ public class MimeTypeUtils
             // 图片
             "bmp", "gif", "jpg", "jpeg", "png",
             // word excel powerpoint
-            "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
+            "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt","et","zip",
             // 压缩文件
             "rar", "zip", "gz", "bz2",
             // 视频格式

+ 5 - 5
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/edu/controller/SysKnowledgeController.java

@@ -39,7 +39,7 @@ public class SysKnowledgeController extends BaseController {
      * 查询知识库标签列表
      */
     @ApiOperation(value = "查询SysKnowledge列表")
-    @RequiresPermissions("system:knowledge:list")
+    @RequiresPermissions("core:knowledge:list")
     @GetMapping("/list")
     public TableDataInfo list(SysKnowledge sysKnowledge) {
         return sysKnowledgeService.selectPage(sysKnowledge);
@@ -50,7 +50,7 @@ public class SysKnowledgeController extends BaseController {
      * 获取知识库标签详细信息
      */
     @ApiOperation(value = "获取SysKnowledge详细信息")
-    @RequiresPermissions("system:knowledge:query")
+    @RequiresPermissions("core:knowledge:query")
     @GetMapping(value = "/{id}")
     public AjaxResult getInfo(@PathVariable("id") Long id) {
         return success(sysKnowledgeService.selectSysKnowledgeById(id));
@@ -60,7 +60,7 @@ public class SysKnowledgeController extends BaseController {
      * 新增知识库标签
      */
     @ApiOperation(value = "新增SysKnowledge")
-    @RequiresPermissions("system:knowledge:add")
+    @RequiresPermissions("core:knowledge:add")
     @Log(title = "知识库标签", businessType = BusinessType.INSERT)
     @PostMapping
     public AjaxResult add(@RequestBody SysKnowledge sysKnowledge) {
@@ -71,7 +71,7 @@ public class SysKnowledgeController extends BaseController {
      * 修改知识库标签
      */
     @ApiOperation(value = "修改SysKnowledge")
-    @RequiresPermissions("system:knowledge:edit")
+    @RequiresPermissions("core:knowledge:edit")
     @Log(title = "知识库标签", businessType = BusinessType.UPDATE)
     @PutMapping
     public AjaxResult edit(@RequestBody SysKnowledge sysKnowledge) {
@@ -82,7 +82,7 @@ public class SysKnowledgeController extends BaseController {
      * 删除知识库标签
      */
     @ApiOperation(value = "删除SysKnowledge")
-    @RequiresPermissions("system:knowledge:remove")
+    @RequiresPermissions("core:knowledge:remove")
     @Log(title = "知识库标签", businessType = BusinessType.DELETE)
     @DeleteMapping("/{ids}")
     public AjaxResult remove(@PathVariable Long[] ids) {

+ 20 - 6
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/edu/controller/SysLearningMaterialsController.java

@@ -21,6 +21,9 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import com.xunmei.common.core.web.page.TableDataInfo;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
 /**
  * 学习资料Controller
  *
@@ -38,7 +41,7 @@ public class SysLearningMaterialsController extends BaseController {
      * 查询学习资料列表
      */
     @ApiOperation(value = "查询SysLearningMaterials列表")
-    @RequiresPermissions("system:materials:list")
+    @RequiresPermissions("core:materials:list")
     @GetMapping("/list")
     public TableDataInfo list(SysLearningMaterialsPageDto sysLearningMaterials) {
         return sysLearningMaterialsService.selectPage(sysLearningMaterials);
@@ -49,7 +52,7 @@ public class SysLearningMaterialsController extends BaseController {
      * 获取学习资料详细信息
      */
     @ApiOperation(value = "获取SysLearningMaterials详细信息")
-    @RequiresPermissions("system:materials:query")
+    @RequiresPermissions("core:materials:query")
     @GetMapping(value = "/{id}")
     public AjaxResult getInfo(@PathVariable("id") Long id) {
         return success(sysLearningMaterialsService.selectSysLearningMaterialsById(id));
@@ -59,7 +62,7 @@ public class SysLearningMaterialsController extends BaseController {
      * 新增学习资料
      */
     @ApiOperation(value = "新增SysLearningMaterials")
-    @RequiresPermissions("system:materials:add")
+    @RequiresPermissions("core:materials:add")
     @Log(title = "学习资料", businessType = BusinessType.INSERT)
     @PostMapping
     public AjaxResult add(@RequestBody SysLearningMaterials sysLearningMaterials) {
@@ -70,7 +73,7 @@ public class SysLearningMaterialsController extends BaseController {
      * 修改学习资料
      */
     @ApiOperation(value = "修改SysLearningMaterials")
-    @RequiresPermissions("system:materials:edit")
+    @RequiresPermissions("core:materials:edit")
     @Log(title = "学习资料", businessType = BusinessType.UPDATE)
     @PutMapping
     public AjaxResult edit(@RequestBody SysLearningMaterials sysLearningMaterials) {
@@ -81,7 +84,7 @@ public class SysLearningMaterialsController extends BaseController {
      * 删除学习资料
      */
     @ApiOperation(value = "删除SysLearningMaterials")
-    @RequiresPermissions("system:materials:remove")
+    @RequiresPermissions("core:materials:remove")
     @Log(title = "学习资料", businessType = BusinessType.DELETE)
     @DeleteMapping("/{ids}")
     public AjaxResult remove(@PathVariable Long[] ids) {
@@ -89,10 +92,21 @@ public class SysLearningMaterialsController extends BaseController {
     }
 
     /**
+     * 导出excel
+     */
+    @ApiOperation(value = "修改SysLearningMaterials")
+    @RequiresPermissions("core:materials:export")
+    @Log(title = "学习资料", businessType = BusinessType.UPDATE)
+    @PostMapping("/export")
+    public void export(SysLearningMaterialsPageDto sysLearningMaterials, HttpServletResponse response) throws IOException {
+        sysLearningMaterialsService.export(sysLearningMaterials,response);
+    }
+
+    /**
      * 修改学习资料,设置未公开/公开
      */
     @ApiOperation(value = "修改SysLearningMaterials")
-    @RequiresPermissions("system:materials:edit")
+    @RequiresPermissions("core:materials:edit")
     @Log(title = "学习资料", businessType = BusinessType.UPDATE)
     @GetMapping("/{id}/{isOpen}")
     public AjaxResult editOpen(@PathVariable Long id, @PathVariable Integer isOpen) {

+ 7 - 0
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/edu/service/ISysLearningMaterialsService.java

@@ -1,5 +1,7 @@
 package com.xunmei.core.edu.service;
 
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.util.List;
 
 import com.baomidou.mybatisplus.extension.service.IService;
@@ -7,6 +9,8 @@ import com.xunmei.common.core.web.page.TableDataInfo;
 import com.xunmei.common.core.domain.edu.domain.SysLearningMaterials;
 import com.xunmei.common.core.domain.edu.dto.SysLearningMaterialsPageDto;
 
+import javax.servlet.http.HttpServletResponse;
+
 /**
  * 学习资料Service接口
  *
@@ -71,4 +75,7 @@ public interface ISysLearningMaterialsService extends IService<SysLearningMateri
     public TableDataInfo selectPage(SysLearningMaterialsPageDto sysLearningMaterials);
 
     int editOpen(Long id, Integer isOpen);
+
+    void export(SysLearningMaterialsPageDto sysLearningMaterials, HttpServletResponse response) throws IOException;
+
 }

+ 71 - 5
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/edu/service/impl/SysLearningMaterialsServiceImpl.java

@@ -1,13 +1,18 @@
 package com.xunmei.core.edu.service.impl;
 
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.*;
 
 import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.poi.excel.ExcelUtil;
+import cn.hutool.poi.excel.ExcelWriter;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.xunmei.common.core.constant.Constants;
 import com.xunmei.common.core.constant.SecurityConstants;
 import com.xunmei.common.core.domain.R;
+import com.xunmei.common.core.utils.DateHelper;
 import com.xunmei.common.core.utils.DateUtils;
 import com.xunmei.common.security.utils.SecurityUtils;
 import com.xunmei.common.core.domain.edu.domain.SysLearningMaterials;
@@ -20,13 +25,15 @@ import com.xunmei.system.api.domain.SysOrg;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.Arrays;
 import java.util.stream.Stream;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.xunmei.common.core.web.page.TableDataInfo;
 
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+
 /**
  * 学习资料Service业务层处理
  *
@@ -53,6 +60,9 @@ public class SysLearningMaterialsServiceImpl extends ServiceImpl<SysLearningMate
         sysLearningMaterials.setUserId(SecurityUtils.getUserId());
         //获取数据
         page = sysLearningMaterialsMapper.selectPageData(page, sysLearningMaterials);
+        for (SysLearningMaterialsPageVo record : page.getRecords()) {
+            extractStringFromUrl(record);
+        }
         //抓换为TableDataInfo适配前端
         TableDataInfo tableDataInfo = new TableDataInfo();
         tableDataInfo.setMsg("操作成功");
@@ -68,13 +78,22 @@ public class SysLearningMaterialsServiceImpl extends ServiceImpl<SysLearningMate
         if (sysOrg == null || ObjectUtil.isEmpty(sysOrg.getPath())) {
             throw new RuntimeException("机构不存在或机构信息缺失!");
         }
-        String[] split = "4352539158904832-4352539158904833-".split("-");
+        String[] split = sysOrg.getPath().split("-");
         List<Long> list = new ArrayList<>();
         Stream.of(split).forEach(s -> list.add(Long.valueOf(s)));
         list.remove(sysLearningMaterials.getOrgId());
         sysLearningMaterials.setOrgIdList(list);
     }
 
+    public void extractStringFromUrl(SysLearningMaterialsPageVo record) {
+        if (ObjectUtil.isEmpty(record.getFile()) || !record.getFile().contains(Constants.HTTP)) {
+            return;
+        }
+        // 获取最后一个斜杠的索引
+        int lastSlashIndex = record.getFile().lastIndexOf("/");
+        // 获取最后一个斜杠后面的字符串
+        record.setFile(record.getFile().substring(lastSlashIndex + 1));
+    }
 
     /**
      * 查询学习资料
@@ -166,4 +185,51 @@ public class SysLearningMaterialsServiceImpl extends ServiceImpl<SysLearningMate
         }
         return 0;
     }
+
+    @Override
+    public void export(SysLearningMaterialsPageDto sysLearningMaterials, HttpServletResponse response) throws
+            IOException {
+
+        SysOrg sysOrg = null;
+        try {
+            R<SysOrg> sysOrgR = remoteOrgService.selectSysOrgById(sysLearningMaterials.getOrgId(), SecurityConstants.INNER);
+            sysOrg = sysOrgR.getData();
+        } catch (Exception e) {
+            throw new RuntimeException("获取机构信息失败,请确认服务可用!");
+        }
+
+        sysLearningMaterials.setPageSize((long) Integer.MAX_VALUE);
+        final TableDataInfo tableDataInfo = this.selectPage(sysLearningMaterials);
+        if (ObjectUtil.isEmpty(tableDataInfo.getRows())) {
+            throw new RuntimeException("导出数据为空!");
+        }
+        List<SysLearningMaterialsPageVo> list = (List<SysLearningMaterialsPageVo>) tableDataInfo.getRows();
+
+        //在内存操作,写到浏览器
+        ExcelWriter writer = ExcelUtil.getWriter(true);
+        writer.addHeaderAlias("title", "资料标题");
+        writer.addHeaderAlias("knowledgeName", "资料类型");
+        writer.addHeaderAlias("orgName", "所属机构");
+        writer.addHeaderAlias("isOpen", "是否公开");
+        writer.addHeaderAlias("createBy", "上传人");
+        writer.addHeaderAlias("createTime", "上传时间");
+
+        //只保留别名的数据
+        writer.setOnlyAlias(true);
+        // 默认配置
+        writer.write(list, true);
+        // 设置content—type
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
+        // 设置标题
+        String fileName = URLEncoder.encode("【" + sysOrg.getName() + "】-履职记录" + DateHelper.getDateString(new Date()), "UTF-8");
+        //Content-disposition是MIME协议的扩展,MIME协议指示MIME用户代理如何显示附加的文件。
+        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
+        ServletOutputStream outputStream = response.getOutputStream();
+        //将Writer刷新到OutPut
+        writer.flush(outputStream, true);
+        outputStream.close();
+        writer.close();
+
+
+    }
 }

+ 4 - 3
soc-modules/soc-modules-core/src/main/resources/mapper/edu/SysLearningMaterialsMapper.xml

@@ -95,16 +95,17 @@
         select m.id,
         title,
         k.name as knowledgeName,
-        org_id,
-        org_name,
+        m.org_id,
+        m.org_name,
         is_open,
         file,
         remark as remark,
-        m.create_by,
+        u.name as create_by,
         m.create_time,
         m.update_time as updateTime
         from sys_learning_materials m
         inner join sys_knowledge k on m.knowledge_id = k.id
+        inner join sys_user u on u.id=m.create_by
         where m.deleted = 0
         and k.deleted = 0
         <choose>