# CompRoomCommentServiceImpl.java

    package cloud.wconnect.frontoffice.service.comproom.impl;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.util.AttachmentUtil;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.S3Utils;
    import cloud.wconnect.frontoffice.cons.S3BucketFunctionCons;
    import cloud.wconnect.frontoffice.entity.comproom.CompRoomComment;
    import cloud.wconnect.frontoffice.mapper.comproom.CompRoomCommentMapper;
    import cloud.wconnect.frontoffice.pojo.dto.comproom.CompRoomCommentDTO;
    import cloud.wconnect.frontoffice.service.comproom.ICompRoomCommentService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;

    import java.text.MessageFormat;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;

    /**
     * <p>
     * 免费房备注表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2024-06-19
     */
    @Service
    @DS("logbook")
    public class CompRoomCommentServiceImpl extends ServiceImpl<CompRoomCommentMapper, CompRoomComment> implements ICompRoomCommentService {

        @Value("${aws-s3.region}")
        private String region;
        @Value("#{${aws-s3.function-bucket}}")
        private Map<String, Map<String, Object>> bucketMap;
        @Autowired
        private ObjectMapper objectMapper;

        /**
         * 查询免费房的备注信息
         *
         * @param compRoomIds 免费房id集合
         * @return java.util.List<cloud.wconnect.frontoffice.entity.comproom.CompRoomComment>
         * @author lichen
         * @date 2024/6/19
         */
        @Override
        public List<CompRoomComment> listByCompRoomId(List<Long> compRoomIds) {
            if (CollectionUtils.isEmpty(compRoomIds)) return Collections.emptyList();
            return list(new LambdaQueryWrapper<CompRoomComment>().in(CompRoomComment::getCompRoomId, compRoomIds)
                    .orderByDesc(CompRoomComment::getLastUpdatedTime));
        }

        /**
         * 校验备注是否存在
         *
         * @param cid 备注id
         * @return cloud.wconnect.frontoffice.entity.comproom.CompRoomComment
         * @author lichen
         * @date 2024/6/19
         */
        @Override
        public CompRoomComment checkExist(long cid) {
            CompRoomComment compRoomComment = getById(cid);
            if (Objects.isNull(compRoomComment)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND, BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        MessageFormat.format("The comp room comment not found, compRoomCommentId:{0}", cid));
            }
            return compRoomComment;
        }

        /**
         * 修改备注
         *
         * @param oriCompRoomComment 修改前的免费房备注
         * @param compRoomCommentDTO 修改后的免费房备注DTO
         * @return void
         * @author lichen
         * @date 2024/6/19
         */
        @Override
        public void update(CompRoomComment oriCompRoomComment, CompRoomCommentDTO compRoomCommentDTO) {
            CompRoomComment comment = new CompRoomComment();
            comment.setId(oriCompRoomComment.getId());
            comment.setContent(compRoomCommentDTO.getContent());
            comment.setAttachments(compRoomCommentDTO.getAttachments());
            comment.setLastUpdatedTime(DateTimeUtils.nowTimestamp());
            //更新并且删除无用的附件
            if (updateById(comment)) {
                //修改前的s3Key集合
                List<String> updateBeforeS3Keys = AttachmentUtil.getKeysByAbbrAttachmentArrayJson(oriCompRoomComment.getAttachments(), objectMapper);
                //修改后的s3Key集合
                List<String> updateAfterS3Keys = AttachmentUtil.getKeysByAbbrAttachmentArrayJson(compRoomCommentDTO.getAttachments(), objectMapper);
                //删除的附件 = 修改前的附件集合 - 修改后的附件集合
                List<String> delS3Keys = Lists.newArrayList(updateBeforeS3Keys);
                delS3Keys.removeAll(updateAfterS3Keys);
                S3Utils.batchDeleteFile(region, bucketMap.get(S3BucketFunctionCons.COMP_ROOM), delS3Keys);
            }
        }

        /**
         * 新增备注
         *
         * @param propertyId 资产id
         * @param compRoomId 免费房id
         * @param commentDTO 备注DTO
         * @param operatorId 操作人id
         * @return java.lang.Long
         * @author lichen
         * @date 2025/12/29
         */
        @Override
        public Long save(int propertyId, Long compRoomId, CompRoomCommentDTO commentDTO, int operatorId) {
            CompRoomComment comment = new CompRoomComment();
            comment.setPropertyId(propertyId);
            comment.setCompRoomId(compRoomId);
            comment.setContent(commentDTO.getContent());
            comment.setCreatedBy(operatorId);
            comment.setLastUpdatedTime(DateTimeUtils.nowTimestamp());
            comment.setAttachments(commentDTO.getAttachments());
            save(comment);

            return comment.getId();
        }

        /**
         * 删除备注
         *
         * @param compRoomComment 免费房备注
         * @return void
         * @author lichen
         * @date 2024/6/19
         */
        @Override
        public void remove(CompRoomComment compRoomComment) {
            if (removeById(compRoomComment.getId())) {
                //删除附件
                List<String> delS3Keys = AttachmentUtil.getKeysByAbbrAttachmentArrayJson(compRoomComment.getAttachments(), objectMapper);
                S3Utils.batchDeleteFile(region, bucketMap.get(S3BucketFunctionCons.COMP_ROOM), delS3Keys);
            }
        }
    }