# ArrivalFileParseByOther.java

    package cloud.wconnect.frontoffice.service.clc.arrival;

    import cloud.wconnect.commonlib.cons.*;
    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.*;
    import cloud.wconnect.frontoffice.cons.S3BucketFunctionCons;
    import cloud.wconnect.frontoffice.entity.clc.ClcMissingReport;
    import cloud.wconnect.frontoffice.pojo.bo.clc.ArrivalBO;
    import cloud.wconnect.frontoffice.pojo.bo.clc.DataBO;
    import cloud.wconnect.frontoffice.service.AmazonS3Service;
    import cloud.wconnect.frontoffice.service.clc.IClcArrivalDetailedFilterConfigService;
    import cloud.wconnect.frontoffice.service.clc.IClcArrivalDetailedService;
    import cloud.wconnect.frontoffice.service.clc.IClcHandleFileService;
    import cloud.wconnect.frontoffice.service.clc.IClcMissingReportService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.apache.poi.ss.usermodel.*;
    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.text.DecimalFormat;
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.regex.Pattern;

    import static java.util.stream.Collectors.toMap;

    /**
     * 解析 arrival 报告
     *
     * @author yyt
     * 2023/7/26
     */
    @Service
    @DS("logbook")
    public class ArrivalFileParseByOther implements IArrivalFileParse {

        @Resource
        private IClcArrivalDetailedFilterConfigService clcArrivalDetailedFilterConfigService;
        @Resource
        private IClcMissingReportService clcMissingReportService;
        @Resource
        private IClcArrivalDetailedService clcArrivalDetailedService;
        @Resource
        private IClcHandleFileService clcHandleFileService;
        @Autowired
        private AmazonS3Service amazonS3Service;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        @Value("${aws-s3.third-party.key.clc.opera-arrival}")
        private String operaArrivalS3key;

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

        private static final String SOURCE = SystemPlatformEnum.OTHER.getDesc();

        private static final Pattern WORD_PATTERN = Pattern.compile("[A-Za-z]+");

        /**
         * 判断支持的平台
         *
         * @param source 平台数据源
         * @return boolean
         * @author yyt
         * @date 2023/7/26
         */
        @Override
        public boolean supports(String source) {
            return SOURCE.equals(source);
        }

        /**
         * 解析报告数据
         *
         * @param propertyId 资产 id
         * @param date       日期
         * @param in         文件流
         * @param timeZone   时区
         * @return 入住信息
         * @author yyt
         * @date 2023/7/26
         */
        @Override
        public List<ArrivalBO> parsReport(Integer propertyId, Long date, InputStream in, String timeZone) throws IOException {
            // company 动态配置规则
            List<String> companyNameConfigs = clcArrivalDetailedFilterConfigService.listForCompanyName(propertyId);

            DecimalFormat decimalFormat = new DecimalFormat("0000");

            try(Workbook workbook = WorkbookFactory.create(in)){
                Sheet sheet = workbook.getSheetAt(0);
                List<ArrivalBO> result = Lists.newArrayListWithExpectedSize(sheet.getLastRowNum());
                for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                    Row row = sheet.getRow(i);
                    if (Objects.isNull(row)) {
                        continue;
                    }

                    // 检查公司名称是否符合要求
                    Cell companyCell = row.getCell(0);
                    if (!clcHandleFileService.checkClcCompany(CellUtils.convertCellStr(companyCell), companyNameConfigs)) {
                        continue;
                    }

                    Cell arrivalDateCell = row.getCell(3);
                    LocalDate startDate = DateTimeUtils.strToLocalDate(CellUtils.convertCellStr(arrivalDateCell, BaseConstant.DATE_FORMAT_YYYY_MM_DD),
                            BaseConstant.DATE_FORMAT_YYYY_MM_DD);
                    // 过滤数据
                    if (DateTimeUtils.localDateToLong(startDate, timeZone) != date) continue;

                    ArrivalBO arrivalBO = new ArrivalBO();
                    arrivalBO.setCompany(CellUtils.convertCellStr(companyCell));
                    arrivalBO.setArrivDate(CellUtils.convertCellStr(arrivalDateCell, BaseConstant.DATE_FORMAT_YYYY_MM_DD));

                    // 房间号不足4位数字时前面补0
                    Cell roomNumberCell = row.getCell(2);
                    arrivalBO.setRoomNumber(decimalFormat.format(Integer.valueOf(CellUtils.convertCellStr(roomNumberCell))));

                    // Departure Date
                    Cell departureDateCell = row.getCell(4);
                    arrivalBO.setDepartDate(CellUtils.convertCellStr(departureDateCell, BaseConstant.DATE_FORMAT_YYYY_MM_DD));

                    // Conf No.
                    Cell confNoCell = row.getCell(5);
                    arrivalBO.setConfNumber(CellUtils.convertCellStr(confNoCell));

                    // Guest Name
                    Cell nameCell = row.getCell(1);
                    arrivalBO.setGuestName(CellUtils.convertCellStr(nameCell));

                    // Room Type
                    Cell typeCell = row.getCell(6);
                    arrivalBO.setRoomType(CellUtils.convertCellStr(typeCell));

                    result.add(arrivalBO);
                }

                if (CollectionUtils.isNotEmpty(result)) {
                    // 将Arrivals数据按照 arrival date + room number 进行分组去重，当出现重复数据时只保留最新的记录
                    Map<String, ArrivalBO> arrivalByUniqKey = result.stream()
                            .collect(toMap(item -> item.getArrivDate()
                                    + "&" + item.getRoomNumber(), val -> val, (oldVal, newVal) -> newVal));
                    result = new ArrayList<>(arrivalByUniqKey.values());
                }

                return result;
            }catch (Exception e){
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parsing file failed.", e);
            }
        }

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

            try (InputStream in = file.getInputStream();
                 Workbook workbook = WorkbookFactory.create(in)) {
                Sheet sheet = workbook.getSheetAt(0);
                Row titleRow = sheet.getRow(0);
                if (Objects.isNull(titleRow)) {
                    throw new BadRequestException(CommonErrorI18nConstant.IMPORT_REPORT_NOT_REQUIRED,
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The imported report is not the one required for the current functionality");
                }

                String companyTitle = CellUtils.convertCellStr(titleRow.getCell(0));
                String nameTitle = CellUtils.convertCellStr(titleRow.getCell(1));
                String roomNumberTitle = CellUtils.convertCellStr(titleRow.getCell(2));
                String checkInDateTitle = CellUtils.convertCellStr(titleRow.getCell(3));
                String departureDateTitle = CellUtils.convertCellStr(titleRow.getCell(4));
                String confNoTitle = CellUtils.convertCellStr(titleRow.getCell(5));
                String roomTypeTitle = CellUtils.convertCellStr(titleRow.getCell(6));
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(BaseConstant.DATE_FORMAT_YYYY_MM_DD);

                for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                    Row row = sheet.getRow(i);
                    if (Objects.isNull(row)) {
                        continue;
                    }

                    Cell companyCell = row.getCell(0);
                    if (Objects.isNull(companyCell) || StringUtils.isBlank(companyCell.toString())) {
                        throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                                i18nMessageUtils.getErrorParamMapNotNeedTranslate(companyTitle),
                                BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                "The company cannot be empty");
                    } else {
                        // 最多 60 个字
                        if (CellUtils.convertCellStr(companyCell).length() > 60) {
                            throw new BadRequestException(CommonErrorI18nConstant.OVERLENGTH,
                                    i18nMessageUtils.getErrorParamMapNotNeedTranslate(companyTitle, 60),
                                    BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                    "The company cannot be more than 60 characters");
                        }
                    }

                    Cell nameCell = row.getCell(1);
                    if (Objects.isNull(nameCell) || StringUtils.isBlank(nameCell.toString())) {
                        throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                                i18nMessageUtils.getErrorParamMapNotNeedTranslate(nameTitle),
                                BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                "The name cannot be empty");
                    } else {
                        // 最多 60 个字
                        if (CellUtils.convertCellStr(nameCell).length() > 60) {
                            throw new BadRequestException(CommonErrorI18nConstant.OVERLENGTH,
                                    i18nMessageUtils.getErrorParamMapNotNeedTranslate(nameTitle, 60),
                                    BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                    "The name cannot be more than 60 characters");
                        }
                    }

                    Cell roomNumberCell = row.getCell(2);
                    if (Objects.isNull(roomNumberCell) || StringUtils.isBlank(roomNumberCell.toString())) {
                        throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                                i18nMessageUtils.getErrorParamMapNotNeedTranslate(roomNumberTitle),
                                BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                "The room number cannot be empty");
                    } else {
                        // 最多 4 个字
                        if (CellUtils.convertCellStr(roomNumberCell).length() > 4) {
                            throw new BadRequestException(CommonErrorI18nConstant.OVERLENGTH,
                                    i18nMessageUtils.getErrorParamMapNotNeedTranslate(roomNumberTitle, 4),
                                    BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                    "The room number cannot be more than 4 characters");
                        }
                    }

                    Cell checkInDateCell = row.getCell(3);
                    if (Objects.isNull(checkInDateCell) || StringUtils.isBlank(checkInDateCell.toString())) {
                        throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                                i18nMessageUtils.getErrorParamMapNotNeedTranslate(checkInDateTitle),
                                BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                "The check-in date cannot be empty");
                    } else {
                        // 格式必须 yyyy-MM-dd
                        try {
                            LocalDate.parse(CellUtils.convertCellStr(checkInDateCell, BaseConstant.DATE_FORMAT_YYYY_MM_DD), dateTimeFormatter);
                        } catch (Exception e) {
                            throw new BadRequestException(CommonErrorI18nConstant.FORMAT_INCORRECT,
                                    i18nMessageUtils.getErrorParamMapNotNeedTranslate(checkInDateTitle, BaseConstant.DATE_FORMAT_YYYY_MM_DD),
                                    BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                    "The check-in date format error");
                        }
                    }

                    Cell departureDateCell = row.getCell(4);
                    if (Objects.isNull(departureDateCell) || StringUtils.isBlank(departureDateCell.toString())) {
                        throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                                i18nMessageUtils.getErrorParamMapNotNeedTranslate(departureDateTitle),
                                BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                "The departure date cannot be empty");
                    } else {
                        // 格式必须 yyyy-MM-dd
                        try {
                            LocalDate.parse(CellUtils.convertCellStr(departureDateCell, BaseConstant.DATE_FORMAT_YYYY_MM_DD), dateTimeFormatter);
                        } catch (Exception e) {
                            throw new BadRequestException(CommonErrorI18nConstant.FORMAT_INCORRECT,
                                    i18nMessageUtils.getErrorParamMapNotNeedTranslate(departureDateTitle, BaseConstant.DATE_FORMAT_YYYY_MM_DD),
                                    BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                    "The departure date format error");
                        }
                    }

                    // 入住日期不能晚于退房日期
                    LocalDate checkInDate = LocalDate.parse(CellUtils.convertCellStr(checkInDateCell, BaseConstant.DATE_FORMAT_YYYY_MM_DD), dateTimeFormatter);
                    LocalDate departureDate = LocalDate.parse(CellUtils.convertCellStr(departureDateCell, BaseConstant.DATE_FORMAT_YYYY_MM_DD), dateTimeFormatter);
                    if (checkInDate.isAfter(departureDate)) {
                        throw new BadRequestException(CommonErrorI18nConstant.CHECK_IN_DATE_CANNOT_GREATER_THAN_CHECK_OUT_DATE,
                                BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                "The check-in date cannot be later than check-out date");
                    }

                    Cell confNoCell = row.getCell(5);
                    if (Objects.isNull(confNoCell) || StringUtils.isBlank(confNoCell.toString())) {
                        throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                                i18nMessageUtils.getErrorParamMapNotNeedTranslate(confNoTitle),
                                BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                "The conf no cannot be empty");
                    } else {
                        // 最多 10 个字
                        if (CellUtils.convertCellStr(confNoCell).length() > 10) {
                            throw new BadRequestException(CommonErrorI18nConstant.OVERLENGTH,
                                    i18nMessageUtils.getErrorParamMapNotNeedTranslate(confNoTitle, 10),
                                    BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                    "The conf no cannot be more than 10 characters");
                        }
                    }

                    Cell typeCell = row.getCell(6);
                    // 类型不能为空
                    if (Objects.isNull(typeCell) || StringUtils.isBlank(typeCell.toString())) {
                        throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                                i18nMessageUtils.getErrorParamMapNotNeedTranslate(roomTypeTitle),
                                BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                "The room type cannot be empty");
                    } else {
                        // 最多 10 个字
                        if (CellUtils.convertCellStr(typeCell).length() > 10) {
                            throw new BadRequestException(CommonErrorI18nConstant.OVERLENGTH,
                                    i18nMessageUtils.getErrorParamMapNotNeedTranslate(roomTypeTitle, 10),
                                    BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                    "The room type cannot be more than 10 characters");
                        }
                    }
                }

            } catch (BadRequestException e) {
                throw e;
            } catch (Exception e) {
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parsing file failed.", e);
            }
        }

        /**
         * 处理导入记录
         *
         * @param tenantBO    租户
         * @param propertyDTO 资产
         * @param importLog   导入记录
         * @param theDate     指定日期
         * @param file        文件
         * @param userId      操作人
         * @return 导入记录
         * @author yyt
         * @date 2024/8/2
         **/
        @Override
        public ClcMissingReport handleImportLog(SymbolBO tenantBO, PropertyDTO propertyDTO, ClcMissingReport importLog,
                                                Long theDate, MultipartFile file, int userId) {
            String key = toS3(tenantBO.getSymbol(), propertyDTO.getAbbr(), file);
            if (Objects.isNull(importLog)) {
                importLog = clcMissingReportService.saveInProgressRecord(propertyDTO.getId(), theDate, key, userId);
            } else {
                // 更新异常为修复中
                ClcMissingReport updateContent = new ClcMissingReport();
                updateContent.setId(importLog.getId());
                updateContent.setStatus(ReportImportResultCons.IMPORT_RESULT_REPAIRING);
                updateContent.setUrl(key);
                clcMissingReportService.updateById(updateContent);
                importLog.setStatus(ReportImportResultCons.IMPORT_RESULT_REPAIRING);
            }
            return importLog;
        }

        /**
         * 上传文件至 s3
         *
         * @param tenantSymbol   租户标识
         * @param propertySymbol 资产标识
         * @param file           文件
         * @return 文件路径
         * @author yyt
         * @date 2024/8/2
         **/
        private String toS3(String tenantSymbol, String propertySymbol, MultipartFile file) {
            String key = tenantSymbol + "/" + propertySymbol + "/" + operaArrivalS3key +
                    UploadUtils.getRandomFileName(file.getOriginalFilename());
            try {
                // 上传文件
                amazonS3Service.uploadFile(S3BucketFunctionCons.CLC_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 file        文件
         * @param importLog   导入记录
         * @param userId      操作人
         * @author yyt
         * @date 2024/8/1
         **/
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void handleFile(SymbolBO tenantBO, PropertyDTO propertyDTO, MultipartFile file, ClcMissingReport importLog, Integer userId) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(BaseConstant.DATE_FORMAT_YYYY_MM_DD);
            boolean importSuccess = true;
            try (InputStream inputStream = file.getInputStream()) {
                List<ArrivalBO> arrivalBOList = parsReport(propertyDTO.getId(), importLog.getTheDate(), inputStream,
                        propertyDTO.getTimezone());
                if (CollectionUtils.isNotEmpty(arrivalBOList)) {
                    DataBO<ArrivalBO> dataBO = new DataBO<>();
                    dataBO.setDate(dateTimeFormatter.format(DateTimeUtils.toZonedDateTime(importLog.getTheDate(),
                            propertyDTO.getTimezone())));
                    dataBO.setData(arrivalBOList);
                    clcArrivalDetailedService.handleArrivalData(propertyDTO, userId, dataBO);
                }
            } catch (Exception e) {
                importSuccess = false;
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parse arrival report error", e);
            } finally {
                if(importSuccess){
                    importSuccess(importLog, userId);
                } else {
                    importFail(importLog);
                }
            }
        }

        /**
         * 导入成功
         *
         * @param importLog 导入记录
         * @param userId    操作人
         * @author yyt
         * @date 2024/8/2
         **/
        private void importSuccess(ClcMissingReport importLog, int userId) {
            TransactionUtils.afterCommitSyncExecute(() -> {
                // 更新导入记录
                ClcMissingReport updateLog = new ClcMissingReport();
                updateLog.setId(importLog.getId());
                updateLog.setStatus(ReportImportResultCons.IMPORT_RESULT_SUCCESS);
                if (importLog.getStatus() == ReportImportResultCons.IMPORT_RESULT_REPAIRING) {
                    updateLog.setResolvedBy(userId).setResolvedTime(DateTimeUtils.nowTimestamp());
                }
                clcMissingReportService.updateById(updateLog);
            });
        }

        /**
         * 导入失败
         *
         * @param importLog 导入记录
         * @author yyt
         * @date 2024/8/2
         **/
        private void importFail(ClcMissingReport importLog) {
            TransactionUtils.afterCompletionRollbackSyncExecute(() -> {
                // 更新导入记录
                ClcMissingReport updateLog = new ClcMissingReport();
                updateLog.setId(importLog.getId());
                updateLog.setStatus(ReportImportResultCons.IMPORT_RESULT_FAIL);
                clcMissingReportService.updateById(updateLog);
            });
        }

    }