IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_account_operation_update]') AND type = 'TR')
DROP TRIGGER [dbo].[mps_account_operation_update]
GO


CREATE trigger [dbo].[mps_account_operation_update] on [dbo].[mps_account_operation] for update as
begin
   declare
      @maxcard  int,
      @ins_accop_id bigint,
      @del_accop_id bigint,
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return
 


-- 需要对预算类的特殊处理一下
declare @dpt table(olddpid int , newdpid int) 
declare @act table(oldaiid int , newaiid int , bcid int) 

if (exists (select 1 from inserted where accop_type='expn' and (accop_category='dptf' or accop_category='dpt'))) 
begin
insert into @dpt(olddpid,newdpid) select distinct accop_objid,0 from inserted where accop_objid>0 
insert into @act(oldaiid,newaiid,bcid) select distinct accop_fiid,0,dp_bcid from inserted,mps_depart where accop_fiid>0 and accop_objid=dp_id 
-- 首先修正，部门是否有预算
update @dpt set newdpid=dp_id from mps_depart where dp_feebudget=0 and dp_id=olddpid 
update @dpt set newdpid=-1*dp_fdpid from mps_depart where dp_id=olddpid and newdpid=0 
update @dpt set newdpid=dp_id from mps_depart where dp_feebudget=0 and dp_id=newdpid*(-1) and  newdpid<0 
update @dpt set newdpid=-1*dp_ffdpid from mps_depart where dp_id=olddpid and newdpid<=0 
update @dpt set newdpid=dp_id from mps_depart where dp_feebudget=0 and dp_id=newdpid*(-1) and  newdpid<0 
update @dpt set newdpid=0 where newdpid<0 
delete @dpt where newdpid=0 
-- 修正费用是否有预算
update @act set newaiid=aib_aiid from mps_account_branch where aib_bcid=bcid and aib_budget=0 and aib_aiid=oldaiid 
update @act set newaiid=-1*ai_faiid  from mps_account_item where ai_id=oldaiid and newaiid=0 
update @act set newaiid=aib_aiid from mps_account_branch where aib_bcid=bcid and aib_budget=0 and aib_aiid=newaiid*(-1) and newaiid<0 
update @act set newaiid=-1*ai_ffaiid  from mps_account_item where ai_id=oldaiid and newaiid<0 
update @act set newaiid=aib_aiid from mps_account_branch where aib_bcid=bcid and aib_budget=0 and aib_aiid=newaiid*(-1) and newaiid<0 
update @act set newaiid=0 where newaiid<0 
delete @act where newaiid=0
end else 
begin 
insert into @dpt(olddpid,newdpid) select distinct accop_objid,accop_objid from inserted 
insert into @act(oldaiid,newaiid) select distinct accop_fiid,accop_fiid from inserted 
end 


declare @t table(
accop_cmpidint,
accop_typechar(4),
accop_categorychar(4),
accop_datechar(10),
accop_objidint,
accop_fiidint,
accop_opdatedatetime,
accop_operationint,
accop_valueint,
accop_valuesuspendint,
accop_bizidint,
accop_novarchar(50),
accop_ordidbigint,
accop_jbidbigint,
accop_backint ,
flag int ) 
insert into @t(
accop_cmpid,
accop_type,
accop_category,
accop_date,
accop_objid,
accop_fiid,
accop_opdate,
accop_operation,
accop_value,
accop_valuesuspend,
accop_bizid,
accop_no,
accop_ordid,
accop_jbid,
accop_back,
flag)
select 
inserted.accop_cmpid,
inserted.accop_type,
inserted.accop_category,
inserted.accop_date,
newdpid,
newaiid,
inserted.accop_opdate,
inserted.accop_operation,
sum(inserted.accop_value) accop_value,
sum(inserted.accop_valuesuspend) accop_valuesuspend,
inserted.accop_bizid,
inserted.accop_no,
inserted.accop_ordid,
inserted.accop_jbid,
inserted.accop_back ,
0 
from inserted ,deleted,@dpt,@act 
where inserted.accop_id=deleted.accop_id and 
inserted.accop_back=1 and deleted.accop_back=1 and 
newdpid=inserted.accop_objid and newaiid=inserted.accop_fiid  
group by inserted.accop_cmpid,
inserted.accop_type,
inserted.accop_category,
inserted.accop_date,
newdpid,
newaiid,
inserted.accop_opdate,
inserted.accop_operation,
inserted.accop_bizid,
inserted.accop_no,
inserted.accop_ordid,
inserted.accop_jbid,
inserted.accop_back 

--accop_operation  0=初始 1=change 11=suspend  100=use  101=use/suspend  use and -suspend 
-- 2 : ready  准备写入 
-- 3 ：写入，执行change动作，同时取消ready 


-- 要分二种情况来记帐
-- 记帐日期能完全对应上的 ， 和记帐日期不能对应上的
-- 系统在有新的记帐日期数据后，会自动删除旧的记帐数据。但如果又有针对旧的数据进行记帐，系统会记在最新的帐上。


update @t set flag=0  
update @t set flag=1 
from mps_account with(nolock)
where accop_cmpid=acc_cmpid and accop_type=acc_type and accop_fiid=acc_fiid and 
  accop_category=acc_category and accop_date=acc_date and 
  accop_objid=acc_objid
  
-- 回退帐务
update mps_account set 
acc_origin=case when accop_operation=0 then 0 else acc_origin end ,-- initial
acc_value=case when accop_operation=0 then acc_changed  -- initial
 when accop_operation=1 then acc_value-accop_value  -- change
 when accop_operation=3 then acc_value-accop_value  -- change 写入 同时 取消ready
else acc_value end ,
acc_used = case when accop_operation=100 then acc_used-accop_value  -- used
   when accop_operation=101 then acc_used-accop_value  --  use and -suspend 
else acc_used end ,

 
acc_changed = case when accop_operation=1 then acc_changed-accop_value  -- change
when accop_operation=3 then acc_changed-accop_value  -- change
else acc_changed end , 
acc_suspended = case when accop_operation=11 then acc_suspended-accop_valuesuspend  -- suspend
  when (accop_operation=101 or accop_operation=100) then acc_suspended+accop_valuesuspend  -- use and -suspend 
else acc_suspended end  ,
acc_ready = case when accop_operation=2 then acc_ready-accop_valuesuspend  -- suspend
when (accop_operation=3 or accop_operation=1) then acc_ready+accop_valuesuspend  -- use and -suspend 
else acc_ready end  
from @t  
where 
accop_cmpid=acc_cmpid and accop_type=acc_type and accop_fiid=acc_fiid and 
accop_category=acc_category and accop_date=acc_date and 
accop_objid=acc_objid and accop_back=0  and flag=1 

-- 如果记帐日期对应不上，那么只需要记：使用   不用关心初始及CHANGE ， 挂帐 在途 
update mps_account set 
acc_used = case when accop_operation=100 then acc_used-accop_value  -- used
   when accop_operation=101 then acc_used-accop_value  --  use and -suspend 
else acc_used end   
from @t  
where 
accop_cmpid=acc_cmpid and accop_type=acc_type and accop_fiid=acc_fiid and 
accop_category=acc_category and accop_date<acc_date and 
accop_objid=acc_objid and accop_back=0  and flag=0

      return

/*  Errors handling  */
error:
    -- raiserror @errno @errmsg
    rollback  transaction
end