Ver Fonte

Merge branch 'feature_fuwuhao' of Server/long-article-recommend into master

dingyunpeng há 10 meses atrás
pai
commit
0110e40689

+ 61 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/remote/WxFetchRemoteService.java

@@ -0,0 +1,61 @@
+package com.tzld.longarticle.recommend.server.remote;
+
+import com.alibaba.fastjson.JSONObject;
+import com.tzld.longarticle.recommend.server.common.HttpPoolFactory;
+import com.tzld.longarticle.recommend.server.model.Content;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.HttpEntity;
+import org.apache.http.StatusLine;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
+import org.springframework.stereotype.Service;
+
+import java.nio.charset.StandardCharsets;
+import java.util.*;
+
+/**
+ * @author dyp
+ */
+@Service
+@Slf4j
+public class WxFetchRemoteService {
+    private final CloseableHttpClient client = HttpPoolFactory.aigcPool();
+    private static final String url = "http://8.217.190.241:8888/crawler/wei_xin/detail";
+
+
+    public Map<String, String> getContent(String contentUrl) {
+        Map<String, String> result = new HashMap<>();
+        JSONObject bodyParam = new JSONObject();
+        bodyParam.put("content_link", contentUrl);
+        bodyParam.put("is_count", true);
+        bodyParam.put("is_cache", false);
+        try {
+            HttpPost httpPost = new HttpPost(url);
+            StringEntity stringEntity = new StringEntity(bodyParam.toJSONString(), StandardCharsets.UTF_8);
+            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
+            httpPost.setEntity(stringEntity);
+            CloseableHttpResponse response = client.execute(httpPost);
+            StatusLine statusLine = response.getStatusLine();
+            if (statusLine.getStatusCode() == 200) {
+                HttpEntity responseEntity = response.getEntity();
+                if (Objects.nonNull(responseEntity)) {
+                    String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
+                    JSONObject jsonObject = JSONObject.parseObject(responseBody);
+                    if (jsonObject.getInteger("code") == 0) {
+                        JSONObject data = jsonObject.getJSONObject("data");
+                        data = data.getJSONObject("data");
+                        result.put("view_count", data.getString("view_count"));
+                        result.put("title", data.getString("title"));
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.error("getViewCount error", e);
+        }
+        return result;
+    }
+
+}

+ 0 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/mapper/crawler/ArticleContentLinkMapper.java

@@ -2,7 +2,6 @@ package com.tzld.longarticle.recommend.server.repository.mapper.crawler;
 
 import com.tzld.longarticle.recommend.server.repository.model.ArticleContentLink;
 import com.tzld.longarticle.recommend.server.repository.model.ArticleContentLinkExample;
-
 import java.util.List;
 
 import org.apache.ibatis.annotations.Mapper;

+ 30 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/model/ArticleContentLink.java

@@ -11,6 +11,12 @@ public class ArticleContentLink {
 
     private Long createTime;
 
+    private String title;
+
+    private Long viewCount;
+
+    private Long updateTime;
+
     public Long getId() {
         return id;
     }
@@ -50,4 +56,28 @@ public class ArticleContentLink {
     public void setCreateTime(Long createTime) {
         this.createTime = createTime;
     }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title == null ? null : title.trim();
+    }
+
+    public Long getViewCount() {
+        return viewCount;
+    }
+
+    public void setViewCount(Long viewCount) {
+        this.viewCount = viewCount;
+    }
+
+    public Long getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(Long updateTime) {
+        this.updateTime = updateTime;
+    }
 }

+ 190 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/model/ArticleContentLinkExample.java

@@ -423,6 +423,196 @@ public class ArticleContentLinkExample {
             addCriterion("create_time not between", value1, value2, "createTime");
             return (Criteria) this;
         }
+
+        public Criteria andTitleIsNull() {
+            addCriterion("title is null");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleIsNotNull() {
+            addCriterion("title is not null");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleEqualTo(String value) {
+            addCriterion("title =", value, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleNotEqualTo(String value) {
+            addCriterion("title <>", value, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleGreaterThan(String value) {
+            addCriterion("title >", value, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleGreaterThanOrEqualTo(String value) {
+            addCriterion("title >=", value, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleLessThan(String value) {
+            addCriterion("title <", value, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleLessThanOrEqualTo(String value) {
+            addCriterion("title <=", value, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleLike(String value) {
+            addCriterion("title like", value, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleNotLike(String value) {
+            addCriterion("title not like", value, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleIn(List<String> values) {
+            addCriterion("title in", values, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleNotIn(List<String> values) {
+            addCriterion("title not in", values, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleBetween(String value1, String value2) {
+            addCriterion("title between", value1, value2, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andTitleNotBetween(String value1, String value2) {
+            addCriterion("title not between", value1, value2, "title");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountIsNull() {
+            addCriterion("view_count is null");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountIsNotNull() {
+            addCriterion("view_count is not null");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountEqualTo(Long value) {
+            addCriterion("view_count =", value, "viewCount");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountNotEqualTo(Long value) {
+            addCriterion("view_count <>", value, "viewCount");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountGreaterThan(Long value) {
+            addCriterion("view_count >", value, "viewCount");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountGreaterThanOrEqualTo(Long value) {
+            addCriterion("view_count >=", value, "viewCount");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountLessThan(Long value) {
+            addCriterion("view_count <", value, "viewCount");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountLessThanOrEqualTo(Long value) {
+            addCriterion("view_count <=", value, "viewCount");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountIn(List<Long> values) {
+            addCriterion("view_count in", values, "viewCount");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountNotIn(List<Long> values) {
+            addCriterion("view_count not in", values, "viewCount");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountBetween(Long value1, Long value2) {
+            addCriterion("view_count between", value1, value2, "viewCount");
+            return (Criteria) this;
+        }
+
+        public Criteria andViewCountNotBetween(Long value1, Long value2) {
+            addCriterion("view_count not between", value1, value2, "viewCount");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeIsNull() {
+            addCriterion("update_time is null");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeIsNotNull() {
+            addCriterion("update_time is not null");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeEqualTo(Long value) {
+            addCriterion("update_time =", value, "updateTime");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeNotEqualTo(Long value) {
+            addCriterion("update_time <>", value, "updateTime");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeGreaterThan(Long value) {
+            addCriterion("update_time >", value, "updateTime");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeGreaterThanOrEqualTo(Long value) {
+            addCriterion("update_time >=", value, "updateTime");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeLessThan(Long value) {
+            addCriterion("update_time <", value, "updateTime");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeLessThanOrEqualTo(Long value) {
+            addCriterion("update_time <=", value, "updateTime");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeIn(List<Long> values) {
+            addCriterion("update_time in", values, "updateTime");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeNotIn(List<Long> values) {
+            addCriterion("update_time not in", values, "updateTime");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeBetween(Long value1, Long value2) {
+            addCriterion("update_time between", value1, value2, "updateTime");
+            return (Criteria) this;
+        }
+
+        public Criteria andUpdateTimeNotBetween(Long value1, Long value2) {
+            addCriterion("update_time not between", value1, value2, "updateTime");
+            return (Criteria) this;
+        }
     }
 
     public static class Criteria extends GeneratedCriteria {

+ 61 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/JobService.java

@@ -0,0 +1,61 @@
+package com.tzld.longarticle.recommend.server.service;
+
+import com.tzld.longarticle.recommend.server.common.ThreadPoolFactory;
+import com.tzld.longarticle.recommend.server.remote.WxFetchRemoteService;
+import com.tzld.longarticle.recommend.server.repository.mapper.crawler.ArticleContentLinkMapper;
+import com.tzld.longarticle.recommend.server.repository.model.ArticleContentLink;
+import com.tzld.longarticle.recommend.server.repository.model.ArticleContentLinkExample;
+import com.tzld.longarticle.recommend.server.util.DateUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.math.NumberUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDate;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author dyp
+ */
+@Service
+@Slf4j
+public class JobService {
+
+    @Autowired
+    private ArticleContentLinkMapper articleContentLinkMapper;
+    @Autowired
+    private WxFetchRemoteService wxFetchRemoteService;
+
+    private long seconds1d = 60 * 60 * 24 * 1000;
+    private long seconds7d = 7 * seconds1d;
+
+    public void syncViewCount() {
+        ArticleContentLinkExample example = new ArticleContentLinkExample();
+
+
+        LocalDate today = LocalDate.now();
+        // 计算7天前的日期
+        LocalDate dateSevenDaysAgo = today.minusDays(7);
+        // 将日期转换为当天开始的时间(00:00:00)并指定时区
+        ZonedDateTime zonedDateTime = dateSevenDaysAgo.atStartOfDay(ZoneId.systemDefault());
+        // 将ZonedDateTime转换为时间戳(毫秒)
+        long time = zonedDateTime.toInstant().toEpochMilli();
+
+
+        example.createCriteria().andCreateTimeGreaterThanOrEqualTo(time);
+        List<ArticleContentLink> links = articleContentLinkMapper.selectByExample(example);
+        for (ArticleContentLink link : links) {
+            Map<String, String> content = wxFetchRemoteService.getContent(link.getContentLink());
+            link.setTitle(content.getOrDefault("title", ""));
+            link.setViewCount(NumberUtils.toLong(content.getOrDefault("view_count", "0"), 0L));
+            link.setUpdateTime(System.currentTimeMillis());
+
+            articleContentLinkMapper.updateByPrimaryKey(link);
+        }
+
+
+    }
+}

+ 4 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/MessageSendCallbackService.java

@@ -55,7 +55,7 @@ public class MessageSendCallbackService {
             }
 
 
-            long createTime = NumberUtils.toLong(root.elementText("CreateTime"), 0);
+            long createTime = NumberUtils.toLong(root.elementText("CreateTime"), 0) * 1000;
 
             Element articleUrlResultEle = root.element("ArticleUrlResult");
             Element resultListEle = articleUrlResultEle.element("ResultList");
@@ -67,6 +67,9 @@ public class MessageSendCallbackService {
                 link.setIdx(NumberUtils.toInt(item.elementText("ArticleIdx"), 0));
                 link.setContentLink(item.elementText("ArticleUrl"));
                 link.setCreateTime(createTime);
+                link.setTitle("");
+                link.setUpdateTime(createTime);
+                link.setViewCount(-1L);
                 links.add(link);
             }
             articleContentLinkMapper.insertBatch(links);

+ 33 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/web/JobController.java

@@ -0,0 +1,33 @@
+package com.tzld.longarticle.recommend.server.web;
+
+import com.tzld.longarticle.recommend.server.model.Result;
+import com.tzld.longarticle.recommend.server.service.JobService;
+import com.tzld.longarticle.recommend.server.service.UserManagementService;
+import com.tzld.longarticle.recommend.server.service.WXListenService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+/**
+ * @author dyp
+ */
+@RestController
+@Slf4j
+@RequestMapping("/job")
+public class JobController {
+    @Autowired
+    private JobService jobService;
+
+
+    @RequestMapping("/syncViewCount")
+    public String syncViewCount() {
+        jobService.syncViewCount();
+        return "success";
+    }
+
+}

+ 55 - 7
long-article-recommend-service/src/main/resources/mapper/crawler/ArticleContentLinkMapper.xml

@@ -7,6 +7,9 @@
     <result column="idx" jdbcType="INTEGER" property="idx" />
     <result column="content_link" jdbcType="VARCHAR" property="contentLink" />
     <result column="create_time" jdbcType="BIGINT" property="createTime" />
+    <result column="title" jdbcType="VARCHAR" property="title" />
+    <result column="view_count" jdbcType="BIGINT" property="viewCount" />
+    <result column="update_time" jdbcType="BIGINT" property="updateTime" />
   </resultMap>
   <sql id="Example_Where_Clause">
     <where>
@@ -67,7 +70,7 @@
     </where>
   </sql>
   <sql id="Base_Column_List">
-    id, gzh_id, idx, content_link, create_time
+    id, gzh_id, idx, content_link, create_time, title, view_count, update_time
   </sql>
   <select id="selectByExample" parameterType="com.tzld.longarticle.recommend.server.repository.model.ArticleContentLinkExample" resultMap="BaseResultMap">
     select
@@ -104,9 +107,11 @@
       SELECT LAST_INSERT_ID()
     </selectKey>
     insert into article_content_link (gzh_id, idx, content_link, 
-      create_time)
+      create_time, title, view_count, 
+      update_time)
     values (#{gzhId,jdbcType=VARCHAR}, #{idx,jdbcType=INTEGER}, #{contentLink,jdbcType=VARCHAR}, 
-      #{createTime,jdbcType=BIGINT})
+      #{createTime,jdbcType=BIGINT}, #{title,jdbcType=VARCHAR}, #{viewCount,jdbcType=BIGINT}, 
+      #{updateTime,jdbcType=BIGINT})
   </insert>
   <insert id="insertSelective" parameterType="com.tzld.longarticle.recommend.server.repository.model.ArticleContentLink">
     <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
@@ -126,6 +131,15 @@
       <if test="createTime != null">
         create_time,
       </if>
+      <if test="title != null">
+        title,
+      </if>
+      <if test="viewCount != null">
+        view_count,
+      </if>
+      <if test="updateTime != null">
+        update_time,
+      </if>
     </trim>
     <trim prefix="values (" suffix=")" suffixOverrides=",">
       <if test="gzhId != null">
@@ -140,6 +154,15 @@
       <if test="createTime != null">
         #{createTime,jdbcType=BIGINT},
       </if>
+      <if test="title != null">
+        #{title,jdbcType=VARCHAR},
+      </if>
+      <if test="viewCount != null">
+        #{viewCount,jdbcType=BIGINT},
+      </if>
+      <if test="updateTime != null">
+        #{updateTime,jdbcType=BIGINT},
+      </if>
     </trim>
   </insert>
   <select id="countByExample" parameterType="com.tzld.longarticle.recommend.server.repository.model.ArticleContentLinkExample" resultType="java.lang.Long">
@@ -166,6 +189,15 @@
       <if test="row.createTime != null">
         create_time = #{row.createTime,jdbcType=BIGINT},
       </if>
+      <if test="row.title != null">
+        title = #{row.title,jdbcType=VARCHAR},
+      </if>
+      <if test="row.viewCount != null">
+        view_count = #{row.viewCount,jdbcType=BIGINT},
+      </if>
+      <if test="row.updateTime != null">
+        update_time = #{row.updateTime,jdbcType=BIGINT},
+      </if>
     </set>
     <if test="example != null">
       <include refid="Update_By_Example_Where_Clause" />
@@ -177,7 +209,10 @@
       gzh_id = #{row.gzhId,jdbcType=VARCHAR},
       idx = #{row.idx,jdbcType=INTEGER},
       content_link = #{row.contentLink,jdbcType=VARCHAR},
-      create_time = #{row.createTime,jdbcType=BIGINT}
+      create_time = #{row.createTime,jdbcType=BIGINT},
+      title = #{row.title,jdbcType=VARCHAR},
+      view_count = #{row.viewCount,jdbcType=BIGINT},
+      update_time = #{row.updateTime,jdbcType=BIGINT}
     <if test="example != null">
       <include refid="Update_By_Example_Where_Clause" />
     </if>
@@ -197,6 +232,15 @@
       <if test="createTime != null">
         create_time = #{createTime,jdbcType=BIGINT},
       </if>
+      <if test="title != null">
+        title = #{title,jdbcType=VARCHAR},
+      </if>
+      <if test="viewCount != null">
+        view_count = #{viewCount,jdbcType=BIGINT},
+      </if>
+      <if test="updateTime != null">
+        update_time = #{updateTime,jdbcType=BIGINT},
+      </if>
     </set>
     where id = #{id,jdbcType=BIGINT}
   </update>
@@ -205,15 +249,19 @@
     set gzh_id = #{gzhId,jdbcType=VARCHAR},
       idx = #{idx,jdbcType=INTEGER},
       content_link = #{contentLink,jdbcType=VARCHAR},
-      create_time = #{createTime,jdbcType=BIGINT}
+      create_time = #{createTime,jdbcType=BIGINT},
+      title = #{title,jdbcType=VARCHAR},
+      view_count = #{viewCount,jdbcType=BIGINT},
+      update_time = #{updateTime,jdbcType=BIGINT}
     where id = #{id,jdbcType=BIGINT}
   </update>
 
   <insert id="insertBatch" parameterType="list">
-    INSERT INTO article_content_link (gzh_id, idx, content_link, create_time)
+    INSERT INTO article_content_link (gzh_id, idx, content_link, create_time, title, view_count, update_time)
     VALUES
     <foreach collection="list" item="item" separator=",">
-      (#{item.gzhId}, #{item.idx}, #{item.contentLink}, #{item.createTime})
+      (#{item.gzhId}, #{item.idx}, #{item.contentLink}, #{item.createTime}, #{item.title}, #{item.viewCount},
+      #{item.updateTime})
     </foreach>
   </insert>
 </mapper>