# HotelKpiCommentServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    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.commonlib.util.DateTimeUtils;
    import cloud.wconnect.workperformance.entity.hotelkpi.HotelKpiComment;
    import cloud.wconnect.workperformance.mapper.hotelkpi.HotelKpiCommentMapper;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.kpi.HotelKpiCommentDTO;
    import cloud.wconnect.workperformance.pojo.query.hotelkpi.HotelKpiCommentQuery;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiCommentService;
    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 jakarta.annotation.Resource;
    import org.springframework.stereotype.Service;

    import java.text.MessageFormat;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.Set;
    import java.util.stream.Collectors;

    /**
     * <p>
     * Hotel KPI Comment 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-06-24
     */
    @Service
    @DS("work_performance")
    public class HotelKpiCommentServiceImpl extends ServiceImpl<HotelKpiCommentMapper, HotelKpiComment> implements IHotelKpiCommentService {

        @Resource
        private UserInternalService userInternalService;
        @Resource
        private BeanMapper beanMapper;

        /**
         * 获取酒店KPI评论
         *
         * @param propertyDTO 资产
         * @param date        日期(yyyyMMdd)
         * @param query       查询条件
         * @return HotelKpiCommentDTO 集合
         * @author zc
         * @date 2024/6/27
         */
        @Override
        public ListPagedDTO<HotelKpiCommentDTO> list(PropertyDTO propertyDTO, long date, HotelKpiCommentQuery query) {
            Page<HotelKpiComment> commentPage = page(new Page<>(query.getPage(), query.getSize()), new LambdaQueryWrapper<HotelKpiComment>()
                    .eq(HotelKpiComment::getWeek, date)
                    .eq(HotelKpiComment::getPropertyId, propertyDTO.getId())
                    .orderByDesc(HotelKpiComment::getUpdatedTime));

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

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

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

        /**
         * 校验是否存在
         *
         * @param id 评论 id
         * @return HotelKpiComment 评论
         * @author zc
         * @date 2024/6/27
         */
        @Override
        public HotelKpiComment checkExist(long id) {
            HotelKpiComment originComment = getById(id);
            if (Objects.isNull(originComment)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The comment does not exist");
            }
            return originComment;
        }

        /**
         * 校验归属
         *
         * @param propertyId    资产 id
         * @param date          日期(yyyyMMdd)
         * @param userId        用户 id
         * @param originComment 原始评论数据
         * @author zc
         * @date 2024/6/27
         */
        @Override
        public void checkBelong(int propertyId, long date, int userId, HotelKpiComment originComment) {
            if (!Objects.equals(originComment.getPropertyId(), propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The comment does not belong to the property; origin: {0}, current: {1}",
                                originComment.getPropertyId(), propertyId));
            }
            if (!Objects.equals(originComment.getWeek(), date)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The comment does not belong to the date; origin: {0}, current: {1}",
                                originComment.getWeek(), date));
            }
            if (!Objects.equals(originComment.getUpdatedBy(), userId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The comment does not belong to the user; origin: {0}, current: {1}",
                                originComment.getUpdatedBy(), userId));
            }
        }

        /**
         * 保存数据
         *
         * @param propertyId 资产 id
         * @param date        日期(yyyyMMdd)
         * @param commentDTO  评论数据DTO
         * @param userId      用户 id
         * @return id      评论 id
         * @author zc
         * @date 2024/6/27
         */
        @Override
        public long save(int propertyId, long date, HotelKpiCommentDTO commentDTO, int userId) {
            HotelKpiComment comment = new HotelKpiComment();
            comment.setId(commentDTO.getId());
            comment.setPropertyId(propertyId);
            comment.setWeek(date);
            comment.setContent(commentDTO.getContent());
            comment.setUpdatedBy(userId);
            comment.setUpdatedTime(DateTimeUtils.nowTimestamp());

            saveOrUpdate(comment);

            return comment.getId();
        }
    }