package cn.smbms.controller;
import cn.smbms.entity.Role;
import cn.smbms.entity.User;
import cn.smbms.service.role.RoleService;
import cn.smbms.service.user.UserService;
import cn.smbms.util.Constants;
import cn.smbms.util.PageSupport;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sun.org.apache.bcel.internal.generic.I2F;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
@RequestMapping("toLogin")
public String toLogin(){
return "login";
}
@RequestMapping("login")
public String login(@RequestParam String userCode,
@RequestParam String userPassword,
HttpServletRequest request,
HttpSession session){
//调用业务层 执行请求处理
/*int a = 1/0;*/
User user = userService.selectUserByCodeSmbms(userCode);
if (user!=null && user.getUserPassword().equals(userPassword)){
session.setAttribute(Constants.USER_SESSION,user);
//用户存在 并且用户名和密码都正确,跳到首页
return "frame";
}else {
request.setAttribute("error","用户名或密码错误");
//用户名或密码错误
//return "redirect:/user/toLogin"; //重定向:路径会发生变化
return "forward:/user/toLogin"; //转发:路径不会变化
}
}
/**
* 注销/退出操作
* @param session
* @return
*/
@RequestMapping("logout")
public String logout(HttpSession session){
session.removeAttribute(Constants.USER_SESSION);//session失效
return "forward:/user/toLogin";
}
/**
*根据条件获取用户信息列表
* @param queryname
* @param queryUserRole
* @param pageIndex
* @return
*/
@RequestMapping("list")
public String getUserList(String queryname, Integer queryUserRole, @RequestParam(defaultValue = "1")Integer pageIndex,
Model model){
//调用业务核心方法,获取结果数据
List<User> userList = userService.seletUserList(queryname,queryUserRole,pageIndex,Constants.pageSize);
//查询总条数
Integer totalCount = userService.getUserCount(queryname,queryUserRole);
//将总条数 传入工具类 计算出总页数
PageSupport support = new PageSupport();
support.setPageSize(Constants.pageSize);
support.setCurrentPageNo(pageIndex);
support.setTotalCount(totalCount);
List<Role> roleList = roleService.selectRoleList();
model.addAttribute("roleList",roleList);
model.addAttribute("userList",userList);
model.addAttribute("currentPageIndex",pageIndex);
model.addAttribute("totalCount",totalCount);//数据总条数
model.addAttribute("totalPageCount",support.getTotalPageCount());// 显示总页数
//条件的回显
model.addAttribute("queryUserName",queryname);
model.addAttribute("queryUserRole",queryUserRole);
return "userlist";
}
/**
* 打开useradd页面的同时 创建user对象
* 参数user和请求from表单中name属性一致就可以实现数据绑定
* @param user
* @return
*/
@RequestMapping("useradd")
public String addUser(@ModelAttribute("user") User user){
return "useradd";
}
/**
* 点击用户新增页面的保存按钮,触发的保存操作
* 参数user会从Model 进行检索到对应的类型数据
* @param user
* @param session
* @return
*/
/* @RequestMapping("useraddSave")
public String addUserSave(User user, MultipartFile multipartFile, HttpSession session){
//给user对象添加 createdBy
user.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
user.setCreationDate(new Date());
if (userService.addNewUser(user)){
return "forward:/user/list?queryname="+user.getUserName();
}else {
return "useradd"; //添加失败 还在添加页面
}
}*/
/* //文件上传
@RequestMapping(value="/useraddsave",method=RequestMethod.POST)
public String addUserSave(User user,HttpSession session,HttpServletRequest request,
@RequestParam(value ="a_idPicPath", required = false) MultipartFile attach){
String idPicPath = null;
//判断文件是否为空
if(!attach.isEmpty()){
String path = request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
*//* logger.info("uploadFile path ============== > "+path);*//*
String oldFileName = attach.getOriginalFilename();//原文件名
*//*logger.info("uploadFile oldFileName ============== > "+oldFileName);*//*
String prefix= FilenameUtils.getExtension(oldFileName);//原文件后缀
*//*logger.debug("uploadFile prefix============> " + prefix);*//*
int filesize = 500000;
*//*logger.debug("uploadFile size============> " + attach.getSize());*//*
if(attach.getSize() > filesize){//上传大小不得超过 500k
request.setAttribute("uploadFileError", " * 上传大小不得超过 500k");
return "useradd";
}else if(prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png")
|| prefix.equalsIgnoreCase("jpeg") || prefix.equalsIgnoreCase("pneg")){//上传图片格式不正确
String fileName = System.currentTimeMillis()+ RandomUtils.nextInt(1000000)+"_Personal.jpg";
*//* logger.debug("new fileName======== " + attach.getName());*//*
File targetFile = new File(path, fileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
//保存
try {
attach.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("uploadFileError", " * 上传失败!");
return "useradd";
}
idPicPath = "statics"+File.separator+"uploadfiles"+File.separator+fileName;
}else{
request.setAttribute("uploadFileError", " * 上传图片格式不正确");
return "useradd";
}
}
user.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
user.setCreationDate(new Date());
user.setIdPicPath(idPicPath);
if(userService.addNewUser(user)){
return "redirect:/user/list";
}
return "useradd";
}
*/
@RequestMapping(value="/useraddsave",method=RequestMethod.POST)
public String addUserSave(User user,HttpSession session,HttpServletRequest request,
@RequestParam(value ="a_idPicPath", required = false)
评论5