IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_weixin_state]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_weixin_state]
GO

-- =============================================
-- Author:郑华建
-- Create date: 2017年12月27日 09点58分
-- Description:创建state
-- =============================================
CREATE procedure [dbo].[mps_new_weixin_state]
@cmpid int,
@wxs_caption nvarchar(500),
@wxs_wxid int,
@parent int,
@wxs_auto int = 0
AS
BEGIN
SET NOCOUNT ON;
declare @identity int
declare @wxs_value varchar(500)
--预处理
set @wxs_auto = case when @wxs_auto!=1 then 0 else 1 end;
set @wxs_caption = case when @wxs_caption='_none_' then '' else @wxs_caption end;
set @wxs_wxid = case when @wxs_wxid=-999999999 then 0 else @wxs_wxid end;
set @parent = case when @parent=-999999999 then 0 else @parent end;
set @wxs_value = case when @wxs_auto=0 then '##ebs##' else '' end;

insert into [mps_weixinstate](wxs_cmpid,wxs_auto,wxs_caption,wxs_gzh,wxs_qyh,wxs_ding,wxs_wxid,wxs_sub,wxs_value) values(@cmpid,@wxs_auto,@wxs_caption,@wxs_wxid,@wxs_wxid,@wxs_wxid,@wxs_wxid,@parent,@wxs_value);

set @identity=@@IDENTITY
if(@wxs_auto=1)
update [mps_weixinstate] set wxs_code='autostate'+CONVERT(varchar,@identity) where wxs_id = @identity
else
update [mps_weixinstate] set wxs_code='defaultstate'+CONVERT(varchar,@identity) where wxs_id = @identity
select wxs_id,wxs_caption,wxs_value from [mps_weixinstate] where wxs_id=@identity;
END