package admin_notification

import (
	"context"

	"tyc-server/app/main/api/internal/svc"
	"tyc-server/app/main/api/internal/types"
	"tyc-server/common/xerr"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type AdminGetNotificationDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAdminGetNotificationDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetNotificationDetailLogic {
	return &AdminGetNotificationDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AdminGetNotificationDetailLogic) AdminGetNotificationDetail(req *types.AdminGetNotificationDetailReq) (resp *types.AdminGetNotificationDetailResp, err error) {
	notification, err := l.svcCtx.GlobalNotificationsModel.FindOne(l.ctx, req.Id)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查找通知失败, err: %v, id: %d", err, req.Id)
	}
	resp = &types.AdminGetNotificationDetailResp{
		Id:               notification.Id,
		Title:            notification.Title,
		Content:          notification.Content,
		NotificationPage: notification.NotificationPage,
		StartDate:        "",
		StartTime:        notification.StartTime,
		EndDate:          "",
		EndTime:          notification.EndTime,
		Status:           notification.Status,
		CreateTime:       notification.CreateTime.Format("2006-01-02 15:04:05"),
		UpdateTime:       notification.UpdateTime.Format("2006-01-02 15:04:05"),
	}
	if notification.StartDate.Valid {
		resp.StartDate = notification.StartDate.Time.Format("2006-01-02")
	}
	if notification.EndDate.Valid {
		resp.EndDate = notification.EndDate.Time.Format("2006-01-02")
	}
	return resp, nil
}
