# HotelKpiWarningCommentServiceImpl.java

    package cloud.wconnect.workperformance.service.hotelkpi.impl.old;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.UserDTO;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.workperformance.entity.hotelkpi.old.HotelKpiWarningComment;
    import cloud.wconnect.workperformance.mapper.hotelkpi.old.HotelKpiWarningCommentMapper;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.kpi.HotelKpiWarningCommentDTO;
    import cloud.wconnect.workperformance.pojo.query.hotelkpi.old.HotelKpiWarningCommentQuery;
    import cloud.wconnect.workperformance.service.hotelkpi.old.IHotelKpiWarningCommentService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;

    import jakarta.annotation.Resource;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author zc
     * @since 2025-01-11
     */
    @Service("hotelKpiWarningCommentServiceImpl_old")
    @DS("work_performance")
    public class HotelKpiWarningCommentServiceImpl extends ServiceImpl<HotelKpiWarningCommentMapper, HotelKpiWarningComment> implements IHotelKpiWarningCommentService {
        @Resource
        private UserInternalService userInternalService;
        @Resource
        private BeanMapper beanMapper;

        /**
         * 获取酒店 KPI 警示评论
         *
         * @param propertyDTO 资产
         * @param query       查询条件
         * @return HotelKpiWarningCommentDTO 集合
         * @author zc
         * @date 2025/1/11
         */
        @Override
        public ListPagedDTO<HotelKpiWarningCommentDTO> list(PropertyDTO propertyDTO, HotelKpiWarningCommentQuery query) {
            Page<HotelKpiWarningComment> commentPage = page(new Page<>(query.getPage(), query.getSize()),
                    new LambdaQueryWrapper<HotelKpiWarningComment>()
                            .eq(HotelKpiWarningComment::getPropertyId, propertyDTO.getId())
                            .eq(HotelKpiWarningComment::getDateType, query.getDateType())
                            .eq(HotelKpiWarningComment::getDate, query.getDate())
                            .eq(HotelKpiWarningComment::getMetricsType, query.getMetricsType())
                            .orderByDesc(HotelKpiWarningComment::getId));

            List<HotelKpiWarningComment> commentList = commentPage.getRecords();

            // 获取用户信息
            Set<Integer> userIds = commentList.stream().map(HotelKpiWarningComment::getUpdatedBy).collect(Collectors.toSet());
            Map<Integer, UserDTO> userId2User = userInternalService.getUserMapByIds(propertyDTO.getTenantId(), userIds);

            return new ListPagedDTO<>(commentPage.getTotal(), beanMapper.mapList(commentList, HotelKpiWarningCommentDTO.class, (comment, dto) -> {
                UserDTO userDTO = userId2User.getOrDefault(comment.getUpdatedBy(), new UserDTO());
                dto.setUpdatedName(userDTO.getName());
                dto.setAvatar(userDTO.getAvatar());
            }));
        }

        /**
         * 获取指定日期范围、指标类型的警示评论
         *
         * @param propertyId   资产 id
         * @param dateType     日期类型（1:week 2:month 3:quarter 4:year）
         * @param startDate    开始日期
         * @param endDate      结束日期
         * @param metricsTypes 指标类型集合
         * @author zc
         * @date 2025/1/11
         */
        @Override
        public List<HotelKpiWarningComment> listByDateRangeAndMetricsTypes(int propertyId, int dateType, long startDate, long endDate,
                                                                           List<Integer> metricsTypes) {
            return list(new LambdaQueryWrapper<HotelKpiWarningComment>()
                    .eq(HotelKpiWarningComment::getPropertyId, propertyId)
                    .eq(HotelKpiWarningComment::getDateType, dateType)
                    .between(HotelKpiWarningComment::getDate, startDate, endDate)
                    .in(CollectionUtils.isNotEmpty(metricsTypes), HotelKpiWarningComment::getMetricsType, metricsTypes));
        }
    }