# TaRecordNotesServiceImpl.java

    package cloud.wconnect.finance.service.ta.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.ta.TaRecordNotes;
    import cloud.wconnect.finance.mapper.ta.TaRecordNotesMapper;
    import cloud.wconnect.finance.pojo.dto.ta.TaRecordsNoteDTO;
    import cloud.wconnect.finance.service.ta.ITaRecordNotesService;
    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>
     * 服务实现类
     * </p>
     *
     * @author zc
     * @since 2023-07-07
     */
    @Service
    @DS("finance")
    public class TaRecordNotesServiceImpl extends ServiceImpl<TaRecordNotesMapper, TaRecordNotes> implements ITaRecordNotesService {
        @Resource
        private UserInternalService userInternalService;
        @Resource
        private BeanMapper beanMapper;

        /**
         * 根据 Records id 获取 TA Records 备注条数
         *
         * @param recordsStatusIds 记录 id
         * @return RecordsId2NoteCount
         * @author zc
         * @date 2023/7/10
         */
        @Override
        public Map<Long, Long> getRecordsId2NoteCount(List<Long> recordsStatusIds) {
            return list(new QueryWrapper<TaRecordNotes>().lambda()
                    .in(TaRecordNotes::getRId, recordsStatusIds)).stream()
                    .collect(Collectors.groupingBy(TaRecordNotes::getRId, Collectors.counting()));
        }

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

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

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

        /**
         * 删除 Records Status Note 数据
         *
         * @param recordsStatusIds Records Status id
         * @author zc
         * @date 2023/7/27
         */
        @Override
        public void removeByRecordsIds(List<Long> recordsStatusIds) {
            if (CollectionUtils.isEmpty(recordsStatusIds)) {
                return;
            }
            remove(new QueryWrapper<TaRecordNotes>().lambda()
                    .in(TaRecordNotes::getRId, recordsStatusIds));
        }
    }