Преглед на файлове

增加文档模板相关改造

wukai преди 1 година
родител
ревизия
e43a40a79c

+ 12 - 2
doc-biz/src/main/java/com/doc/biz/controller/ApiController.java

@@ -45,9 +45,8 @@ public class ApiController extends BaseController {
      * 文件预览
      *
      * @param docId docId
-     * @return
      */
-    @ApiOperation("文件预览")
+    @ApiOperation("通过docId文件预览")
     @GetMapping("/access/{docId}")
     public ResponseEntity<Object> access(@PathVariable(name = "docId") Long docId, @RequestParam Long uid) {
         DocInfo info = docInfoService.selectDocInfoByDocId(docId);
@@ -61,6 +60,17 @@ public class ApiController extends BaseController {
     }
 
     /**
+     * 通过fileId文件预览
+     *
+     * @param fileId fileId
+     */
+    @ApiOperation("通过fileId文件预览")
+    @GetMapping("/view/{fileId}")
+    public ResponseEntity<Object> view(@PathVariable(name = "fileId") String fileId) {
+        return mongoService.download(fileId, false);
+    }
+
+    /**
      * 历史文件预览
      *
      * @param fileId fileId

+ 137 - 0
doc-biz/src/main/java/com/doc/biz/controller/DocTemplateController.java

@@ -0,0 +1,137 @@
+package com.doc.biz.controller;
+
+import com.doc.biz.domain.DocTemplate;
+import com.doc.biz.service.IDocTemplateService;
+import com.doc.biz.service.IMongoService;
+import com.doc.biz.vo.DocumentVO;
+import com.doc.common.annotation.Log;
+import com.doc.common.core.controller.BaseController;
+import com.doc.common.core.domain.AjaxResult;
+import com.doc.common.core.page.TableDataInfo;
+import com.doc.common.enums.BusinessType;
+import com.doc.common.utils.file.FileUtils;
+import com.doc.common.utils.poi.ExcelUtil;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
+import java.io.InputStream;
+import java.util.List;
+
+/**
+ * 文档模板Controller
+ *
+ * @author wukai
+ * @date 2024-01-10
+ */
+@Api(tags = "文档模板")
+@RestController
+@RequestMapping("/biz/template")
+@Slf4j
+public class DocTemplateController extends BaseController {
+    @Resource
+    private IDocTemplateService docTemplateService;
+    @Resource
+    private IMongoService mongoService;
+
+    /**
+     * 查询文档模板列表
+     */
+    @ApiOperation("查询文档模板列表")
+    @PreAuthorize("@ss.hasPermi('biz:template:list')")
+    @GetMapping("/list")
+    public TableDataInfo list(DocTemplate docTemplate) {
+        startPage();
+        List<DocTemplate> list = docTemplateService.selectDocTemplateList(docTemplate);
+        return getDataTable(list);
+    }
+
+    /**
+     * 导出文档模板列表
+     */
+    @ApiOperation("导出文档模板列表")
+    @PreAuthorize("@ss.hasPermi('biz:template:export')")
+    @Log(title = "文档模板", businessType = BusinessType.EXPORT)
+    @PostMapping("/export")
+    public void export(HttpServletResponse response, DocTemplate docTemplate) {
+        List<DocTemplate> list = docTemplateService.selectDocTemplateList(docTemplate);
+        ExcelUtil<DocTemplate> util = new ExcelUtil<DocTemplate>(DocTemplate.class);
+        util.exportExcel(response, list, "文档模板数据");
+    }
+
+    /**
+     * 获取文档模板详细信息
+     */
+    @ApiOperation("获取文档模板详细信息")
+    @PreAuthorize("@ss.hasPermi('biz:template:query')")
+    @GetMapping(value = "/{tmplId}")
+    public AjaxResult getInfo(@PathVariable("tmplId") Long tmplId) {
+        return success(docTemplateService.selectDocTemplateByTmplId(tmplId));
+    }
+
+    /**
+     * 新增文档模板
+     */
+    @ApiOperation("新增文档模板")
+    @PreAuthorize("@ss.hasPermi('biz:template:add')")
+    @Log(title = "文档模板", businessType = BusinessType.INSERT)
+    @PostMapping
+    public AjaxResult add(@RequestBody DocTemplate docTemplate) {
+        try {
+            String filePath = "";
+            String fileName = "";
+            switch (docTemplate.getTmplType()) {
+                case "word":
+                    filePath = "file/new.docx";
+                    fileName = docTemplate.getTmplName() + ".docx";
+                    break;
+                case "excel":
+                    filePath = "file/new.xlsx";
+                    fileName = docTemplate.getTmplName() + ".xlsx";
+                    break;
+                case "ppt":
+                    filePath = "file/new.pptx";
+                    fileName = docTemplate.getTmplName() + ".pptx";
+                    break;
+                default:
+            }
+
+            InputStream is = this.getClass().getResourceAsStream("/" + filePath);
+            MultipartFile multipartFile = FileUtils.getMultipartFile(is, fileName);
+            DocumentVO vo = mongoService.uploadFile(multipartFile);
+            docTemplate.setFileId(vo.getFileId());
+            return toAjax(docTemplateService.insertDocTemplate(docTemplate));
+        } catch (Exception e) {
+            log.error("新建文件出错啦:{}", e.getMessage());
+            return error("新建文件出错");
+        }
+    }
+
+    /**
+     * 修改文档模板
+     */
+    @ApiOperation("修改文档模板")
+    @PreAuthorize("@ss.hasPermi('biz:template:edit')")
+    @Log(title = "文档模板", businessType = BusinessType.UPDATE)
+    @PutMapping
+    public AjaxResult edit(@RequestBody DocTemplate docTemplate) {
+        return toAjax(docTemplateService.updateDocTemplate(docTemplate));
+    }
+
+    /**
+     * 删除文档模板
+     */
+    @ApiOperation("删除文档模板")
+    @PreAuthorize("@ss.hasPermi('biz:template:remove')")
+    @Log(title = "文档模板", businessType = BusinessType.DELETE)
+    @DeleteMapping("/{tmplIds}")
+    public AjaxResult remove(@PathVariable Long[] tmplIds) {
+        return toAjax(docTemplateService.deleteDocTemplateByTmplIds(tmplIds));
+    }
+
+}

+ 65 - 5
doc-biz/src/main/java/com/doc/biz/controller/OnlyOfficeController.java

@@ -2,8 +2,10 @@ package com.doc.biz.controller;
 
 import com.alibaba.fastjson2.JSONObject;
 import com.doc.biz.domain.DocInfo;
+import com.doc.biz.domain.DocTemplate;
 import com.doc.biz.domain.DocVersion;
 import com.doc.biz.service.IDocInfoService;
+import com.doc.biz.service.IDocTemplateService;
 import com.doc.biz.service.IDocVersionService;
 import com.doc.biz.service.IMongoService;
 import com.doc.biz.vo.DocumentVO;
@@ -59,6 +61,8 @@ public class OnlyOfficeController {
     private ISysUserService userService;
     @Resource
     private ISysOperLogService operLogService;
+    @Resource
+    private IDocTemplateService templateService;
 
     /**
      * only office修改回调
@@ -70,6 +74,30 @@ public class OnlyOfficeController {
     @RequestMapping("/callback/{id}")
     @ResponseBody
     public void save(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws Exception {
+        onlyoffice(id, request, response, false);
+    }
+
+    /**
+     * only office修改回调
+     *
+     * @param id       文档ID
+     * @param request  请求参数
+     * @param response 响应参数
+     */
+    @RequestMapping("/tmpl/callback/{id}")
+    @ResponseBody
+    public void tmplSave(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws Exception {
+        onlyoffice(id, request, response, true);
+    }
+
+    /**
+     * 保存文件及后续操作
+     * @param id
+     * @param request  请求参数
+     * @param response 响应参数
+     * @param isTmpl 是否模板
+     */
+    public void onlyoffice(Long id, HttpServletRequest request, HttpServletResponse response, boolean isTmpl) throws Exception {
         PrintWriter writer = null;
         String body = "";
         long start = System.currentTimeMillis();
@@ -110,7 +138,6 @@ public class OnlyOfficeController {
             //获取用户信息
             SysUser user = userService.selectUserById(jsonObj.getJSONArray("actions").getJSONObject(0).getLong("userid"));
             String downloadUri = (String) jsonObj.get("url");
-            DocInfo info = docInfoService.selectDocInfoByDocId(id);
 
             try {
                 MultipartFile multipartFile;
@@ -123,16 +150,30 @@ public class OnlyOfficeController {
                     HttpResponse res = client.execute(httpget);
                     HttpEntity entity = res.getEntity();
                     try (InputStream is = entity.getContent()) {
-                        multipartFile = FileUtils.getMultipartFile(is, info.getFileName());
-                        process(info, user, multipartFile);
+                        if (isTmpl) {
+                            DocTemplate tmpl = templateService.selectDocTemplateByTmplId(id);
+                            multipartFile = FileUtils.getMultipartFile(is, tmpl.getTmplName());
+                            processTmpl(tmpl, user, multipartFile);
+                        } else {
+                            DocInfo info = docInfoService.selectDocInfoByDocId(id);
+                            multipartFile = FileUtils.getMultipartFile(is, info.getFileName());
+                            process(info, user, multipartFile);
+                        }
                     } catch (Exception e) {
                         throw e;
                     }
                 } else {
                     URL url = new URL(downloadUri);
                     HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
-                    multipartFile = FileUtils.getMultipartFile(connection.getInputStream(), info.getFileName());
-                    process(info, user, multipartFile);
+                    if (isTmpl) {
+                        DocTemplate tmpl = templateService.selectDocTemplateByTmplId(id);
+                        multipartFile = FileUtils.getMultipartFile(connection.getInputStream(), tmpl.getTmplName());
+                        processTmpl(tmpl, user, multipartFile);
+                    } else {
+                        DocInfo info = docInfoService.selectDocInfoByDocId(id);
+                        multipartFile = FileUtils.getMultipartFile(connection.getInputStream(), info.getFileName());
+                        process(info, user, multipartFile);
+                    }
                     connection.disconnect();
                 }
 
@@ -179,6 +220,25 @@ public class OnlyOfficeController {
     }
 
     /**
+     * 处理新的文件信息
+     *
+     * @param tmpl          模板信息
+     * @param user          用户信息
+     * @param multipartFile 多媒体文件
+     */
+    private void processTmpl(DocTemplate tmpl, SysUser user, MultipartFile multipartFile) {
+        try {
+            //保存新的文件信息
+            DocumentVO vo = mongoService.uploadFile(multipartFile);
+            tmpl.setFileId(vo.getFileId());
+            tmpl.setUpdateBy(user.getUserName());
+            templateService.updateDocTemplate(tmpl);
+        } catch (Exception e) {
+            log.error("onlyoffice回调出错啦:{}", e.getMessage());
+        }
+    }
+
+    /**
      * 插入操作日志
      *
      * @param body   请求参数

+ 46 - 0
doc-biz/src/main/java/com/doc/biz/domain/DocTemplate.java

@@ -0,0 +1,46 @@
+package com.doc.biz.domain;
+
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.doc.common.annotation.Excel;
+import com.doc.common.core.domain.BaseEntity;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * 文档模板对象 doc_template
+ *
+ * @author wukai
+ * @date 2024-01-10
+ */
+@Data
+@ApiModel(value = "DocTemplate", description = "文档模板")
+public class DocTemplate extends BaseEntity {
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 模板ID
+     */
+    @ApiModelProperty("模板ID")
+    @TableId
+    private Long tmplId;
+
+    /**
+     * 模板类型
+     */
+    @ApiModelProperty("模板类型")
+    @Excel(name = "模板类型")
+    private String tmplType;
+
+
+    /**
+     * 模板名称
+     */
+    @ApiModelProperty("模板名称")
+    @Excel(name = "模板名称")
+    private String tmplName;
+
+    @ApiModelProperty("文件ID")
+    @Excel(name = "文件ID")
+    private String fileId;
+}

+ 62 - 0
doc-biz/src/main/java/com/doc/biz/mapper/DocTemplateMapper.java

@@ -0,0 +1,62 @@
+package com.doc.biz.mapper;
+
+import java.util.List;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.doc.biz.domain.DocTemplate;
+
+/**
+ * 文档模板Mapper接口
+ * 
+ * @author wukai
+ * @date 2024-01-10
+ */
+public interface DocTemplateMapper extends BaseMapper<DocTemplate>
+{
+    /**
+     * 查询文档模板
+     * 
+     * @param tmplId 文档模板主键
+     * @return 文档模板
+     */
+    public DocTemplate selectDocTemplateByTmplId(Long tmplId);
+
+    /**
+     * 查询文档模板列表
+     * 
+     * @param docTemplate 文档模板
+     * @return 文档模板集合
+     */
+    public List<DocTemplate> selectDocTemplateList(DocTemplate docTemplate);
+
+    /**
+     * 新增文档模板
+     * 
+     * @param docTemplate 文档模板
+     * @return 结果
+     */
+    public int insertDocTemplate(DocTemplate docTemplate);
+
+    /**
+     * 修改文档模板
+     * 
+     * @param docTemplate 文档模板
+     * @return 结果
+     */
+    public int updateDocTemplate(DocTemplate docTemplate);
+
+    /**
+     * 删除文档模板
+     * 
+     * @param tmplId 文档模板主键
+     * @return 结果
+     */
+    public int deleteDocTemplateByTmplId(Long tmplId);
+
+    /**
+     * 批量删除文档模板
+     * 
+     * @param tmplIds 需要删除的数据主键集合
+     * @return 结果
+     */
+    public int deleteDocTemplateByTmplIds(Long[] tmplIds);
+}

+ 61 - 0
doc-biz/src/main/java/com/doc/biz/service/IDocTemplateService.java

@@ -0,0 +1,61 @@
+package com.doc.biz.service;
+
+import com.doc.biz.domain.DocTemplate;
+
+import java.util.List;
+
+/**
+ * 文档模板Service接口
+ *
+ * @author wukai
+ * @date 2024-01-10
+ */
+public interface IDocTemplateService {
+    /**
+     * 查询文档模板
+     *
+     * @param tmplId 文档模板主键
+     * @return 文档模板
+     */
+    public DocTemplate selectDocTemplateByTmplId(Long tmplId);
+
+    /**
+     * 查询文档模板列表
+     *
+     * @param docTemplate 文档模板
+     * @return 文档模板集合
+     */
+    public List<DocTemplate> selectDocTemplateList(DocTemplate docTemplate);
+
+    /**
+     * 新增文档模板
+     *
+     * @param docTemplate 文档模板
+     * @return 结果
+     */
+    public int insertDocTemplate(DocTemplate docTemplate);
+
+    /**
+     * 修改文档模板
+     *
+     * @param docTemplate 文档模板
+     * @return 结果
+     */
+    public int updateDocTemplate(DocTemplate docTemplate);
+
+    /**
+     * 批量删除文档模板
+     *
+     * @param tmplIds 需要删除的文档模板主键集合
+     * @return 结果
+     */
+    public int deleteDocTemplateByTmplIds(Long[] tmplIds);
+
+    /**
+     * 删除文档模板信息
+     *
+     * @param tmplId 文档模板主键
+     * @return 结果
+     */
+    public int deleteDocTemplateByTmplId(Long tmplId);
+}

+ 90 - 0
doc-biz/src/main/java/com/doc/biz/service/impl/DocTemplateServiceImpl.java

@@ -0,0 +1,90 @@
+package com.doc.biz.service.impl;
+
+import com.doc.biz.domain.DocTemplate;
+import com.doc.biz.mapper.DocTemplateMapper;
+import com.doc.biz.service.IDocTemplateService;
+import com.doc.common.utils.DateUtils;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * 文档模板Service业务层处理
+ *
+ * @author wukai
+ * @date 2024-01-10
+ */
+@Service
+public class DocTemplateServiceImpl implements IDocTemplateService {
+    @Resource
+    private DocTemplateMapper docTemplateMapper;
+
+    /**
+     * 查询文档模板
+     *
+     * @param tmplId 文档模板主键
+     * @return 文档模板
+     */
+    @Override
+    public DocTemplate selectDocTemplateByTmplId(Long tmplId) {
+        return docTemplateMapper.selectDocTemplateByTmplId(tmplId);
+    }
+
+    /**
+     * 查询文档模板列表
+     *
+     * @param docTemplate 文档模板
+     * @return 文档模板
+     */
+    @Override
+    public List<DocTemplate> selectDocTemplateList(DocTemplate docTemplate) {
+        return docTemplateMapper.selectDocTemplateList(docTemplate);
+    }
+
+    /**
+     * 新增文档模板
+     *
+     * @param docTemplate 文档模板
+     * @return 结果
+     */
+    @Override
+    public int insertDocTemplate(DocTemplate docTemplate) {
+        docTemplate.setCreateTime(DateUtils.getNowDate());
+        return docTemplateMapper.insertDocTemplate(docTemplate);
+    }
+
+    /**
+     * 修改文档模板
+     *
+     * @param docTemplate 文档模板
+     * @return 结果
+     */
+    @Override
+    public int updateDocTemplate(DocTemplate docTemplate) {
+        docTemplate.setUpdateTime(DateUtils.getNowDate());
+        return docTemplateMapper.updateDocTemplate(docTemplate);
+    }
+
+    /**
+     * 批量删除文档模板
+     *
+     * @param tmplIds 需要删除的文档模板主键
+     * @return 结果
+     */
+    @Override
+    public int deleteDocTemplateByTmplIds(Long[] tmplIds) {
+        return docTemplateMapper.deleteDocTemplateByTmplIds(tmplIds);
+    }
+
+    /**
+     * 删除文档模板信息
+     *
+     * @param tmplId 文档模板主键
+     * @return 结果
+     */
+    @Override
+    public int deleteDocTemplateByTmplId(Long tmplId) {
+        return docTemplateMapper.deleteDocTemplateByTmplId(tmplId);
+    }
+}

+ 91 - 0
doc-biz/src/main/resources/mapper/biz/DocTemplateMapper.xml

@@ -0,0 +1,91 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.doc.biz.mapper.DocTemplateMapper">
+
+    <resultMap type="DocTemplate" id="DocTemplateResult">
+        <result property="tmplId"    column="TMPL_ID"    />
+        <result property="tmplType"    column="TMPL_TYPE"    />
+        <result property="tmplName"    column="TMPL_NAME"    />
+        <result property="fileId"    column="FILE_ID"    />
+        <result property="createBy"    column="CREATE_BY"    />
+        <result property="createTime"    column="CREATE_TIME"    />
+        <result property="updateBy"    column="UPDATE_BY"    />
+        <result property="updateTime"    column="UPDATE_TIME"    />
+        <result property="remark"    column="REMARK"    />
+    </resultMap>
+
+    <sql id="selectDocTemplateVo">
+        select TMPL_ID, TMPL_TYPE, TMPL_NAME, FILE_ID, CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME, REMARK from doc_template
+    </sql>
+
+    <select id="selectDocTemplateList" parameterType="DocTemplate" resultMap="DocTemplateResult">
+        <include refid="selectDocTemplateVo"/>
+        <where>
+            <if test="tmplType != null  and tmplType != ''"> and TMPL_TYPE = #{tmplType}</if>
+            <if test="tmplName != null  and tmplName != ''"> and TMPL_NAME like concat('%', #{tmplName}, '%')</if>
+            <if test="fileId != null  and fileId != ''"> and FILE_ID = #{fileId}</if>
+            <if test="createBy != null  and createBy != ''"> and CREATE_BY = #{createBy}</if>
+            <if test="createTime != null "> and CREATE_TIME = #{createTime}</if>
+            <if test="updateBy != null  and updateBy != ''"> and UPDATE_BY = #{updateBy}</if>
+            <if test="updateTime != null "> and UPDATE_TIME = #{updateTime}</if>
+            <if test="remark != null  and remark != ''"> and REMARK = #{remark}</if>
+        </where>
+    </select>
+
+    <select id="selectDocTemplateByTmplId" parameterType="Long" resultMap="DocTemplateResult">
+        <include refid="selectDocTemplateVo"/>
+        where TMPL_ID = #{tmplId}
+    </select>
+
+    <insert id="insertDocTemplate" parameterType="DocTemplate" useGeneratedKeys="true" keyProperty="tmplId">
+        insert into doc_template
+        <trim prefix="(" suffix=")" suffixOverrides=",">
+            <if test="tmplType != null">TMPL_TYPE,</if>
+            <if test="tmplName != null">TMPL_NAME,</if>
+            <if test="fileId != null">FILE_ID,</if>
+            <if test="createBy != null">CREATE_BY,</if>
+            <if test="createTime != null">CREATE_TIME,</if>
+            <if test="updateBy != null">UPDATE_BY,</if>
+            <if test="updateTime != null">UPDATE_TIME,</if>
+            <if test="remark != null">REMARK,</if>
+        </trim>
+        <trim prefix="values (" suffix=")" suffixOverrides=",">
+            <if test="tmplType != null">#{tmplType},</if>
+            <if test="tmplName != null">#{tmplName},</if>
+            <if test="fileId != null">#{fileId},</if>
+            <if test="createBy != null">#{createBy},</if>
+            <if test="createTime != null">#{createTime},</if>
+            <if test="updateBy != null">#{updateBy},</if>
+            <if test="updateTime != null">#{updateTime},</if>
+            <if test="remark != null">#{remark},</if>
+        </trim>
+    </insert>
+
+    <update id="updateDocTemplate" parameterType="DocTemplate">
+        update doc_template
+        <trim prefix="SET" suffixOverrides=",">
+            <if test="tmplType != null">TMPL_TYPE = #{tmplType},</if>
+            <if test="tmplName != null">TMPL_NAME = #{tmplName},</if>
+            <if test="fileId != null">FILE_ID = #{fileId},</if>
+            <if test="createBy != null">CREATE_BY = #{createBy},</if>
+            <if test="createTime != null">CREATE_TIME = #{createTime},</if>
+            <if test="updateBy != null">UPDATE_BY = #{updateBy},</if>
+            <if test="updateTime != null">UPDATE_TIME = #{updateTime},</if>
+            <if test="remark != null">REMARK = #{remark},</if>
+        </trim>
+        where TMPL_ID = #{tmplId}
+    </update>
+
+    <delete id="deleteDocTemplateByTmplId" parameterType="Long">
+        delete from doc_template where TMPL_ID = #{tmplId}
+    </delete>
+
+    <delete id="deleteDocTemplateByTmplIds" parameterType="String">
+        delete from doc_template where TMPL_ID in
+        <foreach item="tmplId" collection="array" open="(" separator="," close=")">
+            #{tmplId}
+        </foreach>
+    </delete>
+</mapper>