USE [MyData]
GO
/*
***** Object: Table [dbo].[BaseUser] Script Date: 08/24/2012 11:50:20 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[BaseUser](
[ID] [
int] IDENTITY(
1,
1) NOT NULL,
[UserName] [varchar](
50) NOT NULL,
[PassWord] [varchar](
50) NULL,
[UserDesc] [nvarchar](
200) NULL,
[UnitID] [
int] NULL,
[PostID] [
int] NULL,
[LocationID] [
int] NULL,
[Phone] [varchar](
50) NULL,
[Email] [varchar](
100) NULL,
[EnableFlag] [bit] NOT NULL,
CONSTRAINT [PK_MyUser] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/*
***** Object: Table [dbo].[FlowStep] Script Date: 08/24/2012 11:50:20 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[FlowStep](
[ID] [
int] IDENTITY(
1,
1) NOT NULL,
[StepID] [
int] NOT NULL,
[FlowID] [
int] NOT NULL,
[StepCode] [varchar](
50) NOT NULL,
[StepName] [nvarchar](
50) NOT NULL,
[IsJoin] [bit] NULL,
[Params] [xml] NULL,
[EnabeFlag] [bit] NOT NULL,
CONSTRAINT [PK_FlowStep] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/*
***** Object: Table [dbo].[FlowInstance] Script Date: 08/24/2012 11:50:20 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[FlowInstance](
[ID] [
int] IDENTITY(
1,
1) NOT NULL,
[FlowID] [
int] NOT NULL,
[FlowName] [varchar](
50) NOT NULL,
[UserID] [
int] NOT NULL,
[UserName] [varchar](
50) NOT NULL,
[ApplyUserName] [varchar](
50) NULL,
[ApplyDateTime] [datetime] NULL,
[EndUserName] [varchar](
50) NULL,
[EndDateTime] [datetime] NULL,
[StatusID] [
int] NULL,
[StatusName] [varchar](
50) NULL,
[ApplyStatus] [nvarchar](
500) NULL,
CONSTRAINT [PK_FlowMain] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/*
***** Object: Table [dbo].[FlowActionTraceData] Script Date: 08/24/2012 11:50:20 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[FlowActionTraceData](
[TraceID] [
int] NOT NULL,
[Data] [xml] NULL,
CONSTRAINT [PK_FlowActionTraceData] PRIMARY KEY CLUSTERED
(
[TraceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/*
***** Object: Table [dbo].[FlowActionTrace] Script Date: 08/24/2012 11:50:20 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[FlowActionTrace](
[ID] [
int] IDENTITY(
1,
1) NOT NULL,
[InstanceID] [
int] NOT NULL,
[FlowID] [
int] NOT NULL,
[FlowName] [nvarchar](
50) NOT NULL,
[FlowCode] [varchar](
50) NOT NULL,
[StepID] [
int] NOT NULL,
[StepName] [nvarchar](
50) NOT NULL,
[StepCode] [varchar](
50) NOT NULL,
[ActionID] [
int] NULL,
[ActionName] [nvarchar](
50) NULL,
[ActionCode] [varchar](
50) NULL,
[OwnerUserID] [
int] NOT NULL,
[OwnerUserName] [varchar](
50) NOT NULL,
[OwnerUserDesc] [nvarchar](
50) NOT NULL,
[SubmitUserID] [
int] NULL,
[SubmitUserName] [varchar](
50) NULL,
[SubmitUserDesc] [nvarchar](
50) NULL,
[ArriveTime] [datetime] NOT NULL,
[ReadTime] [datetime] NULL,
[SubmitTime] [datetime] NULL,
[PreID] [
int] NOT NULL,
[SubmitMemo] [nvarchar](
500) NULL,
CONSTRAINT [PK_FlowActionTrace] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/*
***** Object: Table [dbo].[FlowAction] Script Date: 08/24/2012 11:50:20 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[FlowAction](
[ID] [
int] IDENTITY(
1,
1) NOT NULL,
[ActionID] [
int] NOT NULL,
[StepID] [
int] NOT NULL,
[FlowID] [
int] NOT NULL,
[ActionCode] [varchar](
50) NOT NULL,
[ActionName] [nvarchar](
50) NOT NULL,
[Params] [xml] NULL,
[EnableFlag] [bit] NOT NULL,
CONSTRAINT [PK_FlowAction] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/*
***** Object: Table [dbo].[Flow] Script Date: 08/24/2012 11:50:20 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Flow](
[ID] [
int] NOT NULL,
[FlowCode] [varchar](
50) NOT NULL,
[FlowName] [nvarchar](
50) NULL,
[FlowVersion] [varchar](
50) NOT NULL,
[Params] [xml] NULL,
[EnableFlag] [bit] NOT NULL,
CONSTRAINT [PK_Flow_1] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/*
***** Object: UserDefinedFunction [dbo].[f_splitstr] Script Date: 08/24/2012 11:50:20 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[f_splitstr](@SourceSql varchar(
8000),@StrSeprate varchar(
100))
returns @temp table(F1 varchar(
100))
as
begin
declare @ch
as varchar(
100)
set @SourceSql=@SourceSql+@StrSeprate
while(@SourceSql<>
'')
begin
set @ch=left(@SourceSql,charindex(@StrSeprate,@SourceSql,
1)-
1)
insert @temp values(@ch)
set @SourceSql=stuff(@SourceSql,
1,charindex(@StrSeprate,@SourceSql,
1),
'')
end
return
end
GO
/*
***** Object: UserDefinedFunction [dbo].[f_getstartstatus] Script Date: 08/24/2012 11:50:20 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[f_getstartstatus]
(
@OwnerUserIDStr varchar(
500),
@StepName nvarchar(
50)
)
RETURNS nvarchar(
500)
AS
BEGIN
declare @ret nvarchar(
500)
set @ret=
''
select @ret=@ret+e.UserDesc +
'
(
'+@StepName+
'
);
'
from f_splitstr(@OwnerUserIDStr,
'
,
') d
join dbo.BaseUser e on d.f1=e.ID
return @ret
END
GO
/*
***** Object: UserDefinedFunction [dbo].[f_getflowstatus] Script Date: 08/24/2012 11:50:20 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
create FUNCTION [dbo].[f_getflowstatus]
(
@instanceid
int
)
RETURNS nvarchar(
500)
AS
BEGIN
declare @ret nvarchar(
500)
set @ret=
''
select @ret=@ret+OwnerUserDesc+
'
(
'+StepName+
'
);
'
from dbo.flowactiontrace
where InstanceID=@instanceid
and SubmitTime
is
null
return @ret
END
GO
/*
***** Object: StoredProcedure [dbo].[usp_FlowSubmit] Script Date: 08/24/2012 11:50:18 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- exec usp_FlowSubmit
10,
3,
3,
0,
'
4/2,3
',
null
-- =============================================
CREATE PROCEDURE [dbo].[usp_FlowSubmit]
(
@TraceID
int,--flowactiontrace的id
@AcionID
int,--操作编号
@SubmitUserID
int,--提交人编号
@IsEnd
int,--是否结束步骤
@NextStr varchar(
2000),--
'
1/3,4;3/5,6
'
@Data nvarchar(max)
)
AS
BEGIN
SET NOCOUNT ON;
declare @flowid
int
declare @stepid
int
declare @actioncode varchar(
50)
declare @actionname nvarchar(
50)
declare @instanceid
int
declare @submitusername varchar(
50)
declare @submituserdesc nvarchar(
50)
declare @isjoin bit
select @submitusername=username,
@submituserdesc=userdesc
from dbo.BaseUser
where ID=@SubmitUserID
select @instanceid=instanceid,
@flowid=flowid,
@stepid=stepid
from dbo.FlowActionTrace
where ID=@TraceID
select @actioncode=actioncode,
@actionname=actionname
from dbo.FlowAction
where ActionID=@AcionID and StepID=@stepid and FlowID=@flowid
select @isjoin =isjoin
from dbo.FlowStep
where StepID=@stepid and FlowID=@flowid
begin tran
--提交当前待办
update dbo.FlowActionTrace
set ActionID=@AcionID,
ActionCode=@actioncode,
ActionName=@actionname,
SubmitUserID=@SubmitUserID,
SubmitUserName=@submitusername,
SubmitUserDesc=@submituserdesc,
SubmitTime=GETDATE()
where ID=@TraceID
if(@Data
is not
null)
begin
insert into dbo.FlowActionTraceData(TraceID,Data)
values(@TraceID,@Data)
end
--如果不是会审,删除其他待办
if(@isjoin=
0)
begin
delete dbo.FlowActionTrace
where StepID=@stepid and FlowID=@flowid and ID<>@TraceID
end
--更新主表状态
if(@IsEnd=
1
and (
select COUNT(*)
from dbo.FlowActionTrace
where FlowID=@flowid and SubmitTime
is
null)=
0
)
begin
update dbo.FlowInstance
set EndUserName=@submitusername,
EndDateTime=GETDATE(),
StatusID=
100,
StatusName=
'
正常结束
',
ApplyStatus=
''
where ID=@instanceid
end
else --不是结束时插入待办,可能多个路线,一个路线可能多人
begin
declare @next varchar(
500)
DECLARE db_cursor CURSOR FOR
select * FROM dbo.f_splitstr(@NextStr,
'
;
')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @next
WHILE @@FETCH_STATUS=
0
BEGIN
declare @nextstepid
int --下一步骤
declare @OwnerUserIDStr varchar(
500) --下一步骤处理人
set @nextstepid=convert(
int,left(@next,charindex(
'
/
',@next,
1)-
1))
set @OwnerUserIDStr=right(@next,len(@next)-charindex(
'
/
',@next,
1))
declare @nextstepcode varchar(
50)
declare @nextstepname nvarchar(
50)
select @nextstepcode=stepcode,
@nextstepname =stepname
from FlowStep
where FlowID=@flowid and StepID=@nextstepid
--插入待办
INSERT INTO [FlowActionTrace]
([InstanceID]
,[FlowID]
,[FlowName]
,[FlowCode]
,[StepID]
,[StepName]
,[StepCode]
,[OwnerUserID]
,[OwnerUserName]
,[OwnerUserDesc]
,[ArriveTime],[PreID]
)
SELECT @instanceid,@FlowID,a.FlowName,a.FlowCode,
@nextstepid,@nextstepcode,@nextstepname,
b.ID,b.UserName,b.UserDesc,
getdate(),@TraceID
FROM dbo.Flow a,
(
select e.ID,e.UserName,e.UserDesc
from dbo.f_splitstr(@OwnerUserIDStr,
'
,
') d
join dbo.BaseUser e on d.f1=e.ID
)b WHERE a.id=@flowid
FETCH NEXT FROM db_cursor INTO @next
END
CLOSE db_cursor
DEALLOCATE db_cursor
--更新主表状态
update dbo.FlowInstance
set ApplyStatus=dbo.f_getflowstatus(@instanceid)
where ID=@instanceid
end
commit tran
END
GO
/*
***** Object: StoredProcedure [dbo].[usp_FlowStart] Script Date: 08/24/2012 11:50:18 *****
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- exec usp_FlowStart
1,
1,
1,
2,
'
2,3
'
-- =============================================
CREATE PROCEDURE [dbo].[usp_FlowStart]
(
@FlowID
int,--流程模板编号
@UserID
int,--流程用户
@ApplyUserID
int,--流程申请用户
@NextStepID
int,--下一步骤
@OwnerUserIDStr varchar(
500),--多个下手审批人,以‘,’分隔
@Data nvarchar(max)
)
AS
BEGIN
SET NOCOUNT ON;
declare @stepid
int
declare @stepcode varchar(
50)
declare @stepname nvarchar(
50)
declare @actionid
int
declare @actioncode varchar(
50)
declare @actionname nvarchar(
50)
select @stepid=stepid,
@stepcode=stepcode,
@stepname=stepname
from FlowStep
where FlowID=@FlowID and StepID=
1
select @actionid=actionid,
@actioncode=actioncode,
@actionname=actionname
from FlowAction
where FlowID=@FlowID and StepID=
1 and ActionID=
1
declare @nextstepcode varchar(
50)
declare @nextstepname nvarchar(
50)
select @nextstepcode=stepcode,
@nextstepname=stepname
from FlowStep
where FlowID=@FlowID and StepID=@NextStepID
begin tran
declare @instanceid
int
--插入主表
INSERT INTO [FlowInstance]
([FlowID]
,[FlowName]
,[UserID]
,[UserName]
,[ApplyUserName]
,[ApplyDateTime]
,[StatusID]
,[StatusName]
,[ApplyStatus])--待办状态
SELECT @FlowID,a.FlowName,
@UserID,b.UserName,c.UserName,
GETDATE(),
1,
'
流程中
',dbo.f_getstartstatus(@OwnerUserIDStr,@nextstepname)
FROM dbo.Flow a,dbo.BaseUser b,dbo.BaseUser c
WHERE a.id=@FlowID and b.ID=@UserID and c.ID=@ApplyUserID
set @instanceid=@@identity
--插入申请日志
INSERT INTO [MyData].[dbo].[FlowActionTrace]
([InstanceID]
,[FlowID]
,[FlowName]
,[FlowCode]
,[StepID]
,[StepName]
,[StepCode]
,[ActionID]
,[ActionName]
,[ActionCode]
,[OwnerUserID]
,[OwnerUserName]
,[OwnerUserDesc]
,[SubmitUserID]
,[SubmitUserName]
,[SubmitUserDesc]
,[ArriveTime]
,[ReadTime]
,[SubmitTime])
SELECT @instanceid,@FlowID,a.FlowName,a.FlowCode,
@stepid,@stepcode,@stepname,
@actionid,@actioncode,@actionname,
b.ID,b.UserName,b.UserDesc,
b.ID,b.UserName,b.UserDesc,
getdate(),getdate(),getdate()
FROM dbo.Flow a,dbo.BaseUser b
where a.ID=@FlowID and b.ID=@ApplyUserID
declare @preid
int
set @preid=@@IDENTITY
if(@Data
is not
null)
begin
insert into dbo.FlowActionTraceData(TraceID,Data)
values(@preid,@Data)
end
--插入待办
INSERT INTO [FlowActionTrace]
([InstanceID]
,[FlowID]
,[FlowName]
,[FlowCode]
,[StepID]
,[StepName]
,[StepCode]
,[OwnerUserID]
,[OwnerUserName]
,[OwnerUserDesc]
,[ArriveTime],[PreID]
)
SELECT @instanceid,@FlowID,a.FlowName,a.FlowCode,
@nextstepid,@nextstepcode,@nextstepname,
b.ID,b.UserName,b.UserDesc,
getdate(),@preid
FROM dbo.Flow a,
(
select e.ID,e.UserName,e.UserDesc
from dbo.f_splitstr(@OwnerUserIDStr,
'
,
') d
join dbo.BaseUser e on d.f1=e.ID
) b
WHERE a.id=@FlowID
commit tran
END
GO
/*
***** Object: Default [DF_MyUser_EnableFlag] Script Date: 08/24/2012 11:50:20 *****
*/
ALTER TABLE [dbo].[BaseUser] ADD CONSTRAINT [DF_MyUser_EnableFlag] DEFAULT ((
1)) FOR [EnableFlag]
GO
/*
***** Object: Default [DF_FlowActionTrace_PreID] Script Date: 08/24/2012 11:50:20 *****
*/
ALTER TABLE [dbo].[FlowActionTrace] ADD CONSTRAINT [DF_FlowActionTrace_PreID] DEFAULT ((
0)) FOR [PreID]
GO