调整日志输出方式,更新数据库日志存储结构;首页查询统计数据接口实现
This commit is contained in:
@@ -154,6 +154,7 @@ func CreateChannel(c *fiber.Ctx) error {
|
||||
|
||||
// 创建通道
|
||||
result, err := s.Channel.CreateChannel(
|
||||
c,
|
||||
authContext.Payload.Id,
|
||||
req.ResourceId,
|
||||
req.Protocol,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"gorm.io/gen/field"
|
||||
"platform/pkg/u"
|
||||
"platform/web/auth"
|
||||
"platform/web/core"
|
||||
@@ -14,8 +15,6 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// region 查询套餐
|
||||
|
||||
type ListResourceShortReq struct {
|
||||
core.PageReq
|
||||
ResourceNo *string `json:"resource_no"`
|
||||
@@ -236,9 +235,175 @@ func AllActiveResource(c *fiber.Ctx) error {
|
||||
return c.JSON(resources)
|
||||
}
|
||||
|
||||
// endregion
|
||||
type StatisticPersonalResp struct {
|
||||
Short StatisticShort `json:"short"`
|
||||
Long StatisticLong `json:"long"`
|
||||
}
|
||||
|
||||
// region 创建套餐
|
||||
type StatisticShort struct {
|
||||
ResourceCount int
|
||||
ResourceQuotaSum int
|
||||
ResourceDailyFreeSum int
|
||||
}
|
||||
|
||||
type StatisticLong struct {
|
||||
ResourceCount int
|
||||
ResourceQuotaSum int
|
||||
ResourceDailyFreeSum int
|
||||
}
|
||||
|
||||
func StatisticResourceFree(c *fiber.Ctx) error {
|
||||
// 检查权限
|
||||
session, err := auth.NewProtect(c).Payload(auth.PayloadUser).Do()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 统计套餐剩余数量
|
||||
resources, err := q.Resource.
|
||||
Preload(
|
||||
q.Resource.Short,
|
||||
q.Resource.Long,
|
||||
).
|
||||
Where(
|
||||
q.Resource.UserID.Eq(session.Payload.Id),
|
||||
q.Resource.Active.Is(true),
|
||||
).
|
||||
Select(q.Resource.ID).
|
||||
Find()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var shortCount, shortQuotaSum, shortDailyFreeSum int
|
||||
var longCount, longQuotaSum, longDailyFreeSum int
|
||||
for _, resource := range resources {
|
||||
switch {
|
||||
|
||||
// 短效包量
|
||||
case resource2.Type(resource.Type) == resource2.TypeShort && resource2.Mode(resource.Short.Type) == resource2.ModeCount:
|
||||
if u.Z(resource.Short.Quota) > resource.Short.Used {
|
||||
shortCount++
|
||||
shortQuotaSum += int(u.Z(resource.Short.Quota) - resource.Short.Used)
|
||||
}
|
||||
|
||||
// 长效包量
|
||||
case resource2.Type(resource.Type) == resource2.TypeLong && resource2.Mode(resource.Long.Type) == resource2.ModeCount:
|
||||
if u.Z(resource.Long.Quota) > resource.Long.Used {
|
||||
longCount++
|
||||
longQuotaSum += int(u.Z(resource.Long.Quota) - resource.Long.Used)
|
||||
}
|
||||
|
||||
// 短效包时
|
||||
case resource2.Type(resource.Type) == resource2.TypeShort && resource2.Mode(resource.Short.Type) == resource2.ModeTime:
|
||||
if time.Time(*resource.Short.Expire).After(time.Now()) {
|
||||
if time.Time(*resource.Short.DailyLast) != u.Today() {
|
||||
shortCount++
|
||||
shortDailyFreeSum += int(resource.Short.DailyLimit)
|
||||
} else if resource.Short.DailyLimit > resource.Short.DailyUsed {
|
||||
shortCount++
|
||||
shortDailyFreeSum += int(resource.Short.DailyLimit - resource.Short.DailyUsed)
|
||||
}
|
||||
}
|
||||
|
||||
// 长效包时
|
||||
case resource2.Type(resource.Type) == resource2.TypeLong && resource2.Mode(resource.Long.Type) == resource2.ModeTime:
|
||||
if time.Time(*resource.Long.Expire).After(time.Now()) {
|
||||
if time.Time(*resource.Long.DailyLast) != u.Today() {
|
||||
longCount++
|
||||
longDailyFreeSum += int(resource.Long.DailyLimit)
|
||||
} else if resource.Long.DailyLimit > resource.Long.DailyUsed {
|
||||
longCount++
|
||||
longDailyFreeSum += int(resource.Long.DailyLimit - resource.Long.DailyUsed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c.JSON(StatisticPersonalResp{
|
||||
Short: StatisticShort{
|
||||
ResourceCount: shortCount,
|
||||
ResourceQuotaSum: shortQuotaSum,
|
||||
ResourceDailyFreeSum: shortDailyFreeSum,
|
||||
},
|
||||
Long: StatisticLong{
|
||||
ResourceCount: longCount,
|
||||
ResourceQuotaSum: longQuotaSum,
|
||||
ResourceDailyFreeSum: longDailyFreeSum,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type StatisticResourceUsageReq struct {
|
||||
ResourceNo *string `json:"resource_no"`
|
||||
TimeAfter *time.Time `json:"time_after"`
|
||||
TimeBefore *time.Time `json:"time_before"`
|
||||
}
|
||||
|
||||
type StatisticResourceUsageResp []struct {
|
||||
Date time.Time `json:"date"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
func StatisticResourceUsage(c *fiber.Ctx) error {
|
||||
// 检查权限
|
||||
session, err := auth.NewProtect(c).Payload(auth.PayloadUser).Do()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 解析请求参数
|
||||
var req = new(StatisticResourceUsageReq)
|
||||
if err := g.Validator.Validate(c, req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 统计套餐提取数量
|
||||
do := q.LogsUserUsage.Where()
|
||||
if req.ResourceNo != nil && *req.ResourceNo != "" {
|
||||
var resourceID int32
|
||||
err := q.Resource.
|
||||
Where(
|
||||
q.Resource.UserID.Eq(session.Payload.Id),
|
||||
q.Resource.ResourceNo.Eq(*req.ResourceNo),
|
||||
).
|
||||
Select(q.Resource.ID).
|
||||
Scan(&resourceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
do.Where(q.LogsUserUsage.ResourceID.Eq(resourceID))
|
||||
}
|
||||
if req.TimeAfter != nil {
|
||||
do.Where(q.LogsUserUsage.Time.Gte(orm.LocalDateTime(*req.TimeAfter)))
|
||||
}
|
||||
if req.TimeBefore != nil {
|
||||
do.Where(q.LogsUserUsage.Time.Lte(orm.LocalDateTime(*req.TimeBefore)))
|
||||
}
|
||||
|
||||
var data = new(StatisticResourceUsageResp)
|
||||
err = q.LogsUserUsage.
|
||||
Select(
|
||||
q.LogsUserUsage.Count_.Sum().As("count"),
|
||||
field.NewField("", "date_trunc('day', time)").As("date"),
|
||||
).
|
||||
Where(
|
||||
q.LogsUserUsage.UserID.Eq(session.Payload.Id),
|
||||
do,
|
||||
).
|
||||
Group(
|
||||
field.NewField("", "date_trunc('day', time)"),
|
||||
).
|
||||
Order(
|
||||
field.NewField("", "date").Desc(),
|
||||
).
|
||||
Scan(&data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(data)
|
||||
}
|
||||
|
||||
type CreateResourceReq struct {
|
||||
s.CreateResourceData
|
||||
@@ -330,8 +495,6 @@ func CompleteCreateResource(c *fiber.Ctx) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
func ResourcePrice(c *fiber.Ctx) error {
|
||||
// 检查权限
|
||||
_, err := auth.NewProtect(c).Payload(auth.PayloadInternalServer).Do()
|
||||
|
||||
Reference in New Issue
Block a user