create or replace procedure SP_ZS_CLJMBCSYZ_ZYDZBL     --从路径模板产生医嘱
--过程说明
(
   ad_KDYSBH      in      number default 0  ,--开单医生编号
   ad_ZYID00      in      number     ,         --病人住院id
   as_QYRQSJ      in      char       ,         --医嘱起用日期时间'2003050710:00:00'
   as_YEXM00      in      char       ,         --婴儿姓名
   as_MXSTR0      in      char       ,         --jhmxid用,间隔的串
   as_BXSTR0      in      char       ,         --jhmxid:YPNM00用,间隔的串,--为了解决备选替换问题
   ad_CZY000      in    number     ,         --操作员id号
   as_SFYYYZ      in      char default '0'    ,         --是否延用原来的停止医嘱
   as_YHMSG0      OUT   VARCHAR,           --存储过程提示的错误信息
   as_SYSMSG      OUT   VARCHAR            --系统提示的错误信息

)
AS
-- MODIFICATION HISTORY
-- Person      Date        Comments
-- rbh        2014.12.10   CREATE   DZBL-20141210-007
-- xuxm       2015.08.07   DZBL-20150805-002 （急）路径医嘱选择界面：一个路径同一个阶段存在多组中药处方，每组处方都有勾选需要引用的草药，引用出来会乱掉。详细见附件
-- xuxm       2016.08.25   ZYDZBL-20160824-004 （急）路径病人，一组颗粒剂草药生成到医嘱界面的写入到 BQ_YPYZ00.YFBMBH是57（门诊中药房），导致病区护理系统提取药品的时候，提取药房错误（有颗粒药房也有门诊中药房），正确应该都是提到颗粒药房
-- xuxm       2016.10.17   ZYDZBL-20161012-001  模板调用或路径引用时，输液、注射类药品首日次数默认满频次，口服类药品首日次数默认为当前时间到23:59:59之间的频次点数量。
-- xuxm       2017.12.29   ZYDZBL-20171229-002  路径引用生成医嘱保存首日次数没有起用或空的时候报错.
-- xuxm       2018.01.18   ZYDZBL-20180118-001  临床路径医嘱药品生成起用时间空白
-- wuzy       2018.01.31   ZYDZBL-20180126-001  婴儿医嘱需要保存婴儿姓名
-- wuzy       2018.04.12   ZYDZBL-20180412-001  从临床路径生成的药品长嘱医嘱，参数ZS_SYSRCS为N时，不生成首日次数0
-- wuzy       2018.05.11   ZYDZBL-20180509-001  临床路径维护时，婴儿姓名处有空格不属于婴儿医嘱，需要过滤
-- wuzy       2018.06.04   ZYDZBL-20180531-002  过程加上日志 SP_TransLog
-- xuxm       2018.08.27   ZYDZBL-20180827-005 【临床路径】路径医嘱生成界面：同组药品如果后面一条是非药品导致药品备注无法生成。 
-- zouqc      2019.08.05   ZYDZBL-20190719-003  修改过程游标C_LJYZMX的排序顺序。
-- zouqc      2019.12.18   ZYDZBL-20190830-002  BQ_YPYZMX插入PSMC00字段
--------- ------------- -------------------------------

   Vsysdate      date;                       --日期变量
   Vczyxm0       bm_ygbm00.zwxm00%type;      --操作员姓名
   Vyzmxid       bq_yjyz00.yzmxid%type;      --医嘱号
   Vyfbmbh       xt_ypfwfx.yfbmbh%type;      --领药药房
   Vypdlbh       bm_yd0000.lbbh00%type;      --药品大类编号
   Vkdbq00       zy_brxxb0.dqbq00%type;      --当前病区
   Vdqks00       zy_brxxb0.dqks00%type;      --当前科室
   Vzqdw00       bm_pc0000.zqdw00%type;      --频次单位
   Vypyfmc       bm_ypyf00.ypyfmc%type;      --药品用法名称
   Vyflbid       bm_ypyf00.yflbid%type;      --药品用法类别
   Vmrqssj       bm_ypyf00.mrqssj%type;      --药品用法默认起始
   Vqysj00       char(8);                    --起用时间
   Vsyxtsj       number(1);                    --是否使用系统时间作为医嘱的启用时间
   VCLBZSFTS     char(1);                    --是否使用参数中的长临标志
   Vcounter      number(10);                 --计数器变量
   Vparams       varchar2(255);              --参数变量
   Verrmsg       varchar2(255);              --错误提示变量
   V_SRCS00      number(3);                   --首日次数
   V_YYRQ00       char(8);                    --用药日期
   V_SFICU0                     BM_BMBM00.SFICU0%type;
   V_icuks0                     BM_BMBM00.BMBH00%TYPE;
   Ecustom       exception;                  --错误变量
   E_ICUCW0             EXCEPTION;
   E_NOKC               EXCEPTION;    --没有库存
   Vold_no0000   LJ_JDYZMX.NO0000%type;        --组号
   Vfjfscbz      number(2);                   --附加费生成标志
   i             number(2);                   --
   tempStr       varchar(200);                --  临时字符串
   ls_zlxmid     BM_ZLZD00.ZLXMID%type;
   ls_ypnm00     BM_YD0000.YPNM00%type;
   old_ypnm00     BM_YD0000.YPNM00%type;
   ls_ypgg00     BM_YD0000.YPGG00%type;
   ls_zlxmjc     BM_ZLZD00.ZLXMJC%type;
   ls_ypmc00     BM_YD0000.YPMC00%type;
   ls_qzfs00     BM_YD0000.QZFS00%type;
   ls_lysl00     BQ_YPYZMX.LYL000%type;
   ls_ksrq       varchar(10);
   ls_qyrq       varchar(10);
   ls_qysj       varchar(8);
   tmpstr        varchar(200);
   tmpstr1        varchar(200);
   tmpstr2        varchar(200);
   ls_mrpcid     BQ_YPYZ00.PCID00%type;
   ls_mrzqdw     BQ_YPYZ00.ZQDW00%type;
   ls_mrzqcs     BQ_YPYZ00.ZQDW00%type;
   ls_SFXYZTZRQ  XT_XTCS00.VALUE0%type;
   ls_kcsl       YF_YPKCXX.YKKCSL%TYPE;
   ls_yfbmbh     YF_YPKCXX.YFBMBH%TYPE;
   ls_z2j000     BM_YD0000.Z2J000%type;
   LS_SFPDMRYF   number(5);
   ls_SFKYLRSRCS varchar2(10);
   vyszid0       BQ_YPYZ00.YSZID0%type;
   ls_HZBZ00     varchar2(2);
   V_YEXM00   LJ_JDYZMX.YEXM00%type;--婴儿姓名
   cursor C_yzmxKC is select A.ZLXMID,A.YPNM00,A.ZLXMJC,A.LBBH00,A.MCSL00 FROM VW_ZS_LJJHYZXX_ZYDZBL A,LJ_ZRZC00 B
                    where  A.ZYID00=AD_ZYID00 AND A.ZRZCID=B.ZRZCID  AND  NVL(B.JSRQ00,' ')=' '
                    and  instr (AS_MXSTR0,','||TO_CHAR(A.JHMXID)||',')>0 AND A.YPNM00>0;
   cursor C_ljyzmx is select A.LJYZMX,A.ZLXMID,A.YPNM00,A.MCSL00,B.ZQCS00,A.YYZF00,SL0000,B.ZQDW00  TSDW00,A.YFID00,B.ZQCD00 ,B.ZQDW00,
                             A.JHMXID,A.CLBZ00,A.PCID00,A.PCMC00,A.ZLXMJC,A.NO0000,A.LBBH00,A.YZLB00,A.DMBZ01 AS DMBZ00,A.TZTS00,
                             B.ZQCD00 ZQTS00,A.DW0000,A.YFMC00,A.YPGG00,A.JLDW00,A.ZHL000,A.LJID00,A.RCID00,BRZXJD,
                             A.TS0000,A.MXYF00,A.CYCFBZ,A.TZRQ00,A.TZSJ00,A.BZKSRQ,A.YZCLLB,A.BZ0000,A.CJKS00,A.BBID00,A.BBMC00,A.KSRQ00,BRYZZT,A.SSXMJC,A.PSMC00,A.PSID00
                      from   VW_ZS_LJJHYZXX_ZYDZBL A,BM_PC0000 B,LJ_ZRZC00 C
                      where  a.ZRZCID=c.ZRZCID and  nvl(c.JSRQ00,' ')=' ' and  a.PCID00=b.PCID00(+)
                             and  instr (as_MXSTR0,','||to_char(a.JHMXID)||',')>0 and a.ZYID00=ad_ZYID00
                      --order by a.LJID00,a.RCID00,a.CYCFBZ,NO0000,a.XH0000 ;
                     order by CLBZ00,RCID00,CCPXXH,PXH000,LBID00,YZXZLX,NO0000,TLBZXH,XH0000 ;
