public UploadFileVO uploadFile(UploadFileInVO vo) throws Exception {
MultipartFile[] files = vo.getFile();
String serverUrl = vo.getServerUrl();
Boolean needSave = vo.getNeedSave();
String recordId = vo.getRecordId();
String type = vo.getType()==null?"":vo.getType();
BGoodsInfo target = null;
List<String> savePathList = new ArrayList<>();
if(needSave){
if(StringUtils.isEmpty(recordId)){
throw new ParamException("当需要持久化的时候,recordId不能为空");
}
target = getById(recordId);
if(target == null){
throw new BizException("要操作的商品数据不存在");
}
}
switch (type){
case "1":
case "2":
break;
default:
throw new ParamException("type不合法");
}
UploadFileVO result = new UploadFileVO();
for(MultipartFile file:files){
if(file.isEmpty()){
throw new BizException("文件内容为空,请重新选择文件!");
}
String fileExtension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String fileName = UUID.randomUUID().toString().replaceAll("-","") + fileExtension;
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String dateNowStr = sdf.format(d);
String imagePath = sysConfig.getImagePath();
String centerPath = "goods/"+dateNowStr+"/";
//检查该路径对应的目录是否存在. 如果不存在则创建目录
File directory=new File(imagePath+centerPath);
if (!directory.exists()) {
directory.mkdirs();
}
String savePath = centerPath + fileName;
//保存文件
File dest = new File(imagePath+savePath);
if (!(dest.exists())) {
file.transferTo(dest);
}
savePathList.add(savePath);
}
if(needSave){
switch (type){
case "1":
//确认修改商品图片
List<BDataInfo> dataInfos = new ArrayList<>();
for(String one:savePathList){
BDataInfo entity = new BDataInfo();
entity.setDataPath(one);
dataInfos.add(entity);
}
savePicBatch(false,dataInfos,recordId);
break;
case "2":
//确认修改二维码图片
target.setGBarCodePic(savePathList.get(0));
updateById(target);
break;
}
}
result.setServerUrl(serverUrl+sysConfig.getImageVisitor());
result.setPathList(savePathList);
return result;
}