package model

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

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

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

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

// Query 查询数据
func (a *DatasetRelation) Query(ctx context.Context, params schema.DatasetRelationQueryParam, opts ...schema.DatasetRelationQueryOptions) (*schema.DatasetRelationQueryResult, error) {
	db := entity.GetDatasetRelationDB(ctx, a.db)
	if v := params.BizId; v != "" {
		db = db.Where("biz_id = ?", v)
	}
	if v := params.BizIds; len(v) > 0 {
		db = db.Where("biz_id IN (?)", v)
	}
	if v := params.DatasetId; v != "" {
		db = db.Where("dataset_id = ?", v)
	}
	if v := params.DatasetIds; len(v) > 0 {
		db = db.Where("dataset_id IN (?)", 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("id DESC")

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

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

// Create 创建数据
func (a *DatasetRelation) Create(ctx context.Context, item schema.DatasetRelation) error {
	e := entity.SchemaDatasetRelation(item).ToDatasetRelation()
	if err := entity.GetDatasetRelationDB(ctx, a.db).Create(e).Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}

// Update 更新数据
func (a *DatasetRelation) Update(ctx context.Context, recordID string, item schema.DatasetRelation) error {
	e := entity.SchemaDatasetRelation(item).ToDatasetRelation()
	if err := entity.GetDatasetRelationDB(ctx, a.db).Where("record_id=?", recordID).Omit("record_id").Updates(e).Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}

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

// DeleteByBizId 按业务id删除
func (a *DatasetRelation) DeleteByBizId(ctx context.Context, bizId string) error {
	if err := entity.GetDatasetRelationDB(ctx, a.db).Where("biz_id=?", bizId).Delete(entity.DatasetRelation{}).Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}

// DeleteByBizIdAndType 按业务id+类型删除
func (a *DatasetRelation) DeleteByBizIdAndType(ctx context.Context, bizId string, t int) error {
	if err := entity.GetDatasetRelationDB(ctx, a.db).Where("biz_id=? AND type=?", bizId, t).Delete(entity.DatasetRelation{}).Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}

// DeleteByDatasetId 按知识库id删除
func (a *DatasetRelation) DeleteByDatasetId(ctx context.Context, datasetId string) error {
	if err := entity.GetDatasetRelationDB(ctx, a.db).Where("dataset_id=?", datasetId).Delete(entity.DatasetRelation{}).Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}
