# SupportTeamReportServiceImpl.java

    package cloud.wconnect.workperformance.service.monthlyreport.impl;

    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.S3Utils;
    import cloud.wconnect.commonlib.util.TransactionUtils;
    import cloud.wconnect.workperformance.cons.S3BucketFunctionCons;
    import cloud.wconnect.workperformance.cons.monthlyreport.AreaEnum;
    import cloud.wconnect.workperformance.entity.monthlyreport.MonthlyReport;
    import cloud.wconnect.workperformance.entity.monthlyreport.ReportAttachment;
    import cloud.wconnect.workperformance.pojo.dto.monthlyreport.AttachmentDTO;
    import cloud.wconnect.workperformance.service.monthlyreport.IReportAttachmentService;
    import cloud.wconnect.workperformance.service.monthlyreport.ISupportTeamReportService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    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 org.springframework.transaction.annotation.Transactional;

    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.Executor;

    /**
     * Support Team Report 区域业务层接口
     *
     * @author lichen
     * @date 2023/9/5
     */
    @Service
    @DS("monthly_report")
    public class SupportTeamReportServiceImpl implements ISupportTeamReportService {

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

        /**
         * 保存Support Team Report区域数据
         *
         * @param monthlyReport 月报
         * @param propertyDTO   资产DTO
         * @param attachments   附件集合
         * @return void
         * @author lichen
         * @date 2023/9/5
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void save(MonthlyReport monthlyReport, PropertyDTO propertyDTO, List<AttachmentDTO> attachments) {
            Integer propertyId = propertyDTO.getId();
            //保存前的s3key集合
            List<String> saveBeforeS3Keys = reportAttachmentService.listByReportIdAndAreaId(propertyId, monthlyReport.getId(), AreaEnum.SUPPORT_TEAM_REPORT.getAreaId())
                    .stream().map(ReportAttachment::getFileKey).toList();

            //删除附件
            reportAttachmentService.removeByReportIdAndAreaId(monthlyReport.getPropertyId(), monthlyReport.getId(), AreaEnum.SUPPORT_TEAM_REPORT.getAreaId());

            //保存区域的附件
            List<String> saveAfterS3Keys = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(attachments)) {
                List<ReportAttachment> reportAttachments = Lists.newArrayListWithCapacity(attachments.size());
                for (AttachmentDTO attachment : attachments) {
                    reportAttachments.add(ReportAttachment.builder()
                            .tenantId(propertyDTO.getTenantId())
                            .propertyId(propertyId)
                            .reportId(monthlyReport.getId())
                            .areaId(AreaEnum.SUPPORT_TEAM_REPORT.getAreaId())
                            .fileName(attachment.getFileName())
                            .fileKey(attachment.getFileKey())
                            .build());
                    saveAfterS3Keys.add(attachment.getFileKey());
                }
                reportAttachmentService.saveBatch(reportAttachments);
            }

            /*
             * 事务提交后删除s3附件
             */
            TransactionUtils.afterCommitAsyncExecute(executor, () -> {
                List<String> delS3Keys = Lists.newArrayList(saveBeforeS3Keys);
                delS3Keys.removeAll(saveAfterS3Keys);
                S3Utils.batchDeleteFile(region, bucketMap.get(S3BucketFunctionCons.MONTHLY_REPORT), delS3Keys);
            });
        }
    }