package com.bocloud.ims.booter.controller.stats;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.interfaces.stats.CmpStatsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 首页概览数据
 *
 * @author wangyu
 * @Date 2020-7-24 14:18:19
 */
@RestController
@RequestMapping("/v1/portal")
@Api(value = "首页概览数据", tags = "首页概览数据")
public class PortalStatsController {

    @Autowired
    private CmpStatsService portalStatsService;

    /**
     * 获取首页概览数据
     *
     * @param projectId
     * @param code
     * @param dataType
     * @param vendor
     * @param vendorId
     * @param dcId
     * @param clusterId
     * @param name
     * @param paging
     * @param statsType
     * @param vendorType
     * @param vdcId
     * @param city
     * @param start
     * @param end
     * @param orderBy
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "获取平台信息")
    @Log(module = BoCloudService.IMS, target = "平台概览", operation = "获取平台信息")
    @GetMapping("/platform/stats")
    public BocloudResult statsCmp(@RequestParam(value = "projectId", required = false) Long projectId,
        @RequestParam(value = "code", required = false) String code,
        @RequestParam(value = "dataType", required = false) String dataType,
        @RequestParam(value = "vendor", required = false) String vendor,
        @RequestParam(value = "vendorId", required = false) Long vendorId,
        @RequestParam(value = "dcId", required = false) Long dcId,
        @RequestParam(value = "clusterId", required = false) Long clusterId,
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "paging", required = false) String paging,
        @RequestParam(value = "statsType", required = false) String statsType,
        @RequestParam(value = "vendorType", required = false) String vendorType,
        @RequestParam(value = "vdcId", required = false) Long vdcId,
        @RequestParam(value = "city", required = false) String city,
        @RequestParam(value = "start", required = false) String start,
        @RequestParam(value = "end", required = false) String end,
        @RequestParam(value = "orderBy", required = false) String orderBy,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        JSONObject params = new JSONObject();
        if (null != projectId) {
            params.put("projectId", projectId);
        }
        if (null != code) {
            params.put("code", code);
        }
        if (null != dataType) {
            params.put("dataType", dataType);
        }
        if (null != vendor) {
            params.put("vendor", vendor);
        }
        if (null != vendorId) {
            params.put("vendorId", vendorId);
        }
        if (null != dcId) {
            params.put("dcId", dcId);
        }
        if (null != clusterId) {
            params.put("clusterId", clusterId);
        }
        if (null != name) {
            params.put("name", name);
        }
        if (null != paging) {
            params.put("paging", JSONObject.parse(paging));
        }
        if (null != statsType) {
            params.put("statsType", statsType);
        }
        if (null != vendorType) {
            params.put("vendorType", vendorType);
        }
        if (null != vdcId) {
            params.put("vdcId", vdcId);
        }
        if (null != city) {
            params.put("city", city);
        }
        if (null != start) {
            params.put("start", start);
        }
        if (null != end) {
            params.put("end", end);
        }
        if (null != orderBy) {
            params.put("orderBy", orderBy);
        }
        return portalStatsService.stats(params.toJSONString(), context);
    }

    /**
     * 获取平台概览信息
     *
     * @param vendorId
     * @param type
     * @param perfType
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "获取平台概览信息")
    @Log(module = BoCloudService.IMS, target = "平台概览", operation = "获取平台概览信息")
    @GetMapping("/vendor/stats")
    public BocloudResult vendorStats(@RequestParam(value = "vendorId", required = false) Long vendorId,
        @RequestParam(value = "type", required = false) String type,
        @RequestParam(value = "perfType", required = false) String perfType) {
        JSONObject params = new JSONObject();
        if (null != vendorId) {
            params.put("vendorId", vendorId);
        }
        if (null != type) {
            params.put("type", type);
        }
        if (null != perfType) {
            params.put("perfType", perfType);
        }
        return portalStatsService.vendorStats(params.toJSONString());
    }

}
