com.esd.ps.LoginController.java Source code

Java tutorial

Introduction

Here is the source code for com.esd.ps.LoginController.java

Source

/*
 * Copyright (c) 2013 ????www.hrbesd.com. All rights reserved.
 * 
 * HRBESD PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.esd.ps;

import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.esd.common.util.UsernameAndPasswordMd5;
import com.esd.db.model.manager;
import com.esd.db.model.packWithBLOBs;
import com.esd.db.model.task;
import com.esd.db.model.user;
import com.esd.db.model.usertype;
import com.esd.db.model.workerRecord;
import com.esd.db.service.EmployerService;
import com.esd.db.service.InspectorService;
import com.esd.db.service.ManagerService;
import com.esd.db.service.PackService;
import com.esd.db.service.SalaryService;
import com.esd.db.service.TaskService;
import com.esd.db.service.UserService;
import com.esd.db.service.UserTypeService;
import com.esd.db.service.WorkerRecordService;
import com.esd.db.service.WorkerService;

/**
 * 
 * 
 * @author chen
 * 
 */
@Controller
public class LoginController {
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private UserTypeService userTypeService;
    @Autowired
    private ManagerService managerService;
    @Autowired
    private EmployerService employerService;
    @Autowired
    private InspectorService InspectorService;
    @Autowired
    private WorkerService workerService;
    @Autowired
    private WorkerRecordService workerRecordService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private PackService packService;
    @Autowired
    private SalaryService salaryService;

    /**
     * ???
     */
    @Value("${MSG_USER_NOT_EXIST}")
    private String MSG_USER_NOT_EXIST;

    /**
     * ?
     */
    @Value("${MSG_USER_NOT_EMPTY}")
    private String MSG_USER_NOT_EMPTY;

    /**
     * ??
     */
    @Value("${MSG_PASSWORD_NOT_EMPTY}")
    private String MSG_PASSWORD_NOT_EMPTY;

    /**
     * ?
     */
    @Value("${MSG_PASSWORD_NOT_ERROR}")
    private String MSG_PASSWORD_NOT_ERROR;
    /**
     * ?
     */
    @Value("${MSG_USER_STOP}")
    private String MSG_USER_STOP;

    /**
     * 
     * 
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView index() {
        return new ModelAndView("login");
    }

    /**
     * ?
     * 
     * @return
     */
    @RequestMapping(value = "/quit", method = RequestMethod.GET)
    public ModelAndView quitGet(HttpSession session, HttpServletRequest request) {
        session.removeAttribute(Constants.USER_ID);
        session.removeAttribute(Constants.USER_LVL);
        session.removeAttribute("typeCount");
        session.removeAttribute(Constants.USER_TYPE);
        session.removeAttribute(Constants.ADD_USER_ID);
        session.removeAttribute("aduiting");
        session.removeAttribute("aduited");
        session.removeAttribute("salary");
        session.removeAttribute("uploading");
        //      try {
        //         ServletContext servletContext = request.getSession().getServletContext();
        //         if (servletContext.getAttribute(session.getAttribute(Constants.USER_NAME).toString()) != null) {
        //            servletContext.removeAttribute(session.getAttribute(Constants.USER_NAME).toString());
        //         }
        //      } catch (NullPointerException n) {
        //         return new ModelAndView(Constants.REDIRECT + Constants.COLON + Constants.LOGIN);
        //      }
        session.removeAttribute(Constants.USER_NAME);

        return new ModelAndView(Constants.REDIRECT + Constants.COLON + Constants.LOGIN);
    }

