# BudgetDatabaseCommentRoomServiceImpl.java

    package cloud.wconnect.rateshop.service.budgetdatabase.impl;

    import cloud.wconnect.commonlib.pojo.dto.UserDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.rateshop.entity.budgetdatabase.BudgetDatabaseCommentRoom;
    import cloud.wconnect.rateshop.mapper.budgetdatabase.BudgetDatabaseCommentRoomMapper;
    import cloud.wconnect.rateshop.pojo.dto.budgetdatabase.BudgetDatabaseCommentDTO;
    import cloud.wconnect.rateshop.pojo.dto.budgetdatabase.SaveCommentDTO;
    import cloud.wconnect.rateshop.service.budgetdatabase.IBudgetDatabaseCommentRoomService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.stereotype.Service;

    import java.util.*;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2025-11-10
     */
    @Service
    @DS("rate_shop")
    public class BudgetDatabaseCommentRoomServiceImpl extends ServiceImpl<BudgetDatabaseCommentRoomMapper, BudgetDatabaseCommentRoom> implements IBudgetDatabaseCommentRoomService {

        /**
         * 查询评论
         *
         * @param propertyId 资产 id
         * @param tabId      年
         * @param areas      区域
         * @return 评论
         * @author yyt
         * @date 2025/9/11
         **/
        @Override
        public List<BudgetDatabaseCommentRoom> list(int propertyId, int tabId, Set<Integer> areas) {
            return list(new QueryWrapper<BudgetDatabaseCommentRoom>().lambda()
                    .eq(BudgetDatabaseCommentRoom::getPropertyId, propertyId)
                    .eq(BudgetDatabaseCommentRoom::getTabId, tabId)
                    .in(BudgetDatabaseCommentRoom::getArea, areas));
        }

        /**
         * 封装  comment dto
         *
         * @param comment  comment
         * @param userById user
         * @return comment dto
         * @author yyt
         * @date 2025/11/14
         **/
        @Override
        public BudgetDatabaseCommentDTO packageCommentDTO(BudgetDatabaseCommentRoom comment, Map<Integer, UserDTO> userById) {
            if (Objects.isNull(comment)) return null;

            UserDTO userDTO = userById.getOrDefault(comment.getUpdatedBy(), new UserDTO());

            BudgetDatabaseCommentDTO commentDTO = new BudgetDatabaseCommentDTO();
            commentDTO.setContent(comment.getContent());
            commentDTO.setUpdatedBy(comment.getUpdatedBy());
            commentDTO.setUpdatedByName(userDTO.getName());
            commentDTO.setUpdatedTime(comment.getUpdatedTime());
            commentDTO.setAvatar(userDTO.getAvatar());

            return commentDTO;
        }

        /**
         * 保存评论
         *
         * @param propertyId  资产 id
         * @param commentDTOS 评论
         * @param userId      操作人
         * @author yyt
         * @date 2025/11/14
         **/
        @Override
        public void save(int propertyId, List<SaveCommentDTO> commentDTOS, int userId) {
            long now = DateTimeUtils.nowTimestamp();
            baseMapper.saveOrUpdateBatchReWrite(commentDTOS.stream().map(item ->
                            new BudgetDatabaseCommentRoom()
                                    .setPropertyId(propertyId)
                                    .setTabId(item.getTabId())
                                    .setArea(item.getArea())
                                    .setRow(item.getRow())
                                    .setContent(item.getContent())
                                    .setUpdatedBy(userId)
                                    .setUpdatedTime(now))
                    .sorted(Comparator.comparing(BudgetDatabaseCommentRoom::getTabId)
                            .thenComparing(BudgetDatabaseCommentRoom::getArea)
                            .thenComparing(BudgetDatabaseCommentRoom::getRow))
                    .toList());
        }

    }