두 가지 사고방식: (1) 먼저 태그를 찾아보고platetag(2)연표분조부례: 서비스와dao층(두 가지 실현)

9042 단어 go
//    -tag  
func GetTagList(req *request.TagListCurrentReq) (*response.TagItemListResp, error) {
	tags, total, err := store.TagMysql.GetTagList(req)
	if err != nil {
		logrus.Errorf("GetTagList error: %v, value: %v", err, tags)
	}

	//        
	plateApi := client.NewPlateAPI()
	//      :100  
	ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
	isIncludeUnCommendPlates := int32(1)
	plates, _, err := plateApi.AllPlateInternal(ctx, isIncludeUnCommendPlates)
	if err != nil {
		logrus.Errorf("AllPlateInternal err: %v", err)
	}
	plateMap := make(map[string]*plate.ResponsePlateMainDataResp, 0)
	for _, plateItem := range plates {
		plateCurrent := plateItem
		plateMap[*plateCurrent.Id] = &plateCurrent
	}
	logrus.Infof("plateMap len: %v, content: %v", len(plateMap), plateMap)

	//  plateName
	funcGetPlateName := func(plateID int64) string {
		if plateName, ok := plateMap[utils.SNToID(plateID)]; ok {
			return *plateName.Name
		} else {
			return "     "
		}
	}

	//  plateName
	tagsCurrent := make([]*response.TagListItemResp, 0)
	num := 0
	for i := range tags {
		if i != 0 && tags[i].ID == tags[i-1].ID {
			tagsCurrent[num-1].TagPlateNames = append(tagsCurrent[num-1].TagPlateNames, &response.TagPlateName{
				PlateID:   utils.SNToID(tags[i].PlateID),
				PlateName: funcGetPlateName(tags[i].PlateID),
			})

			total--
			continue
		}

		tagPlateNames := make([]*response.TagPlateName, 0)
		tagPlateNames = append(tagPlateNames, &response.TagPlateName{
			PlateID:   utils.SNToID(tags[i].PlateID),
			PlateName: funcGetPlateName(tags[i].PlateID),
		})

		tagsCurrent = append(tagsCurrent, &response.TagListItemResp{
			Tag:           tags[i].Tag,
			TagPlateNames: tagPlateNames,
		})

		num++
	}

	//  
	if req.Offset < 0 || req.Limit < 0 {
		return &response.TagItemListResp{
			TagList: make([]*response.TagListItemResp, 0),
			Total:   total,
		}, nil
	}
	if req.Limit == 0 {
		req.Limit = 20
	}
	if req.Offset+req.Limit <= int64(len(tagsCurrent)) {
		tagsCurrent = tagsCurrent[req.Offset : req.Offset+req.Limit]
	} else if req.Offset < int64(len(tagsCurrent)) {
		tagsCurrent = tagsCurrent[req.Offset:int64(len(tagsCurrent))]
	} else {
		tagsCurrent = make([]*response.TagListItemResp, 0)
	}

	return &response.TagItemListResp{
		TagList: tagsCurrent,
		Total:   total,
	}, nil
}

//    -      
func GetTagArticleList(req *request.ArticleTagReq) (*response.TagArticleListResp, error) {
	tagArticles, total, err := store.TagMysql.GetTagArticleList(req)
	if err != nil {
		logrus.Errorf("GetTagArticleList error: %v", err)
	}

	//  tags
	tags, _, err := store.TagMysql.GetTagList(&request.TagListCurrentReq{})
	if err != nil {
		logrus.Errorf("GetTagList error: %v", err)
	}
	tagsMap := make(map[int64]string)
	for _, item := range tags {
		tagsMap[item.ID] = item.Title
	}

	tagArticleListItemResp := make([]*response.TagArticleListItemResp, 0)

	for _, item := range tagArticles {
		tagName := ""

		//  tag title
		if tagTitle, ok := tagsMap[item.TagID]; ok {
			tagName = tagTitle
		} else {
			//     
			total--
			continue
		}

		tagArticleListItemResp = append(tagArticleListItemResp, &response.TagArticleListItemResp{
			TagArticle: item,
			TagName:    tagName,
		})
	}

	return &response.TagArticleListResp{
		TagArticleList: tagArticleListItemResp,
		Total:          total,
	}, nil
}
//    -tag  
func GetTagList(req *request.TagListCurrentReq) (*response.TagItemListResp, error) {
	tags, total, err := store.TagMysql.GetTagListWithGroup(req)
	if err != nil {
		logrus.Errorf("GetTagListWithGroup error: %v, value: %v", err, tags)
	}

	//        
	plateApi := client.NewPlateAPI()
	//      :500  
	ctx, _ := context.WithTimeout(context.Background(), 500*time.Millisecond)
	isIncludeUnCommendPlates := int32(1)
	plates, _, err := plateApi.AllPlateInternal(ctx, isIncludeUnCommendPlates)
	if err != nil {
		logrus.Errorf("AllPlateInternal err: %v", err)
	}
	plateMap := make(map[string]*plate.ResponsePlateMainDataResp, 0)
	for _, plateItem := range plates {
		plateCurrent := plateItem
		plateMap[*plateCurrent.Id] = &plateCurrent
	}
	logrus.Infof("plateMap len: %v, content: %v", len(plateMap), plateMap)

	//  plateName
	funcGetPlateName := func(plateID int64) string {
		if plateName, ok := plateMap[utils.SNToID(plateID)]; ok {
			return *plateName.Name
		} else {
			return "     "
		}
	}

	//  plateName
	tagsCurrent := make([]*response.TagListItemResp, 0)
	for _, item := range tags {
		var plateIDs []string
		if item.PlateID != "" {
			plateIDs = strings.Split(item.PlateID, ",")
		} else {
			plateIDs = []string{}
		}

		tagPlateNames := make([]*response.TagPlateName, 0)
		isExistMap := make(map[int64]string, 0)
		for _, plateIDItem := range plateIDs {
			plateIDItemInt, _ := strconv.ParseInt(plateIDItem, 10, 64)
			if _, ok := isExistMap[plateIDItemInt]; !ok {
				tagPlateNames = append(tagPlateNames, &response.TagPlateName{
					PlateID:   utils.SNToID(plateIDItemInt),
					PlateName: funcGetPlateName(plateIDItemInt),
				})

				isExistMap[plateIDItemInt] = "exist"
			}
		}

		tagsCurrent = append(tagsCurrent, &response.TagListItemResp{
			Tag:           item.Tag,
			TagPlateNames: tagPlateNames,
		})
	}

	return &response.TagItemListResp{
		TagList: tagsCurrent,
		Total:   total,
	}, nil
}

