guohongjin
2024-05-01 bd280ea7748a83f1454178da4c93b93aa1be6ed9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package cn.stylefeng.rest.modular.home.controller;
 
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.business.entity.PsychologicalColumn;
import cn.stylefeng.guns.modular.business.entity.PsychologicalRead;
import cn.stylefeng.guns.modular.business.service.IPsychologicalColumnService;
import cn.stylefeng.guns.modular.business.service.IPsychologicalReadService;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.enums.DeleteEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.List;
 
/**
 * 主页接口-心理专栏
 *
 * @author goupan
 * @date 2024/01/02
 */
@RestController
@Api(tags = "主页接口")
@ApiResource(name = "主页接口")
@RequestMapping("/home/psychologicalColumn")
public class HomePsychologicalColumnController {
 
    @Resource
    private IPsychologicalColumnService psychologicalColumnService;
 
    @Resource
    private IPsychologicalReadService psychologicalReadService;
 
    @ApiOperation("心理专栏(分页)")
    @GetResource(name = "心理专栏(分页)", path = "/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页数", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataTypeClass = Integer.class, defaultValue = "20"),
            @ApiImplicitParam(name = "title", value = "标题")
    })
    public ResponseData<PageResult<PsychologicalColumn>> page(Integer pageNo, Integer pageSize, String title) {
        Page<PsychologicalColumn> page = this.psychologicalColumnService.page(
                PageFactory.page(pageNo, pageSize),
                Wrappers.<PsychologicalColumn>lambdaQuery()
                        .eq(PsychologicalColumn::getIsDelete,DeleteEnum.NOT_DELETE.getCode())
                        .like(StrUtil.isNotBlank(title), PsychologicalColumn::getArticleTitle, title)
                        .orderByDesc(PsychologicalColumn::getReleaseTime)
        );
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page));
    }
 
    @ApiOperation("心理专栏")
    @GetResource(name = "心理专栏", path = "/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题")
    })
    public ResponseData<List<PsychologicalColumn>> list(String title) {
        // 查询列表
        List<PsychologicalColumn> list = psychologicalColumnService.list(
                Wrappers.<PsychologicalColumn>lambdaQuery()
                        .eq(PsychologicalColumn::getIsDelete, DeleteEnum.NOT_DELETE.getCode())
                        .like(StrUtil.isNotBlank(title), PsychologicalColumn::getArticleTitle, title)
                        .orderByDesc(PsychologicalColumn::getCreateTime)
        );
        return new SuccessResponseData(list);
    }
 
    @ApiOperation(value = "心理专栏详情", notes = "心理专栏详情,增加阅读量")
    @GetResource(name = "心理专栏", path = "/detail/{id}")
    public ResponseData<PsychologicalColumn> detail(@PathVariable("id") Long id) {
        // 查询详情
        PsychologicalColumn obj = psychologicalColumnService.getById(id);
 
        // 获取当前登录用户信息
        LoginUser loginUser = LoginContext.me().getLoginUser();
        if (loginUser != null) {
            // 获取当前用户的用户Id
            Long userId = loginUser.getUserId();
 
//            // 是否阅读过
//            long count = psychologicalReadService.count(
//                    Wrappers.<PsychologicalRead>lambdaQuery()
//                            .eq(PsychologicalRead::getPsychologicalColumnId, id)
//                            .eq(PsychologicalRead::getUserId, userId)
//            );
//
//            if (count == 0) {
                // 阅读量+1
                psychologicalColumnService.update(
                        Wrappers.<PsychologicalColumn>lambdaUpdate()
                                .set(PsychologicalColumn::getReadNum, obj.getReadNum() + 1)
                                .eq(PsychologicalColumn::getId, id)
                );
 
                // 保存阅读记录
                PsychologicalRead read = new PsychologicalRead();
                read.setPsychologicalColumnId(id);
                read.setUserId(userId);
                psychologicalReadService.save(read);
//            }
        }
 
        return new SuccessResponseData(obj);
    }
 
}