package services import ( "eta_gn/eta_api/models" "eta_gn/eta_api/utils" "fmt" "sync" ) // GetPptReportClassifyTreeRecursive 递归获取ppt报告分类树 func GetPptReportClassifyTreeRecursive(list []*models.Classify, parentId int, classifyPpt map[int][]*models.PptReportItem) []*models.PptReportClassifyItem { res := make([]*models.PptReportClassifyItem, 0) for _, v := range list { if v.ParentId == parentId { t := new(models.PptReportClassifyItem) t.Id = v.Id t.ClassifyName = v.ClassifyName t.Sort = v.Sort t.ParentId = v.ParentId t.Enabled = v.Enabled t.Level = v.Level t.HasChild = v.HasChild t.ClassifyType = v.ClassifyType t.ReportNum = v.ReportNum t.PptList = classifyPpt[v.Id] t.Child = GetPptReportClassifyTreeRecursive(list, v.Id, classifyPpt) res = append(res, t) } } return res } func RecursiveFilterPptNoChildTreeClassify(list []*models.PptReportClassifyItem) []*models.PptReportClassifyItem { res := make([]*models.PptReportClassifyItem, 0) for _, v := range list { v.Child = RecursiveFilterPptNoChildTreeClassify(v.Child) if len(v.Child) == 0 && v.HasChild == 1 { continue } if len(v.Child) == 0 { v.Child = nil } res = append(res, v) } return res } // 更新分类报告计数加个锁 var classifyReportNumLock sync.Mutex // UpdateClassifyReportNum 更新分类报告计数 func UpdateClassifyReportNum(classifyId int) (err error) { classifyReportNumLock.Lock() defer func() { if err != nil { utils.FileLog.Info(fmt.Sprintf("更新分类报告计数失败, %v", err)) } classifyReportNumLock.Unlock() }() classifyItem, e := models.GetClassifyById(classifyId) if e != nil { err = fmt.Errorf("获取分类失败, %v", e) return } // 更新分类报告数 var total int { reportOb := new(models.Report) var cond string switch classifyItem.Level { case 1: cond += ` AND classify_id_first = ?` case 2: cond += ` AND classify_id_second = ?` case 3: cond += ` AND classify_id_third = ?` } pars := make([]interface{}, 0) pars = append(pars, classifyId) count, e := reportOb.GetCountByCondition(cond, pars) if e != nil { err = fmt.Errorf("获取报告计数失败, %v", e) return } total += count } { pptOb := new(models.PptV2) cond := ` AND classify_id = ?` pars := make([]interface{}, 0) pars = append(pars, classifyId) count, e := pptOb.GetCountByCondition(cond, pars) if e != nil { err = fmt.Errorf("获取PPT报告计数失败, %v", e) return } total += count } classifyItem.ReportNum = total if e = classifyItem.UpdateClassify([]string{"ReportNum"}); e != nil { err = fmt.Errorf("更新分类报告计数失败, %v", e) return } // 获取所有分类, 更新父级, 无父级忽略 if classifyItem.ParentId <= 0 { return } classifyOb := new(models.Classify) classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "") if e != nil { err = fmt.Errorf("获取报告列表失败, %v", e) return } classifyIdMap := make(map[int]*models.Classify) classifyParentChildMap := make(map[int][]*models.Classify) for _, v := range classifies { classifyIdMap[v.Id] = v if v.ParentId <= 0 { continue } if classifyParentChildMap[v.ParentId] == nil { classifyParentChildMap[v.ParentId] = make([]*models.Classify, 0) } classifyParentChildMap[v.ParentId] = append(classifyParentChildMap[v.ParentId], v) } // 递归获取需要更新的父级报告数 updateClassifies := CountParentClassifyReportNumRecursive(classifies, classifyItem.ParentId, classifyIdMap, classifyParentChildMap) if len(updateClassifies) == 0 { return } for _, v := range updateClassifies { if e = v.UpdateClassify([]string{"ReportNum"}); e != nil { err = fmt.Errorf("更新父级分类报告计数失败, %v", e) return } } return } // CountParentClassifyReportNumRecursive 递归统计父级分类报告数 func CountParentClassifyReportNumRecursive(list []*models.Classify, parentId int, classifyIdMap map[int]*models.Classify, classifyParentChildMap map[int][]*models.Classify) []*models.Classify { res := make([]*models.Classify, 0) for _, v := range list { // 找父级 if v.Id != parentId { continue } parentItem := classifyIdMap[v.Id] if parentItem == nil { break } // 合计所有直系子分类报告数 children := classifyParentChildMap[v.Id] if len(children) == 0 { break } var t int for _, child := range children { t += child.ReportNum } parentItem.ReportNum = t res = append(res, parentItem) // 继续向上统计父级 if v.ParentId <= 0 { break } parents := CountParentClassifyReportNumRecursive(list, v.ParentId, classifyIdMap, classifyParentChildMap) if len(parents) == 0 { break } res = append(res, parents...) } return res }