f
This commit is contained in:
@@ -2,14 +2,15 @@ package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bdrp-server/common/xerr"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"bdrp-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"bdrp-server/app/main/api/internal/svc"
|
||||
"bdrp-server/app/main/api/internal/types"
|
||||
"bdrp-server/app/main/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -36,22 +37,42 @@ func (l *GetNotificationsLogic) GetNotifications() (resp *types.GetNotifications
|
||||
todayStart := now.Format("2006-01-02") + " 00:00:00"
|
||||
todayEnd := now.Format("2006-01-02") + " 23:59:59"
|
||||
|
||||
// 构建查询条件
|
||||
// 构建查询条件(与后台管理一致:status 1=启用 0=禁用;库中为整型,不能用字符串 "active")
|
||||
builder := l.svcCtx.GlobalNotificationsModel.SelectBuilder().
|
||||
Where("status = ?", "active").
|
||||
Where("status = ?", 1).
|
||||
Where("(start_date IS NULL OR start_date <= ?)", todayEnd). // start_date 是 NULL 或者小于等于今天结束时间
|
||||
Where("(end_date IS NULL OR end_date >= ?)", todayStart) // end_date 是 NULL 或者大于等于今天开始时间
|
||||
Where("(end_date IS NULL OR end_date >= ?)", todayStart) // end_date 是 NULL 或者大于等于今天开始时间
|
||||
|
||||
notificationsModelList, findErr := l.svcCtx.GlobalNotificationsModel.FindAll(l.ctx, builder, "")
|
||||
if findErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "全局通知, 查找通知失败, err:%+v", findErr)
|
||||
}
|
||||
|
||||
var notifications []types.Notification
|
||||
copyErr := copier.Copy(¬ifications, ¬ificationsModelList)
|
||||
if copyErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "全局通知, 复制结构体失败, err:%+v", copyErr)
|
||||
}
|
||||
notifications := toPublicNotifications(notificationsModelList)
|
||||
|
||||
return &types.GetNotificationsResp{Notifications: notifications}, nil
|
||||
return &types.GetNotificationsResp{
|
||||
Notifications: notifications,
|
||||
Total: int64(len(notifications)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func toPublicNotifications(list []*model.GlobalNotifications) []types.Notification {
|
||||
out := make([]types.Notification, 0, len(list))
|
||||
for _, m := range list {
|
||||
n := types.Notification{
|
||||
Title: m.Title,
|
||||
Content: m.Content,
|
||||
NotificationPage: m.NotificationPage,
|
||||
StartTime: m.StartTime,
|
||||
EndTime: m.EndTime,
|
||||
}
|
||||
if m.StartDate.Valid {
|
||||
n.StartDate = m.StartDate.Time.Format("2006-01-02")
|
||||
}
|
||||
if m.EndDate.Valid {
|
||||
n.EndDate = m.EndDate.Time.Format("2006-01-02")
|
||||
}
|
||||
out = append(out, n)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user