AdminNewsApiController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package com.goafanti.admin.controller;
  2. import java.text.ParseException;
  3. import java.util.Arrays;
  4. import javax.annotation.Resource;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.validation.Valid;
  7. import org.springframework.beans.BeanUtils;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.validation.BindingResult;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import com.goafanti.common.bo.Result;
  15. import com.goafanti.common.constant.AFTConstants;
  16. import com.goafanti.common.constant.ErrorConstants;
  17. import com.goafanti.common.controller.BaseApiController;
  18. import com.goafanti.common.enums.AttachmentType;
  19. import com.goafanti.common.model.News;
  20. import com.goafanti.common.utils.DateUtils;
  21. import com.goafanti.common.utils.StringUtils;
  22. import com.goafanti.core.mybatis.JDBCIdGenerator;
  23. import com.goafanti.core.shiro.token.TokenManager;
  24. import com.goafanti.news.bo.InputNews;
  25. import com.goafanti.news.enums.NewsFields;
  26. import com.goafanti.news.service.NewsService;
  27. /**
  28. * 新闻
  29. */
  30. @RestController
  31. @RequestMapping(value = "/api/admin/news")
  32. public class AdminNewsApiController extends BaseApiController {
  33. @Resource
  34. private NewsService newsService;
  35. @Autowired
  36. private JDBCIdGenerator idGenerator;
  37. /**
  38. * 新闻列表
  39. */
  40. @RequestMapping(value = "/list", method = RequestMethod.GET)
  41. public Result list(Integer type, String title, String author, String startCreateTime, String endCreateTime,
  42. String source, Integer hot, String pageSize, String pageNo) {
  43. Result res = new Result();
  44. if (null == type) {
  45. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻类型"));
  46. return res;
  47. }
  48. Integer pNo = 1;
  49. Integer pSize = 10;
  50. if (StringUtils.isNumeric(pageSize)) {
  51. pSize = Integer.parseInt(pageSize);
  52. }
  53. if (StringUtils.isNumeric(pageNo)) {
  54. pNo = Integer.parseInt(pageNo);
  55. }
  56. res.setData(newsService.listNews(type, title, author, startCreateTime, endCreateTime, source, hot, pNo, pSize));
  57. return res;
  58. }
  59. /**
  60. * 新增
  61. */
  62. @RequestMapping(value = "/add", method = RequestMethod.POST)
  63. public Result add(@Valid InputNews news, BindingResult bindingResult, String createTimeFormattedDate) {
  64. Result res = new Result();
  65. if (bindingResult.hasErrors()) {
  66. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  67. NewsFields.getFieldDesc(bindingResult.getFieldError().getField())));
  68. return res;
  69. }
  70. res = disposeInputNews(res, news, createTimeFormattedDate);
  71. if (!res.getError().isEmpty()) {
  72. return res;
  73. }
  74. newsService.save(idGenerator.generateId(), (News) res.getData());
  75. return res;
  76. }
  77. /**
  78. * 修改
  79. */
  80. @RequestMapping(value = "/update", method = RequestMethod.POST)
  81. public Result update(InputNews news, BindingResult bindingResult, String createTimeFormattedDate) {
  82. Result res = new Result();
  83. if (bindingResult.hasErrors()) {
  84. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  85. NewsFields.getFieldDesc(bindingResult.getFieldError().getField())));
  86. return res;
  87. }
  88. if (null == news.getId()) {
  89. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻Id"));
  90. return res;
  91. }
  92. res = disposeInputNews(res, news, createTimeFormattedDate);
  93. if (!res.getError().isEmpty()) {
  94. return res;
  95. }
  96. newsService.update(news.getId(), (News) res.getData());
  97. return res;
  98. }
  99. /**
  100. * 详情
  101. */
  102. @RequestMapping(value = "/detail", method = RequestMethod.GET)
  103. public Result detail(Long id) {
  104. Result res = new Result();
  105. if (null == id) {
  106. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻Id"));
  107. return res;
  108. }
  109. res.setData(newsService.findNewsDetail(id));
  110. return res;
  111. }
  112. /**
  113. * 删除
  114. */
  115. @RequestMapping(value = "/delete", method = RequestMethod.POST)
  116. public Result delete(@RequestParam(name = "ids[]", required = false) String[] ids) {
  117. Result res = new Result();
  118. if (ids == null || ids.length < 1) {
  119. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
  120. } else {
  121. res.setData(newsService.batchDeleteByPrimaryKey(Arrays.asList(ids)));
  122. }
  123. return res;
  124. }
  125. /**
  126. * 新闻题图图片上传
  127. */
  128. @RequestMapping(value = "/upload", method = RequestMethod.POST)
  129. public Result upload(HttpServletRequest req, String sign) {
  130. Result res = new Result();
  131. AttachmentType attachmentType = AttachmentType.getField(sign);
  132. if (attachmentType == AttachmentType.NEWS_TITLE_PICTURE ||
  133. attachmentType == AttachmentType.NEWS_CONTENT_PICTURE) {
  134. res.setData(handleFiles(res, "/news/", false, req, sign, TokenManager.getAdminId()));
  135. } else {
  136. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
  137. }
  138. return res;
  139. }
  140. private Result disposeInputNews(Result res, InputNews news, String createTimeFormattedDate) {
  141. if (StringUtils.isBlank(news.getTitle())) {
  142. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻标题"));
  143. return res;
  144. }
  145. if (StringUtils.isBlank(createTimeFormattedDate)) {
  146. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻时间"));
  147. return res;
  148. }
  149. if (null == news.getType()) {
  150. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻类型"));
  151. return res;
  152. }
  153. News n = new News();
  154. BeanUtils.copyProperties(news, n);
  155. try {
  156. n.setCreateTime(DateUtils.parseDate(createTimeFormattedDate, AFTConstants.YYYYMMDD));
  157. } catch (ParseException e) {
  158. }
  159. res.setData(n);
  160. return res;
  161. }
  162. }