Prechádzať zdrojové kódy

Merge branch 'sitemap' of wangfan/adweb3-server into master

wangfan 5 mesiacov pred
rodič
commit
45ff41ab94

+ 5 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/common/util/NumberUtil.java

@@ -31,4 +31,9 @@ public class NumberUtil {
 
         return PERCENTAGE_FORMAT.format(number);
     }
+
+    /** 安全除法,当分母为0返回0d,不抛出{@link ArithmeticException} */
+    public static double safeDivide(double x, int y) {
+        return y == 0 ? 0d : x / y;
+    }
 }

+ 1 - 3
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/impl/GACountryReportServiceImpl.java

@@ -39,9 +39,7 @@ public class GACountryReportServiceImpl extends ServiceImpl<GACountryReportMappe
         for (CountryStatsVO countryStatsVO : countryStatsVOs) {
             countryStatsVO.setTotalUsersProportion(
                     NumberUtil.formatPercentage(
-                            totalUsersSum == 0
-                                    ? 0
-                                    : (double) countryStatsVO.getTotalUsers() / totalUsersSum,
+                            NumberUtil.safeDivide(countryStatsVO.getTotalUsers(), totalUsersSum),
                             2));
         }
 

+ 23 - 17
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/impl/GADailyReportServiceImpl.java

@@ -2,23 +2,23 @@ package org.jeecg.modules.adweb.dmp.service.impl;
 
 import static org.jeecg.modules.adweb.dmp.vo.report.SiteOverviewStatsVO.DailyStatsVO;
 
+import cn.hutool.core.lang.Pair;
+
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 
-import org.apache.commons.compress.utils.Lists;
 import org.jeecg.common.util.DateUtils;
 import org.jeecg.modules.adweb.common.util.DateUtil;
 import org.jeecg.modules.adweb.dmp.entity.GADailyReport;
 import org.jeecg.modules.adweb.dmp.mapper.GADailyReportMapper;
 import org.jeecg.modules.adweb.dmp.service.IGADailyReportService;
