fusenapi/model/gmodel/ldap_department_logic.go

48 lines
1.3 KiB
Go
Raw Normal View History

2023-11-13 09:59:46 +00:00
package gmodel
2023-11-14 03:45:25 +00:00
import (
"context"
2023-11-13 10:57:11 +00:00
"errors"
2023-11-13 09:59:46 +00:00
2023-11-13 10:57:11 +00:00
"gorm.io/gorm"
)
2023-11-14 03:45:25 +00:00
2023-11-14 03:50:41 +00:00
// 获取列表
func (d *LdapDepartmentModel) GetAll(ctx context.Context, sort string) (resp []LdapDepartment, total int64, err error) {
2023-11-13 10:57:11 +00:00
db := d.db.WithContext(ctx).Model(&LdapDepartment{})
2023-11-14 03:45:25 +00:00
if sort != "" {
db = db.Order(sort)
}
2023-11-14 03:50:41 +00:00
if err = db.Count(&total).Error; err != nil {
2023-11-14 03:45:25 +00:00
return nil, 0, err
}
2023-11-14 03:26:08 +00:00
err = db.Find(&resp).Error
2023-11-14 03:45:25 +00:00
return resp, total, err
}
2023-11-14 03:50:41 +00:00
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
2023-11-13 10:57:11 +00:00
}
2023-11-14 03:45:25 +00:00
2023-11-14 03:50:41 +00:00
// 更新
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
2023-11-13 10:57:11 +00:00
}
2023-11-14 03:50:41 +00:00
// 创建
func (d *LdapDepartmentModel) Create(ctx context.Context, data *LdapDepartment) error {
2023-11-13 10:57:11 +00:00
return d.db.WithContext(ctx).Model(&LdapDepartment{}).Create(&data).Error
2023-11-14 03:45:25 +00:00
}
2023-11-14 03:50:41 +00:00
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)
2023-11-13 10:57:11 +00:00
}
return err
2023-11-14 03:45:25 +00:00
}
2023-11-14 03:50:41 +00:00
return d.Update(ctx, id, data)
2023-11-14 03:45:25 +00:00
}