162 lines
4.8 KiB
Go
162 lines
4.8 KiB
Go
package baishun
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type adminCatalogRow struct {
|
|
VendorGameID int
|
|
Profile string
|
|
InternalGameID string
|
|
Name string
|
|
Cover string
|
|
PreviewURL string
|
|
DownloadURL string
|
|
PackageVersion string
|
|
GSP string
|
|
Status string
|
|
SafeHeight int
|
|
Orientation *int
|
|
AddedConfigID *int64
|
|
Showcase *bool
|
|
UpdateTime string
|
|
}
|
|
|
|
func (s *BaishunService) PageCatalog(
|
|
ctx context.Context,
|
|
sysOrigin string,
|
|
profile string,
|
|
keyword string,
|
|
cursor int,
|
|
limit int,
|
|
) (*AdminBaishunCatalogPageResponse, error) {
|
|
sysOrigin = normalizeAdminSysOrigin(sysOrigin)
|
|
profile, err := s.requestProfile(ctx, sysOrigin, profile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cursor = normalizePageCursor(cursor)
|
|
limit = normalizePageLimit(limit)
|
|
|
|
countQuery := s.buildAdminCatalogQuery(ctx, sysOrigin, profile, keyword)
|
|
var total int64
|
|
if err := countQuery.Count(&total).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var rows []adminCatalogRow
|
|
err = s.buildAdminCatalogQuery(ctx, sysOrigin, profile, keyword).
|
|
Select(`
|
|
cat.vendor_game_id AS vendor_game_id,
|
|
cat.profile AS profile,
|
|
cat.internal_game_id AS internal_game_id,
|
|
cat.name AS name,
|
|
cat.cover AS cover,
|
|
cat.preview_url AS preview_url,
|
|
cat.download_url AS download_url,
|
|
cat.package_version AS package_version,
|
|
cat.gsp AS gsp,
|
|
cat.status AS status,
|
|
cat.safe_height AS safe_height,
|
|
cat.orientation AS orientation,
|
|
cfg.id AS added_config_id,
|
|
cfg.is_showcase AS showcase,
|
|
DATE_FORMAT(cat.update_time, '%Y-%m-%d %H:%i:%s') AS update_time
|
|
`).
|
|
Order("cat.vendor_game_id ASC").
|
|
Limit(limit).
|
|
Offset((cursor - 1) * limit).
|
|
Scan(&rows).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
items := make([]AdminBaishunCatalogItem, 0, len(rows))
|
|
for _, row := range rows {
|
|
items = append(items, AdminBaishunCatalogItem{
|
|
VendorGameID: row.VendorGameID,
|
|
Profile: normalizeBaishunProfile(row.Profile),
|
|
GameID: row.InternalGameID,
|
|
Name: row.Name,
|
|
Cover: row.Cover,
|
|
PreviewURL: row.PreviewURL,
|
|
DownloadURL: row.DownloadURL,
|
|
PackageVersion: row.PackageVersion,
|
|
GSP: row.GSP,
|
|
Status: row.Status,
|
|
SafeHeight: row.SafeHeight,
|
|
Orientation: derefInt(row.Orientation),
|
|
Added: row.AddedConfigID != nil && *row.AddedConfigID > 0,
|
|
Showcase: row.Showcase != nil && *row.Showcase,
|
|
UpdateTime: row.UpdateTime,
|
|
})
|
|
}
|
|
|
|
return &AdminBaishunCatalogPageResponse{
|
|
Cursor: cursor,
|
|
Limit: limit,
|
|
Records: items,
|
|
Total: total,
|
|
}, nil
|
|
}
|
|
|
|
func (s *BaishunService) FetchAdminCatalog(ctx context.Context, req SyncCatalogRequest) (*SyncCatalogResponse, error) {
|
|
return s.SyncCatalog(ctx, req)
|
|
}
|
|
|
|
func (s *BaishunService) buildAdminCatalogQuery(ctx context.Context, sysOrigin string, profile string, keyword string) *gorm.DB {
|
|
query := s.repo.DB.WithContext(ctx).
|
|
Table("baishun_game_catalog AS cat").
|
|
Joins(`
|
|
LEFT JOIN sys_game_list_vendor_ext ext
|
|
ON ext.sys_origin = cat.sys_origin
|
|
AND ext.profile = cat.profile
|
|
AND ext.vendor_type = ?
|
|
AND ext.vendor_game_id = CAST(cat.vendor_game_id AS CHAR)
|
|
AND ext.enabled = 1
|
|
`, baishunVendorType).
|
|
Joins(`
|
|
LEFT JOIN sys_game_list_config cfg
|
|
ON cfg.id = ext.game_list_config_id
|
|
AND cfg.sys_origin = cat.sys_origin
|
|
AND cfg.game_origin = ?
|
|
`, baishunVendorType).
|
|
Where("cat.sys_origin = ? AND cat.profile = ?", sysOrigin, profile)
|
|
keyword = strings.TrimSpace(keyword)
|
|
if keyword != "" {
|
|
like := "%" + keyword + "%"
|
|
query = query.Where(`
|
|
cat.name LIKE ?
|
|
OR cat.internal_game_id LIKE ?
|
|
OR CAST(cat.vendor_game_id AS CHAR) LIKE ?
|
|
`, like, like, like)
|
|
}
|
|
return query
|
|
}
|
|
|
|
func (s *BaishunService) requireProviderConfig(ctx context.Context, sysOrigin string) (baishunRuntimeConfig, error) {
|
|
runtimeCfg, err := s.resolveRuntimeConfig(ctx, sysOrigin)
|
|
if err != nil {
|
|
return baishunRuntimeConfig{}, err
|
|
}
|
|
if strings.TrimSpace(runtimeCfg.PlatformBaseURL) == "" || runtimeCfg.AppID <= 0 || strings.TrimSpace(runtimeCfg.AppKey) == "" {
|
|
return baishunRuntimeConfig{}, NewAppError(http.StatusBadRequest, "baishun_config_missing", "baishun provider config is missing")
|
|
}
|
|
return runtimeCfg, nil
|
|
}
|
|
|
|
func (s *BaishunService) requireProviderConfigForProfile(ctx context.Context, sysOrigin string, profile string) (baishunRuntimeConfig, error) {
|
|
runtimeCfg, err := s.resolveRuntimeConfigForProfile(ctx, sysOrigin, profile)
|
|
if err != nil {
|
|
return baishunRuntimeConfig{}, err
|
|
}
|
|
if strings.TrimSpace(runtimeCfg.PlatformBaseURL) == "" || runtimeCfg.AppID <= 0 || strings.TrimSpace(runtimeCfg.AppKey) == "" {
|
|
return baishunRuntimeConfig{}, NewAppError(http.StatusBadRequest, "baishun_config_missing", "baishun provider config is missing")
|
|
}
|
|
return runtimeCfg, nil
|
|
}
|