首页>代码>基于springboot2+mybatis+thymeleaf+layui整合开发的在线心理咨询管理系统>/YiXinLi-Project/src/main/java/com/pengzhen/yixinli/controller/admin/ArticleController.java
package com.pengzhen.yixinli.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pengzhen.yixinli.common.LoginSession;
import com.pengzhen.yixinli.common.ServerLayResult;
import com.pengzhen.yixinli.entity.Article;
import com.pengzhen.yixinli.entity.Label;
import com.pengzhen.yixinli.service.ArticleService;
import com.pengzhen.yixinli.service.LabelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ArticleController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ArticleService articleService;

    @Autowired
    private LabelService labelService;

    @RequestMapping("/articleUi")
    public String articleListUi() {
        if (!LoginSession.getCurrentUser().getUsername().equals("admin")) {
            return "client/html/index";
        }
        return "admin/article/list";
    }

    @RequestMapping("/articleUiAdd")
    public String articleAddUi() {
        if (!LoginSession.getCurrentUser().getUsername().equals("admin")) {
            return "client/html/index";
        }
        return "admin/article/listform";
    }

    /**
     * 后台文章列表
     *
     * @param page     当前页
     * @param limit    每页多少条
     * @param keyword1 关键字查询
     * @param keyword2
     * @param keyword3
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/article/list")
    public ServerLayResult<Article> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                         @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                         String keyword1, String keyword2, String keyword3) {
        if (keyword1 != null && keyword2 != "" || keyword2 != null && keyword2 != "" || keyword3 != null && keyword3 != "") {
            List<Article> articles = articleService.selectByKeyWord(page, limit, keyword1, keyword2, keyword3);
            ServerLayResult result = new ServerLayResult(0, "", articles.size(), articles);
            return result;
        }
        //封装数据
        ServerLayResult result = new ServerLayResult(0, "", articleService.count(), articleService.selectAll(page, limit));
        return result;
    }


    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/article/del")
    public Map<String, Object> delArticle(@RequestParam("id") int id) {
        Map<String, Object> dataMap = new HashMap<>();
        boolean isSuccess = articleService.deleteByPrimaryKey(id);
        dataMap.put("success", isSuccess);
        return dataMap;
    }


    /**
     * 前台响应json数据
     * 解析保存
     *
     * @param article
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/article/add")
    public Map<String, Object> addArticle(@RequestBody JSONObject article) {
        JSONObject json = JSON.parseObject(article.toJSONString());
        String author = json.getString("author");
        Integer label = json.getInteger("label");
        String title = json.getString("title");
        String content = json.getString("content");
        String status = json.getString("status");
        int temp = 0;
        if (status != null) {
            if (status.equals("on")) {
                temp = 1;
            }
        }
        Label label1 = new Label();
        label1.setId(label);
        Article articles = new Article();
        articles.setAuthor(author);
        articles.setContent(content);
        articles.setTitle(title);
        articles.setStatus(temp);
        articles.setCreateTime(new Date());
        articles.setLabel(label1);
        logger.info(article + "");
        boolean isSuccess = articleService.insert(articles);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("success", isSuccess);
        return dataMap;
    }

    /**
     * 根据前台响应的json对象封装后通过业务方法保存到数据库
     *
     * @param article
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/article/update")
    public Map<String, Object> updateArticle(@RequestBody JSONObject article) {
        JSONObject json = JSON.parseObject(article.toJSONString());
        String author = json.getString("author");
        Integer label = json.getInteger("label");
        Integer id = json.getInteger("id");
        String title = json.getString("title");
        String content = json.getString("content");
        String status = json.getString("status");
        int temp = 0;
        if (status != null) {
            if (status.equals("on")) {
                temp = 1;
            }
        }
        Label label1 = new Label();
        label1.setId(label);
        Article articles = new Article();
        articles.setId(id);
        articles.setAuthor(author);
        articles.setContent(content);
        articles.setTitle(title);
        articles.setStatus(temp);
        articles.setCreateTime(new Date());
        articles.setLabel(label1);
        logger.info(article + "");
        boolean isSuccess = articleService.updateByPrimaryKey(articles);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("success", isSuccess);
        return dataMap;
    }

}


最近下载更多
07311514  LV10 2025年2月17日
李二san  LV1 2024年4月13日
微信网友_6927932988952576  LV12 2024年3月31日
WHY-small  LV15 2023年12月20日
wnnmmb  LV2 2023年10月18日
爱吃鱼的猫Vital  LV6 2023年7月28日
xiaobaitud  LV13 2023年7月10日
微信网友_6461019441418240  LV2 2023年5月6日
夜晚的星666  LV7 2023年5月5日
18836428501  LV1 2023年3月3日
最近浏览更多
13752890376 2025年9月22日
暂无贡献等级
暂无贡献等级
陈小灏  LV18 2025年2月21日
07311514  LV10 2025年2月17日
微信网友_7371218154688512  LV4 2025年2月5日
54ty1212 2025年1月6日
暂无贡献等级
2081aa 2024年6月13日
暂无贡献等级
TY0165  LV20 2024年5月28日
来一杯西瓜冰咩  LV6 2024年5月13日
dddddk 2024年4月23日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友