QuestionController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.AskStatus;
  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());
  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(entity.getAnswer() != null && (!entity.getAnswer().isEmpty() || !in.getAnswer().equals(entity.getAnswer()))){
  156. entity.setAnswerTime(new Date());
  157. entity.setAnswerStatus(AskStatus.ANSWER.index);
  158. Manager manager = shiroHelp.getLoginManager();
  159. entity.setManagerId(manager.getId());
  160. User user = usersService.get(in.getUserId());
  161. messageExtendService.sendAskQuestion(user, entity);
  162. }
  163. if (dto.getIgnoreStatus() != null && dto.getIgnoreStatus() > 0){
  164. entity.setAnswerStatus(AskStatus.IGNORE.index);
  165. User user = usersService.get(in.getUserId());
  166. messageExtendService.sendAskQuestion(user, entity);
  167. }
  168. entity = userAskQuestionService.edit(entity);
  169. if (dto.getOther() !=null && dto.getOther().length > 0){
  170. // 更新回答排序
  171. userAskQuestionService.updateOrder(dto.getOther());
  172. }
  173. managerLogService.log(request);
  174. return ResponseHelp.success(true);
  175. }
  176. @RequestMapping(value = "/ask/detail", method = RequestMethod.GET)
  177. @ApiOperation(value = "提问详情", httpMethod = "GET")
  178. public Response<UserAskQuestionDetailDto> detailAsk(@RequestParam int id, HttpServletRequest request) {
  179. UserAskQuestion entity = userAskQuestionService.get(id);
  180. UserAskQuestionDetailDto dto = Transform.convert(entity, UserAskQuestionDetailDto.class);
  181. if (entity.getManagerId() != null && entity.getManagerId() > 0){
  182. Manager manager = managerService.get(entity.getManagerId());
  183. if (manager != null)
  184. dto.setManager(Transform.convert(manager, ManagerExtendDto.class));
  185. }
  186. User user = usersService.get(entity.getUserId());
  187. dto.setUser(Transform.convert(user, UserExtendDto.class));
  188. Question question = questionService.get(entity.getQuestionId());
  189. dto.setQuestion(Transform.convert(question, QuestionExtendDto.class));
  190. QuestionNo questionNo = questionNoService.get(entity.getQuestionNoId());
  191. dto.setQuestionNo(Transform.convert(questionNo, QuestionNoExtendDto.class));
  192. // 所有回答
  193. List<UserAskQuestion> userAskList = userAskQuestionService.listByQuestion(entity.getQuestionId(), true);
  194. dto.setOthers(Transform.convert(userAskList, UserAskQuestionExtendDto.class));
  195. return ResponseHelp.success(dto);
  196. }
  197. @RequestMapping(value = "/ask/list", method = RequestMethod.GET)
  198. @ApiOperation(value = "提问列表", httpMethod = "GET")
  199. public Response<PageMessage<UserAskQuestionListDto>> listAsk(
  200. @RequestParam(required = false, defaultValue = "1") int page,
  201. @RequestParam(required = false, defaultValue = "100") int size,
  202. @RequestParam(required = false) String askModule,
  203. @RequestParam(required = false) String questionType,
  204. @RequestParam(required = false) String questionModule,
  205. @RequestParam(required = false) Number userId,
  206. @RequestParam(required = false) Number questionNoId,
  207. @RequestParam(required = false) String target,
  208. @RequestParam(required = false) Integer answerStatus,
  209. @RequestParam(required = false) Integer showStatus,
  210. @RequestParam(required = false) Integer moneyRang,
  211. @RequestParam(required = false) Boolean hasRecord,
  212. @RequestParam(required = false) String startTime,
  213. @RequestParam(required = false) String endTime,
  214. @RequestParam(required = false, defaultValue = "id") String order,
  215. @RequestParam(required = false, defaultValue = "desc") String direction,
  216. HttpSession session) {
  217. Page<UserAskQuestion> p = userAskQuestionService.listAdmin(page, size, askModule, questionType, questionModule, userId, questionNoId, AskTarget.ValueOf(target), AskStatus.ValueOf(answerStatus), showStatus, MoneyRange.ValueOf(moneyRang), hasRecord, startTime, endTime, order, DirectionStatus.ValueOf(direction));
  218. List<UserAskQuestionListDto> pr = Transform.convert(p, UserAskQuestionListDto.class);
  219. // 绑定题目
  220. Collection questionIds = Transform.getIds(p, UserAskQuestion.class, "questionId");
  221. List<Question> questionList = questionService.select(questionIds);
  222. Transform.combine(pr, questionList, UserAskQuestionListDto.class, "questionId", "question", Question.class, "id", QuestionExtendDto.class);
  223. // 绑定题目编号
  224. Collection questionNoIds = Transform.getIds(p, UserAskQuestion.class, "questionNoId");
  225. List<QuestionNo> questionNoList = questionNoService.select(questionNoIds);
  226. Transform.combine(pr, questionNoList, UserAskQuestionListDto.class, "questionNoId", "questionNo", QuestionNo.class, "id", QuestionNoExtendDto.class);
  227. // 绑定用户
  228. Collection userIds = Transform.getIds(p, UserAskQuestion.class, "userId");
  229. List<User> userList = usersService.select(userIds);
  230. Transform.combine(pr, userList, UserAskQuestionListDto.class, "userId", "user", User.class, "id", UserExtendDto.class);
  231. // 绑定管理员
  232. Collection managerIds = Transform.getIds(p, UserAskQuestion.class, "managerId");
  233. List<Manager> managerList = managerService.select(managerIds);
  234. Transform.combine(pr, managerList, UserAskQuestionListDto.class, "managerId", "manager", Manager.class, "id", ManagerExtendDto.class);
  235. return ResponseHelp.success(pr, page, size, p.getTotal());
  236. }
  237. }