# CompRoomImportReportController.java

    package cloud.wconnect.frontoffice.controller.comproom;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.cons.SystemPlatformEnum;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.FileCheckUtils;
    import cloud.wconnect.frontoffice.entity.comproom.CompRoomReportLog;
    import cloud.wconnect.frontoffice.parsereport.comproom.IParseGuestsInHouseComplimentary;
    import cloud.wconnect.frontoffice.pojo.dto.comproom.CompRoomImportReportDTO;
    import cloud.wconnect.frontoffice.service.comproom.ICompRoomReportLogService;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.ApplicationContext;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;

    import jakarta.validation.Valid;

    import java.util.*;

    /**
     * 免费房报告处理 Controller
     *
     * @author lichen
     * @date 2024/3/13
     */
    @RestController
    @RequestMapping("/comp-room/import-report")
    public class CompRoomImportReportController {

        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private ICompRoomReportLogService compRoomReportLogService;
        @Value("${file-max-size.import-report.comp-room}")
        private long importReportFileMaxSize;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private ApplicationContext applicationContext;

        /**
         * 查询报告处理日期
         *
         * @param property 资产标识
         * @return java.util.List<cloud.wconnect.frontoffice.pojo.vo.comproom.CompRoomReportLogVO>
         * @author lichen
         * @date 2024/3/13
         */
        @ApiId(538)
        @GetMapping
        public List<CompRoomReportLog> getImportReport(String property) {
            int propertyId = sessionContext.getPropertyId(property);
            //查询报告导入log
            List<CompRoomReportLog> compRoomReportLogs = compRoomReportLogService.listByPropertyId(propertyId);
            if (CollectionUtils.isNotEmpty(compRoomReportLogs)) return compRoomReportLogs;
            return Lists.newArrayList(new CompRoomReportLog()
                    .setReportType(CompRoomReportLog.REPORT_TYPE_GUESTS_IN_HOUSE_COMPLIMENTARY)
                    .setProcessTime(0L));
        }

        /**
         * 导入报告
         *
         * @param property        资产
         * @param importReportDTO 导入DTO
         * @return java.util.Map
         * @author lichen
         * @date 2024/3/14
         */
        @ApiId(539)
        @PostMapping
        public Map importReport(String property, @Valid CompRoomImportReportDTO importReportDTO) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            int loginUserId = sessionContext.getLoginUserId();

            //校验source是否符合预期
            String pmsType = propertyDTO.getPmsType();
            //校验source是否符合预期
            if (!Lists.newArrayList(pmsType, SystemPlatformEnum.OTHER.getDesc()).contains(importReportDTO.getSource())) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_INTEGRATION_SOURCE_SYSTEM_NOT_SUPPORTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The source must be in:" + pmsType + "|" + SystemPlatformEnum.OTHER.getDesc());
            }

            MultipartFile file = importReportDTO.getFile();
            //校验文件大小
            FileCheckUtils.checkFileSize(file, importReportFileMaxSize);

            Collection<IParseGuestsInHouseComplimentary> values = applicationContext.getBeansOfType(IParseGuestsInHouseComplimentary.class).values();
            for (IParseGuestsInHouseComplimentary parser : values) {
                if(parser.supports(importReportDTO.getSource())){
                    //校验
                    parser.checkFile(file);
                    //处理数据
                    parser.handleData(propertyDTO, file, loginUserId);
                    break;
                }
            }

            return Collections.emptyMap();
        }

    }