zss
2024-12-20 c9e36e23b3f95f6027d78483dfc23021d1ec6261
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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;
 
/**
 * <p>
 * 文件清单
 * 服务实现类
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2024-11-08 11:08:11
 */
@Service
public class ManageDocumentListServiceImpl extends ServiceImpl<ManageDocumentListMapper, ManageDocumentList> implements ManageDocumentListService {
 
    @Resource
    private ManageDocumentListMapper manageDocumentListMapper;
 
    @Resource
    GetLook getLook;
 
    @Value("${wordUrl}")
    private String wordUrl;
 
    @Override
    public Map<String, Object> pageManageDocumentList(Page page, ManageDocumentList manageDocumentList) {
        Map<String, Object> map = new HashMap<>();
        map.put("head", PrintChina.printChina(ManageDocumentList.class));
        Map<String, Integer> 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<ManageDocumentList> 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;
    }
}