fusenapi/model/gmodel/ldap_department_logic.go
2023-11-14 11:50:41 +08:00

48 lines
1.3 KiB
Go

package gmodel
import (
"context"
"errors"
"gorm.io/gorm"
)
// 获取列表
func (d *LdapDepartmentModel) GetAll(ctx context.Context, sort string) (resp []LdapDepartment, total int64, err error) {
db := d.db.WithContext(ctx).Model(&LdapDepartment{})
if sort != "" {
db = db.Order(sort)
}
if err = db.Count(&total).Error; err != nil {
return nil, 0, err
}
err = db.Find(&resp).Error
return resp, total, err
}
func (d *LdapDepartmentModel) FindOne(ctx context.Context, id int64) (resp *LdapDepartment, err error) {
err = d.db.WithContext(ctx).Model(&LdapDepartment{}).Where("id = ?", id).Take(&resp).Error
return resp, err
}
// 更新
func (d *LdapDepartmentModel) Update(ctx context.Context, id int64, data *LdapDepartment) error {
return d.db.WithContext(ctx).Model(&LdapDepartment{}).Where("id = ?", id).Updates(&data).Error
}
// 创建
func (d *LdapDepartmentModel) Create(ctx context.Context, data *LdapDepartment) error {
return d.db.WithContext(ctx).Model(&LdapDepartment{}).Create(&data).Error
}
func (d *LdapDepartmentModel) CreateOrUpdate(ctx context.Context, id int64, data *LdapDepartment) error {
_, err := d.FindOne(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return d.Create(ctx, data)
}
return err
}
return d.Update(ctx, id, data)
}