# ClcHandleFileServiceImpl.java

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

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.EventBodyParamEnum;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.bo.SymbolBO;
    import cloud.wconnect.commonlib.pojo.dto.EventBodyDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.commonlib.util.UploadUtils;
    import cloud.wconnect.frontoffice.cons.BusinessEnum;
    import cloud.wconnect.frontoffice.cons.EventSubjectEnum;
    import cloud.wconnect.frontoffice.entity.clc.ClcGuestInhImportLog;
    import cloud.wconnect.frontoffice.entity.clc.ClcMissingReport;
    import cloud.wconnect.frontoffice.service.AmazonS3Service;
    import cloud.wconnect.frontoffice.service.clc.IClcHandleFileService;
    import cloud.wconnect.sns.http.processor.Sender;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Maps;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import org.springframework.web.multipart.MultipartFile;

    import java.util.List;
    import java.util.Map;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    /**
     *
     * @author: yyt
     * 2026-01-29
     */
    @DS("logbook")
    @Service
    public class ClcHandleFileServiceImpl implements IClcHandleFileService {

        private final AmazonS3Service amazonS3Service;
        private final ObjectMapper objectMapper;
        private final Sender snsSender;
        public ClcHandleFileServiceImpl(AmazonS3Service amazonS3Service, ObjectMapper objectMapper, @Qualifier("3pSender") Sender snsSender) {
            this.amazonS3Service = amazonS3Service;
            this.objectMapper = objectMapper;
            this.snsSender = snsSender;
        }

        @Value("${aws-s3.third-party.key.clc.guest-in-house}")
        private String guestInHouseS3key;
        @Value("${aws-s3.third-party.key.clc.opera-arrival}")
        private String arrivalS3key;

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

        /**
         * 发送 guest in house event
         *
         * @param tenantBO    租户信息
         * @param propertyDTO 资产信息
         * @param file        文件
         * @param pms         pms 类型
         * @param importLog   导入记录
         * @param userId      操作人
         * @author yyt
         * @date 2026/1/29
         **/
        @Override
        public void sendGuestInHouseEvent(SymbolBO tenantBO, PropertyDTO propertyDTO, MultipartFile file, String pms, ClcGuestInhImportLog importLog, Integer userId) {
            try {
                // 设置事件体
                Map<String, Object> params = Maps.newHashMapWithExpectedSize(6);
                params.put(EventBodyParamEnum.TENANT_SYMBOL.getDesc(), tenantBO.getSymbol());
                params.put(EventBodyParamEnum.PROPERTY_ABBR.getDesc(), Lists.newArrayList(propertyDTO.getAbbr()));
                params.put(EventBodyParamEnum.SOURCE.getDesc(), pms);
                params.put(EventBodyParamEnum.UID.getDesc(), userId);
                params.put(EventBodyParamEnum.ID.getDesc(), importLog.getId());
                params.put(EventBodyParamEnum.ORIGINATE_FROM.getDesc(), BusinessEnum.CLC_GUEST_IN_HOUSE.getBusiness());

                // 上传文件
                String key = tenantBO.getSymbol() + "/" + propertyDTO.getAbbr() + "/" + guestInHouseS3key +
                        UploadUtils.getRandomFileName(file.getOriginalFilename());
                amazonS3Service.uploadFile(file, key);

                snsSender.publish(EventSubjectEnum.GUEST_IN_HOUSE_SERVICE.getSubject(),
                        JsonUtils.convertObjToString(objectMapper, new EventBodyDTO(key, params)));
            } catch (Exception e) {
                throw new BadRequestException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "import guest in house report exception", e);
            }
        }

        /**
         * 发送 arrival event
         *
         * @param tenantBO    租户信息
         * @param propertyDTO 资产信息
         * @param file        文件
         * @param pms         pms 类型
         * @param importLog   导入记录
         * @param userId      操作人
         * @author yyt
         * @date 2026/1/29
         **/
        @Override
        public void sendArrivalEvent(SymbolBO tenantBO, PropertyDTO propertyDTO, MultipartFile file, String pms, ClcMissingReport importLog, Integer userId) {
            try {
                // 设置事件体
                Map<String, Object> params = Maps.newHashMapWithExpectedSize(7);
                params.put(EventBodyParamEnum.TENANT_SYMBOL.getDesc(), tenantBO.getSymbol());
                params.put(EventBodyParamEnum.PROPERTY_ABBR.getDesc(), Lists.newArrayList(propertyDTO.getAbbr()));
                params.put(EventBodyParamEnum.SOURCE.getDesc(), pms);
                params.put(EventBodyParamEnum.UID.getDesc(), userId);
                params.put(EventBodyParamEnum.ID.getDesc(), importLog.getId());
                params.put(EventBodyParamEnum.DATE.getDesc(), importLog.getTheDate());
                params.put(EventBodyParamEnum.ORIGINATE_FROM.getDesc(), BusinessEnum.CLC_ARRIVAL.getBusiness());

                // 上传文件
                String key = tenantBO.getSymbol() + "/" + propertyDTO.getAbbr() + "/" + arrivalS3key +
                        UploadUtils.getRandomFileName(file.getOriginalFilename());
                amazonS3Service.uploadFile(file, key);

                snsSender.publish(EventSubjectEnum.ARRIVAL_YESTERDAY.getSubject(),
                        JsonUtils.convertObjToString(objectMapper, new EventBodyDTO(key, params)));
            } catch (Exception e) {
                throw new BadRequestException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "Failed to send [Arrivals Report Detailed] report event", e);
            }
        }

        /**
         * 校验公司名称
         *
         * @param company            公司名称
         * @param companyNameConfigs 公司名称配置
         * @return boolean
         * @author yyt
         * @date 2026/1/29
         **/
        @Override
        public boolean checkClcCompany(String company, List<String> companyNameConfigs) {
            if (StringUtils.isBlank(company)) {
                return false;
            }

            /**
             * 优先判断是否符合 动态配置规则
             * 然后再根据 正则判断
             */
            String lowerCaseSrcStr = StringUtils.lowerCase(company);
            if (CollectionUtils.isNotEmpty(companyNameConfigs) && companyNameConfigs.stream().map(String::toLowerCase)
                    .anyMatch(item -> item.equals(lowerCaseSrcStr))) {
                return true;
            }

            Matcher matcher = WORD_PATTERN.matcher(lowerCaseSrcStr);
            String targetWord = "clc";
            while (matcher.find()) {
                String targetStr = matcher.group();
                if (StringUtils.equals(targetWord, targetStr)) {
                    return true;
                }
            }

            return false;
        }

        /**
         * 校验 guest name 符合规则
         *
         * @param guestName guest name
         * @return boolean
         * @author yyt
         * @date 2026/1/29
         **/
        @Override
        public boolean isGuestName(String guestName) {
            if (StringUtils.isBlank(guestName)) return false;

            //正则表达式
            //1.可以包含字母、点、逗号、空格 2.必须是字母开头
            String regex = "^[a-zA-Z ][ ,.a-zA-Z]*$";

            return guestName.matches(regex);
        }

    }