InformController.java
3.32 KB
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
package com.project.demo.web;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.demo.domain.DataTablePageUtil;
import com.project.demo.domain.Inform;
import com.project.demo.domain.Users;
import com.project.demo.mapper.AjaxJson;
import com.project.demo.service.InformService;
import com.project.demo.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
/**
* Created by xuwenhao on 2018/7/8.
*/
@Controller
@RequestMapping("informs")
public class InformController {
@Autowired
private UsersService usersService;
@Autowired
private InformService informService;
@RequestMapping(method = RequestMethod.GET)
public String findAll() {
return "/inform/index";
}
@RequestMapping(value = "/informList",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String investigateList(final HttpSession session, final HttpServletRequest request){
Users currentUser = (Users) session.getAttribute("users");
DataTablePageUtil<Inform> dataTable = new DataTablePageUtil<Inform>(request);
PageHelper.startPage(dataTable.getPage_num(), dataTable.getPage_size());//分页
List<Inform> informs = informService.search();
informs.stream().forEach(d ->{
d.setUserName(usersService.findById(d.getUserId()).getName());
});
PageInfo<Inform> pageInfo = new PageInfo<>(informs);
//封装数据给DataTables
dataTable.setDraw(dataTable.getDraw());
dataTable.setData(pageInfo.getList());
dataTable.setRecordsTotal((int)pageInfo.getTotal());
dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
//返回数据到页面
String jsonObject = JSONObject.toJSONString(dataTable);
return jsonObject;
}
@RequestMapping(value = "/create")
public String create() {
return "/welcome/new";
}
@RequestMapping(value = "/new", method = RequestMethod.POST)
public String save(final HttpSession session, @RequestParam String information){
Users users = (Users)session.getAttribute("users");
Inform inform = new Inform();
inform.setState(true);
inform.setInformation(information);
inform.setCreateTime(new Date());
inform.setUserId(users.getId());
informService.saveInform(inform);
return "redirect:/welcome";
}
@RequestMapping(value = "/deleteInform", method = RequestMethod.POST)
public AjaxJson deleteInform(final HttpSession session, @RequestParam Integer id){
AjaxJson ajaxJson = new AjaxJson();
Users users = (Users)session.getAttribute("users");
informService.deleteInform(id);
ajaxJson.setCode(1);
ajaxJson.setMsg("删除成功");
return ajaxJson;
}
}