package model

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

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

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

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

// Query 查询数据
func (a *Organization) Query(ctx context.Context, params schema.OrganizationQueryParam, opts ...schema.OrganizationQueryOptions) (*schema.OrganizationQueryResult, error) {
	db := entity.GetOrganizationDB(ctx, a.db)
	if v := params.LikeName; v != "" {
		db = db.Where("name LIKE ?", "%"+v+"%")
	}
	if v := params.RecordIds; len(v) > 0 {
		db = db.Where("record_id in (?)", v)
	}
	db = db.Order("Sequence ASC")

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

	return qr, nil
}

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

	return item.ToSchemaOrganization(), nil
}

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

	return item.ToSchemaOrganization(), nil
}

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

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

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

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