whyclxw
2024-07-23 a2cb0eed5a0bb7cf9d5eb8afe74f4ba6836205b4
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whyc.dto.MailDTO;
import com.whyc.dto.Response;
import com.whyc.mapper.DefectiveProductsMapper;
import com.whyc.mapper.DocUserMapper;
import com.whyc.pojo.DefectiveProducts;
import com.whyc.pojo.DocUser;
import com.whyc.pojo.ProductHistory;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
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.util.Date;
import java.util.LinkedList;
import java.util.List;
 
@Service
public class DefectiveProductsService {
    @Autowired(required = false)
    private DefectiveProductsMapper mapper;
 
    @Autowired
    private MailService mailService;
    @Resource
    private DocUserMapper userMapper;
 
    @Autowired
    private DocLogService logService;
 
    @Autowired
    private DefectiveProductsHistoryService defectiveHisService;
 
 
    //录入不良品信息
    public Response addDefective(DefectiveProducts defective, List<MultipartFile> multipartFileList) throws IOException {
        Date date = new Date();
        //压缩包的路径及格式为: doc_file/defective/username_time.zip
        String username = ActionUtil.getUser().getName();
        long time = date.getTime();
        //String feedbackZipPath = "defective" + File.separator + username + "_" + time+".zip";
        String rootFile = CommonUtil.getRootFile();
        if(multipartFileList!=null && multipartFileList.size()!=0){
            String defectiveDirSuffix = "defective" + File.separator + username + File.separator + time + File.separator;
            String feedbackDir = rootFile + defectiveDirSuffix;
            File fileDir = new File(feedbackDir);
            if (!fileDir.exists()) {
                fileDir.mkdirs();
            }
            for (int i = 0; i < multipartFileList.size(); i++) {
                MultipartFile multipartFile = multipartFileList.get(i);
                //存储文件
                String originalFilename = multipartFile.getOriginalFilename();
                String fileName = originalFilename.substring(0, originalFilename.lastIndexOf("."));
                String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
                String feedbackPath = feedbackDir + fileName + suffix;
                File file = new File(feedbackPath);
 
                multipartFile.transferTo(file);
            }
            //ZipUtils.toZip(fileList,new FileOutputStream(new File(rootFile+feedbackZipPath)));
            defective.setFileUrl("doc_file" + File.separator + defectiveDirSuffix);
        }
        //保存
        defective.setStartTime(date);
        mapper.insert(defective);
        //发送邮件
        String receiverIds = defective.getReceiverIds();
        String[] receiverIdsSplit = receiverIds.split(",");
        List<String> receiverMailList = new LinkedList<>();
 
        Integer senderId = defective.getSenderId();
        String senderName = null;
 
        List<DocUser> docUsers = userMapper.selectList(null);
        for (DocUser docUser:docUsers){
            if(docUser.getId().intValue() == senderId){
                senderName = docUser.getName();
                break;
            }
        }
 
        for (String receiverId:receiverIdsSplit){
            for (DocUser docUser:docUsers){
                if(docUser.getId().intValue() == Integer.parseInt(receiverId)){
                    String mail = docUser.getMail();
                    if(mail!=null && !mail.isEmpty()) {
                        receiverMailList.add(mail);
                    }
                    break;
                }
            }
        }
 
        String subject = "【不良品记录】-"+senderName;
        String content = defective.getContent();
 
        MailDTO mailDTO = new MailDTO();
        mailDTO.setTitle(subject);
        mailDTO.setContent(content);
        mailDTO.setMailList(receiverMailList);
        mailService.sendMail(mailDTO);
 
        //记录处理过程
        defectiveHisService.addHis(defective);
       return new Response().set(1,true,"不良品操作完成");
    }
}