Commit 0416601a authored by neo's avatar neo

[DEV] get template json api you hua

parent ba76c7eb
package pwc.taxtech.atms.common.util;
import pwc.taxtech.atms.exception.ApiException;
import pwc.taxtech.atms.exception.FormulaException;
import java.util.Collection;
public class MyAsserts {
public static void assertNotNull(Object obj, FormulaException exception) {
if (obj == null) throw exception;
}
public static void assertNotNull(Object obj, ApiException exception) {
if (obj == null) throw exception;
}
public static void assertTrue(boolean obj, ApiException exception) {
if (!obj) throw exception;
}
public static void assertFalse(boolean obj, ApiException exception) {
if (obj) throw exception;
}
public static void assertNotEmpty(String obj, ApiException exception) {
if (obj == null || obj.isEmpty()) throw exception;
}
public static void assertNotEmpty(Collection obj, ApiException exception) {
if (obj == null || obj.isEmpty()) throw exception;
}
}
......@@ -5,6 +5,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import pwc.taxtech.atms.exception.ApiException;
import pwc.taxtech.atms.exception.ApplicationException;
import pwc.taxtech.atms.exception.ServiceException;
......@@ -21,6 +22,8 @@ public class AtmsExceptionHandler extends ResponseEntityExceptionHandler {
return handleApplicationException((ApplicationException) ex);
} else if (ex instanceof ServiceException) {
return handleServiceException((ServiceException) ex);
} else if (ex instanceof ApiException) {
return ((ApiException) ex).handle();
} else {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
......
......@@ -6,12 +6,21 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import pwc.taxtech.atms.dto.vatdto.TemplateByGroupDto;
import pwc.taxtech.atms.common.util.MyAsserts;
import pwc.taxtech.atms.exception.ApplicationException;
import pwc.taxtech.atms.common.ftp.FTPClientPool;
import pwc.taxtech.atms.dto.*;
import pwc.taxtech.atms.entitiy.Template;
import pwc.taxtech.atms.exception.BadParameterException;
import pwc.taxtech.atms.exception.NotFoundException;
import pwc.taxtech.atms.service.TemplateService;
import javax.servlet.http.HttpServletResponse;
......@@ -48,40 +57,26 @@ public class TemplateController extends BaseController {
@RequestMapping(value = "getTemplateJson", method = RequestMethod.POST, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public @ResponseBody
void getTemplateBlob(@RequestParam Long templateID, HttpServletResponse response) throws URISyntaxException {
if (!isParamValid(templateID)) {
return;
}
String filePath;
File templateFile;
InputStream inputStream = null;
void getTemplateBlob(@RequestParam Long templateID, HttpServletResponse response) {
MyAsserts.assertTrue(isParamValid(templateID),new BadParameterException());
Template template = templateService.getTemplateByID(templateID);
String templatePath = templateService.getTemplatePath(templateID);
MyAsserts.assertNotEmpty(templatePath,new NotFoundException());
if (StringUtils.isBlank(templatePath)) {
return;
}
filePath = this.getClass().getResource("").toURI().getPath();
String tempPath = filePath.substring(0, filePath.indexOf("classes") + "\\classes".length());
templateFile = new File(tempPath + templatePath);
try {
//如果是系统报表就取本地文件夹,如果不是就取FTP
if (template.getIsSystemType()) {
inputStream = new BufferedInputStream(new FileInputStream(templateFile));
} else {
inputStream = ftpClientPool.download(templatePath);
}
try (InputStream inputStream = template.getIsSystemType() ? new BufferedInputStream(new FileInputStream(getTempFile(templatePath))) :
ftpClientPool.download(templatePath); OutputStream out = response.getOutputStream();) {
//客户端保存的文件名
String customFileName = "template_" + DateTime.now().toString("yyyyMMddHHmmss") + ".xlsx";
response.setHeader("Content-Disposition", String.format("inline; filename=\"" + customFileName + "\""));
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream();
while ((len = inputStream.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.flush();
//FileCopyUtils.copy(inputStream, response.getOutputStream());
} catch (FileNotFoundException e) {
......@@ -89,20 +84,16 @@ public class TemplateController extends BaseController {
} catch (Exception e) {
logger.error("Error downloading template file template.xlsx", e);
throw new ApplicationException("Error downloading template file template.xlsx", e);
} finally {
try {
templateFile = null;
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
logger.error("Error closing inputStream. ", e);
} finally {
inputStream = null;
}
}
}
private File getTempFile(String templatePath) throws URISyntaxException {
String filePath = this.getClass().getResource("").toURI().getPath();
String tempPath = filePath.substring(0, filePath.indexOf("classes") + "\\classes".length());
return new File(tempPath + templatePath);
}
@RequestMapping(value = "getTemplateUniqList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public @ResponseBody
List<TemplateUniqDto> getTemplateUniqList(String serviceTypeID, Integer payTaxType, Integer reportType, String industryIDs) {
......
package pwc.taxtech.atms.exception;
import org.springframework.http.ResponseEntity;
public abstract class ApiException extends RuntimeException {
public ApiException() {
super();
}
public ApiException(String message) {
super(message);
}
public ApiException(String message, Throwable cause) {
super(message, cause);
}
public ApiException(Throwable cause) {
super(cause);
}
public abstract<Object> ResponseEntity handle();
}
package pwc.taxtech.atms.exception;
import org.springframework.http.ResponseEntity;
public class BadParameterException extends ApiException {
@Override
public <Object> ResponseEntity handle() {
return ResponseEntity.badRequest().build();
}
}
package pwc.taxtech.atms.exception;
import org.springframework.http.ResponseEntity;
public class BadParameterWithMsgException extends ApiException {
@Override
public <Object> ResponseEntity handle() {
return ResponseEntity.badRequest().body(getMessage());
}
}
package pwc.taxtech.atms.exception;
import org.springframework.http.ResponseEntity;
public class NotFoundException extends ApiException {
@Override
public <Object> ResponseEntity handle() {
return ResponseEntity.notFound().build();
}
}
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