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);
|
}
|
|
}
|