package log

import (
	"log"

	"bsm/full/module/base/logs/internal/impl"
	"bsm/full/module/base/logs/internal/models"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/infra"
	"github.com/gin-gonic/gin"
)

func Total(c *gin.Context) {
	var request map[string]any
	err := c.BindJSON(&request)
	if err != nil {
		log.Printf("解析json异常: %v", err)
		infra.Response.Error(c, errcode.ErrJsonUnmarshal)
		return
	}

	tx := impl.DBService.Model(&models.LogData{})
	if request["service"] != "" {
		tx.Where("service = ?", request["service"])
	}

	var result map[string]any
	if err := tx.Select("level", "count(level)").Group("level").First(&result).Error; err != nil {
		infra.Response.Error(c, errcode.ErrDB)
		return
	}

	infra.Response.Success(c, result)
}
