USE [ChiefmesNew]
GO
/****** Object: StoredProcedure [dbo].[st_Alarm_GenAlarmDealTime] Script Date: 04/05/2016 09:42:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/********************
Create By: Wuchun
Create Date: 2013-08-20
Module: Alarm
Remark: 产生报警派工单的台帐处理时间
update by WUchun on 2013-11-15 增加按班次计算时段逻辑
exec [st_Alarm_GenAlarmDealTime]
truncate table MES_AlarmBillDealTime
select * from MES_AlarmBillDealTime
********************/
ALTER proc [dbo].[st_Alarm_GenAlarmDealTime]
as

declare @rBCCode varchar(10)
select @rBCCode = dbo.[Fn_GetBCCode]()
select distinct Seq = identity(int), AlarmBillNO, AlarmCode, AlarmSetNO, MO, DispatchNO, DispatchPrior, MachineNO, BeginTime,
UnitTime, CalTimesByBC , BCCode
into #AlarmBillTime
from MES_AlarmBill
where DealFlag = 0 and AlarmCode in(select AlarmCode from MES_AlarmTypeSet where TimesShow =1)
and (BCCode = 'All' or charindex(BCCode, @rBCCode)>0)
and UnitTime>0 and datediff(minute,getdate(),NextDealTime)<0

declare @icount int, @i int , @UnitTime float , @MO varchar(50), @DispatchPrior float, @CalTimesByBC bit
declare @AlarmBillNO varchar(100), @AlarmCode varchar(100), @AlarmSetNo varchar(100), @NextDealTime datetime
declare @DispatchNO varchar(100), @MachineNO varchar(100), @BeginTime datetime , @LastDealTime datetime, @BCCode varchar(50)
declare @BCBeginTime datetime , @BCEndTime datetime
select @i= 1
select @icount = COUNT(*) from #AlarmBillTime

while(@i<=@icount)
begin
select @AlarmBillNO = AlarmBillNO, @AlarmCode = AlarmCode, @MO = MO,
@AlarmSetNo = AlarmSetNo, @DispatchNO = DispatchNO, @DispatchPrior = DispatchPrior, @MachineNO = MachineNO
, @BeginTime = BeginTime, @LastDealTime = null, @UnitTime = UnitTime, @CalTimesByBC = CalTimesByBC, @BCCode = BCCode
from #AlarmBillTime
where Seq = @i

--存在符合要求的触发条件则忽略
if(exists(select top 1 * from MES_AlarmBillDealTime
where AlarmbillNO =@AlarmBillNO and AlarmSetNo= @AlarmSetNo
and GETDATE() between DealBegin and DealEnd)
)
begin
set @i = @i+1
continue
end
else
begin
select @BCBeginTime = null, @BCEndTime = null
if(@CalTimesByBC = 1)
begin
if(@BCCode = 'All')
set @BCCode =''
--以班次开始时间计算时段时间
set @BCBeginTime = convert(datetime,dbo.Fn_GetBCBeginTime(@BCCode))
set @BCEndTime = convert(datetime, dbo.Fn_GetBCEndTime(@BCCode))
select @LastDealTime = max(DealEnd)
from MES_AlarmBillDealTime
where AlarmbillNO =@AlarmBillNO and AlarmSetNo= @AlarmSetNo
--大于班次时间则跳过
if(@LastDealTime>@BCEndTime)
begin
set @i = @i+1
continue
end
--小于班次时间则取班次开始时间
if(@LastDealTime<@BCBeginTime or @LastDealTime is null)
set @LastDealTime = @BCBeginTime
end
else
begin
--按派工单开始时间计算时段
select @LastDealTime = max(DealEnd)
from MES_AlarmBillDealTime
where AlarmbillNO =@AlarmBillNO and AlarmSetNo= @AlarmSetNo
if(@LastDealTime is null)
set @LastDealTime = @BeginTime
end
set @NextDealTime = DATEADD(minute, @UnitTime*60, @LastDealTime)

