xinyb_
2021-09-24 17c1055ac108e6bef6c2b515c58b2f9ecea0da73
src/com/yc/batchUpload/service/impl/BatchUploadServiceImpl.java
@@ -5,8 +5,9 @@
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.SessionKey;
import org.commontemplate.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -14,7 +15,9 @@
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @USER: xinyb_
@@ -24,46 +27,104 @@
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, HttpServletRequest request) throws Exception {
    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);
            AttachmentWhereEntity Where = null;
            String matCode = "";
            Map<String,String> matCodeMap = new HashMap<String,String>();
            int cont = 0;
            for (MultipartFile file : files) {
                if (file.getSize() > 0) {
                    AttachmentEntity att = new AttachmentEntity();
                    matCode = file.getOriginalFilename();
                    att.setUnid(UUID.randomUUID().toString().toUpperCase());
                    att.setOriginalFileName(matCode);
                    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());
                    att.setSmallPicture(file.getBytes());
                    att.setFileType(file.getContentType());
                    att.setUploadTime(new Date());
                    att.setLastUpdateTime(new Date());
                    att.setFieldId("photoPath");
                    att.setFieldId(fieldId);
                    att.setFormId(id);
                    att.setAuthorCode((String) request.getSession().getAttribute(SessionKey.USERCODE));
                    att.setAuthorName((String) request.getSession().getAttribute(SessionKey.USERNAME));
                    Where = attachmentIfc.saveAttachment(att, file, "0");
                    if (Where != null && !"".equals(Where.getSeq())) {
                        matCode = matCode.substring(0, matCode.lastIndexOf(".")).trim();
                        cont = batchUploadPhotoPath.UpdatePhotoPath(Where, matCode);
                    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);
                       }
                    }
                }
            }
            if (cont == 1) {
                msg.setOk(cont);
            }
            msg.setOk(cont);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
//            msg.setMsg(e.getCause()!=null?e.getCause().getMessage():e.getMessage());
           e.printStackTrace();
            throw e;
        }
    }