package com.yuanchu.mom.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Pictures;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.ManageControlPlanList;
import com.yuanchu.mom.mapper.ManageControlPlanListMapper;
import com.yuanchu.mom.service.ManageControlPlanListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.utils.HackLoopTableRenderPolicy;
import com.yuanchu.mom.utils.DateImageUtil;
import com.yuanchu.mom.vo.ManageControlPlanListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
/**
*
* 重大风险因素分析及控制计划清单 服务实现类
*
*
* @author
* @since 2024-11-15 02:58:30
*/
@Service
public class ManageControlPlanListServiceImpl extends ServiceImpl implements ManageControlPlanListService {
@Autowired
private UserMapper userMapper;
@Value("${file.path}")
private String imgUrl;
@Override
public IPage getPageList(Page page) {
return baseMapper.getPageList(page, false);
}
@Override
public void exportPersonTraining(HttpServletResponse response) {
// 查询详情
IPage detailedDtos = baseMapper.getPageList(new Page(-1, -1), true);
if (detailedDtos.getRecords().isEmpty()) {
throw new ErrorException("审核通过的数据为空!请审核通过后在导出");
}
ManageControlPlanListVo manageRiskAssessmentResultsVo = detailedDtos.getRecords().get(0);
//获取编制人的签名地址
String writeUrl = userMapper.selectById(manageRiskAssessmentResultsVo.getEditor()).getSignatureUrl();
if (ObjectUtils.isEmpty(writeUrl) || writeUrl.equals("")) {
throw new ErrorException("找不到检验人的签名");
}
//获取复核人的签名地址
String examineUrl = null;
if (manageRiskAssessmentResultsVo.getApproval() != null) {
examineUrl = userMapper.selectById(manageRiskAssessmentResultsVo.getApproval()).getSignatureUrl();
if (StringUtils.isBlank(examineUrl)) {
throw new ErrorException("找不到复核人的签名");
}
}
//获取批准人的签名地址
String ratifyUrl = null;
if (manageRiskAssessmentResultsVo.getApproval() != null) {
ratifyUrl = userMapper.selectById(manageRiskAssessmentResultsVo.getApproval()).getSignatureUrl();
if (StringUtils.isBlank(ratifyUrl)) {
throw new ErrorException("找不到复核人的签名");
}
}
int index = 1;
for (ManageControlPlanListVo detailedDto : detailedDtos.getRecords()) {
detailedDto.setIndex(index);
index++;
}
// 获取路径
InputStream inputStream = this.getClass().getResourceAsStream("/static/analysis-risk-factors.docx");
String finalExamineUrl = examineUrl;
String finalRatifyUrl = ratifyUrl;
Configure configure = Configure.builder()
.bind("trainingDetailedList", new HackLoopTableRenderPolicy())
.build();
XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
new HashMap() {{
put("trainingDetailedList", detailedDtos.getRecords());
put("writeUrl", StringUtils.isNotBlank(writeUrl) ? Pictures.ofLocal(imgUrl + "/" + writeUrl).create() : null);
put("examineUrl", StringUtils.isNotBlank(finalExamineUrl) ? Pictures.ofLocal(imgUrl + "/" + finalExamineUrl).create() : null);
put("ratifyUrl", StringUtils.isNotBlank(finalRatifyUrl) ? Pictures.ofLocal(imgUrl + "/" + finalRatifyUrl).create() : null);
put("writeDateUrl", manageRiskAssessmentResultsVo.getEditorDate() != null ?
Pictures.ofStream(DateImageUtil.createDateImage(manageRiskAssessmentResultsVo.getEditorDate())).create() : null);
put("examineDateUrl", manageRiskAssessmentResultsVo.getApproveDate() != null ?
Pictures.ofStream(DateImageUtil.createDateImage(manageRiskAssessmentResultsVo.getApproveDate())).create() : null);
put("ratifyDateUrl", manageRiskAssessmentResultsVo.getApproveDate() != null ?
Pictures.ofStream(DateImageUtil.createDateImage(manageRiskAssessmentResultsVo.getApproveDate())).create() : null);
}});
try {
response.setContentType("application/msword");
String fileName = URLEncoder.encode(
"危险因素辨识与风险评价结果一览", "UTF-8");
response.setHeader("Content-disposition",
"attachment;filename=" + fileName + ".docx");
OutputStream os = response.getOutputStream();
template.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("导出失败");
}
}
}