kimi42345
2020-03-17 0bf1c5465f1f3198c53ad02f3209148afabb2038
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.highdatas.srs.web;
 
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.highdatas.srs.entity.Topic;
import com.highdatas.srs.pojo.CodeMsg;
import com.highdatas.srs.pojo.Result;
import com.highdatas.srs.service.ITopicService;
import com.highdatas.srs.util.DbUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
 
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kimi
 * @since 2020-01-15
 */
@RestController
@RequestMapping("/topic")
public class TopicController {
    @Autowired
    ITopicService topicService;
 
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Result get(@PathVariable String id)  {
        Topic topic = topicService.selectById(id);
        return Result.success(topic);
    }
 
    @RequestMapping(value = "/page/{pageno}", method = RequestMethod.GET)
    public Result page(@PathVariable Integer pageno, HttpServletRequest request)  {
        String title = request.getParameter("title");
        String desp = request.getParameter("desp");
        EntityWrapper<Topic> topicEntityWrapper = new EntityWrapper<>();
        if (!StringUtils.isEmpty(title)) {
            topicEntityWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(desp)) {
            topicEntityWrapper.like("desp", desp);
        }
        Page<Topic> topicPage= new Page<Topic>(pageno, 15);
        topicEntityWrapper.orderBy("create_time desc");
        Page<Topic> result = topicService.selectPage(topicPage, topicEntityWrapper);
        List<Topic> records = result.getRecords();
        JSONArray array = new JSONArray();
        for (Topic record : records) {
            JSONObject one = (JSONObject) JSON.toJSON(record);
            Date createTime = record.getCreateTime();
            String s = DbUtils.convert_before(createTime.getTime());
            one.fluentPut("time", s);
            array.add(one);
        }
        JSONObject object = new JSONObject();
        object.fluentPut("records", array);
        object.fluentPut("total", result.getTotal());
        object.fluentPut("maxPage", result.getPages());
        object.fluentPut("pageno", result.getCurrent());
 
        return Result.success(object);
    }
 
    @RequestMapping(value = "/exam/{id}", method = RequestMethod.GET)
    public Result exam(@PathVariable String id)  {
        Topic topic = topicService.selectById(id);
        topic.setExamine(true);
 
        boolean insert = topic.updateById();
 
        if (insert) {
            return Result.success(null);
        } else {
            return Result.error(CodeMsg.INSERT_ERROR);
        }
 
    }
 
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public Result add(HttpServletRequest request)  {
        String id = request.getParameter("id");
        String title = request.getParameter("title");
        String userId = request.getParameter("userId");
        if(StringUtils.isEmpty(title)) {
            return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
        }
        String desp = request.getParameter("desp");
        if (StringUtils.isEmpty(id)) {
            Topic topic = new Topic().setUserId(userId).setTitle(title).setDesp(desp).setCreateTime(new Date());
            topic.setId(DbUtils.getUUID());
            boolean insert = topic.insert();
            if (insert) {
                return Result.success(null);
            } else {
                return Result.error(CodeMsg.INSERT_ERROR);
            }
        } else {
            Topic topic = topicService.selectById(id);
            boolean b = topic.setDesp(desp).setTitle(title).updateById();
            if (b) {
                return Result.success(null);
            } else {
                return Result.error(CodeMsg.INSERT_ERROR);
            }
        }
 
 
    }
}