张延森 5 年 前
コミット
ced4335a50

+ 29
- 0
foyo-service/src/main/java/com/huiju/foyo/common/CommUtils.java ファイルの表示

@@ -0,0 +1,29 @@
1
+package com.huiju.foyo.common;
2
+
3
+import javax.servlet.http.HttpServletRequest;
4
+
5
+public class CommUtils {
6
+    public static boolean isStrEmpty(String str) {
7
+        return null == str || "".equals(str.trim()) || "null".equalsIgnoreCase(str) || "undefined".equalsIgnoreCase(str);
8
+    }
9
+
10
+    public static String getIpAddr(HttpServletRequest request) {
11
+        String ip = request.getHeader("X-Real-IP");
12
+        if (!isStrEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
13
+            return ip;
14
+        }
15
+
16
+        ip = request.getHeader("X-Forwarded-For");
17
+        if (!isStrEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
18
+            // 多次反向代理后会有多个IP值,第一个为真实IP。
19
+            int index = ip.indexOf(',');
20
+            if (index != -1) {
21
+                return ip.substring(0, index);
22
+            } else {
23
+                return ip;
24
+            }
25
+        } else {
26
+            return request.getRemoteAddr();
27
+        }
28
+    }
29
+}

+ 12
- 0
foyo-service/src/main/java/com/huiju/foyo/common/ResponseBean.java ファイルの表示

