IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_up_doc_readinfo]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_up_doc_readinfo]
GO
create proc [dbo].[mps_up_doc_readinfo]
@docid bigint,
@branchid int,
@branchname nvarchar(50),
@userdpid int,
@userdepart nvarchar(50),
@useruid int,
@username nvarchar(50),
@docv_click int,
@docv_read int,
@docv_like int,
@cmpid int
as 
begin
set nocount on
declare @docvid bigint;
update mps_document set doc_click = case when doc_click is null then 1 else doc_click + 1 end  where doc_id=@docid and doc_cmpid=@cmpid
select @docvid=docv_id from mps_document_view where docv_cuid = @useruid and docv_docid = @docid and docv_bcid = @branchid and docv_dpid = @userdpid
if @docvid >0 
begin
update mps_document_view set docv_click += 1, docv_lastdate = getdate() where docv_id = @docvid
end 
else
begin
insert into mps_document_view(docv_docid,docv_bcid,docv_branch,docv_click,docv_cuid,docv_cuname,docv_date,docv_depart,docv_dpid,docv_lastdate)
values(@docid,@branchid,@branchname,1,@useruid,@username,getdate(),@userdepart,@userdpid,getdate())
end
select docv_like from mps_document_view
where docv_cuid = @useruid and docv_docid = @docid and docv_bcid = @branchid and docv_dpid = @userdpid
set nocount off
end