| 
									
										
										
										
											2025-01-01 02:00:06 +08:00
										 |  |  | package notification | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2025-04-09 15:58:06 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 	"tyc-server/common/xerr" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-01 02:00:06 +08:00
										 |  |  | 	"github.com/jinzhu/copier" | 
					
						
							|  |  |  | 	"github.com/pkg/errors" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-27 12:17:18 +08:00
										 |  |  | 	"tyc-server/app/main/api/internal/svc" | 
					
						
							|  |  |  | 	"tyc-server/app/main/api/internal/types" | 
					
						
							| 
									
										
										
										
											2025-01-01 02:00:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/zeromicro/go-zero/core/logx" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type GetNotificationsLogic struct { | 
					
						
							|  |  |  | 	logx.Logger | 
					
						
							|  |  |  | 	ctx    context.Context | 
					
						
							|  |  |  | 	svcCtx *svc.ServiceContext | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewGetNotificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetNotificationsLogic { | 
					
						
							|  |  |  | 	return &GetNotificationsLogic{ | 
					
						
							|  |  |  | 		Logger: logx.WithContext(ctx), | 
					
						
							|  |  |  | 		ctx:    ctx, | 
					
						
							|  |  |  | 		svcCtx: svcCtx, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (l *GetNotificationsLogic) GetNotifications() (resp *types.GetNotificationsResp, err error) { | 
					
						
							|  |  |  | 	// 获取今天的日期 | 
					
						
							|  |  |  | 	now := time.Now() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 获取开始和结束日期的时间戳 | 
					
						
							|  |  |  | 	todayStart := now.Format("2006-01-02") + " 00:00:00" | 
					
						
							|  |  |  | 	todayEnd := now.Format("2006-01-02") + " 23:59:59" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 构建查询条件 | 
					
						
							|  |  |  | 	builder := l.svcCtx.GlobalNotificationsModel.SelectBuilder(). | 
					
						
							| 
									
										
										
										
											2025-05-27 18:35:01 +08:00
										 |  |  | 		Where("status = ?", 1). | 
					
						
							| 
									
										
										
										
											2025-01-01 02:00:06 +08:00
										 |  |  | 		Where("(start_date IS NULL OR start_date <= ?)", todayEnd). // start_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) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &types.GetNotificationsResp{Notifications: notifications}, nil | 
					
						
							|  |  |  | } |