/** 
 * @Title: GroupsService.java 
 * @Package com.zhiwei.service 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author hero 
 * @date 2016年1月27日 下午6:52:22 
 * @version V1.0 
 */
package com.zhiwei.weixin.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhiwei.conf.Conf;
import com.zhiwei.dao.GroupsDao;
import com.zhiwei.dao.impl.GroupsDaoImpl;
import com.zhiwei.weixin.Groups;
import com.zhiwei.weixin.Users;
import com.zhiwei.weixin.pojo.Group;

/**
 * @ClassName: GroupsService
 * @Description: TODO(分组管理的业务处理类)
 * @author hero
 * @date 2016年1月27日 下午6:52:22
 */
public class GroupsService
{

    private Groups groups;
    private Users users;
    private GroupsDao groupsDao;
    private static Logger log = LoggerFactory.getLogger(TemplateService.class);

    public GroupsService()
    {
        groups = new Groups();
        users = new Users();
        groupsDao = new GroupsDaoImpl();
    }

    /**
     * 
     * @Title: getOpenIdList
     * @Description: TODO(根据微信分组名查询其下的所有用户)
     * @param @param groupName
     * @param @return 设定文件
     * @return List<String> 返回类型
     */
    public List<String> getOpenIdList(String groupName)
    {
        List<String> openidList = new ArrayList<String>();
        int groupId = groupsDao.getGroupId(Conf.appId, groupName);
        if (-100 != groupId)
        {
            List<String> openIdList = users.getUserList();
            for (String openid : openIdList)
            {
                if (groupId == groups.showGroupIdByOpenid(openid))
                {
                    openidList.add(openid);
                }
                else
                {
                    log.info(openid + "用户不再" + groupName + "分组中");
                }
            }
        }
        else
        {
            HashMap<String, Group> groupMap = groups.findAllGroup();
            for (Entry<String, Group> entry : groupMap.entrySet())
            {
                groupsDao.addGroup(entry.getValue(), Conf.appId);
            }
            Group group = groupMap.get(groupName);
            if (null != group)
            {
                groupId = group.getId();
                List<String> openIdList = users.getUserList();
                for (String openid : openIdList)
                {
                    if (groupId == groups.showGroupIdByOpenid(openid))
                    {
                        openidList.add(openid);
                    }
                    else
                    {
                        log.info(openid + "用户不再" + groupName + "分组中");
                    }
                }
            }
        }
        return openidList;
    }
    
    
    /**
     * 
     * @Title: getOpenIdList
     * @Description: TODO(根据微信分组名查询其下的所有用户)
     * @param @param groupName
     * @param @return 设定文件
     * @return List<String> 返回类型
     */
    public List<String> getOpenIdList_m(String groupName)
    {
        List<String> openidList = new ArrayList<String>();
        int groupId = groupsDao.getGroupId(Conf.appId, groupName);
        if (-100 != groupId)
        {
        	openidList = users.getUserListByGroupId(groupId);
        }
        else
        {
            HashMap<String, Group> groupMap = groups.findAllGroup();
            for (Entry<String, Group> entry : groupMap.entrySet())
            {
                groupsDao.addGroup(entry.getValue(), Conf.appId);
            }
            Group group = groupMap.get(groupName);
            if (null != group)
            {
                groupId = group.getId();
                List<String> openIdList = users.getUserList();
                for (String openid : openIdList)
                {
                    if (groupId == groups.showGroupIdByOpenid(openid))
                    {
                        openidList.add(openid);
                    }
                    else
                    {
                        log.info(openid + "用户不再" + groupName + "分组中");
                    }
                }
            }
        }
        return openidList;
    }
    
}
