1. AttachmentSDK 增加了新的数据模型

2. Seata starter 调整了 seata包版本, 修改了统一配置文件
This commit is contained in:
ajaxfan
2021-02-08 11:58:53 +08:00
parent a9469363f4
commit 8b88f7314e
39 changed files with 540 additions and 191 deletions

View File

@ -6,6 +6,7 @@ import java.util.Optional;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentDetail;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.DownloadEntity;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.UploadObject;
/**
@ -44,6 +45,14 @@ public interface AttachmentClient {
*/
Optional<byte[]> downloadFilesByBusinessId(String businessId);
/**
* 下载业务ID下的所有资源
*
* @param bid
* @return
*/
Optional<DownloadEntity> downloadOriginalFilesByBusinessId(String businessId);
/**
* 下载指定的资源数据
*
@ -52,7 +61,15 @@ public interface AttachmentClient {
* @return
*/
Optional<byte[]> downloadFileByObjectId(String objectId);
/**
* 下载指定的资源数据
*
* @param bid
* @param oid
* @return
*/
Optional<DownloadEntity> downloadOriginalFileByObjectId(String objectId);
/**
* 上传资源文件
@ -62,8 +79,6 @@ public interface AttachmentClient {
* @return
*/
Optional<UploadObject> upload(String businessId, File file);
/**
* 上传资源文件
@ -74,4 +89,18 @@ public interface AttachmentClient {
*/
Optional<UploadObject> upload(String businessId, String filename, byte[] array);
/**
* 根据业务id删除附件
*
* @param businessId
*/
boolean deleteByBid(String businessId);
/**
* 根据对象id删除附件
*
* @param objectId
*/
String deleteByOid(String objectId);
}

View File

@ -6,6 +6,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -17,6 +18,7 @@ import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.convertor.FileConvertor;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.convertor.ModelConvertor;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentDetail;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.DownloadEntity;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.UploadObject;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.service.DocumentCenterService;
@ -42,6 +44,16 @@ public class DefaultAttachmentClient implements AttachmentClient {
/* 文件转换器 */
private @Autowired FileConvertor fileConvertor;
/**
* 生成业务id
*
* @return
*/
@Override
public Optional<String> getBusinessId() {
return Optional.of(String.format("{\"id\":\"%s\"}", IdUtil.getSnowflake(23, 16).nextIdStr()));
}
/**
* 根据业务id列表查询详单
*
@ -66,16 +78,6 @@ public class DefaultAttachmentClient implements AttachmentClient {
return Optional.ofNullable(detail);
}
/**
* 生成业务id
*
* @return
*/
@Override
public Optional<String> getBusinessId() {
return Optional.of(String.format("{\"id\":\"%s\"}", IdUtil.getSnowflake(23, 16).nextIdStr()));
}
/**
* 查询指定Id文件的详细信息
*
@ -121,7 +123,7 @@ public class DefaultAttachmentClient implements AttachmentClient {
try (ZipOutputStream zout = new ZipOutputStream(out)) {
list.stream().forEach(data -> {
try {
zout.putNextEntry(new ZipEntry(data.getId() + data.getFilename()));// 文件项名称
zout.putNextEntry(new ZipEntry(data.getFilename()));// 文件项名称
zout.write(downloadFileByObjectId(data.getId()).get());// 文件数据流
} catch (IOException e) {
log.error(e.getMessage());
@ -137,6 +139,45 @@ public class DefaultAttachmentClient implements AttachmentClient {
return Optional.empty();
}
@Override
public Optional<DownloadEntity> downloadOriginalFilesByBusinessId(String businessId) {
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
if (op.isPresent()) {
List<AttachmentEntity> list = op.get().get(businessId);
// 如果业务id下只包含单独一个文件则直接返回该文件数据
if (list.size() == 1) {
return downloadOriginalFileByObjectId(list.get(0).getId());
}
// 如果大于1个则下载返回压缩包数据流
else if (list.size() > 1) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
try (ZipOutputStream zout = new ZipOutputStream(out)) {
list.stream().forEach(data -> {
try {
zout.putNextEntry(new ZipEntry(data.getFilename()));// 文件项名称
zout.write(downloadFileByObjectId(data.getId()).get());// 文件数据流
} catch (IOException e) {
log.error(e.getMessage());
}
});
}
return Optional.ofNullable(new DownloadEntity()
.setFilename(String.format("%s.zip", UUID.randomUUID())).setStream(out.toByteArray()));
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return Optional.empty();
}
@Override
public Optional<DownloadEntity> downloadOriginalFileByObjectId(String objectId) {
return modelConvertor.toDownloadEntity(documentCenterService.getObjectDetail(objectId));
}
/**
* 文件上传
*
@ -150,7 +191,7 @@ public class DefaultAttachmentClient implements AttachmentClient {
log.debug(res);
return Optional.empty();
return modelConvertor.toUploadObject(res);
}
@Override
@ -160,7 +201,31 @@ public class DefaultAttachmentClient implements AttachmentClient {
log.debug(res);
return Optional.empty();
return modelConvertor.toUploadObject(res);
}
@Override
public boolean deleteByBid(String businessId) {
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
/*
* AttachmentDetail 是一个Map类型对象 key 业务id value List<AttachmentEntity>
*/
op.ifPresent(attachmentDetail -> {
attachmentDetail.keySet().stream().forEach(key -> {
List<AttachmentEntity> list = attachmentDetail.get(key);
list.stream().forEach(attachmentEntity -> {
documentCenterService.deleteByOid(attachmentEntity.getId());
});
});
});
return op.isPresent();
}
@Override
public String deleteByOid(String objectId) {
return documentCenterService.deleteByOid(objectId);
}
}