while(getdate()>@NextDealTime)
begin
if(DATEDIFF(HOUR, @NextDealTime, GETDATE())<2*@UnitTime)
begin
insert into MES_AlarmBillDealTime(AlarmBillNo, AlarmCode, AlarmSetNo, MO, DispatchNo, DispatchPrior, MachineNO,
DealBegin, DealEnd, DealFlag, BeginTime, UnitTime, CreateTime)
select @AlarmBillNO, @AlarmCode, @AlarmSetNo, @MO, @DispatchNo, @DispatchPrior, @MachineNO,
dateadd(second,1,@LastDealTime), @NextDealTime, 0, @BeginTime, @UnitTime, GETDATE()
end
set @LastDealTime = @NextDealTime
set @NextDealTime = DATEADD(minute, @UnitTime*60, @LastDealTime)
end

insert into MES_AlarmBillDealTime(AlarmBillNo, AlarmCode, AlarmSetNo, MO, DispatchNo, DispatchPrior, MachineNO,
DealBegin, DealEnd, DealFlag, BeginTime, UnitTime, CreateTime)
select @AlarmBillNO, @AlarmCode, @AlarmSetNo, @MO, @DispatchNo, @DispatchPrior, @MachineNO,
dateadd(second,1,@LastDealTime), @NextDealTime, 0, @BeginTime, @UnitTime, GETDATE()
update MES_AlarmBill set NextDealTime = @NextDealTime where AlarmBillNO = @AlarmBillNO and BCCode = @BCCode
end
set @i = @i+1
end

delete from MES_AlarmBillDealTime where DealEnd is null
delete from MES_AlarmBillDealTime where CreateTime<dateadd(day,-1,getdate())
drop table #AlarmBillTime

/****** Object: StoredProcedure [dbo].[st_MES_GenStatStopCard] Script Date: 10/17/2013 09:54:34 ******/
SET ANSI_NULLS ON
---------------------------------------------------------------------------------------------------------
select top 100 * from MES_AlarmBill order by CreateDate desc

select top 100 * from MES_AlarmBillDealTime

st_Alarm_GenAlarmDealTime的更多相关文章

随机推荐

  1. CCScrollView 实现帮助界面、关卡选择

    本文出自[无间落叶]:http://blog.leafsoar.com/archives/2013/07-27.html 本文介绍了 CCScrollView 来编写帮助界面和关卡选择界面的方法,在编 ...

  2. mk文件剖析

    一个Android.mk file用来向编译系统描述你的源代码.具体来说:该文件是GNU Makefile的一小部分,会被编译系统解析一次或多次.你可以在每一个Android.mk file中定义一个 ...

  3. busybox filesystem add ldd function

    /******************************************************************** * busybox filesystem add ldd f ...

  4. Java Socket(3): NIO

    NIO采取通道(Channel)和缓冲区(Buffer)来传输和保存数据,它是非阻塞式的I/O,即在等待连接.读写数据(这些都是在一线程以客户端的程序中会阻塞线程的操作)的时候,程序也可以做其他事情, ...

  5. linux下打开chm文件的方法

    windows中,通常情况下,chm文件可以使用系统自带的程序打开,但是linux就没有那么幸运了,那么,如何在linux下打开chm 文件呢?有小编来为您介绍介绍,本篇,小编以ubuntu环境为例 ...

  6. ORACLE RAC 监听配置 (listener.ora tnsnames.ora)

    Oracle RAC 监听器的配置与单实例稍有不同,但原理和实现方法基本上是相同的.在Oracle中 tns进程用于为指定网络地址上的一个或多个Oracle 实例提供服务注册,并响应来自客户端对该服务 ...

  7. 在Ubuntu下卸载Apache

    卸载Apache 转自:http://blog.csdn.net/chmo2011/article/details/7026384 1. 删除apache 代码: $ sudo apt-get --p ...

  8. Period(KMP,循环节问题)

    题意: 求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k 分析: i-next[i]恰好是一个循环节 #include <map> #include <set> ...

  9. HDU-1438 钥匙计数之一

    http://acm.hdu.edu.cn/showproblem.php?pid=1438                                钥匙计数之一 Time Limit: 200 ...

  10. [GRYZ2015]INCR

    题目描述 数列 A1,A2,...,AN,修改最少的数字,使得数列严格单调递增. 输入格式 第 1 行,1 个整数 N 第 2 行,N 个整数 A1,A2,...,AN 输出格式 1 个整数,表示最少 ...