付延余
2022-12-16 f0f8ee8c4a945adbc742d9bab69382b28ad311fb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.wgcloud.controller;
 
import com.github.pagehelper.PageInfo;
import com.wgcloud.entity.HostGroup;
import com.wgcloud.service.HostGroupService;
import com.wgcloud.service.LogInfoService;
import com.wgcloud.util.PageUtil;
import com.wgcloud.util.staticvar.StaticKeys;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @version v3.3
 * @ClassName:HostGroupController.java
 * @author: http://www.wgstart.com
 * @date: 2021年1月16日
 * @Description: 监控资源分组
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Controller
@RequestMapping("/hostGroup")
public class HostGroupController {
 
 
    private static final Logger logger = LoggerFactory.getLogger(HostGroupController.class);
 
 
    @Resource
    private HostGroupService hostGroupService;
    @Resource
    private LogInfoService logInfoService;
 
 
    /**
     * 根据条件查询HostGroup信息列表
     *
     * @param HostGroup
     * @param model
     * @return
     */
    @RequestMapping(value = "list")
    public String hostGroupList(HostGroup HostGroup, Model model, HttpServletRequest request) {
        String errorMsg = "查询分组信息错误";
 
 
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            StringBuffer url = new StringBuffer();
            if (!StringUtils.isEmpty(HostGroup.getGroupType())) {
                params.put("groupType", HostGroup.getGroupType());
                url.append("&groupType=").append(HostGroup.getGroupType());
            }
            PageInfo pageInfo = hostGroupService.selectByParams(params, Integer.valueOf(HostGroup.getPage()), Integer.valueOf(HostGroup.getPageSize()));
            PageUtil.initPageNumber(pageInfo, model);
            model.addAttribute("page", pageInfo);
            model.addAttribute("pageUrl", "/hostGroup/list?1=1" + url.toString());
            model.addAttribute("hostGroup", HostGroup);
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "hostGroup/list";
    }
 
 
    /**
     * 添加
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "edit")
    public String edit(Model model, HttpServletRequest request) {
        String errorMsg = "添加分组信息错误";
        String id = request.getParameter("id");
        HostGroup hostGroup = new HostGroup();
        try {
            if (StringUtils.isEmpty(id)) {
                model.addAttribute("hostGroup", hostGroup);
 
                return "hostGroup/add";
            }
            hostGroup = hostGroupService.selectById(id);
            model.addAttribute("hostGroup", hostGroup);
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "hostGroup/add";
    }
 
    /**
     * 保存机组信息
     *
     * @param HostGroup
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "save")
    public String saveHostGroup(HostGroup HostGroup, Model model, HttpServletRequest request) {
        try {
            if (StringUtils.isEmpty(HostGroup.getId())) {
                hostGroupService.save(HostGroup);
                hostGroupService.saveLog(request, StaticKeys.LOG_ADD, HostGroup);
            } else {
                hostGroupService.updateById(HostGroup);
                hostGroupService.saveLog(request, StaticKeys.LOG_UPDATE, HostGroup);
            }
 
        } catch (Exception e) {
            logger.error("保存分组错误:", e);
            logInfoService.save("保存分组错误", e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "redirect:/hostGroup/list";
    }
 
 
    /**
     * 删除主机分组
     *
     * @param model
     * @param request
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "del")
    public String delete(Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        String errorMsg = "删除分组错误";
        try {
            if (!StringUtils.isEmpty(request.getParameter("id"))) {
                String[] ids = request.getParameter("id").split(",");
                for (String id : ids) {
                    HostGroup hostGroup = hostGroupService.selectById(id);
                    hostGroupService.deleteById(request.getParameter("id").split(","));
                    hostGroupService.saveLog(request, StaticKeys.LOG_DEL, hostGroup);
                }
            }
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
 
        return "redirect:/hostGroup/list";
    }
 
}