package model

import (
	"context"
	"gorm.io/gorm"
	"yx-dataset-server/app/errors"
	"yx-dataset-server/app/model/entity"
	"yx-dataset-server/app/schema"
)

// NewUserDataset 创建UserDataset存储实例
func NewUserDataset(db *gorm.DB) *UserDataset {
	return &UserDataset{db}
}

// UserDataset UserDataset存储
type UserDataset struct {
	db *gorm.DB
}

func (a *UserDataset) getQueryOption(opts ...schema.UserDatasetQueryOptions) schema.UserDatasetQueryOptions {
	var opt schema.UserDatasetQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}
	return opt
}

// Query 查询数据
func (a *UserDataset) Query(ctx context.Context, params schema.UserDatasetQueryParam, opts ...schema.UserDatasetQueryOptions) (*schema.UserDatasetQueryResult, error) {
	db := entity.GetUserDatasetDB(ctx, a.db)
	if v := params.UserId; v != "" {
		db = db.Where("user_id = ?", v)
	}
	db = db.Order("id DESC")

	opt := a.getQueryOption(opts...)
	var list entity.UserDatasets
	pr, err := WrapPageQuery(ctx, db, opt.PageParam, &list)
	if err != nil {
		return nil, errors.ErrDBServerInternalError
	}
	qr := &schema.UserDatasetQueryResult{
		PageResult: pr,
		Data:       list.ToSchemaUserDatasets(),
	}

	return qr, nil
}

// Get 查询指定数据
func (a *UserDataset) Get(ctx context.Context, recordID string, opts ...schema.UserDatasetQueryOptions) (*schema.UserDataset, error) {
	db := entity.GetUserDatasetDB(ctx, a.db).Where("record_id=?", recordID)
	var item entity.UserDataset
	ok, err := FindOne(ctx, db, &item)
	if err != nil {
		return nil, errors.ErrDBServerInternalError
	} else if !ok {
		return nil, nil
	}

	return item.ToSchemaUserDataset(), nil
}

// Create 创建数据
func (a *UserDataset) Create(ctx context.Context, item schema.UserDataset) error {
	UserDataset := entity.SchemaUserDataset(item).ToUserDataset()
	result := entity.GetUserDatasetDB(ctx, a.db).Create(UserDataset)
	if err := result.Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}

// Update 更新数据
func (a *UserDataset) Update(ctx context.Context, recordID string, item schema.UserDataset) error {
	UserDataset := entity.SchemaUserDataset(item).ToUserDataset()
	result := entity.GetUserDatasetDB(ctx, a.db).Where("record_id=?", recordID).Omit("record_id", "creator").Updates(UserDataset)
	if err := result.Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}

// Delete 删除数据
func (a *UserDataset) Delete(ctx context.Context, recordID string) error {
	result := entity.GetUserDatasetDB(ctx, a.db).Where("record_id=?", recordID).Delete(entity.UserDataset{})
	if err := result.Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}

// DeleteByUserId 删除数据
func (a *UserDataset) DeleteByUserId(ctx context.Context, recordID string) error {
	result := entity.GetUserDatasetDB(ctx, a.db).Where("user_id=?", recordID).Delete(entity.UserDataset{})
	if err := result.Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}

// UpdateStatus 更新状态
func (a *UserDataset) UpdateStatus(ctx context.Context, recordID string, status int) error {
	result := entity.GetUserDatasetDB(ctx, a.db).Where("record_id=?", recordID).Update("status", status)
	if err := result.Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}
