package com.aaa.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.aaa.service.IUserService;
import com.aaa.vo.User;
import com.util.Constants;
@Controller
@RequestMapping("/user")
public class UserController {
//成员:有一个业务层对象
@Resource
private IUserService userService;
//跳到登录页面
@RequestMapping("/tologin")
public String toLogin(){
return "view/login";
}
//执行登录操作
@RequestMapping("/login")
public String login(User user,HttpServletRequest request,Model model){
HttpSession session=request.getSession();
User resultUser=userService.login(user);
if(resultUser==null){
request.setAttribute("errorMsg", "用户名或密码错误!");
return "view/login";//返回登录界面,并提示用户信息
}
else{
session.setAttribute(Constants.USER_SESSION,resultUser);
//session.setAttribute("currentUser", resultUser);//登录成功,将用户信息存到session中
return "redirect:/product/index";//返回主页
}
}
//注销登录
@RequestMapping("/exit")
public String exit(User user,HttpSession session){
session.invalidate();
return "redirect:/product/index";
}
//跳到注册页面
@RequestMapping("/toreg")
public String toreg(){
return "view/register";
}
//注册,增加用户操作
@RequestMapping("/reg")
public String reg(User user){
System.out.println("++++++++");
userService.reg(user);
return "redirect:tologin";
}
//验证用户名不能重复
@ResponseBody
@RequestMapping("/checkname")
public int checkname(String username){
List<User> checkname = userService.checkname(username);
System.out.println("xxx"+checkname);
if(checkname.size()>0){
return 1;
}else{
return 0;
}
}
//验证手机号不能重复
@ResponseBody
@RequestMapping("/checktel")
public int checktel(String telephone){
List<User> checktel = userService.checktel(telephone);
System.out.println("sss"+checktel);
if(checktel.size()>0){
return 1;
}else{
return 0;
}
}
//跳到个人中心,执行查询用户信息操作
@RequestMapping("/toinfo/{uid}")
public String toinfo(@PathVariable("uid")Integer uid,Model model){
List<User> userList=userService.selectByIds(uid);
model.addAttribute("userList",userList.get(0));
return "view/info";
}
//跳到商家个人中心,执行查询用户信息操作
@RequestMapping("/tobackinfo/{uid}")
public String tobackinfo(@PathVariable("uid")Integer uid,Model model){
List<User> userList=userService.selectByIds(uid);
System.out.println(uid);
model.addAttribute("userList",userList.get(0));
return "back/backinfo";
}
//前台用户更新,完善信息
@RequestMapping("/backupdate")
public String backupdate(User user){
userService.update(user);
return "back/updatesuccess";
}
//个人中心信息修改,完善个人信息
@RequestMapping("/update")
public String update(User user){
userService.update(user);
return "redirect:/product/index";
}
/*//跳到后台
@RequestMapping("/back")
public String toback(){
return "back/main";
}
*/
//跳到后台登录页面
@RequestMapping("/tobacklogin")
public String tobacklogin(){
return "back/login";
}
//后台登录
@RequestMapping("/backlogin")
public String backlogin(User user,HttpServletRequest request,Model model){
HttpSession session=request.getSession();
User resultUsers=userService.backlogin(user);
if(resultUsers==null){
request.setAttribute("errorMsg", "手机号或密码错误!");
return "back/login";//返回登录界面,并提示用户信息
}
else{
session.setAttribute(Constants.USER_SESSION,resultUsers);
//session.setAttribute("backcurrentUser", resultUsers);//登录成功,将用户信息存到session中
return "/back/main";//返回主页
}
}
//注销登录
@RequestMapping("/backexit")
public String backexit(User user,HttpSession session){
session.invalidate();
return "/back/login";
}
//跳到店铺申请界面
@RequestMapping("/tobackreg")
public String tobackreg(){
return "/back/reg";
}
//店铺申请
@RequestMapping("/backreg")
public String backreg(User user){
System.out.println("++++++++");
userService.backreg(user);
return "redirect:tobacklogin";
}
//我要开店
@RequestMapping("/tokaidian")
public String tokaidian(){
return "/back/kaidian";
}
//基础测试,添加用户
@RequestMapping("/toAdd")
public String toAdd(){
return "user/add";
}
@RequestMapping("/add")
public String add(User user){
userService.add(user);
return "redirect:searchAll";
}
//对user表查找;model:存放数据
@RequestMapping("/searchAll")
public String searchAll(Model model){
List<User>userList=userService.searchAll();
model.addAttribute("size",userList.size());
model.addAttribute("userList",userList);
return "user/userList"; //viewName
}
//对user表查找;model:存放数据
@RequestMapping("/search")
public String search(Model model){
List<User>userList=userService.search();
model.addAttribute("size",userList.size());
model.addAttribute("userList",userList);
return "user/userList"; //viewName
}
//模糊查询
@RequestMapping("/seAll")
public String select(User user,Model model){
System.out.println(user.getUsername());
List<User> userList = userService.seAll(user);
model.addAttribute("size",userList.size());
model.addAttribute("userList", userList);
return "user/userList";
}
@RequestMapping("/del/{username}")
public String del(@PathVariable("username")String username){
System.out.println(username);
userService.del(username);
return "redirect:/user/search";
};
@RequestMapping("/toupdate/{uid}")
public String selectById(@PathVariable("uid")int uid,Model model){
System.out.println("qqqqq");
List<User> userList= userService.selectByIds(uid);
model.addAttribute("user", userList.get(0));
System.out.println("1111111111");
return "user/update";
}
@RequestMapping("/userupdate")
public String userupdate(User user){
userService.update(user);
return "redirect:searchAll";
}
}