+import org.jeecg.modules.adweb.enquiry.mapper.AdwebEnquiryMapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.Comparator;
-import java.util.Date;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -28,7 +28,7 @@ import java.util.stream.Collectors;
 public class GADailyReportServiceImpl extends ServiceImpl<GADailyReportMapper, GADailyReport>
         implements IGADailyReportService {
 
-    @Autowired private GADailyReportMapper gaDailyReportMapper;
+    @Autowired private AdwebEnquiryMapper adwebEnquiryMapper;
 
     /**
      * 查询指定时间区间内的{@DailyStatsVO}
@@ -36,7 +36,7 @@ public class GADailyReportServiceImpl extends ServiceImpl<GADailyReportMapper, G
      * <p>如数据库中某天数据缺失,需填充空数据
      */
     public List<DailyStatsVO> getDailyStatsWithinPeriod(String siteCode, Date start, Date end) {
-        List<DailyStatsVO> dailyStatsVOs = Lists.newArrayList();
+        Map<String, DailyStatsVO> dailyStatsVOs = Maps.newHashMap();
 
         List<GADailyReport> gaDailyReports =
                 this.list(
@@ -49,19 +49,18 @@ public class GADailyReportServiceImpl extends ServiceImpl<GADailyReportMapper, G
         // 1. VO数据填充
         for (GADailyReport gaDailyReport : gaDailyReports) {
             DailyStatsVO dailyStatsVO = new DailyStatsVO();
-            dailyStatsVO.setDate(
-                    DateUtils.date2Str(gaDailyReport.getDate(), DateUtils.date_sdf.get()));
+            String date = DateUtils.date2Str(gaDailyReport.getDate(), DateUtils.date_sdf.get());
+            dailyStatsVO.setDate(date);
             dailyStatsVO.setTotalUsers(gaDailyReport.getTotalUsers());
             dailyStatsVO.setSessions(gaDailyReport.getSessions());
             dailyStatsVO.setBounceRate(gaDailyReport.getBounceRate());
             dailyStatsVO.setAvgSessionDuration(gaDailyReport.getAvgSessionDuration());
             dailyStatsVO.setPageViews(gaDailyReport.getPageViews());
             dailyStatsVO.setPageViewsPerSession(gaDailyReport.getPageViewsPerSession());
-
             // TODO: 待后续步骤设置
-            dailyStatsVO.setEnquires(0);
+            //  dailyStatsVO.setEnquires(0);
 
-            dailyStatsVOs.add(dailyStatsVO);
+            dailyStatsVOs.put(date, dailyStatsVO);
         }
 
         // 2. 缺失日期补充
@@ -70,18 +69,25 @@ public class GADailyReportServiceImpl extends ServiceImpl<GADailyReportMapper, G
                         DateUtil.getAllDaysBetween(start, end).stream()
                                 .map(date -> DateUtils.date2Str(date, DateUtils.date_sdf.get()))
                                 .collect(Collectors.toSet()),
-                        dailyStatsVOs.stream()
-                                .map(DailyStatsVO::getDate)
-                                .collect(Collectors.toSet()));
+                        dailyStatsVOs.keySet());
         absentDays.forEach(
                 date -> {
                     DailyStatsVO dailyStatsVO = new DailyStatsVO();
                     dailyStatsVO.setDate(date); // 其它数值字段设置为默认值
-                    dailyStatsVOs.add(dailyStatsVO);
+                    dailyStatsVOs.put(date, dailyStatsVO);
                 });
 
+        // 3. 询盘数据补充
+        List<Pair<String, Integer>> enquiryDailyCounts =
+                adwebEnquiryMapper.getEnquiryDailyCounts(siteCode, start, end);
+        enquiryDailyCounts.forEach(
+                enquiryDailyCount ->
+                        dailyStatsVOs
+                                .get(enquiryDailyCount.getKey())
+                                .setEnquires(enquiryDailyCount.getValue()));
+
         // 3. 根据日期排序并返回
-        return dailyStatsVOs.stream()
+        return dailyStatsVOs.values().stream()
                 .sorted(Comparator.comparing(DailyStatsVO::getDate)) // 排日期升序排序
                 .toList();
     }

+ 1 - 2
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/impl/GAPagePathReportServiceImpl.java

@@ -54,8 +54,7 @@ public class GAPagePathReportServiceImpl
             pagePathStatsVO.setPagePath(siteUrl + pagePathStatsVO.getPagePath());
             pagePathStatsVO.setPvProportion(
                     NumberUtil.formatPercentage(
-                            totalPVs == 0 ? 0 : (double) pagePathStatsVO.getPageViews() / totalPVs,
-                            2));
+                            NumberUtil.safeDivide(pagePathStatsVO.getPageViews(), totalPVs), 2));
         }
 
         return pagePathStatsVOs;

+ 2 - 4
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/impl/GASourceMediumReportServiceImpl.java

@@ -41,13 +41,11 @@ public class GASourceMediumReportServiceImpl
             int totalUsers = sourceMediumStatsVO.getTotalUsers();
             sourceMediumStatsVO.setTotalUsersProportion(
                     NumberUtil.formatPercentage(
-                            totalUsersSum == 0 ? 0 : (double) totalUsers / totalUsersSum, 2));
+                            NumberUtil.safeDivide(totalUsers, totalUsersSum), 2));
 
             sourceMediumStatsVO.setNewUsersRatio(
                     NumberUtil.formatPercentage(
-                            totalUsers == 0
-                                    ? 0
-                                    : (double) sourceMediumStatsVO.getNewUsers() / totalUsers,
+                            NumberUtil.safeDivide(sourceMediumStatsVO.getNewUsers(), totalUsers),
                             2));
         }
 

+ 12 - 8
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/vo/report/SiteOverviewStatsVO.java

