460 lines
13 KiB
Go
460 lines
13 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
type DemoBootstrapSummary struct {
|
|
TenantCode string `json:"tenantCode"`
|
|
ChannelCode string `json:"channelCode"`
|
|
EventID string `json:"eventId"`
|
|
ReleaseID string `json:"releaseId"`
|
|
SourceID string `json:"sourceId"`
|
|
BuildID string `json:"buildId"`
|
|
CardID string `json:"cardId"`
|
|
VariantManualEventID string `json:"variantManualEventId"`
|
|
VariantManualRelease string `json:"variantManualReleaseId"`
|
|
VariantManualCardID string `json:"variantManualCardId"`
|
|
}
|
|
|
|
func (s *Store) EnsureDemoData(ctx context.Context) (*DemoBootstrapSummary, error) {
|
|
tx, err := s.Begin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
|
|
var tenantID string
|
|
if err := tx.QueryRow(ctx, `
|
|
INSERT INTO tenants (tenant_code, name, status)
|
|
VALUES ('tenant_demo', 'Demo Tenant', 'active')
|
|
ON CONFLICT (tenant_code) DO UPDATE SET
|
|
name = EXCLUDED.name,
|
|
status = EXCLUDED.status
|
|
RETURNING id
|
|
`).Scan(&tenantID); err != nil {
|
|
return nil, fmt.Errorf("ensure demo tenant: %w", err)
|
|
}
|
|
|
|
var channelID string
|
|
if err := tx.QueryRow(ctx, `
|
|
INSERT INTO entry_channels (
|
|
tenant_id, channel_code, channel_type, platform_app_id, display_name, status, is_default
|
|
)
|
|
VALUES ($1, 'mini-demo', 'wechat_mini', 'wx-demo-appid', 'Demo Mini Channel', 'active', true)
|
|
ON CONFLICT (tenant_id, channel_code) DO UPDATE SET
|
|
channel_type = EXCLUDED.channel_type,
|
|
platform_app_id = EXCLUDED.platform_app_id,
|
|
display_name = EXCLUDED.display_name,
|
|
status = EXCLUDED.status,
|
|
is_default = EXCLUDED.is_default
|
|
RETURNING id
|
|
`, tenantID).Scan(&channelID); err != nil {
|
|
return nil, fmt.Errorf("ensure demo entry channel: %w", err)
|
|
}
|
|
|
|
var eventID string
|
|
if err := tx.QueryRow(ctx, `
|
|
INSERT INTO events (
|
|
tenant_id, event_public_id, slug, display_name, summary, status
|
|
)
|
|
VALUES ($1, 'evt_demo_001', 'demo-city-run', 'Demo City Run', 'Launch flow demo event', 'active')
|
|
ON CONFLICT (event_public_id) DO UPDATE SET
|
|
tenant_id = EXCLUDED.tenant_id,
|
|
slug = EXCLUDED.slug,
|
|
display_name = EXCLUDED.display_name,
|
|
summary = EXCLUDED.summary,
|
|
status = EXCLUDED.status
|
|
RETURNING id
|
|
`, tenantID).Scan(&eventID); err != nil {
|
|
return nil, fmt.Errorf("ensure demo event: %w", err)
|
|
}
|
|
|
|
var releaseRow struct {
|
|
ID string
|
|
PublicID string
|
|
}
|
|
if err := tx.QueryRow(ctx, `
|
|
INSERT INTO event_releases (
|
|
release_public_id,
|
|
event_id,
|
|
release_no,
|
|
config_label,
|
|
manifest_url,
|
|
manifest_checksum_sha256,
|
|
route_code,
|
|
status
|
|
)
|
|
VALUES (
|
|
'rel_demo_001',
|
|
$1,
|
|
1,
|
|
'Demo Config v1',
|
|
'https://oss-mbh5.colormaprun.com/gotomars/event/releases/evt_demo_001/rel_e7dd953743c5c0d2/manifest.json',
|
|
'demo-checksum-001',
|
|
'route-demo-001',
|
|
'published'
|
|
)
|
|
ON CONFLICT (release_public_id) DO UPDATE SET
|
|
event_id = EXCLUDED.event_id,
|
|
config_label = EXCLUDED.config_label,
|
|
manifest_url = EXCLUDED.manifest_url,
|
|
manifest_checksum_sha256 = EXCLUDED.manifest_checksum_sha256,
|
|
route_code = EXCLUDED.route_code,
|
|
status = EXCLUDED.status
|
|
RETURNING id, release_public_id
|
|
`, eventID).Scan(&releaseRow.ID, &releaseRow.PublicID); err != nil {
|
|
return nil, fmt.Errorf("ensure demo release: %w", err)
|
|
}
|
|
|
|
if _, err := tx.Exec(ctx, `
|
|
UPDATE events
|
|
SET current_release_id = $2
|
|
WHERE id = $1
|
|
`, eventID, releaseRow.ID); err != nil {
|
|
return nil, fmt.Errorf("attach demo release: %w", err)
|
|
}
|
|
|
|
sourceNotes := "demo source config imported from local event sample"
|
|
source, err := s.UpsertEventConfigSource(ctx, tx, UpsertEventConfigSourceParams{
|
|
EventID: eventID,
|
|
SourceVersionNo: 1,
|
|
SourceKind: "event_bundle",
|
|
SchemaID: "event-source",
|
|
SchemaVersion: "1",
|
|
Status: "active",
|
|
Notes: &sourceNotes,
|
|
Source: map[string]any{
|
|
"app": map[string]any{
|
|
"id": "sample-classic-001",
|
|
"title": "顺序赛示例",
|
|
},
|
|
"branding": map[string]any{
|
|
"tenantCode": "tenant_demo",
|
|
"entryChannel": "mini-demo",
|
|
},
|
|
"map": map[string]any{
|
|
"tiles": "../map/lxcb-001/tiles/",
|
|
"mapmeta": "../map/lxcb-001/tiles/meta.json",
|
|
},
|
|
"playfield": map[string]any{
|
|
"kind": "course",
|
|
"source": map[string]any{
|
|
"type": "kml",
|
|
"url": "../kml/lxcb-001/10/c01.kml",
|
|
},
|
|
},
|
|
"game": map[string]any{
|
|
"mode": "classic-sequential",
|
|
},
|
|
"content": map[string]any{
|
|
"h5Template": "content-h5-test-template.html",
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ensure demo event config source: %w", err)
|
|
}
|
|
|
|
buildLog := "demo build generated from sample classic-sequential.json"
|
|
build, err := s.UpsertEventConfigBuild(ctx, tx, UpsertEventConfigBuildParams{
|
|
EventID: eventID,
|
|
SourceID: source.ID,
|
|
BuildNo: 1,
|
|
BuildStatus: "success",
|
|
BuildLog: &buildLog,
|
|
Manifest: map[string]any{
|
|
"schemaVersion": "1",
|
|
"releaseId": "rel_demo_001",
|
|
"version": "2026.04.01",
|
|
"app": map[string]any{
|
|
"id": "sample-classic-001",
|
|
"title": "顺序赛示例",
|
|
},
|
|
"map": map[string]any{
|
|
"tiles": "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/",
|
|
"mapmeta": "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/meta.json",
|
|
},
|
|
"playfield": map[string]any{
|
|
"kind": "course",
|
|
"source": map[string]any{
|
|
"type": "kml",
|
|
"url": "https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/10/c01.kml",
|
|
},
|
|
},
|
|
"game": map[string]any{
|
|
"mode": "classic-sequential",
|
|
},
|
|
"assets": map[string]any{
|
|
"contentHtml": "https://oss-mbh5.colormaprun.com/gotomars/event/content-h5-test-template.html",
|
|
},
|
|
},
|
|
AssetIndex: []map[string]any{
|
|
{
|
|
"assetType": "manifest",
|
|
"assetKey": "manifest",
|
|
},
|
|
{
|
|
"assetType": "mapmeta",
|
|
"assetKey": "mapmeta",
|
|
},
|
|
{
|
|
"assetType": "playfield",
|
|
"assetKey": "playfield-kml",
|
|
},
|
|
{
|
|
"assetType": "content_html",
|
|
"assetKey": "content-html",
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ensure demo event config build: %w", err)
|
|
}
|
|
|
|
if err := s.AttachBuildToRelease(ctx, tx, releaseRow.ID, build.ID); err != nil {
|
|
return nil, fmt.Errorf("attach demo build to release: %w", err)
|
|
}
|
|
|
|
tilesPath := "map/lxcb-001/tiles/"
|
|
mapmetaPath := "map/lxcb-001/tiles/meta.json"
|
|
playfieldPath := "kml/lxcb-001/10/c01.kml"
|
|
contentPath := "event/content-h5-test-template.html"
|
|
manifestChecksum := "demo-checksum-001"
|
|
if err := s.ReplaceEventReleaseAssets(ctx, tx, releaseRow.ID, []UpsertEventReleaseAssetParams{
|
|
{
|
|
EventReleaseID: releaseRow.ID,
|
|
AssetType: "manifest",
|
|
AssetKey: "manifest",
|
|
AssetURL: "https://oss-mbh5.colormaprun.com/gotomars/event/releases/evt_demo_001/rel_e7dd953743c5c0d2/manifest.json",
|
|
Checksum: &manifestChecksum,
|
|
Meta: map[string]any{"source": "release-manifest"},
|
|
},
|
|
{
|
|
EventReleaseID: releaseRow.ID,
|
|
AssetType: "tiles",
|
|
AssetKey: "tiles-root",
|
|
AssetPath: &tilesPath,
|
|
AssetURL: "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/",
|
|
Meta: map[string]any{"kind": "directory"},
|
|
},
|
|
{
|
|
EventReleaseID: releaseRow.ID,
|
|
AssetType: "mapmeta",
|
|
AssetKey: "mapmeta",
|
|
AssetPath: &mapmetaPath,
|
|
AssetURL: "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/meta.json",
|
|
Meta: map[string]any{"format": "json"},
|
|
},
|
|
{
|
|
EventReleaseID: releaseRow.ID,
|
|
AssetType: "playfield",
|
|
AssetKey: "course-kml",
|
|
AssetPath: &playfieldPath,
|
|
AssetURL: "https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/10/c01.kml",
|
|
Meta: map[string]any{"format": "kml"},
|
|
},
|
|
{
|
|
EventReleaseID: releaseRow.ID,
|
|
AssetType: "content_html",
|
|
AssetKey: "content-html",
|
|
AssetPath: &contentPath,
|
|
AssetURL: "https://oss-mbh5.colormaprun.com/gotomars/event/content-h5-test-template.html",
|
|
Meta: map[string]any{"kind": "content-page"},
|
|
},
|
|
}); err != nil {
|
|
return nil, fmt.Errorf("ensure demo event release assets: %w", err)
|
|
}
|
|
|
|
var cardPublicID string
|
|
if err := tx.QueryRow(ctx, `
|
|
INSERT INTO cards (
|
|
card_public_id,
|
|
tenant_id,
|
|
entry_channel_id,
|
|
card_type,
|
|
title,
|
|
subtitle,
|
|
cover_url,
|
|
event_id,
|
|
display_slot,
|
|
display_priority,
|
|
status
|
|
)
|
|
VALUES (
|
|
'card_demo_001',
|
|
$1,
|
|
$2,
|
|
'event',
|
|
'Demo City Run',
|
|
'今日推荐路线',
|
|
'https://oss-mbh5.colormaprun.com/gotomars/assets/demo-cover.jpg',
|
|
$3,
|
|
'home_primary',
|
|
100,
|
|
'active'
|
|
)
|
|
ON CONFLICT (card_public_id) DO UPDATE SET
|
|
tenant_id = EXCLUDED.tenant_id,
|
|
entry_channel_id = EXCLUDED.entry_channel_id,
|
|
card_type = EXCLUDED.card_type,
|
|
title = EXCLUDED.title,
|
|
subtitle = EXCLUDED.subtitle,
|
|
cover_url = EXCLUDED.cover_url,
|
|
event_id = EXCLUDED.event_id,
|
|
display_slot = EXCLUDED.display_slot,
|
|
display_priority = EXCLUDED.display_priority,
|
|
status = EXCLUDED.status
|
|
RETURNING card_public_id
|
|
`, tenantID, channelID, eventID).Scan(&cardPublicID); err != nil {
|
|
return nil, fmt.Errorf("ensure demo card: %w", err)
|
|
}
|
|
|
|
var manualEventID string
|
|
if err := tx.QueryRow(ctx, `
|
|
INSERT INTO events (
|
|
tenant_id, event_public_id, slug, display_name, summary, status
|
|
)
|
|
VALUES ($1, 'evt_demo_variant_manual_001', 'demo-variant-manual-run', 'Demo Variant Manual Run', 'Manual 多赛道联调活动', 'active')
|
|
ON CONFLICT (event_public_id) DO UPDATE SET
|
|
tenant_id = EXCLUDED.tenant_id,
|
|
slug = EXCLUDED.slug,
|
|
display_name = EXCLUDED.display_name,
|
|
summary = EXCLUDED.summary,
|
|
status = EXCLUDED.status
|
|
RETURNING id
|
|
`, tenantID).Scan(&manualEventID); err != nil {
|
|
return nil, fmt.Errorf("ensure variant manual demo event: %w", err)
|
|
}
|
|
|
|
var manualReleaseRow struct {
|
|
ID string
|
|
PublicID string
|
|
}
|
|
if err := tx.QueryRow(ctx, `
|
|
INSERT INTO event_releases (
|
|
release_public_id,
|
|
event_id,
|
|
release_no,
|
|
config_label,
|
|
manifest_url,
|
|
manifest_checksum_sha256,
|
|
route_code,
|
|
status,
|
|
payload_jsonb
|
|
)
|
|
VALUES (
|
|
'rel_demo_variant_manual_001',
|
|
$1,
|
|
1,
|
|
'Demo Variant Manual Config v1',
|
|
'https://oss-mbh5.colormaprun.com/gotomars/event/releases/evt_demo_001/rel_e7dd953743c5c0d2/manifest.json',
|
|
'demo-variant-checksum-001',
|
|
'route-variant-a',
|
|
'published',
|
|
$2::jsonb
|
|
)
|
|
ON CONFLICT (release_public_id) DO UPDATE SET
|
|
event_id = EXCLUDED.event_id,
|
|
config_label = EXCLUDED.config_label,
|
|
manifest_url = EXCLUDED.manifest_url,
|
|
manifest_checksum_sha256 = EXCLUDED.manifest_checksum_sha256,
|
|
route_code = EXCLUDED.route_code,
|
|
status = EXCLUDED.status,
|
|
payload_jsonb = EXCLUDED.payload_jsonb
|
|
RETURNING id, release_public_id
|
|
`, manualEventID, `{
|
|
"play": {
|
|
"assignmentMode": "manual",
|
|
"courseVariants": [
|
|
{
|
|
"id": "variant_a",
|
|
"name": "A 线",
|
|
"description": "短线体验版",
|
|
"routeCode": "route-variant-a",
|
|
"selectable": true
|
|
},
|
|
{
|
|
"id": "variant_b",
|
|
"name": "B 线",
|
|
"description": "长线挑战版",
|
|
"routeCode": "route-variant-b",
|
|
"selectable": true
|
|
}
|
|
]
|
|
}
|
|
}`).Scan(&manualReleaseRow.ID, &manualReleaseRow.PublicID); err != nil {
|
|
return nil, fmt.Errorf("ensure variant manual demo release: %w", err)
|
|
}
|
|
|
|
if _, err := tx.Exec(ctx, `
|
|
UPDATE events
|
|
SET current_release_id = $2
|
|
WHERE id = $1
|
|
`, manualEventID, manualReleaseRow.ID); err != nil {
|
|
return nil, fmt.Errorf("attach variant manual demo release: %w", err)
|
|
}
|
|
|
|
var manualCardPublicID string
|
|
if err := tx.QueryRow(ctx, `
|
|
INSERT INTO cards (
|
|
card_public_id,
|
|
tenant_id,
|
|
entry_channel_id,
|
|
card_type,
|
|
title,
|
|
subtitle,
|
|
cover_url,
|
|
event_id,
|
|
display_slot,
|
|
display_priority,
|
|
status
|
|
)
|
|
VALUES (
|
|
'card_demo_variant_manual_001',
|
|
$1,
|
|
$2,
|
|
'event',
|
|
'Demo Variant Manual Run',
|
|
'多赛道手动选择联调',
|
|
'https://oss-mbh5.colormaprun.com/gotomars/assets/demo-cover.jpg',
|
|
$3,
|
|
'home_primary',
|
|
95,
|
|
'active'
|
|
)
|
|
ON CONFLICT (card_public_id) DO UPDATE SET
|
|
tenant_id = EXCLUDED.tenant_id,
|
|
entry_channel_id = EXCLUDED.entry_channel_id,
|
|
card_type = EXCLUDED.card_type,
|
|
title = EXCLUDED.title,
|
|
subtitle = EXCLUDED.subtitle,
|
|
cover_url = EXCLUDED.cover_url,
|
|
event_id = EXCLUDED.event_id,
|
|
display_slot = EXCLUDED.display_slot,
|
|
display_priority = EXCLUDED.display_priority,
|
|
status = EXCLUDED.status
|
|
RETURNING card_public_id
|
|
`, tenantID, channelID, manualEventID).Scan(&manualCardPublicID); err != nil {
|
|
return nil, fmt.Errorf("ensure variant manual demo card: %w", err)
|
|
}
|
|
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &DemoBootstrapSummary{
|
|
TenantCode: "tenant_demo",
|
|
ChannelCode: "mini-demo",
|
|
EventID: "evt_demo_001",
|
|
ReleaseID: releaseRow.PublicID,
|
|
SourceID: source.ID,
|
|
BuildID: build.ID,
|
|
CardID: cardPublicID,
|
|
VariantManualEventID: "evt_demo_variant_manual_001",
|
|
VariantManualRelease: manualReleaseRow.PublicID,
|
|
VariantManualCardID: manualCardPublicID,
|
|
}, nil
|
|
}
|