195 lines
5.1 KiB
Go
195 lines
5.1 KiB
Go
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")
|
|
|
|
// 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)
|
|
if name == "" {
|
|
return nil, errors.New("name required")
|
|
}
|
|
|
|
o := &model.Org{
|
|
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),
|
|
}
|
|
if err := mysql.OrgCreate(o); err != nil {
|
|
logrus.WithFields(logrus.Fields{
|
|
"func": "OrgCreate",
|
|
}).Warnf("mysql.OrgCreate: %v", err)
|
|
return nil, err
|
|
}
|
|
return o, nil
|
|
}
|
|
|
|
func OrgUpdate(idv string, req OrgUpdateRequest) (*model.Org, error) {
|
|
oid := strings.TrimSpace(idv)
|
|
if oid == "" {
|
|
return nil, ErrNotFound
|
|
}
|
|
existing, found, err := mysql.OrgByID(&oid)
|
|
if err != nil {
|
|
logrus.WithFields(logrus.Fields{
|
|
"func": "OrgUpdate",
|
|
}).Warnf("mysql.OrgByID: %v", err)
|
|
return nil, err
|
|
}
|
|
if !found || existing == nil {
|
|
logrus.WithFields(logrus.Fields{
|
|
"func": "OrgUpdate",
|
|
}).Warnf("mysql.OrgByID: not found")
|
|
return nil, ErrNotFound
|
|
}
|
|
if req.ParentID != nil {
|
|
existing.ParentID = strings.TrimSpace(*req.ParentID)
|
|
}
|
|
if req.Name != nil {
|
|
n := strings.TrimSpace(*req.Name)
|
|
if n == "" {
|
|
return nil, errors.New("name required")
|
|
}
|
|
existing.Name = n
|
|
}
|
|
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)
|
|
}
|
|
if req.AdminRegion != nil {
|
|
existing.AdminRegion = strings.TrimSpace(*req.AdminRegion)
|
|
}
|
|
if req.Introduction != nil {
|
|
existing.Introduction = strings.TrimSpace(*req.Introduction)
|
|
}
|
|
if err := mysql.OrgUpdate(existing); err != nil {
|
|
logrus.WithFields(logrus.Fields{
|
|
"func": "OrgUpdate",
|
|
}).Warnf("mysql.OrgUpdate: %v", err)
|
|
return nil, err
|
|
}
|
|
return existing, nil
|
|
}
|
|
|
|
func OrgDelete(idv string) error {
|
|
oid := strings.TrimSpace(idv)
|
|
if oid == "" {
|
|
return ErrNotFound
|
|
}
|
|
ok, err := mysql.OrgDelete(&oid)
|
|
if err != nil {
|
|
logrus.WithFields(logrus.Fields{
|
|
"func": "OrgDelete",
|
|
}).Warnf("mysql.OrgDelete: %v", err)
|
|
return err
|
|
}
|
|
if !ok {
|
|
logrus.WithFields(logrus.Fields{
|
|
"func": "OrgDelete",
|
|
}).Warnf("mysql.OrgDelete: not found")
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func OrgDetail(idv string) (*model.Org, error) {
|
|
oid := strings.TrimSpace(idv)
|
|
if oid == "" {
|
|
return nil, ErrNotFound
|
|
}
|
|
o, found, err := mysql.OrgByID(&oid)
|
|
if err != nil {
|
|
logrus.WithFields(logrus.Fields{
|
|
"func": "OrgDetail",
|
|
}).Warnf("mysql.OrgByID: %v", err)
|
|
return nil, err
|
|
}
|
|
if !found || o == nil {
|
|
logrus.WithFields(logrus.Fields{
|
|
"func": "OrgDetail",
|
|
}).Warnf("mysql.OrgByID: not found")
|
|
return nil, ErrNotFound
|
|
}
|
|
return o, nil
|
|
}
|
|
|
|
func OrgPage(parentID *string, page, size int) ([]model.Org, int64, error) {
|
|
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)
|
|
}
|