package cn.smbms.controller;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
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.multipart.MultipartFile;
import com.alibaba.fastjson.JSONArray;
import com.sun.corba.se.impl.copyobject.JavaStreamObjectCopierImpl;
import cn.smbms.entity.Role;
import cn.smbms.entity.User;
import cn.smbms.service.RoleService;
import cn.smbms.service.UserService;
import cn.smbms.utils.Constants;
import cn.smbms.utils.PageSupport;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userservice;
@Resource
private RoleService roleservice ;
//跳转到登陆页面
@RequestMapping(value="login.html")
public String login(){
return "login";
}
//执行登陆功能
@RequestMapping(value="/doLogin.html",method=RequestMethod.POST)
public String doLogin(@RequestParam String userCode,
@RequestParam String userPassword,
HttpSession session,
HttpServletRequest request){
User user = userservice.login(userCode,userPassword);
if(user != null){
if(!user.getUserPassword().equals(userPassword)){
request.setAttribute("error", "密码不正确!");
return "login";
}
session.setAttribute(Constants.USER_SESSION, user);
return "redirect:/user/main.html";
}else{
request.setAttribute("error", "用户不存在!");
return "login";
}
}
@RequestMapping(value="/main.html")
public String main(HttpSession session){
return "frame";
}
/**处理异常*/
@RequestMapping(value="exLogin.html",method=RequestMethod.GET)
public String exLogin(@RequestParam String userCode,
@RequestParam String userPassword){
User user = userservice.login(userCode, userPassword);
if(user == null){
throw new RuntimeException("输入的用户名或密码不正确!");
}
return "redirect:/user/main.html";
}
/* @ExceptionHandler(value={RuntimeException.class})
public String handException(RuntimeException e,HttpServletRequest req){
req.setAttribute("e", e);
return "error";
}*/
/**获取用户列表*/
@RequestMapping(value="/userlist.html")
public String getUserList(Model model,
@RequestParam(value="queryUserName",required=false) String queryUserName,
@RequestParam(value="queryUserRole",required=false) String queryUserRole,
@RequestParam(value="pageIndex",required=false) Integer pageIndex){
int _queryUserRole = 0;
int pageSize = Constants.pageSize;
//当前页码
int currentPageNo = 1;
if(queryUserName == null){
queryUserName = "";
}
if(queryUserRole != null && !queryUserRole.equals("")){
_queryUserRole = Integer.parseInt(queryUserRole);
}
if(pageIndex != null){
try {
currentPageNo = Integer.valueOf(pageIndex);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
return "redirect:/user/syserror.html";
}
}
List<User> userList = userservice.getUserList(queryUserName, _queryUserRole, currentPageNo, pageSize);
int totalCount = userservice.getUserCount(queryUserName, _queryUserRole);
PageSupport pages = new PageSupport();
pages.setCurrentPageNo(currentPageNo);
pages.setPageSize(pageSize);
pages.setTotalCount(totalCount);
int totalPageCount = pages.getTotalPageCount();
if(currentPageNo < 1){
currentPageNo = 1;
}
if(currentPageNo > totalPageCount){
currentPageNo = totalPageCount;
}
model.addAttribute("queryUserName", queryUserName);
model.addAttribute("userList", userList);
List<Role> roleList = roleservice.getRoleList();
model.addAttribute("roleList", roleList);
model.addAttribute("queryUserRole", _queryUserRole);
model.addAttribute("totalPageCount", totalPageCount);
model.addAttribute("totalCount", totalCount);
model.addAttribute("currentPageNo", currentPageNo);
return "userlist";
}
@RequestMapping(value="/syserror.html")
public String sysError(){
return "syserror";
}
@RequestMapping(value="/getUserCode.html")
@ResponseBody
public String getUserCodeExit(@RequestParam("userCode") String userCode){
int count = userservice.getUserCode(userCode);
return JSONArray.toJSONString(count);
}
//添加用户信息
@RequestMapping(value="/add.html",produces="text/html;charset=UTF-8",method=RequestMethod.POST)
public String addUser(@ModelAttribute User user,HttpSession ss,
HttpServletRequest req,
@RequestParam(value="a_idPicPath",required=false)MultipartFile[] attachs){
String idPicPath = null;
String workPicPath = null;
String errorInfo = null;
Boolean flag = true;
String path = req.getSession().getServletContext().
getRealPath("statics"+File.separator+"uploadfiles");
for (int i = 0; i < attachs.length; i++) {
MultipartFile attach = attachs[i];
if(!attach.isEmpty()){
if(i == 0){
errorInfo = "uploadFileError";
}else if(i == 1){
errorInfo = "uploadWpError";
}
String oldFileName = attach.getOriginalFilename();
String prefix = FilenameUtils.getExtension(oldFileName);
int fileSize = 500000;
if(attach.getSize() > fileSize){
req.setAttribute("uploadFileError", " * 上传文件大小不等超过500kB");
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";
File taFile = new File(path,fileName);
if(!taFile.exists()){
taFile.mkdirs();
}
try {
attach.transferTo(taFile);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
req.setAttribute("uploadFileError", "上传错误!");
flag = false;
}
if(i == 0){
idPicPath = path+File.pathSeparator+fileName;
}else if(i == 1){
workPicPath = path+File.pathSeparator+fileName;
}
}else{
req.setAttribute("uploadFileError", "上传错误!");
flag = false;
}
}
}
if(flag){
user.setCreatedBy(((User)ss.getAttribute(Constant