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.ProcessDealMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.ProcessDeal;
import com.yuanchu.mom.pojo.ProcessTotaldeal;
import com.yuanchu.mom.mapper.ProcessTotaldealMapper;
import com.yuanchu.mom.pojo.User;
import com.yuanchu.mom.service.ProcessTotaldealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.utils.QueryWrappers;
import org.apache.commons.io.IOUtils;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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.*;
/**
*
* 检测或校准物品的处置总表(历史) 服务实现类
*
*
* @author
* @since 2024-11-02 03:59:09
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class ProcessTotaldealServiceImpl extends ServiceImpl implements ProcessTotaldealService {
@Resource
private ProcessTotaldealMapper processTotaldealMapper;
@Resource
private ProcessDealMapper processDealMapper;
@Value("${wordUrl}")
private String wordUrl;
@Value("${file.path}")
private String imgUrl;
@Resource
GetLook getLook;
@Resource
private UserMapper userMapper;
@Override
public Map pageProcessTotaldeal(Page page, ProcessTotaldeal processTotaldeal) {
Map map = new HashMap<>();
map.put("head", PrintChina.printChina(ProcessTotaldeal.class));
map.put("body", processTotaldealMapper.pageProcessTotaldeal(page, QueryWrappers.queryWrappers(processTotaldeal)));
return map;
}
@Override
public int submitProcessTotaldeal(Integer id) {
Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
User user = userMapper.selectById(userId);
if (ObjectUtils.isEmpty(user.getSignatureUrl())) throw new ErrorException("未找到填表人的电子签名,请上传自己的电子签名!");
ProcessTotaldeal processTotaldeal = processTotaldealMapper.selectById(id);
processTotaldeal.setSubmitUser(userId);
processTotaldeal.setSubmitState("已提交");
processTotaldeal.setSubmitUrl(user.getSignatureUrl());
//生成样品处理申请表并将填表人的电子签名印上
processTotaldeal.setUrl(processTotaldeal(id,user.getSignatureUrl()));
return processTotaldealMapper.updateById(processTotaldeal);
}
@Override
public int checkProcessTotaldeal(Integer id, String state) {
Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
User user = userMapper.selectById(userId);
if (ObjectUtils.isEmpty(user.getSignatureUrl())) throw new ErrorException("未找到审核人的电子签名,请上传自己的电子签名!");
ProcessTotaldeal processTotaldeal = processTotaldealMapper.selectById(id);
processTotaldeal.setExamineUser(userId);
processTotaldeal.setExamineState(state);
processTotaldeal.setExamineUrl(user.getSignatureUrl());
if (state.equals("不通过")) {
processTotaldeal.setSubmitState("待提交");
}
//将审核人的电子签名印上
wordInsertUrl(new HashMap() {{
put("examineUrl", new FilePictureRenderData(100,50,imgUrl + "/" + user.getSignatureUrl()));
}}, wordUrl+"/"+processTotaldeal.getUrl());
return processTotaldealMapper.updateById(processTotaldeal);
}
@Override
public int ratifyProcessTotaldeal(Integer id, String state) {
Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
User user = userMapper.selectById(userId);
if (ObjectUtils.isEmpty(user.getSignatureUrl())) throw new ErrorException("未找到批准人的电子签名,请上传自己的电子签名!");
ProcessTotaldeal processTotaldeal = processTotaldealMapper.selectById(id);
processTotaldeal.setRatifyUser(userId);
processTotaldeal.setRatifyState(state);
processTotaldeal.setRatifyUrl(user.getSignatureUrl());
if (state.equals("不通过")) {
processTotaldeal.setSubmitState("待提交");
}
//将批准人的签名印上
wordInsertUrl(new HashMap() {{
put("ratifyUrl", new FilePictureRenderData(100,50,imgUrl + "/" + user.getSignatureUrl()));
}}, wordUrl+"/"+processTotaldeal.getUrl());
return processTotaldealMapper.updateById(processTotaldeal);
}
//生成样品处理表word
private String processTotaldeal(Integer id,String signatureUrl){
List processDeals = processDealMapper.selectList(Wrappers.lambdaQuery().eq(ProcessDeal::getTotaldealId,id));
InputStream inputStream = this.getClass().getResourceAsStream("/static/sample-deal.docx");
ConfigureBuilder builder = Configure.builder();
builder.useSpringEL(true);
List