Create Or Replace Trigger TR_ZY_ZY_BRXXB0_INSERT
Before insert or update of fbbh00
on "SD_ZY"."ZY_BRXXB0"
FOR  EACH  ROW
declare
   ls_BRID00  ZY_BRXXB0.BRID00%TYPE;
   Vcounter   number(10);
   ls_YBZXLB  IC_YBBRLB.ybzxlb%TYPE;
   ls_SFXNH0  IC_YBBRLB.sfxnh0%TYPE;
   LS_INCS00  varchar2(100);
   LS_TYJE00    number(12,2);
   LS_TYJE01    number(12,2);
   --异常声明 同一病案号,只能对应一个病人ID
   E_BRID00    EXCEPTION;     
   V_XT_YJXMKFHIS xt_xtcs00.value0%type;
     V_JY_LIS_AND_HIS xt_xtcs00.value0%type;
     V_FLAG00   char(1);
-- MODIFICATION HISTORY
-- Person      Date        Comments
--             2007.06.20 zy_brxxb0增加医保卡号字段，由触发器自动维护
-- zhr         2008.01.14 农合病人不允许欠费。
-- zhr         2008.08.31 增加从BM_BRXXB0取fbbh00值
-- xzw         2009.03.26 获取病人的医保中心类别
-- liuj        2010.05.28 农合病人允许欠费金额参数控制
-- liuj        2013.03.08 增加参数ZY_YXYBBRQFJE_JMYB居民医保跟职工医保可分开控制允许欠费金额 for ZYSF-20121219-002 
--zhangyc      2016.12.06 ZY_BRZHXX.TYJE00改为函数(SF_ZY_ZHYXQF)统一处理  by ZYSF-20161116-001 
-- daihq       2017.01.03 增加参数控制，检验与智业HIS做接口时，inserting不处理 for LIS-20170118-001
-- linshu      2018.11.30 剔除掉对病案号的判断 BY ZYSF-20181123-001
begin
   if inserting then
      --医技项目是否由非本公司的HIS进行扣费(智业HIS接口使用，涉及触发器tr_yj_yw0000_adel、TR_ZY_ZY_BRXXB0_INSERT),Y;是,N:否,默认N
      begin
          select nvl(max(trim(value0)),'N') into V_XT_YJXMKFHIS from xt_xtcs00 where name00='XT_YJXMKFHIS';
      exception
       when others then
            V_XT_YJXMKFHIS:='N';
      end;
      --使用检验接口,NPJYJK：南平检验接口；TO_HIS：弘扬HIS；TO_ZHIYHIS：智业HIS；TO_BSOFT：杭州创业HIS；默认为空
      begin
          select nvl(max(trim(value0)),'') into V_JY_LIS_AND_HIS from xt_xtcs00 where name00='JY_LIS_AND_HIS';
      exception
       when others then
            V_JY_LIS_AND_HIS:='';
      end;  
    
        if (trim(V_JY_LIS_AND_HIS)='TO_ZHIYHIS') and (trim(V_XT_YJXMKFHIS)='Y') then
            return;
        end if;  
        
      --select  NVL(max(BRID00),0)  into  ls_BRID00  from  ZY_BRXXB0  where  MRN000 = :NEW.MRN000 and CYRQ00 is null;
      select  ybbrlb,yblb00,fbbh00,ybkh00 into :new.ybbrlb,:new.yblb00,:new.fbbh00,:new.ybkh00 from bm_brxxb0 where brid00=:new.brid00;
      if :NEW.SZYS00=0 then
         :NEW.SZYS00:=:NEW.SXYS00;
      end if;   
      begin
          select ybzxlb,sfxnh0 into ls_YBZXLB,ls_SFXNH0 from IC_YBBRLB where YBLB00=:new.yblb00 AND FBBH00=:new.FBBH00;
      exception
          when others then
               ls_YBZXLB :='0';
               ls_SFXNH0 :='0';
      end;
      :new.YBZXLB := ls_YBZXLB;
      --IF  ls_BRID00 <> 0   THEN   --该病案号的病人已有记录
      --    if  ls_BRID00 <> :NEW.BRID00  then
      --        RAISE  E_BRID00;
      --    end if;
      --END IF;
   end if;
   if updating then
     begin
      select sfxnh0,YBZXLB into ls_SFXNH0,ls_YBZXLB from IC_YBBRLB where YBLB00=:new.yblb00 AND FBBH00=:new.FBBH00;
      exception
          when others then
               ls_SFXNH0 :='0';
     end;
     -- by ZYSF-20161116-001 改为函数(SF_ZY_ZHYXQF)统一处理--begin--
    /*
     --铁路医院要求每个医保病人无限欠费
     if ((:new.fbbh00=3) or (nvl(:old.fbbh00,0)=3)) and :new.yblb00<>'@'  and ls_SFXNH0<>'1' then
      begin
        select to_number(trim(Value0)) into Vcounter from xt_xtcs00 where name00='ZY_YXYBBRQFJE';
      exception
        when others then
        Vcounter:=0;
      end;  
      if Vcounter>0 then
         if :new.fbbh00=3 then
            update zy_brzhxx set tyje00=-1*Vcounter where brid00=:new.brid00 and sfdj00='0';
         else
            update zy_brzhxx set tyje00=0 where brid00=:new.brid00  and sfdj00='0';
         end if;
      end if;
      -- 泉州市人民医院要求居民医保允许欠费金额分开控制 for ZYSF-20121219-002
      if :new.ybbrlb='C' then
        begin
          select to_number(trim(Value0)) into Vcounter from xt_xtcs00 where name00='ZY_YXYBBRQFJE_JMYB';
        exception
          when others then
          Vcounter:=-1;
        end;  
        if Vcounter>=0 then
           if :new.fbbh00=3 then
              update zy_brzhxx set tyje00=-1*Vcounter where brid00=:new.brid00 and sfdj00='0';
           else
              update zy_brzhxx set tyje00=0 where brid00=:new.brid00  and sfdj00='0';
           end if;
        end if;
      end if;
      ------------------- end if----------------------
     end if;
     --2010.05.28 邵武人民医院要求医保新农合允许欠费金额分开控制
     if  ((:new.fbbh00=3) or (nvl(:old.fbbh00,0)=3)) and ls_SFXNH0='1' then
      begin
        select to_number(trim(Value0)) into Vcounter from xt_xtcs00 where name00='ZY_YXXNHBRQFJE';
      exception
        when others then
        Vcounter:=0;
      end;  
      if Vcounter>0 then
         if :new.fbbh00=3 then
            update zy_brzhxx set tyje00=-1*Vcounter where brid00=:new.brid00 and sfdj00='0';
         else
            update zy_brzhxx set tyje00=0 where brid00=:new.brid00  and sfdj00='0';
         end if;
      end if;      
     end if; 
   */
    -- by ZYSF-20161116-001 改为函数(SF_ZY_ZHYXQF)统一处理--end-- 
 LS_INCS00:='1'||'|'||:new.yblb00||'|'||:new.YBBRLB||'|'||:new.FBBH00||'|'||LS_YBZXLB||'|'||LS_SFXNH0;--类型|YBLB00|YBBRLB|FBBH00|YBZXLB|SFXNH0
 begin
     select SF_ZY_ZHYXQF(:new.zyid00 ,:new.brid00,LS_INCS00) into LS_TYJE00 from dual;
 exception
    when others then
     LS_TYJE00:=0;
 end;
   select TYJE00 into LS_TYJE01 from ZY_BRZHXX where BRID00=:new.brid00;
   if LS_TYJE01=0 then
     update zy_brzhxx set tyje00=-1*LS_TYJE00 where brid00=:new.brid00  and sfdj00='0';   
   end if; 
 end if;
exception
     WHEN E_BRID00 THEN
        RAISE_APPLICATION_ERROR(-20911,'该病案号已被别的病人所使用!'||'！*');
          ROLLBACK;
     WHEN OTHERS THEN
      RAISE_APPLICATION_ERROR(-20912,substr(nvl(sqlerrm,'出错原因不详'),1,40)||'，请记录此信息并和系统管理员联系！'||'！*');
      ROLLBACK;
end;