TemplateGroupController.java 7.11 KB
package pwc.taxtech.atms.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
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.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 org.springframework.web.multipart.MultipartFile;
import pwc.taxtech.atms.common.ftp.FtpService;
import pwc.taxtech.atms.common.message.ErrorMessage;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.TemplateGroupDto;
import pwc.taxtech.atms.exception.ServiceException;
import pwc.taxtech.atms.service.impl.TemplateGroupServiceImpl;

import java.util.List;

@RestController
@RequestMapping("/api/v1/templateGroup/")
public class TemplateGroupController {
    private static final Logger logger = LoggerFactory.getLogger(TemplateGroupController.class);

    @Autowired
    TemplateGroupServiceImpl templateGroupService;

    @Autowired
    FtpService ftpService;

    @ApiOperation(value = "获取所有的模板分组")
    @RequestMapping(value = "getall", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    List<TemplateGroupDto> get() {
        logger.debug("TemplateGroupController Get");
        return templateGroupService.get();
    }

    @ApiOperation(value = "根据服务类型和行业获取模板分组")
    @RequestMapping(value = "getByIndustry/{serviceTypeId}/{industryId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    List<TemplateGroupDto> getTemplateGroupByIndustry(@PathVariable int serviceTypeId, @RequestParam Integer taxPayType, @PathVariable String industryId) {
        return templateGroupService.get(serviceTypeId, taxPayType, industryId);
    }

    @RequestMapping(value = "updateTemplateGroupName", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto<Object> updateTemplateGroupName(@RequestBody TemplateGroupDto templateGroupDto) {
        return templateGroupService.updateTemplateGroupName(templateGroupDto);
    }

    @RequestMapping(value = "deleteTemplateGroup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto<Object> deleteTemplateGroup(@RequestBody TemplateGroupDto templateGroupDto) {
        OperationResultDto<Object> result = templateGroupService.deleteTemplateGroup(templateGroupDto);
        if (result.getResult()) {
            List<String> pathList = (List<String>) result.getData();
            if (pathList != null && pathList.size() > 0) {
                for (String path : pathList) {
                    try {
                        //ftpService.delete(path);
                    } catch (Exception e) {
                    }
                }
            }
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "获取Sheet名称")
    @RequestMapping(value = "getSheetNameList", method = RequestMethod.POST)
    public OperationResultDto getSheetNameList(@RequestParam MultipartFile file,
                                               @RequestParam String filename,
                                               @RequestParam String tempFileName) {
        try {
            if (null == file) {
                return OperationResultDto.error(ErrorMessage.NoFile);
            }
            return OperationResultDto.success(templateGroupService.getSheetNameList(file));
        } catch (Exception e) {
            logger.error("getSheetNameList error.", e);
        }
        return OperationResultDto.error();
    }

    @ResponseBody
    @ApiOperation(value = "导入模板")
    @RequestMapping(value = "importTemplateGroupExcelFile", method = RequestMethod.POST)
    public OperationResultDto importTemplateGroupExcelFile(@RequestParam MultipartFile file,
                                                           @RequestParam String filename,
                                                           @RequestParam String tempFileName,
                                                           @RequestParam String jsonModel) {
        try {
            if (null == file) {
                return OperationResultDto.error(ErrorMessage.NoFile);
            }
            TemplateGroupDto templateGroupDto = JSON.parseObject(jsonModel, TemplateGroupDto.class);
            if (CollectionUtils.isEmpty(templateGroupDto.getSheetNameList())) {
                return OperationResultDto.error(ErrorMessage.NoSelectSheet);
            }
            templateGroupService.importTemplateGroupExcelFile(file, templateGroupDto);
            return OperationResultDto.success();
        } catch (ServiceException e) {
            return OperationResultDto.error(e.getMessage());
        } catch (Exception e) {
            logger.error("importTemplateGroupExcelFile error.", e);
        }
        return OperationResultDto.error(ErrorMessage.SystemError);
    }

    @ResponseBody
    @ApiOperation(value = "导入报表")
    @RequestMapping(value = "importTemplateExcelFile", method = RequestMethod.POST)
    public OperationResultDto importTemplateExcelFile(@RequestParam MultipartFile file,
                                                      @RequestParam Long templateGroupId,
                                                      @RequestParam String sheetList,
                                                      @RequestParam String tempFileName,
                                                      @RequestParam String reportType,
                                                      @RequestParam String filename) {
        try {
            templateGroupService.importTemplateExcelFile(file, templateGroupId, reportType, sheetList);
            return OperationResultDto.success();
        } catch (ServiceException e) {
            return OperationResultDto.error(e.getMessage());
        } catch (Exception e) {
            logger.error("importTemplateExcelFile error.", e);
        }
        return OperationResultDto.error(ErrorMessage.SystemError);
    }

    @ResponseBody
    @ApiOperation(value = "模板另存为")
    @RequestMapping(value = "addTemplateGroup", method = RequestMethod.POST)
    public OperationResultDto addTemplateGroup(@RequestBody TemplateGroupDto templateGroupDto) {
        try {
            templateGroupService.addTemplateGroup(templateGroupDto);
            return OperationResultDto.success();
        } catch (ServiceException e) {
            return OperationResultDto.error(e.getMessage());
        } catch (Exception e) {
            logger.error("addTemplateGroup error.", e);
        }
        return OperationResultDto.error(ErrorMessage.SystemError);
    }

}