package com.jay.monitor.data.server.controllers;

import com.alibaba.fastjson.JSON;
import com.cat.file.message.MessageManagerProcess;
import com.jay.monitor.data.core.enums.MsgTypeEnums;
import com.jay.monitor.data.core.model.response.ResponseCommonModel;
import com.jay.monitor.data.core.utils.ResponseUtils;
import com.jay.monitor.data.server.enums.CommonEnums;
import com.jay.monitor.data.server.report.DayAlertProcess;
import com.jay.monitor.data.server.store.StoreDataManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ： liukx
 * @describe ：首页
 * @time ： 2019/3/1 - 16:31
 */
@Controller
@RequestMapping(value = "/api")
public class ApiController {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");

    private String jsonPage = "/base/json";


    @Autowired
    private StoreDataManager storeDataManager;

    @Autowired
    private DayAlertProcess alertProcess;

    @Autowired
    protected MessageManagerProcess messageManagerProcess;

    @RequestMapping(value = "/log/mq", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String searchMqIdLog(@RequestParam(value = "date", required = false) String date, @RequestParam("id") String id) throws Exception {
        if (StringUtils.isEmpty(date)) {
            date = sdf.format(new Date());
        }
        String body = storeDataManager.selectContentById(MsgTypeEnums.MQ, date, id);
        return body == null ? JSON.toJSONString(ResponseUtils.falses(CommonEnums.COMMON_DATA_NOT_FOUND)) : body;
    }

    @RequestMapping(value = "/log/mq/error", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String searchMqErrorLog(@RequestParam(value = "date", required = false) String date, @RequestParam("id") String id) throws Exception {
        if (StringUtils.isEmpty(date)) {
            date = sdf.format(new Date());
        }
        String body = storeDataManager.selectContentById(MsgTypeEnums.MQ, date, id);
        return body == null ? JSON.toJSONString(ResponseUtils.falses(CommonEnums.COMMON_DATA_NOT_FOUND)) : body;
    }

    @RequestMapping(value = "/log/url/error", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String searchUrlErrorLog(@RequestParam(value = "date", required = false) String date, @RequestParam("id") String id) throws Exception {
        if (StringUtils.isEmpty(date)) {
            date = sdf.format(new Date());
        }
        String body = storeDataManager.selectContentById(MsgTypeEnums.URL, date, id);
        return body == null ? JSON.toJSONString(ResponseUtils.falses(CommonEnums.COMMON_DATA_NOT_FOUND)) : body;
    }

    @RequestMapping(value = "/log/url", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String searchUrlIdLog(@RequestParam(value = "date", required = false) String date, @RequestParam("id") String id) throws Exception {
        if (StringUtils.isEmpty(date)) {
            date = sdf.format(new Date());
        }
        String body = storeDataManager.selectContentById(MsgTypeEnums.URL, date, id);
        return body == null ? JSON.toJSONString(ResponseUtils.falses(CommonEnums.COMMON_DATA_NOT_FOUND)) : body;
    }

    @RequestMapping(value = "/log/console", method = RequestMethod.GET)
    public String logConsole(@RequestParam(value = "logId") String logId, Model model) throws Exception {
        String message = messageManagerProcess.getMessage(logId);

        if (StringUtils.isEmpty(message)) {
            message = "找不到对应的消息";
        }

        model.addAttribute("jsonData", message);
        return jsonPage;
    }

    @RequestMapping(value = "/alert/send", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ResponseCommonModel alertSend(@RequestParam(value = "date", required = false) String date) throws Exception {
        if (StringUtils.isEmpty(date)) {
            date = sdf.format(new Date());
        }
        alertProcess.alert(date);
        return ResponseUtils.trues();
    }

}
