-- =============================================
-- Author: <Author,,huangxing>
-- Create date: <Create Date,2012-05-25,>
-- Description: <Description,触发器实现从TagBlinkLogs往历史表TagLocationHis20125插入一条数据,>
-- =============================================
CREATE trigger tr_TagBlinkLogsToTagLocationHis
on TagBlinkLogs
for insert
as
begin
declare @TagId nvarchar(14) --TagId
declare @inTime datetime --进基站时间
declare @State nvarchar(14) --人员表上的状态
declare @AnchorID nvarchar(14) --基站ID
declare @capabilities nvarchar(14) --电量
declare @InMineTime datetime --下井时间
declare @tag nvarchar(14) --临时状态
declare @tempAnchorId nvarchar(14) --临时基站ID
declare @tempState nvarchar(14) --临时状态ID
declare @tableName nvarchar(1000) --表名
declare @strSqlOne nvarchar(1000) --SQL语句
declare @strSqlTwo nvarchar(1000) --SQL语句
declare @tempMacToTag nvarchar(14) -- mac地址转换为TagId
declare @ryDepartmentId nvarchar(14), --人员部门ID
@ryClassTypeId nvarchar(14), --人员工种ID
@kqInMineTime datetime, --插入考勤表的下井时间
@ryInMineTime datetime, --下井时间
-- @ryAnchorType nvarchar(14), --人员表目前所处的基站类型,用于判断升井
@newAnchorType nvarchar(14) --最新插入的基站类型
set @tableName='TagLocationHis'+convert(varchar(4),datepart(year,getdate()))+convert(varchar(2),datepart(month,getdate()))
select @TagId=i.TagId,@AnchorID=i.AnchorID,@inTime=i.TimeStamp,@capabilities=i.capabilities from inserted i,Assets a where i.TagId=a.TagId
--更新电量和进基站时间
--在井口不显示下井时间InMineTime,有 AnchorId,state=1在线
--在井下 有inMineTime
update Assets set AnchorId=@AnchorID where TagId=@TagId and InMineTime is null --更新基站ID
select @tempState=a.state from Assets a inner join Anchors b on a.AnchorId=b.AnchorId where a.TagId=@TagId and a.InMineTime is null and b.AnchorType=1 --井口
if (@tempState=255) -- ***
begin --
update Assets set LastInMineTime=null,LastOutMineTime=null where LastInMineTime is not null or LastOutMineTime is not null --更新最近一次下井时间为空
update b set b.State=1,b.AnchorId=@AnchorId,b.inTime=@inTime from Assets b,inserted where b.TagId=inserted.TagId
end
--井下时,如果下井时间为空,则更新下井时间为进基站时间,状态为在线
select @tempState=a.state from Assets a inner join Anchors b on a.AnchorId=b.AnchorId where a.TagId=@TagId and a.InMineTime is null and a.State is not null and a.AnchorId is not null and b.AnchorType=0 --井下
if (@tempState=1) -- ***
begin
update b set b.inMineTime=@inTime,b.LastInMineTime=@inTime from Assets b,inserted where b.TagId=inserted.TagId
end
--TagBlinkLogs插一条记录,当下井时间不为空时,实时更新基站ID,电量和进基站时间 以下暂时注释,b.inTime=@inTime
update b set b.AnchorId=@AnchorId,b.BatteryState=@capabilities from Assets b,inserted where b.TagId=inserted.TagId and b.InMineTime is not null
--更新进基站时间InMineTime 根据TagId取TagLocationHis20124最小时间
--if(select count(*) from Assets where InMineTime is null and TagId=@TagId)>0
-- begin
-- select @InMineTime=min(a.inTime) from TagLocationHis20124 a inner join Assets b on a.TagId=b.TagId where b.InMineTime is null and b.TagId=@TagId
-- update Assets set InMineTime=@InMineTime where TagId=@TagId
-- end
-- update Assets set State=1 where TagId=@TagId
--历史表找本Tagid最大的那条记录的AnchorId,目的是跟TagBlinkLogs表插入的记录比较,AnchorId是否有变化
--如没有则插入一条记录,如相同则不插入 不同则插入一条记录
--在TagLocationHis20124表中存在inserted表的AnchorID 则返回
--以下这条if语句写法是错误的 ******
-- set @strSqlOne='if(select AnchorId from '+@tableName+' where id in (select max(id) from '+@tableName+' where TagId='+@TagId+'))='+@AnchorID
set @strSqlOne='select '+@tempAnchorId+'=AnchorId from ' +@tableName+' where id in (select max(id) from '+@tableName+' where TagId='+@TagId+')'
exec(@strSqlOne)
if(@tempAnchorId<>@AnchorID or @tempAnchorId is null)
begin
--@tempAnchorId的判断原来放这里
--更新状态 State: 1在线,2 求救,3 禁区,5 求救,8 离线, 255 移除升井
--这里要判断复杂的状态变化 比如考虑求救 更新TagLocationHis20124的各状态
select @tag=State from Assets where TagId=@TagId
if @tag='' or @tag is null
begin
set @tag=255 --添加人员时初始化@tag为255
end
--在AlarmLogs表在TagId相同的情况下找最大那条记录的Status
if (select a.Status from AlarmLogs a where a.AlarmLogId in (select max(AlarmLogId) from AlarmLogs) and a.TagId=@TagId)=64
-- if(select a.Status from AlarmLogs a where a.TagId=@TagId)=64
begin
set @tag=2 -- 求救
end
if (select a.Status from AlarmLogs a where a.AlarmLogId in (select max(AlarmLogId) from AlarmLogs) and a.TagId=@TagId)=32
begin
set @tag=2 -- 求救
end
if (select a.Status from AlarmLogs a where a.AlarmLogId in (select max(AlarmLogId) from AlarmLogs) and a.TagId=@TagId)=96
begin
set @tag=1 --在线
end
--离线处理 待加
set @State=@tag
update b set b.State=@State from Assets b,inserted where b.TagId=inserted.TagId
--临时基站ID与基站ID不相等时执行下列语句
-- set @strSqlTwo='insert into '+@tableName+' (TagId,inTime,outTime,RegionId,AnchorId,X,Y,Z,state) values('+@TagId+','''+convert(nvarchar(100),@inTime,121)+''','''','''','+@AnchorID+','''','''','''','+@State+')'
set @strSqlTwo='insert into '+@tableName+' (TagId,inTime,outTime,RegionId,AnchorId,X,Y,Z,state) values('+@TagId+','''+convert(nvarchar(100),@inTime,121)+''',null,null,'+@AnchorID+',null,null,null,'+@State+')'
-- set @strSqlTwo='insert into '+@tableName+' (TagId,inTime,outTime,RegionId,AnchorId,X,Y,Z,state,BatteryState,BatteryVoltage,DealAlmMsg)
--values('+@TagId+','''+convert(nvarchar(100),@inTime,121)+''','''','''','+@AnchorID+','''','''','''','+@State+',''12'',''3'','''')'
exec(@strSqlTwo)
exec SearchAnchorOut_Two --执行存储过程,得到outTime
--升井(有下井时间且在井下呆的时间至少为班次的时长,当再次被井口基站收到时表示升井,同时插入考勤表一条记录)
--人员表基站类型为0 TagBlinkLogs基站类型为1
--升井
select @ryInMineTime=InMineTime from Assets where InMineTime is not null
-- select @ryAnchorType=b.AnchorType from Assets a inner join Anchors b on a.AnchorId=b.AnchorId and b.AnchorType=0 and a.TagId=@TagId
select @newAnchorType=b.AnchorType from inserted i inner join Anchors b on i.AnchorId=b.AnchorId and b.AnchorType=1
if(@newAnchorType=1 and datediff(Minute,@ryInMineTime,getdate())>3) --根据时间或工种的时间来插入考勤记录
begin
update Assets set InMineTime=null,state=255,OutMineTime=@inTime,LastOutMineTime=@inTime where TagId=@TagId and InMineTime is not null and IsValid=1 --更新下井时间为空
--插入考勤记录
select @ryDepartmentId=a.DepartmentId,@ryClassTypeId=a.ClassTypeId,@kqInMineTime=a.LastInMineTime from Assets a left join Departments d on a.DepartmentId=d.DepartmentId left join classtype c on a.ClassTypeId=c.classtypeid where a.OutMineTime
is not null
insert into dbo.Attendance (TagId,DepartmentId,ClassTypeId,InMineTime,OutMineTime,DataType,CreateTime,Remark) values (@TagId,@ryDepartmentId,@ryClassTypeId,@kqInMineTime,@inTime,1,getdate(),'')
update Assets set OutMineTime=null,inTime=null,AnchorId=null,outTime=null,State=255,InMineTime=null,Distance=null where OutMineTime is not null and tagId=@TagId
end
end
end
作者原创:黄杏 转载请注明来源,谢谢!