# BgCommentServiceImpl.java

    package cloud.wconnect.frontoffice.service.badguest.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.badguest.BgComment;
    import cloud.wconnect.frontoffice.mapper.badguest.BgCommentMapper;
    import cloud.wconnect.frontoffice.pojo.dto.badguest.BadGuestCommentDTO;
    import cloud.wconnect.frontoffice.service.badguest.IBgCommentService;
    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.util.Collections;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;

    /**
     * <p>
     * 黑名单备注表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2024-06-17
     */
    @Service
    @DS("logbook")
    public class BgCommentServiceImpl extends ServiceImpl<BgCommentMapper, BgComment> implements IBgCommentService {

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

        /**
         * 查询黑名单备注信息
         *
         * @param badGuestIds 黑名单id集合
         * @return java.util.List<cloud.wconnect.frontoffice.entity.badguest.BgComment>
         * @author lichen
         * @date 2024/6/17
         */
        @Override
        public List<BgComment> listByBadGuestId(List<Long> badGuestIds) {
            if (CollectionUtils.isEmpty(badGuestIds)) return Collections.emptyList();
            return list(new LambdaQueryWrapper<BgComment>()
                    .in(BgComment::getBadGuestId, badGuestIds)
                    .orderByDesc(BgComment::getLastUpdatedTime));
        }

        /**
         * 校验是否存在
         *
         * @param cid 备注id
         * @return cloud.wconnect.frontoffice.entity.badguest.BgComment
         * @author lichen
         * @date 2024/6/17
         */
        @Override
        public BgComment checkExist(long cid) {
            BgComment bgComment = getById(cid);
            if (Objects.isNull(bgComment)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "Guests Blacklist comment not found, commentId=" + cid);
            }
            return bgComment;
        }

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

        /**
         * 新增备注
         *
         * @param propertyId         属性id
         * @param badGuestId         黑名单id
         * @param badGuestCommentDTO 黑名单备注DTO
         * @param operatorId         操作人id
         * @return java.lang.Long
         * @author lichen
         * @date 2025/12/29
         */
        @Override
        public Long save(int propertyId, Long badGuestId, BadGuestCommentDTO badGuestCommentDTO, int operatorId) {
            BgComment comment = new BgComment();
            comment.setPropertyId(propertyId);
            comment.setBadGuestId(badGuestId);
            comment.setContent(badGuestCommentDTO.getContent());
            comment.setCreatedBy(operatorId);
            comment.setLastUpdatedTime(DateTimeUtils.nowTimestamp());
            comment.setAttachments(badGuestCommentDTO.getAttachments());
            save(comment);

            return comment.getId();
        }

        /**
         * 删除备注
         *
         * @param bgComment 黑名单备注
         * @return void
         * @author lichen
         * @date 2024/6/17
         */
        @Override
        public void remove(BgComment bgComment) {
            if (removeById(bgComment.getId())) {
                //删除附件
                List<String> delS3Keys = AttachmentUtil.getKeysByAbbrAttachmentArrayJson(bgComment.getAttachments(), objectMapper);
                S3Utils.batchDeleteFile(region, bucketMap.get(S3BucketFunctionCons.GUESTS_BLACKLIST), delS3Keys);
            }
        }
    }