luodangjia
2024-04-28 53d9ce668f7775b959f2d1be6f5caf30f43e5f29
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package cn.stylefeng.rest.modular.user.controller;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.stylefeng.guns.modular.business.dto.MentalTestMyMentalAppointmentDTO;
import cn.stylefeng.guns.modular.business.dto.MentalTestMyTestOrderDTO;
import cn.stylefeng.guns.modular.business.dto.MentalTestMyTestTopicDTO;
import cn.stylefeng.guns.modular.business.dto.MentalTestTopicAndRecordDTO;
import cn.stylefeng.guns.modular.business.entity.MentalTestRecord;
import cn.stylefeng.guns.modular.business.entity.MentalTestResult;
import cn.stylefeng.guns.modular.business.entity.MentalTestTopic;
import cn.stylefeng.guns.modular.business.entity.OrderMentalTest;
import cn.stylefeng.guns.modular.business.service.*;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.rule.enums.MentalAppointmentStatusEnum;
import cn.stylefeng.roses.kernel.rule.enums.OrderStatusFlagEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
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 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.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
 
/**
 * 主页接口-html富文本页面
 *
 * @author goupan
 * @date 2024/01/02
 */
@RestController
@Api(tags = "心理测试接口")
@ApiResource(name = "心理测试接口")
@RequestMapping("/mentalTest")
public class MyMentalTestController {
 
    @Resource
    private IMentalTestRecordService mentalTestRecordService;
 
    @Resource
    private IMentalTestResultService mentalTestResultService;
 
    @Resource
    private IOrderMentalTestService orderMentalTestService;
 
    @Resource
    private IOrderConsultOneService orderConsultOneService;
 
    @Resource
    private IMentalTestTopicService mentalTestTopicService;
 
    @Resource
    private IMentalAppointmentService mentalAppointmentService;
 
