package com.ly.control;
import com.ly.entity.Dept;
import com.ly.entity.Emp;
import com.ly.service.IDeptService;
import com.ly.service.IEmpService;
import com.ly.service.impl.DeptServiceImpl;
import com.ly.service.impl.EmpServiceImpl;
import com.ly.util.DBUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class EmpServlet extends HttpServlet {
private IEmpService service = new EmpServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// /emp?op=all /emp?op=one&id=5 /emp?op=save /emp?op=update /emp?op=del&id=5
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
resp.setCharacterEncoding("utf-8");
String op = req.getParameter("op");
switch (op){
case "all":
//查询全部
List<Emp> list = service.findAllEmp();
req.getSession().setAttribute("empList",list);
resp.sendRedirect("empList.jsp");
break;
case "one":
// 编辑时 查询一个
findById(req,resp);
break;
case "save":
//添加
// saveEmp(req,resp);
// updateEmp(req,resp,op);
// break;
case "update":
//修改
updateEmp(req,resp,op);
break;
case "del":
//删除
delEmp(req,resp);
break;
case "queryDept":
//查询所有的部门信息
queryDept(req,resp);
break;
}
}
/**
* 修改 员工信息
* @param req
* @param resp
* @throws IOException
*/
private void updateEmp(HttpServletRequest req,HttpServletResponse resp,String op)throws IOException{
String id = req.getParameter("id");
String name = req.getParameter("empName");
String salary = req.getParameter("salary");
String date = req.getParameter("bornDate");
String depId = req.getParameter("depId");
String sex = req.getParameter("sex");
int empId = Integer.valueOf(id);
double s = Double.valueOf(salary);
int ss = Integer.valueOf(sex);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try{
d = sdf.parse(date);
}catch (Exception e){
System.out.println("日期转换失败,"+e.getMessage());
}
int did = Integer.valueOf(depId);
Emp emp = new Emp(empId,name,s,ss,d,did);
int i=0;
String info ="";
if("save".equals(op)){
info="添加";
i = service.saveEmp(emp);
}else{
info="修改";
i = service.updateEmp(emp);
}
if(i>0){
PrintWriter out =resp.getWriter();
out.write("<script>");
out.write("alert('"+info+"成功');");
out.write("window.location.href='/emp?op=all'");
out.write("</script>");
}
}
/**
* 按照主键查询 员工信息
* @param req
* @param resp
* @throws IOException
*/
private void findById(HttpServletRequest req,HttpServletResponse resp) throws IOException{
// 1 获得 请求参数id
String id = req.getParameter("id");
//2. 查询所有的部门信息
IDeptService dept = new DeptServiceImpl();
List<Dept> deptList = dept.queryAllDept();
req.getSession().setAttribute("deptList",deptList);
//3. 查询员工的信息
Emp emp = service.findEmpById(Integer.valueOf(id));
req.setAttribute("empInfo",emp);
try{
req.getRequestDispatcher("empEdit.jsp").forward(req,resp);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
/**
*
* @param req
* @param resp
* @throws IOException
*/
/* private void saveEmp(HttpServletRequest req,HttpServletResponse resp) throws IOException{
String id = req.getParameter("id");
String name = req.getParameter("empName");
String salary = req.getParameter("salary");
String date = req.getParameter("bornDate");
String depId = req.getParameter("depId");
String sex = req.getParameter("sex");
int empId = Integer.valueOf(id);
double s = Double.valueOf(salary);
int ss = Integer.valueOf(sex);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try{
d = sdf.parse(date);
}catch (Exception e){
System.out.println("日期转换失败,"+e.getMessage());
}
int did = Integer.valueOf(depId);
Emp emp = new Emp(empId,name,s,ss,d,did);
int i = service.saveEmp(emp);
if(i>0){
PrintWriter out =resp.getWriter();
out.write("<script>");
out.write("alert('添加成功');");
out.write("window.location.href='/emp?op=all'");
out.write("</script>");
}
}
*/
/**
* 查询所有 的部门信息
* @param req
* @param resp
* @throws IOException
*/
private void queryDept(HttpServletRequest req,HttpServletResponse resp) throws IOException{
IDeptService dept = new DeptServiceImpl();
List<Dept> deptList = dept.queryAllDept();
req.getSession().setAttribute("deptList",deptList);
resp.sendRedirect("empAdd.jsp");
}
/**
* 按照id删除员工
* @param req
* @param resp
* @throws IOException
*/
private void delEmp(HttpServletRequest req,HttpServletResponse resp) throws IOException {
//获得 要删除的id, 执行删除
resp.setContentType("text/html;charset=UTF-8");
resp.setCharacterEncoding("utf-8");
String empId = req.getParameter("empId");
int i = service.delEmpById(Integer.valueOf(empId));
if(i>0){
PrintWriter out =resp.getWriter();
out.write("<script>");
out.write("alert('删除成功');");
out.write("window.location.href='/emp?op=all'");
out.write("</script>");
}
}
}