package com.li.springboot.controller;
import com.li.springboot.bean.Dept;
import com.li.springboot.bean.Employee;
import com.li.springboot.bean.Job;
import com.li.springboot.bean.User;
import com.li.springboot.mapper.DeptMapper;
import com.li.springboot.mapper.EmployeeMapper;
import com.li.springboot.mapper.JobMapper;
import com.li.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @author lt
* @version 1.0
* @date 2019/12/16 11:08 下午
*/
@Controller
public class EmployeeController {
@Autowired
DeptMapper deptMapper;
@Autowired
EmployeeMapper employeeMapper;
@Autowired
UserMapper userMapper;
@Autowired
JobMapper jobMapper;
//显示雇员信息
@GetMapping("/employees")
public String getAllEmployee(Model model){
Collection<Employee> employees = employeeMapper.getAllEmployee();
employees.forEach(item->{
Job jobById = jobMapper.getJobById(item.getJobId());
item.setJobId(jobById.getName());
Dept dep = deptMapper.getDeptById(item.getDeptId());
item.setDeptId(dep.getName());
});
model.addAttribute("employees",employees);
return "employee/list";
}
//前往添加页面
@GetMapping("/emp")
public String toAddEmp(Model model){
Collection<Dept> depts = deptMapper.getAllDept();
Collection<Job> jobs = jobMapper.getAllJob();
System.out.println(jobs);
model.addAttribute("jobs",jobs);
model.addAttribute("depts",depts);
return "employee/add";
}
//添加员工
@PostMapping("/emp/add")
public String addEmp(Employee employee){
System.out.println(employee);
employeeMapper.insertEmp(employee);
return "redirect:/employees";
}
//前往编辑页面
@GetMapping("/emp/update/{id}")
public String getUpdatePage(Model model,@PathVariable("id") String id){
Collection<Dept> depts = deptMapper.getAllDept();
Collection<Job> jobs = jobMapper.getAllJob();
Employee empById = employeeMapper.getEmpById(id);
model.addAttribute("jobs",jobs);
model.addAttribute("depts",depts);
model.addAttribute("emp",empById);
return "employee/update";
}
//前往编辑页面
@GetMapping("/emp/update1")
public String getUpdatePage1(Model model, HttpSession session){
User user = (User)session.getAttribute("userInfor");
Employee emp = employeeMapper.getEmpById(user.getEmployeeid().toString());
Collection<Dept> depts = deptMapper.getAllDept();
Collection<Job> jobs = jobMapper.getAllJob();
Employee empById = employeeMapper.getEmpById(String.valueOf(emp.getId()));
model.addAttribute("jobs",jobs);
model.addAttribute("depts",depts);
model.addAttribute("emp",empById);
return "employee/update1";
}
//保存修改后的信息
@PostMapping("/employee/update1")
public String deleteUser1(Employee employee){
employeeMapper.updateEmployById(employee);
return "redirect:/emp/update1";
}
//删除员工
@PostMapping("/emp/del/{id}")
public String deleteUser(@PathVariable("id") String id){
employeeMapper.deleteEmp(id);
return "redirect:/employees";
}
//查询员工
@PostMapping("/emp/select")
public String selectEmpByName(@RequestParam(value = "name") String name ,Integer num, Model model){
Collection<Employee> employees = employeeMapper.getEmpByName(name,num==null?0:(num-1)*15);
model.addAttribute("employees",employees);
return "employee/list";
}
//获取员工信息下拉框
@PostMapping("/getemployee")
@ResponseBody
public Object getEmployee(Model model){
List<Employee> employees = employeeMapper.getAllEmployee();
List<User> allUser = userMapper.getAllUser();
allUser.forEach(item->{
if (item.getEmployeeid() != null && !"".equals(item.getEmployeeid())){
for (int i = 0;i< employees.size();i++){
if (employees.get(i).equals(item.getEmployeeid())){
employees.remove(i);
}
}
}
});
return employees;
}
//保存修改后的信息
@PostMapping("/employee/update")
public String deleteUser(Employee employee){
employeeMapper.updateEmployById(employee);
return "redirect:/employees";
}
}