@@ -23,6 +23,18 @@ public class ResponseBean<T> implements Serializable {
23 23
     public ResponseBean() {
24 24
     }
25 25
 
26
+    public static ResponseBean success(Object o) {
27
+        ResponseBean resp = new ResponseBean();
28
+        resp.addSuccess(o);
29
+        return resp;
30
+    }
31
+
32
+    public static ResponseBean error(String message) {
33
+        ResponseBean resp = new ResponseBean();
34
+        resp.addError(message);
35
+        return resp;
36
+    }
37
+
26 38
     public void addDefaultError() {
27 39
         this.code = "1";
28 40
         this.message = "失败";

+ 66
- 7
foyo-service/src/main/java/com/huiju/foyo/controller/TaCaseController.java ファイルの表示

@@ -4,24 +4,26 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 4
 import com.baomidou.mybatisplus.core.metadata.IPage;
5 5
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6 6
 import com.huiju.foyo.common.BaseController;
7
+import com.huiju.foyo.common.CommUtils;
7 8
 import com.huiju.foyo.common.ConstantFoyo;
8 9
 import com.huiju.foyo.common.ResponseBean;
9 10
 import com.huiju.foyo.model.TaCase;
11
+import com.huiju.foyo.model.TaCover;
12
+import com.huiju.foyo.model.TaLikeLog;
10 13
 import com.huiju.foyo.service.ITaCaseService;
14
+import com.huiju.foyo.service.ITaCoverService;
15
+import com.huiju.foyo.service.ITaLikeLogService;
11 16
 import org.slf4j.Logger;
12 17
 import org.slf4j.LoggerFactory;
13 18
 import org.springframework.beans.factory.annotation.Autowired;
14
-import org.springframework.web.bind.annotation.PathVariable;
15
-import org.springframework.web.bind.annotation.RequestBody;
16
-import org.springframework.web.bind.annotation.RequestMapping;
17
-import org.springframework.web.bind.annotation.RequestMethod;
18
-import org.springframework.web.bind.annotation.RequestParam;
19
-import org.springframework.web.bind.annotation.ResponseBody;
20
-import org.springframework.web.bind.annotation.RestController;
19
+import org.springframework.web.bind.annotation.*;
21 20
 
21
+import javax.servlet.http.HttpServletRequest;
22 22
 import java.time.LocalDateTime;
23 23
 import java.util.Arrays;
24
+import java.util.HashMap;
24 25
 import java.util.List;
26
+import java.util.Map;
25 27
 
26 28
 /**
27 29
  * <p>
@@ -40,6 +42,11 @@ public class TaCaseController extends BaseController {
40 42
     @Autowired
41 43
     public ITaCaseService iTaCaseService;
42 44
 
45
+    @Autowired
46
+    public ITaLikeLogService iTaLikeLogService;
47
+
48
+    @Autowired
49
+    public ITaCoverService iTaCoverService;
43 50
 
44 51
     /**
45 52
      * 分页查询列表
@@ -202,4 +209,56 @@ public class TaCaseController extends BaseController {
202 209
         }
203 210
         return responseBean;
204 211
     }
212
+
213
+    @GetMapping("/pc/case/{caseId}")
214
+    public ResponseBean get(@PathVariable Integer caseId) {
215
+        TaCase taCase = iTaCaseService.getById(caseId);
216
+        if (null == taCase) {
217
+            return ResponseBean.error("非法的案例或者动态ID");
218
+        }
219
+
220
+        // 封面
221
+        QueryWrapper<TaCover> wrapper = new QueryWrapper<>();
222
+        wrapper.eq("type", taCase.getType());
223
+        wrapper.last("limit 1");
224
+        TaCover taCover = iTaCoverService.getOne(wrapper);
225
+
226
+        // 上一条, 下一条
227
+        List<TaCase> brothers = iTaCaseService.getBrothers(taCase);
228
+
229
+        // 推荐随机获取数据
230
+        QueryWrapper<TaCase> query = new QueryWrapper<>();
231
+        query.eq("ta_case_type_id", taCase.getTaCaseTypeId());
232
+        query.ne("id", caseId);
233
+        query.last("ORDER BY RAND() LIMIT 10");
234
+        List<TaCase> recommends = iTaCaseService.list(query);
235
+
236
+        Map<String, Object> result = new HashMap<String, Object>(){{
237
+            put("current", taCase);
238
+            put("cover", taCover);
239
+            put("brothers", brothers);
240
+            put("recommends", recommends);
241
+        }};
242
+
243
+        return ResponseBean.success(result);
244
+    }
245
+
246
+    @PostMapping("/pc/like/case/{caseId}")
247
+    public ResponseBean postLike(@PathVariable Integer caseId, HttpServletRequest request) {
248
+        String ip = CommUtils.getIpAddr(request);
249
+
250
+        TaCase taCase = iTaCaseService.getById(caseId);
251
+        if (null == taCase) {
252
+            return ResponseBean.error("非法的案例或者动态ID");
253
+        }
254
+
255
+        TaLikeLog taLikeLog = new TaLikeLog();
256
+        taLikeLog.setAgent("ip="+ip);
257
+        taLikeLog.setCreateDate(LocalDateTime.now());
258
+        taLikeLog.setTarget(caseId);
259
+        taLikeLog.setTargetType("case");
260
+
261
+        Integer likeNum = iTaLikeLogService.postCaseLike(taCase, taLikeLog);
262
+        return ResponseBean.success(likeNum);
263
+    }
205 264
 }

+ 9
- 0
foyo-service/src/main/java/com/huiju/foyo/dao/TaCaseMapper.java ファイルの表示

@@ -1,8 +1,13 @@
1 1
 package com.huiju.foyo.dao;
2 2
 
3
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
3 4
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 5
 import com.huiju.foyo.model.TaCase;
5 6
 import org.apache.ibatis.annotations.Mapper;
7
+import org.apache.ibatis.annotations.Param;
8
+import org.apache.ibatis.annotations.Update;
9
+
10
+import java.util.List;
6 11
 
7 12
 /**
8 13
  * <p>
@@ -15,4 +20,8 @@ import org.apache.ibatis.annotations.Mapper;
15 20
 @Mapper
16 21
 public interface TaCaseMapper extends BaseMapper<TaCase> {
17 22
 
23
+    @Update("update ta_case set like_num = IFNULL(like_num, 0) + #{num} where id = #{id}")
24
+    int increase(@Param("id") Integer id,@Param("num") int num);
25
+
26
+    List<TaCase> selectBrothers(@Param("id")Integer id, @Param("type")String type);
18 27
 }

+ 9
- 0
foyo-service/src/main/java/com/huiju/foyo/dao/TaLikeLogMapper.java ファイルの表示

@@ -0,0 +1,9 @@
1
+package com.huiju.foyo.dao;
2
+
3
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4
+import com.huiju.foyo.model.TaLikeLog;
5
+import org.apache.ibatis.annotations.Mapper;
6
+
7
+@Mapper
8
+public interface TaLikeLogMapper extends BaseMapper<TaLikeLog> {
9
+}

+ 2
- 0
foyo-service/src/main/java/com/huiju/foyo/model/TaCase.java ファイルの表示

@@ -101,4 +101,6 @@ public class TaCase implements Serializable {
101 101
      * 发布类型
102 102
      */
103 103
     private String type;
104
+
105
+    private Integer likeNum;
104 106
 }

+ 29
- 0
foyo-service/src/main/java/com/huiju/foyo/model/TaLikeLog.java ファイルの表示

@@ -0,0 +1,29 @@
1
+package com.huiju.foyo.model;
2
+
3
+import com.baomidou.mybatisplus.annotation.IdType;
4
+import com.baomidou.mybatisplus.annotation.TableId;
5
+import lombok.Data;
6
+import lombok.EqualsAndHashCode;
7
+import lombok.experimental.Accessors;
8
+
9
+import java.io.Serializable;
10
+import java.time.LocalDateTime;
11
+
12
+@Data
13
+@EqualsAndHashCode(callSuper = false)
14
+@Accessors(chain = true)
15
+public class TaLikeLog implements Serializable {
16
+
17
+    private static final long serialVersionUID = 1L;
18
+
19
+    @TableId(value = "serial_no", type = IdType.AUTO)
20
+    private Integer SerialNo;
21
+
22
+    private Integer target;
23
+
24
+    private String agent;
25
+
26
+    private String targetType;
27
+
28
+    private LocalDateTime createDate;
29
+}

+ 3
- 0
foyo-service/src/main/java/com/huiju/foyo/service/ITaCaseService.java ファイルの表示

@@ -3,6 +3,8 @@ package com.huiju.foyo.service;
3 3
 import com.baomidou.mybatisplus.extension.service.IService;
4 4
 import com.huiju.foyo.model.TaCase;
5 5
 
6
+import java.util.List;
7
+
6 8
 /**
7 9
  * <p>
8 10
  * 案例表  服务类
@@ -13,4 +15,5 @@ import com.huiju.foyo.model.TaCase;
13 15
  */
14 16
 public interface ITaCaseService extends IService<TaCase> {
15 17
 
18
+    List<TaCase> getBrothers(TaCase taCase);
16 19
 }

+ 9
- 0
foyo-service/src/main/java/com/huiju/foyo/service/ITaLikeLogService.java ファイルの表示

@@ -0,0 +1,9 @@
1
+package com.huiju.foyo.service;
2
+
3
+import com.baomidou.mybatisplus.extension.service.IService;
4
+import com.huiju.foyo.model.TaCase;
5
+import com.huiju.foyo.model.TaLikeLog;
6
+
7
+public interface ITaLikeLogService extends IService<TaLikeLog> {
8
+    Integer postCaseLike(TaCase taCase, TaLikeLog taLikeLog);
9
+}

+ 9
- 0
foyo-service/src/main/java/com/huiju/foyo/service/impl/TaCaseServiceImpl.java ファイルの表示

@@ -4,8 +4,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
4 4
 import com.huiju.foyo.dao.TaCaseMapper;
5 5
 import com.huiju.foyo.model.TaCase;
6 6
 import com.huiju.foyo.service.ITaCaseService;
7
+import org.springframework.beans.factory.annotation.Autowired;
7 8
 import org.springframework.stereotype.Service;
8 9
 
10
+import java.util.List;
11
+
9 12
 /**
10 13
  * <p>
11 14
  * 案例表  服务实现类
@@ -16,5 +19,11 @@ import org.springframework.stereotype.Service;
16 19
  */
17 20
 @Service
18 21
 public class TaCaseServiceImpl extends ServiceImpl<TaCaseMapper, TaCase> implements ITaCaseService {
22
+    @Autowired
23
+    TaCaseMapper taCaseMapper;
19 24
 
25
+    @Override
26
+    public List<TaCase> getBrothers(TaCase taCase) {
27
+        return taCaseMapper.selectBrothers(taCase.getId(), taCase.getType());
28
+    }
20 29
 }

+ 39
- 0
foyo-service/src/main/java/com/huiju/foyo/service/impl/TaLikeLogServiceImpl.java ファイルの表示

@@ -0,0 +1,39 @@
1
+package com.huiju.foyo.service.impl;
2
+
3
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
5
+import com.huiju.foyo.dao.TaCaseMapper;
6
+import com.huiju.foyo.dao.TaLikeLogMapper;
7
+import com.huiju.foyo.model.TaCase;
8
+import com.huiju.foyo.model.TaLikeLog;
9
+import com.huiju.foyo.service.ITaLikeLogService;
10
+import org.springframework.beans.factory.annotation.Autowired;
11
+import org.springframework.stereotype.Service;
12
+
13
+@Service
14
+public class TaLikeLogServiceImpl extends ServiceImpl<TaLikeLogMapper, TaLikeLog> implements ITaLikeLogService {
15
+
16
+    @Autowired
17
+    TaLikeLogMapper taLikeLogMapper;
18
+
19
+    @Autowired
20
+    TaCaseMapper taCaseMapper;
21
+
22
+    @Override
23
+    public Integer postCaseLike(TaCase taCase, TaLikeLog taLikeLog) {
24
+        // 校验当前是否已经点过赞
25
+        QueryWrapper<TaLikeLog> wrapper = new QueryWrapper<>();
26
+        wrapper.eq("agent", taLikeLog.getAgent());
27
+        wrapper.eq("target", taLikeLog.getTarget());
28
+        wrapper.eq("target_type", taLikeLog.getTargetType());
29
+
30
+        Integer count = taLikeLogMapper.selectCount(wrapper);
31
+        if (count > 0) {
32
+            return null != taCase.getLikeNum() ? taCase.getLikeNum() : 0;
33
+        }
34
+
35
+        taCaseMapper.increase(taCase.getId(), 1);
36
+        taCase = taCaseMapper.selectById(taCase.getId());
37
+        return taCase.getLikeNum();
38
+    }
39
+}

+ 20
- 1
foyo-service/src/main/resources/mapper/TaCaseMapper.xml ファイルの表示

@@ -1,5 +1,24 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 3
 <mapper namespace="com.huiju.foyo.dao.TaCaseMapper">
4
-
4
+    <select id="selectBrothers" resultType="com.huiju.foyo.model.TaCase">
5
+        SELECT
6
+          *
7
+        FROM
8
+          ta_case
9
+        WHERE
10
+          id IN (
11
+            SELECT
12
+              CASE
13
+                WHEN SIGN(id - #{id}) &gt; 0 THEN MIN(id)
14
+                WHEN SIGN(id - #{id}) &lt; 0 THEN MAX(id)
15
+              END
16
+            FROM
17
+            ta_case
18
+            GROUP BY SIGN(id - #{id})
19
+            ORDER BY SIGN(id - #{id})
20
+         )
21
+         AND type = #{type}
22
+        ORDER BY id
23
+    </select>
5 24
 </mapper>

+ 5
- 0
foyo-service/src/main/resources/mapper/TaLikeLogMapper.xml ファイルの表示

@@ -0,0 +1,5 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3
+<mapper namespace="com.huiju.foyo.dao.TaLikeLogMapper">
4
+
5
+</mapper>