# ClcInternalController.java

    package cloud.wconnect.frontoffice.controller.clc.internal;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.controller.BaseInternalController;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.frontoffice.pojo.bo.clc.CardDataBO;
    import cloud.wconnect.frontoffice.pojo.bo.clc.CrawLogBO;
    import cloud.wconnect.frontoffice.pojo.bo.clc.CrewFaxBO;
    import cloud.wconnect.frontoffice.pojo.bo.clc.SiteDataBO;
    import cloud.wconnect.frontoffice.pojo.group.General;
    import cloud.wconnect.frontoffice.service.clc.IClcCardDataService;
    import cloud.wconnect.frontoffice.service.clc.IClcMissingReportService;
    import cloud.wconnect.frontoffice.service.clc.IClcSiteCrewFaxService;
    import cloud.wconnect.frontoffice.service.clc.IClcSiteDataService;
    import com.google.common.collect.ImmutableMap;
    import jakarta.validation.Valid;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.validation.annotation.Validated;
    import org.springframework.web.bind.annotation.*;

    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;

    /**
     * 控制层: Clc
     *
     * @author yyt
     * 2023/9/5
     */
    @Validated
    @RestController
    @RequestMapping("/internal/clc")
    public class ClcInternalController extends BaseInternalController {

        @Autowired
        private PropertyInternalService propertyInternalService;
        @Autowired
        private IClcSiteDataService clcSiteDataService;
        @Autowired
        private IClcCardDataService clcCardDataService;
        @Autowired
        private IClcMissingReportService clcMissingReportService;
        @Autowired
        private IClcSiteCrewFaxService clcSiteCrewFaxService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        @Value("${internal.service.auth-key.basic}")
        private String authKey;

        /**
         * 保存未退房 site data 数据
         *
         * @param tenant    租户标识
         * @param property  资产标识
         * @param siteDatas site data 数据
         * @param sign      签名
         * @return {}
         * @author yyt
         * @date 2023/9/5
         */
        @PostMapping("/site-data/not-checked-out")
        public Map saveNotCheckOutSiteData(@RequestParam String tenant, @RequestParam String property,
                                @RequestBody List<@Valid SiteDataBO> siteDatas, @RequestHeader String sign) {
            if (CollectionUtils.isEmpty(siteDatas)) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("siteDatas"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the siteDatas must not be empty");
            }

            checkIntranetParams(authKey, sign, tenant, property);

            // 处理数据
            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
            clcSiteDataService.handleNotCheckOutSiteData(propertyDTO, siteDatas);

            return Collections.emptyMap();
        }

        /**
         * 保存已退房 site data 数据
         *
         * @param tenant    租户标识
         * @param property  资产标识
         * @param siteDatas site data 数据
         * @param sign      签名
         * @return {}
         * @author yyt
         * @date 2023/9/5
         */
        @PostMapping("/site-data/checked-out")
        public Map saveCheckOutSiteData(@RequestParam String tenant, @RequestParam String property,
                                @RequestBody List<@Valid SiteDataBO> siteDatas, @RequestHeader String sign) {
            if (CollectionUtils.isEmpty(siteDatas)) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("siteDatas"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the siteDatas must not be empty");
            }

            checkIntranetParams(authKey, sign, tenant, property);

            // 处理数据
            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
            clcSiteDataService.handleSiteData(propertyDTO, siteDatas, true);

            return Collections.emptyMap();
        }

        /**
         * 保存采集 card data 数据
         *
         * @param tenant    租户标识
         * @param property  资产标识
         * @param cardDatas card data 数据
         * @param sign      签名
         * @return {}
         * @author yyt
         * @date 2023/9/5
         */
        @PostMapping("/card-data")
        public Map saveCardData(@RequestParam String tenant, @RequestParam String property,
                                @RequestBody List<@Valid CardDataBO> cardDatas, @RequestHeader String sign) {
            if (CollectionUtils.isEmpty(cardDatas)) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("cardDatas"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the cardDatas must not be empty");
            }

            checkIntranetParams(authKey, sign, tenant, property);

            // 处理数据
            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
            clcCardDataService.handleCardData(propertyDTO, cardDatas);

            return Collections.emptyMap();
        }

        /**
         * 查询已存在的 crew fax
         *
         * @param tenant    租户标识
         * @param property  资产标识
         * @param faxNumbers Fax Number
         * @param sign      签名
         * @return 已存在的 crew fax
         * @author yyt
         * @date 2023/11/4
         */
        @GetMapping("/crew-fax/existed")
        public Map<String, List<String>> listCrewFax(@RequestParam String tenant, @RequestParam String property,
                               @RequestParam String faxNumbers, @RequestHeader String sign) {
            if (StringUtils.isBlank(faxNumbers)) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("faxNumbers"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the faxNumbers must not be empty");
            }

            checkIntranetParams(authKey, sign, tenant, property, faxNumbers);

            // 处理数据
            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

            return ImmutableMap.of("existedFaxNumbers", clcSiteCrewFaxService.listExistedFaxNumbers(propertyDTO.getId(), Arrays.stream(faxNumbers.split(",")).toList()));
        }

        /**
         * 保存采集 crew fax 数据
         *
         * @param tenant    租户标识
         * @param property  资产标识
         * @param crewFaxes crew fax 数据
         * @param sign      签名
         * @return {}
         * @author yyt
         * @date 2023/9/5
         */
        @PostMapping("/crew-fax")
        public Map saveCrewFax(@RequestParam String tenant, @RequestParam String property,
                               @RequestBody List<@Valid CrewFaxBO> crewFaxes, @RequestHeader String sign) {
            if(CollectionUtils.isEmpty(crewFaxes)){
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("crewFaxes"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the crewFaxes must not be empty");
            }

            checkIntranetParams(authKey, sign, tenant, property);

            // 处理数据
            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
            clcSiteCrewFaxService.handleCrewFax(propertyDTO, crewFaxes);

            return Collections.emptyMap();
        }

        /**
         * 保存采集日志
         *
         * @param tenant    租户标识
         * @param property  资产标识
         * @param crawLogBO 采集日志
         * @param sign      签名
         * @return {}
         * @author yyt
         * @date 2023/9/5
         */
        @PostMapping("/crawl-log")
        public Map saveCrawlLog(@RequestParam String tenant, @RequestParam String property,
                                @Validated(General.class) @RequestBody CrawLogBO crawLogBO, @RequestHeader String sign) {
            checkIntranetParams(authKey, sign, tenant, property);

            // 保存日志
            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
            clcMissingReportService.saveLog(propertyDTO, crawLogBO);

            return Collections.emptyMap();
        }

    }