package com.keep.ssm.controller;
import com.keep.ssm.domain.*;
import com.keep.ssm.service.*;
import java.io.File;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
@Controller
public class EployeeController {
@Autowired
private EmployeeService empService;
@Autowired
private DepartmentService deptService;
public EmployeeService getEmpService() {
return empService;
}
public void setEmpService(EmployeeService empService) {
this.empService = empService;
}
public DepartmentService getDeptService() {
return deptService;
}
public void setDeptService(DepartmentService deptService) {
this.deptService = deptService;
}
@RequestMapping(value = "/emps", method = RequestMethod.GET)
public String getEmps(Map<String, Object> map) {
map.put("emps", empService.findAllEmps());
System.out.println(empService.findAllEmps().size());
return "/emp/emps";
}
@RequestMapping(value = "/emp/add", method = RequestMethod.GET)
public String addEmp(Map<String, Object> map) {
map.put("depts", deptService.findAllDepts());
HashMap<Boolean, String> sexMap = new HashMap<Boolean, String>();
sexMap.put(true, "男");
sexMap.put(false, "女");
map.put("sexMap", sexMap);
Employee emp = new Employee();
emp.setDept(new Department());
map.put("employee", emp);
return "/emp/add";
}
@RequestMapping(value = "/emp/add", method = RequestMethod.POST)
public String addEmp(@Valid @ModelAttribute("employee") Employee employee, BindingResult bindingResult, Map<String, Object> map) {
if (bindingResult.getFieldErrorCount() > 0) {
System.out.println("校验失败!");
HashMap<Boolean, String> sexMap = new HashMap<Boolean, String>();
sexMap.put(true, "男");
sexMap.put(false, "女");
map.put("sexMap", sexMap);
map.put("depts", deptService.findAllDepts());
return "/emp/add";
}
empService.addEmp(employee);
return "redirect:/emps";
}
@RequestMapping(value = "/emp/{id}")
public String updateEmp(@PathVariable("id") String id, Model model) {
System.out.println("修改" + id);
Employee emp = empService.findEmpById(id);
model.addAttribute("employee", emp);
model.addAttribute("depts", deptService.findAllDepts());
HashMap<Boolean, String> sexMap = new HashMap<Boolean, String>();
sexMap.put(true, "男");
sexMap.put(false, "女");
model.addAttribute("sexMap", sexMap);
return "emp/update";
}
@RequestMapping(value = "/emp/update", method = RequestMethod.POST)
public String updateEmp(@Valid @ModelAttribute("employee") Employee employee, BindingResult bindingResult, Map<String, Object> map) {
if (bindingResult.getFieldErrorCount() > 0) {
System.out.println("校验失败!");
HashMap<Boolean, String> sexMap = new HashMap<Boolean, String>();
sexMap.put(true, "男");
sexMap.put(false, "女");
map.put("sexMap", sexMap);
map.put("depts", deptService.findAllDepts());
map.put("employee", employee);
return "emp/update";
}
System.out.println(employee);
empService.updateEmp(employee);
return "redirect:/emps";
}
@RequestMapping(value = "/emp/{id}", method = RequestMethod.DELETE)
public String deleteEmp(@PathVariable("id") String id, Map<String, Object> map) {
System.out.println("删除" + id);
empService.deleteEmpById(id);
return "redirect:/emps";
}
@RequestMapping(value = "/emp/load", method = RequestMethod.GET)
public String showDownloadFiles(HttpServletRequest request, Model model) {
String downloadPath = request.getServletContext().getRealPath("/temp");
File downloadFile = new File(downloadPath);
List<String> fileNames = new ArrayList<String>();
File[] files = downloadFile.listFiles();
for (File f : files != null ? files : new File[0]) {
fileNames.add(f.getName());
}
model.addAttribute("fileNames", fileNames);
return "emp/emps";
}
}