    @ApiOperation(value = "我的性格分析预约")
    @GetResource(name = "我的性格分析预约", path = "/myMentalAppointment")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "consultantName",value = "咨询师名称")
    })
    public ResponseData<List<MentalTestMyMentalAppointmentDTO>> myMentalAppointment(String consultantName) {
        // 获取当前登录用户信息
        LoginUser loginUser = LoginContext.me().getLoginUser();
        Long userId = loginUser.getUserId();
 
        List<MentalTestMyMentalAppointmentDTO> list = mentalAppointmentService.myMentalAppointment(
                userId,
                consultantName,
                Arrays.asList(
                        MentalAppointmentStatusEnum.WAIT_SERVICE.getCode(),
                        MentalAppointmentStatusEnum.IN_SERVICE.getCode(),
                        MentalAppointmentStatusEnum.DONE.getCode()
                )
        );
        return new SuccessResponseData(list);
    }
 
    @ApiOperation(value = "我的测试")
    @GetResource(name = "我的测试", path = "/myTestTopic")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "topicId",value = "题库ID")
    })
    public ResponseData<List<MentalTestMyTestTopicDTO>> myTestTopic(Long topicId) {
        // 获取当前登录用户信息
        LoginUser loginUser = LoginContext.me().getLoginUser();
        Long userId = loginUser.getUserId();
 
        // 查询测试记录
        List<MentalTestMyTestTopicDTO> list = mentalTestRecordService.myTestTopic(userId, topicId);
        if (CollUtil.isEmpty(list)) {
            return new ErrorResponseData<>("无测试记录!");
        }
 
        List<Long> topicIdList = list.stream().map(o -> o.getTopicId()).collect(Collectors.toList());
        List<MentalTestResult> mentalTestResultAll = mentalTestResultService.list(
                Wrappers.<MentalTestResult>lambdaQuery()
                        .eq(MentalTestResult::getUserId, userId)
                        .in(MentalTestResult::getTopicId, topicIdList)
                        .groupBy(MentalTestResult::getTopicId)
        );
        List<OrderMentalTest> orderMentalTestAll = orderMentalTestService.list(
                Wrappers.<OrderMentalTest>lambdaQuery()
                        .eq(OrderMentalTest::getUserId, userId)
                        .eq(OrderMentalTest::getStatusFlag, OrderStatusFlagEnum.PAY_SUCCESS.getCode())
                        .in(OrderMentalTest::getGoodsId, topicIdList)
                        .groupBy(OrderMentalTest::getGoodsId)
        );
 
        return new SuccessResponseData(list.stream().map(o -> {
            // 是否后台创建
            o.setIsBack(orderMentalTestAll.stream().filter(oo -> oo.getOrderNo().equals(o.getAnswerNo())).findFirst().map(OrderMentalTest::getIsBack).orElse(false));
 
            // 是否测试
            o.setTestFlag(mentalTestResultAll.stream().filter(oo -> oo.getTopicId().equals(o.getTopicId())).findFirst().isPresent());
 
            // 是否购买
            o.setPayFlag(orderMentalTestAll.stream().filter(oo -> oo.getGoodsId().equals(o.getTopicId())).findFirst().isPresent());
 
            return o;
        }).collect(Collectors.toList()));
    }
 
    @ApiOperation(value = "我的测试(订单)")
    @GetResource(name = "我的测试(订单)", path = "/myTestOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "topicId",value = "题库ID")
    })
    public ResponseData<List<MentalTestMyTestOrderDTO>> myTestOrder(Long topicId) {
        // 获取当前登录用户信息
        LoginUser loginUser = LoginContext.me().getLoginUser();
        Long userId = loginUser.getUserId();
 
        // 查询测试记录
        List<MentalTestMyTestOrderDTO> list = mentalTestRecordService.myTestOrder(userId, topicId);
        if (CollUtil.isEmpty(list)) {
            return new ErrorResponseData<>("无测试订单!");
        }
 
        List<Long> topicIdList = list.stream().map(o -> o.getGoodsId()).collect(Collectors.toList());
        List<OrderMentalTest> orderMentalTestAll = orderMentalTestService.list(
                Wrappers.<OrderMentalTest>lambdaQuery()
                        .eq(OrderMentalTest::getUserId, userId)
                        .eq(OrderMentalTest::getStatusFlag, OrderStatusFlagEnum.PAY_SUCCESS.getCode())
                        .in(OrderMentalTest::getGoodsId, topicIdList)
                        .groupBy(OrderMentalTest::getGoodsId)
        );
 
        return new SuccessResponseData(list.stream().map(o -> {
            // 是否购买
            o.setPayFlag(orderMentalTestAll.stream().filter(oo -> oo.getGoodsId().equals(o.getGoodsId())).findFirst().isPresent());
            return o;
        }).collect(Collectors.toList()));
    }
 
    @ApiOperation(value = "获取心理测试结果")
    @GetResource(name = "获取心理测试结果", path = "/mentalTestRecord/{id}")
    public ResponseData<MentalTestTopicAndRecordDTO> mentalTestRecord(@PathVariable("id") Long id) {
        MentalTestRecord mentalTestRecord = mentalTestRecordService.getById(id);
        Assert.notNull(mentalTestRecord, "测试记录不存在!");
        MentalTestTopicAndRecordDTO dto = new MentalTestTopicAndRecordDTO();
        if (mentalTestRecord != null) {
            MentalTestTopic topic = mentalTestTopicService.getById(mentalTestRecord.getTopicId());
            if (topic != null) {
                BeanUtil.copyProperties(topic, dto);
            }
            // 覆盖题库id
            BeanUtil.copyProperties(mentalTestRecord, dto);
        }
 
        // 是否后台创建
        OrderMentalTest orderMentalTest = orderMentalTestService.getOrderByNo(mentalTestRecord.getAnswerNo());
        dto.setIsBack(Optional.ofNullable(orderMentalTest).map(OrderMentalTest::getIsBack).orElse(false));
 
        // 是否支付1v1咨询订单
        dto.setIsPayOrderConsultOne(orderConsultOneService.getIsPayOrderConsultOne(mentalTestRecord.getOrderConsultOneId(), null));
 
        return new SuccessResponseData(dto);
    }
 
    @ApiOperation(value = "获取心理测试结果(订单)")
    @GetResource(name = "获取心理测试结果(订单)", path = "/mentalTestRecordByOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNo",value = "订单编号"),
            @ApiImplicitParam(name = "topicId",value = "题库ID"),
    })
    public ResponseData<MentalTestTopicAndRecordDTO> mentalTestRecordByOrder(String orderNo, Long topicId) {
        MentalTestRecord mentalTestRecord = mentalTestRecordService.getOne(
                Wrappers.<MentalTestRecord>lambdaQuery()
                        .eq(ObjUtil.isNotEmpty(orderNo), MentalTestRecord::getAnswerNo, orderNo)
                        .eq(ObjUtil.isNotEmpty(topicId), MentalTestRecord::getTopicId, topicId)
                        .orderByDesc(MentalTestRecord::getCreateTime)
                        .last("LIMIT 1")
        );
        Assert.notNull(mentalTestRecord, "没有该记录!");
 
        // 心理题库信息
        MentalTestTopicAndRecordDTO dto = new MentalTestTopicAndRecordDTO();
        if (mentalTestRecord != null) {
            MentalTestTopic topic = mentalTestTopicService.getById(mentalTestRecord.getTopicId());
            if (topic != null) {
                BeanUtil.copyProperties(topic, dto);
            }
            BeanUtil.copyProperties(mentalTestRecord, dto);
 
            // 是否后台创建
            OrderMentalTest orderMentalTest = orderMentalTestService.getOrderByNo(mentalTestRecord.getAnswerNo());
            dto.setIsBack(Optional.ofNullable(orderMentalTest).map(OrderMentalTest::getIsBack).orElse(false));
 
            // 是否支付1v1咨询订单
            dto.setIsPayOrderConsultOne(orderConsultOneService.getIsPayOrderConsultOne(mentalTestRecord.getOrderConsultOneId(), null));
        }
        return new SuccessResponseData(dto);
    }
 
}