lxw
2022-09-08 658137c0bc058f5e7b068a02d6a5aae8a3eaa8b6
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.constant.UserOperation;
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Response;
import com.whyc.mapper.SoftwareMapper;
import com.whyc.pojo.DocUser;
import com.whyc.pojo.Software;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
import com.whyc.util.DateUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class SoftwareService {
    @Autowired(required = false)
    private SoftwareMapper mapper;
 
    @Autowired
    private DocLogService logService;
 
    public List<Software> excelParse(InputStream inputStream) throws IOException, InvalidFormatException, ParseException {
        List<Software> softwareList = new LinkedList<>();
 
        Workbook workbook = null;
        workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
        //取第一个sheet表
        Sheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        //固定5列
        short cellNum = 5;
        //取固定部分值
        Software common = new Software();
        common.setFileName(sheet.getRow(2).getCell(2).getStringCellValue());
        String typeStr = sheet.getRow(3).getCell(2).getStringCellValue();
        String[] typeArr = typeStr.split(" ");
        for (int i = 0; i < typeArr.length; i++) {
            //excel单元格中的✔对应字符
            if(typeArr[i].contains("þ")){
                common.setType(typeArr[i].replace("þ","").trim());
                break;
            }
        }
        common.setVersion(sheet.getRow(4).getCell(2).getStringCellValue());
        common.setBasedVersion(sheet.getRow(4).getCell(4).getStringCellValue());
 
        common.setOwner(sheet.getRow(5).getCell(2).getStringCellValue());
        common.setFilingDate(sheet.getRow(5).getCell(4).getStringCellValue());
        //最后一行,取发布说明
        common.setReleaseNotes(sheet.getRow(lastRowNum).getCell(2).getStringCellValue());
 
        //第8行开始,倒数第2行截止
        int applyModelNum = lastRowNum + 1 - 8;
        for (int i = 0; i < applyModelNum; i++) {
            Software software = new Software();
            BeanUtils.copyProperties(common,software);
            //取第3列,第5列
            Cell cell = sheet.getRow(7 + i).getCell(2);
            cell.setCellType(Cell.CELL_TYPE_STRING);
            software.setApplyMaterialCode(cell.getStringCellValue());
            software.setApplyModel(sheet.getRow(7+i).getCell(4).getStringCellValue());
 
            softwareList.add(software);
        }
 
        softwareList = softwareList.stream().filter(software -> !software.getApplyMaterialCode().equals("")).collect(Collectors.toList());
 
        return softwareList;
 
    }
    //查询软件列表的信息
    public Response getAllSoftware(String fileName,String applyMaterialCode,String applyModel,String owner, int pageCurr, int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
        List list=mapper.getFileUrl(fileName,applyMaterialCode,applyModel,owner);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list.size()>0,pageInfo,"软件信息返回");
    }
    //根据subcode查询软件列表
    public Response getSoftBySubCode(String subCode) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("apply_material_code",subCode);
        wrapper.orderByAsc("version");
        List list=mapper.selectList(wrapper);
        return new Response().setII(1,list.size()>0,list,"软件信息返回");
    }
 
    
    //根据软件名称实现软件下载
    public void downLoadSoftware(HttpServletRequest req, HttpServletResponse resp, int id) {
        String fileDirName = FileDirPath.getFileDirName();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);
        wrapper.last("limit 1");
        Software software=mapper.selectOne(wrapper);
        try {
            // 转码防止乱码
            //resp.addHeader("Content-Disposition", "attachment;filename=" + new String(softwareName.getBytes("UTF-8"), "ISO8859-1"));
            resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode ( software.getFileName()+".zip", "utf-8"));
            OutputStream out = resp.getOutputStream();
            FileInputStream in = new FileInputStream(fileDirName+File.separator+software.getFileUrl());
            int len=0;
            byte[] buffer =new byte[1024];
            //7. 将缓冲区中的数据输出
            while ((len=in.read(buffer))>0){
                out.write(buffer,0,len);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //记录日志
        DocUser docUser= ActionUtil.getUser();
        String operationDetail="具体软件名称为:"+software.getFileName();
        String opreationMsg="执行了软件下载操作";
        String terminalIp=req.getRemoteAddr();
        logService.recordOperationLog(docUser.getId(),docUser.getName(), UserOperation.TYPE_DOWNLOAD.getType(),new Date(),terminalIp,opreationMsg,operationDetail);
    }
 
    public Response upload(MultipartFile file1, MultipartFile file2, List<Software> softwareList) throws IOException {
        String file1Name = file1.getOriginalFilename();
        String file2Name = file2.getOriginalFilename();
        Software software = softwareList.get(0);
        Date date = new Date();
        String dateUnion = DateUtil.YYYY_MM_DD_HH_MM_SS_UNION.format(date);
        //文件重命名
        file1Name = file1Name.substring(0,file1Name.lastIndexOf(".")) + "_" + dateUnion +file1Name.substring(file1Name.lastIndexOf("."));
        file2Name = file2Name.substring(0,file2Name.lastIndexOf(".")) + "_" + dateUnion +file2Name.substring(file2Name.lastIndexOf("."));
        //存储路径
        String rootFile = CommonUtil.getRootFile();
        String softwareDir = rootFile + "software" + File.separator + software.getOwner();
        String softwareHttpUrl = softwareDir.substring(softwareDir.lastIndexOf("doc_file"+ File.separator + "software"));
        File softwareDirFile = new File(softwareDir);
        if(!softwareDirFile.exists()){
            softwareDirFile.mkdirs();
        }
        file1.transferTo(new File(softwareDir+File.separator+file1Name));
        file2.transferTo(new File(softwareDir+File.separator+file2Name));
        //设置路径
        for (Software software1:softwareList){
            software1.setFileUrl(softwareHttpUrl+File.separator+file1Name);
            software1.setExcelUrl(softwareHttpUrl+File.separator+file2Name);
            software1.setCreateTime(date);
        }
        //写入数据库
        insertBatch(softwareList);
        return new Response().setII(1,"上传完成");
    }
 
    private void insertBatch(List<Software> softwareList){
        mapper.insertBatchSomeColumn(softwareList);
    }
}