package com.yuanchu.mom.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.dto.FePowerStableDto;
import com.yuanchu.mom.dto.FePowerStableExportDto;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.FeMeasuredQuantityMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.FeMeasuredQuantity;
import com.yuanchu.mom.pojo.FePowerStable;
import com.yuanchu.mom.mapper.FePowerStableMapper;
import com.yuanchu.mom.service.FePowerStableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.utils.HackLoopTableRenderPolicy;
import com.yuanchu.mom.utils.DateImageUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
/**
*
* 设施和环境条件-设施和环境条件要求-电源稳定性 服务实现类
*
*
* @author
* @since 2024-11-07 04:16:52
*/
@Service
public class FePowerStableServiceImpl extends ServiceImpl implements FePowerStableService {
@Resource
private FeMeasuredQuantityMapper feMeasuredQuantityMapper;
@Resource
private UserMapper userMapper;
@Value("${file.path}")
private String imgUrl;
@Override
public IPage getLaboratoryFacilityPowerStablePage(Page page) {
return baseMapper.getLaboratoryFacilityPowerStablePage(page);
}
@Override
public Map getCalibrationDate(Integer deviceId) {
return baseMapper.getCalibrationDate(deviceId);
}
/**
* 导出电源稳定性
* @param powerStableId
* @param response
*/
@Override
public void exportFePowerStable(Integer powerStableId, HttpServletResponse response) {
FePowerStableExportDto powerStable = baseMapper.selectPowerStable(powerStableId);
// 检测人
String testerUrl = null;
if (powerStable.getTesterId() != null) {
testerUrl = userMapper.selectById(powerStable.getTesterId()).getSignatureUrl();
if (StringUtils.isBlank(testerUrl)) {
throw new ErrorException("找不到检测人的签名");
}
}
// 核查人
String checkerUrl = null;
if (powerStable.getCheckerId() != null) {
checkerUrl = userMapper.selectById(powerStable.getCheckerId()).getSignatureUrl();
if (StringUtils.isBlank(checkerUrl)) {
throw new ErrorException("找不到核查人的签名");
}
}
// 查询详情
List feMeasuredQuantities = feMeasuredQuantityMapper.selectList(Wrappers.lambdaQuery()
.eq(FeMeasuredQuantity::getPowerStableId, powerStableId));
// 获取路径
InputStream inputStream = this.getClass().getResourceAsStream("/static/power-stable.docx");
Configure configure = Configure.builder()
.bind("measuredQuantityList", new HackLoopTableRenderPolicy())
.build();
String finalTesterUrl = testerUrl;
String finalCheckerUrl = checkerUrl;
XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
new HashMap() {{
put("stable", powerStable);
put("measuredQuantityList", feMeasuredQuantities);
put("testerUrl", StringUtils.isNotBlank(finalTesterUrl) ? Pictures.ofLocal(imgUrl + "/" + finalTesterUrl).create() : null);
put("checkerUrl", StringUtils.isNotBlank(finalCheckerUrl) ? Pictures.ofLocal(imgUrl + "/" + finalCheckerUrl).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("导出失败");
}
}
}