package com.star.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
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 org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.RequestScope;
import org.springframework.web.servlet.ModelAndView;
import com.star.model.User;
import com.star.service.UserService;
import com.star.validator.UserValidator;
@Controller
@RequestMapping(value = "/user")
@SessionAttributes("UserSession")
public class UserController {
@Autowired
private HttpServletRequest request;
@Autowired
private UserService userService;
private List<User> list = null;
/**
* 请求登录
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView doLogin(@RequestParam("name") String name, @RequestParam("password") String password,
Model model) {
User user = new User();
user.setName(name);
user.setPassword(password);
ModelAndView mv = new ModelAndView();
User u = userService.selectUserByNamePass(user);
if (!u.equals(null)) {
model.addAttribute("UserSession", u);
HttpSession session = request.getSession();
session.setAttribute("currNum", 1);
mv.setViewName("frame");
} else {
mv.addObject("error", "用户名和密码有误,请检查!");
mv.setViewName("error");// 如果登录用户名和密码存在问题,则跳转到错误页面
}
return mv;
}
public List<User> selectAllUser() {
list = userService.selectAllUser();
return list;
}
/**
* 请求查询所有用户数据
* @param list
* @return
*/
@RequestMapping(value ="/main")
public ModelAndView main(int pageNum) {
ModelAndView mv = new ModelAndView();
HttpSession session = request.getSession();
if(session.getAttribute("UserSession")==null) {
mv.setViewName("error");
mv.addObject("error", "用户没有登录,请登录!");
}
else {
mv.setViewName("main");
selectAllUser();
int count = list.size();
System.out.println(count+"总数===========");
int pageSize = 5;
double aa =(double)count;
double bb = (double)pageSize;
double c = aa/bb;
System.out.println(c+"c===========");
int sumPageNum = (int) Math.ceil(c);//总页数
System.out.println(sumPageNum+"总页数===========");
session.setAttribute("lastNum", sumPageNum);
List<User> l = new ArrayList<User>();
int x = pageNum;
if(pageNum<sumPageNum) {
int a =(x-1)*pageSize;
int b = pageNum*pageSize-1;
for(int i=a;i<b;i++) {
l.add(list.get(i));
}
}
if(pageNum==sumPageNum){
for(int i=(x-1)*pageSize;i<count;i++) {
l.add(list.get(i));
}
}
mv.addObject("userList", l);
}
return mv;
}
/**
* 通过id查询一条用户数据
*
* @param id
* @param user
* @return
*/
@RequestMapping(value = "/goSelectById")
public String goSelectById(int id, User user) {
user = userService.selectUserById(id);
if (user != null)
return "main";
else
return "error";
}
/**
* 跳转到添加用户界面接口
*
* @return
*/
@RequestMapping(value = "/insertUser")
public String addUser() {
return "insert";
}
/**
* 添加一条用户数据
*
* @param user
* @return
*/
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public String doInsert(@RequestParam("name") String name, @RequestParam("password") String password,
@RequestParam("birth") String birth, @RequestParam("phone") String phone,
@RequestParam("department") String department) {
User user = new User();
user.setName(name);
user.setPassword(password);
user.setBirth(birth);
user.setPhone(phone);
user.setDepartment(department);
if (userService.insertUser(user) > 0)
return "redirect:main";
else
return "error";
}
@RequestMapping(value = "/modify")
public ModelAndView modify(int id) {
ModelAndView mv = new ModelAndView();
mv.setViewName("update");
User user = userService.selectUserById(id);
mv.addObject("user", user);
return mv;
}
/**
* 更新用户信息
*
* @param user
* @return
*/
@RequestMapping(value = "/update")
public String doUpdate(@RequestParam("id") String id, @RequestParam("name") String name,
@RequestParam("password") String password, @RequestParam("birth") String birth,
@RequestParam("phone") String phone, @RequestParam("department") String department) {
int ID = Integer.valueOf(id);
User user = new User();
user.setId(ID);
user.setName(name);
user.setBirth(birth);
user.setPassword(password);
user.setPhone(phone);
user.setDepartment(department);
if (userService.updateUser(user) > 0)
return "redirect:main";
else
return "error";
}
/**
* 删除一条用户信息
*
* @param id
* @return
*/
@RequestMapping(value = "/delete")
public String doDelete(int id) {
if (userService.deleteUser(id) > 0)
return "redirect:main";
else
return "error";
}
/**
* 请求解析和返回json数据
*/
@RequestMapping(value = "/json")
@ResponseBody
public ModelAndView doJson() {
ModelAndView mv = new ModelAndView();
List<User> list = new ArrayList<User>();
list = userService.selectAllUser();
mv.addObject("list", list);
mv.setViewName("json");
return mv;
}
@RequestMapping(value = "/search")
public String search() {
return "search";
}
@RequestMapping(value = "/login")
public String login() {
return "login";
}
@RequestMapping(value = "/getuser", method = RequestMethod.POST)
public ModelAndView getuser(@RequestParam("name") String name) {
ModelAndView mv = new ModelAndView();
List<User> userList = userService.selectUserByName(name);
mv.setViewName("search");
mv.addObject("userList", userList);
return mv;
}
@RequestMapping(value = "/info", method = RequestMethod.GET)
public ModelAndView info() {
ModelAndView mv = new ModelAndView();
User user = (User) request.getSession().getAttribute("UserSession");
if (!user.equals(null)) {
mv.addObject("user", user);
mv.setViewName("info");
} else {
mv.setViewName("error");
mv.addObject("error", "session为空请登录!");
}
return mv;
}
}