update tag count

This commit is contained in:
aichy126 2023-05-17 12:11:59 +08:00
parent 25ccd8f717
commit 83b4dfe4f9
2 changed files with 91 additions and 7 deletions

View File

@ -62,7 +62,7 @@ var migrations = []Migration{
NewMigration("update user pin hide features", updateRolePinAndHideFeatures, true),
NewMigration("update question post time", updateQuestionPostTime, true),
NewMigration("add login limitations", addLoginLimitations, true),
NewMigration("update question answer count", updateQuestionCount, true),
NewMigration("update count", updateCount, true),
}
// GetCurrentDBVersion returns the current db version

View File

@ -4,10 +4,20 @@ import (
"fmt"
"github.com/answerdev/answer/internal/entity"
"github.com/davecgh/go-spew/spew"
"github.com/segmentfault/pacman/log"
"xorm.io/xorm"
)
func updateCount(x *xorm.Engine) error {
spew.Dump("update count")
// updateQuestionCount(x)
updateTagCount(x)
// updateUserQuestionCount(x)
// updateUserAnswerCount(x)
return nil
}
func updateQuestionCount(x *xorm.Engine) error {
//question answer count
answers := make([]entity.Answer, 0)
@ -40,11 +50,85 @@ func updateQuestionCount(x *xorm.Engine) error {
}
}
//tag question count
//user question count
//user answer count
return nil
}
// updateTagCount update tag count
func updateTagCount(x *xorm.Engine) error {
tagRelList := make([]entity.TagRel, 0)
err := x.Find(&tagRelList, &entity.TagRel{})
if err != nil {
return fmt.Errorf("get tag rel failed: %w", err)
}
questionIDs := make([]string, 0)
questionsAvailableMap := make(map[string]bool)
for _, item := range tagRelList {
questionIDs = append(questionIDs, item.ObjectID)
questionsAvailableMap[item.ObjectID] = false
}
questionList := make([]entity.Question, 0)
err = x.In("id", questionIDs).In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).Find(&questionList, &entity.Question{})
if err != nil {
return fmt.Errorf("get questions failed: %w", err)
}
for _, question := range questionList {
_, ok := questionsAvailableMap[question.ID]
if ok {
questionsAvailableMap[question.ID] = true
}
}
for id, ok := range questionsAvailableMap {
if !ok {
if _, err = x.Cols("status").Update(&entity.TagRel{Status: entity.TagRelStatusDeleted}, &entity.TagRel{ObjectID: id}); err != nil {
log.Errorf("update %+v config failed: %s", id, err)
}
}
}
//select tag count
newTagRelList := make([]entity.TagRel, 0)
err = x.Find(&newTagRelList, &entity.TagRel{Status: entity.TagRelStatusAvailable})
if err != nil {
return fmt.Errorf("get tag rel failed: %w", err)
}
tagCountMap := make(map[string]int)
for _, v := range newTagRelList {
_, ok := tagCountMap[v.TagID]
if !ok {
tagCountMap[v.TagID] = 1
} else {
tagCountMap[v.TagID]++
}
}
TagList := make([]entity.Tag, 0)
err = x.Find(&TagList, &entity.Tag{})
if err != nil {
return fmt.Errorf("get tag failed: %w", err)
}
for _, tag := range TagList {
_, ok := tagCountMap[tag.ID]
if ok {
tag.QuestionCount = tagCountMap[tag.ID]
if _, err = x.Update(tag, &entity.Tag{ID: tag.ID}); err != nil {
log.Errorf("update %+v tag failed: %s", tag.ID, err)
return fmt.Errorf("update tag failed: %w", err)
}
} else {
tag.QuestionCount = 0
if _, err = x.Update(tag, &entity.Tag{ID: tag.ID}); err != nil {
log.Errorf("update %+v tag failed: %s", tag.ID, err)
return fmt.Errorf("update tag failed: %w", err)
}
}
}
return nil
}
// updateUserQuestionCount update user question count
func updateUserQuestionCount(x *xorm.Engine) error {
return nil
}
// updateUserAnswerCount update user answer count
func updateUserAnswerCount(x *xorm.Engine) error {
return nil
}