# SvaDepartmentCategoryStatisticsCommentServiceImpl.java

    package cloud.wconnect.workperformance.service.sva.scheduleactual.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.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.UserDTO;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.workperformance.entity.sva.SvaDepartmentCategoryStatisticsComment;
    import cloud.wconnect.workperformance.mapper.sva.SvaDepartmentCategoryStatisticsCommentMapper;
    import cloud.wconnect.workperformance.pojo.dto.sva.hoursstatistics.SvaDepCategoryStatisticCommentDTO;
    import cloud.wconnect.workperformance.pojo.query.sva.schedulevsactual.SvaHoursStatisticsCommentQuery;
    import cloud.wconnect.workperformance.service.sva.scheduleactual.ISvaDepartmentCategoryStatisticsCommentService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import jakarta.annotation.Nullable;
    import jakarta.annotation.Resource;
    import java.text.MessageFormat;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.Set;
    import java.util.stream.Collectors;

    /**
     * <p>
     * SVA 部门分类工时统计备注 服务实现类
     * </p>
     *
     * @author zc
     * @since 2025-02-08
     */
    @Service
    @DS("work_performance")
    public class SvaDepartmentCategoryStatisticsCommentServiceImpl extends ServiceImpl<SvaDepartmentCategoryStatisticsCommentMapper, SvaDepartmentCategoryStatisticsComment> implements ISvaDepartmentCategoryStatisticsCommentService {
        @Resource
        private UserInternalService userInternalService;
        @Resource
        private BeanMapper beanMapper;

        /**
         * 根据部门分类获取指定日期范围内的工时统计评论
         *
         * @param propertyDTO 资产
         * @param query       查询条件
         * @return ListPagedDTO<SvaDepCategoryStatisticCommentDTO>
         * @author zc
         * @date 2025/2/8
         */
        @Override
        public ListPagedDTO<SvaDepCategoryStatisticCommentDTO> list(PropertyDTO propertyDTO, SvaHoursStatisticsCommentQuery query) {
            Page<SvaDepartmentCategoryStatisticsComment> commentPage = page(new Page<>(query.getPage(), query.getSize()),
                    new LambdaQueryWrapper<SvaDepartmentCategoryStatisticsComment>()
                            .eq(SvaDepartmentCategoryStatisticsComment::getPropertyId, propertyDTO.getId())
                            .eq(SvaDepartmentCategoryStatisticsComment::getDepartmentCategoryId, query.getDepartmentCategoryId())
                            .eq(SvaDepartmentCategoryStatisticsComment::getFromDate, query.getFromDate())
                            .eq(SvaDepartmentCategoryStatisticsComment::getToDate, query.getToDate())
                            .orderByDesc(SvaDepartmentCategoryStatisticsComment::getId));

            List<SvaDepartmentCategoryStatisticsComment> commentList = commentPage.getRecords();

            // 获取用户信息
            Set<Integer> userIds = commentList.stream().map(SvaDepartmentCategoryStatisticsComment::getUpdatedBy).collect(Collectors.toSet());
            Map<Integer, UserDTO> userId2User = userInternalService.getUserMapByIds(propertyDTO.getTenantId(), userIds);

            return new ListPagedDTO<>(commentPage.getTotal(), beanMapper.mapList(commentList, SvaDepCategoryStatisticCommentDTO.class,
                    (comment, dto) -> {
                        UserDTO userDTO = userId2User.getOrDefault(comment.getUpdatedBy(), new UserDTO());
                        dto.setUpdatedName(userDTO.getName());
                        dto.setAvatar(userDTO.getAvatar());
                    }));
        }

        /**
         * 校验是否存在
         *
         * @param id id
         * @return SvaDepartmentCategoryStatisticsComment
         * @author zc
         * @date 2025/2/8
         */
        @Override
        public SvaDepartmentCategoryStatisticsComment checkExist(long id) {
            SvaDepartmentCategoryStatisticsComment originComment = getById(id);
            if (Objects.isNull(originComment)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The comment does not exist");
            }
            return originComment;
        }

        /**
         * 校验归属
         *
         * @param propertyId    资产 id
         * @param operatorId    操作人 id
         * @param originComment 原始评论数据
         * @author zc
         * @date 2025/2/8
         */
        @Override
        public void checkBelong(int propertyId, int operatorId, SvaDepartmentCategoryStatisticsComment originComment) {
            if (!Objects.equals(originComment.getPropertyId(), propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The warning comment does not belong to the property; origin: {0}, current: {1}",
                                originComment.getPropertyId(), propertyId));
            }
            if (!Objects.equals(originComment.getUpdatedBy(), operatorId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The warning comment does not belong to the user; origin: {0}, current: {1}",
                                originComment.getUpdatedBy(), operatorId));
            }
        }

        /**
         * 保存数据
         *
         * @param propertyId 资产 id
         * @param commentDTO 评论数据
         * @param operatorId 操作人 id
         * @return id
         * @author zc
         * @date 2025/2/8
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public long save(int propertyId, SvaDepCategoryStatisticCommentDTO commentDTO, int operatorId) {
            SvaDepartmentCategoryStatisticsComment comment = beanMapper.map(commentDTO,
                    SvaDepartmentCategoryStatisticsComment.class, entity -> {
                        entity.setPropertyId(propertyId);
                        entity.setUpdatedBy(operatorId);
                        entity.setUpdatedTime(DateTimeUtils.nowTimestamp());
                    });
            saveOrUpdate(comment);
            return comment.getId();
        }

        /**
         * 根据日期范围获取部门分类工时统计评论
         *
         * @param propertyId           资产 id
         * @param fromDate             开始日期
         * @param toDate               结束日期
         * @param departmentCategoryIds 部门分类 id
         * @return SvaDepartmentCategoryStatisticsComment 集合
         * @author zc
         * @date 2025/2/8
         */
        @Override
        public List<SvaDepartmentCategoryStatisticsComment> listByDateRangeAndDepCategoryId(int propertyId, long fromDate,
                                                                                            long toDate, @Nullable List<Integer> departmentCategoryIds) {
            return list(new LambdaQueryWrapper<SvaDepartmentCategoryStatisticsComment>()
                    .eq(SvaDepartmentCategoryStatisticsComment::getPropertyId, propertyId)
                    .in(CollectionUtils.isNotEmpty(departmentCategoryIds),
                            SvaDepartmentCategoryStatisticsComment::getDepartmentCategoryId, departmentCategoryIds)
                    .eq(SvaDepartmentCategoryStatisticsComment::getFromDate, fromDate)
                    .eq(SvaDepartmentCategoryStatisticsComment::getToDate, toDate));
        }
    }