Ver Fonte

统计月销量与月访问量

chao há 3 anos atrás
pai
commit
d73301f70a

+ 12 - 6
backup.sql

@@ -24,18 +24,24 @@ ALTER TABLE product
     MODIFY COLUMN visibility char(2) default '3' not null comment '商品可见度:3:所有人可见,2:普通机构可见,1:会员机构可见,4:仅医美机构可见' AFTER priceFlag;
 
 -- =================================== 2021年12月 小版本 start =====================================
+
 -- 1. 商品增加销量记录,可按月统计销量,便于搜索排序
+DROP TABLE IF EXISTS `cm_product_sales_record`;
 CREATE TABLE `cm_product_sales_record`(
      `id` INT NOT NULL AUTO_INCREMENT,
-     `productId` int(11) NOT NULL COMMENT '商品ID',
-     `productCount` int(11) DEFAULT NULL COMMENT '销量',
-     `addTime` DATETIME DEFAULT NULL COMMENT '销售时间',
+     `productId` INT(11) NOT NULL COMMENT '商品ID',
+     `sales` INT(11) DEFAULT NULL COMMENT '销量',
+     `saleTime` DATETIME DEFAULT NULL COMMENT '销售时间',
      PRIMARY KEY (`id`)
 ) ENGINE=INNODB CHARSET=utf8mb4 COMMENT='商品销量记录表';
+
+-- 2. 商品详情访问量记录,可按月统计,便于搜索排序
+DROP TABLE IF EXISTS `cm_product_views_record`;
 CREATE TABLE `cm_product_views_record`(
      `id` INT NOT NULL AUTO_INCREMENT,
-     `productId` int(11) NOT NULL COMMENT '商品ID',
-     `views` int(11) DEFAULT NULL COMMENT '访问量',
-     `countTime` DATETIME DEFAULT NULL COMMENT '统计日期',
+     `productId` INT(11) NOT NULL COMMENT '商品ID',
+     `views` INT(11) DEFAULT NULL COMMENT '访问量',
+     `viewTime` DATETIME DEFAULT NULL COMMENT '访问日期',
      PRIMARY KEY (`id`)
 ) ENGINE=INNODB CHARSET=utf8mb4 COMMENT='商品详情访问量记录表';
+

+ 1 - 7
src/main/java/com/caimei365/commodity/annotation/StatisticsAspect.java

@@ -10,11 +10,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
 import org.springframework.data.redis.core.RedisCallback;
 import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.expression.EvaluationContext;
-import org.springframework.expression.Expression;
-import org.springframework.expression.ExpressionParser;
-import org.springframework.expression.spel.standard.SpelExpressionParser;
-import org.springframework.expression.spel.support.StandardEvaluationContext;
 import org.springframework.stereotype.Component;
 import redis.clients.jedis.commands.JedisCommands;
 
@@ -23,7 +18,6 @@ import java.lang.reflect.Method;
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Date;
-import java.util.Objects;
 
 /**
  * 统计切面
@@ -58,8 +52,8 @@ public class StatisticsAspect {
         String prefix = statistics.prefix();
         String field = statistics.field();
         int expire = statistics.expire();
-        boolean contains = Arrays.asList(parameters).contains(field);
         String fieldKey = null;
+        boolean contains = null != parameters && Arrays.asList(parameters).contains(field);
         if (contains) {
             for (int i = 0; i < parameters.length; i++) {
                 if (parameters[i] != null && parameters[i].equals(field)) {

+ 2 - 2
src/main/java/com/caimei365/commodity/mapper/PriceMapper.java

@@ -72,7 +72,7 @@ public interface PriceMapper {
     /**
      * 添加销量记录
      * @param productId 商品Id
-     * @param productCount 销量
+     * @param sales 销量
      */
-    void insertProductSalesRecord(Integer productId, Integer productCount);
+    void insertProductSalesRecord(Integer productId, Integer sales);
 }

+ 5 - 0
src/main/java/com/caimei365/commodity/mapper/SearchMapper.java

@@ -248,4 +248,9 @@ public interface SearchMapper {
     List<Integer> findPromotion(List<Integer> productIds);
 
     List<Integer> findPromotionId(Integer p_id);
+    /**
+     * 统计月销量与月访问量
+     */
+    int countViewsByDate(Integer productId, String date);
+    int countSalesByDate(Integer productId, String date);
 }

+ 3 - 3
src/main/java/com/caimei365/commodity/service/impl/PriceServiceImpl.java

