package model

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

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

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

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

// Query 查询数据
func (a *User) Query(ctx context.Context, params schema.UserQueryParam, opts ...schema.UserOptions) (*schema.UserQueryResult, error) {
	db := entity.GetUserDB(ctx, a.db)

	if v := params.LikeName; v != "" {
		db = db.Where("real_name LIKE ?", "%"+v+"%")
	}

	if v := params.RecordIDs; len(v) > 0 {
		db = db.Where("record_id in (?)", v)
	}

	if v := params.OrgId; v != "" {
		db = db.Where("org_id=?", v)
	}
	if v := params.RoleCode; len(v) > 0 {
		subQuery := entity.GetRoleDB(ctx, a.db).Where("code in (?)", v).Select("role_id")
		db = db.Where("role_id in (?)", subQuery)
	}
	if v := params.UserName; v != "" {
		db = db.Where("user_name=?", v)
	}
	db = db.Order("id DESC")

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

	return qr, nil
}

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

	return item.ToSchemaUser(), nil
}

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

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

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

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

// UpdatePassword 更新密码
func (a *User) UpdatePassword(ctx context.Context, recordID, password string) error {
	result := entity.GetUserDB(ctx, a.db).Where("record_id=?", recordID).Update("password", password)
	if err := result.Error; err != nil {
		return errors.WithStack(err)
	}
	return nil
}
