diff --git a/src/main/java/com/controller/DepartmentController.java b/src/main/java/com/controller/DepartmentController.java new file mode 100644 index 0000000..87da4cd --- /dev/null +++ b/src/main/java/com/controller/DepartmentController.java @@ -0,0 +1,130 @@ +package com.controller; + +import com.entity.Department; +import com.service.IDeptService; +import com.utils.Page; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.servlet.ModelAndView; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @ClassName DepartmentController + * @Description TODO + * @Author YHT + * @Date 2021/5/30 12:14 + */ +@Controller +public class DepartmentController { + @Autowired + private IDeptService deptService; + + /*** + * 跳转至部门列表界面 + * @param page 结果的页数 + * @return 部门列表页面 + */ + @RequestMapping("/toDepartment") + public ModelAndView toDepartment(Page page){ + ModelAndView mv = new ModelAndView(); + List departmentList = deptService.listDept(page); + int total = deptService.getDeptTotal(); + int pageIndex = total / page.getCount(); + pageIndex = total % page.getCount() == 0 ? pageIndex : pageIndex+1; + page.setTotalIndex(pageIndex); + page.calculateLast(total); + mv.addObject("deptList",departmentList); + mv.addObject("page",page); + mv.addObject("total",total); + mv.setViewName("department"); + return mv; + } + + /** + * 添加部门 + * @param department 部门信息 + * @return 部门列表页面 + */ + @RequestMapping("/addDept") + public ModelAndView addDept(Department department){ + ModelAndView mv = new ModelAndView(); + //调用service层的方法 + deptService.addDept(department); + //重定向到部门列表页面 + mv.setViewName("redirect:/toDepartment"); + return mv; + } + + /** + * 查找部门 + * @param deptName 部门名称 + * @param page 页面页数 + * @return 部门列表页面 + */ + @RequestMapping("/searchDept") + public ModelAndView searchDept(String deptName, Page page){ + ModelAndView mv = new ModelAndView(); + //根据部门名称查找部门信息,并进行分页显示 + List departmentList = deptService.findDeptByName(deptName, page); + //得到总记录数 + int total = departmentList.size(); + //每页显示三条,总共显示的页数 + int pageIndex = total / page.getCount(); + pageIndex = total % page.getCount() == 0 ? pageIndex : pageIndex+1; + page.setTotalIndex(pageIndex); + page.calculateLast(total); + //添加信息到modelview。 + mv.addObject("deptList",departmentList); + mv.addObject("page",page); + mv.addObject("total",total); + //返回部门信息列表页面 + mv.setViewName("department"); + return mv; + } + + /** + * 跳转至修改部门信息页面 + * @param id 部门的id号 + * @return + */ + @RequestMapping("/toModifyDept") + public ModelAndView toModifyDept(int id){ + ModelAndView mv = new ModelAndView(); + //找到部门信息 + Department department = deptService.findDeptById(id); + //将该部门信息返回到页面 + mv.addObject("dept",department); + //跳转至修改信息界面 + mv.setViewName("updateDepartment"); + return mv; + } + + /** + * 修改部门信息 + * @param department 部门 + * @return 部门信息列表界面 + */ + @RequestMapping("/modifyDept") + public ModelAndView modifyDept(Department department){ + ModelAndView mv = new ModelAndView(); + //修改部门信息 + deptService.modifyDept(department); + //重定向到部门信息列表界面 + mv.setViewName("redirect:/toDepartment"); + return mv; + } + @RequestMapping("/deleteDept") + public ModelAndView deleteDept(int id){ + ModelAndView mv = new ModelAndView(); + //修改部门信息 + deptService.deleteDept(id); + //重定向到部门信息列表界面 + mv.setViewName("redirect:/toDepartment"); + return mv; + } +} + diff --git a/src/main/java/com/controller/EmployeeController.java b/src/main/java/com/controller/EmployeeController.java new file mode 100644 index 0000000..71abf37 --- /dev/null +++ b/src/main/java/com/controller/EmployeeController.java @@ -0,0 +1,181 @@ +package com.controller; + +import com.entity.Department; +import com.entity.Employee; +import com.service.IDeptService; +import com.service.IEmpService; +import com.utils.Page; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.servlet.ModelAndView; + +import java.text.SimpleDateFormat; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @ClassName EmployeeController + * @Description TODO + * @Author YHT + * @Date 2021/5/30 12:15 + */ +@Controller +public class EmployeeController { + @Autowired + private IEmpService empService; + @Autowired + private IDeptService deptService; + + /** + * 跳转至员工列表页面 + * @param page + * @return + */ + @RequestMapping("/toEmployee") + public ModelAndView toEmployee(Page page){ + ModelAndView mv = new ModelAndView(); + //查询所有员工 + List employeeList = empService.listEmp(page); + //给每个员工设置部门名称 + for (Employee emp : employeeList) { + emp.setDeptName(deptService.findDeptById(emp.getDeptId()).getDeptName()); + } + //获取数据库中记录总数 + int total = empService.getEmpTotal(); + //页数。即:每页显示三条记录,总共展示几页 + int pageIndex = total / page.getCount(); + pageIndex = total % page.getCount() == 0 ? pageIndex : pageIndex+1; + page.setTotalIndex(pageIndex); + page.calculateLast(total); + //将数据返回员工列表页面 + mv.addObject("empList",employeeList); + mv.addObject("page",page); + mv.addObject("total",total); + mv.setViewName("employee"); + return mv; + } + + /** + * 根据名称查找员工 + * @param page + * @param name + * @param email + * @param empId + * @return + */ + @RequestMapping("/searchEmp") + public ModelAndView toEmployee(Page page, String name, String email, String empId){ + ModelAndView mv = new ModelAndView(); + //根据部门名称查找部门信息,并进行分页显示 + Map map = new HashMap<>(); + if(name != "" && name != null){ + map.put("name", name); + } + if(email != "" && email != null){ + map.put("email", email); + } + if(empId != "" && empId != null){ + map.put("empId", empId); + } + map.put("page", page); + //得到符合条件的记录 + List employeeList = empService.searchEmp(map); + System.out.println(employeeList); + //设置员工的部门名称 + for (Employee emp : employeeList) { + emp.setDeptName(deptService.findDeptById(emp.getDeptId()).getDeptName()); + } + //得到总记录数 + int total = employeeList.size(); + //每页显示三条,总共显示的页数 + int pageIndex = total / page.getCount(); + pageIndex = total % page.getCount() == 0 ? pageIndex : pageIndex+1; + page.setTotalIndex(pageIndex); + page.calculateLast(total); + //将数据返回给页面 + mv.addObject("empList",employeeList); + mv.addObject("page",page); + mv.addObject("total",total); + //返回部门信息列表页面 + mv.setViewName("employee"); + return mv; + } + + /** + * 跳转至添加员工页面 + * @return + */ + @RequestMapping("/toAddEmp") + public ModelAndView toAddEmp(){ + ModelAndView mv = new ModelAndView(); + //添加信息到modelview。 + List departmentList = deptService.findAllDepartment(); + mv.addObject("deptList",departmentList); + mv.setViewName("addEmployee"); + return mv; + } + + /** + * 添加员工 + * @param employee + * @return + */ + @RequestMapping("/addEmp") + public ModelAndView addEmp(Employee employee){ + ModelAndView mv = new ModelAndView(); + //添加员工到数据库 + empService.addEmp(employee); + mv.setViewName("redirect:/toEmployee"); + return mv; + } + + /** + * 跳转至修改员工界面 + * @param id + * @return + */ + @RequestMapping("/toModifyEmp") + public ModelAndView toUpdateEmp(int id){ + ModelAndView mv = new ModelAndView(); + //得到员工信息 + mv.addObject("emp", empService.selectEmpById(id)); + //得到所有部门信息 + List departmentList = deptService.findAllDepartment(); + mv.addObject("deptList",departmentList); + mv.setViewName("updateEmployee"); + return mv; + } + + /** + * 修改员工信息 + * @param employee + * @return + */ + @RequestMapping("/modifyEmp") + public ModelAndView modifyEmp(Employee employee){ + ModelAndView mv = new ModelAndView(); + //修改数据库中信息 + empService.modifyEmp(employee); + List departmentList = deptService.findAllDepartment(); + mv.addObject("deptList",departmentList); + //重定向到员工列表界面 + mv.setViewName("redirect:/toEmployee"); + return mv; + } + + /** + * 删除员工 + * @param id + * @return + */ + @RequestMapping("/deleteEmp") + public ModelAndView deleteEmp(int id){ + ModelAndView mv = new ModelAndView(); + //删除员工 + empService.deleteEmp(id); + mv.setViewName("redirect:/toEmployee"); + return mv; + } +} \ No newline at end of file diff --git a/src/main/java/com/controller/loginController.java b/src/main/java/com/controller/loginController.java index d2a6002..f92590e 100644 --- a/src/main/java/com/controller/loginController.java +++ b/src/main/java/com/controller/loginController.java @@ -1,5 +1,178 @@ package com.controller; +import com.entity.User; +import com.entity.Visitor; +import com.service.IUserService; +import com.service.IVisitorService; +import com.utils.MD5Utils; +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.ResponseBody; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * @ClassName loginController + * @Description TODO + * @Author YHT + * @Date 2021/5/28 8:02 + */ +@Controller public class loginController { + @Autowired + private IUserService userService; + @Autowired + private IVisitorService visitorService; + + /** + * 跳转至主页 + * @param username 用户名 + * @param password 密码 + * @param request + * @return + */ + @RequestMapping("/main") + @ResponseBody + public ModelAndView toMain(String username, String password, HttpServletRequest request){ + ModelAndView mv = new ModelAndView(); + mv.setViewName("main"); + //使用session存储数据 + HttpSession session = request.getSession(); + mv.addObject("status", "ok"); + //根据用户名和密码查找用户 + User user = userService.findByNameAndPwd(username, MD5Utils.stringToMD5(password)); + //访客信息 + Visitor visitor = new Visitor(); + //用户不为空,则表示登录成功 + if(user != null){ + if(user.getName().equals(username) && user.getPassword().equals(MD5Utils.stringToMD5(password))){ + //跳转至首页 + mv.setViewName("main"); + // mv.addObject("userName", user.getName()); + // mv.addObject("status", "ok"); + //存储数据 + mv.addObject("userId", user.getId()); + session.setAttribute("userName", user.getName()); + session.setAttribute("userId", user.getId()); + //添加访客信息 + visitor.setName(user.getName()); + visitor.setEmail(user.getEmail()); + visitor.setIdentity(user.getIdentity()); + visitor.setTelPhone(user.getTelNumber()); + Date date = new Date(System.currentTimeMillis()); + visitor.setVisitTime(date); + //存储方可信息 + visitorService.addVisitor(visitor); + }else { + //查询失败,返回登录页 + mv.setViewName("redirect:/index.jsp"); + mv.addObject("status", "userNameOrPwdError"); + } + }else{ + //返回首页 + mv.setViewName("redirect:/index.jsp"); + mv.addObject("status", "userNameOrPwdError"); + } + return mv; + } + + /** + * 跳转至注册页面 + * @param user 用户 + * @return 返回登录页面 + */ + @RequestMapping("/register") + public String toRegister(User user){ + //对用户密码进行加密 + user.setPassword(MD5Utils.stringToMD5(user.getPassword())); + //将用户信息插入数据库 + userService.insertUser(user); + //重定向到首页 + return "redirect:/index.jsp"; + } + + /** + * 跳转至用户信息页 + * @param id + * @return + */ + @RequestMapping("/toUserMsg") + public ModelAndView toUserMsg(Integer id){ + ModelAndView mv = new ModelAndView(); + //根据id找到用户 + User user = userService.findById(id); + mv.setViewName("userMsg"); + System.out.println(id); + System.out.println(user); + //将用户信息返回用户信息页 + mv.addObject("loginUser", user); + return mv; + } + + /** + * 修改用户信息 + * @param user + * @return + */ + @RequestMapping("/modifyUserMsg") + public ModelAndView modifyUserMsg(User user){ + ModelAndView mv = new ModelAndView(); + //修改用户信息 + userService.modifyUser(user); + mv.addObject("loginUser", user); + //修改完成后,跳转至用户信息页 + mv.setViewName("userMsg"); + return mv; + } + + /** + * 修改用户密码 + * @param id + * @return + */ + @RequestMapping("/toModifyPwd") + public ModelAndView toModifyPwd(int id){ + ModelAndView mv = new ModelAndView(); + //找到用户 + User user = userService.findById(id); + mv.setViewName("modifyPwd"); + mv.addObject("loginUser", user); + return mv; + } + + /** + * 修改密码 + * @param id + * @param password + * @param rePassword + * @return + */ + @RequestMapping("/modifyPwd") + public ModelAndView modifyPwd(Integer id, String password, String rePassword){ + ModelAndView mv = new ModelAndView(); + //判断两次输入的密码是否一致 + if(password.equals(rePassword)){ + //一致。则更改数据库,重定向到登录页 + userService.modifyPwd(password, id); + mv.setViewName("redirect:/index.jsp"); + }else{ + //跳转到修改密码页面 + mv.setViewName("modifyPwd"); + } + return mv; + } + /** + * 跳转至关于页面 + * @return + */ + @RequestMapping("/about") + public String about(){ + return "about"; + } } \ No newline at end of file