PlotDirector/PlotLine/Sql/342_ContextAssetPresence.sql

64 lines
2.2 KiB
Transact-SQL

SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
-- Canonical, explicit presence. Never infer it from legacy action/state events.
IF NOT EXISTS (SELECT 1 FROM dbo.AssetEventTypes WHERE TypeName = N'Present')
INSERT dbo.AssetEventTypes (TypeName, MarkerText, SortOrder, IsActive)
VALUES (N'Present', N'P', 5, 1);
GO
CREATE OR ALTER PROCEDURE dbo.AssetEvent_Save
@AssetEventID int = NULL,
@StoryAssetID int,
@SceneID int,
@AssetEventTypeID int,
@FromStateID int = NULL,
@ToStateID int = NULL,
@EventTitle nvarchar(200),
@EventDescription nvarchar(max) = NULL
AS
BEGIN
SET NOCOUNT ON;
-- Presence/mention records describe a scene link, not a narrative transition.
-- Discard stale state controls from every existing event editor.
IF EXISTS (SELECT 1 FROM dbo.AssetEventTypes WHERE AssetEventTypeID = @AssetEventTypeID
AND TypeName IN (N'Present', N'Mentioned', N'First Mentioned'))
BEGIN
SET @FromStateID = NULL;
SET @ToStateID = NULL;
END;
IF @AssetEventID IS NULL OR @AssetEventID = 0
BEGIN
INSERT dbo.AssetEvents (StoryAssetID, SceneID, AssetEventTypeID, FromStateID, ToStateID, EventTitle, EventDescription)
VALUES (@StoryAssetID, @SceneID, @AssetEventTypeID, @FromStateID, @ToStateID, @EventTitle, @EventDescription);
SET @AssetEventID = CAST(SCOPE_IDENTITY() AS int);
END
ELSE
BEGIN
UPDATE dbo.AssetEvents
SET StoryAssetID = @StoryAssetID,
SceneID = @SceneID,
AssetEventTypeID = @AssetEventTypeID,
FromStateID = @FromStateID,
ToStateID = @ToStateID,
EventTitle = @EventTitle,
EventDescription = @EventDescription,
UpdatedDate = SYSUTCDATETIME()
WHERE AssetEventID = @AssetEventID;
END
IF @ToStateID IS NOT NULL
BEGIN
UPDATE dbo.StoryAssets
SET CurrentStateID = @ToStateID,
IsResolved = CASE WHEN EXISTS (SELECT 1 FROM dbo.AssetStates WHERE AssetStateID = @ToStateID AND IsResolvedState = 1) THEN 1 ELSE IsResolved END,
UpdatedDate = SYSUTCDATETIME()
WHERE StoryAssetID = @StoryAssetID;
END
SELECT @AssetEventID;
END;
GO