package com.yuanchu.mom.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.UUID;
import com.alibaba.excel.EasyExcel;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.common.PrintChina;
import com.yuanchu.mom.excel.ManageDocumentListListener;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.pojo.Company;
import com.yuanchu.mom.pojo.InsOrderFile;
import com.yuanchu.mom.pojo.ManageDocumentList;
import com.yuanchu.mom.mapper.ManageDocumentListMapper;
import com.yuanchu.mom.service.ManageDocumentListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.utils.QueryWrappers;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
* 文件清单
* 服务实现类
*
*
* @author
* @since 2024-11-08 11:08:11
*/
@Service
public class ManageDocumentListServiceImpl extends ServiceImpl implements ManageDocumentListService {
@Resource
private ManageDocumentListMapper manageDocumentListMapper;
@Resource
GetLook getLook;
@Value("${wordUrl}")
private String wordUrl;
@Override
public Map pageManageDocumentList(Page page, ManageDocumentList manageDocumentList) {
Map map = new HashMap<>();
map.put("head", PrintChina.printChina(ManageDocumentList.class));
Map map1 = getLook.selectPowerByMethodAndUserId("pageManageDocumentList");
if (map1.get("look") == 1) manageDocumentList.setCreateUser(map1.get("userId"));
map.put("body", manageDocumentListMapper.pageManageDocumentList(page, QueryWrappers.queryWrappers(manageDocumentList)));
return map;
}
@Override
public int uploadFile(Integer id, MultipartFile file) {
String urlString;
String pathName;
String path;
ManageDocumentList manageDocumentList = manageDocumentListMapper.selectById(id);
if (ObjectUtils.isNotEmpty(manageDocumentList.getUrl())){
// 删除旧文件
File oldFile = new File(wordUrl + "/" + manageDocumentList.getUrl());
oldFile.delete();
}
//上传新文件
path = wordUrl;
try {
File realpath = new File(path);
if (!realpath.exists()) {
realpath.mkdirs();
}
pathName = UUID.randomUUID() + "_" + file.getOriginalFilename();
urlString = realpath + "/" + pathName;
file.transferTo(new File(urlString));
} catch (Exception e) {
e.printStackTrace();
System.err.println("附件上传错误");
return 0;
}
manageDocumentList.setUrl(pathName);
return manageDocumentListMapper.updateById(manageDocumentList);
}
@Override
public void importExcel(List list) {
if (CollectionUtil.isEmpty(list)) {
return;
}
list = list.stream().filter(manageDocumentList -> ObjectUtils.isNotEmpty(manageDocumentList.getName())).collect(Collectors.toList());
saveBatch(list);
}
public String wordToPdf(String wordPath, String pdfPath) {
FileOutputStream os = null;
try {
InputStream is = new ClassPathResource("/lib/license.xml").getInputStream();
License license = new License();
license.setLicense(is);
if (!license.getIsLicensed()) {
System.out.println("License验证不通过...");
return null;
}
//生成一个空的PDF文件
File file = new File(pdfPath.replace(".pdf", ".pdf"));
os = new FileOutputStream(file);
//要转换的word文件
com.aspose.words.Document doc = new com.aspose.words.Document(wordPath);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}