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

import com.jay.monitor.data.core.enums.MsgTypeEnums;
import com.jay.monitor.data.core.model.response.ListResponseModel;
import com.jay.monitor.data.core.model.response.PageResponseModel;
import com.jay.monitor.data.core.utils.ResponseUtils;
import com.jay.monitor.data.server.models.entity.JayMonitorMq;
import com.jay.monitor.data.server.models.request.trace.MQRequest;
import com.jay.monitor.data.server.models.request.trace.SQLRequest;
import com.jay.monitor.data.server.models.request.trace.URLRequest;
import com.jay.monitor.data.server.models.response.trace.SQLTraceResponse;
import com.jay.monitor.data.server.models.response.trace.URLTraceResponse;
import com.jay.monitor.data.server.store.ConfigStoreManager;
import com.jay.monitor.data.server.store.TranceJsonManager;
import com.jay.monitor.data.server.utils.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

//import com.jay.monitor.data.server.services.impl.DefaultBackstageRouteService;

/**
 * 后台路由管理
 *
 * @author liukx
 * @Date 2019-02-28 15:16
 */
@Controller
@RequestMapping(value = "/trace")
public class TraceConsoleController {

    final static private String page = "/trace/";

    @Autowired
    private ConfigStoreManager configStoreManager;

    @Autowired
    private TranceJsonManager tranceJsonManager;

    //////////////////////////////////////// route //////////////////////////////////////////////

    @RequestMapping(value = "/mqList.html", method = RequestMethod.GET)
    public String list(Model model) throws Exception {
        model.addAttribute("catUrl", configStoreManager.getLogMonitorUrl());
        return page + "mqList";
    }

    @RequestMapping(value = "/urlList.html", method = RequestMethod.GET)
    public String urlList(Model model) throws Exception {
        model.addAttribute("catUrl", configStoreManager.getLogMonitorUrl());
        return page + "urlList";
    }

    @RequestMapping(value = "/sqlList.html", method = RequestMethod.GET)
    public String sqlList(Model model) throws Exception {
        model.addAttribute("catUrl", configStoreManager.getLogMonitorUrl());
        return page + "sqlList";
    }

    ///////////////////////////////////////////// API ////////////////////////////////////////////

    @RequestMapping(value = "/mqNodeId", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ListResponseModel<JayMonitorMq> findMQList(@RequestBody MQRequest request) throws Exception {
        String date = DataUtils.parseDayString(request.getDate());
        // 将参数对应的全部匹配赋值
        PageResponseModel pageResponseModel = tranceJsonManager
            .searchDataByList(MsgTypeEnums.MQ, date, request.getPageNo(), request.getPageSize(), request);
        return ResponseUtils.trueList(pageResponseModel.getPageModel().getResultSet());
    }

    @RequestMapping(value = "/mqList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public PageResponseModel mqNodeId(@RequestBody MQRequest request) throws Exception {
        String date = DataUtils.parseDayString(request.getDate());
        // 将参数对应的全部匹配赋值
        PageResponseModel pageResponseModel = tranceJsonManager
            .searchDataByList(MsgTypeEnums.MQ, date, request.getPageNo(), request.getPageSize(), request);
        return pageResponseModel;
    }

    @RequestMapping(value = "/urlList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public PageResponseModel<URLTraceResponse> findUrlList(@RequestBody URLRequest request) throws Exception {
        String date = DataUtils.parseDayString(request.getDate());
        // 将参数对应的全部匹配赋值
        PageResponseModel pageResponseModel = tranceJsonManager
            .searchDataByList(MsgTypeEnums.URL, date, request.getPageNo(), request.getPageSize(), request);
        return pageResponseModel;
    }

    @RequestMapping(value = "/sqlList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public PageResponseModel<SQLTraceResponse> findSqlList(@RequestBody SQLRequest request) throws Exception {
        PageResponseModel pageResponseModel = getDataPageList(MsgTypeEnums.SQL, request);
        return pageResponseModel;
    }

    private PageResponseModel getDataPageList(MsgTypeEnums msgTypeEnums, SQLRequest request) throws Exception {
        String date = DataUtils.parseDayString(request.getDate());
        // 将参数对应的全部匹配赋值
        PageResponseModel pageResponseModel =
            tranceJsonManager.searchDataByList(msgTypeEnums, date, request.getPageNo(), request.getPageSize(), request);
        return pageResponseModel;
    }
}
