123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- package com.qxgmat.controller.api;
- import com.alibaba.fastjson.JSONObject;
- import com.nuliji.tools.Response;
- import com.nuliji.tools.ResponseHelp;
- import com.nuliji.tools.Transform;
- import com.nuliji.tools.exception.AuthException;
- import com.qxgmat.data.constants.enums.SettingKey;
- import com.qxgmat.data.constants.enums.logic.SentenceLogic;
- import com.qxgmat.data.constants.enums.module.PaperOrigin;
- import com.qxgmat.data.dao.entity.*;
- import com.qxgmat.dto.extend.CommentExtendDto;
- import com.qxgmat.dto.extend.UserPaperBaseExtendDto;
- import com.qxgmat.dto.extend.UserReportExtendDto;
- import com.qxgmat.dto.request.*;
- import com.qxgmat.dto.response.*;
- import com.qxgmat.help.ShiroHelp;
- import com.qxgmat.service.UserPaperService;
- import com.qxgmat.service.extend.SentenceService;
- import com.qxgmat.service.inline.SentencePaperService;
- import com.qxgmat.service.UserCollectQuestionService;
- import com.qxgmat.service.UsersService;
- import com.qxgmat.service.extend.QuestionFlowService;
- import com.qxgmat.service.inline.*;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpSession;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @RestController
- @RequestMapping("/api/sentence")
- @Api(tags = "长难句", description = "长难句接口")
- public class SentenceController
- {
- @Autowired
- private ShiroHelp shiroHelp;
- @Autowired
- private SentenceArticleService sentenceArticleService;
- @Autowired
- private SentenceQuestionService sentenceQuestionService;
- @Autowired
- private SentencePaperService sentencePaperService;
- @Autowired
- private SentenceCodeService sentenceCodeService;
- @Autowired
- private SentenceService sentenceService;
- @Autowired
- private QuestionService questionService;
- @Autowired
- private SettingService settingService;
- @Autowired
- private CourseDataService courseDataService;
- @Autowired
- private CommentService commentService;
- @Autowired
- private UsersService usersService;
- @Autowired
- private UserPaperService userPaperService;
- @Autowired
- private UserReportService userReportService;
- @Autowired
- private UserSentenceProgressService userSentenceProgressService;
- @Autowired
- private UserSentenceRecordService userSentenceRecordService;
- @Autowired
- private UserCollectQuestionService userCollectQuestionService;
- @Autowired
- private QuestionFlowService questionFlowService;
- @RequestMapping(value = "/info", method = RequestMethod.GET)
- @ApiOperation(value = "所有长难句信息", httpMethod = "GET")
- public Response<UserSentenceInfoDto> info(HttpSession session) {
- UserSentenceInfoDto dto = new UserSentenceInfoDto();
- User user = (User) shiroHelp.getLoginUser();
- if (user != null){
-
- SentenceCode code = sentenceCodeService.isActive(user.getId());
- if (code != null){
- dto.setCode(code.getCode());
- }
- }
-
- Setting entity = settingService.getByKey(SettingKey.SENTENCE);
- JSONObject value = entity.getValue();
- dto.setChapters(value.getJSONArray("chapters"));
- dto.setTrailPages(value.getInteger("trailPages"));
-
- CourseData courseData = courseDataService.getSentence();
- if(courseData != null){
- List<Comment> commentList = commentService.list(1, 1, "course_data", courseData.getId().toString());
- dto.setComments(Transform.convert(commentList, CommentExtendDto.class));
- }
-
- return ResponseHelp.success(dto);
- }
- @RequestMapping(value = "/active", method = RequestMethod.PUT)
- @ApiOperation(value = "激活长难句", httpMethod = "PUT")
- public Response<Boolean> active(@RequestBody @Validated UserSentenceCodeDto dto) {
- User user = (User) shiroHelp.getLoginUser();
- if (user == null) throw new AuthException("需要登录");
- sentenceService.active(user.getId(), dto.getCode());
- return ResponseHelp.success(true);
- }
- @RequestMapping(value = "/article/list", method = RequestMethod.GET)
- @ApiOperation(value = "长难句文章列表", httpMethod = "GET")
- public Response<List<UserSentenceArticleDto>> listArticle(
- HttpSession session) {
- User user = (User) shiroHelp.getLoginUser();
- List<SentenceArticle> p = sentenceArticleService.all();
- List<UserSentenceArticleDto> pr = Transform.convert(p, UserSentenceArticleDto.class);
- if (user != null){
-
- List<UserSentenceProgress> progressList = userSentenceProgressService.all(user.getId());
- Map<String, UserSentenceProgress> progressMap = new HashMap<>();
- for (UserSentenceProgress progress:progressList){
- progressMap.put(String.format("%d-%d", progress.getChapter(), progress.getPart()), progress);
- }
- for (UserSentenceArticleDto dto : pr){
- UserSentenceProgress progress = progressMap.get(String.format("%d-%d", dto.getChapter(), dto.getPart()));
- dto.setProgress(progress == null ? 0 : progress.getProgress());
- dto.setTimes(progress == null ? 0:progress.getTimes());
- }
- }
- return ResponseHelp.success(pr);
- }
- @RequestMapping(value = "/article/detail", method = RequestMethod.GET)
- @ApiOperation(value = "长难句文章详情", httpMethod = "GET")
- public Response<UserSentenceArticleDetailDto> detailArticle(
- @RequestParam(required = true) Integer articleId
- ) {
- User user = (User) shiroHelp.getLoginUser();
- SentenceArticle article = sentenceArticleService.get(articleId);
- UserSentenceArticleDetailDto dto = Transform.convert(article, UserSentenceArticleDetailDto.class);
- if (user != null){
-
- UserSentenceProgress progress = userSentenceProgressService.get(user.getId(), article.getChapter(), article.getPart());
- if (progress != null){
- dto.setProgress(progress.getProgress());
- dto.setTimes(progress.getTimes());
- }
- }
- return ResponseHelp.success(dto);
- }
- @RequestMapping(value = "/article/progress", method = RequestMethod.PUT)
- @ApiOperation(value = "更新长难句文章进度", httpMethod = "PUT")
- public Response<Boolean> articleProcess(@RequestBody @Validated UserSentenceProgressDto dto) {
- UserSentenceProgress entity = Transform.dtoToEntity(dto);
- User user = (User) shiroHelp.getLoginUser();
- if (user == null) throw new AuthException("需要登录");
-
- if (dto.getCurrentChapter() != null && dto.getCurrentPart() != null && dto.getTime() != null && dto.getTime() > 0){
- userSentenceRecordService.add(UserSentenceRecord.builder()
- .userId(user.getId())
- .chapter(dto.getCurrentChapter())
- .part(dto.getCurrentPart())
- .userTime(dto.getTime())
- .build());
- }
-
-
- userSentenceProgressService.updateProgress(user.getId(), dto.getChapter(), dto.getPart(), dto.getProgress());
- return ResponseHelp.success(true);
- }
- @RequestMapping(value = "/paper/list", method = RequestMethod.GET)
- @ApiOperation(value = "长难句组卷列表", httpMethod = "GET")
- public Response<List<UserSentencePaperDto>> listPaper(
- HttpSession session) {
- User user = (User) shiroHelp.getLoginUser();
- List<SentencePaper> p;
-
- if (user != null && sentenceCodeService.isActive(user.getId()) != null){
- p = sentencePaperService.listByLogic(SentenceLogic.NO);
- } else {
- p = sentencePaperService.listByLogic(SentenceLogic.TRAIL);
- }
- List<UserSentencePaperDto> pr = Transform.convert(p, UserSentencePaperDto.class);
-
- Map<Integer, Integer[]> questionNoIdsMap = new HashMap<>();
- for(SentencePaper paper : p){
- questionNoIdsMap.put(paper.getId(), paper.getQuestionNoIds());
- }
- Map statMap = sentenceQuestionService.statPaperMap(questionNoIdsMap);
- Transform.combine(pr, statMap, UserSentencePaperDto.class, "id", "stat");
- if (user != null){
-
- Collection ids = Transform.getIds(p, SentencePaper.class, "id");
- List<UserPaper> userPaperList = userPaperService.listWithOrigin(user.getId(), PaperOrigin.SENTENCE, ids, null);
- Transform.combine(pr, userPaperList, UserSentencePaperDto.class, "id", "paper", UserPaper.class, "originId", UserPaperBaseExtendDto.class);
-
- Map userPaperMap = Transform.getMap(userPaperList, UserPaper.class, "originId", "id");
- Transform.combine(pr, userPaperMap, UserSentencePaperDto.class, "id", "userPaperId");
-
- Collection paperIds = Transform.getIds(userPaperList, UserPaper.class, "id");
- List<UserReport> reportList = userReportService.listWithLast(paperIds);
- Transform.combine(pr, reportList, UserSentencePaperDto.class, "userPaperId", "report", UserReport.class, "paperId", UserReportExtendDto.class);
- }
- return ResponseHelp.success(pr);
- }
- @RequestMapping(value = "/paper", method = RequestMethod.GET)
- @ApiOperation(value = "获取练习卷", notes = "获取练习卷", httpMethod = "GET")
- public Response<PaperBaseDto> detailPaper(
- @RequestParam(required = true, name="id") Integer paperId
- ) {
- User user = (User) shiroHelp.getLoginUser();
- SentencePaper paper = sentencePaperService.get(paperId);
- PaperBaseDto paperDto = Transform.convert(paper, PaperBaseDto.class);
- return ResponseHelp.success(paperDto);
- }
- }
|