You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.2 KiB

package com.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.entity.Admin;
import com.service.AdminService;
import com.util.PageHelper;
import com.util.VeDate;
//定义为控制器
@Controller
// 设置路径
@RequestMapping(value = "/admin" , produces = "text/plain;charset=utf-8")
public class AdminController extends BaseController {
// @Autowired的作用是自动注入依赖的ServiceBean
@Autowired
private AdminService adminService;
// 管理员准备
@RequestMapping("index.action")
public String index() {
return "admin/index";
}
// 管理员登录 1 验证用户名是否存在 2 验证密码是否正确
@RequestMapping("login.action")
public String login() {
String username = this.getRequest().getParameter("username");
String password = this.getRequest().getParameter("password");
Admin adminEntity = new Admin();
adminEntity.setUsername(username);
List<Admin> adminlist = this.adminService.getAdminByCond(adminEntity);
if (adminlist.size() == 0) {
this.getRequest().setAttribute("message", "用户名不存在");
return "redirect:/admin/index.action";
} else {
Admin admin = adminlist.get(0);
if (password.equals(admin.getPassword())) {
this.getSession().setAttribute("adminid", admin.getAdminid());
this.getSession().setAttribute("adminname", admin.getUsername());
this.getSession().setAttribute("realname", admin.getRealname());
this.getSession().setAttribute("role", "管理员");
} else {
this.getRequest().setAttribute("message", "密码错误");
return "redirect:/admin/index.action";
}
}
return "admin/main";
}
// 管理员准备修改密码
@RequestMapping("prePwd.action")
public String prePwd() {
return "admin/editpwd";
}
// 修改密码
@RequestMapping("editpwd.action")
public String editpwd() {
String adminid = (String) this.getSession().getAttribute("adminid");
String password = this.getRequest().getParameter("password");
String repassword = this.getRequest().getParameter("repassword");
Admin admin = this.adminService.getAdminById(adminid);
if (password.equals(admin.getPassword())) {
admin.setPassword(repassword);
this.adminService.updateAdmin(admin);
} else {
this.getRequest().setAttribute("message", "旧密码错误");
}
return "redirect:/admin/prePwd.action";
}
// 管理员退出登录
@RequestMapping("exit.action")
public String exit() {
this.getSession().removeAttribute("adminid");
this.getSession().removeAttribute("adminname");
this.getSession().removeAttribute("realname");
return "redirect:/admin/index.action";
}
// 准备添加数据
@RequestMapping("createAdmin.action")
public String createAdmin() {
return "admin/addadmin";
}
// 添加数据
@RequestMapping("addAdmin.action")
public String addAdmin(Admin admin) {
admin.setAddtime(VeDate.getStringDateShort());
this.adminService.insertAdmin(admin);
return "redirect:/admin/createAdmin.action";
}
// AJAX添加数据
@RequestMapping("xaddAdmin.action")
@ResponseBody //将java对象转为json格式的数据
public String xaddAdmin(Admin admin) {
admin.setAddtime(VeDate.getStringDateShort());
int x = this.adminService.insertAdmin(admin);
JSONObject json = new JSONObject();
json.put("result", x);
//System.out.println(json.toString());
return json.toString();
}
// 通过主键删除数据
@RequestMapping("deleteAdmin.action")
public String deleteAdmin(String id) {
this.adminService.deleteAdmin(id);
return "redirect:/admin/getAllAdmin.action";
}
// AJAX通过主键删除数据
@RequestMapping("xdeleteAdmin.action")
@ResponseBody //将java对象转为json格式的数据
public String xdeleteAdmin(String id) {
int x = this.adminService.deleteAdmin(id);
JSONObject json = new JSONObject();
json.put("result", x);
//System.out.println(json.toString());
return json.toString();
}
// 批量删除数据
@RequestMapping("deleteAdminByIds.action")
public String deleteAdminByIds() {
String[] ids = this.getRequest().getParameterValues("adminid");
if (ids != null) {
for (String adminid : ids) {
this.adminService.deleteAdmin(adminid);
}
}
return "redirect:/admin/getAllAdmin.action";
}
// 更新数据
@RequestMapping("updateAdmin.action")
public String updateAdmin(Admin admin) {
this.adminService.updateAdmin(admin);
return "redirect:/admin/getAllAdmin.action";
}
// AJAX更新数据
@RequestMapping("xupdateAdmin.action")
@ResponseBody //将java对象转为json格式的数据
public String xupdateAdmin(Admin admin) {
int x = this.adminService.updateAdmin(admin);
JSONObject json = new JSONObject();
json.put("result", x);
//System.out.println(json.toString());
return json.toString();
}
// 显示全部数据
@RequestMapping("getAllAdmin.action")
public String getAllAdmin(String number) {
List<Admin> adminList = this.adminService.getAllAdmin();
PageHelper.getUserPage(adminList, "admin", "getAllAdmin", 10, number, this.getRequest());
return "admin/listadmin";
}
// AJAX显示全部数据
@RequestMapping(value = "getDataList.action", produces = "application/json; charset=utf-8")
@ResponseBody //将java对象转为json格式的数据
public Map<String, Object> getDataList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
// 定义一个Map对象 用来返回数据
Map<String, Object> map = new HashMap<String, Object>();
Page<Admin> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
List<Admin> list = this.adminService.getAllAdmin();
// 返回的map中定义Layui的数据格式
map.put("count", pager.getTotal());
map.put("data", list);
map.put("code", 0);
map.put("msg", "");
return map;
}
// 按条件查询数据 (模糊查询)
@RequestMapping("queryAdminByCond.action")
public String queryAdminByCond(String cond, String name, String number) {
Admin admin = new Admin();
if(cond != null){
if ("username".equals(cond)) {
admin.setUsername(name);
}
if ("password".equals(cond)) {
admin.setPassword(name);
}
if ("realname".equals(cond)) {
admin.setRealname(name);
}
if ("contact".equals(cond)) {
admin.setContact(name);
}
if ("addtime".equals(cond)) {
admin.setAddtime(name);
}
}
List<String> nameList = new ArrayList<String>();
List<String> valueList = new ArrayList<String>();
nameList.add(cond);
valueList.add(name);
PageHelper.getPage(this.adminService.getAdminByLike(admin), "admin", nameList, valueList, 10, number, this.getRequest(), "query");
name = null;
cond = null;
return "admin/queryadmin";
}
// 按主键查询数据
@RequestMapping("getAdminById.action")
public String getAdminById(String id) {
Admin admin = this.adminService.getAdminById(id);
this.getRequest().setAttribute("admin", admin);
return "admin/editadmin";
}
}