TaPostController.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package com.yunzhi.demo.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.yunzhi.demo.common.BaseController;
  6. import com.yunzhi.demo.common.Constants;
  7. import com.yunzhi.demo.common.ResponseBean;
  8. import com.yunzhi.demo.common.StringUtils;
  9. import com.yunzhi.demo.entity.*;
  10. import com.yunzhi.demo.service.*;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13. import io.swagger.annotations.ApiParam;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.web.bind.annotation.PathVariable;
  18. import org.springframework.web.bind.annotation.RequestBody;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestMethod;
  21. import org.springframework.web.bind.annotation.RequestParam;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * <p>
  29. * 文章表 前端控制器
  30. * </p>
  31. *
  32. * @author yansen
  33. * @since 2021-04-15
  34. */
  35. @Api(tags = "文章表")
  36. @RestController
  37. @RequestMapping("/")
  38. public class TaPostController extends BaseController {
  39. private final Logger logger = LoggerFactory.getLogger(TaPostController.class);
  40. @Autowired
  41. public ITaPostService iTaPostService;
  42. @Autowired
  43. public ITdPostTypeService iTdPostTypeService;
  44. @Autowired
  45. public ITaPostDataService iTaPostDataService;
  46. @Autowired
  47. public ITaPostTestService iTaPostTestService;
  48. @Autowired
  49. public ITaPostSaveService iTaPostSaveService;
  50. @Autowired
  51. public ITaShareSettingService iTaShareSettingService;
  52. /**
  53. * 分页查询列表
  54. * @param pageNum
  55. * @param pageSize
  56. * @return
  57. */
  58. @RequestMapping(value="/{client}/post",method= RequestMethod.GET)
  59. @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
  60. public ResponseBean taPostList(@ApiParam(value = "客户端", allowableValues = "admin,ma") @PathVariable String client,
  61. @ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
  62. @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
  63. @ApiParam("名称") @RequestParam(value = "name", required = false) String name,
  64. @ApiParam("分类") @RequestParam(value = "typeId", required = false) String typeId,
  65. @ApiParam("状态") @RequestParam(value = "status", required = false) Integer status) throws Exception{
  66. IPage<TaPost> pg = new Page<>(pageNum, pageSize);
  67. QueryWrapper<TaPost> queryWrapper = new QueryWrapper<TaPost>()
  68. .like(!StringUtils.isEmpty(name), "name", name)
  69. .eq(!StringUtils.isEmpty(typeId), "type_id", typeId)
  70. .eq(null != status, "status", status)
  71. .gt("status", Constants.STATUS_DELETED)
  72. .orderByDesc("weight")
  73. .orderByDesc("create_date");
  74. IPage<TaPost> result = iTaPostService.page(pg, queryWrapper);
  75. return ResponseBean.success(result);
  76. }
  77. /**
  78. * 保存对象
  79. * @param taPost 实体对象
  80. * @return
  81. */
  82. @RequestMapping(value="/admin/post",method= RequestMethod.POST)
  83. @ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
  84. public ResponseBean taPostAdd(@ApiParam("保存内容") @RequestBody TaPost taPost) throws Exception{
  85. TdPostType postType = iTdPostTypeService.getById(taPost.getTypeId());
  86. if (null != postType) {
  87. taPost.setTypeName(postType.getName());
  88. }
  89. if (iTaPostService.save(taPost)){
  90. TaPostData postData = new TaPostData();
  91. postData.setPostId(taPost.getPostId());
  92. iTaPostDataService.save(postData);
  93. return ResponseBean.success(iTaPostService.getById(taPost.getPostId()));
  94. }else {
  95. return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
  96. }
  97. }
  98. /**
  99. * 根据id删除对象
  100. * @param id 实体ID
  101. */
  102. @RequestMapping(value="/admin/post/{id}", method= RequestMethod.DELETE)
  103. @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
  104. public ResponseBean taPostDelete(@ApiParam("对象ID") @PathVariable Integer id) throws Exception {
  105. TaPost taPost = iTaPostService.getById(id);
  106. if (null == taPost || Constants.STATUS_DELETED.equals(taPost.getStatus())) {
  107. return ResponseBean.success("success");
  108. }
  109. taPost.setStatus(Constants.STATUS_DELETED);
  110. if(iTaPostService.updateById(taPost)){
  111. return ResponseBean.success("success");
  112. }else {
  113. return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
  114. }
  115. }
  116. /**
  117. * 修改对象
  118. * @param id 实体ID
  119. * @param taPost 实体对象
  120. * @return
  121. */
  122. @RequestMapping(value="/admin/post/{id}",method= RequestMethod.PUT)
  123. @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
  124. public ResponseBean taPostUpdate(@ApiParam("对象ID") @PathVariable String id,
  125. @ApiParam("更新内容") @RequestBody TaPost taPost) throws Exception{
  126. TdPostType postType = iTdPostTypeService.getById(taPost.getTypeId());
  127. if (null != postType) {
  128. taPost.setTypeName(postType.getName());
  129. }
  130. if (iTaPostService.updateById(taPost)){
  131. return ResponseBean.success(iTaPostService.getById(id));
  132. }else {
  133. return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
  134. }
  135. }
  136. /**
  137. * 根据id查询文章详情
  138. * @param id 实体ID
  139. */
  140. @RequestMapping(value="/{client}/post/{id}",method= RequestMethod.GET)
  141. @ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = TaPost.class)
  142. public ResponseBean taPostGet(@ApiParam(value = "客户端", allowableValues = "admin,ma") @PathVariable String client,
  143. @ApiParam("文章ID") @PathVariable String id) throws Exception{
  144. // 获取文章信息
  145. TaPost taPost = iTaPostService.getById(id);
  146. if (null == taPost || Constants.STATUS_DELETED.equals(taPost.getStatus())) {
  147. throw new Exception("文章内容不存在");
  148. }
  149. // 获取统计信息
  150. TaPostData taPostData = iTaPostDataService.getById(id);
  151. taPost.setPostData(taPostData);
  152. // 分享信息
  153. TaShareSetting taShareSetting = iTaShareSettingService.getSettingBy(Constants.RESOURCE_TYPE_POST, id);
  154. if (!"admin".equals(client)) {
  155. if (Constants.STATUS_NORMAL.equals(taShareSetting.getStatus())) {
  156. taPost.setShareSetting(taShareSetting);
  157. }
  158. } else {
  159. taPost.setShareSetting(taShareSetting);
  160. }
  161. // 获取试题
  162. List<TaPostTest> postTestList = new ArrayList<>();
  163. if ("admin".equals(client)) {
  164. postTestList = iTaPostTestService.getListByPostId(id);
  165. } else {
  166. // 当前人员是否收藏过
  167. TaPerson taPerson = getCurrentPerson();
  168. boolean saved = iTaPostSaveService.checkSaved(taPerson.getPersonId(), id);
  169. taPost.setIsSaved(saved);
  170. // 埋点数据
  171. iTaPostDataService.recordBy(id, taPerson);
  172. // 随机取题目
  173. if (null != taPost.getAnswerNum() && taPost.getAnswerNum() > 0) {
  174. for (int i = 0; i < taPost.getAnswerNum(); i++) {
  175. TaPostTest taPostTest = iTaPostTestService.getRandRowBy(id);
  176. taPostTest.setCorrectAnswers(null);
  177. postTestList.add(taPostTest);
  178. }
  179. }
  180. }
  181. taPost.setPostTestList(postTestList);
  182. return ResponseBean.success(taPost);
  183. }
  184. }