HostInfoCtrl.java 4.98 KB
package com.ui.ctrl;


import com.alibaba.fastjson.JSON;
import com.ui.common.TagTypeEnum;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.req.HostInfoReq;
import com.ui.model.req.SetHostTagsReq;
import com.ui.model.req.TagReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

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

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

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

    @Autowired
    HttpRestClient httpRestClient;

    @RequestMapping("/toHostInfos")
    public ModelAndView toHostInfos() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("host/hostInfoList");

        mv.addObject("tagTypeList", TagTypeEnum.getAllType());
        mv.addObject("tagsListJson", this.getHostTagsByType("","groupName").getData());

        return mv;
    }

    /**
     * 根据标签类别,查询该类别下的属于tag
     * @param type
     * @return
     */
    @RequestMapping("/getHostTagsByType")
    @ResponseBody
    public BaseResponse getHostTagsByType(String type,String idMark){
        if(type==null){
            type="";
        }
        TagReq req=new TagReq();
        req.setTagTypeContent(type);
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.HOST_ALL_GROUPS, req, BaseResponse.class);
        List<Map<String, String>> hostGroups = (List<Map<String, String>>) response.getData();
        List<Map<String, String>> tags = new ArrayList<Map<String, String>>();
        if (hostGroups != null && hostGroups.size() > 0) {
            for (Map<String, String> m : hostGroups) {
                HashMap map = new HashMap();
                map.put("text", m.get("groupName"));
                if("groupName".equals(idMark)){
                    map.put("id", m.get("groupName"));
                }else{
                    map.put("id", m.get("id"));
                }
                tags.add(map);
            }
        }
        return new BaseResponse<>(JSON.toJSON(tags));
    }

    @RequestMapping("/toEditHostInfos")
    public ModelAndView toEditHostInfos(Map model) {
        model.put("tagTypeMapJson", JSON.toJSONString(TagTypeEnum.getAllTypeMap()));
        return new ModelAndView("host/editHostInfo",model);
    }


    @RequestMapping("/getHostInfos")
    @ResponseBody
    public BaseResponse getHostInfos(HostInfoReq req) throws Exception {

        List<String> tagsList = JSON.parseArray(req.getTags(), String.class);
        req.setTagsList(tagsList);
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.GET_HOST_INFOS, req, BaseResponse.class);
        return response;
    }


    @RequestMapping("/saveHostInfo")
    @ResponseBody
    public BaseResponse saveHostInfo(HostInfoReq req) throws Exception {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.SAVE_HOST_INFOS, req, BaseResponse.class);
        return response;
    }


    @RequestMapping("/delHostInfo")
    @ResponseBody
    public BaseResponse<Integer> delHostInfo(int id,String hostIp) throws Exception {
        //hostIp ,用作日志
        Map<String, Object> map=new HashMap<>();
        map.put("id",id);
        map.put("hostIp",hostIp);
        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.DEL_HOST_INFOS + "?id=" + id, BaseResponse.class);
        return response;
    }

    @RequestMapping("/getHostInfoById")
    @ResponseBody
    public BaseResponse getHostInfoById(int id) throws Exception {
        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.GET_HOST_INFO_BY_ID + "?id=" + id, BaseResponse.class);
        return response;
    }


    @RequestMapping("/synHostInfo")
    @ResponseBody
    public BaseResponse synHostInfo() throws Exception {

        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.SYN_HOST_INFOS, BaseResponse.class);
        return response;
    }


    @RequestMapping("/toSetHostTags")
    public ModelAndView toSetHostTags(String hostIds) {
        ModelAndView mv = new ModelAndView("host/setHostTags");
        mv.addObject("tagTypeMapJson", JSON.toJSONString(TagTypeEnum.getAllTypeMap()));
        return mv;
    }

    /**
     * 备注--参数传递乱码,所以定义了一个SetHostTagsReq类
     *
     * @param req
     * @return
     */
    @RequestMapping("/saveHostTags")
    @ResponseBody
    public BaseResponse saveHostTags(SetHostTagsReq req) {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.SAVE_HOST_TAGS, req, BaseResponse.class);
        return response;
    }

}