| | |
| | | package com.ycl.jxkg.controller.admin; |
| | | |
| | | import com.ycl.jxkg.base.BaseApiController; |
| | | import com.ycl.jxkg.base.RestResponse; |
| | | import com.ycl.jxkg.base.SystemCode; |
| | | import com.ycl.jxkg.domain.Question; |
| | | import com.ycl.jxkg.domain.TextContent; |
| | | import com.ycl.jxkg.domain.enums.QuestionTypeEnum; |
| | | import com.ycl.jxkg.domain.question.QuestionObject; |
| | | import com.ycl.jxkg.service.QuestionService; |
| | | import com.ycl.jxkg.service.TextContentService; |
| | | import com.ycl.jxkg.utility.*; |
| | | import com.ycl.jxkg.viewmodel.admin.question.QuestionEditRequestVM; |
| | | import com.ycl.jxkg.viewmodel.admin.question.QuestionPageRequestVM; |
| | | import com.ycl.jxkg.viewmodel.admin.question.QuestionResponseVM; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.ycl.jxkg.base.BaseApiController; |
| | | import com.ycl.jxkg.base.Result; |
| | | import com.ycl.jxkg.base.SystemCode; |
| | | import com.ycl.jxkg.domain.question.QuestionObject; |
| | | import com.ycl.jxkg.domain.vo.admin.question.QuestionEditRequestVO; |
| | | import com.ycl.jxkg.domain.vo.admin.question.QuestionPageRequestVO; |
| | | import com.ycl.jxkg.domain.vo.admin.question.QuestionResponseVO; |
| | | import com.ycl.jxkg.service.QuestionService; |
| | | import com.ycl.jxkg.enums.QuestionTypeEnum; |
| | | import com.ycl.jxkg.utils.ErrorUtil; |
| | | import com.ycl.jxkg.utils.HtmlUtil; |
| | | import com.ycl.jxkg.utils.JsonUtil; |
| | | import com.ycl.jxkg.utils.PageInfoHelper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.validation.Valid; |
| | | |
| | | @RequiredArgsConstructor |
| | | @RestController("AdminQuestionController") |
| | | @RequestMapping(value = "/api/admin/question") |
| | | public class QuestionController extends BaseApiController { |
| | | |
| | | private final QuestionService questionService; |
| | | private final TextContentService textContentService; |
| | | |
| | | @Autowired |
| | | public QuestionController(QuestionService questionService, TextContentService textContentService) { |
| | | this.questionService = questionService; |
| | | this.textContentService = textContentService; |
| | | } |
| | | |
| | | @RequestMapping(value = "/page", method = RequestMethod.POST) |
| | | public RestResponse<PageInfo<QuestionResponseVM>> pageList(@RequestBody QuestionPageRequestVM model) { |
| | | PageInfo<Question> pageInfo = questionService.page(model); |
| | | PageInfo<QuestionResponseVM> page = PageInfoHelper.copyMap(pageInfo, q -> { |
| | | QuestionResponseVM vm = modelMapper.map(q, QuestionResponseVM.class); |
| | | vm.setCreateTime(DateTimeUtil.dateFormat(q.getCreateTime())); |
| | | vm.setScore(ExamUtil.scoreToVM(q.getScore())); |
| | | TextContent textContent = textContentService.selectById(q.getInfoTextContentId()); |
| | | QuestionObject questionObject = JsonUtil.toJsonObject(textContent.getContent(), QuestionObject.class); |
| | | public Result<PageInfo<QuestionResponseVO>> pageList(@RequestBody QuestionPageRequestVO model) { |
| | | PageInfo<QuestionResponseVO> pageInfo = questionService.page(model); |
| | | PageInfo<QuestionResponseVO> page = PageInfoHelper.copyMap(pageInfo, q -> { |
| | | QuestionResponseVO vo = new QuestionResponseVO(); |
| | | BeanUtils.copyProperties(q, vo); |
| | | QuestionObject questionObject = JsonUtil.toJsonObject(q.getContent(), QuestionObject.class); |
| | | String clearHtml = HtmlUtil.clear(questionObject.getTitleContent()); |
| | | vm.setShortTitle(clearHtml); |
| | | return vm; |
| | | vo.setShortTitle(clearHtml); |
| | | return vo; |
| | | }); |
| | | return RestResponse.ok(page); |
| | | return Result.ok(page); |
| | | } |
| | | |
| | | @RequestMapping(value = "/edit", method = RequestMethod.POST) |
| | | public RestResponse edit(@RequestBody @Valid QuestionEditRequestVM model) { |
| | | RestResponse validQuestionEditRequestResult = validQuestionEditRequestVM(model); |
| | | public Result edit(@RequestBody @Valid QuestionEditRequestVO model) { |
| | | Result validQuestionEditRequestResult = validQuestionEditRequestVM(model); |
| | | if (validQuestionEditRequestResult.getCode() != SystemCode.OK.getCode()) { |
| | | return validQuestionEditRequestResult; |
| | | } |
| | |
| | | questionService.updateFullQuestion(model); |
| | | } |
| | | |
| | | return RestResponse.ok(); |
| | | return Result.ok(); |
| | | } |
| | | |
| | | @RequestMapping(value = "/select/{id}", method = RequestMethod.POST) |
| | | public RestResponse<QuestionEditRequestVM> select(@PathVariable Integer id) { |
| | | QuestionEditRequestVM newVM = questionService.getQuestionEditRequestVM(id); |
| | | return RestResponse.ok(newVM); |
| | | public Result<QuestionEditRequestVO> select(@PathVariable Integer id) { |
| | | QuestionEditRequestVO newVM = questionService.getQuestionEditRequestVM(id); |
| | | return Result.ok(newVM); |
| | | } |
| | | |
| | | |
| | | @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST) |
| | | public RestResponse delete(@PathVariable Integer id) { |
| | | Question question = questionService.selectById(id); |
| | | question.setDeleted(true); |
| | | questionService.updateByIdFilter(question); |
| | | return RestResponse.ok(); |
| | | public Result delete(@PathVariable Integer id) { |
| | | questionService.removeById(id); |
| | | return Result.ok(); |
| | | } |
| | | |
| | | private RestResponse validQuestionEditRequestVM(QuestionEditRequestVM model) { |
| | | private Result validQuestionEditRequestVM(QuestionEditRequestVO model) { |
| | | int qType = model.getQuestionType().intValue(); |
| | | boolean requireCorrect = qType == QuestionTypeEnum.SingleChoice.getCode() || qType == QuestionTypeEnum.TrueFalse.getCode(); |
| | | if (requireCorrect) { |
| | | if (StringUtils.isBlank(model.getCorrect())) { |
| | | String errorMsg = ErrorUtil.parameterErrorFormat("correct", "不能为空"); |
| | | return new RestResponse<>(SystemCode.ParameterValidError.getCode(), errorMsg); |
| | | return new Result<>(SystemCode.ParameterValidError.getCode(), errorMsg); |
| | | } |
| | | } |
| | | |
| | | if (qType == QuestionTypeEnum.GapFilling.getCode()) { |
| | | Integer fillSumScore = model.getItems().stream().mapToInt(d -> ExamUtil.scoreFromVM(d.getScore())).sum(); |
| | | Integer questionScore = ExamUtil.scoreFromVM(model.getScore()); |
| | | if (!fillSumScore.equals(questionScore)) { |
| | | String errorMsg = ErrorUtil.parameterErrorFormat("score", "空分数和与题目总分不相等"); |
| | | return new RestResponse<>(SystemCode.ParameterValidError.getCode(), errorMsg); |
| | | } |
| | | } |
| | | return RestResponse.ok(); |
| | | return Result.ok(); |
| | | } |
| | | |
| | | @PostMapping(value = "/status") |
| | | public Result<String> status(@RequestBody QuestionResponseVO question) { |
| | | questionService.updateStatus(question); |
| | | return Result.ok("操作成功"); |
| | | } |
| | | |
| | | } |