// Code generated by gen-table. DO NOT EDIT.
// Code generated by gen-table. DO NOT EDIT.
// Code generated by gen-table. DO NOT EDIT.

package repository

import (
	"time"

	"github.com/ch3nnn/webstack-go/internal/dal/model"
	"github.com/ch3nnn/webstack-go/internal/dal/query"

	"gorm.io/gen"
	"gorm.io/gen/field"
)

var _ iSysUserDao = (*sysUserDao)(nil)

// ------------------------------------
// SysUser  ColumnName
// ------------------------------------
type iWhereSysUserFunc interface {
	WhereByID(id int) func(dao gen.Dao) gen.Dao
	WhereByUsername(username string) func(dao gen.Dao) gen.Dao
	WhereByPassword(password string) func(dao gen.Dao) gen.Dao
	WhereByCreatedAt(createdAt time.Time) func(dao gen.Dao) gen.Dao
	WhereByUpdatedAt(updatedAt time.Time) func(dao gen.Dao) gen.Dao
	WhereByDeletedAt(deletedAt time.Time) func(dao gen.Dao) gen.Dao
}

// ------------------------------------
// SysUser  Generate Function
// ------------------------------------
type iGenSysUserFunc interface {
	Create(m *model.SysUser) (*model.SysUser, error)
	Delete(whereFunc ...func(dao gen.Dao) gen.Dao) error
	DeletePhysical(whereFunc ...func(dao gen.Dao) gen.Dao) error
	Update(v interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (rowsAffected int64, err error)
	FindCount(whereFunc ...func(dao gen.Dao) gen.Dao) (int64, error)
	FindOne(whereFunc ...func(dao gen.Dao) gen.Dao) (*model.SysUser, error)
	FindAll(whereFunc ...func(dao gen.Dao) gen.Dao) ([]*model.SysUser, error)
	FindPage(page int, pageSize int, orderColumns []field.Expr, whereFunc ...func(dao gen.Dao) gen.Dao) ([]*model.SysUser, int64, error)
	Scan(result interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (err error)
	ScanPage(page int, pageSize int, orderColumns []field.Expr, result interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (count int64, err error)
}

type iSysUserDao interface {
	iWhereSysUserFunc
	iGenSysUserFunc
}

type sysUserDao struct {
	sysUserDo query.ISysUserDo
}

func (s *sysUserDao) WhereByID(id int) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.SysUser.ID.Eq(id))
	}
}

func (s *sysUserDao) WhereByUsername(username string) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.SysUser.Username.Eq(username))
	}
}

func (s *sysUserDao) WhereByPassword(password string) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.SysUser.Password.Eq(password))
	}
}

func (s *sysUserDao) WhereByCreatedAt(createdAt time.Time) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.SysUser.CreatedAt.Eq(createdAt))
	}
}

func (s *sysUserDao) WhereByUpdatedAt(updatedAt time.Time) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.SysUser.UpdatedAt.Eq(updatedAt))
	}
}

func (s *sysUserDao) WhereByDeletedAt(deletedAt time.Time) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.SysUser.DeletedAt.Eq(deletedAt))
	}
}

func (s *sysUserDao) Create(m *model.SysUser) (*model.SysUser, error) {
	if err := s.sysUserDo.Create(m); err != nil {
		return nil, err
	}
	return s.FindOne(s.WhereByID(m.ID))
}

func (s *sysUserDao) FindCount(whereFunc ...func(dao gen.Dao) gen.Dao) (int64, error) {
	return s.sysUserDo.Scopes(whereFunc...).Count()
}

func (s *sysUserDao) FindOne(whereFunc ...func(dao gen.Dao) gen.Dao) (*model.SysUser, error) {
	return s.sysUserDo.Scopes(whereFunc...).First()
}

func (s *sysUserDao) FindAll(whereFunc ...func(dao gen.Dao) gen.Dao) ([]*model.SysUser, error) {
	return s.sysUserDo.Scopes(whereFunc...).Find()
}

func (s *sysUserDao) FindPage(page int, pageSize int, orderColumns []field.Expr, whereFunc ...func(dao gen.Dao) gen.Dao) ([]*model.SysUser, int64, error) {
	return s.sysUserDo.Scopes(whereFunc...).Order(orderColumns...).FindByPage((page-1)*pageSize, pageSize)
}

// 注意 当通过 struct 更新时，GORM 只会更新非零字段
// 如果想确保指定字段被更新，使用 map 来完成更新操作
func (s *sysUserDao) Update(v interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (rowsAffected int64, err error) {
	info, err := s.sysUserDo.Scopes(whereFunc...).Updates(v)
	if err != nil {
		return rowsAffected, err
	}

	return info.RowsAffected, nil
}

func (s *sysUserDao) Delete(whereFunc ...func(dao gen.Dao) gen.Dao) error {
	if _, err := s.sysUserDo.Scopes(whereFunc...).Delete(); err != nil {
		return err
	}
	return nil
}

func (s *sysUserDao) DeletePhysical(whereFunc ...func(dao gen.Dao) gen.Dao) error {
	if _, err := s.sysUserDo.Unscoped().Scopes(whereFunc...).Delete(); err != nil {
		return err
	}
	return nil
}

func (s *sysUserDao) Scan(result interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (err error) {
	return s.sysUserDo.Scopes(whereFunc...).Scan(result)
}

func (s *sysUserDao) ScanPage(page int, pageSize int, orderColumns []field.Expr, result interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (count int64, err error) {
	return s.sysUserDo.Scopes(whereFunc...).Order(orderColumns...).ScanByPage(result, (page-1)*pageSize, pageSize)
}
