query_set_statistics.go 3.0 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/xtime"
	"github.com/zeromicro/go-zero/core/fx"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/querySet/query"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
)

func (querySetService *QuerySetService) StatisticsQuery(ctx *domain.Context, query *query.StatisticsQuery) (interface{}, error) {
	if err := query.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
	var (
		total                  int64
		totalToday             int64
		totalYesterday         int64
		totalEnable            int64
		platformTotal          int64
		platformTotalYesterday int64
	)
	fx.Parallel(func() {
		total, _, err = querySetRepository.Find(map[string]interface{}{
			"context": ctx,
			"limit":   1,
			"type":    domain.SchemaTable.ToString(),
		})
		if err != nil {
			return
		}
		totalEnable, _, err = querySetRepository.Find(map[string]interface{}{
			"context": ctx,
			"limit":   1,
			"type":    domain.SchemaTable.ToString(),
			"status":  domain.StatusOn,
		})
		if err != nil {
			return
		}
		totalToday, _, err = querySetRepository.Find(map[string]interface{}{
			"context":   ctx,
			"limit":     1,
			"type":      domain.SchemaTable.ToString(),
			"beginTime": xtime.BeginningOfDay().Local(),
			"endTime":   xtime.BeginningOfDay().AddDate(0, 0, 1).Local(),
		})
		if err != nil {
			return
		}
		totalYesterday, _, err = querySetRepository.Find(map[string]interface{}{
			"context":   ctx,
			"limit":     1,
			"type":      domain.SchemaTable.ToString(),
			"beginTime": xtime.BeginningOfDay().AddDate(0, 0, -1).Local(),
			"endTime":   xtime.BeginningOfDay().Local(),
		})
		if err != nil {
			return
		}
	}, func() {
		platformTotal, _, err = querySetRepository.Find(map[string]interface{}{
			"limit": 1,
			"type":  domain.SchemaTable.ToString(),
		})
		if err != nil {
			return
		}
		platformTotalYesterday, _, err = querySetRepository.Find(map[string]interface{}{
			"limit":     1,
			"type":      domain.SchemaTable.ToString(),
			"beginTime": xtime.BeginningOfDay().AddDate(0, 0, -1).Local(),
			"endTime":   xtime.BeginningOfDay().Local(),
		})
		if err != nil {
			return
		}
	})

	if err != nil {
		return nil, factory.FastError(err)
	}
	return map[string]interface{}{
		"schemaStatistics": map[string]interface{}{
			"total":          total,
			"totalToday":     totalToday,
			"totalYesterday": totalYesterday,
			"totalEnable":    totalEnable,
		},
		"platformSchemaStatistics": map[string]interface{}{
			"total":          platformTotal,
			"totalYesterday": platformTotalYesterday,
		},
	}, nil
}