IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_insert_forum]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_insert_forum]
GO
create proc [dbo].[mps_insert_forum]
@cmpid int,
@branchid int,
@branchname nvarchar(200),
@userdpid int,
@userdepart nvarchar(200),
@usertype int,
@useruid int,
@username nvarchar(200),
@ebstype varchar(100),
@ctgid int,
@objid int,
@ffmid int,
@content nvarchar(2000),
@attach nvarchar(2000),
@fffmid bigint,
@ebstag int,
@userids varchar(max),
@message int
as
begin
set nocount on 

--create table #messagecuid ( cuid int )

if @userids = '_none_'  set @userids = ''
if @fffmid = -999999999 set @fffmid = 0 
if @ffmid = -999999999 set @ffmid = 0
if @ebstag = -999999999 set @ebstag = 0
if @attach = '_none_' set @attach = ''
if @message = -999999999 set @message = 0

declare @nowtime datetime
declare @fcuid bigint,@fcuname nvarchar(255),@wordcuid int

set @nowtime = GETDATE()
if @ffmid > 0
begin
select @fcuid = fm_cuid , @fcuname = fm_cuname,@wordcuid = fm_workcuid
from mps_forum where fm_id = @ffmid
end else
begin
set @fcuid = 0
set @fcuname = ''
end

if(@fffmid=0 and @ffmid>0) set @fffmid = @ffmid
insert into mps_forum
(fm_cmpid,fm_bcid,fm_branch,fm_dpid,fm_depart,fm_usertype,fm_cuid,fm_cuname,
fm_type,fm_ctgid,fm_objid,fm_createdtime,fm_updatedtime,fm_like,fm_dislike,
fm_ffmid,fm_text,fm_view,fm_attach,fm_fffmid,fm_fcuid,fm_fcuname,fm_ebstag) 
values(@cmpid,@branchid,@branchname,@userdpid,@userdepart,@usertype,
@useruid,@username,@ebstype,@ctgid,@objid,@nowtime,@nowtime,0,0,@ffmid,@content,0,@attach,@fffmid,@fcuid,@fcuname,@ebstag);

declare @fmid int

select top 1 @fmid = fm_id  from mps_forum
where fm_cmpid =@cmpid and  fm_usertype = @usertype
and fm_cuid = @useruid and fm_createdtime = @nowtime and fm_type = @ebstype
    and fm_ctgid =@ctgid and fm_objid = @objid and fm_ebstag = @ebstag
order by fm_id desc


if @fffmid>0 and @ffmid>0
begin
if(exists(select fm_id from mps_forum where fm_id = @ffmid and fm_work='wrk' and fm_workstatus = 1 and fm_workcuid = @useruid))
begin
update mps_forum set fm_workstatus = 0 where fm_id = @ffmid
end
if(exists(select fm_id from mps_forum where fm_id = @ffmid and fm_work='wrk'))
begin
update mps_forum set fm_work = 'wrkdeal',fm_workcuid = (select b.fm_workcuid from mps_forum b where fm_id = @ffmid) 
where fm_id = @fmid
end
if(exists(select fm_id from mps_forum where fm_id = @ffmid and fm_work='wrkdeal'))
begin
update mps_forum set fm_work = 'wrkdeal',fm_workcuid = (select b.fm_workcuid from mps_forum b where fm_id = @ffmid),
fm_ffmid = (select b.fm_ffmid from mps_forum b where fm_id = @ffmid)
where fm_id = @fmid
end
end
select * from mps_forum where fm_id = @fmid

