# STRReportServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.component.SessionContext;
    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.entity.monthlyreport.ReportRemark;
    import cloud.wconnect.workperformance.mapper.monthlyreport.ReportRemarkMapper;
    import cloud.wconnect.workperformance.pojo.dto.monthlyreport.AttachmentDTO;
    import cloud.wconnect.workperformance.pojo.dto.monthlyreport.strreport.STRReportDetailDTO;
    import cloud.wconnect.workperformance.service.monthlyreport.IReportAttachmentService;
    import cloud.wconnect.workperformance.service.monthlyreport.IReportRemarkService;
    import cloud.wconnect.workperformance.service.monthlyreport.ISTRReportService;
    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.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.Executor;

    /**
     * STR Report区域服务层
     *
     * @author lichen
     * @date 2023/9/4
     */
    @Service
    @DS("monthly_report")
    public class STRReportServiceImpl implements ISTRReportService {

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

        /**
         * 查询STR Report区域详情
         *
         * @param monthlyReport 月报
         * @param propertyId    资产id
         * @return cloud.wconnect.workperformance.pojo.dto.monthlyreport.strreport.STRReportDetailDTO
         * @author lichen
         * @date 2023/9/4
         */
        @Override
        public STRReportDetailDTO getSTRReport(MonthlyReport monthlyReport, Integer propertyId) {
            List<ReportAttachment> attachments = reportAttachmentService
                    .listByReportIdAndAreaId(propertyId,monthlyReport.getId(), AreaEnum.STR_REPORT.getAreaId());

            String remark = reportRemarkService.listByReportIdAndAreaId(monthlyReport.getId(), AreaEnum.STR_REPORT.getAreaId());
            return new STRReportDetailDTO(beanMapper.mapList(attachments, AttachmentDTO.class),remark);
        }

        /**
         * 保存STR Report区域数据
         *
         * @param monthlyReport      月报
         * @param propertyDTO        资产DTO
         * @param strReportDetailDTO 区域数据
         * @return void
         * @author lichen
         * @date 2023/9/4
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void save(MonthlyReport monthlyReport, PropertyDTO propertyDTO, STRReportDetailDTO strReportDetailDTO) {
            //查询保存前的附件key集合
            List<String> saveBeforeS3Keys = reportAttachmentService.listByReportIdAndAreaId(propertyDTO.getId(), monthlyReport.getId(), AreaEnum.STR_REPORT.getAreaId())
                    .stream().map(ReportAttachment::getFileKey).toList();

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

            //批量保存附件
            List<AttachmentDTO> attachments = strReportDetailDTO.getAttachments();
            List<String> saveAfterS3Keys = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(attachments)){
                List<ReportAttachment> reportAttachments = Lists.newArrayListWithCapacity(attachments.size());
                for (AttachmentDTO attachmentDTO : attachments) {
                    reportAttachments.add(ReportAttachment.builder()
                            .tenantId(propertyDTO.getTenantId())
                            .propertyId(propertyDTO.getId())
                            .reportId(monthlyReport.getId())
                            .areaId(AreaEnum.STR_REPORT.getAreaId())
                            .fileName(attachmentDTO.getFileName())
                            .fileKey(attachmentDTO.getFileKey())
                            .build());
                    saveAfterS3Keys.add(attachmentDTO.getFileKey());
                }
                reportAttachmentService.saveBatch(reportAttachments);
            }

            //保存备注
            ReportRemark reportRemark = ReportRemark.builder()
                    .tenantId(propertyDTO.getTenantId())
                    .propertyId(propertyDTO.getId())
                    .reportId(monthlyReport.getId())
                    .areaId(AreaEnum.STR_REPORT.getAreaId())
                    .remark(strReportDetailDTO.getRemark())
                    .build();
            reportRemarkMapper.saveOrUpdateByUnique(reportRemark);

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