package com.hotent.uc.controller;


import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.google.gson.JsonArray;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.StringUtil;
import com.hotent.uc.manager.ExtOrgLogManager;
import com.hotent.uc.manager.ZabbixApi;
import com.hotent.uc.manager.impl.DefaultZabbixApiImpl;
import com.hotent.uc.model.Host;
import com.hotent.uc.model.OrgExternalSynLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description  zabbix 监控处理类
 * @author  yangzibin
 * @date    2024/9/4 14:06
 * @return
 */
@RestController
@RequestMapping("/uc/zabbix/")
@Api(tags="同步组织机构日志记录")
@ApiGroup(group= {ApiGroupConsts.GROUP_UC})
public class ZabbixController extends BaseController<ExtOrgLogManager, OrgExternalSynLog> {
	@Value("${zabbix.config.url}")
	private String url;
	@Value("${zabbix.config.username}")
	private String user;
	@Value("${zabbix.config.password}")
	private String password;
	/**
	 * @description 获取数据列表
	 * @author  yangzibin
	 * @date    2024/9/4 14:18
	 * @param
	 * @return  com.hotent.base.model.CommonResult<java.lang.String>
	 */
	@RequestMapping(value = "getZabbixList", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取数据列表", httpMethod = "GET", notes = "获取数据列表")
	public CommonResult<Object> getZabbixList()
			throws Exception {
		ZabbixApi zabbixApi = new DefaultZabbixApiImpl(url);
		zabbixApi.init();
		boolean login = zabbixApi.login(user, password);
		if (!login) {
			throw new RuntimeException("zabbix login failed!");
		}
		List<Host> hosts = zabbixApi.hostList();
		hosts=hosts.stream().filter(item->item.getStatus().equals("0")).collect(Collectors.toList());
		return new CommonResult<Object>(true,"获取主机列表",hosts);
	}


	/**
	 * @description
	 * @author  yangzibin
	 * @date    2024/9/24 16:51
	 * @param	params
	 * @return  com.hotent.base.model.CommonResult<java.lang.Object>
	 */
	@RequestMapping(value = "createZabbixWeb", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "创建监控", httpMethod = "POST", notes = "创建监控")
	public CommonResult<Object> createZabbixWeb( @RequestBody String params
	)
			throws Exception {
		JSONObject jsonObject =new JSONObject(params);
		String boData= (String) jsonObject.get("boData");

		JSONObject jsonObjectBoData =new JSONObject(boData);
		JSONObject  qzdef_zabbixjk= (JSONObject) jsonObjectBoData.get("qzdef_zabbixjk");

		String  hostId= (String) qzdef_zabbixjk.get("hostId");
		String hostIp=(String) qzdef_zabbixjk.get("hostIp");
		String hostName=(String) qzdef_zabbixjk.get("hostName");
		String webName=(String) qzdef_zabbixjk.get("webName");

		cn.hutool.json.JSONArray sub_qzsub_jkxz=qzdef_zabbixjk.getJSONArray("sub_qzsub_jkxz");

		ZabbixApi zabbixApi = new DefaultZabbixApiImpl(url);
		zabbixApi.init();
		boolean login = zabbixApi.login(user, password);
		if (!login) {
			throw new RuntimeException("zabbix login failed!");
		}
		String httpId = zabbixApi.createZabbixWeb(webName,hostName,hostId,hostIp,sub_qzsub_jkxz);
		return new CommonResult<Object>(true,"创建web监控成功","");
	}

	/**
	 * @description 获取数据列表
	 * @author  yangzibin
	 * @date    2024/9/4 14:18
	 * @param
	 * @return  com.hotent.base.model.CommonResult<java.lang.String>
	 */
	@RequestMapping(value = "getZabbixAlertList", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取告警数据列表", httpMethod = "GET", notes = "获取告警数据列表")
	public CommonResult<Object> getZabbixAlertList(String hostId,String triggerName)
			throws Exception {
		ZabbixApi zabbixApi = new DefaultZabbixApiImpl(url);
		zabbixApi.init();
		boolean login = zabbixApi.login(user, password);
		if (!login) {
			throw new RuntimeException("zabbix login failed!");
		}
		JSONArray hosts = zabbixApi.getAlarmDataObtaining(hostId);

		if(StringUtil.isNotEmpty(triggerName)){
			JSONArray result=new JSONArray();
			for(int i=0;i<hosts.size();i++){
				String subject=hosts.getJSONObject(i).getString("subject");
				if(subject.contains(triggerName)){
					result.set(result.size(),hosts.getJSONObject(i));

				}
			}
			return new CommonResult<Object>(true,"获取告警列表",result);
		}else{
			return new CommonResult<Object>(true,"获取告警列表",hosts);
		}

	}
	/**
	 * @description 获取问题列表
	 * @author  yangzibin
	 * @date    2024/9/4 14:18
	 * @param
	 * @return  com.hotent.base.model.CommonResult<java.lang.String>
	 */
	@RequestMapping(value = "getProblemList", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取问题列表", httpMethod = "GET", notes = "获取问题列表")
	public CommonResult<Object> getProblemList(String hostId)
			throws Exception {
		ZabbixApi zabbixApi = new DefaultZabbixApiImpl(url);
		zabbixApi.init();
		boolean login = zabbixApi.login(user, password);
		if (!login) {
			throw new RuntimeException("zabbix login failed!");
		}
		JSONArray hosts = zabbixApi.getProblemList(hostId);
		return new CommonResult<Object>(true,"获取问题列表",hosts);
	}



	/**
	 * @description 获取触发器
	 * @author  yangzibin
	 * @date    2024/9/4 14:18
	 * @param
	 * @return  com.hotent.base.model.CommonResult<java.lang.String>
	 */
	@RequestMapping(value = "getZabbixTriggerList", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取触发器", httpMethod = "GET", notes = "获取触发器")
	public CommonResult<Object> getZabbixTriggerList(String hostId)
			throws Exception {
		ZabbixApi zabbixApi = new DefaultZabbixApiImpl(url);
		zabbixApi.init();
		boolean login = zabbixApi.login(user, password);
		if (!login) {
			throw new RuntimeException("zabbix login failed!");
		}
		JSONArray hosts = zabbixApi.getTriggerList(hostId);
		return new CommonResult<Object>(true,"获取告警列表",hosts);
	}

	/**
	 * @description 处理告警数据
	 * @author  yangzibin
	 * @date    2024/9/4 14:18
	 * @param
	 * @return  com.hotent.base.model.CommonResult<java.lang.String>
	 */
	@RequestMapping(value = "productZabbixAlertList", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "处理告警数据", httpMethod = "GET", notes = "处理告警数据")
	public CommonResult<Object> productZabbixAlertList()
			throws Exception {
		ZabbixApi zabbixApi = new DefaultZabbixApiImpl(url);
		zabbixApi.init();
		boolean login = zabbixApi.login(user, password);
		if (!login) {
			throw new RuntimeException("zabbix login failed!");
		}
		 zabbixApi.productZabbixAlertData();
		return new CommonResult<Object>(true,"处理成功");

	}
}