    /**
     * ????
     * 
     * @param username
     * @return
     */
    @RequestMapping(value = "/checkUserName", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> checkUserName(String username) {

        return checkLoginName(username);
    }

    /**
     * ????,??
     * 
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView loginPost(String username, String password, RedirectAttributes redirectAttributes,
            HttpSession session, HttpServletRequest request) {
        if (StringUtils.isBlank(username)) {
            redirectAttributes.addFlashAttribute(Constants.MESSAGE, MSG_USER_NOT_EMPTY);
        }
        if (StringUtils.isBlank(password)) {
            redirectAttributes.addFlashAttribute(Constants.MESSAGE, MSG_PASSWORD_NOT_EMPTY);
        }
        // ??
        //      int m = loginName(username, request);
        //      if (m == 0) {
        //         redirectAttributes.addFlashAttribute(Constants.MESSAGE, "?");
        //         return new ModelAndView(Constants.REDIRECT + Constants.COLON + Constants.LOGIN);
        //      }
        user user = userService.getAllUsersByUserName(username);
        if (user == null) {
            redirectAttributes.addFlashAttribute(Constants.MESSAGE, MSG_USER_NOT_EXIST);
        } else {
            if (user.getUserStatus() == false) {
                redirectAttributes.addFlashAttribute(Constants.MESSAGE, MSG_USER_STOP);
                redirectAttributes.addFlashAttribute(Constants.USER_NAME, username);
                return new ModelAndView(Constants.REDIRECT + Constants.COLON + Constants.LOGIN);
            }
            UsernameAndPasswordMd5 md5 = new UsernameAndPasswordMd5();
            String md5Password = md5.getMd5(username, password);
            //logger.debug("md5Password:{}", md5Password);
            // ?
            if (md5Password.equals(user.getPassword())) {

                session.setAttribute(Constants.USER_NAME, user.getUsername());
                session.setAttribute(Constants.USER_ID, user.getUserId());
                session.setAttribute(Constants.USER_LVL, user.getUserLvl());
                session.setAttribute(Constants.USER_TYPE, user.getUsertype());
                usertype userType = userTypeService.getUserTypeById(user.getUsertype());
                //logger.debug("typeName:{}", userType.getUserTypeNameEnglish());
                String typeName = userType.getUserTypeNameEnglish();
                if (typeName.equals(Constants.MANAGER)) {
                    if (managerService.getCountManagerIdByUserId(user.getUserId()) == 0) {
                        return new ModelAndView(Constants.MANAGER + Constants.SLASH + Constants.MANAGER
                                + Constants.UNDERLINE + Constants.ADD, Constants.USER_REGISTED, 0);
                    }
                } else if (typeName.equals(Constants.EMPLOYER)) {
                    if (employerService.getCountEmployerIdByUserId(user.getUserId()) == 0) {
                        return new ModelAndView(Constants.MANAGER + Constants.SLASH + Constants.EMPLOYER
                                + Constants.UNDERLINE + Constants.ADD, Constants.USER_REGISTED, 0);
                    }
                } else if (typeName.equals("inspector")) {
                    if (InspectorService.getCountInspectorIdByUserId(user.getUserId()) == 0) {
                        return new ModelAndView(Constants.MANAGER + Constants.SLASH + "inspector"
                                + Constants.UNDERLINE + Constants.ADD, Constants.USER_REGISTED, 0);
                    }

                } else if (typeName.equals(Constants.WORKER)) {
                    if (workerService.getCountWorkerIdByUserId(user.getUserId()) == 0) {
                        return new ModelAndView(Constants.MANAGER + Constants.SLASH + Constants.WORKER
                                + Constants.UNDERLINE + Constants.ADD, Constants.USER_REGISTED, 0);
                    }
                }
                return new ModelAndView(
                        Constants.REDIRECT + Constants.COLON + Constants.SECURITY + Constants.SLASH + typeName);

            } else {
                redirectAttributes.addFlashAttribute(Constants.MESSAGE, MSG_PASSWORD_NOT_ERROR);
            }
        }
        redirectAttributes.addFlashAttribute(Constants.USER_NAME, username);
        redirectAttributes.addFlashAttribute(Constants.USER_PASSWORD, password);
        return new ModelAndView(Constants.REDIRECT + Constants.COLON + Constants.LOGIN);
    }

    /**
     * ?
     * 
     * 
     * @param request
     */
    @RequestMapping(value = "/checkOldTask", method = RequestMethod.POST)
    public void checkOldTaskPOST(HttpServletRequest request) {
        List<workerRecord> workerRecordList = workerRecordService.getDoingTask();
        SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATETIME_FORMAT);
        if (workerRecordList.isEmpty() == false || workerRecordList != null) {
            for (Iterator<workerRecord> iterator = workerRecordList.iterator(); iterator.hasNext();) {
                workerRecord workerRecord = (workerRecord) iterator.next();
                Date begin, end;
                int m = 0;
                try {
                    begin = sdf.parse(sdf.format(workerRecord.getTaskDownTime()));
                    end = sdf.parse(sdf.format(new Date()));
                    long between = (end.getTime() - begin.getTime());// 
                    int packLockTime = workerRecord.getTaskLockTime();
                    //logger.debug(":{}", (packLockTime - between));
                    if ((packLockTime - between) == 0 || (packLockTime - between) < 0) {
                        // worker_record
                        workerRecord update = new workerRecord();
                        update.setTaskStatu(2);// 2
                        update.setTaskEffective(0);
                        update.setTaskOverTime(new Date());//
                        update.setRecordId(workerRecord.getRecordId());
                        StackTraceElement[] items = Thread.currentThread().getStackTrace();
                        update.setUpdateMethod(items[1].toString());
                        m = workerRecordService.updateByPrimaryKeySelective(update);
                        if (m > 0) {
                            // task
                            task task = new task();
                            task.setWorkerId(0);
                            task.setTaskMarkTime(0.00);
                            task.setTaskId(workerRecord.getTaskId());
                            task.setTaskUpload(false);
                            StackTraceElement[] item = Thread.currentThread().getStackTrace();
                            task.setUpdateMethod(item[1].toString());
                            taskService.updateByTaskId(task);
                        }
                        //                  packWithBLOBs pack = new packWithBLOBs();
                        //                  pack.setPackStatus(0);
                        //                  pack.setPackId(workerRecord.getPackId());
                        //                  packService.updateByPrimaryKeySelective(pack);

                        // 
                        String url = request.getSession().getServletContext().getRealPath(Constants.SLASH);
                        File fold = new File(url + Constants.WORKERTEMP);
                        if (fold.exists()) {
                            File zipFile = new File(url + Constants.SLASH + workerRecord.getDownPackName());
                            if (zipFile.exists()) {
                                zipFile.delete();
                            }
                        }
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * ??
     * 
     * @param username
     * @return
     */
    public Map<String, Object> checkLoginName(String username) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(username)) {
            user user = userService.getAllUsersByUserName(username);
            if (user == null) {
                map.clear();
                map.put(Constants.MESSAGE, MSG_USER_NOT_EXIST);
                map.put(Constants.REPLAY, Constants.ZERO);
                return map;
            }
            if (user.getUserStatus() == false) {
                map.clear();
                map.put(Constants.MESSAGE, MSG_USER_STOP);
                map.put(Constants.REPLAY, Constants.ZERO);
                return map;
            }
            map.clear();
            map.put(Constants.REPLAY, Constants.ONE);
        }
        return map;
    }

    /**
     * ??
     * @param loginName
     * @param request
     * @return
     */
    public int loginName(String loginName, HttpServletRequest request) {
        ServletContext servletContext = request.getSession().getServletContext();
        if (servletContext.getAttribute(loginName) == null) {
            servletContext.setAttribute(loginName, loginName);
            return 1;
        }
        return 0;
    }

    /**
     * ,
     * @return
     */
    @RequestMapping(value = "/moneyList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> moneyListPost() {
        Map<String, Object> map = new HashMap<String, Object>();
        //
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int week = cal.get(Calendar.DAY_OF_WEEK);
        //
        if (week == 7) {
            week = 0;
        }
        //logger.debug("week:{}",week);
        //
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // ()?
        GregorianCalendar gc = new GregorianCalendar();
        // ??
        gc.setTime(new Date());
        // ??
        gc.add(5, -week);
        // 
        String beginDate = sdf.format(gc.getTime());
        String endDate = sdf.format(new Date());
        SimpleDateFormat sdf1 = new SimpleDateFormat("MMdd");
        String beginDate1 = sdf1.format(gc.getTime());
        String endDate1 = sdf1.format(new Date());
        //logger.debug("beginDate:{},endDate:{}",beginDate,endDate);
        manager manager = managerService.selectByPrimaryKey(1);
        List<Map<String, Object>> monthList = salaryService.getMoneyList("", "", endDate);
        List<Map<String, Object>> weekList = salaryService.getMoneyList(beginDate, endDate, "");
        List<Map<String, Object>> totleList = salaryService.getMoneyList("", "", "");

        map.put("salary", manager.getSalary());
        map.put("monthList", monthList);
        map.put("weekList", weekList);
        map.put("totleList", totleList);
        map.put("weekDate", beginDate1 + "-" + endDate1);
        return map;
    }

    /**
     * ,?,?,?
     * @return
     */
    @RequestMapping(value = "/datas", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> datasPost() {
        Map<String, Object> map = new HashMap<String, Object>();
        int peopleCountTotle = workerService.getWorkerCount();
        double moneyTotle = 0.00;
        try {
            moneyTotle = salaryService.getMoneyTotle(0);
        } catch (NullPointerException n) {

        }

        int taskCountTotle = taskService.getWorkerIdZeroCountByPackId(0);
        int taskCount = taskService.getCountTaskDoing(1);
        double moneyToday = 0.00;
        try {
            moneyToday = salaryService.getMoneyTotle(1);
        } catch (NullPointerException n) {
            moneyToday = 0.00;
        }
        BigDecimal b = new BigDecimal(moneyTotle / 18);
        moneyTotle = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        BigDecimal b1 = new BigDecimal(moneyToday / 18);
        moneyToday = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        map.put("peopleCountTotle", peopleCountTotle);
        map.put("moneyTotle", moneyTotle);
        map.put("taskCountTotle", taskCountTotle);
        map.put("taskCount", taskCount);
        map.put("moneyToday", moneyToday);
        return map;
    }
}