package cn.updev.Action.User;

import cn.updev.EventWeight.Rate.EventGroupRate;
import cn.updev.Users.Group.GroupInfo.GroupInfoFactory;
import cn.updev.Users.Group.GroupMemberRule.PrimaryUserRule;
import cn.updev.Users.Static.FuctionClass.Login;
import cn.updev.Users.Static.UserOrGroupDAO.UserOrGroupQuery;
import cn.updev.Users.Static.UserOrGroupInterface.IGroupInfo;
import cn.updev.Users.Static.UserOrGroupInterface.IGroupUser;
import cn.updev.Users.Static.UserOrGroupInterface.IUser;
import cn.updev.Users.User.GroupUserInfo;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

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

/**
 * Created by hypo on 15-12-3.
 */
public class UserGroupAction extends ActionSupport {

    private Integer groupId;
    private String groupName;
    private String groupIntro;

    public String createUserGroup(){

        Login login = new Login();

        if(login.isNotLogined()){
            return LOGIN;
        }

        if(groupName == null || groupName.trim().length() == 0){
            return INPUT;
        }

        IUser user = login.getLoginedUser();
        PrimaryUserRule createGroup = new PrimaryUserRule();
        IGroupInfo groupInfo = createGroup.createGroup(groupName, groupIntro, user.getUserId());

        if(groupInfo != null){

            groupId = groupInfo.getGroupId();
            return SUCCESS;
        }

        return INPUT;

    }

    public String groupManage(){

        Login login = new Login();

        if(login.isNotLogined()){
            return LOGIN;
        }

        IUser user = login.getLoginedUser();

        HttpServletRequest request = ServletActionContext.getRequest();

        if(groupId == null || groupId < 1){

            List<IGroupInfo> groupInfoList = new UserOrGroupQuery().queryGroupAllUserJoined(user.getUserId());
            List<IGroupInfo> createGroup = new ArrayList<IGroupInfo>();
            List<IGroupInfo> manageGroup = new ArrayList<IGroupInfo>();
            List<IGroupInfo> joinGroup = new ArrayList<IGroupInfo>();

            for(IGroupInfo info : groupInfoList){
                UserOrGroupQuery DAO = new UserOrGroupQuery();
                IGroupUser groupUser = DAO.queryGroupUser(user.getUserId(), info.getGroupId());

                if(groupUser.isCreater()){
                    createGroup.add(info);
                }else if(groupUser.isAdmin()){
                    manageGroup.add(info);
                }else if(groupUser.isUser()){
                    joinGroup.add(info);
                }
            }

            request.setAttribute("createGroup",createGroup);
            request.setAttribute("manageGroup",manageGroup);
            request.setAttribute("joinGroup",joinGroup);

            return INPUT;
        }

        UserOrGroupQuery DAO = new UserOrGroupQuery();

        IGroupInfo groupInfo = DAO.queryGroupInfoById(groupId);
        List<GroupUserInfo> groupUser = DAO.queryGroupMemberInfoAll(groupId);
        List<EventGroupRate> groupTask = new ArrayList<EventGroupRate>();
        IGroupUser userInfo = DAO.queryGroupUser(user.getUserId(), groupId);

        this.groupName = groupInfo.getGroupName();
        this.groupIntro = groupInfo.getGroupIntro();
        request.setAttribute("groupInfo",groupInfo);
        request.setAttribute("groupUser", groupUser);
        request.setAttribute("groupTask", groupTask);
        request.setAttribute("userInfo", userInfo);

        return SUCCESS;
    }

    public String updateGroupInfo(){

        Login login = new Login();

        if(login.isNotLogined()){
            return LOGIN;
        }

        if(groupName == null || groupName.trim().length() == 0 || groupIntro == null || groupIntro.trim().length() == 0){

            return SUCCESS;
        }

        IUser user = login.getLoginedUser();

        UserOrGroupQuery DAO = new UserOrGroupQuery();

        IGroupUser userInfo = DAO.queryGroupUser(user.getUserId(), groupId);

        if(!userInfo.isAdmin() && !userInfo.isCreater()){
            return ERROR;
        }

        IGroupInfo groupInfo = DAO.queryGroupInfoById(groupId);
        groupInfo.setGroupName(this.groupName);
        groupInfo.setGroupIntro(this.groupIntro);

        GroupInfoFactory factory = new GroupInfoFactory();
        factory.updateGroupInfo(groupInfo);

        return SUCCESS;
    }



    public Integer getGroupId() {
        return groupId;
    }

    public void setGroupId(Integer groupId) {
        this.groupId = groupId;
    }

    public String getGroupIntro() {
        return groupIntro;
    }

    public void setGroupIntro(String groupIntro) {
        this.groupIntro = groupIntro;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }
}

@@ -73,16 +73,115 @@
 upRate;%0A
+import cn.updev.EventWeight.Rate.EventGroupRateManage;%0Aimport cn.updev.Events.Group.EventGroupDAO;%0A
 import c
@@ -3154,82 +3154,8 @@
 d);%0A
-        List%3CEventGroupRate%3E groupTask = new ArrayList%3CEventGroupRate%3E();%0A
     
@@ -3473,81 +3473,296 @@
 te(%22
-groupTask%22, groupTask);%0A        request.setAttribute(%22userInfo%22, userInfo
+userInfo%22, userInfo);%0A%0A        //%E5%9B%A2%E9%98%9F%E4%BA%8B%E4%BB%B6%E7%BB%84%E8%BF%9B%E5%BA%A6%E7%BB%9F%E8%AE%A1%0A        EventGroupDAO groupDAO = new EventGroupDAO();%0A        List%3CInteger%3E groupIds = groupDAO.getTeamEventGroupId(groupId);%0A        EventGroupRateManage rateManage = new EventGroupRateManage();%0A        rateManage.setTeamEventGroupRate(groupIds
 );%0A%0A

update user action