package model

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

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

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

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

// Query 查询数据
func (a *Dataset) Query(ctx context.Context, params schema.DatasetQueryParam, opts ...schema.DatasetQueryOptions) (*schema.DatasetQueryResult, error) {
	db := entity.GetDatasetDB(ctx, a.db)
	if v := params.LikeName; v != "" {
		db = db.Where("name LIKE ?", "%"+v+"%")
	}
	if v := params.OrgId; v != "" {
		db = db.Where("org_id=?", v)
	}
	if v := params.RecordIds; len(v) > 0 {
		db = db.Where("record_id in (?)", v)
	}
	if v := params.Type; v > 0 {
		db = db.Where("type = ?", v)
	}
	if v := params.Types; len(v) > 0 {
		db = db.Where("type IN (?)", v)
	}

	db = db.Order("sequence ASC")

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

	return qr, nil
}

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

	return item.ToSchemaDataset(), nil
}

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

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

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

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

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

//// UpdateSequence 更新排序
//func (a *Datasets) UpdateSequence(ctx context.Context, recordID string, sequence int) error {
//	result := entity.GetDatasetDB(ctx, a.db).Where("record_id=?", recordID).Update("sequence", sequence)
//	if err := result.Error; err != nil {
//		return errors.ErrDBServerInternalError
//	}
//	return nil
//}
//
//// UpdateBatchSequenceMinus 批量排序-1
//func (a *Datasets) UpdateBatchSequenceMinus(ctx context.Context, oldSequence, sequence int) error {
//	result := entity.GetDatasetDB(ctx, a.db).Where("sequence <= ? and sequence > ?", sequence, oldSequence).Update("sequence", gorm.Expr("sequence - 1"))
//	if err := result.Error; err != nil {
//		return errors.ErrDBServerInternalError
//	}
//	return nil
//}
//
//// GetMaxSequence 查询最大排序值
//func (a *Datasets) GetMaxSequence(ctx context.Context) (int, error) {
//	var maxSeq int
//	result := entity.GetDatasetDB(ctx, a.db).Select("MAX(sequence) as sequence")
//	if result == nil {
//		return maxSeq, nil
//	}
//	if err := result.Error; err != nil {
//		return 0, errors.ErrDBServerInternalError
//	}
//	result.Scan(&maxSeq)
//	return maxSeq, nil
//}
