# ImportCaseByExcel.java

    package cloud.wconnect.finance.service.chargeback.importcase;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.exception.ParseReportException;
    import cloud.wconnect.commonlib.pojo.bo.SymbolBO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.FileCheckUtils;
    import cloud.wconnect.commonlib.util.UploadUtils;
    import cloud.wconnect.finance.cons.S3BucketFunctionCons;
    import cloud.wconnect.finance.entity.chargeback.CbCaseImportLog;
    import cloud.wconnect.finance.pojo.bo.chargeback.CaseListBO;
    import cloud.wconnect.finance.pojo.dto.chargeback.ImportCaseDTO;
    import cloud.wconnect.finance.service.AmazonS3Service;
    import cloud.wconnect.finance.service.chargeback.ICbCaseService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.web.multipart.MultipartFile;

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;

    /**
     * 导入 case
     *
     * @author yyt
     * 2023/9/8
     */
    @Service
    @DS("finance")
    public class ImportCaseByExcel implements IImportCase {

        @Resource
        private ICbCaseService cbCaseService;
        @Resource
        private IParseCaseCommonService parseCaseCommonService;
        @Autowired
        private AmazonS3Service amazonS3Service;

        @Value("${aws-s3.third-party.key.chargeback.case}")
        private String s3keyCase;

        @Value("${file-max-size.import-report.chargeback}")
        private Long importReportFileMaxSize;

        private static final int IMPORT_EXCEL = 2;

        /**
         * 判断支持的平台
         *
         * @param type 导入方式
         * @return boolean
         * @author yyt
         * @date 2024/8/9
         */
        @Override
        public boolean supports(Integer type) {
            return type == IMPORT_EXCEL;
        }

        /**
         * 解析报告数据
         *
         * @param propertyDTO 资产
         * @param in          文件流
          * @return journal 数据
         * @author yyt
         * @date 2024/8/9
         */
        @Override
        public List<CaseListBO> parsReport(PropertyDTO propertyDTO, InputStream in) throws IOException {
            return parseCaseCommonService.parseReport(propertyDTO, in);
        }

        /**
         * 检查文件
         *
         * @param propertyDTO 资产
         * @param file        文件
         * @return ImportCaseDTO
         * @author yyt
         * @date 2024/8/9
         **/
        @Override
        public ImportCaseDTO checkFile(PropertyDTO propertyDTO, MultipartFile file) {
            // 限制文件大小
            FileCheckUtils.checkFileSize(file, importReportFileMaxSize);
            // 校验文件格式
            UploadUtils.checkFileFormat(file, Lists.newArrayList("xls", "xlsx"));

            try (InputStream inputStream = file.getInputStream()) {
                parseCaseCommonService.checkFile(propertyDTO, inputStream);
            } catch (BadRequestException e) {
                throw e;
            } catch (Exception e) {
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parsing file failed.", e);
            }

            return new ImportCaseDTO(toS3(propertyDTO.getTenantSymbol(), propertyDTO.getAbbr(), file), null);
        }

        /**
         * 上传文件至 s3
         *
         * @param tenantSymbol   租户标识
         * @param propertySymbol 资产标识
         * @param file           文件
         * @return 文件路径
         * @author yyt
         * @date 2024/8/9
         **/
        private String toS3(String tenantSymbol, String propertySymbol, MultipartFile file) {
            String key = tenantSymbol + "/" + propertySymbol + "/" + s3keyCase +
                    UploadUtils.getRandomFileName(file.getOriginalFilename());
            try {
                // 上传文件
                amazonS3Service.uploadFile(S3BucketFunctionCons.CHARGEBACK_IMPORT_RECORD, file, key);
            } catch (Exception e) {
                throw new BadRequestException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "Uploading to s3 failure", e);
            }
            return key;
        }

        /**
         * 处理导入记录
         *
         * @param tenantBO    租户
         * @param propertyDTO 资产
         * @param fileName    文件名称
         * @param key         文件路径
         * @param userId      操作人
         * @return 导入记录
         * @author yyt
         * @date 2024/8/9
         **/
        @Override
        public CbCaseImportLog handleImportLog(SymbolBO tenantBO, PropertyDTO propertyDTO, String fileName, String key, int userId) {
            return null;
        }

        /**
         * 处理文件
         *
         * @param importDTO importDTO
         * @author yyt
         * @date 2024/8/9
         **/
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void handleFile(ImportCaseDTO importDTO) {
            PropertyDTO propertyDTO = importDTO.getPropertyDTO();
            try (InputStream inputStream = amazonS3Service.getFile(S3BucketFunctionCons.CHARGEBACK_IMPORT_RECORD, importDTO.getKey())) {
                List<CaseListBO> cases = parsReport(propertyDTO, inputStream);
                if (!cases.isEmpty()) {
                    cbCaseService.handleCaseData(propertyDTO, importDTO.getUserId(), cases);
                }
            } catch (Exception e) {
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parse case report error", e);
            }
        }

    }