|
@@ -1,5 +1,6 @@
|
|
|
package org.jeecg.modules.adweb.dmp.service.google;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
@@ -11,6 +12,8 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.commons.lang3.reflect.TypeUtils;
|
|
|
import org.jeecg.common.util.DateUtils;
|
|
|
import org.jeecg.common.util.FastJsonUtil;
|
|
|
+import org.jeecg.modules.adweb.common.mapper.CommonMapper;
|
|
|
+import org.jeecg.modules.adweb.common.util.DateUtil;
|
|
|
import org.jeecg.modules.adweb.common.util.RestTemplateUtil;
|
|
|
import org.jeecg.modules.adweb.dmp.dto.OpenAPIRequest;
|
|
|
import org.jeecg.modules.adweb.dmp.dto.OpenAPIResponse;
|
|
@@ -29,7 +32,9 @@ import org.springframework.core.ParameterizedTypeReference;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* Google Analytics数据同步 - http://data-bridge.v3.adwebcloud.com:9002/swagger-ui/index.html
|
|
@@ -50,6 +55,7 @@ public class GAReportService {
|
|
|
|
|
|
@Autowired private IGACountryReportService gaCountryReportService;
|
|
|
|
|
|
+ @Autowired private CommonMapper commonMapper;
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
@PostConstruct
|
|
@@ -63,14 +69,20 @@ public class GAReportService {
|
|
|
* @param googleGTM
|
|
|
*/
|
|
|
void queryGACountryDailyReport(GoogleGTM googleGTM) {
|
|
|
- // 1. 构建报表请求参数
|
|
|
+ // 1. 报表时间范围
|
|
|
+ Date startDate =
|
|
|
+ this.getReportStartDate(
|
|
|
+ "dmp_ga_country_report", "date", "site_id = " + googleGTM.getSiteId());
|
|
|
+ Date endDate = new Date();
|
|
|
+
|
|
|
+ // 2. 构建GA报表请求参数
|
|
|
// 使用ADWEB_CUSTOM_REPORT而不是ADWEB_COUNTRY_CHART,因为需要date作为dimension
|
|
|
GAReportRequestDTO gaReportRequest = new GAReportRequestDTO();
|
|
|
gaReportRequest.setPropertyResourceName(
|
|
|
toGAPropertyResourceName(googleGTM.getGaPropertyId()));
|
|
|
gaReportRequest.setReportType(ReportType.ADWEB_CUSTOM_REPORT);
|
|
|
- gaReportRequest.setStartDate("2024-01-01");
|
|
|
- gaReportRequest.setEndDate("2024-10-11");
|
|
|
+ gaReportRequest.setStartDate(DateUtils.date2Str(startDate, DateUtils.date_sdf.get()));
|
|
|
+ gaReportRequest.setEndDate(DateUtils.date2Str(endDate, DateUtils.date_sdf.get()));
|
|
|
gaReportRequest.setMetrics(List.of(ReportConstant.METRIC_TOTAL_USERS));
|
|
|
gaReportRequest.setDimensions(
|
|
|
List.of(ReportConstant.DIMENSION_DATE, ReportConstant.DIMENSION_COUNTRY));
|
|
@@ -95,9 +107,14 @@ public class GAReportService {
|
|
|
countryReport.add(reportRow);
|
|
|
}
|
|
|
|
|
|
- // 3. 保存数据库
|
|
|
- // TODO - 重复数据
|
|
|
- gaCountryReportService.saveOrUpdateBatch(countryReport);
|
|
|
+ // 3. 更新数据库 - 删除旧数据,插入新数据
|
|
|
+ QueryWrapper<GACountryReport> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("site_id", googleGTM.getSiteId());
|
|
|
+ queryWrapper.ge("date", startDate);
|
|
|
+ queryWrapper.le("date", endDate);
|
|
|
+ gaCountryReportService.remove(queryWrapper);
|
|
|
+
|
|
|
+ gaCountryReportService.saveBatch(countryReport);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -137,6 +154,23 @@ public class GAReportService {
|
|
|
return openAPIResponse.getData();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 计算GA报表开始时间
|
|
|
+ *
|
|
|
+ * <p>1. 若表中没有数据,取一年前
|
|
|
+ *
|
|
|
+ * <p>2. 表中最大时间减一天 - 如果10月10号凌晨2点执行,最大时间可能是10号,但9号数据GA侧有更新
|
|
|
+ */
|
|
|
+ private Date getReportStartDate(String tableName, String fieldName, String filter) {
|
|
|
+ Date maxDate = commonMapper.getMaxDate(tableName, fieldName, filter);
|
|
|
+ if (Objects.isNull(maxDate)) {
|
|
|
+ // 1. 一年前
|
|
|
+ return DateUtil.plusDays(new Date(), -365);
|
|
|
+ }
|
|
|
+ // 2. 最大时间减一天
|
|
|
+ return DateUtil.plusDays(maxDate, -1);
|
|
|
+ }
|
|
|
+
|
|
|
private final String toGAPropertyResourceName(String gaPropertyId) {
|
|
|
return StringUtils.isNumeric(gaPropertyId) ? "properties/" + gaPropertyId : gaPropertyId;
|
|
|
}
|