@@ -0,0 +1,133 @@
|
||||
package imadser
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"91porn-server/app/service/adser"
|
||||
"91porn-server/common/enum/imad"
|
||||
)
|
||||
|
||||
const maxAdsPerPosition = 10
|
||||
|
||||
type IMAdItem struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Cover string `json:"cover"`
|
||||
Href string `json:"href"`
|
||||
Position string `json:"position"`
|
||||
PositionName string `json:"positionName"`
|
||||
SortCode int `json:"sortCode"`
|
||||
CoverImgSize string `json:"coverImgSize"`
|
||||
WatchTime int `json:"watchTime"`
|
||||
}
|
||||
|
||||
type AdPositionGroup struct {
|
||||
Position string `json:"position"`
|
||||
PositionName string `json:"positionName"`
|
||||
List []IMAdItem `json:"list"`
|
||||
}
|
||||
|
||||
func GetAdsByPositionCode(positionCode string) (positionName string, list []IMAdItem) {
|
||||
slots, err := adser.JtAdvertiseThreeServer()
|
||||
if err != nil {
|
||||
return "", []IMAdItem{}
|
||||
}
|
||||
return SelectAdsByPositionCode(slots, positionCode)
|
||||
}
|
||||
|
||||
func GetAdsByPositionCodes(positionCodes []string) []AdPositionGroup {
|
||||
slots, err := adser.JtAdvertiseThreeServer()
|
||||
if err != nil {
|
||||
return []AdPositionGroup{}
|
||||
}
|
||||
return SelectAdsByPositionCodes(slots, positionCodes)
|
||||
}
|
||||
|
||||
func SelectAdsByPositionCode(slots []adser.AdSlot, positionCode string) (string, []IMAdItem) {
|
||||
positionCode = strings.TrimSpace(positionCode)
|
||||
matches := make([]IMAdItem, 0)
|
||||
positionName := ""
|
||||
for _, slot := range slots {
|
||||
if strings.TrimSpace(slot.AdvertiseLocationCode) != positionCode {
|
||||
continue
|
||||
}
|
||||
positionName = slot.AdvertiseLocationName
|
||||
for _, ad := range slot.AdDetailInfoList {
|
||||
extra := ad.GetExtraData()
|
||||
matches = append(matches, IMAdItem{
|
||||
ID: ad.AdvertiseCode,
|
||||
Title: ad.AdvertiseName,
|
||||
Description: ad.AdvertiseDesc,
|
||||
Cover: ad.GetCoverLsj(),
|
||||
Href: ad.GetRealLink(nil, nil),
|
||||
Position: positionCode,
|
||||
PositionName: slot.AdvertiseLocationName,
|
||||
SortCode: ad.Sort,
|
||||
CoverImgSize: extra.CoverImgSize,
|
||||
WatchTime: extra.WatchTime,
|
||||
})
|
||||
}
|
||||
}
|
||||
sort.SliceStable(matches, func(i, j int) bool {
|
||||
return matches[i].SortCode < matches[j].SortCode
|
||||
})
|
||||
if len(matches) > maxAdsPerPosition {
|
||||
matches = matches[:maxAdsPerPosition]
|
||||
}
|
||||
return positionName, matches
|
||||
}
|
||||
|
||||
func SelectAdsByPositionCodes(slots []adser.AdSlot, positionCodes []string) []AdPositionGroup {
|
||||
normalized := normalizePositionCodes(positionCodes)
|
||||
groups := make([]AdPositionGroup, 0, len(normalized))
|
||||
for _, positionCode := range normalized {
|
||||
positionName, list := SelectAdsByPositionCode(slots, positionCode)
|
||||
groups = append(groups, AdPositionGroup{
|
||||
Position: positionCode,
|
||||
PositionName: positionName,
|
||||
List: list,
|
||||
})
|
||||
}
|
||||
return groups
|
||||
}
|
||||
|
||||
func ListIMAdPositionCodesWithAds() []string {
|
||||
slots, err := adser.JtAdvertiseThreeServer()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
positions := make([]string, 0)
|
||||
seen := make(map[string]struct{})
|
||||
for _, slot := range slots {
|
||||
code := strings.TrimSpace(slot.AdvertiseLocationCode)
|
||||
if !imad.IsPositionCode(code) || len(slot.AdDetailInfoList) == 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[code]; ok {
|
||||
continue
|
||||
}
|
||||
seen[code] = struct{}{}
|
||||
positions = append(positions, code)
|
||||
}
|
||||
sort.Strings(positions)
|
||||
return positions
|
||||
}
|
||||
|
||||
func normalizePositionCodes(positionCodes []string) []string {
|
||||
seen := make(map[string]struct{}, len(positionCodes))
|
||||
normalized := make([]string, 0, len(positionCodes))
|
||||
for _, positionCode := range positionCodes {
|
||||
positionCode = strings.TrimSpace(positionCode)
|
||||
if positionCode == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[positionCode]; ok {
|
||||
continue
|
||||
}
|
||||
seen[positionCode] = struct{}{}
|
||||
normalized = append(normalized, positionCode)
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
Reference in New Issue
Block a user