-- 判断是否勾选人
declare @mfmid int
declare @ss2 varchar(4000)= '',@i int= 0,@ss1 varchar(4000)
declare @mdpid int, @mdepart varchar(500), @mname varchar(200)
declare @mmcuid int
declare @existsids int = 0
while (@userids>'') 
begin
set @i=charindex(',',@userids)
if (@i>0)
begin
set @ss2 = substring(@userids,1,@i-1) 
set @userids = substring(@userids,@i+1,len(@userids)-@i)
end else 
begin
set @ss2 = @userids 
set @userids = '' 
end 
if (@ss2>'') 
begin
select @mdpid = cu_dpid,@mname = cu_name,@mdepart = cu_depart
from mps_employee
where cu_id = convert(bigint,@ss2)
--插入提醒
set @ss1 = ''
set @ss1 = '@'+@mname
insert into mps_forum
(fm_cmpid,fm_bcid,fm_branch,fm_dpid,fm_depart,fm_usertype,fm_cuid,fm_cuname,
fm_type,fm_ctgid,fm_objid,fm_createdtime,fm_updatedtime,fm_like,fm_dislike,
fm_ffmid,fm_text,fm_view,fm_attach,fm_fffmid,fm_fcuid,fm_fcuname,fm_ebstag,fm_work,fm_workstatus,fm_workcuid) 
values(@cmpid,@branchid,@branchname,@userdpid,@userdepart,@usertype,
@useruid,@username,@ebstype,@ctgid,@objid,@nowtime,@nowtime,0,0,@fmid,@ss1,0,'',@fmid,@useruid,@username,@ebstag,'wrk',1,convert(bigint,@ss2));
--查询出刚插入的fmid
select top 1 @mfmid = fm_id from mps_forum
where fm_cmpid = @cmpid  and fm_usertype = @usertype 
  and fm_cuid = @useruid and fm_type = @ebstype and  fm_ctgid = @ctgid and fm_objid = @objid
  and fm_createdtime = @nowtime  and fm_ffmid = @fmid and fm_workstatus =1 
  and convert(varchar(200),fm_text) = @ss1 and fm_fffmid = @fmid and fm_work = 'wrk' and fm_workcuid = convert(bigint,@ss2)
--insert into #messagecuid(cuid) values(convert(bigint,@ss2))
set @mmcuid = convert(bigint,@ss2)
--给协作人发送消息
if(@ebstype='biz') exec mps_forum_message_attention_biz @cmpid,@mfmid,@ctgid,@objid,@ebstype,@mmcuid
else if(@ebstype='doc') exec mps_forum_message_attention_doc @cmpid,@mfmid,@ctgid,@objid,@ebstype,@mmcuid
end
set @existsids = 1
end

if(@existsids = 1)
begin
-- 更新fm_work值
update mps_forum set fm_work = 'wrkall' where fm_id = @fmid
-- 查询出子评论
select * ,0 fm_count,0 fm_userlike,0 fm_userdislike,
getdate() as fm_enddate 
from mps_forum a
where fm_ffmid = @fmid or fm_fffmid = @fmid
end


if(@ffmid=0 and @fffmid = 0 and (@ebstype = 'biz' or @ebstype ='doc') and @fmid >0 and @ctgid >0 and @objid>0 and @message=0 )
begin
-- 给创建人发送消息
if(@ebstype = 'biz') exec mps_forum_message_biz @cmpid,@ctgid,@objid,@fmid,@ebstype,@useruid
else if(@ebstype = 'doc') exec mps_forum_message_doc @cmpid,@ctgid,@objid,@fmid,@ebstype,@useruid
exec mps_forum_managepeople @cmpid,@ebstype,@ctgid,@objid,@useruid,0,@fmid

declare @tbname1 nvarchar(50)
declare @dbname1 nvarchar(50)
declare @fkey1   nvarchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@ctgid,0,@tbname1 output,@fkey1 output ,@dbname1 output 

if exists (select 1 from syscolumns  where id=object_id(@tbname1) and name='ord_forum')
begin
declare @sql22 nvarchar(1000)
set @sql22= 'update '+@tbname1+' set ord_forum = ord_forum +1 where ord_id='+convert(varchar(50),@objid)
exec(@sql22)
end
end else 
if(@ffmid>0 and @fffmid>0 and @fmid>0  and @ctgid >0 and @objid>0 and @message=0)
begin
--给评论人发送消息
if @wordcuid = @useruid 
begin
if(@ebstype='biz') exec mps_forum_message_attention_biz @cmpid,@fmid,@ctgid,@objid,@ebstype,@fcuid
else if(@ebstype='doc') exec mps_forum_message_attention_doc @cmpid,@fmid,@ctgid,@objid,@ebstype,@fcuid
end else
begin
if(@ebstype='biz') exec mps_forum_message_attention_biz @cmpid,@fmid,@ctgid,@objid,@ebstype,@wordcuid
else if(@ebstype='doc') exec mps_forum_message_attention_doc @cmpid,@fmid,@ctgid,@objid,@ebstype,@wordcuid
end
end
set nocount off
end