# HotelKpiAnalysisController.java

    package cloud.wconnect.workperformance.controller.hotelkpi;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.workperformance.cons.hotelkpi.HotelKpiDateTypeCons;
    import cloud.wconnect.workperformance.entity.hotelkpi.HotelKpiAnalysisComment;
    import cloud.wconnect.workperformance.entity.hotelkpi.HotelKpiMetricsConfig;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.analysis.HotelKpiAnalysisPackageDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.kpi.HotelKpiCommentDTO;
    import cloud.wconnect.workperformance.pojo.query.hotelkpi.HotelKpiAnalysisQuery;
    import cloud.wconnect.workperformance.pojo.query.hotelkpi.HotelKpiCommentQuery;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiAnalysisCommentService;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiAnalysisService;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiMetricsConfigService;
    import com.google.common.collect.ImmutableMap;
    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;
    import org.springframework.web.bind.annotation.*;

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

    /**
     * <p>
     * Hotel KPI Analysis 前端控制器
     * </p>
     *
     * @author zc
     * @since 2024-06-27
     */
    @RestController
    @RequestMapping("/hotel-kpi")
    public class HotelKpiAnalysisController {

        @Resource
        private IHotelKpiMetricsConfigService metricsConfigService;
        @Resource
        private IHotelKpiAnalysisService analysisService;
        @Resource
        private IHotelKpiAnalysisCommentService analysisCommentService;
        @Resource
        private PropertyI18nRedisService propertyI18nRedisService;
        @Resource
        private SessionContext sessionContext;

        /**
         * 获取指标对比分析数据
         *
         * @param property 资产标识
         * @param query    查询条件
         * @return HotelKpiAnalysisDTO 集合
         * @author zc
         * @date 2024/6/27
         */
        @ApiId(1211)
        @GetMapping("/analysis")
        public HotelKpiAnalysisPackageDTO get(@RequestParam String property, @Valid HotelKpiAnalysisQuery query) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);

            // 校验指标配置是否存在
            HotelKpiMetricsConfig metricsConfig = metricsConfigService.checkExistByDateAndMetricsType(propertyDTO.getId(),
                    HotelKpiDateTypeCons.YEARLY, query.getTargetYear(), query.getMetricsType(), Boolean.FALSE, null);

            return analysisService.getAnalysis(propertyDTO, query, metricsConfig);
        }

        /**
         * 获取指标对比分析评论
         *
         * @param property 资产标识
         * @param year     年份(yyyy)
         * @param query    查询条件
         * @return HotelKpiCommentDTO 集合
         * @author zc
         * @date 2024/6/28
         */
        @ApiId(1212)
        @GetMapping("/{year:\\d+}/analysis/comment")
        public ListPagedDTO<HotelKpiCommentDTO> listComment(@RequestParam String property, @PathVariable long year,
                                                            @Valid HotelKpiCommentQuery query) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            return analysisCommentService.listComment(propertyDTO, year, query);
        }

        /**
         * 保存酒店KPI评论
         *
         * @param property   资产标识
         * @param year       年份(yyyy)
         * @param commentDTO 评论数据DTO
         * @return 评论 id
         * @author zc
         * @date 2024/6/28
         */
        @ApiId(1213)
        @PostMapping("/{year:\\d+}/analysis/comment")
        public Map<String, Long> saveComment(@RequestParam String property, @PathVariable long year,
                                             @Valid @RequestBody HotelKpiCommentDTO commentDTO) {
            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();
            if (Objects.nonNull(commentDTO.getId())) {
                // 校验是否存在
                HotelKpiAnalysisComment originComment = analysisCommentService.checkExist(commentDTO.getId());
                // 校验归属
                analysisCommentService.checkBelong(propertyId, year, loginUserId, originComment);
            }
            // 保存数据
            return ImmutableMap.of("id", analysisCommentService.save(propertyId, year, commentDTO, loginUserId));
        }

        /**
         * 删除评论
         *
         * @param property 资产标识
         * @param year     年份(yyyy)
         * @param cid      评论 id
         * @return emptyMap
         * @author zc
         * @date 2024/6/28
         */
        @ApiId(1214)
        @DeleteMapping("/{year:\\d+}/analysis/comment/{cid:\\d+}")
        public Map<Object, Object> deleteComment(@RequestParam String property, @PathVariable long year,
                                                 @PathVariable long cid) {
            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();
            // 校验是否存在
            HotelKpiAnalysisComment originComment = analysisCommentService.checkExist(cid);
            // 校验归属
            analysisCommentService.checkBelong(propertyId, year, loginUserId, originComment);
            // 删除
            analysisCommentService.removeById(cid);
            return Collections.emptyMap();
        }
    }