package com.zhiqiyun.open.mvc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dliyun.oap.framework.service.RouterService;
import com.dliyun.oap.framework.service.ServiceMethodHandler;
import com.zhiqiyun.open.annotation.Permission;
import com.zhiqiyun.open.core.models.app.ApiRequestLog;
import com.zhiqiyun.open.core.models.app.AppKeyInfo;
import com.zhiqiyun.open.core.service.ApiRequestLogService;
import com.zhiqiyun.open.core.service.AppKeyInfoService;
import com.zhiqiyun.open.core.service.PlaceBaseInfoService;
import com.zhiqiyun.open.core.service.UploadFileInfoService;
import com.zhiqiyun.open.mvc.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class IndexController {

    @Autowired
    private RouterService routerService;

    @Autowired
    private AppKeyInfoService appKeyInfoService;

    @Autowired
    private ApiRequestLogService apiRequestLogService;

    @Autowired
    private PlaceBaseInfoService placeBaseInfoService;

    @Autowired
    private UploadFileInfoService uploadFileInfoService;

    @GetMapping("/")
    public void index(HttpServletResponse response) throws IOException {
        response.sendRedirect("/index.html");
    }

    @ResponseBody
    @Permission(tags = "控制面板", writeLog = false)
    @PostMapping("/dashboard")
    public Result dashboard() throws IOException {
        Map<String, ServiceMethodHandler> serviceMethodHandlerMap = this.routerService.getOapContext().getAllServiceMethodHandlers();
        int serviceMethodHandlerCount = serviceMethodHandlerMap.size();

        long appKeyCount = this.appKeyInfoService.count();
        long placeCount = this.placeBaseInfoService.count();
        long fileCount = this.uploadFileInfoService.count();


        QueryWrapper<ApiRequestLog> wrapper = new QueryWrapper<>();
        wrapper.select("app_key appKey", "count(id) value");
        wrapper.groupBy("app_key");
        wrapper.orderByDesc("value");
        wrapper.last("limit 8");
        List<Map<String, Object>> requestLogPieData = this.apiRequestLogService.listMaps(wrapper);
        for (Map<String, Object> dataMap : requestLogPieData) {
            AppKeyInfo appKeyInfo = this.appKeyInfoService.findByAppKey(dataMap.get("appKey").toString());
            if (appKeyInfo == null) {
                dataMap.put("name", "未知应用");
            } else {
                dataMap.put("name", appKeyInfo.getName());
            }
        }

        wrapper= new QueryWrapper<>();
        wrapper.select("DATE_FORMAT(service_begin_time, '%Y-%m-%d') d", "count(id) value");
        wrapper.groupBy("d");
        wrapper.orderByAsc("d");
        wrapper.last("limit 15");
        List<Map<String, Object>> requestLogLineData = this.apiRequestLogService.listMaps(wrapper);

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("serviceMethodHandlerCount", serviceMethodHandlerCount);
        dataMap.put("appKeyCount", appKeyCount);
        dataMap.put("placeCount", placeCount);
        dataMap.put("fileCount", fileCount);
        dataMap.put("requestLogPieData", requestLogPieData);
        dataMap.put("requestLogLineData", requestLogLineData);

        return Result.instance(Result.Code.SUCCESS).setData(dataMap);
    }
}
