# BudgetDatabaseCommentFbServiceImpl.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.BudgetDatabaseCommentFb;
    import cloud.wconnect.rateshop.mapper.budgetdatabase.BudgetDatabaseCommentFbMapper;
    import cloud.wconnect.rateshop.pojo.dto.budgetdatabase.BudgetDatabaseCommentDTO;
    import cloud.wconnect.rateshop.pojo.dto.budgetdatabase.SaveCommentDTO;
    import cloud.wconnect.rateshop.service.budgetdatabase.IBudgetDatabaseCommentFbService;
    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 BudgetDatabaseCommentFbServiceImpl extends ServiceImpl<BudgetDatabaseCommentFbMapper, BudgetDatabaseCommentFb> implements IBudgetDatabaseCommentFbService {

        /**
         * 保存评论
         *
         * @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 BudgetDatabaseCommentFb()
                                    .setPropertyId(propertyId)
                                    .setTabId(item.getTabId())
                                    .setArea(item.getArea())
                                    .setRow(item.getRow())
                                    .setContent(item.getContent())
                                    .setUpdatedBy(userId)
                                    .setUpdatedTime(now))
                    .sorted(Comparator.comparing(BudgetDatabaseCommentFb::getTabId)
                            .thenComparing(BudgetDatabaseCommentFb::getArea)
                            .thenComparing(BudgetDatabaseCommentFb::getRow))
                    .toList());
        }

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

        /**
         * 封装  comment dto
         *
         * @param comment  comment
         * @param userById user
         * @return comment dto
         * @author yyt
         * @date 2025/11/14
         **/
        @Override
        public BudgetDatabaseCommentDTO packageCommentDTO(BudgetDatabaseCommentFb 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;
        }

    }