Commit a2da757d authored by frank.xa.zhang's avatar frank.xa.zhang

add attachment file upload function -- frank

parent 22d6151f
......@@ -8,6 +8,7 @@ package pwc.taxtech.atms.common.util;
* Version 1.0
**/
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
......@@ -128,6 +129,23 @@ public class FileExcelUtil {
}
}
public static void setFileHeadInfo(HttpServletResponse response, HttpServletRequest request, String fileName) {
try {
// 获取客户端浏览器的类型
String agent = request.getHeader("User-Agent");
// 对文件名重新编码
String encodingFileName = FileExcelUtil.encodeDownloadFilename(fileName, agent);
// 告诉客户端允许断点续传多线程连接下载
response.setHeader("Accept-Ranges", "bytes");
//文件后缀
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment; filename=" + encodingFileName);
} catch (IOException e) {
logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + "发生的异常是: ", e);
throw new RuntimeException(e);
}
}
/**
* 设置下载zip的响应头信息
*
......@@ -188,6 +206,20 @@ public class FileExcelUtil {
}
}
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, String fileName, String filePathToBeServed) {
FileExcelUtil.setFileHeadInfo(response, request, fileName);
try {
File fileToDownload = new File(filePathToBeServed);
InputStream inputStream = new FileInputStream(fileToDownload);
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
inputStream.close();
} catch (Exception exception) {
logger.error(exception.getMessage());
}
}
/**
* 生成excel到指定路径
*
......@@ -277,7 +309,7 @@ public class FileExcelUtil {
public static void deleteColumn(Sheet sheet, int columnToDelete, List<Integer> cols) {
for (int rId = 0; rId <= sheet.getLastRowNum(); rId++) {
Row row = sheet.getRow(rId);
if(row == null)
if (row == null)
continue;
for (int cID = columnToDelete; cID <= row.getLastCellNum(); cID++) {
Cell cOld = row.getCell(cID);
......@@ -309,7 +341,7 @@ public class FileExcelUtil {
*/
public static void cloneCell(Cell cNew, Cell cOld) {
try {
if(cOld == null || cNew == null)
if (cOld == null || cNew == null)
return;
cNew.setCellComment(cOld.getCellComment());
cNew.setCellStyle(cOld.getCellStyle());
......
......@@ -17,6 +17,8 @@ import pwc.taxtech.atms.organization.entity.OrganizationDirector;
import pwc.taxtech.atms.organization.entity.OrganizationShareholder;
import pwc.taxtech.atms.service.impl.OrganizationHKServiceImpl;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@RestController
......@@ -121,7 +123,7 @@ public class OrganizationHKController {
@RequestMapping(value = "upload", method = RequestMethod.POST)
public @ResponseBody
OperationResultDto<Object> uploadOrgAttach(@RequestParam(value = "file") CommonsMultipartFile[] files, @RequestParam(value = "entityId") String orgId) {
OperationResultDto<Object> uploadOrgAttach(@RequestParam(value = "file") CommonsMultipartFile[] files, @RequestParam String entityId) {
logger.info("POST /api/v1/orgHK/upload");
logger.debug("enter upload file method");
OperationResultDto<Object> resultDto = new OperationResultDto<>();
......@@ -140,20 +142,35 @@ public class OrganizationHKController {
}
}
logger.debug("validate entityId");
if (StringUtils.isEmpty(orgId) || !organizationHKService.checkOrgExist(orgId)) {
if (StringUtils.isEmpty(entityId) || !organizationHKService.checkOrgExist(entityId)) {
resultDto.setResult(false);
logger.warn("organization [{}] not exist", orgId);
resultDto.setResultMsg("organization " + orgId + " not exist");
logger.warn("organization [{}] not exist", entityId);
resultDto.setResultMsg("organization " + entityId + " not exist");
return resultDto;
}
logger.debug("start upload");
try {
resultDto = organizationHKService.uploadAttachs(files, orgId);
resultDto = organizationHKService.uploadAttachs(files, entityId);
} catch (ApplicationException ex) {
resultDto.setResult(false);
}
return resultDto;
}
@RequestMapping(value = "downloadAttachment", method = RequestMethod.GET)
public @ResponseBody
OperationResultDto<Object> downloadAttachment(HttpServletRequest request, HttpServletResponse response, @RequestParam("attachmentId") Long attachmentId) {
logger.info("POST /api/v1/orgHK/downloadAttachment");
return organizationHKService.downloadAttachment(request, response, attachmentId);
}
@RequestMapping(value = "deleteAttachment", method = RequestMethod.DELETE)
public @ResponseBody
OperationResultDto<Object> deleteAttachment(@RequestParam("attachmentId") Long attachmentId) {
logger.info("POST /api/v1/orgHK/deleteAttachment");
return organizationHKService.deleteAttachment(attachmentId);
}
}
......@@ -16,6 +16,7 @@ import org.springframework.web.multipart.commons.CommonsMultipartFile;
import pwc.taxtech.atms.common.*;
import pwc.taxtech.atms.common.message.LogMessage;
import pwc.taxtech.atms.common.message.OrganizationMessage;
import pwc.taxtech.atms.common.util.FileExcelUtil;
import pwc.taxtech.atms.dto.OperationLogDto;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.UpdateLogParams;
......@@ -26,12 +27,19 @@ import pwc.taxtech.atms.organization.dao.OrganizationAttachmentMapper;
import pwc.taxtech.atms.organization.dao.OrganizationDirectorMapper;
import pwc.taxtech.atms.organization.dao.OrganizationHKMapper;
import pwc.taxtech.atms.organization.dao.OrganizationShareholderMapper;
import pwc.taxtech.atms.organization.dpo.OrganizationAttachmentDto;
import pwc.taxtech.atms.organization.dpo.OrganizationDirectorDto;
import pwc.taxtech.atms.organization.dpo.OrganizationHKDto;
import pwc.taxtech.atms.organization.dpo.OrganizationShareholderDto;
import pwc.taxtech.atms.organization.entity.*;
import pwc.taxtech.atms.organization.mapper.OrganizationAttachmentTMapper;
import pwc.taxtech.atms.organization.mapper.OrganizationDirectorTMapper;
import pwc.taxtech.atms.organization.mapper.OrganizationHKTMapper;
import pwc.taxtech.atms.organization.mapper.OrganizationShareholderTMapper;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
......@@ -74,6 +82,18 @@ public class OrganizationHKServiceImpl {
@Resource
private OrganizationAttachmentMapper organizationAttachmentMapper;
@Autowired
private OrganizationShareholderTMapper organizationShareholderTMapper;
@Autowired
private OrganizationAttachmentTMapper organizationAttachmentTMapper;
@Autowired
private OrganizationDirectorTMapper organizationDirectorTMapper;
@Autowired
private OrganizationHKTMapper organizationHKTMapper;
@Value("${orgaization.file.location}")
private String fileDirectory;
......@@ -82,12 +102,15 @@ public class OrganizationHKServiceImpl {
List<OrganizationHK> organizationHKS = organizationHKMapper.selectByExample(null);
List<OrganizationShareholder> organizationShareholders = organizationShareholderMapper.selectByExample(null);
List<OrganizationDirector> organizationDirectors = organizationDirectorMapper.selectByExample(null);
List<OrganizationAttachment> organizationAttachments = organizationAttachmentMapper.selectByExample(null);
organizationHKS.forEach(a -> {
OrganizationHKDto organizationHKDto = CommonUtils.copyProperties(a, new OrganizationHKDto());
List<OrganizationShareholderDto> tempShareholders = organizationShareholders.stream().filter(b -> b.getEntityId().equals(a.getId())).map(b -> CommonUtils.copyProperties(b, new OrganizationShareholderDto())).collect(Collectors.toList());
List<OrganizationDirectorDto> tempDirectors = organizationDirectors.stream().filter(c -> c.getEntityId().equals(a.getId())).map(c -> CommonUtils.copyProperties(c, new OrganizationDirectorDto())).collect(Collectors.toList());
OrganizationHKDto organizationHKDto = organizationHKTMapper.toOrganizationHKDto(a);
List<OrganizationShareholderDto> tempShareholders = organizationShareholders.stream().filter(b -> b.getEntityId().equals(a.getId())).map(organizationShareholderTMapper::toOrgShareholder).collect(Collectors.toList());
List<OrganizationDirectorDto> tempDirectors = organizationDirectors.stream().filter(c -> c.getEntityId().equals(a.getId())).map(organizationDirectorTMapper::toOrganizationDirectorDto).collect(Collectors.toList());
List<OrganizationAttachmentDto> tempAttachments = organizationAttachments.stream().filter(d -> d.getEntityId().equals(a.getId())).map(organizationAttachmentTMapper::toOrganizationAttachmentDto).collect(Collectors.toList());
organizationHKDto.setShareholders(tempShareholders);
organizationHKDto.setDirectors(tempDirectors);
organizationHKDto.setAttachments(tempAttachments);
organizationHKDtos.add(organizationHKDto);
});
return organizationHKDtos;
......@@ -232,8 +255,7 @@ public class OrganizationHKServiceImpl {
return organizationDto;
}
private OrganizationHKDto genarateSubOrgs(OrganizationHKDto parentOrg, List<OrganizationHK> orgs,
List<ServiceType> serviceList, Integer useType) {
private OrganizationHKDto genarateSubOrgs(OrganizationHKDto parentOrg, List<OrganizationHK> orgs, List<ServiceType> serviceList, Integer useType) {
if (parentOrg.getParentId() > 0) {
OrganizationHK organization = orgs.stream().filter(x -> x.getId().equals(parentOrg.getParentId()))
.collect(Collectors.toList()).get(0);
......@@ -519,7 +541,7 @@ public class OrganizationHKServiceImpl {
final String fileNameToSave = (fileNameYearMonthDay + "_" + fileNameWithoutExtension + "_"
+ CommonUtils.getUUID().substring(0, 6) + "." + fileExtension).toLowerCase();
// relatedDirectory:相对路径目录,以/结尾
final String relatedDirectory = pathYearMonth + "/" + "organization attachments" + "/";
final String relatedDirectory = pathYearMonth + "/";
// generatedFileDirectory:绝对路径目录
final String generatedFileDirectory = fileDirectory + relatedDirectory;
......@@ -548,7 +570,7 @@ public class OrganizationHKServiceImpl {
result.setResultMsg("output file is empty");
return result;
}
result.setData(organizationAttachments);
result.setResult(true);
logger.debug("output file size: {}", organizationAttachments.size());
logger.debug("OrganizationHKServiceImpl uploadAttachs end");
......@@ -581,7 +603,26 @@ public class OrganizationHKServiceImpl {
throw new ApplicationException(e.getMessage());
}
OperationResultDto<Object> saveResult = fileService.saveFile(inputStream, filePath);
return saveResult;
}
public OperationResultDto<Object> downloadAttachment(HttpServletRequest request, HttpServletResponse response, Long attachmentId) {
OperationResultDto<Object> resultDto = new OperationResultDto<>();
OrganizationAttachment attachment = organizationAttachmentMapper.selectByPrimaryKey(attachmentId);
Assert.notNull(attachment, "attachemtn is null");
String fileName = attachment.getDocumentName();
String filePath = fileDirectory + attachment.getDocumentPath();
FileExcelUtil.downloadFile(request, response, fileName, filePath);
resultDto.setResult(true);
return resultDto;
}
public OperationResultDto<Object> deleteAttachment(Long attachmentId) {
OperationResultDto<Object> resultDto = new OperationResultDto<>();
OrganizationAttachmentExample example = new OrganizationAttachmentExample();
example.createCriteria().andIdEqualTo(attachmentId);
organizationAttachmentMapper.deleteByExample(example);
resultDto.setResult(true);
return resultDto;
}
}
......@@ -69,3 +69,5 @@ email.store.protocol=${email.store.protocol}
email.address=${email.address}
email.password=${email.password}
email.aesKey=${email.aesKey}
orgaization.file.location=${orgaization.file.location}
......@@ -64,4 +64,6 @@ org_sync_url=http://10.96.238.10/erp-main-data-test-v2/api/companies
org_sync_token=174af08f
dd_pubkey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKUfMPRKV6I5num1dDWcxTrgTjXf5LctsVj0CpbwHE83mmjUO5CAlvA0Fwy30ajCX5sLmsyi+Eu/4uNmM6GQF3kCAwEAAQ==
ebs_call_url=http://172.20.201.201:8020/ebs-proxy-test/dts
\ No newline at end of file
ebs_call_url=http://172.20.201.201:8020/ebs-proxy-test/dts
orgaization.file.location=C:/TEMP/organization/upload
......@@ -63,4 +63,6 @@ org_sync_token=174af08f
dd_pubkey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKUfMPRKV6I5num1dDWcxTrgTjXf5LctsVj0CpbwHE83mmjUO5CAlvA0Fwy30ajCX5sLmsyi+Eu/4uNmM6GQF3kCAwEAAQ==
ebs_call_url=http://172.20.3.109:8010/ebs-proxy-test/dts
\ No newline at end of file
ebs_call_url=http://172.20.3.109:8010/ebs-proxy-test/dts
orgaization.file.location=/mnt/organization/upload/
\ No newline at end of file
......@@ -63,4 +63,6 @@ org_sync_token=174af08f
dd_pubkey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKUfMPRKV6I5num1dDWcxTrgTjXf5LctsVj0CpbwHE83mmjUO5CAlvA0Fwy30ajCX5sLmsyi+Eu/4uNmM6GQF3kCAwEAAQ==
ebs_call_url=http://172.20.3.109:8020/ebs-proxy-test/dts
\ No newline at end of file
ebs_call_url=http://172.20.3.109:8020/ebs-proxy-test/dts
orgaization.file.location=/mnt/organization/upload/
\ No newline at end of file
......@@ -63,4 +63,6 @@ org_sync_token=174af08f
dd_pubkey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKUfMPRKV6I5num1dDWcxTrgTjXf5LctsVj0CpbwHE83mmjUO5CAlvA0Fwy30ajCX5sLmsyi+Eu/4uNmM6GQF3kCAwEAAQ==
ebs_call_url=http://172.20.2.220:8080/ebs-proxy-stage/dts
\ No newline at end of file
ebs_call_url=http://172.20.2.220:8080/ebs-proxy-stage/dts
orgaization.file.location=/mnt/organization/upload/
\ No newline at end of file
......@@ -64,4 +64,6 @@ org_sync_token=174af08f
dd_pubkey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKUfMPRKV6I5num1dDWcxTrgTjXf5LctsVj0CpbwHE83mmjUO5CAlvA0Fwy30ajCX5sLmsyi+Eu/4uNmM6GQF3kCAwEAAQ==
ebs_call_url=http://172.20.201.98:8020/ebs-proxy-test/dts
\ No newline at end of file
ebs_call_url=http://172.20.201.98:8020/ebs-proxy-test/dts
orgaization.file.location=/mnt/organization/upload/
\ No newline at end of file
......@@ -13,6 +13,11 @@
<version>0.1.1</version>
</parent>
<properties>
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
<lombok.version>1.18.8</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
......@@ -95,8 +100,44 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins></build>
</project>
package pwc.taxtech.atms.organization.dpo;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class OrganizationAttachmentDto {
@JsonSerialize(using = ToStringSerializer.class)
String id;
String entityId;
String documentName;
}
......@@ -53,4 +53,6 @@ public class OrganizationHKDto {
private List<OrganizationDirectorDto> directors;
@JsonProperty("shareholders")
private List<OrganizationShareholderDto> shareholders;
@JsonProperty("attachments")
private List<OrganizationAttachmentDto> attachments;
}
package pwc.taxtech.atms.organization.dpo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@Getter
@Setter
@ToString
public class UploadAttachmentDto {
CommonsMultipartFile[] files;
String entityId;
}
package pwc.taxtech.atms.organization.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import pwc.taxtech.atms.organization.dpo.OrganizationAttachmentDto;
import pwc.taxtech.atms.organization.entity.OrganizationAttachment;
@Mapper(componentModel = "spring")
public interface OrganizationAttachmentTMapper {
OrganizationAttachmentTMapper ORGANIZATION_ATTACHMENT_MAPPER = Mappers.getMapper(OrganizationAttachmentTMapper.class);
@Mapping(source = "id", target = "id")
OrganizationAttachmentDto toOrganizationAttachmentDto(OrganizationAttachment organizationAttachment);
}
package pwc.taxtech.atms.organization.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import pwc.taxtech.atms.organization.dpo.OrganizationDirectorDto;
import pwc.taxtech.atms.organization.entity.OrganizationDirector;
@Mapper(componentModel = "spring")
public interface OrganizationDirectorTMapper {
OrganizationDirectorTMapper ORGANIZATION_DIRECTOR_T_MAPPER = Mappers.getMapper(OrganizationDirectorTMapper.class);
@Mapping(source = "id", target = "id")
OrganizationDirectorDto toOrganizationDirectorDto(OrganizationDirector organizationDirector);
}
package pwc.taxtech.atms.organization.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import pwc.taxtech.atms.organization.dpo.OrganizationHKDto;
import pwc.taxtech.atms.organization.entity.OrganizationHK;
@Mapper(componentModel = "spring")
public interface OrganizationHKTMapper {
OrganizationHKTMapper ORGANIZATION_HK_MAPPER = Mappers.getMapper(OrganizationHKTMapper.class);
@Mapping(source = "createTime", target = "createTime" ,dateFormat = "yyyy/MM/dd")
@Mapping(source = "updateTime", target = "updateTime" ,dateFormat = "yyyy/MM/dd")
@Mapping(source = "dateOfIncorporation", target = "dateOfIncorporation" ,dateFormat = "yyyy/MM/dd")
OrganizationHKDto toOrganizationHKDto(OrganizationHK organizationHK);
}
package pwc.taxtech.atms.organization.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import pwc.taxtech.atms.organization.dpo.OrganizationShareholderDto;
import pwc.taxtech.atms.organization.entity.OrganizationShareholder;
@Mapper(componentModel = "spring")
public interface OrganizationShareholderTMapper {
OrganizationShareholderTMapper ORGANIZATION_SHAREHOLDER_T_MAPPER = Mappers.getMapper(OrganizationShareholderTMapper.class);
@Mapping(source = "id",target = "id")
OrganizationShareholderDto toOrgShareholder(OrganizationShareholder organizationShareholder);
}
......@@ -40,6 +40,12 @@ webservices.factory('orgHKService', ['$http', 'apiConfig', function ($http, apiC
},
updateShareholders: function (orgShareholders) {
return $http.post('/orgHK/updateShareholders', orgShareholders, apiConfig.create());
},
attachmentDownload: function (attachmentId) {
return $http.get('/orgHK/downloadAttachment?attachmentId=' + attachmentId, apiConfig.create({responseType: 'arraybuffer'}));
},
attachmentDelete: function (attachmentId) {
return $http.delete('/orgHK/downloadAttachment?attachmentId=' + attachmentId, apiConfig.create());
}
}
}]);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment