heritage-api/service/org-service.go

195 lines
5.1 KiB
Go
Raw Normal View History

2026-03-13 08:35:54 +00:00
package service
import (
"errors"
"strings"
"github.com/sirupsen/logrus"
"myschools.me/heritage/heritage-api/model"
"myschools.me/heritage/heritage-api/mysql"
)
var ErrNotFound = errors.New("not found")
2026-03-18 09:18:06 +00:00
// OrgCreateRequest 创建组织请求结构
type OrgCreateRequest struct {
ParentID string `json:"parentId"`
Name string `json:"name"`
Enabled *bool `json:"enabled"`
Sort *int `json:"sort"`
Remark string `json:"remark"`
ProtectionLevel string `json:"protectionLevel"`
ProtectionType string `json:"protectionType"`
AnnouncementBatch string `json:"announcementBatch"`
AdminRegion string `json:"adminRegion"`
CurrentOrg string `json:"currentOrg"`
CurrentUser string `json:"currentUser"`
Introduction string `json:"introduction"`
}
// OrgUpdateRequest 更新组织请求结构
type OrgUpdateRequest struct {
ParentID *string `json:"parentId"`
Name *string `json:"name"`
Enabled *bool `json:"enabled"`
Sort *int `json:"sort"`
Remark *string `json:"remark"`
ProtectionLevel *string `json:"protectionLevel"`
ProtectionType *string `json:"protectionType"`
AnnouncementBatch *string `json:"announcementBatch"`
AdminRegion *string `json:"adminRegion"`
CurrentOrg *string `json:"currentOrg"`
CurrentUser *string `json:"currentUser"`
Introduction *string `json:"introduction"`
}
// OrgResponse 组织响应结构
type OrgResponse struct {
Org *model.Org `json:"org"`
}
// OrgListResponse 组织列表响应结构
type OrgListResponse struct {
Items []model.Org `json:"items"`
Total int64 `json:"total"`
Page int `json:"page"`
Size int `json:"size"`
}
func OrgCreate(req OrgCreateRequest) (*model.Org, error) {
name := strings.TrimSpace(req.Name)
2026-03-13 08:35:54 +00:00
if name == "" {
return nil, errors.New("name required")
}
2026-03-18 09:18:06 +00:00
2026-03-13 08:35:54 +00:00
o := &model.Org{
2026-03-18 09:18:06 +00:00
ID: newID(),
ParentID: strings.TrimSpace(req.ParentID),
Name: name,
Remark: strings.TrimSpace(req.Remark),
ProtectionLevel: strings.TrimSpace(req.ProtectionLevel),
ProtectionType: strings.TrimSpace(req.ProtectionType),
AnnouncementBatch: strings.TrimSpace(req.AnnouncementBatch),
AdminRegion: strings.TrimSpace(req.AdminRegion),
Introduction: strings.TrimSpace(req.Introduction),
2026-03-13 08:35:54 +00:00
}
if err := mysql.OrgCreate(o); err != nil {
logrus.WithFields(logrus.Fields{
2026-03-18 09:18:06 +00:00
"func": "OrgCreate",
2026-03-13 08:35:54 +00:00
}).Warnf("mysql.OrgCreate: %v", err)
return nil, err
}
return o, nil
}
2026-03-18 09:18:06 +00:00
func OrgUpdate(idv string, req OrgUpdateRequest) (*model.Org, error) {
2026-03-13 08:35:54 +00:00
oid := strings.TrimSpace(idv)
if oid == "" {
return nil, ErrNotFound
}
existing, found, err := mysql.OrgByID(&oid)
if err != nil {
logrus.WithFields(logrus.Fields{
2026-03-18 09:18:06 +00:00
"func": "OrgUpdate",
2026-03-13 08:35:54 +00:00
}).Warnf("mysql.OrgByID: %v", err)
return nil, err
}
if !found || existing == nil {
logrus.WithFields(logrus.Fields{
2026-03-18 09:18:06 +00:00
"func": "OrgUpdate",
2026-03-13 08:35:54 +00:00
}).Warnf("mysql.OrgByID: not found")
return nil, ErrNotFound
}
2026-03-18 09:18:06 +00:00
if req.ParentID != nil {
existing.ParentID = strings.TrimSpace(*req.ParentID)
2026-03-13 08:35:54 +00:00
}
2026-03-18 09:18:06 +00:00
if req.Name != nil {
n := strings.TrimSpace(*req.Name)
2026-03-13 08:35:54 +00:00
if n == "" {
return nil, errors.New("name required")
}
existing.Name = n
}
2026-03-18 09:18:06 +00:00
if req.Remark != nil {
existing.Remark = strings.TrimSpace(*req.Remark)
}
if req.ProtectionLevel != nil {
existing.ProtectionLevel = strings.TrimSpace(*req.ProtectionLevel)
}
if req.ProtectionType != nil {
existing.ProtectionType = strings.TrimSpace(*req.ProtectionType)
}
if req.AnnouncementBatch != nil {
existing.AnnouncementBatch = strings.TrimSpace(*req.AnnouncementBatch)
2026-03-13 08:35:54 +00:00
}
2026-03-18 09:18:06 +00:00
if req.AdminRegion != nil {
existing.AdminRegion = strings.TrimSpace(*req.AdminRegion)
2026-03-13 08:35:54 +00:00
}
2026-03-18 09:18:06 +00:00
if req.Introduction != nil {
existing.Introduction = strings.TrimSpace(*req.Introduction)
2026-03-13 08:35:54 +00:00
}
if err := mysql.OrgUpdate(existing); err != nil {
logrus.WithFields(logrus.Fields{
2026-03-18 09:18:06 +00:00
"func": "OrgUpdate",
2026-03-13 08:35:54 +00:00
}).Warnf("mysql.OrgUpdate: %v", err)
return nil, err
}
return existing, nil
}
2026-03-18 09:18:06 +00:00
func OrgDelete(idv string) error {
2026-03-13 08:35:54 +00:00
oid := strings.TrimSpace(idv)
if oid == "" {
return ErrNotFound
}
ok, err := mysql.OrgDelete(&oid)
if err != nil {
logrus.WithFields(logrus.Fields{
2026-03-18 09:18:06 +00:00
"func": "OrgDelete",
2026-03-13 08:35:54 +00:00
}).Warnf("mysql.OrgDelete: %v", err)
return err
}
if !ok {
logrus.WithFields(logrus.Fields{
2026-03-18 09:18:06 +00:00
"func": "OrgDelete",
2026-03-13 08:35:54 +00:00
}).Warnf("mysql.OrgDelete: not found")
return ErrNotFound
}
return nil
}
2026-03-18 09:18:06 +00:00
func OrgDetail(idv string) (*model.Org, error) {
2026-03-13 08:35:54 +00:00
oid := strings.TrimSpace(idv)
if oid == "" {
return nil, ErrNotFound
}
o, found, err := mysql.OrgByID(&oid)
if err != nil {
logrus.WithFields(logrus.Fields{
2026-03-18 09:18:06 +00:00
"func": "OrgDetail",
2026-03-13 08:35:54 +00:00
}).Warnf("mysql.OrgByID: %v", err)
return nil, err
}
if !found || o == nil {
logrus.WithFields(logrus.Fields{
2026-03-18 09:18:06 +00:00
"func": "OrgDetail",
2026-03-13 08:35:54 +00:00
}).Warnf("mysql.OrgByID: not found")
return nil, ErrNotFound
}
return o, nil
}
2026-03-18 09:18:06 +00:00
func OrgPage(parentID *string, page, size int) ([]model.Org, int64, error) {
2026-03-13 08:35:54 +00:00
if page < 1 {
page = 1
}
if size < 1 {
size = 20
}
if size > 200 {
size = 200
}
offset := (page - 1) * size
return mysql.OrgList(parentID, &offset, &size)
}