begin
  Vsysdate:=sysdate;
  as_SYSMSG :='SP_ZS_CLJMBCSYZ_ZYDZBL('||nvl(to_char(AD_KDYSBH),'null')||','||
            nvl(to_char(ad_ZYID00),'null')||','||as_QYRQSJ||','||as_YEXM00||','||as_MXSTR0||','||as_BXSTR0||','||
            nvl(to_char(ad_CZY000),'null')||')';
  SP_TRANSLOG(Vsysdate,'SP_ZS_CLJMBCSYZ_ZYDZBL',ad_CZY000,0,as_SYSMSG);          
  select zwxm00 into Vczyxm0 from bm_ygbm00 where ygbh00=ad_czy000;
  select DQBQ00,DQKS00,YSZID0 into VKDBQ00,VDQKS00,VYSZID0 from ZY_BRXXB0 where ZYID00=AD_ZYID00;
  tmpstr:=' ';
  tmpstr1:=' ';
  tmpstr2:=' ';
  as_YHMSG0:=' ' ;
  select count(*) into LS_SFPDMRYF  from XT_XTCS00 where name00='ZS_LJSFZPDMRYFKC' and VALUE0='Y' ;
  select nvl(max(trim(value0)),'N') into ls_SFKYLRSRCS from XT_XTCS00 where name00='ZS_SFKYLRSRCS';

  for kcmx in C_yzmxKC loop
       --判断库存  begin
         begin
           --select YKKCSL into ls_kcsl from YF_YPKCXX a,BM_YD0000 b where yfbmbh=ls_yfbmbh and a.ypnm00=kcmx.ypnm00 and a.ZTBZ00='0'  and a.YPNM00=b.YPNM00 and b.TZRQ00 is null;
           select YKKCSL into ls_kcsl from XT_YPFWFX a,YF_YPKCXX b  where a.YPLBBH=kcmx.LBBH00 and a.SLBMBH=VKDBQ00
                            and a.YFBMBH=b.YFBMBH and YPNM00=kcmx.YPNM00 and b.ZTBZ00='0'
                            and ((LS_SFPDMRYF=1 and a.MRBZ00='Y') or LS_SFPDMRYF=0)
                            and exists(select 1 from BM_YD0000 where YPNM00=b.YPNM00 and (TZRQ00 is null or TZRQ00>=to_char(sysdate,'YYYYMMDD')))
                            and rownum=1;
         exception
           when others then
           ls_kcsl:=-1000;
         end ;
         if kcmx.MCSL00<=0 then
           if tmpstr2=' ' then
             tmpstr2:=kcmx.zlxmjc;
           else
             tmpstr2:=tmpstr2||','||kcmx.zlxmjc;
           end if;

         end if;
         if ls_kcsl<>-1000 and ls_kcsl<=0 then
           if tmpstr=' ' then
             tmpstr:=kcmx.zlxmjc;
           else
             tmpstr:=tmpstr||','||kcmx.zlxmjc;
           end if;
         elsif ls_kcsl=-1000 then
           if tmpstr1=' ' then
             tmpstr1:=kcmx.zlxmjc;
           else
              tmpstr1:=tmpstr1||','||kcmx.zlxmjc;
           end if;
         end if;
         --判断库存 end

  end loop;
  if tmpstr<>' ' then
    as_YHMSG0:=tmpstr||'药品, 在药房库存不足！';
  end if;
  if  tmpstr1<>' ' then
     if as_YHMSG0=' ' then
       as_YHMSG0:=tmpstr1||'药品,'||' 在药房已暂停使用！';
     else
       as_YHMSG0:=as_YHMSG0||','||tmpstr1||'药品,'||' 在药房已暂停使用！';
     end if;
  end if;
  if tmpstr2<>' ' then
     if as_YHMSG0=' ' then
       as_YHMSG0:=tmpstr2||'药品,'||'剂量不能为0！';
     else
       as_YHMSG0:=as_YHMSG0||','||tmpstr2||'药品,'||' 剂量不对！';
     end if;
  end if;
  if as_YHMSG0<>' ' then
    raise E_NOKC;
  end if;
  begin
    select VALUE0 into ls_SFXYZTZRQ from XT_XTCS00 where name00='ZS_LJ_SFXYZTZRQ';
  exception
    when others then
    ls_SFXYZTZRQ:='N' ;
  end;
  V_icuks0 := Vdqks00;
  select        NVL(MAX(SFICU0),'0') INTO V_SFICU0 FROM BM_BMBM00 WHERE BMBH00=Vkdbq00;
  IF V_SFICU0='1' THEN
     begin
        select a.ksh000 into V_icuks0 from bm_bqks00 a, bm_bmbm00 b where a.ksh000=b.bmbh00 and b.sficu0='1' and a.bqh000=Vkdbq00;
     exception
     when OTHERS then
       raise E_ICUCW0;
        return;
     end;
  END IF;
  --取服务器时间做医嘱启用时间
  select count(*) into Vcounter from xt_xtcs00 where name00='ZS_YZ_DQSJWYZQYSJ' and value0='Y';
  if Vcounter>0 then
     Vsyxtsj:=1;
  else
     Vsyxtsj:=0;
  end if;
  Vqysj00 := substr(as_qyrqsj,12,5)||':00';
  Vold_no0000:=0;
  tmpstr:='';
  old_ypnm00:=0;
  for zlmx in C_ljyzmx loop
      --zlmx.BZKSRQ--> zlmx.ksrq00 modify by 20013.02.19
    if zlmx.ksrq00>=to_char(sysdate,'YYYYMMDD') then ls_qyrq:=zlmx.bzksrq;
    else
      select to_char(sysdate,'YYYYMMDD') into ls_qyrq from dual;     --过期的用引入的日期
    end if;
    --如果为婴儿医嘱，需要替换婴儿姓名
    begin
       --LJ_JDYZMX.YEXM00存在空格
       select trim(YEXM00) into V_YEXM00 from LJ_JDYZMX where JHMXID = ZLMX.JHMXID;
    exception
       when others then
           V_YEXM00:='';
    end;
    --找到病人的婴儿姓名并替换
    if nvl(V_YEXM00,'')<>'' or V_YEXM00 is not null then  
      begin
         select YEXM00 into V_YEXM00 from BQ_JSXX00 where ZYID00=ad_ZYID00 and rownum=1;
      exception
         when others then
             V_YEXM00:='';
      end;
    end if;
  
    if nvl(zlmx.ypnm00,0)=0 then
      if Vold_no0000<>0 and old_ypnm00>0  then  --生成YZNR00,YZNR01
             SP_BQ_YPFJFY (Vyzmxid,ad_CZY000);
         end if; 
         i:=instr(as_BXSTR0,','||to_char(zlmx.jhmxid)||':') ;
         if i>0 then
            tempStr:=substr(as_BXSTR0,i+length(','||to_char(zlmx.jhmxid)||':'),length(as_BXSTR0)-i-length(','||to_char(zlmx.jhmxid)||':')+1);
              if instr(tempStr,',')>0 then
                ls_zlxmid:=to_number(substr(tempStr,1,instr(tempStr,',')-1));
              else ls_zlxmid:=to_number(tempStr);
              end if;

         else
            ls_zlxmid:=zlmx.zlxmid;
         end if;

         if Vsyxtsj=1 then
             select to_char(sysdate,'hh24:mi:ss') into Vqysj00 from dual;
         else
           begin
              select Substr(VALUE0,1,8) into Vqysj00 from xt_xtcs00 where NAME00 = 'BQ_PTYZMRQYSJ';
           exception
              when others then
              Vqysj00:='08:00:00';
           end;
         end if;

       if ls_zlxmid<>'0' then
         begin
          select ZLXMJC into ls_zlxmjc from vw_bq_zlxm00 where ZLXMID=ls_zlxmid  ;
           exception
              when others then
             as_YHMSG0:='项目：'||zlmx.zlxmjc||'已暂停使用！';
           raise E_NOKC;
         end ;
       end if  ;


       --  select ZLXMJC into ls_zlxmjc from bm_zlzd00 where ZLXMID=ls_zlxmid;

         select sq_bq_yzmx00_yzmxid.nextval into VYZMXID from dual where rownum=1;
         insert into BQ_YJYZ00(YZMXID,  ZYID00,  YEXM00,  YZLB00,  CLBZ00,
                          PXSJ00,  ZLXMID,  ZLXMJC,  ZXKS00,  KZYS00,
                          LRRQ00,LRSJ00,  LRRXM0,
                          QYRQ00,  QYSJ00,
                          TS0000,  TSDW00,
                          BQH000,  KSH000,  PCID00,  MCSL00,  DW0000,  PCMC00,
                          YZZT00,  TQFS00,  YZCLLB,  BZ0000,YZLY00,
                          LJID00,MBRCID,BRZXJD,JHMXID,TZRQ00,TZSJ00,
                          CJKS00,BB0000,BBID00,XGLYID,SSXMJC,YSZID0)
         Values           (VYZMXID, AD_ZYID00,nvl(V_YEXM00,as_YEXM00),ZLMX.YZLB00,ZLMX.CLBZ00,
                          to_char(sysdate,'hh24:mi:ss'),LS_ZLXMID,ZLMX.ZLXMJC,SF_ZS_XMZXKS_ZYDZBL(ZLMX.ZLXMID,VKDBQ00,V_ICUKS0),AD_KDYSBH,
                          to_char(sysdate,'yyyymmdd'),to_char(sysdate,'hh24:mi:ss'),VCZYXM0,
                         -- to_char(to_date(substr(as_QYRQSJ,1,10),'yyyy.mm.dd'),'yyyymmdd'),Vqysj00,
                          LS_QYRQ,VQYSJ00,
                          --0, zlmx.tsdw00,
                          decode(ZLMX.CLBZ00,'0',0,ZLMX.ZQTS00), ZLMX.TSDW00,
                          vkdbq00,V_icuks0,NVL(zlmx.pcid00,0),nvl(ZLMX.MCSL00,1),ZLMX.DW0000,nvl(ZLMX.PCMC00,' '),
                          '7','0',nvl(ZLMX.YZCLLB,'正常'),zlmx.bz0000,'1',
                          ZLMX.LJID00,ZLMX.RCID00,ZLMX.BRZXJD,ZLMX.JHMXID,DECODE(LS_SFXYZTZRQ,'Y',ZLMX.TZRQ00,''),decode(LS_SFXYZTZRQ,'Y',ZLMX.TZSJ00,''),
                          decode(ZLMX.CJKS00,0,VKDBQ00,ZLMX.CJKS00),ZLMX.BBMC00,ZLMX.BBID00,decode(AS_SFYYYZ,'0',0,ZLMX.LJYZMX),ZLMX.SSXMJC,VYSZID0);
         --自动生成附加费用
         if sql%notfound then
           Verrmsg:='生成非药医嘱时发生错误！';
           Raise ECustom;
         end if;
         sp_bq_yjfjfy_zydzbl(Vyzmxid,0);
         old_ypnm00:=zlmx.ypnm00;
    elsif zlmx.ypnm00>0 then --药品
           if zlmx.no0000<>Vold_no0000  or  zlmx.no0000=0 then---插入主表zlmx.no0000相同的是同组的
             if Vold_no0000<>0 and old_ypnm00>0  then  --生成YZNR00,YZNR01
                SP_BQ_YPFJFY_zydzbl (Vyzmxid,ad_CZY000);
             end if;
             begin
              /* select decode(zlmx.lbbh00,'1','1','2','2','0') into Vypdlbh from dual;
               select yfbmbh into vyfbmbh from xt_ypfwfx
               where  slbmbh = vkdbq00 and yplbbh =Vypdlbh   and mrbz00 = 'Y' and rownum = 1;*/
                  select  YFBMBH into vyfbmbh
         from ( select M.YFBMBH  from YF_YPKCXX M,XT_YPFWFX N
                where M.YFBMBH=N.YFBMBH and SLBMBH=vkdbq00   and ZTBZ00='0' and   M.YPNM00 = zlmx.ypnm00
                order by MRBZ00 desc, PX0000)
      where ROWNUM<=1;

             exception
               when others then
               Vyfbmbh := 0;
             end;

           if nvl(ZLMX.YFID00,' ')=' ' or Zlmx.Yfid00='' then VYFLBID:=0; end if;
           begin
            select YFLBID,nvl(MRQSSJ,'A') into VYFLBID,VQYSJ00 from BM_YPYF00 where trim(YFID00)=trim(zlmx.yfid00);
            if Vqysj00<>'A' then
               Vqysj00 := substr(VQYSJ00,0,5)||':00';
             end if;
           exception
              when others then
                Vyflbid:=0;
                --Vqysj00:='08:00:00';
           end;
           if Vsyxtsj=1 then
              select to_char(sysdate,'hh24:mi:ss') into Vqysj00 from dual;
           else

             if Vqysj00='A' and Vyflbid in (1,2) then  --取启用时间
               begin
                 select Substr(VALUE0,1,8) into Vqysj00 from Xt_Xtcs00 where NAME00 = 'BQ_ZSYZMRQYSJ';
               exception
                 when others then
                 Vqysj00:='08:00:00';
               end;
             elsif Vqysj00='A' then
               begin
                 select Substr(VALUE0,1,8)  into Vqysj00 from XT_XTCS00 where NAME00 = 'BQ_PTYZMRQYSJ';
               exception
                 when others then
                 Vqysj00:='09:00:00';
               end;
             end if;
           end if;
           if zlmx.mcsl00<=0 then
              Verrmsg:=zlmx.zlxmjc||'每次用量必须输入大于0的数字！';
              raise Ecustom;
           end if;
           select SQ_BQ_YZMX00_YZMXID.NEXTVAL into VYZMXID from dual where rownum=1;
           select PCID00,ZQDW00,ZQCS00 into LS_MRPCID,LS_MRZQDW,LS_MRZQCS from VW_BQ_CYMRPC_ZYDZBL where rownum=1;
           V_YYRQ00:='';
           if zlmx.clbz00 = '0' then
                --长嘱首日次数 全部 为0
               select nvl(max(HZBZ00),'0') into ls_HZBZ00  from BQ_YFLB00 where YFLBID=VYFLBID;
               if V_SRCS00= -1 or LS_SFKYLRSRCS='Y' or (ls_SFKYLRSRCS='M' and ls_HZBZ00='0' ) or V_SRCS00='-1' then
                 V_SRCS00:= 0;
               else
                  --长嘱首日次数 全部 为0
                 begin
                   V_SRCS00:= SF_ZS_SRCS00_DZBL(Vqysj00,ZLMX.YFID00,ZLMX.PCID00);
               exception
                 when others then
                 V_SRCS00:=0;
               end;
               end if;
               
               begin
                   select decode(trim(VALUE0),'Y',0,-1) into V_SRCS00 from XT_XTCS00 where NAME00 = 'ZS_SYSRCS';
               exception
                 when others then
                 V_SRCS00:= -1; 
               end;
            else
                --临嘱首日次数 -1
                V_SRCS00 := -1;
                if (zlmx.lbbh00='2') then
                   V_YYRQ00 :=to_char(sysdate+1,'YYYYMMDD');
                end if;
           end if;

           insert into BQ_YPYZ00( YZID00, ZYID00, YEXM00, YPLBBH,CLBZ00,
                           PXSJ00, KZYS00, LRRQ00, LRSJ00, LRRXM0,
                           QYRQ00, QYSJ00, ZQS000, ZQCD00, ZQDW00,JYCS00,ZQCYTS,
                           BQH000, KSH000, YFBMBH, PCID00, PCMC00,
                           YFID00, YPYFMC, YYZF00, YZZT00,YZLY00,TZRQ00,TZSJ00,BZ0000,SRCS00,YYRQ00,YSZID0)
                  values(  VYZMXID,AD_ZYID00,nvl(V_YEXM00,as_YEXM00),Zlmx.LBBH00,ZLMX.CLBZ00,
                           to_char(sysdate,'hh24:mi:ss'),AD_KDYSBH,to_char(sysdate,'yyyymmdd'),to_char(sysdate,'hh24:mi:ss'),VCZYXM0,
                           ls_qyrq,Vqysj00, 1, nvl(zlmx.zqcd00,1),nvl(zlmx.zqdw00,'天'),decode(ZLMX.LBBH00,'2',decode(ZLMX.YFMC00,'冲服',0,Zlmx.TS0000),0),decode(ZLMX.LBBH00,'2',Zlmx.Ts0000,1),
                           --zlmx.ts0000,
                            VKDBQ00, VDQKS00,VYFBMBH,decode(zlmx.cycfbz,'0',ZLMX.PCID00,'1',LS_MRPCID),nvl(ZLMX.PCMC00,' '),
                           ZLMX.YFID00,ZLMX.YFMC00,ZLMX.YYZF00,'7','1',decode(LS_SFXYZTZRQ,'Y',ZLMX.TZRQ00,''),decode(LS_SFXYZTZRQ,'Y',zlmx.TZSJ00,''),ZLMX.BZ0000,V_SRCS00,V_YYRQ00,VYSZID0);
         Vfjfscbz:=0;

         end if;

           --取得备用药品begin
           i:=instr(AS_BXSTR0,','||to_char(ZLMX.JHMXID)||':');
           if i>0 then
              TEMPSTR:=substr(AS_BXSTR0,i+length(','||to_char(ZLMX.JHMXID)||':'),length(AS_BXSTR0)-i-length(','||to_char(Zlmx.JHMXID)||':')+1);
              if instr(TEMPSTR,',')>0 then
                LS_YPNM00:=to_number(substr(TEMPSTR,1,instr(TEMPSTR,',')-1));
              else LS_YPNM00:=to_number(TEMPSTR);
              end if;
           else
              LS_YPNM00:=ZLMX.YPNM00;
           end if;
           --取得备用药品end

         select YPGG00,YPMC00,QZFS00,Z2J000 into LS_YPGG00,LS_YPMC00,LS_QZFS00,LS_Z2J000 from BM_YD0000 where YPNM00=LS_YPNM00;
       --判断库存  begin
         --select yfbmbh into ls_yfbmbh from xt_ypfwfx where yplbbh=zlmx.lbbh00 and mrbz00='Y' and slbmbh=Vkdbq00 and rownum<=1;
         begin
           --select YKKCSL into ls_kcsl from YF_YPKCXX where yfbmbh=ls_yfbmbh and ypnm00=ls_ypnm00 and ZTBZ00='0' ;
            select YKKCSL into LS_KCSL from XT_YPFWFX a,YF_YPKCXX b  where a.YPLBBH=ZLMX.LBBH00 and a.SLBMBH=VKDBQ00 and a.YFBMBH=b.YFBMBH and YPNM00=ls_YPNM00 and b.ZTBZ00='0'
            and ((LS_SFPDMRYF=1 and a.MRBZ00='Y') or LS_SFPDMRYF=0)
            and rownum=1;
         exception
           when others then
           Ls_Kcsl:=-1;
         end ;
         if LS_KCSL=0 then
           TMPSTR:=LS_YPMC00||', 可能是药品目录已停用或规格已变化,请通知网络中心重新维护！';
           AS_YHMSG0:='YPNM00='||to_char(LS_YPNM00)||'  药品名称'||'库存不足,可能是药品目录已停用或规格已变化,请通知网络中心重新维护！';
           raise E_NOKC;
         elsif LS_KCSL<0 then
           TMPSTR:='在药房已暂停使用！';
           as_YHMSG0:='YPNM00='||to_char(LS_YPNM00)||'  药品名称'||LS_YPMC00||'在药房已暂停使用！';
           raise E_NOKC;
         end if;
         --判断库存 end

         if Zlmx.CLBZ00='2' then
            select decode(LS_QZFS00,'2',ceil(ZLMX.TS0000*ZLMX.MCSL00),'0',ceil(ZLMX.MCSL00)*ZLMX.TS0000,ZLMX.TS0000*ZLMX.MCSL00) into LS_LYSL00 from dual;
         end if;
         if ZLMX.CLBZ00='1' then LS_LYSL00:=ZLMX.SL0000; end if;
         if ZLMX.CLBZ00='0' then
            LS_LYSL00:=SF_BQ_LYLJS0(LS_QZFS00,ZLMX.MCSL00,LS_Z2J000,ZLMX.ZQCS00,1);
            --ls_lysl00:=0;
         end if;
         insert into BQ_YPYZMX(YZID00,YZMXID,YPNM00,YPMC00,YPGG00,
                               MCSL00,JLDW00,LYL000,ZYFYDW,Z2J000,TQFS00,YZCLLB,DMBZ00,XMYFMC,
                               LJID00,MBRCID,BRZXJD,JHMXID,XGLYID,PSMC00,PSID00)
         values(VYZMXID,SQ_BQ_YZMX00_YZMXID.nextval,LS_YPNM00,LS_YPMC00,LS_YPGG00,
              ZLMX.MCSL00,ZLMX.JLDW00, LS_LYSL00, ZLMX.DW0000,ZLMX.ZHL000,'0',nvl(ZLMX.YZCLLB,'正常'),nvl(ZLMX.DMBZ00,'0'),ZLMX.MXYF00,
              ZLMX.LJID00,ZLMX.RCID00,ZLMX.BRZXJD,ZLMX.JHMXID,decode(AS_SFYYYZ,'0',0,ZLMX.LJYZMX),ZLMX.PSMC00,ZLMX.PSID00);
         /*if Vfjfscbz=0 then
           sp_bq_ypfjfy(Vyzmxid,0);
           Vfjfscbz:=1;
         end if;*/
    end if;
    VOLD_NO0000:=ZLMX.NO0000;
    Old_Ypnm00:=ZLMX.YPNM00;
    update LJ_YZMX00 a set LSRCID=(select RCID00 from LJ_JDZXQK where ZRZCID=a.ZRZCID and (to_char(sysdate,'YYYYMMDD') between ZXKSRQ and ZXJSRQ) and DQJDBZ='1' and rownum=1)

           where   LJYZMX=ZLMX.LJYZMX;
  end loop;
  if VOLD_NO0000<>0 and OLD_YPNM00>0  then  --生成YZNR00,YZNR01
      SP_BQ_YPFJFY_ZYDZBL (Vyzmxid,AD_CZY000);
  end if;

exception
  when no_data_found then
    as_YHMSG0:='数据没有找到！';
    raise_application_error(-20001,substr('数据没有找到！*'||SQLERRM||','||As_Sysmsg,1,220));
  when E_ICUCW0 then
    AS_YHMSG0:='取ICU科室错误！';
    Raise_Application_Error(-20004,substrb('取ICU科室错误！*返回信息:'||AS_SYSMSG,1,220));
  when e_nokc then
    Raise_Application_Error(-20010,substrb(As_Yhmsg0||'！*',1,220));
  when Ecustom       then
    as_YHMSG0:=Verrmsg;
    raise_application_error(-20010,substr(VERRMSG||'！*'||AS_SYSMSG,1,220));
  when others        then
    as_YHMSG0:=VERRMSG;
    raise_application_error(-20002,substr(nvl(SQLERRM, '原因不明出错')||'！*'||AS_SYSMSG,1,220));
end;
