fusenapi/model/gmodel/ldap_department_logic.go
laodaming e4c8d033dc fix
2023-11-13 18:57:11 +08:00

45 lines
1.3 KiB
Go

package gmodel
import (
"context"
"errors"
"gorm.io/gorm"
)
//获取列表
func (d *LdapDepartmentModel)GetList(ctx context.Context,page,limit int,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
}
offset := (page - 1) * limit
err = db.Offset(offset).Limit(limit).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)
}