# EngineeringRequestCommentServiceImpl.java

    package cloud.wconnect.frontoffice.service.engineeringrequest.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.pojo.dto.UserDTO;
    import cloud.wconnect.commonlib.service.UserInternalService;
    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.engineeringrequest.EngineeringRequestComment;
    import cloud.wconnect.frontoffice.mapper.engineeringrequest.EngineeringRequestCommentMapper;
    import cloud.wconnect.frontoffice.pojo.bo.engineeringrequest.NotifyBodyBO;
    import cloud.wconnect.frontoffice.pojo.dto.engineeringrequest.EngineeringRequestCommentDTO;
    import cloud.wconnect.frontoffice.service.engineeringrequest.IEngineeringRequestCommentService;
    import cloud.wconnect.frontoffice.service.engineeringrequest.notify.EngineeringRequestNotifyService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    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;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-05-10
     */
    @Service
    @DS("logbook")
    public class EngineeringRequestCommentServiceImpl extends ServiceImpl<EngineeringRequestCommentMapper, EngineeringRequestComment> implements IEngineeringRequestCommentService {

        @Autowired
        private EngineeringRequestNotifyService engineeringRequestCommentOperation;
        @Autowired
        private UserInternalService userInternalService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private ObjectMapper objectMapper;

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

        /**
         * 添加 Engineering Request 附件
         *
         * @param propertyId  资产
         * @param id          Engineering Request Id
         * @param attachments 附件
         * @param userId      操作人
         * @author yyt
         * @date 2023/3/29
         */
        @Override
        public void saveEngineeringRequestAttachment(int propertyId, Integer id, List<String> attachments, int userId) {
            if (CollectionUtils.isEmpty(attachments)) {
                return;
            }

            saveBatch(attachments.stream().map(item -> new EngineeringRequestComment().setPropertyId(propertyId)
                            .setRequestId(id).setContent(item).setCommentType(EngineeringRequestComment.TYPE_REQUEST_ATTACHMENT)
                            .setCreateId(userId).setCreateTime(DateTimeUtils.nowTimestamp()))
                    .collect(Collectors.toList()));
        }

        /**
         * 清除 Engineering Request 附件
         *
         * @param id Engineering Request Id
         * @author yyt
         * @date 2023/3/29
         */
        @Override
        public void clearEngineeringRequestAttachment(Integer id) {
            remove(new QueryWrapper<EngineeringRequestComment>().lambda().eq(EngineeringRequestComment::getRequestId, id)
                    .eq(EngineeringRequestComment::getCommentType, EngineeringRequestComment.TYPE_REQUEST_ATTACHMENT));
        }

        /**
         * 获取 Engineering Request 附件
         *
         * @param id Engineering Request Id
         * @return Engineering Request 附件
         * @author yyt
         * @date 2023/3/29
         */
        @Override
        public List<EngineeringRequestComment> getEngineeringRequestAttachment(Integer id) {
            return baseMapper.selectList(new QueryWrapper<EngineeringRequestComment>().lambda()
                    .eq(EngineeringRequestComment::getRequestId, id)
                    .eq(EngineeringRequestComment::getCommentType, EngineeringRequestComment.TYPE_REQUEST_ATTACHMENT));
        }

        /**
         * 获取备注
         *
         * @param id Engineering Request Id
         * @return Engineering Request 备注
         * @author yyt
         * @date 2023/3/30
         */
        @Override
        public List<EngineeringRequestComment> listEngineeringRequestComment(int id) {
            return baseMapper.selectList(new QueryWrapper<EngineeringRequestComment>().lambda()
                    .eq(EngineeringRequestComment::getRequestId, id)
                    .ne(EngineeringRequestComment::getCommentType, EngineeringRequestComment.TYPE_REQUEST_ATTACHMENT)
                    .orderByDesc(EngineeringRequestComment::getId));
        }

        /**
         * 新增备注
         *
         * @param id         Engineering Request Id
         * @param propertyId 资产 id
         * @param commentDTO 备注信息
         * @param userId     操作人
         * @author yyt
         * @date 2023/3/30
         */
        @Override
        public void saveComment(int id, int propertyId, EngineeringRequestCommentDTO commentDTO, int userId) {
            EngineeringRequestComment comment = beanMapper.map(commentDTO, EngineeringRequestComment.class);
            comment.setRequestId(id).setPropertyId(propertyId).setCreateId(userId)
                    .setCreateTime(DateTimeUtils.nowTimestamp());
            boolean result = save(comment);

            if (result) {
                NotifyBodyBO notifyBodyBO = new NotifyBodyBO(id);
                notifyBodyBO.setComment(comment);
                engineeringRequestCommentOperation.notify(notifyBodyBO);
            }
        }

        /**
         * 校验备注存在
         *
         * @param requestId Engineering Request Id
         * @param id        备注 id
         * @return 备注
         * @author yyt
         * @date 2023/3/30
         */
        @Override
        public EngineeringRequestComment checkExist(int requestId, int id) {
            EngineeringRequestComment comment = getOne(new QueryWrapper<EngineeringRequestComment>().lambda()
                    .eq(EngineeringRequestComment::getRequestId, requestId)
                    .eq(EngineeringRequestComment::getId, id));
            if (Objects.isNull(comment)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the engineeringRequestComment is not found;id:" + id);
            }
            return comment;
        }

        /**
         * 查询备注
         *
         * @param ids  engineering request id
         * @param type 备注类型
         * @return 备注
         * @author yyt
         * @date 2024/11/18
         **/
        @Override
        public List<EngineeringRequestComment> list(List<Integer> ids, int type) {
            return list(new QueryWrapper<EngineeringRequestComment>().lambda()
                    .in(EngineeringRequestComment::getRequestId, ids)
                    .eq(EngineeringRequestComment::getCommentType, type));
        }

        /**
         * 查询 Engineering Request 备注
         *
         * @param tenantId  租户 id
         * @param requestId engineering request id
         * @return 备注
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public List<EngineeringRequestCommentDTO> listCommentDTO(Integer tenantId, int requestId) {
            List<EngineeringRequestComment> comments = listEngineeringRequestComment(requestId);
            if (CollectionUtils.isEmpty(comments)) {
                return Collections.emptyList();
            }

            List<Integer> userIds = comments.stream().map(EngineeringRequestComment::getCreateId).distinct()
                    .collect(Collectors.toList());
            Map<Integer, UserDTO> userById = userInternalService.getUsers(tenantId, userIds)
                    .stream().collect(Collectors.toMap(UserDTO::getId, v -> v));
            return beanMapper.mapList(comments, EngineeringRequestCommentDTO.class,
                    item -> {
                        UserDTO userDTO = userById.get(item.getCreateId());
                        item.setCreateName(userDTO.getName());
                        item.setAvatar(userDTO.getAvatar());
                    });
        }

        /**
         * 删除 Engineering Request 备注
         *
         * @param comment Engineering Request 备注
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void delete(EngineeringRequestComment comment) {
            boolean result = removeById(comment.getId());
            if(result && comment.getCommentType() == EngineeringRequestComment.TYPE_ATTACHMENT){
                List<String> delS3Keys = AttachmentUtil.getKeysByAttachments(Lists.newArrayList(comment.getContent()), objectMapper);
                S3Utils.batchDeleteFile(region,bucketMap.get(S3BucketFunctionCons.ENGINEERING_REQUEST),delS3Keys);
            }

        }

    }