feat:多项功能优化
This commit is contained in:
@@ -3,6 +3,7 @@ package logic
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/alert"
|
||||
@@ -48,13 +49,48 @@ func (s *Service) ListAlertRules(ctx context.Context) ([]*ent.AlertRule, error)
|
||||
Order(ent.Desc(alertrule.FieldCreatedAt), ent.Desc(alertrule.FieldID)).All(ctx)
|
||||
}
|
||||
|
||||
// ListAlerts 查询预警消息(status 空=全部)
|
||||
func (s *Service) ListAlerts(ctx context.Context, status string) ([]*ent.Alert, error) {
|
||||
q := s.ctx.EntClient.Alert.Query()
|
||||
if status != "" {
|
||||
q = q.Where(alert.Status(status))
|
||||
// AlertQuery 预警查询条件:status/type 空=不限;from/to 为 YYYY-MM-DD(空=不限);page<=0 表示不分页。
|
||||
type AlertQuery struct {
|
||||
Status string
|
||||
Type string
|
||||
From string
|
||||
To string
|
||||
Page int
|
||||
PageSize int
|
||||
}
|
||||
|
||||
// ListAlerts 查询预警消息(支持状态/类型/时间范围筛选 + 分页),返回当前页数据与命中总数
|
||||
func (s *Service) ListAlerts(ctx context.Context, q AlertQuery) ([]*ent.Alert, int, error) {
|
||||
query := s.ctx.EntClient.Alert.Query()
|
||||
if q.Status != "" {
|
||||
query = query.Where(alert.Status(q.Status))
|
||||
}
|
||||
return q.Order(ent.Desc(alert.FieldCreatedAt), ent.Desc(alert.FieldID)).All(ctx)
|
||||
if q.Type != "" {
|
||||
query = query.Where(alert.Type(q.Type))
|
||||
}
|
||||
if q.From != "" {
|
||||
if f, err := time.Parse("2006-01-02", q.From); err == nil {
|
||||
query = query.Where(alert.CreatedAtGTE(f))
|
||||
}
|
||||
}
|
||||
if q.To != "" {
|
||||
if t, err := time.Parse("2006-01-02", q.To); err == nil {
|
||||
query = query.Where(alert.CreatedAtLT(t.Add(24 * time.Hour)))
|
||||
}
|
||||
}
|
||||
total, err := query.Count(ctx)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if q.Page > 0 {
|
||||
size := q.PageSize
|
||||
if size <= 0 {
|
||||
size = 20
|
||||
}
|
||||
query = query.Offset((q.Page - 1) * size).Limit(size)
|
||||
}
|
||||
list, err := query.Order(ent.Desc(alert.FieldCreatedAt), ent.Desc(alert.FieldID)).All(ctx)
|
||||
return list, total, err
|
||||
}
|
||||
|
||||
// UnreadAlertCount 未读预警数
|
||||
|
||||
Reference in New Issue
Block a user