huangxiao 1 місяць тому
батько
коміт
45305a1b0a
1 змінених файлів з 67 додано та 4 видалено
  1. 67 4
      src/main/java/net/mingsoft/tf/www/BrowseAction.java

+ 67 - 4
src/main/java/net/mingsoft/tf/www/BrowseAction.java

@@ -1,23 +1,86 @@
 package net.mingsoft.tf.www;
 
+import cn.hutool.core.date.DatePattern;
+import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import net.mingsoft.base.entity.ResultData;
+import net.mingsoft.tf.biz.IBrowseBiz;
+import net.mingsoft.tf.biz.IBrowseRecordsBiz;
+import net.mingsoft.tf.entity.BrowseEntity;
 import net.mingsoft.tf.entity.BrowseRecordsEntity;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;
 
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Set;
+
 @Controller
 @RequestMapping("/tf/browse")
 public class BrowseAction {
+    @Autowired
+    private IBrowseRecordsBiz browseRecordsBiz;
+    @Autowired
+    private IBrowseBiz browseBiz;
+    private static Set<String> day = new HashSet<>();
+
     /**
      * 记录访问量
      */
-    @PostMapping(value = "/visits")
+    @RequestMapping(value = "/visits", method = {RequestMethod.GET, RequestMethod.POST})
     @ResponseBody
-    public ResultData visits(@RequestBody BrowseRecordsEntity browseRecords) {
-        // TODO 记录访问量和访问历史,起线程
+    public ResultData visits(@RequestBody BrowseRecordsEntity entity) {
+        saveBrowseRecordsEntity(entity);
+        incrementDailyVisits();
+        incrementVisits(entity);
         return ResultData.build().success();
     }
+
+    private void saveBrowseRecordsEntity(BrowseRecordsEntity entity) {
+        browseRecordsBiz.save(entity);
+        browseRecordsBiz.update("DELETE FROM mdiy_form_browse_records WHERE id NOT IN (SELECT id FROM (SELECT id FROM mdiy_form_browse_records ORDER BY create_date DESC LIMIT ?) AS tmp)", 30);
+    }
+
+    private void incrementDailyVisits() {
+        Date now = new Date();
+        String time = DatePattern.NORM_DATE_FORMAT.format(now);
+        if (day.contains(time)) {
+            browseBiz.update("update mdiy_form_browse set BROWSE_VISITS = BROWSE_VISITS + 1 where BROWSE_DATA = now()");
+        } else {
+            BrowseEntity browseEntity = browseBiz.getOne(new LambdaQueryWrapper<>(BrowseEntity.class).eq(BrowseEntity::getBrowseData, time));
+            if (browseEntity == null) {
+                browseEntity = new BrowseEntity();
+                browseEntity.setBrowseData(now);
+                browseEntity.setBrowseVisits(1);
+            } else {
+                browseEntity.setBrowseVisits(browseEntity.getBrowseVisits() + 1);
+            }
+            browseBiz.saveOrUpdate(browseEntity);
+            day.add(time);
+        }
+    }
+
+    private void incrementVisits(BrowseRecordsEntity entity) {
+        String sql = "";
+        String id = null;
+        if (StrUtil.isNotBlank(entity.getRecordsEnterpriseId())) {
+            id = entity.getRecordsEnterpriseId();
+            sql = "update mdiy_form_enterprise set ENTERPRISE_VISITS = ifnull(ENTERPRISE_VISITS, 0) + 1 where id = ?";
+        }
+        if (StrUtil.isNotBlank(entity.getRecordsDesignId())) {
+            id = entity.getRecordsDesignId();
+            sql = "update mdiy_form_design set DESIGN_VISITS = ifnull(DESIGN_VISITS, 0) + 1 where id = ?";
+        }
+        if (StrUtil.isNotBlank(entity.getRecordsProductsId())) {
+            id = entity.getRecordsProductsId();
+            sql = "update mdiy_form_enterprise_products set PRODUCT_VISITS = ifnull(PRODUCT_VISITS, 0) + 1 where id = ?";
+        }
+        if (StrUtil.isNotBlank(sql) && StrUtil.isNotBlank(id)) {
+            browseBiz.update(sql, id);
+        }
+    }
 }