# HotelKpiInternalController.java

    package cloud.wconnect.workperformance.controller.hotelkpi.internal;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.controller.BaseInternalController;
    import cloud.wconnect.commonlib.exception.InternalServerErrorException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.explain.HotelKpiExplainDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.internal.crawl.value.v1.HotelKpiCrawlDailyDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.internal.crawl.value.v1.HotelKpiCrawlValueDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.internal.crawl.value.v2.HotelKpiV2CrawlValueDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.strdata.week.crawl.HotelKpiStrWeekCrawlDTO;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiCrawlValueService;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiExplainMonthlyService;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiStrWeekService;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiWeekStartDayService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.*;

    import java.util.Collections;
    import java.util.Map;

    /**
     * <p>
     * Hotel KPI Internal 前端控制器
     * </p>
     *
     * @author zc
     * @since 2024-06-29
     */
    @Slf4j
    @RestController
    @RequestMapping("/internal/hotel-kpi")
    public class HotelKpiInternalController extends BaseInternalController {

        @Resource
        private IHotelKpiCrawlValueService crawlValueService;
        @Resource
        private IHotelKpiStrWeekService strWeekService;
        @Resource
        private IHotelKpiExplainMonthlyService explainMonthlyService;
        @Resource
        private IHotelKpiWeekStartDayService weekStartDayService;
        @Resource
        private PropertyInternalService propertyInternalService;
        @Resource
        private ObjectMapper objectMapper;
        @Value("${internal.service.auth-key.basic}")
        private String authKey;

        /**
         * 保存采集数据
         *
         * @param tenant   租户标识
         * @param property 资产标识
         * @param saveDTO  采集数据DTO
         * @param sign     签名
         * @return empty map
         * @author zc
         * @date 2025/8/26
         */
        @PostMapping("/explain")
        public Map<Object, Object> saveExplainValue(@RequestParam String tenant, @RequestParam String property,
                                                    @RequestBody @Valid HotelKpiExplainDTO saveDTO,
                                                    @RequestHeader String sign) {
            try {
                log.info("HotelKpiInternal - Save Explain value start, tenant:{}, property:{}", tenant, property);

                // 校验签名
                checkIntranetParams(authKey, sign, tenant, property);

                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

                // 处理采集的 Explain
                explainMonthlyService.save(propertyDTO, saveDTO);

                log.info("HotelKpiInternal - Save Explain value end, tenant:{}, property:{}", tenant, property);
                return Collections.emptyMap();
            } catch (Exception e) {
                log.error("HotelKpiInternal - Save Explain value error, tenant:{}, property:{}, dataJson:{}, exception:{}",
                        tenant, property, JsonUtils.convertObjToString(objectMapper, saveDTO), e);

                throw new InternalServerErrorException(BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getCode(),
                        "Save Explain value error", e);
            }
        }

        /**
         * 保存采集数据
         *
         * @param tenant        租户标识
         * @param property      资产标识
         * @param crawlValueDTO 采集数据DTO
         * @param sign          签名
         * @return empty map
         * @author zc
         * @date 2024/6/30
         */
        @PostMapping("/value")
        public Map<Object, Object> saveCrawlValue(@RequestParam String tenant, @RequestParam String property,
                                                  @RequestBody @Valid HotelKpiCrawlValueDTO crawlValueDTO,
                                                  @RequestHeader String sign) {
            try {
                log.info("HotelKpiInternal - Save crawl value start, tenant:{}, property:{}", tenant, property);

                // 校验签名
                checkIntranetParams(authKey, sign, crawlValueDTO.getDateType(), tenant, property);

                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

                // 处理采集数据
                crawlValueService.handleCrawlValue(propertyDTO, crawlValueDTO);

                log.info("HotelKpiInternal - Save crawl value end, tenant:{}, property:{}", tenant, property);
                return Collections.emptyMap();
            } catch (Exception e) {
                log.error("HotelKpiInternal - Save crawl value error, tenant:{}, property:{}, dataJson:{}, exception:{}",
                        tenant, property, JsonUtils.convertObjToString(objectMapper, crawlValueDTO), e);

                throw new InternalServerErrorException(BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getCode(),
                        "Save crawl value error", e);
            }
        }

        /**
         * 保存采集数据
         *
         * @param tenant        租户标识
         * @param property      资产标识
         * @param crawlValueDTO 采集数据DTO
         * @param sign          签名
         * @return empty map
         * @author zc
         * @date 2025/8/25
         */
        @PostMapping("/value/v2")
        public Map<Object, Object> saveCrawlValueV2(@RequestParam String tenant, @RequestParam String property,
                                                    @RequestBody @Valid HotelKpiV2CrawlValueDTO crawlValueDTO,
                                                    @RequestHeader String sign) {
            try {
                log.info("HotelKpiInternal - Save crawl value v2 start, tenant:{}, property:{}", tenant, property);

                // 校验签名
                checkIntranetParams(authKey, sign, crawlValueDTO.getDateType(), tenant, property);

                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

                // 处理采集数据
                crawlValueService.handleCrawlValueV2(propertyDTO, crawlValueDTO);

                log.info("HotelKpiInternal - Save crawl value v2 end, tenant:{}, property:{}", tenant, property);
                return Collections.emptyMap();
            } catch (Exception e) {
                log.error("HotelKpiInternal - Save crawl value v2 error, tenant:{}, property:{}, dataJson:{}, exception:{}",
                        tenant, property, JsonUtils.convertObjToString(objectMapper, crawlValueDTO), e);

                throw new InternalServerErrorException(BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getCode(),
                        "Save crawl value v2 error", e);
            }
        }

        /**
         * 保存采集数据
         *
         * @param tenant        租户标识
         * @param property      资产标识
         * @param crawlValueDTO 采集数据DTO
         * @param sign          签名
         * @return empty map
         * @author zc
         * @date 2025/8/25
         */
        @PostMapping("/value/str-week")
        public Map<Object, Object> saveCrawlValueStrWeek(@RequestParam String tenant, @RequestParam String property,
                                                         @RequestBody @Valid HotelKpiStrWeekCrawlDTO crawlValueDTO,
                                                         @RequestHeader String sign) {
            try {
                log.info("HotelKpiInternal - Save crawl value Str Week start, tenant:{}, property:{}", tenant, property);

                // 校验签名
                checkIntranetParams(authKey, sign, tenant, property);

                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

                // 处理采集数据
                strWeekService.saveCrawl(propertyDTO, crawlValueDTO);

                log.info("HotelKpiInternal - Save crawl value Str Week end, tenant:{}, property:{}", tenant, property);
                return Collections.emptyMap();
            } catch (Exception e) {
                log.error("HotelKpiInternal - Save crawl value Str Week error, tenant:{}, property:{}, dataJson:{}, exception:{}",
                        tenant, property, JsonUtils.convertObjToString(objectMapper, crawlValueDTO), e);

                throw new InternalServerErrorException(BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getCode(),
                        "Save crawl value Str Week error", e);
            }
        }

        /**
         * 保存采集数据-每日
         *
         * @param tenant   租户标识
         * @param property 资产标识
         * @param dailyDTO 采集数据DTO
         * @param sign     签名
         * @return empty map
         * @author zc
         * @date 2025/3/21
         */
        @PostMapping("/value/daily")
        public Map<Object, Object> saveCrawlValueDaily(@RequestParam String tenant, @RequestParam String property,
                                                       @RequestBody @Valid HotelKpiCrawlDailyDTO dailyDTO,
                                                       @RequestHeader String sign) {
            try {
                log.info("HotelKpiInternal - Save crawl value daily start, tenant:{}, property:{}", tenant, property);

                // 校验签名
                checkIntranetParams(authKey, sign, dailyDTO.getMetricsType(), tenant, property);

                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

                // 处理采集数据
                crawlValueService.handleCrawlValueDaily(propertyDTO, dailyDTO);

                log.info("HotelKpiInternal - Save crawl value daily end, tenant:{}, property:{}", tenant, property);
                return Collections.emptyMap();
            } catch (Exception e) {
                log.error("HotelKpiInternal - Save crawl value daily error, tenant:{}, property:{}, dataJson:{}, exception:{}",
                        tenant, property, JsonUtils.convertObjToString(objectMapper, dailyDTO), e);

                throw new InternalServerErrorException(BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getCode(),
                        "Save crawl value daily error", e);
            }
        }

        /**
         * 获取周起始日
         *
         * @param tenant   租户标识
         * @param property 资产标识
         * @param sign     签名
         * @return 周起始日 1 ~ 7
         * @author zc
         * @date 2025/2/25
         */
        @GetMapping("/week-start-day")
        public Map<String, Integer> getWeekStartDay(@RequestParam String tenant, @RequestParam String property,
                                                    @RequestHeader String sign) {
            try {
                log.info("HotelKpiInternal - Get week start day start, tenant:{}, property:{}", tenant, property);

                // 校验签名
                checkIntranetParams(authKey, sign, tenant, property);

                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

                log.info("HotelKpiInternal - Get week start day end, tenant:{}, property:{}", tenant, property);
                return Collections.singletonMap("weekStartDay", weekStartDayService.getWeekStartDay(propertyDTO.getId()).getValue());
            } catch (Exception e) {
                log.error("HotelKpiInternal - Get week start day error, tenant:{}, property:{}, exception:{}", tenant, property, e);

                throw new InternalServerErrorException(BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getCode(),
                        "Get week start day error", e);
            }
        }
    }