You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.9 KiB
67 lines
1.9 KiB
package model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// OrgInsert 插入机构
|
|
func OrgInsert(ctx context.Context, db *gorm.DB, org *Organization) (int64, error) {
|
|
result := db.WithContext(ctx).Create(org)
|
|
if result.Error != nil {
|
|
return 0, result.Error
|
|
}
|
|
return org.Id, nil
|
|
}
|
|
|
|
// OrgFindOne 根据ID查询机构
|
|
func OrgFindOne(ctx context.Context, db *gorm.DB, id int64) (*Organization, error) {
|
|
var org Organization
|
|
result := db.WithContext(ctx).First(&org, id)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
return &org, nil
|
|
}
|
|
|
|
// OrgFindAll 查询所有启用的机构
|
|
func OrgFindAll(ctx context.Context, db *gorm.DB) ([]Organization, error) {
|
|
var orgs []Organization
|
|
err := db.WithContext(ctx).Where("status = 1").Order("sort_order ASC, id ASC").Find(&orgs).Error
|
|
return orgs, err
|
|
}
|
|
|
|
// OrgUpdate 更新机构
|
|
func OrgUpdate(ctx context.Context, db *gorm.DB, org *Organization) error {
|
|
return db.WithContext(ctx).Save(org).Error
|
|
}
|
|
|
|
// OrgDelete 删除机构
|
|
func OrgDelete(ctx context.Context, db *gorm.DB, id int64) error {
|
|
return db.WithContext(ctx).Delete(&Organization{}, id).Error
|
|
}
|
|
|
|
// OrgHasChildren 检查是否有子机构
|
|
func OrgHasChildren(ctx context.Context, db *gorm.DB, parentId int64) (bool, error) {
|
|
var count int64
|
|
err := db.WithContext(ctx).Model(&Organization{}).Where("parent_id = ? AND status = 1", parentId).Count(&count).Error
|
|
return count > 0, err
|
|
}
|
|
|
|
// OrgFindOneByCode 根据编码查询机构
|
|
func OrgFindOneByCode(ctx context.Context, db *gorm.DB, code string) (*Organization, error) {
|
|
var org Organization
|
|
result := db.WithContext(ctx).Where("code = ?", code).First(&org)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
return &org, nil
|
|
}
|
|
|