HostGroupCtrl.java 6.67 KB
package com.ui.ctrl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ui.cloud.model.AutoScalingGroup;
import com.ui.common.TagTypeEnum;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.HostGroup;
import com.ui.model.req.PageRequest;
import com.ui.model.req.TagReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * Created by yoho on 2016/6/14.
 * 查询机器信息
 */
@Controller
@RequestMapping("hostGroup")
public class HostGroupCtrl {

    Logger log = LoggerFactory.getLogger(HostGroupCtrl.class);

    @Autowired
    HttpRestClient httpRestClient;

    @RequestMapping("/toHostGroup")
    public ModelAndView getHostGroups(Map model) {
        model.put("tagTypeList",TagTypeEnum.getAllType());
        model.put("tagTypeMapJson", JSON.toJSONString(TagTypeEnum.getAllTypeMap()));
        return new ModelAndView("host/hostGroupList",model);
    }


    @RequestMapping("/getHostGroups")
    @ResponseBody
    public BaseResponse getHostGroups(TagReq req) {
        BaseResponse response=httpRestClient.defaultPost(HttpUriContants.HOST_GROUP_GETALL_WITH_PAGE, req, BaseResponse.class);
        return response;
    }

    @RequestMapping("/getHostGroupsWithTreegrid")
    @ResponseBody
    public BaseResponse getHostGroupsWithTreegrid(TagReq req) {
        //"?tagTypeContent=" + req.getTagTypeContent()
        BaseResponse<List<HostGroup>> response = httpRestClient.exchangeForpost(HttpUriContants.HOST_ALL_GROUPS , new ParameterizedTypeReference<BaseResponse<List<HostGroup>>>() {
        }, req);
        List<HostGroup> allHostGroups= (List<HostGroup>) response.getData();
    // ls.add(t1);ls.add(t11);ls.add(t12);ls.add(t111);ls.add(t2);
        return new BaseResponse(turnToTree(allHostGroups));

    }

    @RequestMapping("/getAllTagsGroupByType")
    @ResponseBody
    public BaseResponse getAllTagsGroupByType() {
        try{
            BaseResponse response=httpRestClient.defaultPost(HttpUriContants.HOST_ALL_TAGS_GROUP_BY_TYPE, null, BaseResponse.class);
            return  response;
        }catch (Exception e){
            log.error("getAllTagsGroupByType error",e);
            return null;
        }

    }

    @RequestMapping("/getAllTagsGroupTreegridByType")
    @ResponseBody
    public BaseResponse getAllTagsGroupTreegridByType() {
        try{
            BaseResponse<List<HostGroup>> response = httpRestClient.exchangeForpost(HttpUriContants.HOST_ALL_GROUPS , new ParameterizedTypeReference<BaseResponse<List<HostGroup>>>() {
            }, new TagReq());
            List<HostGroup> allHostGroups= (List<HostGroup>) response.getData();
            List<HostGroup> allHostGroupTreeList=turnToTree(allHostGroups);

            Map<String,List<HostGroup>> map=  new HashMap<String,List<HostGroup>>();
            if(allHostGroupTreeList!=null&&allHostGroupTreeList.size()>0){
                for(HostGroup tag:allHostGroupTreeList){
                    if(map.keySet().contains(tag.getTagType())){
                        map.get(tag.getTagType()).add(tag);
                    }else{
                        List<HostGroup> ls=new ArrayList<HostGroup>();
                        ls.add(tag);
                        map.put(tag.getTagType(),ls);
                    }
                }
            }
            return  new BaseResponse(map);
        }catch (Exception e){
            log.error("getAllTagsGroupByType error",e);
            return null;
        }
    }

    private List<HostGroup> turnToTree(List<HostGroup> allHostGroups ){
        if(allHostGroups==null||allHostGroups.size()<=0){
            return new ArrayList<>();
        }
        Map<Integer,HostGroup> map=new HashMap<>();
        for(HostGroup group:allHostGroups){
            map.put(group.getId(),group);
        }
        //选择根节点
        List<HostGroup> rootHostGroup=new ArrayList<>();
        for(HostGroup group:allHostGroups){
            if(!map.keySet().contains(group.getPid())){
                rootHostGroup.add(group);
            }
        }
        //递归寻找子节点
        resuriveTreeChildren(rootHostGroup,map);
        return rootHostGroup;
    }

    private void resuriveTreeChildren(List<HostGroup> rootHostGroup,Map<Integer,HostGroup> allMap){
        if(rootHostGroup!=null&&rootHostGroup.size()>0){
            for(HostGroup group:rootHostGroup){
                for(Integer key:allMap.keySet()){
                    HostGroup thisNode= allMap.get(key);
                    if(thisNode.getPid()==group.getId()){
                        if(group.getChildren()==null){
                            List<HostGroup> child=new ArrayList<>();
                            child.add(thisNode);
                            group.setChildren(child);
                        }else{
                            group.getChildren().add(thisNode);
                        }
                    }
                }
                resuriveTreeChildren(group.getChildren(), allMap);
            }
        }

    }


    @RequestMapping("/saveHostGroup")
    @ResponseBody
    public BaseResponse saveHostGroup(int id,String groupName,String descr,String tagType,int pid) {
        Map map = new HashMap<>();
        map.put("id",id);
        map.put("groupName",groupName);
        map.put("descr",descr);
        map.put("tagType",tagType);
        map.put("pid",pid);
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.HOST_GROUP_SAVE, map, BaseResponse.class);
        return response;
    }

    @RequestMapping("/deleteHostGroup")
    @ResponseBody
    public BaseResponse deleteHostGroup(int id) {
        Map map = new HashMap<>();
        map.put("id",id);
        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.HOST_GROUP_DELETE, BaseResponse.class, map);
        return response;
    }

    @RequestMapping("/getAllGroups")
    @ResponseBody
    public BaseResponse getAllGroups() {
        try{
            TagReq req=new TagReq();
            BaseResponse response=httpRestClient.defaultPost(HttpUriContants.HOST_ALL_GROUPS, req, BaseResponse.class);
            return  response;
        }catch (Exception e){
            log.error("getAllGroups error",e);
            return null;
        }

    }



}