|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
"github.com/jinzhu/copier"
|
|
|
"github.com/pkg/errors"
|
|
|
"github.com/tiptok/gocomm/pkg/cache"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/db/models"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
|
|
|
"gorm.io/gorm"
|
|
|
)
|
|
|
|
|
|
type ChatSessionRecordRepository struct {
|
|
|
*cache.CachedRepository
|
|
|
}
|
|
|
|
|
|
func (repository *ChatSessionRecordRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ChatSessionRecord) (*domain.ChatSessionRecord, error) {
|
|
|
var (
|
|
|
err error
|
|
|
m = &models.ChatSessionRecord{}
|
|
|
tx = conn.DB()
|
|
|
)
|
|
|
if m, err = repository.DomainModelToModel(dm); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
if tx = tx.Model(m).Save(m); tx.Error != nil {
|
|
|
return nil, tx.Error
|
|
|
}
|
|
|
dm.Id = m.Id
|
|
|
return repository.ModelToDomainModel(m)
|
|
|
|
|
|
}
|
|
|
|
|
|
func (repository *ChatSessionRecordRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ChatSessionRecord) (*domain.ChatSessionRecord, error) {
|
|
|
var (
|
|
|
err error
|
|
|
m *models.ChatSessionRecord
|
|
|
tx = conn.DB()
|
|
|
)
|
|
|
if m, err = repository.DomainModelToModel(dm); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
tx = tx.Model(m).Updates(m)
|
|
|
return nil, tx.Error
|
|
|
}
|
|
|
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
return repository.ModelToDomainModel(m)
|
|
|
}
|
|
|
|
|
|
func (repository *ChatSessionRecordRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ChatSessionRecord) (*domain.ChatSessionRecord, error) {
|
|
|
var (
|
|
|
err error
|
|
|
m *models.ChatSessionRecord
|
|
|
tx = transaction.DB()
|
|
|
)
|
|
|
if m, err = repository.DomainModelToModel(dm); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
oldVersion := dm.Version
|
|
|
m.Version += 1
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
|
|
|
if tx.RowsAffected == 0 {
|
|
|
return nil, domain.ErrUpdateFail
|
|
|
}
|
|
|
return nil, tx.Error
|
|
|
}
|
|
|
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
return repository.ModelToDomainModel(m)
|
|
|
}
|
|
|
|
|
|
func (repository *ChatSessionRecordRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ChatSessionRecord) (*domain.ChatSessionRecord, error) {
|
|
|
var (
|
|
|
tx = conn.DB()
|
|
|
m = &models.ChatSessionRecord{Id: dm.Id}
|
|
|
)
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
tx = tx.Where("id = ?", m.Id).Delete(m)
|
|
|
return m, tx.Error
|
|
|
}
|
|
|
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
|
|
|
return dm, err
|
|
|
}
|
|
|
return repository.ModelToDomainModel(m)
|
|
|
}
|
|
|
|
|
|
func (repository *ChatSessionRecordRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatSessionRecord, error) {
|
|
|
var (
|
|
|
err error
|
|
|
tx = conn.DB()
|
|
|
m = new(models.ChatSessionRecord)
|
|
|
)
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
tx = tx.Model(m).Where("id = ?", id).First(m)
|
|
|
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
|
|
|
return nil, domain.ErrNotFound
|
|
|
}
|
|
|
return m, tx.Error
|
|
|
}
|
|
|
cacheModel := new(models.ChatSessionRecord)
|
|
|
cacheModel.Id = id
|
|
|
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
return repository.ModelToDomainModel(m)
|
|
|
}
|
|
|
|
|
|
func (repository *ChatSessionRecordRepository) FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatSessionRecord, error) {
|
|
|
var (
|
|
|
err error
|
|
|
tx = conn.DB()
|
|
|
m = new(models.ChatSessionRecord)
|
|
|
)
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
tx = tx.Model(m).Unscoped().Where("id = ?", id).First(m)
|
|
|
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
|
|
|
return nil, domain.ErrNotFound
|
|
|
}
|
|
|
return m, tx.Error
|
|
|
}
|
|
|
cacheModel := new(models.ChatSessionRecord)
|
|
|
cacheModel.Id = id
|
|
|
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
return repository.ModelToDomainModel(m)
|
|
|
}
|
|
|
|
|
|
func (repository *ChatSessionRecordRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ChatSessionRecord, error) {
|
|
|
var (
|
|
|
tx = conn.DB()
|
|
|
ms []*models.ChatSessionRecord
|
|
|
dms = make([]*domain.ChatSessionRecord, 0)
|
|
|
total int64
|
|
|
)
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
tx = tx.Model(&ms).Order("id asc")
|
|
|
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
|
|
|
return dms, tx.Error
|
|
|
}
|
|
|
return dms, nil
|
|
|
}
|
|
|
|
|
|
if _, err := repository.Query(queryFunc); err != nil {
|
|
|
return 0, nil, err
|
|
|
}
|
|
|
|
|
|
for _, item := range ms {
|
|
|
if dm, err := repository.ModelToDomainModel(item); err != nil {
|
|
|
return 0, dms, err
|
|
|
} else {
|
|
|
dms = append(dms, dm)
|
|
|
}
|
|
|
}
|
|
|
return total, dms, nil
|
|
|
}
|
|
|
|
|
|
func (repository *ChatSessionRecordRepository) FindByCompanyUser(ctx context.Context, conn transaction.Conn, companyId, userId int64, queryOptions map[string]interface{}) (int64, []*domain.ChatSessionRecord, error) {
|
|
|
var (
|
|
|
tx = conn.DB()
|
|
|
ms []*models.ChatSessionRecord
|
|
|
dms = make([]*domain.ChatSessionRecord, 0)
|
|
|
total int64
|
|
|
)
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
tx = tx.Model(&ms).Order("id asc")
|
|
|
tx.Where("company_id = ?", companyId).Where("user_id = ?", userId)
|
|
|
if v, ok := queryOptions["sessionId"]; ok {
|
|
|
tx.Where("session_id =?", v)
|
|
|
}
|
|
|
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
|
|
|
return dms, tx.Error
|
|
|
}
|
|
|
return dms, nil
|
|
|
}
|
|
|
|
|
|
if _, err := repository.Query(queryFunc); err != nil {
|
|
|
return 0, nil, err
|
|
|
}
|
|
|
|
|
|
for _, item := range ms {
|
|
|
if dm, err := repository.ModelToDomainModel(item); err != nil {
|
|
|
return 0, dms, err
|
|
|
} else {
|
|
|
dms = append(dms, dm)
|
|
|
}
|
|
|
}
|
|
|
return total, dms, nil
|
|
|
}
|
|
|
|
|
|
func (repository *ChatSessionRecordRepository) ModelToDomainModel(from *models.ChatSessionRecord) (*domain.ChatSessionRecord, error) {
|
|
|
to := &domain.ChatSessionRecord{}
|
|
|
err := copier.Copy(to, from)
|
|
|
return to, err
|
|
|
}
|
|
|
|
|
|
func (repository *ChatSessionRecordRepository) DomainModelToModel(from *domain.ChatSessionRecord) (*models.ChatSessionRecord, error) {
|
|
|
to := &models.ChatSessionRecord{}
|
|
|
err := copier.Copy(to, from)
|
|
|
return to, err
|
|
|
}
|
|
|
|
|
|
func NewChatSessionRecordRepository(cache *cache.CachedRepository) domain.ChatSessionRecordRepository {
|
|
|
return &ChatSessionRecordRepository{CachedRepository: cache}
|
|
|
} |
...
|
...
|
|