package com.ctrip.zeus.service.model.impl;

import com.ctrip.zeus.dal.core.GroupDo;
import com.ctrip.zeus.model.entity.Group;
import com.ctrip.zeus.model.entity.GroupServer;
import com.ctrip.zeus.model.entity.GroupSlb;
import com.ctrip.zeus.model.entity.VirtualServer;
import com.ctrip.zeus.service.model.SlbRepository;
import com.ctrip.zeus.service.model.handler.GroupQuery;
import com.ctrip.zeus.service.model.GroupRepository;
import com.ctrip.zeus.service.model.handler.GroupSync;
import com.ctrip.zeus.service.model.ArchiveService;
import com.ctrip.zeus.service.model.handler.SlbQuery;
import com.ctrip.zeus.support.C;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:xingchaowang
 * @date: 3/7/2015.
 */
@Repository("groupRepository")
public class GroupRepositoryImpl implements GroupRepository {
    @Resource
    private GroupSync groupSync;
    @Resource
    private GroupQuery groupQuery;
    @Resource
    private SlbRepository slbRepository;
    @Resource
    private ArchiveService archiveService;

    @Override
    public List<Group> list() throws Exception {
        List<Group> list = new ArrayList<>();
        for (Group group : groupQuery.getAll()) {
            list.add(group);
        }
        return list;

    }

    @Override
    public List<Group> list(String slbName, String virtualServerName) throws Exception {
        List<Group> list = new ArrayList<>();
        Long slbId = slbRepository.get(slbName).getId();
        VirtualServer vs = slbRepository.getVirtualServer(null, slbId, virtualServerName);
        for (Group group : groupQuery.getByVirtualServer(vs.getId())) {
            list.add(group);
        }
        return list;
    }

    @Override
    public List<Group> list(Long slbId) throws Exception {
        List<Group> result = new ArrayList<>();
        for (GroupSlb groupSlb : slbRepository.listGroupSlbsBySlb(slbId)) {
            result.add(getById(groupSlb.getGroupId()));
        }
        return result;
    }

    @Override
    public List<Group> listLimit(Long fromId, int maxCount) throws Exception {
        return groupQuery.getLimit(fromId, maxCount);
    }

    @Override
    public List<Group> list(Long[] ids) throws Exception {
        return groupQuery.batchGetByIds(ids);
    }

    @Override
    public Group getById(Long id) throws Exception {
        return groupQuery.getById(id);
    }

    @Override
    public Group get(String groupName) throws Exception {
        return groupQuery.get(groupName);
    }

    @Override
    public Group getByAppId(String appId) throws Exception {
        return groupQuery.getByAppId(appId);
    }

    @Override
    public Group add(Group group) throws Exception {
        GroupDo d = groupSync.add(group);
        archiveService.archiveGroup(groupQuery.getById(d.getId()));
        return C.toGroup(d);

    }

    @Override
    public Group update(Group group) throws Exception {
        if (group == null)
            return null;
        GroupDo d = groupSync.update(group);
        group = groupQuery.getById(d.getId());
        archiveService.archiveGroup(group);
        return group;
    }

    @Override
    public int delete(Long groupId) throws Exception {
        int count = groupSync.delete(groupId);
        return count;

    }

    @Override
    public List<String> listGroupsByGroupServer(String groupServerIp) throws Exception {
        return groupQuery.getByGroupServer(groupServerIp);
    }

    @Override
    public List<String> listGroupServerIpsByGroup(Long groupId) throws Exception {
        return groupQuery.getGroupServerIpsByGroup(groupId);
    }

    @Override
    public List<GroupServer> listGroupServersByGroup(Long groupId) throws Exception {
        return groupQuery.getGroupServersByGroup(groupId);
    }
}

@@ -746,19 +746,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A/**%0A
@@ -1230,156 +1230,35 @@
     
-List%3CGroup%3E list = new ArrayList%3C%3E();%0A        for (Group group : groupQuery.getAll()) %7B%0A            list.add(group);%0A        %7D%0A        return list;%0A
+return groupQuery.getAll();
 %0A   
@@ -1782,41 +1782,39 @@
     
-List%3CGroup%3E result = new ArrayLis
+Set%3CLong%3E groupIds = new HashSe
 t%3C%3E(
@@ -1848,17 +1848,16 @@
 oupSlb :
- 
 slbRepos
@@ -1907,25 +1907,19 @@
     
-result.add(getByI
+groupIds.ad
 d(gr
@@ -1934,25 +1934,24 @@
 etGroupId())
-)
 ;%0A        %7D%0A
@@ -1969,14 +1969,77 @@
 urn 
-result
+groupQuery.batchGetByIds(groupIds.toArray(new Long%5BgroupIds.size()%5D))
 ;%0A  

fix bug of duplicate groups get by slbId