@@ -93,24 +93,28 @@ public class SiteOverviewStatsVO {
                                                 * dailyStatsVO.getSessions())
                         .sum();
         siteOverviewStatsVO.setAvgTimeOnPage(
-                NumberUtil.formatDecimal(sessions == 0 ? 0 : sessionDurationSum / sessions, 2)
+                NumberUtil.formatDecimal(NumberUtil.safeDivide(sessionDurationSum, sessions), 2)
                         .doubleValue());
 
         // 2.3 平均访问页面数
         siteOverviewStatsVO.setPageViewsPerSession(
-                NumberUtil.formatDecimal(sessions == 0 ? 0 : (double) pageViews / sessions, 2)
+                NumberUtil.formatDecimal(NumberUtil.safeDivide(pageViews, sessions), 2)
                         .doubleValue());
 
         // 2.4 跳出率
-        // -- SUM (bounceRate)
-        double bounceRateSum =
-                dailyStatsVOs.stream().mapToDouble(DailyStatsVO::getBounceRate).sum();
-        siteOverviewStatsVO.setBounceRate(NumberUtil.formatPercentage(bounceRateSum / numDays, 2));
+        // -- SUM (bounceRate * * sessions)
+        double bounceSum =
+                dailyStatsVOs.stream()
+                        .mapToDouble(
+                                dailyStatsVO ->
+                                        dailyStatsVO.getBounceRate() * dailyStatsVO.getSessions())
+                        .sum();
+        siteOverviewStatsVO.setBounceRate(
+                NumberUtil.formatPercentage(NumberUtil.safeDivide(bounceSum, sessions), 2));
 
         // 2.5 UV到询盘转化率
         siteOverviewStatsVO.setEnquiryConversionRate(
-                NumberUtil.formatPercentage(
-                        totalUsers == 0 ? 0 : (double) enquires / totalUsers, 2));
+                NumberUtil.formatPercentage(NumberUtil.safeDivide(enquires, totalUsers), 2));
 
         siteOverviewStatsVO.setDailyStats(dailyStatsVOs);
 

+ 15 - 4
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/enquiry/mapper/AdwebEnquiryMapper.java

@@ -1,14 +1,17 @@
 package org.jeecg.modules.adweb.enquiry.mapper;
 
-import java.util.Date;
-import java.util.List;
+import cn.hutool.core.lang.Pair;
 
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+
 import org.apache.ibatis.annotations.Param;
 import org.jeecg.modules.adweb.enquiry.dto.param.EnquirySearchDto;
 import org.jeecg.modules.adweb.enquiry.dto.result.EnquiryListDto;
 import org.jeecg.modules.adweb.enquiry.entity.AdwebEnquiry;
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+import java.util.Date;
+import java.util.List;
 
 /**
  * @Description: 询盘信息存储表单
@@ -65,5 +68,13 @@ public interface AdwebEnquiryMapper extends BaseMapper<AdwebEnquiry> {
 
     List<EnquiryListDto> getWastedEnquiry(String wasteEnquiryType, String ip, String email, String keyword, String searchText, String type, Integer siteId);
 
-
+    /**
+     * 获取每日询盘数量
+     *
+     * @param siteCode
+     * @param start
+     * @param end
+     * @return
+     */
+    List<Pair<String, Integer>> getEnquiryDailyCounts(String siteCode, Date start, Date end);
 }

+ 20 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/enquiry/mapper/xml/AdwebEnquiryMapper.xml

@@ -303,4 +303,24 @@
         and t1.principal_type != 1
     </update>
 
+
+    <select id="getEnquiryDailyCounts" resultType="cn.hutool.core.lang.Pair">
+        SELECT
+        date_format(record_ctime, '%Y-%m-%d') `date`,
+        count(id) enquires
+        FROM
+        adweb_enquiry
+        WHERE
+        `status` != 0
+        AND user_effective != 0
+        AND site_code = #{siteCode}
+        <if test="start != null">
+            AND record_ctime >= #{start}
+        </if>
+        <if test="end != null">
+            AND #{end} > record_ctime
+        </if>
+        GROUP BY date
+    </select>
+
 </mapper>