# PastReservationCommentServiceImpl.java

    package cloud.wconnect.frontoffice.service.pastreservation.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.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.dto.UserDTO;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.frontoffice.entity.pastreservation.PastReservation;
    import cloud.wconnect.frontoffice.entity.pastreservation.PastReservationComment;
    import cloud.wconnect.frontoffice.mapper.pastreservation.PastReservationCommentMapper;
    import cloud.wconnect.frontoffice.pojo.dto.pastreservation.PastReservationCommentDTO;
    import cloud.wconnect.frontoffice.service.pastreservation.IPastReservationCommentService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    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.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 连续入住人员信息关联备注 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2024-10-01
     */
    @Service
    @DS("logbook")
    public class PastReservationCommentServiceImpl extends ServiceImpl<PastReservationCommentMapper, PastReservationComment> implements IPastReservationCommentService {

        @Autowired
        private UserInternalService userInternalService;
        @Autowired
        private BeanMapper beanMapper;

        /**
         * 查询备注
         *
         * @param ids 连续入住信息 id
         * @return 备注
         * @author yyt
         * @date 2024/10/2
         **/
        @Override
        public List<PastReservationComment> listByPastReservationId(List<Long> ids) {
            return list(new QueryWrapper<PastReservationComment>().lambda().in(PastReservationComment::getPtRsvId, ids));
        }

        /**
         * 添加备注
         *
         * @param pastReservation 连续入住信息
         * @param dto             备注
         * @param userId          操作人
         * @author yyt
         * @date 2024/10/2
         **/
        @Override
        public void addComment(PastReservation pastReservation, PastReservationCommentDTO dto, int userId) {
            save(new PastReservationComment().setPropertyId(pastReservation.getPropertyId())
                    .setPtRsvId(pastReservation.getId()).setComment(dto.getComment()).setCreatedBy(userId).setCreatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 检查存在
         *
         * @param id 备注 id
         * @return 备注
         * @author yyt
         * @date 2024/10/2
         **/
        @Override
        public PastReservationComment checkExist(long id) {
            PastReservationComment comment = getById(id);
            if (Objects.isNull(comment)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the past reservation comment is not found");
            }

            return comment;
        }

        /**
         * 查询最新的备注
         *
         * @param ids 连续入住信息 id
         * @return 最新的备注
         * @author yyt
         * @date 2024/11/7
         **/
        @Override
        public Map<Long, String> listLatest(List<Long> ids) {
            List<PastReservationComment> comments = baseMapper.listLatest(ids);
            return comments.stream().collect(Collectors.toMap(PastReservationComment::getPtRsvId, PastReservationComment::getComment));
        }

        /**
         * 分页查询数据
         *
         * @param id   连续入住信息 id
         * @param page 页码
         * @param size 每页显示条数
         * @return 备注
         * @author yyt
         * @date 2024/12/3
         **/
        @Override
        public Page<PastReservationComment> listPage(long id, int page, int size) {
            return page(new Page<>(page, size), new QueryWrapper<PastReservationComment>().lambda()
                    .eq(PastReservationComment::getPtRsvId, id)
                    .orderByDesc(PastReservationComment::getId));
        }

        /**
         * 删除备注
         *
         * @param id 备注 id
         * @author yyt
         * @date 2024/12/3
         **/
        @Override
        public void delete(long id) {
            removeById(id);
        }

        /**
         * 获取备注
         *
         * @param tenantId 租户 id
         * @param id       连续入住信息 id
         * @param page     页码
         * @param size     每页显示条数
         * @return 备注
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public ListPagedDTO<PastReservationCommentDTO> listCommentDTO(int tenantId, long id, int page, int size) {
            Page<PastReservationComment> pageDetail = listPage(id, page, size);
            List<PastReservationComment> records = pageDetail.getRecords();
            if (CollectionUtils.isEmpty(records)) {
                return new ListPagedDTO<>();
            }

            Map<Integer, UserDTO> userById = userInternalService.getUsers(tenantId,
                            records.stream().map(PastReservationComment::getCreatedBy).distinct().collect(Collectors.toList()))
                    .stream().collect(Collectors.toMap(UserDTO::getId, v -> v));

            return new ListPagedDTO<>(pageDetail.getTotal(), beanMapper.mapList(records, PastReservationCommentDTO.class,
                    item -> {
                        UserDTO userDTO = userById.get(item.getCreatedBy());
                        item.setCreatedByName(userDTO.getName());
                        item.setAvatar(userDTO.getAvatar());
                    }));
        }

    }