whyclxw
1 天以前 e81a9becf6e180df535ee430a958960f1546dda5
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.dto.Response;
import com.whyc.mapper.MaterialApprovingMapper;
import com.whyc.pojo.DocUser;
import com.whyc.pojo.MaterialApproving;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
import com.whyc.util.FileUtil;
import com.whyc.util.Zip4jUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
 
@Service
public class MaterialApprovingService {
 
    @Resource
    private MaterialApprovingMapper mapper;
 
    @Autowired
    private MailService mailService;
 
    @Autowired
    private MailUserService mailUserService;
 
    /**
     *
     * @param file
     * @return
     * @throws IOException
     * @throws InvalidFormatException
     */
    public Response zipParse(MultipartFile file) throws IOException, InvalidFormatException {
        List<MaterialApproving> list = new LinkedList<>();
        Response response = new Response();
        //检查是否为zip
        boolean isZip = Zip4jUtil.checkZipFileParam(file);
        if(!isZip){
            return response.set(1,false,"上传的文件格式不是zip");
        }
        //zip存储路径:doc_file/product_approving/${username}/{dateFormat}/${timeStamp}
        String rootFile = CommonUtil.getRootFile();
        DocUser user = ActionUtil.getUser();
        String dateFormat = new SimpleDateFormat("YYYY-MM").format(new Date());
        long timeStamp = System.currentTimeMillis();
        String filePath = rootFile + File.separator + "material_approving" + File.separator + user.getName() + File.separator + dateFormat+ File.separator + timeStamp;
        File parentFile = new File(filePath);
        String originalFilename = file.getOriginalFilename();
        File zipFile = new File(filePath+File.separator+ originalFilename);
        /*if(!zipFile.exists()){
            zipFile.mkdirs();
        }*/
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        file.transferTo(zipFile);
        //解压文件夹
        Zip4jUtil.unPackZip(zipFile,null,filePath);
        //遍历解压后的文件夹路径,解析excel
        System.out.println(filePath);
        List<String> fileList = new ArrayList<>();
        fileList = FileUtil.getStaticFilePath(parentFile,fileList);
        for (int i = 0; i < fileList.size(); i++) {
            String fileTempUrl = fileList.get(i);
            //查询到xls数据
            if(fileTempUrl.contains(".xls")){
                File fileTemp = new File(fileTempUrl);
                Workbook workbook = null;
                workbook = WorkbookFactory.create(fileTemp);
                List<? extends PictureData> allPictures = workbook.getAllPictures();
                //取第一个sheet表
                Sheet sheet = workbook.getSheetAt(0);
                int lastRowNum = sheet.getLastRowNum();
                //取第三行,并以第三行开始
                Row rowTemp = sheet.getRow(1);
                short lastCellNum = rowTemp.getLastCellNum();
                //先单独获取产品型号和版本号/分类
                Row row = sheet.getRow(1);
                String type = row.getCell(1).getStringCellValue();
 
                Row row2 = sheet.getRow(2);
                String productModel = row2.getCell(5).getStringCellValue();
                Row row3 = sheet.getRow(3);
                String productVersion = row3.getCell(5).getStringCellValue();
                for (int l = 8; l < lastRowNum-1; l++) {
                    Cell cellTemp = sheet.getRow(l).getCell(1);
                    cellTemp.setCellType(Cell.CELL_TYPE_STRING);
                    if(cellTemp.getStringCellValue().equals("")){
                        break;
                    }
                    MaterialApproving approving = new MaterialApproving();
                    approving.setCreateDate(new Date());
 
                    for (int m = 2; m < lastCellNum; m++) {
                        row = sheet.getRow(l);
                        Cell cell = row.getCell(m);
                        String cellValue = null;
                        int cellValueInt = 0;
                        Double cellValueDouble = null;
                        if(m == 3){
                            //cellValueDouble = cell.getNumericCellValue();
                            //DecimalFormat decimalFormat = new DecimalFormat("0");
                            //cellValue = decimalFormat.format(cellValueDouble);
                            cell.setCellType(Cell.CELL_TYPE_STRING);
                            cellValue = cell.getStringCellValue();
                        }
                        else if(m ==7){
                            cellValueDouble = cell.getNumericCellValue();
                        }
                        else {
                            cellValue = cell.getStringCellValue();
                        }
 
                        switch (m){
                            case 2:{approving.setCategory(cellValue);}break;
                            case 3:{approving.setSubCode(cellValue);}break;
                            case 4:{approving.setSubName(cellValue);}break;
                            case 5:{approving.setSubModel(cellValue);}break;
                            case 6:{approving.setUnit(cellValue);}break;
                            case 7:{approving.setQuantity(cellValueDouble.intValue());}break;
                            case 8:{approving.setProducer(cellValue);}break;
                            case 9:{approving.setNotes(cellValue);}break;
                        }
                    }
                    list.add(approving);
                }
            }
        }
        for (MaterialApproving MaterialApproving : list) {
            if(MaterialApproving.getSubName()==null ||MaterialApproving.getSubName().equals("")){
                return response.set(1,false,"拒绝解析,excel文件中记录包含名称为空的记录");
            }
        }
        //追加物料规范校验 暂时去除校验
        /*List<MaterialCheckDTO> checkList = list.stream().map(bom->{
            MaterialCheckDTO dto = new MaterialCheckDTO();
            dto.setNum(bom.getId());
            dto.setSubCode(bom.getSubCode());
            dto.setSubName(bom.getSubName());
            dto.setSubModel(bom.getSubModel());
            return dto;
        }).collect(Collectors.toList());
        List<MaterialCheckDTO> irregularList = CommonUtil.checkFormat(checkList);
        if(irregularList.size()>0){
            return response.setII(1,false,irregularList,"名称或型号命名不规范");
        }*/
        String nextTitle = originalFilename.substring(0,originalFilename.lastIndexOf("."));
        return response.setIII(1,true,list,nextTitle,filePath);
    }
 
    public void insert(List<MaterialApproving> cApprovingList) {
        mapper.insertBatchSomeColumn(cApprovingList);
    }
 
    public List<MaterialApproving> getListByStatus(int status) {
        QueryWrapper<MaterialApproving> query = Wrappers.query();
        query.eq("status",status);
        return mapper.selectList(query);
    }
 
    public void endStatus(Integer mainId) {
        UpdateWrapper<MaterialApproving> update = Wrappers.update();
        update.set("status",2).eq("main_id",mainId);
        mapper.update(null,update);
    }
 
    public List<MaterialApproving> getListByMainId(Integer mainId) {
        QueryWrapper<MaterialApproving> query = Wrappers.query();
        query.eq("main_id",mainId);
        return mapper.selectList(query);
    }
 
    public MaterialApproving getListByMaterialId(Integer materialId) {
        QueryWrapper<MaterialApproving> query = Wrappers.query();
        query.eq("material_id",materialId).last(" limit 1");
        return mapper.selectOne(query);
    }
}