QuestionController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package com.qxgmat.controller.admin;
  2. import com.github.pagehelper.Page;
  3. import com.nuliji.tools.*;
  4. import com.nuliji.tools.exception.ParameterException;
  5. import com.qxgmat.data.constants.enums.status.AskStatus;
  6. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  7. import com.qxgmat.data.constants.enums.user.AskTarget;
  8. import com.qxgmat.data.constants.enums.user.MoneyRange;
  9. import com.qxgmat.data.dao.entity.*;
  10. import com.qxgmat.data.relation.entity.QuestionNoRelation;
  11. import com.qxgmat.dto.admin.extend.*;
  12. import com.qxgmat.dto.admin.request.*;
  13. import com.qxgmat.dto.admin.response.QuestionDetailDto;
  14. import com.qxgmat.dto.admin.response.UserAskQuestionDetailDto;
  15. import com.qxgmat.dto.admin.response.UserAskQuestionListDto;
  16. import com.qxgmat.help.ShiroHelp;
  17. import com.qxgmat.service.ExercisePaperService;
  18. import com.qxgmat.service.ManagerService;
  19. import com.qxgmat.service.UsersService;
  20. import com.qxgmat.service.inline.ManagerLogService;
  21. import com.qxgmat.service.inline.QuestionNoService;
  22. import com.qxgmat.service.inline.QuestionService;
  23. import com.qxgmat.service.inline.UserAskQuestionService;
  24. import io.swagger.annotations.Api;
  25. import io.swagger.annotations.ApiOperation;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.http.MediaType;
  30. import org.springframework.validation.annotation.Validated;
  31. import org.springframework.web.bind.annotation.*;
  32. import javax.servlet.http.HttpServletRequest;
  33. import javax.servlet.http.HttpSession;
  34. import java.util.Collection;
  35. import java.util.Date;
  36. import java.util.List;
  37. @RestController("AdminQuestionController")
  38. @RequestMapping("/admin/question")
  39. @Api(tags = "题目接口", description = "题目相关", produces = MediaType.APPLICATION_JSON_VALUE)
  40. public class QuestionController {
  41. private static final Logger logger = LoggerFactory.getLogger(QuestionController.class);
  42. @Autowired
  43. private ShiroHelp shiroHelp;
  44. @Autowired
  45. private ManagerLogService managerLogService;
  46. @Autowired
  47. private ExercisePaperService exercisePaperService;
  48. @Autowired
  49. private QuestionNoService questionNoService;
  50. @Autowired
  51. private QuestionService questionService;
  52. @Autowired
  53. private UserAskQuestionService userAskQuestionService;
  54. @Autowired
  55. private ManagerService managerService;
  56. @Autowired
  57. private UsersService usersService;
  58. @RequestMapping(value = "/add", method = RequestMethod.POST)
  59. @ApiOperation(value = "添加题目", httpMethod = "POST")
  60. public Response<Question> add(@RequestBody @Validated QuestionDto dto, HttpServletRequest request) {
  61. Question entity = Transform.dtoToEntity(dto);
  62. entity = questionService.add(entity);
  63. // 更新编号绑定
  64. questionNoService.bindQuestion(dto.getQuestionNoIds(), entity.getId());
  65. // 更新编号关联
  66. questionNoService.relationQuestion(dto.getRelationQuestion());
  67. managerLogService.log(request);
  68. return ResponseHelp.success(entity);
  69. }
  70. @RequestMapping(value = "/edit", method = RequestMethod.PUT)
  71. @ApiOperation(value = "修改题目", httpMethod = "PUT")
  72. public Response<Boolean> edit(@RequestBody @Validated QuestionDto dto, HttpServletRequest request) {
  73. logger.info("dto: {}, {}, {}", dto, dto.getAnswer(), dto.getContent());
  74. Question entity = Transform.dtoToEntity(dto);
  75. logger.info("entity: {}", entity);
  76. entity = questionService.edit(entity);
  77. // 更新编号绑定
  78. questionNoService.bindQuestion(dto.getQuestionNoIds(), entity.getId());
  79. // 更新编号关联
  80. questionNoService.relationQuestion(dto.getRelationQuestion());
  81. managerLogService.log(request);
  82. return ResponseHelp.success(true);
  83. }
  84. @RequestMapping(value = "/detail", method = RequestMethod.GET)
  85. @ApiOperation(value = "获取题目", httpMethod = "GET")
  86. public Response<QuestionDetailDto> detail(int id, HttpServletRequest request) {
  87. Question entity = questionService.get(id);
  88. QuestionDetailDto dto = Transform.convert(entity, QuestionDetailDto.class);
  89. List<QuestionNo> questionNoList = questionNoService.listByQuestion(entity.getId());
  90. Integer[] ids = new Integer[questionNoList.size()];
  91. for (int index = 0; index < questionNoList.size(); index++) {
  92. ids[index] = questionNoList.get(index).getId();
  93. }
  94. dto.setQuestionNoIds(ids);
  95. // List<QuestionNo> questionNoList = questionNoService.listByQuestion(entity.getId());
  96. // List<QuestionNoExtendDto> questionNos = Transform.convert(questionNoList, QuestionNoExtendDto.class);
  97. // dto.setQuestionNos(questionNos);
  98. return ResponseHelp.success(dto);
  99. }
  100. @RequestMapping(value = "/add/no", method = RequestMethod.POST)
  101. @ApiOperation(value = "添加编号", httpMethod = "POST")
  102. public Response<QuestionNo> addNo(@RequestBody @Validated QuestionNoDto dto, HttpServletRequest request) {
  103. QuestionNo entity = Transform.dtoToEntity(dto);
  104. entity = questionNoService.add(entity);
  105. managerLogService.log(request);
  106. return ResponseHelp.success(entity);
  107. }
  108. @RequestMapping(value = "/delete/no", method = RequestMethod.DELETE)
  109. @ApiOperation(value = "删除题目编号", httpMethod = "DELETE")
  110. public Response<Boolean> delete(int id, HttpServletRequest request) {
  111. questionNoService.delete(id);
  112. managerLogService.log(request);
  113. return ResponseHelp.success(true);
  114. }
  115. @RequestMapping(value = "/list/no", method = RequestMethod.POST)
  116. @ApiOperation(value = "题目编号列表:通过id/编号", httpMethod = "POST")
  117. public Response<List<QuestionNoExtendDto>> listNo(@RequestBody @Validated QuestionNoSearchDto dto, HttpServletRequest request) {
  118. List<QuestionNoRelation> questionNos;
  119. if (dto.getIds() != null && dto.getIds().length > 0){
  120. questionNos = questionNoService.listWithRelationByIds(dto.getIds());
  121. }else if(dto.getNos() != null && dto.getNos().length > 0){
  122. questionNos = questionNoService.listWithRelationByNos(dto.getNos(), dto.getModule());
  123. }else{
  124. throw new ParameterException("需要id或编号");
  125. }
  126. List<QuestionNoExtendDto> questionNoExtendDtos = Transform.convert(questionNos, QuestionNoExtendDto.class);
  127. return ResponseHelp.success(questionNoExtendDtos);
  128. }
  129. @RequestMapping(value = "/search/stem", method = RequestMethod.POST)
  130. @ApiOperation(value = "搜索题目编号列表:题干搜索", httpMethod = "POST")
  131. public Response<PageMessage<QuestionNoExtendDto>> searchStem(@RequestBody @Validated QuestionNoSearchDto dto, HttpServletRequest request) {
  132. PageResult<QuestionNoRelation> p = questionNoService.searchStem(dto.getPage(), dto.getSize(), dto.getContent());
  133. List<QuestionNoExtendDto> pr = Transform.convert(p, QuestionNoExtendDto.class);
  134. return ResponseHelp.success(pr, dto.getPage(), dto.getSize(), p.getTotal());
  135. }
  136. @RequestMapping(value = "/search/no", method = RequestMethod.POST)
  137. @ApiOperation(value = "搜索题目编号列表:题目编号搜索", httpMethod = "POST")
  138. public Response<PageMessage<QuestionNoExtendDto>> searchNo(@RequestBody @Validated QuestionNoSearchDto dto, HttpServletRequest request) {
  139. PageResult<QuestionNoRelation> p = questionNoService.searchNo(dto.getPage(), dto.getSize(), dto.getKeyword(), dto.getModule());
  140. List<QuestionNoExtendDto> pr = Transform.convert(p, QuestionNoExtendDto.class);
  141. return ResponseHelp.success(pr, dto.getPage(), dto.getSize(), p.getTotal());
  142. }
  143. @RequestMapping(value = "/ask/edit", method = RequestMethod.PUT)
  144. @ApiOperation(value = "修改提问信息", httpMethod = "PUT")
  145. public Response<Boolean> editAsk(@RequestBody @Validated UserAskQuestionDto dto, HttpServletRequest request) {
  146. UserAskQuestion entity = Transform.dtoToEntity(dto);
  147. UserAskQuestion in = userAskQuestionService.get(entity.getId());
  148. // 调整回答
  149. if(!entity.getAnswer().isEmpty() || !in.getAnswer().equals(entity.getAnswer())){
  150. entity.setAnswerTime(new Date());
  151. entity.setAnswerStatus(AskStatus.ANSWER.index);
  152. Manager manager = shiroHelp.getLoginManager();
  153. entity.setManagerId(manager.getId());
  154. }
  155. entity = userAskQuestionService.edit(entity);
  156. if (dto.getOther() !=null && dto.getOther().length > 0){
  157. // 更新回答排序
  158. userAskQuestionService.updateOrder(dto.getOther());
  159. }
  160. managerLogService.log(request);
  161. return ResponseHelp.success(true);
  162. }
  163. @RequestMapping(value = "/ask/detail", method = RequestMethod.GET)
  164. @ApiOperation(value = "提问详情", httpMethod = "GET")
  165. public Response<UserAskQuestionDetailDto> detailAsk(@RequestParam int id, HttpServletRequest request) {
  166. UserAskQuestion entity = userAskQuestionService.get(id);
  167. UserAskQuestionDetailDto dto = Transform.convert(entity, UserAskQuestionDetailDto.class);
  168. if (entity.getManagerId() != null && entity.getManagerId() > 0){
  169. Manager manager = managerService.get(entity.getManagerId());
  170. if (manager != null)
  171. dto.setManager(Transform.convert(manager, ManagerExtendDto.class));
  172. }
  173. User user = usersService.get(entity.getUserId());
  174. dto.setUser(Transform.convert(user, UserExtendDto.class));
  175. Question question = questionService.get(entity.getQuestionId());
  176. dto.setQuestion(Transform.convert(question, QuestionExtendDto.class));
  177. QuestionNo questionNo = questionNoService.get(entity.getQuestionNoId());
  178. dto.setQuestionNo(Transform.convert(questionNo, QuestionNoExtendDto.class));
  179. // 所有回答
  180. List<UserAskQuestion> userAskList = userAskQuestionService.listByQuestion(entity.getQuestionId(), true);
  181. dto.setOthers(Transform.convert(userAskList, UserAskQuestionExtendDto.class));
  182. return ResponseHelp.success(dto);
  183. }
  184. @RequestMapping(value = "/ask/list", method = RequestMethod.GET)
  185. @ApiOperation(value = "提问列表", httpMethod = "GET")
  186. public Response<PageMessage<UserAskQuestionListDto>> listAsk(
  187. @RequestParam(required = false, defaultValue = "1") int page,
  188. @RequestParam(required = false, defaultValue = "100") int size,
  189. @RequestParam(required = false) String type,
  190. @RequestParam(required = false) Integer category,
  191. @RequestParam(required = false) Number userId,
  192. @RequestParam(required = false) Number questionNoId,
  193. @RequestParam(required = false) String target,
  194. @RequestParam(required = false) Integer status,
  195. @RequestParam(required = false) Integer showStatus,
  196. @RequestParam(required = false) Integer moneyRang,
  197. @RequestParam(required = false, defaultValue = "id") String order,
  198. @RequestParam(required = false, defaultValue = "desc") String direction,
  199. HttpSession session) {
  200. Page<UserAskQuestion> p = userAskQuestionService.listWithUser(page, size, type, category, userId, questionNoId, AskTarget.ValueOf(target), AskStatus.ValueOf(status), showStatus, MoneyRange.ValueOf(moneyRang), order, DirectionStatus.ValueOf(direction));
  201. List<UserAskQuestionListDto> pr = Transform.convert(p, UserAskQuestionListDto.class);
  202. // 绑定题目
  203. Collection questionIds = Transform.getIds(p, UserAskQuestion.class, "questionId");
  204. List<Question> questionList = questionService.select(questionIds);
  205. Transform.combine(pr, questionList, UserAskQuestionListDto.class, "questionId", "question", Question.class, "id", QuestionExtendDto.class);
  206. // 绑定题目编号
  207. Collection questionNoIds = Transform.getIds(p, UserAskQuestion.class, "questionNoId");
  208. List<QuestionNo> questionNoList = questionNoService.select(questionNoIds);
  209. Transform.combine(pr, questionNoList, UserAskQuestionListDto.class, "questionNoId", "questionNo", QuestionNo.class, "id", QuestionNoExtendDto.class);
  210. // 绑定用户
  211. Collection userIds = Transform.getIds(p, UserAskQuestion.class, "userId");
  212. List<User> userList = usersService.select(userIds);
  213. Transform.combine(pr, userList, UserAskQuestionListDto.class, "userId", "user", User.class, "id", UserExtendDto.class);
  214. // 绑定管理员
  215. Collection managerIds = Transform.getIds(p, UserAskQuestion.class, "managerId");
  216. List<Manager> managerList = managerService.select(managerIds);
  217. Transform.combine(pr, managerList, UserAskQuestionListDto.class, "managerId", "manager", Manager.class, "id", ManagerExtendDto.class);
  218. return ResponseHelp.success(pr, page, size, p.getTotal());
  219. }
  220. }