QuestionController.java 14 KB

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