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;
|
}
|
}
|
}
|