1
BEGIN
2
DECLARE GID INT DEFAULT 0;
3
DECLARE GL VARCHAR(100000);
4
Set @GID=0;
5
SET group_concat_max_len=100000;
6
Select GroupId from MasterGroups Where Name='All Clients' and Depth=0 order by GroupId limit 1 INTO @GID;
7
IF @GID=0 THEN
8
Insert Into MasterGroups (Name,FullName,depth,Notes,Children,Permissions,Template,AutoJoinScript,MaintenanceID,MaintWindowApplied,GroupType, Priority) Values('All Clients','All Clients',0,'This group is automatically created by the LabTech system to contain all your clients.','',0,0,0,0,Now(),1, 5);
9
Select GroupId from MasterGroups Where Name='All Clients' and Depth=0 order by GroupId limit 1 INTO @GID;
10
END IF;
11
IF @GID>0 THEN
12
INSERT IGNORE INTO MasterGroups (NAME, FullName,Notes, Control,ControlID,ParentID,Parents,Depth,`Master`,Children,Permissions,Template,AutoJoinScript,MaintenanceID,MaintWindowApplied,GroupType,Priority,GUID) (SELECT Clients.Name,CONCAT('All Clients.',Clients.Name),'Auto Created for easy Permission Setting.',1,Clients.ClientID,@GID,CONCAT(',',@GID,','),1,2,'',0,0,0,0,NOW(),1, 5, UUID() FROM Clients WHERE (flags & 4) = 0 AND ClientID NOT IN (SELECT ControlID FROM MasterGroups WHERE Control=1 AND ParentID=@GID));
13
DELETE FROM MasterGroups WHERE ParentID=@GID AND controlID NOT IN (SELECT ClientID FROM Clients WHERE (flags & 4) = 0);
14
UPDATE MasterGroups JOIN Clients ON MasterGroups.ControlID=Clients.ClientID SET MasterGroups.Name=Clients.Name, Fullname=CONCAT('All Clients.',Clients.Name) WHERE MasterGroups.Control=1 AND MasterGroups.ParentID=@GID AND MasterGroups.Name<>Clients.Name;
15
SELECT GROUP_CONCAT(Groupid) FROM Mastergroups WHERE ParentID=@GID into @GL;
16
UPDATE MasterGroups SET Children=CONCAT(',',@GL,',') WHERE GroupID=@GID;
17
END IF;
18
END