xinyb
2023-11-23 9bea358a93cdb1d6a1ff31321edebb4052635199
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
package com.yc.batchUpload.service.impl;
 
import com.yc.batchUpload.service.BatchUploadPhotoPath;
import com.yc.batchUpload.service.BatchUploadService;
import com.yc.batchUpload.utils.CallBackMsg;
import com.yc.entity.attachment.AttachmentEntity;
import com.yc.entity.attachment.AttachmentWhereEntity;
import com.yc.sdk.shopping.entity.MatCodeEntity;
import com.yc.sdk.shopping.service.MatCodeIfc;
import com.yc.service.upload.AttachmentIfc;
import com.yc.utils.ImageUtils;
import org.commontemplate.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @USER: xinyb_
 * @DATE: 2021-09-17 14:25
 */
@Service("BatchUploadServiceImpl")
public class BatchUploadServiceImpl implements BatchUploadService {
    @Autowired
    private AttachmentIfc attachmentIfc;
    @Autowired 
    private MatCodeIfc matCodeIfc ;
    @Autowired
    private BatchUploadPhotoPath batchUploadPhotoPath;
 
    @Override
    public CallBackMsg uploadBatchImage(String formId, List<MultipartFile> files,String userCode,String userName, HttpServletRequest request) throws Exception {
        CallBackMsg msg = new CallBackMsg();
        try {
            //String userCode = (String)request.getSession().getAttribute(SessionKey.USERCODE);
            //String userName = (String) request.getSession().getAttribute(SessionKey.USERNAME) ;
            Integer id = (formId != null ? Integer.parseInt(formId) : 0);
            Map<String,String> matCodeMap = new HashMap<String,String>();
            int cont = 0;
            for (MultipartFile file : files) {
                if (file.getSize() > 0) {
                    AttachmentEntity att = new AttachmentEntity();
                    String fieldId = "PhotoPath" ;
                    
                    String originalFilename = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")).trim();
                    String matCodes[] = originalFilename.split("@") ;   //同一个物料编号如果上传多个图片时,文件名之间用分号隔开,如主图片:a.jpg , 明细图片: a@1.jpg,a@2.jpg , a@3.jpg 
                    String matCode = matCodes[0] ;
                    if (matCodes.length==1) {   //如果没有后缀,则表示更新主图片 PhotoPath 字段,如果有后缀则表示更新 明细图片 Images 字段
                        fieldId = "PhotoPath" ;
                        if (matCodeMap.get(matCode+"_PhotoPath") == null) {
                            //先清除已经存在的图片
                            batchUploadPhotoPath.deteleMatCodePhotoPath(matCode);
                            
                            //表示已经更新过主图片了
                            matCodeMap.put(matCode+"_PhotoPath", matCode) ;
                        }
                    }else {
                        fieldId = "Images" ;
                        if (matCodeMap.get(matCode+"_Images") == null) {
                            //先清除已经存在的图片
                            batchUploadPhotoPath.deteleMatCodeImages(matCode);
                        
                            //表示已经更新过明细图片了
                            matCodeMap.put(matCode+"_Images", matCode) ;
                        }
                    }
                    
                    MatCodeEntity matCodeEntity = matCodeIfc.getMatCodeForGenerationQrCode( matCode);
                    if (matCodeEntity == null) continue;
                    
                    //取已经存在的unid  ,主图片 
                    if (fieldId.equals("PhotoPath")) {
                        if (matCodeEntity.getPhotoPath()!=null&&!"".equals(matCodeEntity.getPhotoPath())) {
                            String[] unids = matCodeEntity.getPhotoPath().split(";") ;
                            String unid = unids[0] ;
                            Integer seq = null;
                            if (unids.length>1) {
                                seq = Integer.parseInt( unids[1]);
                            }
                            att.setUnid(unid);
                            att.setSeq(seq);
                        }
                    }else {   //明细图片
                        if (matCodeEntity.getImages()!=null&& !"".equals(matCodeEntity.getImages())) {
                            String[] unids = matCodeEntity.getImages().split(";") ;
                            String unid = unids[0] ;
                            att.setUnid(unid);
                        }
                        
                    }
                    if (att.getUnid()==null|| att.getUnid().equals("")) {
                        att.setUnid(UUID.randomUUID().toString().toUpperCase());
                    }
                    att.setOriginalFileName(file.getOriginalFilename());
                    att.setFileSize(file.getSize());
                    att.setOriginalPicture(file.getBytes());
                    if(file.getContentType().contains("image")) {//是图片需要进行缩略图处理
                        att.setSmallPicture(ImageUtils.buildSmallPicReName(file, 180, 180));//缩略图
                    }
//                    att.setSmallPicture(file.getBytes());
                    att.setFileType(file.getContentType());
                    att.setUploadTime(new Date());
                    att.setLastUpdateTime(new Date());
                    att.setFieldId(fieldId);
                    att.setFormId(id);
                    att.setDoccode(matCodeEntity.getDocCode());
                    att.setRowId(matCode);
                    att.setAuthorCode(userCode);
                    att.setAuthorName(userName);
                    AttachmentWhereEntity attachmentWhereEntity = attachmentIfc.saveAttachment(att, file, fieldId.equals("PhotoPath")? "0":"2");
                    if (attachmentWhereEntity != null && !"".equals(attachmentWhereEntity.getUnid())) {
                        if (fieldId.equals("PhotoPath")) {
                            //更新主图片
                            cont = batchUploadPhotoPath.updateMatCodePhotoPath(attachmentWhereEntity, matCode);
                        } else {
                            //更新明细图片
                            cont = batchUploadPhotoPath.updateMatCodeImages(attachmentWhereEntity, matCode);
                        }
                    }
                }
            }
            msg.setOk(cont);
            return msg;
        } catch (Exception e) {
//            msg.setMsg(e.getCause()!=null?e.getCause().getMessage():e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }
}