InformController.java 3.32 KB
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.AjaxJson;
import com.project.demo.domain.DataTablePageUtil;
import com.project.demo.domain.Inform;
import com.project.demo.domain.Users;
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;

    }




}