@@ -131,14 +131,14 @@ public class PriceServiceImpl implements PriceService {
         for (Object productObject : infoArr) {
             JSONObject productTemp = (JSONObject) productObject;
             Integer productId = (Integer) productTemp.get("productId");
-            Integer productCount = (Integer) productTemp.get("productCount");
+            Integer sales = (Integer) productTemp.get("productCount");
             // 获取商品销量信息
             ProductSalesVo salesVo = priceMapper.getProductSalesInfo(productId);
             if (null == salesVo || ObjectUtils.isEmpty(salesVo)) {
                 return ResponseJson.error("商品数据异常!", null);
             }
             int dbCount = (null == salesVo.getProductCount()) ? 0 : salesVo.getProductCount();
-            salesVo.setProductCount(dbCount + productCount);
+            salesVo.setProductCount(dbCount + sales);
             // 获取供应商名称
             String shopName = shopMapper.getShopNameByShopId(salesVo.getShopId());
             salesVo.setShopName(shopName);
@@ -157,7 +157,7 @@ public class PriceServiceImpl implements PriceService {
                 priceMapper.updateProductSales(salesVo);
             }
             // 添加销量记录
-            priceMapper.insertProductSalesRecord(productId, productCount);
+            priceMapper.insertProductSalesRecord(productId, sales);
             log.info("【更新商品销量】:" + salesVo.toString());
         }
         return ResponseJson.success(null);

+ 16 - 5
src/main/java/com/caimei365/commodity/service/impl/SearchIndexServiceImpl.java

@@ -23,9 +23,10 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
+import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
+import java.time.temporal.ChronoUnit;
+import java.util.*;
 
 /**
  * Description
@@ -109,7 +110,7 @@ public class SearchIndexServiceImpl implements SearchIndexService {
         // 获取最大文档ID
         Integer mainId = searchQueryService.getIdByDocId("", null);
         mainId = mainId == -1 ? 0 : mainId;
-        // 获取推送记录数
+        // 设置商品文档 并返回推送记录数
         Integer productRecord = setAllProductData(mainId, productCount, false);
         if (productCount > productRecord) {
             log.warn("批量添加商品文档异常,部分商品未添加。");
@@ -1131,12 +1132,22 @@ public class SearchIndexServiceImpl implements SearchIndexService {
         product.setP_all(1);
         // 价格等级
         product.setP_price_grade(priceUtilService.getPriceGrade(product.getP_price().doubleValue()));
-        DocumentDTO<ProductDO> productDoc = new DocumentDTO<>();
+        // 统计月销量与月访问量
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(new Date());
+        calendar.add(Calendar.DAY_OF_MONTH, -30);
+        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
+        int views = searchMapper.countViewsByDate(product.getP_id(), dateStr);
+        int sales = searchMapper.countSalesByDate(product.getP_id(), dateStr);
+        product.setP_sales(sales);
+        product.setP_favorite(views);
         // 设促销id
         List<Integer> promotionIds = searchMapper.findPromotionId(product.getP_id());
         if (promotionIds != null && promotionIds.size() > 0) {
             product.setP_promotions_id(promotionIds.get(0));
         }
+        // 文档设值
+        DocumentDTO<ProductDO> productDoc = new DocumentDTO<>();
         productDoc.setCmd("add");
         productDoc.setFields(product);
         productList.add(productDoc);

+ 2 - 2
src/main/resources/mapper/PriceMapper.xml

@@ -130,8 +130,8 @@
         VALUES (#{productId}, #{productName}, #{shopName}, #{productCount}, #{property})
     </insert>
     <insert id="insertProductSalesRecord">
-        INSERT INTO cm_product_sales_record(productId, productCount, addTime)
-        VALUES (#{productId}, #{productCount}, NOW())
+        INSERT INTO cm_product_sales_record(productId, sales, saleTime)
+        VALUES (#{productId}, #{sales}, NOW())
     </insert>
     <update id="updateProductSales" parameterType="com.caimei365.commodity.model.vo.ProductSalesVo">
         UPDATE cm_product_sales

+ 6 - 0
src/main/resources/mapper/SearchMapper.xml

@@ -545,4 +545,10 @@
         order by pr.type desc
         limit 1
     </select>
+    <select id="countViewsByDate" resultType="java.lang.Integer">
+        select sum(views) from cm_product_views_record where productId = #{productId} and viewTime > #{date}
+    </select>
+    <select id="countSalesByDate" resultType="java.lang.Integer">
+        select sum(sales) from cm_product_sales_record where productId = #{productId} and saleTime > #{date}
+    </select>
 </mapper>