package com.yuanchu.mom.service.impl;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.config.ConfigureBuilder;
import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.*;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.common.PrintChina;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.ProcessSampleMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.*;
import com.yuanchu.mom.mapper.ProcessTotalSampleMapper;
import com.yuanchu.mom.service.ProcessTotalSampleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.utils.QueryWrappers;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* 样品接收总表 服务实现类
*
*
* @author
* @since 2024-12-12 05:02:58
*/
@Service
public class ProcessTotalSampleServiceImpl extends ServiceImpl implements ProcessTotalSampleService {
@Value("${wordUrl}")
private String wordUrl;
@Value("${file.path}")
private String imgUrl;
@Resource
GetLook getLook;
@Resource
private UserMapper userMapper;
@Resource
private ProcessTotalSampleMapper processTotalSampleMapper;
@Resource
private ProcessSampleMapper processSampleMapper;
@Override
public Map pageProcessTotalSample(Page page, ProcessTotalSample processTotalSample) {
Map map = new HashMap<>();
map.put("head", PrintChina.printChina(ProcessTotalSample.class));
map.put("body", processTotalSampleMapper.pageProcessTotalSample(page, QueryWrappers.queryWrappers(processTotalSample)));
return map;
}
@Override
public int submitProcessTotalSample(Integer id) {
Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
User user = userMapper.selectById(userId);
if (ObjectUtils.isEmpty(user.getSignatureUrl())) throw new ErrorException("未找到填表人的电子签名,请上传自己的电子签名!");
ProcessTotalSample processTotalSample = processTotalSampleMapper.selectById(id);
processTotalSample.setSubmitUser(userId);
processTotalSample.setSubmitState("已提交");
processTotalSample.setSubmitUrl(user.getSignatureUrl());
//生成样品处理申请表并将填表人的电子签名印上
processTotalSample.setUrl(processTotalSample(id,user.getSignatureUrl()));
return processTotalSampleMapper.updateById(processTotalSample);
}
@Override
public int checkProcessTotalSample(Integer id, String state) {
Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
User user = userMapper.selectById(userId);
if (ObjectUtils.isEmpty(user.getSignatureUrl())) throw new ErrorException("未找到审核人的电子签名,请上传自己的电子签名!");
ProcessTotalSample processTotalSample = processTotalSampleMapper.selectById(id);
processTotalSample.setExamineUser(userId);
processTotalSample.setExamineState(state);
processTotalSample.setExamineUrl(user.getSignatureUrl());
if (state.equals("不通过")) {
processTotalSample.setSubmitState("待提交");
}
//将审核人的电子签名印上
wordInsertUrl(new HashMap() {{
put("examineUrl", new FilePictureRenderData(100,50,imgUrl + "/" + user.getSignatureUrl()));
}}, wordUrl+"/"+processTotalSample.getUrl());
return processTotalSampleMapper.updateById(processTotalSample);
}
@Override
public int ratifyProcessTotalSample(Integer id, String state) {
Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
User user = userMapper.selectById(userId);
if (ObjectUtils.isEmpty(user.getSignatureUrl())) throw new ErrorException("未找到批准人的电子签名,请上传自己的电子签名!");
ProcessTotalSample processTotalSample = processTotalSampleMapper.selectById(id);
processTotalSample.setRatifyUser(userId);
processTotalSample.setRatifyState(state);
processTotalSample.setRatifyUrl(user.getSignatureUrl());
if (state.equals("不通过")) {
processTotalSample.setSubmitState("待提交");
}
//将批准人的签名印上
wordInsertUrl(new HashMap() {{
put("ratifyUrl", new FilePictureRenderData(100,50,imgUrl + "/" + user.getSignatureUrl()));
}}, wordUrl+"/"+processTotalSample.getUrl());
return processTotalSampleMapper.updateById(processTotalSample);
}
//生成样品处理表word
private String processTotalSample(Integer id,String signatureUrl){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
List processSamples = processSampleMapper.selectList(Wrappers.lambdaQuery().eq(ProcessSample::getTotalSampleId,id));
String url;
try {
InputStream inputStream = this.getClass().getResourceAsStream("/static/sample-receive.docx");
File file = File.createTempFile("temp", ".tmp");
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(inputStream, outputStream);
url = file.getAbsolutePath();
} catch (FileNotFoundException e) {
throw new ErrorException("找不到模板文件");
} catch (IOException e) {
throw new RuntimeException(e);
}
ConfigureBuilder builder = Configure.builder();
builder.useSpringEL(true);
List