guohongjin
2024-05-01 1901fceb6ddaa56a57f3131191454554c3e77e68
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package cn.stylefeng.rest.modular.order.controller;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.business.dto.MentalTestQuestionOptionDTO;
import cn.stylefeng.guns.modular.business.dto.MentalTestTopicDTO;
import cn.stylefeng.guns.modular.business.dto.MentalTestTopicPageDTO;
import cn.stylefeng.guns.modular.business.dto.request.MentalTestAnswerSubmitRequest;
import cn.stylefeng.guns.modular.business.entity.*;
import cn.stylefeng.guns.modular.business.service.*;
import cn.stylefeng.rest.modular.home.service.MentalTestBizService;
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.rule.enums.DeleteEnum;
import cn.stylefeng.roses.kernel.rule.enums.OrderStatusFlagEnum;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
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 cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 主页接口-html富文本页面
 *
 * @author goupan
 * @date 2024/01/02
 */
@RestController
@Api(tags = "心理测试接口")
@ApiResource(name = "心理测试接口")
@RequestMapping("/mentalTest")
public class MentalTestController {
 
    @Resource
    private IMentalTestClassService mentalTestClassService;
 
    @Resource
    private IMentalTestTopicService mentalTestTopicService;
 
    @Resource
    private IOrderMentalTestService orderMentalTestService;
 
    @Resource
    private IMentalTestRecordService mentalTestRecordService;
 
    @Resource
    private IMentalTestResultService mentalTestResultService;
 
    @Resource
    private MentalTestBizService mentalTestBizService;
 
    @ApiOperation(value = "心理测试分类")
    @GetResource(name = "心理测试分类", path = "/classList")
    public ResponseData<MentalTestClass> classList() {
        List<MentalTestClass> list = mentalTestClassService.list(
                Wrappers.<MentalTestClass>lambdaQuery()
                        .eq(MentalTestClass::getStatusFlag, StatusEnum.ENABLE.getCode())
                        .eq(MentalTestClass::getIsDelete, DeleteEnum.NOT_DELETE.getCode())
        );
        return new SuccessResponseData(list);
    }
 
    @ApiOperation(value = "心理测试题库")
    @GetResource(name = "心理测试题库", path = "/topicPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页:第几页(从1开始)", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页:每页大小(默认10)", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "classIds", value = "分类串,逗号拼接", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标题", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "testType", value = "测试类型:0免费,1付费", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "testFlag", value = "是否测试:0未测试,1已测试", dataTypeClass = Integer.class, paramType = "query")
    })
    public ResponseData<Page<MentalTestTopicPageDTO>> topicPage(Integer pageNo, Integer pageSize, String classIds, String title, Integer testType, Integer testFlag) {
        // 获取当前登录用户信息
        LoginUser loginUser = LoginContext.me().getLoginUser();
        Long userId = loginUser.getUserId();
 
        Page<MentalTestTopicPageDTO> page = mentalTestTopicService.topicPage(
                PageFactory.page(pageNo, pageSize),
                classIds,
                title,
                testType,
                testFlag,
                userId,
                StatusEnum.ENABLE.getCode()
        );
 
        // 用户已测试的题库
        List<MentalTestRecord> testRecordList = mentalTestRecordService.list(
                Wrappers.<MentalTestRecord>lambdaQuery()
                        .select(MentalTestRecord::getId, MentalTestRecord::getTopicId)
                        .eq(MentalTestRecord::getUserId, userId)
                        .orderByDesc(MentalTestRecord::getCreateTime)
        );
 
        page.getRecords().stream().forEach(o -> {
            // 是否测试
            o.setTestFlag(testRecordList.stream().filter(to -> o.getId().equals(to.getTopicId())).findFirst().isPresent());
 
            // 测试结果记录ID列表
            o.setMentalTestRecordIdList(testRecordList.stream().filter(to -> o.getId().equals(to.getTopicId())).map(MentalTestRecord::getId).collect(Collectors.toList()));
        });
        return new SuccessResponseData(page);
    }
 
    @ApiOperation(value = "心理测试题库详情")
    @GetResource(name = "心理测试题库详情", path = "/topicDetail/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "题库ID", dataTypeClass = Long.class, paramType = "query")
    })
    public ResponseData<MentalTestTopicDTO> topicDetail(@PathVariable("id") Integer id) {
        // 心理测试题库
        MentalTestTopic testTopic = mentalTestTopicService.getById(id);
        MentalTestTopicDTO topicDto = BeanUtil.toBean(testTopic, MentalTestTopicDTO.class);
 
        // 获取当前登录用户信息
        LoginUser loginUser = LoginContext.me().getLoginUser();
        Long userId = loginUser.getUserId();
 
        // 心理测试结果
        long testCount = mentalTestResultService.count(
                Wrappers.<MentalTestResult>lambdaQuery()
                        .eq(MentalTestResult::getTopicId, id)
                        .eq(MentalTestResult::getUserId, userId)
        );
        // 是否测试
        topicDto.setTestFlag(testCount > 0);
 
        // 用户已购买的题库
        long payOrderCount = orderMentalTestService.count(
                Wrappers.<OrderMentalTest>lambdaQuery()
                        .eq(OrderMentalTest::getGoodsId, id)
                        .eq(OrderMentalTest::getUserId, userId)
                        .eq(OrderMentalTest::getStatusFlag, OrderStatusFlagEnum.PAY_SUCCESS.getCode())
        );
        // 是否购买
        topicDto.setPayFlag(payOrderCount > 0);
 
        // 获取心理测试问题、选项
        List<MentalTestQuestionOptionDTO> questionOptionList = mentalTestTopicService.getQuestionOptionByTopicId(Long.valueOf(id));
        topicDto.setQuestionCount(questionOptionList.size());
        topicDto.setQuestionList(questionOptionList);
 
        return new SuccessResponseData(topicDto);
    }
 
    @ApiOperation(value = "测试答题提交")
    @PostResource(name = "测试答题提交", path = "/answerSubmit")
    public ResponseData<MentalTestRecord> answerSubmit(@RequestBody MentalTestAnswerSubmitRequest req) {
        Assert.isTrue(StrUtil.isNotEmpty(req.getMentalTestOrderNo()), "心理测试订单编号不能为空");
        MentalTestRecord mentalTestRecord = mentalTestBizService.answerSubmit(req);
        return new SuccessResponseData(mentalTestRecord);
    }
 
}