package emp.controller;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONArray;
import emp.controller.vo.EmpAttendance;
import emp.dao.mapper.ApplicantsMapper;
import emp.dao.mapper.AttendanceMapper;
import emp.dao.mapper.EmployeeMapper;
import emp.dao.mapper.MettingMapper;
import emp.dao.mapper.TrainMapper;
import emp.dao.model.Applicants;
import emp.dao.model.Attendance;
import emp.dao.model.Employee;
import emp.dao.model.Metting;
import emp.dao.model.Train;
import emp.utils.DateUtils;
import utils.page.PageView;
@Controller
@RequestMapping("/manager")
public class ManagerController {
@Autowired
private EmployeeMapper empMapper;
@Autowired
private AttendanceMapper attMapper;
@Autowired
private MettingMapper mettingMapper;
@Autowired
private ApplicantsMapper appMapper;
@Autowired
private TrainMapper trainMapper;
@RequestMapping("login")
public String loginPage() {
return "manager/login";
}
@RequestMapping("login-form")
public ModelAndView loginForm(String username, String pwd, HttpSession session) {
ModelAndView mav = new ModelAndView("redirect:home");
Employee record = new Employee(username, pwd);
record.setRole(2);
List<Employee> emp = empMapper.select(record);
if(emp == null || emp.size() == 0) {
mav.addObject("msg", "用户名或密码错误");
mav.setViewName("manager/login");
}else {
session.setAttribute("currManager", emp.get(0));
}
return mav;
}
@RequestMapping("logout")
public String logout(HttpSession session) {
session.removeAttribute("currManager");
return "redirect:login";
}
@RequestMapping("home")
public ModelAndView home(HttpSession session) {
ModelAndView mav = new ModelAndView("manager/home");
Employee emp = (Employee) session.getAttribute("currManager");
String day = DateUtils.getTodayStr();
Attendance att = attMapper.findEmpAttToday(day , emp.getEmpId());
if(att != null) {
if(att.getCheckin() != null) {
mav.addObject("checkin", DateUtils.parseTime(att.getCheckin()));
}
if(att.getCheckout() != null) {
mav.addObject("checkout", DateUtils.parseTime(att.getCheckout()));
}
}
mav.addObject("today", DateUtils.getTodayStr());
mav.addObject("now", DateUtils.getCnDateTimeStr());
return mav;
}
/**
* 上班打卡
* @param emp
* @return
*/
@RequestMapping("checkin")
public String checkin(String today, HttpSession session) {
Employee emp = (Employee) session.getAttribute("currManager");
Attendance att = attMapper.findEmpAttToday(today , emp.getEmpId());
boolean insert = false;
if(att == null) {
att = new Attendance();
insert = true;
}
att.setAttDay(today);
att.setEmpId(emp.getEmpId());
att.setCheckin(new Date());
String status = "正常";
if(DateUtils.checkLate(att.getCheckin())) {
status = "迟到";
}
att.setStatus(status);
if(insert) {
attMapper.insert(att);
}else {
attMapper.updateByPrimaryKeySelective(att);
}
return "redirect:attmgr";
}
/**
* 下班打卡
* @param emp
* @return
*/
@RequestMapping("checkout")
public String checkout(String today, HttpSession session) {
Employee emp = (Employee) session.getAttribute("currManager");
Attendance att = attMapper.findEmpAttToday(today , emp.getEmpId());
boolean insert = false;
if(att == null) {
att = new Attendance();
insert = true;
}
att.setAttDay(today);
att.setEmpId(emp.getEmpId());
att.setCheckout(new Date());
String status = "";
if(StringUtils.isNotBlank(att.getStatus())) {
status += att.getStatus();
}
if(DateUtils.checkLeave(att.getCheckout())) {
if(StringUtils.isNotBlank(status)) {
status += "|";
}
status += "早退";
}
att.setStatus(status);
if(insert) {
attMapper.insert(att);
}else {
attMapper.updateByPrimaryKeySelective(att);
}
return "redirect:attmgr";
}
/* *********************员工信息管理**************************/
@RequestMapping("toaddemp")
public ModelAndView toAddEmp(HttpSession session) {
ModelAndView mav = new ModelAndView("manager/emp-add");
Employee mgr = (Employee) session.getAttribute("currManager");//当前登录的主管
mav.addObject("mgr", mgr);
return mav;
}
@RequestMapping("saveEmp")
public String saveEmp(Employee emp) {
empMapper.insert(emp);
return "redirect:empmgr";
}
@RequestMapping("empmgr")
public ModelAndView empList(PageView<Employee> page, HttpSession session) {
ModelAndView mav = new ModelAndView("manager/emp-list");
Employee emp = new Employee();
emp.setRole(1);//主管只能管理普通员工
Employee mgr = (Employee) session.getAttribute("currManager");//当前登录的主管
emp.setDept(mgr.getDept());//主管只能查询自己部门的员工
List<Employee> list = empMapper.selectByRowBounds(emp, new RowBounds(page.getFirstResult(), page.getMaxresult())); //empService.findUsers(page.getFirstResult(), page.getMaxresult());
page.setRecords(list);
page.setTotalrecord(empMapper.selectCount(new Employee()));
mav.addObject("page", page);
return mav;
}
@RequestMapping("toupdateemp")
public ModelAndView toUserModify(Integer uid) {
ModelAndView mav = new ModelAndView("manager/emp-update");
Employee emp = empMapper.selectByPrimaryKey(uid);
mav.addObject("emp", emp);
return mav;
}
@RequestMapping("updateemp")
public String updateUser(Employee emp) {
empMapper.updateByPrimaryKey(emp);
return "redirect:empmgr";
}
@RequestMapping("toupdatesal")
public ModelAndView toupdatesal(Integer uid) {
ModelAndView mav = new ModelAndView("manager/emp-update2");
Employee emp = empMapper.selectByPrimaryKey(uid);
mav.addObject("emp", emp);
return mav;
}
@RequestMapping("updateemp2")
public String updateUser2(Integer empId, Integer discount) {
Employee emp = empMapper.selectByPrimaryKey(empId);
emp.setDiscount(discount);
return "redirect:empmgr";
}
/**
* 注销员工
* @param uid
* @return
*/
@RequestMapping("deleteemp")
public String deleteUser(Integer uid) {
Employee emp = empMapper.selectByPrimaryKey(uid);
emp.setStatus(-1);
empMapper.updateByPrimaryKeySelective(emp);
return "redirect:empmgr";
}
/* *********************员工信息管理END**************************/
/* *********************考勤信息管理**************************/
/**
* 考勤信息列表
* @param page
* @return
*/
@RequestMapping("attmgr")
public ModelAndView attList(PageView<EmpAttendance> page, HttpSession session) {
ModelAndView mav = new ModelAndView("manager/att-list");
Employee mgr = (Employee) session.getAttribute("currManager");//当前登录的主管
List<Attendance> list = attMapper.findDeptAttendancePage(mgr.getDept(), page.getFirstResult(), page.getMaxresult());
List<EmpAttendance> vos = new ArrayList<>();
for (Attendance att : list) {
EmpAttendance vo = new EmpAttendance();
vo.setEmp(empMapper.selectByPrimaryKey(att.getEmpId()));
vo.setAtt(att);
vos.add(vo);
}
page