func (this *UpgradeService) moveTag() {
	usnI := 1
	tags := []info.Tag{}
	db.ListByQ(db.Tags, bson.M{}, &tags)
	for _, eachTag := range tags {
		tagTitles := eachTag.Tags
		now := time.Now()
		if tagTitles != nil && len(tagTitles) > 0 {
			for _, tagTitle := range tagTitles {
				noteTag := info.NoteTag{}
				noteTag.TagId = bson.NewObjectId()
				noteTag.Count = 1
				noteTag.Tag = tagTitle
				noteTag.UserId = eachTag.UserId
				noteTag.CreatedTime = now
				noteTag.UpdatedTime = now
				noteTag.Usn = usnI
				noteTag.IsDeleted = false
				db.Insert(db.NoteTags, noteTag)
				usnI++
			}
		}
	}
}
示例#2
0
// 添加或更新标签, 先查下是否存在, 不存在则添加, 存在则更新
// 都要统计下tag的note数
// 什么时候调用? 笔记添加Tag, 删除Tag时
// 删除note时, 都可以调用
// 万能
func (this *TagService) AddOrUpdateTag(userId string, tag string) info.NoteTag {
	userIdO := bson.ObjectIdHex(userId)
	noteTag := info.NoteTag{}
	db.GetByQ(db.NoteTags, bson.M{"UserId": userIdO, "Tag": tag}, &noteTag)

	// 存在, 则更新之
	if noteTag.TagId != "" {
		// 统计note数
		count := noteService.CountNoteByTag(userId, tag)
		noteTag.Count = count
		noteTag.UpdatedTime = time.Now()
		//		noteTag.Usn = userService.IncrUsn(userId), 更新count而已
		db.UpdateByIdAndUserId(db.NoteTags, noteTag.TagId.Hex(), userId, noteTag)
		return noteTag
	}

	// 不存在, 则创建之
	noteTag.TagId = bson.NewObjectId()
	noteTag.Count = 1
	noteTag.Tag = tag
	noteTag.UserId = bson.ObjectIdHex(userId)
	noteTag.CreatedTime = time.Now()
	noteTag.UpdatedTime = noteTag.CreatedTime
	noteTag.Usn = userService.IncrUsn(userId)
	noteTag.IsDeleted = false
	db.Insert(db.NoteTags, noteTag)

	return noteTag
}