241 lines
6.1 KiB
Go
241 lines
6.1 KiB
Go
package services
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/models"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
)
|
|
|
|
func EnsureCrmProjectDocCategory(tid int, projectID uint64, projectName string, currentCategoryID uint64) (uint64, error) {
|
|
if models.Orm == nil || tid <= 0 {
|
|
return 0, nil
|
|
}
|
|
projectName = strings.TrimSpace(projectName)
|
|
if projectName == "" {
|
|
return 0, nil
|
|
}
|
|
|
|
models.EnsureOaDocDefaultCategories(tid)
|
|
|
|
if currentCategoryID > 0 {
|
|
n, err := models.Orm.QueryTable(new(models.OaDocCategory)).
|
|
Filter("tid", tid).
|
|
Filter("id", currentCategoryID).
|
|
Filter("is_deleted", 0).Count()
|
|
if err == nil && n > 0 {
|
|
return currentCategoryID, nil
|
|
}
|
|
}
|
|
|
|
rootID, err := crmProjectDocRootCategory(tid)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
var sameName []models.OaDocCategory
|
|
if _, err := models.Orm.QueryTable(new(models.OaDocCategory)).
|
|
Filter("tid", tid).
|
|
Filter("parent_id", rootID).
|
|
Filter("scope", models.DocScopeShared).
|
|
Filter("user_id", 0).
|
|
Filter("name", projectName).
|
|
Filter("is_deleted", 0).
|
|
OrderBy("id").All(&sameName); err != nil && err != orm.ErrNoRows {
|
|
return 0, err
|
|
}
|
|
for _, c := range sameName {
|
|
bound, err := crmProjectDocCategoryBound(tid, c.ID, projectID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if !bound {
|
|
return c.ID, crmProjectDocBindCategory(tid, projectID, c.ID)
|
|
}
|
|
}
|
|
|
|
item := &models.OaDocCategory{
|
|
Tid: tid,
|
|
UserID: 0,
|
|
ParentID: rootID,
|
|
Name: projectName,
|
|
Remark: "项目文档:" + projectName,
|
|
Sort: 0,
|
|
IsSystem: models.DocCategoryNormal,
|
|
Scope: models.DocScopeShared,
|
|
IsDeleted: 0,
|
|
}
|
|
id, err := models.Orm.Insert(item)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
categoryID := uint64(id)
|
|
return categoryID, crmProjectDocBindCategory(tid, projectID, categoryID)
|
|
}
|
|
|
|
func RenameCrmProjectDocCategory(tid int, categoryID uint64, newName string) error {
|
|
newName = strings.TrimSpace(newName)
|
|
if models.Orm == nil || tid <= 0 || categoryID == 0 || newName == "" {
|
|
return nil
|
|
}
|
|
var item models.OaDocCategory
|
|
if err := models.Orm.QueryTable(new(models.OaDocCategory)).
|
|
Filter("tid", tid).Filter("id", categoryID).
|
|
Filter("is_deleted", 0).One(&item); err != nil {
|
|
return ErrDocCategoryNotFound
|
|
}
|
|
if item.Name == newName {
|
|
return nil
|
|
}
|
|
now := time.Now()
|
|
_, err := models.Orm.QueryTable(new(models.OaDocCategory)).
|
|
Filter("id", categoryID).Filter("tid", tid).
|
|
Update(orm.Params{"name": newName, "update_time": now})
|
|
return err
|
|
}
|
|
|
|
func CrmProjectDocCategoryNameMap(tid int) map[uint64]string {
|
|
result := make(map[uint64]string)
|
|
if models.Orm == nil || tid <= 0 {
|
|
return result
|
|
}
|
|
var list []models.OaDocCategory
|
|
if _, err := models.Orm.QueryTable(new(models.OaDocCategory)).
|
|
Filter("tid", tid).Filter("is_deleted", 0).All(&list); err != nil && err != orm.ErrNoRows {
|
|
return result
|
|
}
|
|
for _, c := range list {
|
|
result[c.ID] = c.Name
|
|
}
|
|
return result
|
|
}
|
|
|
|
func CrmProjectDocCategoryInScope(a OaDocActor, rootID, categoryID uint64) bool {
|
|
if rootID == 0 || categoryID == 0 {
|
|
return false
|
|
}
|
|
if categoryID == rootID {
|
|
return true
|
|
}
|
|
children, err := OaDocCategoryDescendants(a, rootID)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
for _, id := range children {
|
|
if id == categoryID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
type ProjectDocFolder struct {
|
|
ID uint64 `json:"id"`
|
|
ParentID uint64 `json:"parent_id"`
|
|
Name string `json:"name"`
|
|
DocCount int64 `json:"doc_count"`
|
|
Children []*ProjectDocFolder `json:"children"`
|
|
}
|
|
|
|
func CrmProjectDocFolderTree(a OaDocActor, rootID uint64) ([]*ProjectDocFolder, error) {
|
|
empty := []*ProjectDocFolder{}
|
|
if rootID == 0 {
|
|
return empty, nil
|
|
}
|
|
var list []models.OaDocCategory
|
|
if _, err := models.Orm.QueryTable(new(models.OaDocCategory)).
|
|
Filter("tid", a.Tid).Filter("is_deleted", 0).OrderBy("id").All(&list); err != nil && err != orm.ErrNoRows {
|
|
return nil, err
|
|
}
|
|
nodes := make(map[uint64]*ProjectDocFolder, len(list))
|
|
for i := range list {
|
|
if list[i].Scope != models.DocScopeShared || list[i].UserID != 0 {
|
|
continue
|
|
}
|
|
nodes[list[i].ID] = &ProjectDocFolder{
|
|
ID: list[i].ID,
|
|
ParentID: list[i].ParentID,
|
|
Name: list[i].Name,
|
|
Children: []*ProjectDocFolder{},
|
|
}
|
|
}
|
|
root, ok := nodes[rootID]
|
|
if !ok {
|
|
return empty, nil
|
|
}
|
|
for i := range list {
|
|
node, ok := nodes[list[i].ID]
|
|
if !ok || node.ID == rootID {
|
|
continue
|
|
}
|
|
if parent, ok := nodes[list[i].ParentID]; ok {
|
|
parent.Children = append(parent.Children, node)
|
|
}
|
|
}
|
|
if counts, err := OaDocCountByCategory(a, models.DocScopeShared, -1); err == nil {
|
|
for id, cnt := range counts {
|
|
if n, ok := nodes[id]; ok {
|
|
n.DocCount = cnt
|
|
}
|
|
}
|
|
}
|
|
return []*ProjectDocFolder{root}, nil
|
|
}
|
|
|
|
func crmProjectDocRootCategory(tid int) (uint64, error) {
|
|
var root models.OaDocCategory
|
|
err := models.Orm.QueryTable(new(models.OaDocCategory)).
|
|
Filter("tid", tid).
|
|
Filter("scope", models.DocScopeShared).
|
|
Filter("is_system", models.DocCategorySystem).
|
|
Filter("is_deleted", 0).
|
|
OrderBy("id").One(&root)
|
|
if err == nil {
|
|
return root.ID, nil
|
|
}
|
|
id, insertErr := models.Orm.Insert(&models.OaDocCategory{
|
|
Tid: tid,
|
|
UserID: 0,
|
|
ParentID: 0,
|
|
Name: models.DocCategoryNameProject,
|
|
Sort: 0,
|
|
Remark: "系统内置分类,不可删除",
|
|
IsSystem: models.DocCategorySystem,
|
|
Scope: models.DocScopeShared,
|
|
IsDeleted: 0,
|
|
})
|
|
if insertErr != nil {
|
|
return 0, insertErr
|
|
}
|
|
return uint64(id), nil
|
|
}
|
|
|
|
func crmProjectDocCategoryBound(tid int, categoryID, excludeProjectID uint64) (bool, error) {
|
|
qs := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
|
Filter("tenant_id", strconv.Itoa(tid)).
|
|
Filter("doc_category_id", categoryID).
|
|
Filter("delete_time__isnull", true)
|
|
if excludeProjectID > 0 {
|
|
qs = qs.Exclude("id", excludeProjectID)
|
|
}
|
|
n, err := qs.Count()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return n > 0, nil
|
|
}
|
|
|
|
func crmProjectDocBindCategory(tid int, projectID, categoryID uint64) error {
|
|
if projectID == 0 {
|
|
return nil
|
|
}
|
|
_, err := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
|
Filter("id", projectID).
|
|
Filter("tenant_id", strconv.Itoa(tid)).
|
|
Update(orm.Params{"doc_category_id": categoryID, "update_time": time.Now()})
|
|
return err
|
|
}
|