//  
func (s *tagMysqlStore) GetTagListWithGroup(req *request.TagListCurrentReq) ([]*response.TagListAdminItemRespWithGroup, int64, error) {
	tags := []*response.TagListAdminItemRespWithGroup{}

	var (
		total int64
		err   error
	)

	if req.PlateID == "" {
		if req.TagKeyword == "" {
			sql := "SELECT `tag`.*, GROUP_CONCAT(plate_tag.plate_id) as `plate_id` " +
				"FROM `tag` left join plate_tag " +
				"ON plate_tag.tag_id = tag.id AND `plate_tag`.`deleted_at` IS NULL " +
				"WHERE `tag`.`deleted_at` IS NULL " +
				"GROUP BY tag.id " +
				"ORDER BY `tag`.`created_at` DESC "
			total = s.db.Raw(sql).Scan(&tags).RowsAffected
			if req.Limit != 0 {
				err = s.db.Raw(sql+"limit ? offset ?", req.Limit, req.Offset).Scan(&tags).Error
			} else {
				err = s.db.Raw(sql).Scan(&tags).Error
			}
		} else {
			sql := "SELECT `tag`.*, GROUP_CONCAT(plate_tag.plate_id) as `plate_id` " +
				"FROM `tag` left join plate_tag " +
				"ON plate_tag.tag_id = tag.id AND `plate_tag`.`deleted_at` IS NULL " +
				"WHERE `tag`.`deleted_at` IS NULL " +
				"AND (`tag`.`title` like ? OR `tag`.`alias` like ?) " +
				"GROUP BY tag.id " +
				"ORDER BY `tag`.`created_at` DESC "
			total = s.db.Raw(sql, "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%").Scan(&tags).RowsAffected
			if req.Limit != 0 {
				err = s.db.Raw(sql+"limit ? offset ?", "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%", req.Limit, req.Offset).Scan(&tags).Error
			} else {
				err = s.db.Raw(sql, "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%").Scan(&tags).Error
			}
		}
	} else {
		if req.TagKeyword == "" {
			sql := "SELECT `tag`.*, GROUP_CONCAT(plate_tag.plate_id) as `plate_id` " +
				"FROM `tag` left join plate_tag " +
				"ON plate_tag.tag_id = tag.id AND `plate_tag`.`deleted_at` IS NULL " +
				"WHERE `tag`.`deleted_at` IS NULL " +
				"AND `plate_tag`.`plate_id` = ? " +
				"GROUP BY tag.id " +
				"ORDER BY `tag`.`created_at` DESC "
			total = s.db.Raw(sql, req.PlateID).Scan(&tags).RowsAffected
			if req.Limit != 0 {
				err = s.db.Raw(sql+"limit ? offset ?", req.PlateID, req.Limit, req.Offset).Scan(&tags).Error
			} else {
				err = s.db.Raw(sql, req.PlateID).Scan(&tags).Error
			}
		} else {
			sql := "SELECT `tag`.*, GROUP_CONCAT(plate_tag.plate_id) as `plate_id` " +
				"FROM `tag` left join plate_tag " +
				"ON plate_tag.tag_id = tag.id AND `plate_tag`.`deleted_at` IS NULL " +
				"WHERE `tag`.`deleted_at` IS NULL " +
				"AND `plate_tag`.`plate_id` = ? " +
				"AND (`tag`.`title` like ? OR `tag`.`alias` like ?) " +
				"GROUP BY tag.id " +
				"ORDER BY `tag`.`created_at` DESC "
			total = s.db.Raw(sql, req.PlateID, "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%").Scan(&tags).RowsAffected
			if req.Limit != 0 {
				err = s.db.Raw(sql+"limit ? offset ?", req.PlateID, "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%", req.Limit, req.Offset).Scan(&tags).Error
			} else {
				err = s.db.Raw(sql, req.PlateID, "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%").Scan(&tags).Error
			}
		}
	}

	return tags, total, err
}

좋은 웹페이지 즐겨찾기