# AcApiCommentServiceImpl.java

    package cloud.wconnect.finance.service.api.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.exception.ForbiddenException;
    import cloud.wconnect.commonlib.pojo.dto.UserDTO;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.finance.entity.api.AcApiComment;
    import cloud.wconnect.finance.mapper.api.AcApiCommentMapper;
    import cloud.wconnect.finance.pojo.dto.api.AcApiCommentDTO;
    import cloud.wconnect.finance.service.api.IAcApiCommentService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import jakarta.annotation.Resource;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;

    import java.text.MessageFormat;
    import java.util.*;
    import java.util.stream.Collectors;

    /**
     * <p>
     * api  备注 服务实现类
     * </p>
     *
     * @author zc
     * @since 2023-08-04
     */
    @Service
    @DS("finance")
    public class AcApiCommentServiceImpl extends ServiceImpl<AcApiCommentMapper, AcApiComment> implements IAcApiCommentService {
        @Resource
        private UserInternalService userInternalService;
        @Resource
        private BeanMapper beanMapper;

        /**
         * 根据 Api id 获取备注
         *
         * @param propertyId 资产 id
         * @param apiIds     Api id
         * @return AcApiComment 集合
         * @author zc
         * @date 2023/8/4
         */
        @Override
        public List<AcApiComment> listByApiIds(int propertyId, List<Long> apiIds) {
            if (CollectionUtils.isEmpty(apiIds)) {
                return Collections.emptyList();
            }
            return list(new QueryWrapper<AcApiComment>().lambda()
                    .eq(AcApiComment::getPropertyId, propertyId)
                    .in(AcApiComment::getApiId, apiIds));
        }

        /**
         * 获取备注集合
         *
         * @param tenantId   租户 id
         * @param propertyId 资产 id
         * @param apiId      Api id
         * @return AcApiCommentDTO 集合
         * @author zc
         * @date 2023/8/7
         */
        @Override
        public List<AcApiCommentDTO> list(int tenantId, int propertyId, long apiId) {
            List<AcApiComment> apiCommentList = list(new QueryWrapper<AcApiComment>().lambda()
                    .eq(AcApiComment::getPropertyId, propertyId)
                    .eq(AcApiComment::getApiId, apiId)
                    .orderByDesc(AcApiComment::getId));
            if (CollectionUtils.isEmpty(apiCommentList)) {
                return Collections.emptyList();
            }
            Set<Integer> userIds = apiCommentList.stream()
                    .map(AcApiComment::getOperatorId).collect(Collectors.toSet());
            Map<Integer, UserDTO> userId2UserDTO = userInternalService.getUserMapByIds(tenantId, userIds);
            return beanMapper.mapList(apiCommentList, AcApiCommentDTO.class, item -> {
                UserDTO userDTO = userId2UserDTO.getOrDefault(item.getOperatorId(), new UserDTO());
                item.setOperatorName(userDTO.getName());
                item.setAvatar(userDTO.getAvatar());
            });
        }

        /**
         * 校验数据是否存在，并返回数据对象
         *
         * @param id 备注 id
         * @return AcApiComment
         * @author zc
         * @date 2023/8/7
         */
        @Override
        public AcApiComment checkExist(long id) {
            AcApiComment apiComment = getById(id);
            if (Objects.isNull(apiComment)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The apiComment is not found; noteId: " + id);
            }
            return apiComment;
        }

        /**
         * 校验数据归属
         *
         * @param propertyId   资产 id
         * @param apiId        API id
         * @param acApiComment 备注数据
         * @author zc
         * @date 2023/8/7
         */
        @Override
        public void checkBelong(int propertyId, long apiId, AcApiComment acApiComment) {
            if (!acApiComment.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The acApiComment does not belong to this property; propertyId:{0}; noteId:{1}",
                                propertyId, acApiComment.getId()));
            }
            if (!acApiComment.getApiId().equals(apiId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The acApiComment does not belong to this api; apiId:{0}; noteId:{1}",
                                apiId, acApiComment.getId()));
            }
        }
    }