GoldenQuotesController.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package top.lvzhiqiang.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.ResponseBody;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import top.lvzhiqiang.dto.R;
  7. import top.lvzhiqiang.entity.GoldenQuotes;
  8. import top.lvzhiqiang.enumeration.ResultCodeEnum;
  9. import top.lvzhiqiang.exception.BusinessException;
  10. import top.lvzhiqiang.exception.ParameterException;
  11. import top.lvzhiqiang.mapper.CoinMapper;
  12. import top.lvzhiqiang.util.DateUtils;
  13. import top.lvzhiqiang.util.StringUtils;
  14. import javax.annotation.Resource;
  15. import java.time.LocalDate;
  16. /**
  17. * 金句Controller
  18. *
  19. * @author lvzhiqiang
  20. * 2025/9/22 15:36
  21. */
  22. @RestController
  23. @RequestMapping("/goldenQuotes")
  24. public class GoldenQuotesController {
  25. @Resource
  26. private CoinMapper coinMapper;
  27. /**
  28. * insertOrUpdateWatchlist
  29. *
  30. * @author lvzhiqiang
  31. * 2025/9/22 15:36
  32. */
  33. @RequestMapping("/insertOrUpdateGoldenQuotes")
  34. @ResponseBody
  35. public R insertOrUpdateGoldenQuotes(String id, String author, String sourcePlatform, String originalUrl, String publishTime, String isPinned, String pinPriority, String tags, String content, String crudType, String userName) {
  36. if (StringUtils.isEmpty(crudType)) {
  37. throw new ParameterException("crudType为空!");
  38. }
  39. Integer userId;
  40. if (StringUtils.isEmpty(userName)) {
  41. throw new ParameterException("userName为空!");
  42. } else {
  43. JSONObject coinUser = coinMapper.findUserByUsername(userName);
  44. if (coinUser == null) {
  45. throw new ParameterException("用户不存在!");
  46. }
  47. userId = coinUser.getInteger("id");
  48. }
  49. if ("1".equals(crudType)) {
  50. // 新增
  51. if (StringUtils.isEmpty(author) || StringUtils.isEmpty(sourcePlatform) || StringUtils.isEmpty(content)) {
  52. throw new ParameterException("参数为空!");
  53. }
  54. GoldenQuotes goldenQuotes = new GoldenQuotes();
  55. goldenQuotes.setAuthor(author);
  56. goldenQuotes.setSourcePlatform(sourcePlatform);
  57. goldenQuotes.setPublishTime(StringUtils.isEmpty(publishTime) ? LocalDate.now() : LocalDate.parse(publishTime, DateUtils.dateFormatter));
  58. goldenQuotes.setContent(content);
  59. goldenQuotes.setOriginalUrl(originalUrl);
  60. goldenQuotes.setIsPinned(StringUtils.isEmpty(isPinned) ? 2 : Integer.parseInt(isPinned));
  61. goldenQuotes.setPinPriority(StringUtils.isEmpty(pinPriority) ? 0 : Integer.parseInt(pinPriority));
  62. goldenQuotes.setTags(tags);
  63. coinMapper.insertGoldenQuotes(goldenQuotes);
  64. } else if ("2".equals(crudType)) {
  65. // 修改
  66. if (StringUtils.isEmpty(id)) {
  67. throw new ParameterException("id为空!");
  68. }
  69. GoldenQuotes goldenQuotes = coinMapper.findGoldenQuotesById(id);
  70. if (goldenQuotes == null) {
  71. throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "id 不存在!");
  72. }
  73. if (StringUtils.isNotEmpty(author)) {
  74. goldenQuotes.setAuthor(author);
  75. }
  76. if (StringUtils.isNotEmpty(sourcePlatform)) {
  77. goldenQuotes.setSourcePlatform(sourcePlatform);
  78. }
  79. if (StringUtils.isNotEmpty(originalUrl)) {
  80. goldenQuotes.setOriginalUrl(originalUrl);
  81. }
  82. if (StringUtils.isNotEmpty(publishTime)) {
  83. goldenQuotes.setPublishTime(LocalDate.parse(publishTime, DateUtils.dateFormatter));
  84. }
  85. if (StringUtils.isNotEmpty(isPinned)) {
  86. goldenQuotes.setIsPinned(Integer.valueOf(isPinned));
  87. }
  88. if (StringUtils.isNotEmpty(pinPriority)) {
  89. goldenQuotes.setPinPriority(Integer.valueOf(pinPriority));
  90. }
  91. if (StringUtils.isNotEmpty(tags)) {
  92. goldenQuotes.setTags(tags);
  93. }
  94. if (StringUtils.isNotEmpty(content)) {
  95. goldenQuotes.setContent(content);
  96. }
  97. coinMapper.updateGoldenQuotes(goldenQuotes);
  98. }
  99. return R.ok().data("success");
  100. }
  101. }