TemplateController.java 5.66 KB
Newer Older
1 2
package pwc.taxtech.atms.controller;

3
import org.apache.commons.lang3.StringUtils;
4
import org.joda.time.DateTime;
5 6 7 8 9
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.*;
10
import pwc.taxtech.atms.common.ApplicationException;
11 12
import pwc.taxtech.atms.common.ftp.FTPClientPool;
import pwc.taxtech.atms.dto.*;
13
import pwc.taxtech.atms.entitiy.Template;
14 15
import pwc.taxtech.atms.service.TemplateService;

16 17
import javax.servlet.http.HttpServletResponse;
import java.io.*;
18
import java.net.URISyntaxException;
19 20
import java.util.Collections;
import java.util.List;
21 22 23 24

@RestController
@RequestMapping(value = "api/v1/template")
public class TemplateController {
25
    private static final Logger logger = LoggerFactory.getLogger(TemplateController.class);
26 27 28

    @Autowired
    TemplateService templateService;
29 30 31

    @Autowired
    FTPClientPool ftpClientPool;
32

33 34
    @RequestMapping(value = "get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
35
    List<TemplateDto> get(@RequestParam String templateGroupID, @RequestParam String reportType) {
36 37 38 39 40
        if (StringUtils.isEmpty(templateGroupID)) {
            return Collections.emptyList();
        }

        try {
41
            return templateService.get(templateGroupID, (reportType != null && !"null".equals(reportType)) ? Integer.parseInt(reportType) : null);
42 43 44 45 46
        } catch (Exception e) {
            logger.error("GetCellConfigList", e);
        }
        return Collections.emptyList();
    }
47

48
    @RequestMapping(value = "getTemplateJson", method = RequestMethod.POST, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
49
    public @ResponseBody
50
    void getTemplateBlob(@RequestParam String templateID, HttpServletResponse response) throws URISyntaxException {
51 52 53
        String filePath;
        File templateFile;
        InputStream inputStream = null;
54
        Template template = templateService.getTemplateByID(templateID);
55
        String templatePath = templateService.getTemplatePath(templateID);
56
        if (StringUtils.isBlank(templateID)) {
57 58 59
            return;
        }

60
        if (StringUtils.isBlank(templatePath)) {
61 62
            return;
        }
63
        filePath = this.getClass().getResource("").toURI().getPath();
64
        String tempPath = filePath.substring(0, filePath.indexOf("classes") + "\\classes".length());
65
        templateFile = new File(tempPath + templatePath);
66 67

        try {
68 69 70 71 72 73
            //如果是系统报表就取本地文件夹,如果不是就取FTP
            if (template.getIsSystemType()) {
                inputStream = new BufferedInputStream(new FileInputStream(templateFile));
            } else {
                inputStream = ftpClientPool.download(templatePath);
            }
74 75 76 77
            //客户端保存的文件名
            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");
78
            int len = 0;
79 80
            byte[] buffer = new byte[1024];
            OutputStream out = response.getOutputStream();
81 82
            while ((len = inputStream.read(buffer)) > 0) {
                out.write(buffer, 0, len);
83 84 85
            }

            //FileCopyUtils.copy(inputStream, response.getOutputStream());
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } 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;
            }
        }
103
    }
104

105 106 107 108
    @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) {
        return templateService.getTemplateUniqList(serviceTypeID, payTaxType, reportType, industryIDs);
109
    }
110

111 112 113
    @RequestMapping(value = "updateTemplateName", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto<Object> updateTemplateName(@RequestBody UpateNameParam param) {
114 115
        return templateService.updateTemplateName(param);
    }
116 117 118 119 120 121 122 123 124 125 126 127 128

    @RequestMapping(value = "deleteTemplate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto<String> deleteTemplate(@RequestBody DeleteTemplateParam param) {
        OperationResultDto<String> result = templateService.deleteTemplate(param);
        if (result.getResult() && StringUtils.isNotBlank(result.getData())) {
            try {
                ftpClientPool.delete(result.getData());
            } catch (Exception e) {
            }
        }
        return result;
    }
129 130 131 132 133 134

    @RequestMapping(value = "rowColName/{id}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto setRowColName(@PathVariable String id, @RequestBody List<CellBriefDto> cellInfo) {
        return templateService.setRowColName(id, cellInfo);
    }
135
}