package cn.updev.Action.Admin;

import cn.updev.EventWeight.Rate.EventGroupRateManage;
import cn.updev.Events.Event.EventDAO;
import cn.updev.Events.Group.EventGroupDAO;
import cn.updev.Users.Static.Login;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 作为后台的第一层拦截器
 * 作为区分普通用户和超级管理员用户进行分流
 * Created by hypo on 15-11-22.
 */
public class AdminAction extends ActionSupport {

    private String email;
    private String password;

    public String login(){

        if(this.email == null || this.password == null){
            return LOGIN;
        }

        Login login = new Login(this.email, this.password);

        if(login.judge()){

            return SUCCESS;
        }else {
            return LOGIN;
        }
    }

    public String execute() throws Exception {

        // 获得用户ID并进行权限判断权限判断
        Integer userId = 1;

        //如果是普通用户
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        EventDAO eventDAO = new EventDAO();
        EventGroupDAO groupDAO = new EventGroupDAO();

        Integer allUserEventNum = eventDAO.getAllUserEventNum(userId);
        Integer unFinishUserEventNum = eventDAO.getUnFinishUserEventNum(userId);
        Integer allteamEventNum = eventDAO.getAllTeamEventNum(userId);
        Integer unFinishteamEventNum = eventDAO.getUnFinishteamEventNum(userId);
        Integer allFinishEventNum = allUserEventNum + allteamEventNum - unFinishUserEventNum - unFinishteamEventNum;

        Integer allEventNum = allUserEventNum + allteamEventNum;
        Integer eventFinishRate;

        if(allEventNum == 0){
            eventFinishRate = 0;
        }else {
            Integer finishEventNum = allEventNum - unFinishUserEventNum - unFinishteamEventNum;
            eventFinishRate = 100 * finishEventNum / allEventNum;
        }

        session.setAttribute("unFinishUserEventNum", unFinishUserEventNum);
        session.setAttribute("unFinishteamEventNum", unFinishteamEventNum);
        session.setAttribute("allFinishEventNum", allFinishEventNum);
        session.setAttribute("eventFinishRate", eventFinishRate);

        List<Integer> groupIds = groupDAO.getUserEventGroupId(userId);
        EventGroupRateManage rateManage = new EventGroupRateManage();
        rateManage.setEventGroupRate(groupIds);

        return "user";
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

@@ -192,16 +192,29 @@
 .Static.
+FuctionClass.
 Login;%0Ai
@@ -307,16 +307,50 @@
 ntext;%0A%0A
+import javax.servlet.http.Cookie;%0A
 import j
@@ -638,35 +638,556 @@
 rd;%0A
-%0A    public String login()%7B
+    private Boolean remember;%0A%0A    public String login()%7B%0A%0A        HttpServletRequest request = ServletActionContext.getRequest();%0A        Cookie%5B%5D cookies = request.getCookies();%0A%0A        for(Cookie cookie : cookies)%7B%0A            if(cookie.getName().equals(%22ID%22))%7B%0A                this.email = cookie.getValue();%0A            %7Delse if(cookie.getName().equals(%22KEY%22))%7B%0A                this.email = cookie.getValue();%0A            %7D%0A        %7D%0A%0A        Login login = new Login();%0A        if(!login.isNotLogined())%7B%0A            return SUCCESS;%0A        %7D
 %0A%0A  
@@ -1278,38 +1278,32 @@
      %7D%0A%0A        
-Login 
 login = new Logi
@@ -1470,36 +1470,344 @@
 ing 
-execute() throws Exception %7B
+logout()%7B%0A%0A        Login login = new Login();%0A%0A        if(login.getLoginedUser() == null)%7B%0A            return LOGIN;%0A        %7D%0A%0A        login.logout();%0A%0A        return SUCCESS;%0A    %7D%0A%0A    public String execute() throws Exception %7B%0A%0A        Login login = new Login();%0A        if(login.isNotLogined())%7B%0A            return LOGIN;%0A        %7D
 %0A%0A  
@@ -1858,17 +1858,50 @@
 serId = 
-1
+login.getLoginedUser().getUserId()
 ;%0A%0A     
@@ -3706,11 +3706,167 @@
 d;%0A    %7D
+%0A%0A    public Boolean getRemember() %7B%0A        return remember;%0A    %7D%0A%0A    public void setRemember(Boolean remember) %7B%0A        this.remember = remember;%0A    %7D
 %0A%7D%0A

update login