|
1 | | -CREATE OR ALTER PROCEDURE usp_MoveCompetencyGroupInSelfAssessment |
2 | | - @SelfAssessmentID INT, |
3 | | - @GroupID INT, |
4 | | - @Direction NVARCHAR(10) |
| 1 | + |
| 2 | + |
| 3 | +CREATE PROCEDURE usp_RenumberSelfAssessmentStructure |
| 4 | + @SelfAssessmentID INT |
5 | 5 | AS |
6 | 6 | BEGIN |
7 | 7 | SET NOCOUNT ON; |
8 | 8 |
|
9 | | - DECLARE @CurrentMinOrder INT, @CurrentMaxOrder INT; |
| 9 | + /* |
| 10 | + Step 1: Build an ordered list of groups |
| 11 | + - Each group is ranked by its current Min(Ordering) |
| 12 | + - Ungrouped competencies (NULL CompetencyGroupID) are treated as their own "pseudo group" |
| 13 | + */ |
| 14 | + ;WITH GroupRanks AS ( |
| 15 | + SELECT |
| 16 | + CompetencyGroupID, |
| 17 | + ROW_NUMBER() OVER (ORDER BY MIN(Ordering)) AS GroupRank |
| 18 | + FROM SelfAssessmentStructure |
| 19 | + WHERE SelfAssessmentID = @SelfAssessmentID |
| 20 | + GROUP BY CompetencyGroupID |
| 21 | + ) |
| 22 | + /* |
| 23 | + Step 2: Renumber groups and competencies |
| 24 | + - Groups get ranked (1,2,3…) |
| 25 | + - Within each group, competencies are ordered by their current Ordering and renumbered (1,2,3…) |
| 26 | + */ |
| 27 | + UPDATE sas |
| 28 | + SET sas.Ordering = rn.NewOrdering |
| 29 | + FROM SelfAssessmentStructure sas |
| 30 | + INNER JOIN ( |
| 31 | + SELECT |
| 32 | + s.ID, |
| 33 | + -- new group position * 1000 + row within group |
| 34 | + -- gives room between groups and avoids clashes |
| 35 | + (g.GroupRank * 1000) + |
| 36 | + ROW_NUMBER() OVER (PARTITION BY s.CompetencyGroupID, g.GroupRank ORDER BY s.Ordering, s.ID) AS NewOrdering |
| 37 | + FROM SelfAssessmentStructure s |
| 38 | + INNER JOIN GroupRanks g |
| 39 | + ON g.CompetencyGroupID = s.CompetencyGroupID |
| 40 | + WHERE s.SelfAssessmentID = @SelfAssessmentID |
| 41 | + ) rn ON sas.ID = rn.ID; |
10 | 42 |
|
11 | | - -- Get current group's ordering range |
12 | | - SELECT |
13 | | - @CurrentMinOrder = MIN(Ordering), |
14 | | - @CurrentMaxOrder = MAX(Ordering) |
15 | | - FROM SelfAssessmentStructure |
16 | | - WHERE SelfAssessmentID = @SelfAssessmentID |
17 | | - AND CompetencyGroupID = @GroupID; |
| 43 | +END |
18 | 44 |
|
19 | | - IF @CurrentMinOrder IS NULL RETURN; |
| 45 | +GO |
20 | 46 |
|
21 | | - -- Find the adjacent group |
22 | | - DECLARE @OtherGroupID INT, @OtherMinOrder INT, @OtherMaxOrder INT; |
| 47 | +CREATE OR ALTER PROCEDURE usp_MoveCompetencyGroupInSelfAssessment |
| 48 | + @SelfAssessmentID INT, |
| 49 | + @GroupID INT, |
| 50 | + @Direction NVARCHAR(10) |
| 51 | +AS |
| 52 | +BEGIN |
| 53 | + SET NOCOUNT ON; |
23 | 54 |
|
24 | | - ;WITH GroupOrders AS ( |
25 | | - SELECT CompetencyGroupID, |
26 | | - MIN(Ordering) AS MinOrder, |
27 | | - MAX(Ordering) AS MaxOrder |
| 55 | + -- Build current group ranks |
| 56 | + ;WITH GroupRanks AS ( |
| 57 | + SELECT |
| 58 | + CompetencyGroupID, |
| 59 | + ROW_NUMBER() OVER (ORDER BY MIN(Ordering)) AS GroupRank |
28 | 60 | FROM SelfAssessmentStructure |
29 | 61 | WHERE SelfAssessmentID = @SelfAssessmentID |
30 | | - AND CompetencyGroupID IS NOT NULL |
31 | 62 | GROUP BY CompetencyGroupID |
32 | 63 | ) |
33 | | - SELECT TOP 1 |
34 | | - @OtherGroupID = CompetencyGroupID, |
35 | | - @OtherMinOrder = MinOrder, |
36 | | - @OtherMaxOrder = MaxOrder |
37 | | - FROM GroupOrders |
38 | | - WHERE CompetencyGroupID <> @GroupID |
39 | | - AND ( |
40 | | - (@Direction = 'up' AND MinOrder < @CurrentMinOrder) OR |
41 | | - (@Direction = 'down' AND MinOrder > @CurrentMinOrder) |
42 | | - ) |
43 | | - ORDER BY |
44 | | - CASE WHEN @Direction = 'up' THEN MinOrder END DESC, |
45 | | - CASE WHEN @Direction = 'down' THEN MinOrder END ASC; |
| 64 | + SELECT * INTO #GroupRanks FROM GroupRanks; |
| 65 | + |
| 66 | + DECLARE @CurrentRank INT; |
| 67 | + SELECT @CurrentRank = GroupRank FROM #GroupRanks WHERE CompetencyGroupID = @GroupID; |
| 68 | + |
| 69 | + IF @CurrentRank IS NULL |
| 70 | + BEGIN |
| 71 | + DROP TABLE #GroupRanks; |
| 72 | + RETURN; -- group not found |
| 73 | + END |
| 74 | + |
| 75 | + DECLARE @SwapGroupID INT, @SwapRank INT; |
46 | 76 |
|
47 | | - IF @OtherGroupID IS NULL RETURN; -- already at top/bottom |
| 77 | + IF @Direction = 'up' |
| 78 | + BEGIN |
| 79 | + SELECT TOP 1 @SwapGroupID = CompetencyGroupID, @SwapRank = GroupRank |
| 80 | + FROM #GroupRanks |
| 81 | + WHERE GroupRank < @CurrentRank |
| 82 | + ORDER BY GroupRank DESC; |
| 83 | + END |
| 84 | + ELSE IF @Direction = 'down' |
| 85 | + BEGIN |
| 86 | + SELECT TOP 1 @SwapGroupID = CompetencyGroupID, @SwapRank = GroupRank |
| 87 | + FROM #GroupRanks |
| 88 | + WHERE GroupRank > @CurrentRank |
| 89 | + ORDER BY GroupRank ASC; |
| 90 | + END |
48 | 91 |
|
49 | | - -- Now swap by shifting order values |
50 | | - DECLARE @TempOffset INT = 10000; |
| 92 | + IF @SwapGroupID IS NULL |
| 93 | + BEGIN |
| 94 | + DROP TABLE #GroupRanks; |
| 95 | + RETURN; -- already at top/bottom |
| 96 | + END |
51 | 97 |
|
52 | | - -- Move current group temporarily out of the way |
53 | | - UPDATE SelfAssessmentStructure |
54 | | - SET Ordering = Ordering + @TempOffset |
55 | | - WHERE SelfAssessmentID = @SelfAssessmentID |
56 | | - AND CompetencyGroupID = @GroupID; |
| 98 | + /* |
| 99 | + Step 1: Temporarily bump the current group's Ordering so it sorts last |
| 100 | + (this avoids collisions before renumbering). |
| 101 | + */ |
| 102 | + UPDATE sas |
| 103 | + SET Ordering = Ordering + 100000 |
| 104 | + FROM SelfAssessmentStructure sas |
| 105 | + WHERE SelfAssessmentID = @SelfAssessmentID AND CompetencyGroupID = @GroupID; |
57 | 106 |
|
58 | | - -- Move other group into current group's slot |
59 | | - UPDATE SelfAssessmentStructure |
60 | | - SET Ordering = Ordering - (@OtherMinOrder - @CurrentMinOrder) |
61 | | - WHERE SelfAssessmentID = @SelfAssessmentID |
62 | | - AND CompetencyGroupID = @OtherGroupID; |
| 107 | + /* |
| 108 | + Step 2: Call the renumbering procedure to rebuild a clean sequence. |
| 109 | + */ |
| 110 | + EXEC usp_RenumberSelfAssessmentStructure @SelfAssessmentID; |
63 | 111 |
|
64 | | - -- Move current group (now offset) into other group's slot |
65 | | - UPDATE SelfAssessmentStructure |
66 | | - SET Ordering = Ordering - @TempOffset + (@OtherMinOrder - @CurrentMinOrder) |
67 | | - WHERE SelfAssessmentID = @SelfAssessmentID |
68 | | - AND CompetencyGroupID = @GroupID; |
| 112 | + DROP TABLE #GroupRanks; |
69 | 113 | END |
| 114 | +GO |
0 commit comments