pg_sent_notification_repository.go 6.2 KB
package repository

import (
	"fmt"
	"github.com/linmadan/egglib-go/utils/snowflake"

	"github.com/go-pg/pg"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
)

type SentNotificationRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func (repository *SentNotificationRepository) nextIdentify() (int64, error) {
	IdWorker, err := snowflake.NewIdWorker(3)
	if err != nil {
		return 0, err
	}
	id, err := IdWorker.NextId()
	return id, err
}

func (repository *SentNotificationRepository) Save(sentNotification *domain.SentNotification) (*domain.SentNotification, error) {
	tx := repository.transactionContext.PgTx
	if sentNotification.Identify() == nil {
		if nextId, err := repository.nextIdentify(); err != nil {
			return sentNotification, err
		} else {
			sentNotification.SentNotificationId = nextId
		}
		if _, err := tx.QueryOne(
			pg.Scan(&sentNotification.SentNotificationId, &sentNotification.Notification.NotificationId, &sentNotification.Receiver, &sentNotification.IsRead, &sentNotification.ReadTime),
			"INSERT INTO sent_notifications (id, notification_id, receiver, is_read, read_time) VALUES (?, ?, ?, ?, ?) RETURNING id, notification_id, receiver, is_read, read_time",
			sentNotification.SentNotificationId, sentNotification.Notification.NotificationId, sentNotification.Receiver, sentNotification.IsRead, sentNotification.ReadTime); err != nil {
			return sentNotification, err
		}
	} else {
		if _, err := tx.QueryOne(
			pg.Scan(&sentNotification.SentNotificationId, &sentNotification.Notification.NotificationId, &sentNotification.Receiver, &sentNotification.IsRead, &sentNotification.ReadTime),
			"UPDATE sent_notifications SET notification_id=?, receiver=?, is_read=?, read_time=? WHERE id=? RETURNING id, notification_id, receiver, is_read, read_time",
			sentNotification.Notification.NotificationId, sentNotification.Receiver, sentNotification.IsRead, sentNotification.ReadTime, sentNotification.Identify()); err != nil {
			return sentNotification, err
		}
	}
	return sentNotification, nil
}
func (repository *SentNotificationRepository) Remove(sentNotification *domain.SentNotification) (*domain.SentNotification, error) {
	tx := repository.transactionContext.PgTx
	sentNotificationModel := new(models.SentNotification)
	sentNotificationModel.Id = sentNotification.Identify().(int64)
	if _, err := tx.Model(sentNotificationModel).WherePK().Delete(); err != nil {
		return sentNotification, err
	}
	return sentNotification, nil
}
func (repository *SentNotificationRepository) FindOne(queryOptions map[string]interface{}) (*domain.SentNotification, error) {
	tx := repository.transactionContext.PgTx
	sentNotificationModel := new(models.SentNotification)
	query := tx.Model(sentNotificationModel).Relation("Notification")
	if sentNotificationId, ok := queryOptions["sentNotificationId"]; ok {
		query = query.Where("sent_notification.id = ?", sentNotificationId)
	}
	if err := query.First(); err != nil {
		if err.Error() == "pg: no rows in result set" {
			return nil, fmt.Errorf("没有此资源")
		} else {
			return nil, err
		}
	}
	if sentNotificationModel.Id == 0 {
		return nil, nil
	} else {
		return repository.transformPgModelToDomainModel(sentNotificationModel)
	}
}
func (repository *SentNotificationRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.SentNotification, error) {
	tx := repository.transactionContext.PgTx
	var sentNotificationModels []*models.SentNotification
	sentNotifications := make([]*domain.SentNotification, 0)
	query := tx.Model(&sentNotificationModels).Relation("Notification")
	if receiver, ok := queryOptions["receiver"]; ok && (receiver != int64(0)) {
		query = query.Where(`sent_notification.receiver @> '{"uid":?}'`, receiver)
	}
	if notificationType, ok := queryOptions["notificationType"]; ok && (notificationType != int(0)) {
		query = query.Where("notification.notification_type = ?", notificationType)
	}
	if offset, ok := queryOptions["offset"]; ok {
		offset := offset.(int)
		if offset > -1 {
			query = query.Offset(offset)
		}
	} else {
		query = query.Offset(0)
	}
	if limit, ok := queryOptions["limit"]; ok {
		limit := limit.(int)
		if limit > -1 {
			query = query.Limit(limit)
		}
	} else {
		query = query.Limit(20)
	}
	if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
		return 0, sentNotifications, err
	} else {
		for _, sentNotificationModel := range sentNotificationModels {
			if sentNotification, err := repository.transformPgModelToDomainModel(sentNotificationModel); err != nil {
				return 0, sentNotifications, err
			} else {
				sentNotifications = append(sentNotifications, sentNotification)
			}
		}
		return int64(count), sentNotifications, nil
	}
}
func (repository *SentNotificationRepository) transformPgModelToDomainModel(sentNotificationModel *models.SentNotification) (*domain.SentNotification, error) {
	var notification *domain.Notification
	if sentNotificationModel.Notification == nil {
		notification = nil
	} else {
		notification = &domain.Notification{
			NotificationId:       sentNotificationModel.Notification.Id,
			NotificationType:     sentNotificationModel.Notification.NotificationType,
			NotificationTitle:    sentNotificationModel.Notification.NotificationTitle,
			NotificationContent:  sentNotificationModel.Notification.NotificationContent,
			NotificationTime:     sentNotificationModel.Notification.NotificationTime,
			ExternalResourceType: sentNotificationModel.Notification.ExternalResourceType,
			ExternalResource:     sentNotificationModel.Notification.ExternalResource,
		}
	}
	return &domain.SentNotification{
		SentNotificationId: sentNotificationModel.Id,
		Notification:       notification,
		Receiver:           sentNotificationModel.Receiver,
		IsRead:             sentNotificationModel.IsRead,
		ReadTime:           sentNotificationModel.ReadTime,
	}, nil
}
func NewSentNotificationRepository(transactionContext *pgTransaction.TransactionContext) (*SentNotificationRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &SentNotificationRepository{
			transactionContext: transactionContext,
		}, nil
	}
}