Commit d2c98ae6 authored by 周昊's avatar 周昊

1、开发pc端导出固有风险告知卡word文件接口

parent 057fb6b3
package com.censoft.censoftrongtong.controller;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.convert.Convert;
import com.censoft.censoftrongtong.domain.RiskPlan;
import com.censoft.censoftrongtong.domain.dto.RiskNotificationExportWordDto;
import com.censoft.censoftrongtong.service.IRiskPlanInherentListService;
import com.censoft.censoftrongtong.service.IRiskPlanService;
import com.ruoyi.common.utils.WordUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -35,6 +43,9 @@ public class RiskPlanController extends BaseController
@Autowired
private IRiskPlanService riskPlanService;
@Resource
private IRiskPlanInherentListService riskPlanInherentListService;
/**
* 查询风险计划列表
*/
......@@ -102,4 +113,19 @@ public class RiskPlanController extends BaseController
{
return toAjax(riskPlanService.deleteRiskPlanByIds(ids));
}
@GetMapping("/exportWord/riskNotification/{inherentId}")
public void exportWord(@PathVariable Long inherentId,
HttpServletRequest request, HttpServletResponse response) throws IOException {
RiskNotificationExportWordDto exportWordDto = riskPlanInherentListService.getRiskNotificationExportWordDto(inherentId);
Map<String, Object> dataMap = Convert.convert(Map.class, exportWordDto);
WordUtil word = new WordUtil();
word.exportWord(request, response,dataMap,"风险告知卡");
}
}
package com.censoft.censoftrongtong.domain.dto;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
/**
* @author 周昊
* @desc ...
* @date 2023-07-21 11:33:52
*/
@Data
public class RiskNotificationExportWordDto {
/**
* 风险源名称
*/
private String name;
/**
* 风险等级
*/
private String level;
/**
* 危险源名称
*/
private String dangerName;
/**
* 风险点类型
*/
private String pointType;
/**
* 准事故类型
*/
private String type;
/**
* 安全警示标志
*/
private String safetyWarningSigns;
/**
* 应采取的管理措施
*/
private String measuresAdministration;
/**
* 管控责任单位
*/
private String deptName;
/**
* 管控责任人
*/
private String userName;
/**
* 应急措施
*/
@TableField("measures_emergency")
private String measuresEmergency;
}
......@@ -2,6 +2,7 @@ package com.censoft.censoftrongtong.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.censoft.censoftrongtong.domain.RiskPlanInherentList;
import com.censoft.censoftrongtong.domain.dto.RiskNotificationExportWordDto;
import com.censoft.censoftrongtong.domain.dto.RiskPlanAppExistingListDto;
import com.censoft.censoftrongtong.domain.dto.RiskPlanAppInherentListDetailsDto;
import com.censoft.censoftrongtong.domain.dto.RiskPlanAppInherentListDto;
......@@ -22,4 +23,6 @@ public interface RiskPlanInherentListMapper extends BaseMapper<RiskPlanInherentL
RiskPlanAppInherentListDetailsDto getRiskPlanAppInherentListDetailsDtoByInherentId(@Param("inherentId") Long inherentId);
List<RiskPlanAppExistingListDto> getExistingListByInherentId(@Param("inherentId") Long inherentId);
RiskNotificationExportWordDto getRiskNotificationExportWordDto(@Param("inherentId") Long inherentId);
}
......@@ -2,10 +2,7 @@ package com.censoft.censoftrongtong.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.censoft.censoftrongtong.domain.RiskPlanInherentList;
import com.censoft.censoftrongtong.domain.dto.RiskInherentListSaveDto;
import com.censoft.censoftrongtong.domain.dto.RiskPlanAppExistingListDto;
import com.censoft.censoftrongtong.domain.dto.RiskPlanAppInherentListDetailsDto;
import com.censoft.censoftrongtong.domain.dto.RiskPlanAppInherentListDto;
import com.censoft.censoftrongtong.domain.dto.*;
import java.util.List;
......@@ -25,4 +22,6 @@ public interface IRiskPlanInherentListService extends IService<RiskPlanInherentL
RiskPlanAppInherentListDetailsDto getRiskPlanAppInherentListDetailsDtoByInherentId(Long inherentId);
List<RiskPlanAppExistingListDto> getExistingListByInherentId(Long inherentId);
RiskNotificationExportWordDto getRiskNotificationExportWordDto(Long inherentId);
}
......@@ -5,10 +5,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.censoft.censoftrongtong.domain.RiskPlan;
import com.censoft.censoftrongtong.domain.RiskPlanInherentList;
import com.censoft.censoftrongtong.domain.RiskPlanTask;
import com.censoft.censoftrongtong.domain.dto.RiskInherentListSaveDto;
import com.censoft.censoftrongtong.domain.dto.RiskPlanAppExistingListDto;
import com.censoft.censoftrongtong.domain.dto.RiskPlanAppInherentListDetailsDto;
import com.censoft.censoftrongtong.domain.dto.RiskPlanAppInherentListDto;
import com.censoft.censoftrongtong.domain.dto.*;
import com.censoft.censoftrongtong.enums.RiskPlanStatusType;
import com.censoft.censoftrongtong.mapper.RiskPlanInherentListMapper;
import com.censoft.censoftrongtong.mapper.RiskPlanMapper;
......@@ -225,4 +222,9 @@ public class RiskPlanInherentListServiceImpl
public List<RiskPlanAppExistingListDto> getExistingListByInherentId(Long inherentId) {
return riskPlanInherentListMapper.getExistingListByInherentId(inherentId);
}
@Override
public RiskNotificationExportWordDto getRiskNotificationExportWordDto(Long inherentId) {
return riskPlanInherentListMapper.getRiskNotificationExportWordDto(inherentId);
}
}
......@@ -113,4 +113,22 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
WHERE
reil.inherent_id = #{inherentId}
</select>
<select id="getRiskNotificationExportWordDto"
resultType="com.censoft.censoftrongtong.domain.dto.RiskNotificationExportWordDto">
SELECT
IFNULL( rpil.`name`, ' ' ) AS `name`,
rpil.`level`,
rpil.hazard_source_name AS dangerName,
rpil.point_type AS pointType,
rpil.type,
rpil.safety_warning_signs AS safetyWarningSigns,
rpil.measures_administration AS measuresAdministration,
rpil.measures_dept_name AS deptName,
rpil.measures_user_name AS userName,
rpil.measures_emergency AS measuresEmergency
FROM
risk_plan_inherent_list rpil
WHERE
rpil.id = #{inherentId}
</select>
</mapper>
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -139,6 +139,13 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.ruoyi.common.utils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import static freemarker.template.Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS;
/**
* @author 笑小枫
* @date 2022/7/27
* @see <a href="https://www.xiaoxiaofeng.com">https://www.xiaoxiaofeng.com</a>
*/
public class WordUtil {
private final Configuration configuration;
public WordUtil() {
configuration = new Configuration(DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
configuration.setDefaultEncoding("UTF-8");
}
public void createWord(Map<String, Object> dataMap, String templateName, String fileName) {
// 模板文件所在路径
configuration.setClassForTemplateLoading(this.getClass(), "/templates");
Template t = null;
try {
// 获取模板文件
t = configuration.getTemplate(templateName, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
// 导出文件
File outFile = new File(fileName);
try (Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), StandardCharsets.UTF_8))) {
if (t != null) {
// 将填充数据填入模板文件并输出到目标文件
t.process(dataMap, out);
}
} catch (IOException | TemplateException e1) {
e1.printStackTrace();
}
}
public void exportWord(HttpServletRequest request, HttpServletResponse response,Map<String, Object> dataMap,String exportName) throws IOException {
// 1.创建临时文件夹
String rootPath = request.getSession().getServletContext().getRealPath("/");
String fileName = UUID.randomUUID().toString().replace("-", "");
// 加载word中的数据信息
WordUtil word = new WordUtil();
word.createWord(dataMap, "word.xml", rootPath + "/" + fileName + ".doc");
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
// 设置浏览器以下载的方式处理该文件名
response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(exportName + ".doc", "UTF-8"))));
File file = new File(rootPath + "/" + fileName + ".doc");
try (InputStream fin = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream()) {
// 缓冲区
byte[] buffer = new byte[512];
int bytesToRead;
// 通过循环将读入的Word文件的内容输出到浏览器中
while ((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 删除临时文件
file.delete();
}
}
}
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