whyclxw
2024-07-25 84b1cd58ac84ff1312e39d86a294f6f10aacc771
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package com.whyc.service;
 
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.DefectiveDto;
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.DefectiveProductsHistory;
import com.whyc.pojo.DocUser;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
import com.whyc.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
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;
 
 
 
    //录入不良品信息
    @Transactional
    public Response addDefective(DefectiveProducts defective, List<MultipartFile> multipartFileList) throws IOException {
        //初次录入状态为0
        defective.setConfirmStatus(0);
        //压缩包的路径及格式为: doc_file/defective/username_time.zip
        String senderName = ActionUtil.getUser().getName();
        Integer senderId =ActionUtil.getUser().getId().intValue();
        defective.setSenderId(senderId);
        defective.setSenderName(senderName);
        Date date=new Date();
        defective.setRecordTime(date);
        defective.setRestProduct(defective.getBadProduct());
        if(defective.getStartTime()==null){
            //保存
            defective.setStartTime(date);
        }
        String recordtime = ActionUtil.sdfwithFTP.format(defective.getRecordTime());
        String rootFile = CommonUtil.getRootFile();
        if(multipartFileList!=null && multipartFileList.size()!=0){
            String defectiveDirSuffix = "defective" + File.separator + senderName + File.separator+ recordtime + 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(defectiveDirSuffix);
        }
 
 
        mapper.insert(defective);
        //发送邮件
        String receiverIds = defective.getReceiverIds();
        String[] receiverIdsSplit = receiverIds.split(",");
        List<String> receiverMailList = new LinkedList<>();
 
 
        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.getSenderName()+"上传了厂商:"+defective.getProvideName()+",产品:"+defective.getProductName()+",型号:"+defective.getType()+"的不良品记录;\n"+
                "不良品数量为:"+defective.getBadProduct()+",具体不良信息为:"+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,"不良品操作完成");
    }
 
    //不良品首页推送
    public Response getDefectiveLimit(DefectiveDto defectiveDto) {
        PageHelper.startPage(defectiveDto.getPageCurr(),defectiveDto.getPageSize());
        List<DefectiveProducts> list=mapper.getDefectiveLimit(defectiveDto);
        String rootFile = CommonUtil.getRootFile();//主路径
        for (DefectiveProducts defective:list) {
            String filePath=defective.getFileUrl();
            String startTimeEx=ActionUtil.sdfwithday.format(defective.getStartTime());
            defective.setStartTimeEx(startTimeEx);
            if(filePath!=null&&!filePath.isEmpty()){
                defective.setFileUrl("doc_file"+File.separator+filePath);
                defective.setNameList(FileUtil.getFileNameWithOutDirectory(rootFile+filePath));
 
            }
            List<DefectiveProductsHistory> hisList=defective.getHisList();
            if(hisList!=null&&hisList.size()>0){
                for (DefectiveProductsHistory his:defective.getHisList()) {
                    String hisDelPath=his.getDelUrl();
                    if(hisDelPath!=null&&!hisDelPath.isEmpty()){
                        his.setDelUrl("doc_file"+File.separator+hisDelPath);
                        his.setHisNameList(FileUtil.getFileNameWithOutDirectory(rootFile+hisDelPath));
                    }
                }
 
            }
        }
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list!=null,pageInfo,"不良品首页推送");
    }
 
    //处理不良品
    @Transactional
    public Response updateDefective(DefectiveProductsHistory defectiveHis, List<MultipartFile> multipartFileList) {
        //获取上一次记录信息
        QueryWrapper qwrapper=new QueryWrapper();
        qwrapper.eq("id",defectiveHis.getDeftId());
        qwrapper.last("limit 1");
        DefectiveProducts defective=mapper.selectOne(qwrapper);
        //获取
        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;
            }
        }
        defective.setSenderName(senderName);
        //修改当前表记录
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.set("receiver_ids",defectiveHis.getReceiverIds());
        wrapper.set("receiver_names",defectiveHis.getReceiverNames());
        int resProduct=defective.getRestProduct()-defectiveHis.getDelProduct();
        defectiveHis.setRestProduct(resProduct);
        wrapper.set("rest_product",resProduct);
        if(resProduct==0){
            defectiveHis.setConfirmStatus(2);//不良品数量为0结束
        }else {
            defectiveHis.setConfirmStatus(1);
        }
        wrapper.set("confirm_status",defectiveHis.getConfirmStatus());
        wrapper.set("note",defectiveHis.getNote());
        wrapper.eq("id",defectiveHis.getDeftId());
        mapper.update(null,wrapper);
        //记录处理记录
 
        defectiveHisService.updateDefective(defective,defectiveHis,multipartFileList);
 
        //发送邮件
        String receiverIds = defectiveHis.getReceiverIds();
        String[] receiverIdsSplit = receiverIds.split(",");
        List<String> receiverMailList = new LinkedList<>();
 
        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 = defectiveHis.getDelName()+"修复不良品个数:"+defectiveHis.getDelProduct()+",剩余不良品个数:"+defectiveHis.getRestProduct()+"\n"
                +"修复方式:"+defective.getNote();
 
        MailDTO mailDTO = new MailDTO();
        mailDTO.setTitle(subject);
        mailDTO.setContent(content);
        mailDTO.setMailList(receiverMailList);
        mailService.sendMail(mailDTO);
 
        return new Response().set(1,true);
    }
 
    //归档不良品
    @Transactional
    public Response stopDefective(int deftId) {
        //修改当前表记录
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.set("confirm_status",3);//归档
        wrapper.set("receiver_ids","");
        wrapper.set("receiver_names","");
        wrapper.set("rest_product",0);
        wrapper.set("note","");
        wrapper.eq("id",deftId);
        mapper.update(null,wrapper);
        //记录处理记录
        DefectiveProductsHistory defectiveHis=new DefectiveProductsHistory();
        defectiveHis.setDeftId(deftId);
        defectiveHis.setDelId(ActionUtil.getUser().getId().intValue());
        defectiveHis.setConfirmStatus(3);
        defectiveHis.setDelProduct(0);
        defectiveHis.setDelName(ActionUtil.getUser().getName());
        defectiveHis.setRestProduct(0);
        defectiveHisService.stopDefective(defectiveHis);
        return new Response().set(1,true);
    }
}