View File

@ -0,0 +1,8 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.config;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignSupportConfig {
}

View File

@ -1,16 +1,23 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.convertor;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Optional;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.DownloadEntity;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.UploadObject;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.SysStorageVO;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.down.DownStream;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.down.SysStorageVO;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.query.QueryResult;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.upload.UploadStream;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.upload.UploadStreamData;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.hutool.core.net.URLEncoder;
import lombok.extern.slf4j.Slf4j;
/**
@ -39,7 +46,7 @@ public class ModelConvertor {
AttachmentEntity entity = null;
try {
SysStorageVO vo = toSysStorageVO(json);
SysStorageVO vo = tpDownPO(json);
log.debug("convert to model: {}", vo);
entity = new AttachmentEntity();
@ -72,13 +79,36 @@ public class ModelConvertor {
byte[] array = null;
try {
array = toSysStorageVO(json).getFileStream();
array = tpDownPO(json).getFileStream();
} catch (JsonProcessingException e) {
log.error(e.getMessage());
}
return Optional.ofNullable(array);
}
public Optional<DownloadEntity> toDownloadEntity(String json) {
DownloadEntity entity = new DownloadEntity();
try {
SysStorageVO vo = tpDownPO(json);
entity.setFilename(URLEncoder.DEFAULT.encode(vo.getOriginalName(), Charset.forName("UTF-8")))
.setStream(vo.getFileStream());
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return Optional.ofNullable(entity);
}
public Optional<UploadObject> toUploadObject(String json) {
try {
return Optional.ofNullable(new UploadObject().setId(toUploadPO(json).getFileId()));
} catch (JsonProcessingException e) {
log.error(e.getMessage());
}
return Optional.empty();
}
/////////////////////////////////////////////////////////////////////////// private
/////////////////////////////////////////////////////////////////////////// method
/**
@ -89,9 +119,18 @@ public class ModelConvertor {
* @throws JsonMappingException
* @throws JsonProcessingException
*/
private SysStorageVO toSysStorageVO(String json) throws JsonMappingException, JsonProcessingException {
private SysStorageVO tpDownPO(String json) throws JsonMappingException, JsonProcessingException {
log.debug("current convertor json is: {}", json);
return objectMapper.readValue(json, DownStream.class).getData().getSysStorageVO();
}
private SysStorageVO toUploadPO(String json) throws JsonMappingException, JsonProcessingException {
log.debug("current convertor json is: {}", json);
List<UploadStreamData> list = objectMapper.readValue(json, UploadStream.class).getData();
if (list.size() > 0) {
return list.get(0).getSysStorageVO();
}
return null;
}
}

View File

@ -25,4 +25,9 @@ public class DocumentCenterServiceFallback implements DocumentCenterService {
return null;
}
@Override
public String deleteByOid(String fileId) {
return null;
}
}

View File

@ -0,0 +1,13 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class DownloadEntity {
private String filename;
private byte[] stream;
}

View File

@ -27,7 +27,7 @@ public interface DocumentCenterService {
* @param fileId
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/downloadAllStream")
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/downloadFileAllStream")
String getObjectDetail(@RequestParam("fileId") String fileId);
/**
@ -48,4 +48,13 @@ public interface DocumentCenterService {
String upload(@RequestPart("appCode") String appCode, @RequestPart("objectId") String objectId,
@RequestPart("multipartFiles") MultipartFile multipartFiles);
/**
* 删除附件信息
*
* @param fileId
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/disk")
String deleteByOid(@RequestParam("fileId") String fileId);
}

View File

@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.config.FeignSupportConfig;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.fallback.QueryServiceFallback;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentDetail;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
@ -17,7 +18,7 @@ import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.FeedbackMessage;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.Snowflake;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.param.QueryParameter;
@FeignClient(value = "core-service-ebtp-updownload", fallback = QueryServiceFallback.class)
@FeignClient(value = "core-service-ebtp-updownload", configuration = FeignSupportConfig.class, fallback = QueryServiceFallback.class)
public interface QueryService {
@RequestMapping(method = RequestMethod.GET, value = "v1/business/id")

View File

@ -1,4 +1,4 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.down;
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo;
import lombok.Data;

View File

@ -1,5 +1,7 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.down;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.SysStorageVO;
import lombok.Data;
@Data

View File

@ -0,0 +1,15 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.upload;
import java.util.List;
import lombok.Data;
@Data
public class UploadStream {
private Integer code;
private Boolean success;
private String message;
private List<UploadStreamData> data;
}

View File

@ -0,0 +1,12 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.upload;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.SysStorageVO;
import lombok.Data;
@Data
public class UploadStreamData {
private SysStorageVO sysStorageVO;
}