package com.pqs9900.controller.DataInfo;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import com.pqs9900.pojo.DataInfo.AlarmInfo;
import com.pqs9900.service.DataInfo.AlarmInfoService;

/** @Author denghuajun
 * @Description //TODO 告警信息
 * @Date  2019/3/15 14:25
 **/
@Controller
@RequestMapping(value ="/alarm")
public class AlarmInfoController {
	// 日志记录
    private static final Logger logger = LoggerFactory.getLogger(AlarmInfoController.class);

	@Autowired
	private AlarmInfoService alarmInfoService;
    @Autowired
    UserLogDetailService userLog;

	@RequestMapping(value="alarmTip",method=RequestMethod.GET)
	@ResponseBody
	public void alarmTip(HttpServletRequest request,String alarmIndex){
		alarmInfoService.alarmTip(alarmIndex);
	}

	/**
	 * 发送严重告警事件给指定用户
	 * @author cdf
	 * @date 2021/8/17
	 */

    @RequestMapping(value="alarmForSeverity",method=RequestMethod.GET)
    @ResponseBody
    public void alarmForSeverity(@RequestParam("alarmIndex") String alarmIndex){
        alarmInfoService.alarmForSeverity(alarmIndex);
    }

	@PostMapping( "getAlarmInfo")
    @ResponseBody
    public HttpResult getAlarmInfo(String startTime, String endTime,String state,String type,Integer alarmLevel,Integer lineGrade,HttpServletRequest request) {
        HttpResult result= new HttpResult();
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "getAlarmInfo");
        if(!StringUtils.isEmpty(datePojo.getMsg())){
            result=PubUtils.assignmentResult(null,500,datePojo.getMsg());
            return result;
        }
        List<AlarmInfo> alarmInfo;
        try {
        	alarmInfo = alarmInfoService.getAlarmInfo(datePojo.getStartTime(), datePojo.getEndTime(),state,type,alarmLevel,lineGrade);
            if (null == alarmInfo) {
                result.setCode(500);
                result.setMessage("告警列表数据为空");
                userLog.getLog("查询告警列表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            } else {
                result.setCode(200);
                result.setBody(alarmInfo);
                userLog.getLog("查询告警列表", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            }
        } catch (Exception e) {
            result.setCode(500);
            result.setMessage("获取告警列表失败");
            logger.error("后台获取数据出错，方法名为：getAlarmInfo，异常为：" + e);
            userLog.getLog("查询告警列表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        }
        return result;
	}


    @PostMapping( "getAlarmInfoDetail")
    @ResponseBody
    public HttpResult getAlarmInfoDetail(String state,String type,Integer typeIndex,Integer devLineType,String processTime, HttpServletRequest request) {
        HttpResult result= new HttpResult();

        List<AlarmInfo> alarmInfo;
        try {
            alarmInfo = alarmInfoService.getAlarmInfoDetail(state,type,typeIndex,devLineType,processTime);
            if (null == alarmInfo) {
                result.setCode(500);
                result.setMessage("告警列表数据为空");
                userLog.getLog("查询告警列表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            } else {
                result.setCode(200);
                result.setBody(alarmInfo);
                userLog.getLog("查询告警列表", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            }
        } catch (Exception e) {
            result.setCode(500);
            result.setMessage("获取告警列表失败");
            logger.error("后台获取数据出错，方法名为：getAlarmInfoDetail，异常为：" + e);
            userLog.getLog("查询告警列表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        }
        return result;
    }



    @PostMapping( "unAlarmInfo")
    @ResponseBody
    public HttpResult unAlarmInfo(HttpServletRequest request) {
        HttpResult result= new HttpResult();
        List<AlarmInfo> alarmInfo;
        try {
        	alarmInfo = alarmInfoService.unAlarmInfo();
            if (null == alarmInfo) {
                result.setCode(500);
                result.setMessage("告警列表数据为空");
                userLog.getLog("查询告警列表", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            } else {
                result.setCode(200);
                result.setBody(alarmInfo);
                userLog.getLog("查询告警列表", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            }
        } catch (Exception e) {
            result.setCode(500);
            result.setMessage("获取告警列表失败");
            logger.error("后台获取数据出错，方法名为：unAlarmInfo，异常为：" + e.toString());
            userLog.getLog("查询告警列表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        }
        return result;
	}

	@PostMapping( "updateAlarmInfo")
    @ResponseBody
    public HttpResult updateAlarmInfo(String type,Integer typeIndex,Integer devLineType,HttpServletRequest request) {
        HttpResult result= new HttpResult();

        try {
        	alarmInfoService.updateAlarmInfo(type,typeIndex,devLineType);

            result.setCode(200);
            result.setMessage("处理告警事件成功");
            userLog.getLog("处理告警事件", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
		} catch (Exception e) {
			// TODO: handle exception
            result.setCode(500);
            result.setMessage("处理告警事件失败");
            logger.error("处理告警事件失败，方法名为：updateAlarmInfo，异常为：" + e);
            userLog.getLog("处理告警事件", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
		}
        return result;
	}
}
