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;
/**
*
* 前端控制器
*
*
* @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 topicEntityWrapper = new EntityWrapper<>();
if (!StringUtils.isEmpty(title)) {
topicEntityWrapper.like("title", title);
}
if (!StringUtils.isEmpty(desp)) {
topicEntityWrapper.like("desp", desp);
}
Page topicPage= new Page(pageno, 15);
topicEntityWrapper.orderBy("create_time desc");
Page result = topicService.selectPage(topicPage, topicEntityWrapper);
List 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);
}
}
}
}