package com.chinaweal.aiccs.aiccs.statistic.controller;

import com.alibaba.fastjson.JSON;
import com.chinaweal.aiccs.aiccs.statistic.entity.StatisticsResult;
import com.chinaweal.aiccs.aiccs.statistic.service.IStatisticsResultService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.enums.StatisticsModuleEnum;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 统计信息接口
 **/
@Api(tags = "统计接口")
@RestController
@RequestMapping("statistics")
public class StatisticsController extends BaseController {

    @Autowired
    private IStatisticsResultService statisticsResultService;

    /**
     * 通过模块数据获取最新的定时统计数据
     */
    @GetMapping("latest/{moduleTag}")
    @ApiOperation("通过模块数据获取最新的定时统计数据")
    public RestResult<?> getLatestStatisticsByModuleTag(@ApiParam("模块标签") @PathVariable String moduleTag) {
        StatisticsModuleEnum moduleEnum = StatisticsModuleEnum.getEnumByModuleTag(moduleTag);
        if (moduleEnum == null) return RestResult.error(ResultCode.PARAM_IS_INVALID);
        StatisticsResult result = statisticsResultService.getLatest(moduleTag);
        return result == null ? OK : RestResult.ok(JSON.parse(result.getDataJson()));
    }

}
