# GmwrStrServiceImpl.java

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

    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.workperformance.cons.S3BucketFunctionCons;
    import cloud.wconnect.workperformance.entity.gmwr.GmwrStr;
    import cloud.wconnect.workperformance.mapper.gmwr.GmwrStrMapper;
    import cloud.wconnect.workperformance.pojo.bo.gmwr.str.GmwrStrDataBO;
    import cloud.wconnect.workperformance.service.AmazonS3Service;
    import cloud.wconnect.workperformance.service.gmwr.IGmwrStrService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import jakarta.annotation.Resource;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Service;

    import java.io.IOException;
    import java.io.InputStream;

    /**
     * <p>
     * GM Weekly Report STR 原始数据 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-10-18
     */
    @Slf4j
    @Service
    @DS("work_performance")
    public class GmwrStrServiceImpl extends ServiceImpl<GmwrStrMapper, GmwrStr> implements IGmwrStrService {

        @Resource
        private ObjectMapper objectMapper;
        @Resource
        private AmazonS3Service amazonS3Service;

        /**
         * 获取最新的 Str 数据
         *
         * @param propertyId 资产 id
         * @return GmwrStr
         * @author zc
         * @date 2024/10/18
         */
        @Override
        public GmwrStr getNewOne(int propertyId) {
            return getOne(new LambdaQueryWrapper<GmwrStr>()
                    .eq(GmwrStr::getPropertyId, propertyId)
                    .orderByDesc(GmwrStr::getDataDate)
                    .last("limit 1"));
        }

        /**
         * 处理报告数据
         *
         * @param propertyDTO 资产信息
         * @param bo          数据
         * @param path        文件路径
         * @author zc
         * @date 2024/10/18
         */
        @Override
        public void handleStrWeekReport(PropertyDTO propertyDTO, GmwrStrDataBO bo, String path) {
            // 上传文件到 S3
            uploadReport(path);

            // 如果数据不存在则创建新对象
            GmwrStr gmwrStr = new GmwrStr();
            gmwrStr.setPropertyId(propertyDTO.getId());
            gmwrStr.setDataDate(bo.getDataDate());
            gmwrStr.setFileUrl(path);
            gmwrStr.setDetail(JsonUtils.convertObjListToString(objectMapper, bo.getList()));
            gmwrStr.setUpdatedTime(DateTimeUtils.nowTimestamp());

            baseMapper.insertOrUpdateByUnique(gmwrStr);
        }

        /**
         * 上传报告，用于点击指标下载报告
         *
         * @param fileUrl S3 路径
         * @author zc
         * @date 2025/8/22
         */
        private void uploadReport(String fileUrl) {
            // 上传报告，用于日志下载报告
            try (InputStream in = amazonS3Service.getFile(fileUrl)) {
                amazonS3Service.uploadFile(S3BucketFunctionCons.GM_WEEKLY_REPORT, in, fileUrl);
            } catch (IOException e) {
                log.error("GmwrStrDataServiceImpl Upload Str report error. fileUrl: {}", fileUrl, e);
            }
        }
    }