| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package top.lvzhiqiang.controller;
- import com.alibaba.fastjson.JSONObject;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.RestController;
- import top.lvzhiqiang.dto.R;
- import top.lvzhiqiang.entity.GoldenQuotes;
- import top.lvzhiqiang.enumeration.ResultCodeEnum;
- import top.lvzhiqiang.exception.BusinessException;
- import top.lvzhiqiang.exception.ParameterException;
- import top.lvzhiqiang.mapper.CoinMapper;
- import top.lvzhiqiang.util.DateUtils;
- import top.lvzhiqiang.util.StringUtils;
- import javax.annotation.Resource;
- import java.time.LocalDate;
- /**
- * 金句Controller
- *
- * @author lvzhiqiang
- * 2025/9/22 15:36
- */
- @RestController
- @RequestMapping("/goldenQuotes")
- public class GoldenQuotesController {
- @Resource
- private CoinMapper coinMapper;
- /**
- * insertOrUpdateWatchlist
- *
- * @author lvzhiqiang
- * 2025/9/22 15:36
- */
- @RequestMapping("/insertOrUpdateGoldenQuotes")
- @ResponseBody
- 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) {
- if (StringUtils.isEmpty(crudType)) {
- throw new ParameterException("crudType为空!");
- }
- Integer userId;
- if (StringUtils.isEmpty(userName)) {
- throw new ParameterException("userName为空!");
- } else {
- JSONObject coinUser = coinMapper.findUserByUsername(userName);
- if (coinUser == null) {
- throw new ParameterException("用户不存在!");
- }
- userId = coinUser.getInteger("id");
- }
- if ("1".equals(crudType)) {
- // 新增
- if (StringUtils.isEmpty(author) || StringUtils.isEmpty(sourcePlatform) || StringUtils.isEmpty(content)) {
- throw new ParameterException("参数为空!");
- }
- GoldenQuotes goldenQuotes = new GoldenQuotes();
- goldenQuotes.setAuthor(author);
- goldenQuotes.setSourcePlatform(sourcePlatform);
- goldenQuotes.setPublishTime(StringUtils.isEmpty(publishTime) ? LocalDate.now() : LocalDate.parse(publishTime, DateUtils.dateFormatter));
- goldenQuotes.setContent(content);
- goldenQuotes.setOriginalUrl(originalUrl);
- goldenQuotes.setIsPinned(StringUtils.isEmpty(isPinned) ? 2 : Integer.parseInt(isPinned));
- goldenQuotes.setPinPriority(StringUtils.isEmpty(pinPriority) ? 0 : Integer.parseInt(pinPriority));
- goldenQuotes.setTags(tags);
- coinMapper.insertGoldenQuotes(goldenQuotes);
- } else if ("2".equals(crudType)) {
- // 修改
- if (StringUtils.isEmpty(id)) {
- throw new ParameterException("id为空!");
- }
- GoldenQuotes goldenQuotes = coinMapper.findGoldenQuotesById(id);
- if (goldenQuotes == null) {
- throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "id 不存在!");
- }
- if (StringUtils.isNotEmpty(author)) {
- goldenQuotes.setAuthor(author);
- }
- if (StringUtils.isNotEmpty(sourcePlatform)) {
- goldenQuotes.setSourcePlatform(sourcePlatform);
- }
- if (StringUtils.isNotEmpty(originalUrl)) {
- goldenQuotes.setOriginalUrl(originalUrl);
- }
- if (StringUtils.isNotEmpty(publishTime)) {
- goldenQuotes.setPublishTime(LocalDate.parse(publishTime, DateUtils.dateFormatter));
- }
- if (StringUtils.isNotEmpty(isPinned)) {
- goldenQuotes.setIsPinned(Integer.valueOf(isPinned));
- }
- if (StringUtils.isNotEmpty(pinPriority)) {
- goldenQuotes.setPinPriority(Integer.valueOf(pinPriority));
- }
- if (StringUtils.isNotEmpty(tags)) {
- goldenQuotes.setTags(tags);
- }
- if (StringUtils.isNotEmpty(content)) {
- goldenQuotes.setContent(content);
- }
- coinMapper.updateGoldenQuotes(goldenQuotes);
- }
- return R.ok().data("success");
- }
- }
|