create or replace procedure SP_YF_ZYDJDJCZ
-- MODIFICATION HISTORY
-- Person      Date        Comments
-- qks         2013.02.20  Create:1、头煎 必须在 浸泡 30分钟之后进行 2、目前是按步骤进行扫描 3、"是否全送"未处理
-- qks         2013.03.15  增加字段YF_JYXXB0.SFCYDY,YPYFMC; 处理"是否全送"
-- qks         2013.03.20  写入YF_JYXXB0.GHH000
-- qks         2013.04.25  修改VCFLSH0取值
-- qks         2013.05.10  修改YF_JYXXB0.ZYSFJJ取值;送药登记不验证二煎
-- qks         2013.05.13  增加字段YF_JYXXB0.DQKS00
-- qks         2013.05.28  增加字段YF_JYXXB0.FYDLXH
-- qks         2013.06.19  YF_JYXXB0.YPYFSM改为写入煎药方式名称
-- qks         2013.06.20  入参PTXM000值开头值有变
-- qks         2013.07.03  允许直接改为"已送药"状态
-- qks         2013.08.19  修改VZYSFJJ门诊取值
-- qks         2014.06.10  二煎 必须在 头煎 15分钟之后进行;送药 必须在 二煎 10分钟之后进行
-- qks         2014.07.16  增加字段YF_JYXXB0.JPCZY0;FYCZY0保留二煎操作员
-- qks         2015.02.11  住院中药处方的条形码开头改为Z
-- qks         2015.08.21  前台传入具体的操作时间
-- qks         2016.09.18  前台传入具体的浸泡时间、头煎时间、二煎时间; for YF9-20160911-001
                           /*浸泡时间 在交接时间上自动加20分钟以内的时间
                             头煎时间 在浸泡时间上自动加30分钟以上的时间
                             二煎时间 分成三挡:  4剂以内 27分钟;7剂一下 35分钟; 7剂以上 40分钟
                           */
-- qks         2016.09.18  特殊煎药打印：要求门诊和住院处方有开中药细目用法名称,写入YF_JYXXB0.XMYFMC; for YF9-20160918-001
-- qks         2017.10.31  PDA修改内容归档; for YF9-20171031-001
-- liwm        2018.03.26  重新归档,解决条码无法扫描的问题 for YF9-20180326-003
-- qks         2019.09.25  重新整理:YF_JYXXB0增加了先煎操作员、后入操作员、当前状态值; for YF9-20190925-001
-- qks         2019.10.22  省人民:浸泡30分钟后,才可以进行先煎或头煎;先煎（如流程有需要）15分钟后才可以进行头煎;
--                         头煎共40分钟，后入（是在头煎结束前10分钟下锅）;二煎：头煎40分钟后才可以开始，15分钟后才可以结束; for YF9-20191024-002
-- liwm        2019.11.07  二煎需要再头煎40分钟才能进行，改为二煎需要在头煎20分钟之后进行. YF9-20191107-001
-- liwm        2019.10.30  增加住院医嘱执行日期 YF9-20191015-004
-- qks         2019.12.13  省人民煎药流程时间控制要求按照以下规则修改: for YF9-20191213-003
--                         1、浸泡30分钟，才能开始煎药(头煎或先煎)
--                         2、先煎进行10分钟就可以进行头煎
--                         3、头煎进行20分钟就可以二煎（如果有先煎，20分钟是包括先煎和头煎的间隔时间）
--                         4、如果有后入，那么后入是在头煎进行了10分钟就可以后入了，后入10分钟就可以进行二煎
--                         5、二煎进行15分钟，就可以结束煎药
-- linshu      2021.02.03  省人民煎药流程时间控制要求按照以下规则修改: for YF9-20210126-001
--                         1、没有特殊煎煮(先煎、后下)处方，头煎20min+二煎15min；
--                         2、有先煎的处方，先煎10min+头煎20min+二煎15min
--                         3、有后下的处方，头煎10min+后下10min+二煎15min
--                         4、有先煎后下的处方,先煎10min+头煎10min+后下10min+二煎15min
-- linshu      2021.09.13  住院摆药时，全送模式下,13点前的病人服药日期应为摆药日期; for YF9-20210906-001
-- linshu      2022.04.06  在进行二煎确认时，注释掉判断先煎十分钟后才能进行二煎。确保无论有没有先煎的流程都要头煎二十分钟才能进行二煎登记; for YF9-20220406-001
-- linshu      2022.06.23  接入时(POPFLAG=1)写入YF_JYXXB0.KDJFRQ(扣代煎费日期)、YF_JYXXB0.KDJFRQ(扣代煎费日期)值; for YF9-20220527-002
-- linshu      2022.09.06  修正了住院病人取扣代煎时间公式; for YF9-20220805-001
-- linshu      2022.11.28  增加日志功能; for YF9-20221111-001
-- dsm         2022.12.12  增加调用SP_WLZS_JSSJTB YF9-20221214-001
-- heyi        2023.04.23  将发药与打包分别使用自己的操作标志（原发药操作标志与打包操作标志相同） for YF9-20230505-001
-- linshu      2023.05.15  入参PJYJHM0是“null“字符串的，转为空值处理; for YF9-20230508-001
-- linminghan  2023.08.29  送药时(POPFLAG=7)住院代煎记录送药日期、送药时间及送药人; for YF9-20230821-001
-- linminghan  2024.01.08  现场归档; for YF9-20231224-001
-- heyi        2024.02.19  补归档YF13-20231120-001 YDDJ-20231218-001 需现场提供给his9开发归档
(
  POPFLAG  in     varchar2,             --操作标志，1:接收 2:浸泡 3:头煎 4:二煎 5:结束 6:打包 7:送药
  PTXM000  in     varchar2,             --条码:门诊='1'+YF_MZCF00.CFLSH0,住院='0'+YF_YZYPSQ.YZID00
  PCZY000  in     number,               --操作员
  PXTCZY0  in     number,               --系统操作员
  PCZYKS0  in     number,               --操作员科室
  PCOMMIT  in     varchar2 default 'N', --是否提交
  PZXCGBZ  out    number,               --执行成功标志 1成功 0失败
  PTSXX00  out    varchar2,             --执行结果提示信息，成功的情况下也可能有警告信息
  PXXXX00  out    varchar2,             --执行结果详细信息，成功的情况下也可能有警告信息，一般用来显示参数等指标
  PSFQX00  in     varchar2 default 'N', --是否取消操作
  PBZ0000  in     varchar2 default '' , --备注
  Ptqfzsl  in     number default 0,     --业务操作提前分钟数量
  PJPSJ00  in     number default 0,     --浸泡时间 在交接时间上自动加20分钟以内的时间
  PTJSJ00  in     number default 0,     --头煎时间 在浸泡时间上自动加30分钟以上的时间
  PEJSJ00  in     number default 0,     --二煎时间 4剂以内 27分钟
  PEJSJ01  in     number default 0,     --二煎时间 7剂一下 35分钟
  PEJSJ02  in     number default 0,     --二煎时间 7剂以上 40分钟
  PJYJHM0  in     varchar2 default 0,   --煎药机
  PBZJHM0  in     varchar2 default 0,   --打包机
  PZXRQ00  in     varchar2 default ''   --住院医嘱执行日期
)
as
  VCOUNT0   number;
  Vsysdate  date;                        --日期变量

  VBRXM00   BM_BRXXB0.BRXM00%type;
  VBRID00   BM_BRXXB0.BRID00%type;
  VBRXB00   BM_BRXXB0.BRXB00%type;
  VBRCSRQ   BM_BRXXB0.BRCSRQ%type;
  VZYGHID   YF_YZYPSQ.ZYID00%type;
  VBQBMBH   YF_YZYPSQ.BQBMBH%type;
  VCH0000   YF_YZYPSQ.CH0000%type;
  VJPKSSJ   YF_JYXXB0.JYKSSJ%type;
  VZYDJS0   YF_YZYPSQ.ZYDJS0%type;
  VYYRQ00   YF_YZYPSQ.YYRQ00%type;
  VPFCZY0   BM_YGBM00.YGBH00%type;
  VFYRQ00   YF_MZCF00.FYRQ00%type;
  VFYSJ00   YF_MZCF00.FYSJ00%type;
  VYPYFMC   YF_JYXXB0.YPYFMC%type;
  VYPYFSM   YF_JYXXB0.YPYFSM%type;
  VGHH000   YF_JYXXB0.GHH000%type;
  VSYRQ00   YF_JYXXB0.SYRQ00%type;
  VDQKS00   YF_JYXXB0.DQKS00%type;
  VFYDLXH   YF_CFDL00.FYDLXH%type;
  VXMYFMC   YF_JYXXB0.XMYFMC%type;
  VJYJSSJ   YF_JYXXB0.JYJSSJ%type;
 -- VJYJHM0   YF_JYXXB0.JYJHM0%type;
 -- VBZJHM0   YF_JYXXB0.BZJHM0%type;
 -- VJYJSRQ   YF_JYXXB0.JYJSRQ%type;

  VCFLSH0   YF_MZCF00.CFLSH0%type;       --处方号
  VSFCYDY   number(1);                   --是否出院带药
  VDJSFQS   number(1);                   --代煎是否全送
  VCZLXMC   varchar2(20);                --操作类型名称
  VBQLXMC   varchar2(20);                --标签类型名称
  POPFLAG_P char(1);                     --前一状态
  VCZLXMC_P varchar2(20);                --操作类型名称(前一状态)
  VMZZYBZ   char(1);                     --门诊住院标志:0门诊1住院
  VLSRQ00   char(8);                     --临时日期变量
  VZYSFJJ   char(1);                     --是否急煎 N否 Y是
  VCZRQSJ   varchar2(16);
  VEJSJ00   number(7,2);                 --二煎时间(分钟)
  ECUSTOM   exception;                   --错误变量
  VXJSJ00   YF_JYXXB0.XJSJ00%type;       --先煎结束时间
  VHRSJ00   YF_JYXXB0.HRSJ00%type;       --后入结束时间
  VSFQS00   YF_JYXXB0.SFQS00%type;
  Vparams    varchar2(400);

begin
    Vparams :='POPFLAG='||nvl(POPFLAG,'null')||';'||'PTXM000='||nvl(PTXM000,'null')||';'||'PCZY000='||nvl(to_char(PCZY000),'null')||';'||
            'PXTCZY0='||nvl(to_char(PXTCZY0),'null')||';'||'PCOMMIT='||nvl(PCOMMIT,'null')||';'||'PSFQX00='||nvl(PSFQX00,'null')||';'||
            'PBZ0000='||nvl(to_char(PBZ0000),'null')||';'||'Ptqfzsl='||nvl(to_char(Ptqfzsl),'null')||';'||'PJPSJ00='||nvl(to_char(PJPSJ00),'null')||';'||
            'PTJSJ00='||nvl(to_char(PTJSJ00),'null')||';'||'PEJSJ00='||nvl(to_char(PEJSJ00),'null')||';'||'PEJSJ01='||nvl(to_char(PEJSJ01),'null')||';'||
            'PEJSJ02='||nvl(to_char(PEJSJ02),'null')||';'||'PJYJHM0='||nvl(to_char(PJYJHM0),'null')||';'||'PBZJHM0='||nvl(to_char(PBZJHM0),'null')||';'||
            'PZXRQ00='||nvl(PZXRQ00,'null')||';';
  SP_TransLog(sysdate,'SP_YF_ZYDJDJCZ',PCZY000,PCZYKS0,Vparams);
  PZXCGBZ := 0;
  if nvl(Ptqfzsl,0)<>0 and POPFLAG<>'1' then
    Vsysdate := sysdate-(Ptqfzsl/(24*60));
  else
    Vsysdate := sysdate;
  end if;

  if POPFLAG = '1' then VCZLXMC := '接收';POPFLAG_P:='0';VCZLXMC_P:='';
  elsif POPFLAG = '2' then VCZLXMC := '浸泡';POPFLAG_P:='1';VCZLXMC_P:='接收';
  elsif POPFLAG = '3.1' then VCZLXMC := '先煎';POPFLAG_P:='2';VCZLXMC_P:='浸泡';
  elsif POPFLAG = '3' then VCZLXMC := '头煎';POPFLAG_P:='2';VCZLXMC_P:='浸泡';
  elsif POPFLAG = '3.3' then VCZLXMC := '后入';POPFLAG_P:='2';VCZLXMC_P:='头煎';
  elsif POPFLAG = '4' then VCZLXMC := '二煎';POPFLAG_P:='3';VCZLXMC_P:='头煎';
  elsif POPFLAG = '5' then VCZLXMC := '结束';POPFLAG_P:='4';VCZLXMC_P:='二煎';
  elsif POPFLAG = '6' then VCZLXMC := '打包';POPFLAG_P:='5';VCZLXMC_P:='结束';
  elsif POPFLAG = '7' then VCZLXMC := '送药';POPFLAG_P:='6';VCZLXMC_P:='打包';
  end if;
  VSFCYDY := 0;
  VDJSFQS := 0;
  VCFLSH0 := substr(PTXM000,2,length(PTXM000));
  if substr(VCFLSH0,1,1)='0' and mod(length(VCFLSH0),2)=1 then
    VCFLSH0 := substr(VCFLSH0,2,length(VCFLSH0));
  end if;

  if substr(PTXM000,1,1)='0' then
     VMZZYBZ:='0';
     VBQLXMC:='门诊';
  elsif (substr(PTXM000,1,1)='1' or upper(substr(PTXM000,1,1))='Z') then
     VMZZYBZ:='1';
     VBQLXMC:='住院';
  else
     PTSXX00:='条码【'||PTXM000||'】开头只能是1或0 ！，请重新确认';
     raise Ecustom;
  end if;

  if POPFLAG = '1' then
    if substr(PTXM000,1,1)='0' then
      select nvl(max(GHID00),0),max(BRXM00),max(BRID00),max(XB0000),max(CSRQ00),max(GHKS00),max(FYRQ00),max(ZYDJS0),
             max(FYRQ00),max(FYSJ00),max(PFR000),max(GHH000),max(ZYSFJJ) --decode(max(CFDGS0),'3','Y','4','Y','N')
        into VZYGHID,VBRXM00,VBRID00,VBRXB00,VBRCSRQ,VBQBMBH,VYYRQ00,VZYDJS0,VFYRQ00,VFYSJ00,VPFCZY0,VGHH000,VZYSFJJ
        from yf_mzcf00 where cflsh0=VCFLSH0;
      select max(YPYFMC) into VYPYFMC  from yf_mzcfmx where cflsh0=VCFLSH0 and rownum=1;
      select nvl(max(FYDLXH),0) into VFYDLXH from YF_CFDL00 where cflsh0=VCFLSH0;
      for XMYF in (select distinct XMYFMC from yf_mzcfmx where cflsh0=VCFLSH0 and XMYFMC is not null) loop
        VXMYFMC := substrb(VXMYFMC||replace(replace(XMYF.XMYFMC,'(',''),')','')||',',1,50);
      end loop;
    else
      select nvl(max(a.ZYID00),0),max(b.XM0000),max(b.BRID00),max(b.XB0000),max(b.CSRQ00),max(a.BQBMBH),max(a.CH0000),
             decode(max(decode(C.DJSFQS,'1',1,0)),'1',decode(sign(13-to_number(substrb(max(a.FYSJ00),1,2))),1,max(a.FYR000),max(a.YYRQ00)),max(a.YYRQ00)),   -- 住院摆药时，全送模式下,13点前的病人服药日期应为摆药日期 YF9-20210906-001
             max(a.ZYDJS0),max(a.FYRQ00),max(a.FYSJ00),
             max(decode(a.YZXZ00,'3',1,0)),max(decode(C.DJSFQS,'1',1,0)),max(a.FYR000),max(trim(a.YPYFMC)),max(b.ZYH000),decode(upper(substrb(max(a.PCMC00),1,3)),'S.T','Y','N'),
             max(a.YFSMMC),max(a.DQKS00)
        into
             VZYGHID,VBRXM00,VBRID00,VBRXB00,VBRCSRQ,VBQBMBH,VCH0000,
             VYYRQ00,
             VZYDJS0,VFYRQ00,VFYSJ00,
             VSFCYDY,VDJSFQS,VPFCZY0,VYPYFMC,VGHH000,VZYSFJJ,
             VYPYFSM,VDQKS00
        from yf_yzypsq a,zy_brxxb0 b,bq_ypyz00 c where a.yzid00=to_number(VCFLSH0) and a.ZYID00=b.zyid00 and a.yzid00=c.yzid00(+);

      select nvl(max(JYFSMC),VYPYFSM) into VYPYFSM from YS_JYFS00 a,BQ_YPYZ00 b where a.BH0000=b.JYFS00 AND B.YZID00=to_number(VCFLSH0);
      for XMYF in (select distinct XMYFMC from BQ_YPYZMX where YZID00=to_number(VCFLSH0) and XMYFMC is not null) loop
        VXMYFMC := substrb(VXMYFMC||replace(replace(XMYF.XMYFMC,'(',''),')','')||',',1,50);
      end loop;
    end if;
    if substrb(VXMYFMC,-1,1) = ',' then
      VXMYFMC := substrb(VXMYFMC,1,lengthb(VXMYFMC)-1);
    end if;
    if VZYGHID = 0 then
      PTSXX00:='没找到'||VBQLXMC||'条码【'||PTXM000||'】';
      raise Ecustom;
    end if;
    if VZYDJS0=0 then
      PTSXX00:=VBQLXMC||'条码【'||PTXM000||'】对应处方的代煎数为0！，请重新确认';
      raise Ecustom;
    end if;

    if POPFLAG=1 and PEJSJ00>0 then
      if VZYDJS0<=4 then
         VEJSJ00 := PEJSJ00;
      elsif VZYDJS0<=7 then
         VEJSJ00 := PEJSJ01;
      else
         VEJSJ00 := PEJSJ02;
      end if;
    end if;

    begin
      insert into YF_JYXXB0 (
        JYDH00  , --'煎药单号   (SQ_YF_JYXXB0_JYDH00)';
        MZZYBZ  , --'门诊住院标志:''0''门诊''1''住院';
        ZYGHID  , --'住院(ZYID00)或门诊(GHID00)流水号';
        CFLSH0  , --'处方流水号(YF_MZCF00.CFLSH0或YF_YZYPSQ.YZID00)';
        GHH000  , -- 挂号号或住院号
        BRID00  , --'病人ID号    F.K=SF_BRXXB0.BRID00';
        BRXM00  , --'病人姓名    F.K=SF_BRXXB0.XM0000';
        XB0000  , --'性别';
        SFCRB0  , --'是否传染病 0 否';
        ZYSFJJ  , --'是否急煎 N否 Y是';
        CSRQ00  , --'出生日期';
        LYRQ00  , --'领药日期';
        LYSJ00  , --'领药时间';
        LYCZY0  , --'领药操作员';
        SFQS00  , --'是否全送:0否1是';
        BQBMBH  , --病区部门编号
        DQKS00  , --住院科室
        CH0000  , --床号
        JYCS00  , --煎药次数(代煎贴数)
        YYRQ00  , --用药日期
        YPYFMC  , --药品用法名称
        PFCZY0  , --配方操作员
        PFRQ00  , --'配方日期(摆药日期)';
        PFSJ00  , --'配方时间(摆药时间)';
        SFCYDY  , --是否出院带药
        YPYFSM  , --药品用法说明
        FYDLXH  , --发药队列序号

        JPCZY0  , --浸泡操作员
        JPKSRQ  ,
        JPKSSJ  ,

        JYCZY0  , --头煎操作员
        JYRQ00  ,
        JYKSSJ  ,

        EJCZY0  , --二煎操作员
        ECJYRQ  ,
        ECJYSJ  ,
        XMYFMC  ,
        FLAG00  ,
        KDJFRQ  ,
        KDJFSJ
      )
      --values (
      select
        SQ_YF_JYXXB0_JYDH00.nextval,
        VMZZYBZ,
        VZYGHID,
        VCFLSH0,
        VGHH000,
        VBRID00,
        VBRXM00,
        VBRXB00,
        '0',
        VZYSFJJ,
        VBRCSRQ,
        to_char(Vsysdate,'YYYYMMDD'),
        to_char(Vsysdate,'HH24:MI:SS'),
        PCZY000,
        to_char(VDJSFQS),
        VBQBMBH,
        VDQKS00,
        VCH0000,
        VZYDJS0,
        VYYRQ00,
        VYPYFMC,
        VPFCZY0,
        VFYRQ00,
        VFYSJ00,
        to_char(VSFCYDY),
        VYPYFSM,
        VFYDLXH,
      --);
        decode(nvl(PJPSJ00,0),0,null,PCZY000),
        decode(nvl(PJPSJ00,0),0,'20991231',to_char(Vsysdate+PJPSJ00/(24*60),'YYYYMMDD')),
        decode(nvl(PJPSJ00,0),0,null,to_char(Vsysdate+PJPSJ00/(24*60),'HH24:MI:SS')),

        decode(nvl(PTJSJ00,0),0,null,PCZY000),
        decode(nvl(PTJSJ00,0),0,'20991231',to_char(Vsysdate+(PJPSJ00+PTJSJ00)/(24*60),'YYYYMMDD')),
        decode(nvl(PTJSJ00,0),0,null,to_char(Vsysdate+(PJPSJ00+PTJSJ00)/(24*60),'HH24:MI:SS')),

        decode(nvl(VEJSJ00,0),0,null,PCZY000),
        decode(nvl(VEJSJ00,0),0,'20991231',to_char(Vsysdate+(PJPSJ00+PTJSJ00+VEJSJ00)/(24*60),'YYYYMMDD')),
        decode(nvl(VEJSJ00,0),0,null,to_char(Vsysdate+(PJPSJ00+PTJSJ00+VEJSJ00)/(24*60),'HH24:MI:SS')),
        VXMYFMC,
        POPFLAG,
        decode(VMZZYBZ,'0',SF_YF_MZDJKFSJ(VBRID00,VCFLSH0,'1'),SF_YF_ZYDJKFSJ(VZYGHID,VCFLSH0,'1')),
        decode(VMZZYBZ,'0',SF_YF_MZDJKFSJ(VBRID00,VCFLSH0,'2'),SF_YF_ZYDJKFSJ(VZYGHID,VCFLSH0,'2'))
      from dual;
    exception
      when others then
        Ptsxx00 := '条码【'||PTXM000||'】已'||VCZLXMC||'过,请重新确认!*';
        raise Ecustom;
    end;
  else
    select max(LYRQ00||LYSJ00) into VCZRQSJ from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;
    if VCZRQSJ is not null then
      if Vsysdate<to_date(VCZRQSJ,'YYYYMMDDHH24:MI:SS') then
          PTSXX00:=replace('操作时间:'||to_char(Vsysdate,'HH24:MI:SS')||',小于接收时间:'||substrb(VCZRQSJ,9,8),':','：');
          raise Ecustom;
      end if;
    end if;
  end if;

  if POPFLAG = '2' then
    Update YF_JYXXB0 set JPCZY0=nvl(PCZY000,JPCZY0), --JYCZY0=nvl(PCZY000,JYCZY0),
      JPKSRQ=to_char(Vsysdate,'YYYYMMDD'),JPKSSJ=to_char(Vsysdate,'HH24:MI:SS'),FLAG00=POPFLAG
    where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(JPKSRQ,'20991231')='20991231';
    if sql%notfound then
        PTSXX00:='条码【'||PTXM000||'】没找到 或者 已'||VCZLXMC||'过,请重新确认';
        raise Ecustom;
    end if;
  end if;



  if POPFLAG = '3' then

    select max(JPKSRQ),max(JPKSSJ),max(XJSJ00) into VLSRQ00,VJPKSSJ,VXJSJ00 from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;
    if VLSRQ00 is null then
        PTSXX00:='没找到'||VBQLXMC||'条码【'||PTXM000||'】';
        raise Ecustom;
    elsif VLSRQ00='20991231' then
        PTSXX00:='条码【'||PTXM000||'】未进行'||VCZLXMC_P||'过,请重新确认';
        raise Ecustom;
    end if;

    --头煎 必须在 浸泡30分钟或先煎10分钟(如果有需要)之后进行;
    if VXJSJ00 is not null then
      if Floor((Vsysdate-to_date(VLSRQ00||VXJSJ00,'yyyymmddhh24:mi:ss'))*24*60)<10 then
        PTSXX00:=VBQLXMC||'条码【'||PTXM000||'】'||'先煎'||to_char(Floor((Vsysdate-to_date(VLSRQ00||VXJSJ00,'yyyymmddhh24:mi:ss'))*24*60))||'分钟,未到最低10分钟规定！请重新确认';
        raise Ecustom;
      end if;
    --if Floor((Vsysdate-to_date(VLSRQ00||VJPKSSJ,'yyyymmddhh24:mi:ss'))*24*60)<1 then
    elsif Floor((Vsysdate-to_date(VLSRQ00||VJPKSSJ,'yyyymmddhh24:mi:ss'))*24*60)<30 then
        PTSXX00:=VBQLXMC||'条码【'||PTXM000||'】'||VCZLXMC_P||to_char(Floor((Vsysdate-to_date(VLSRQ00||VJPKSSJ,'yyyymmddhh24:mi:ss'))*24*60))||'分钟,未到最低30分钟规定！请重新确认';
        raise Ecustom;
    end if;

    Update YF_JYXXB0 set JYCZY0=nvl(PCZY000,JYCZY0),JYRQ00=to_char(Vsysdate,'YYYYMMDD'),JYKSSJ=to_char(Vsysdate,'HH24:MI:SS'),JYJHM0=nvl(decode(PJYJHM0,'null',null,PJYJHM0),JYJHM0),FLAG00=POPFLAG
     where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(JYRQ00,'20991231')='20991231';
    if sql%notfound then
        PTSXX00:='条码【'||PTXM000||'】已'||VCZLXMC||'过,请重新确认';
        raise Ecustom;
    end if;
  end if;


if POPFLAG = '3.1' then

    select max(JPKSRQ),max(JPKSSJ) into VLSRQ00,VJPKSSJ from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;
    if VLSRQ00 is null then
        PTSXX00:='没找到'||VBQLXMC||'条码【'||PTXM000||'】';
        raise Ecustom;
    elsif VLSRQ00='20991231' then
        PTSXX00:='条码【'||PTXM000||'】未进行'||VCZLXMC_P||'过,请重新确认';
        raise Ecustom;
    end if;

    --先煎或头煎,必须在浸泡 30分钟 之后进行;
    if Floor((Vsysdate-to_date(VLSRQ00||VJPKSSJ,'yyyymmddhh24:mi:ss'))*24*60)<30 then
        PTSXX00:=VBQLXMC||'条码【'||PTXM000||'】'||VCZLXMC_P||to_char(Floor((Vsysdate-to_date(VLSRQ00||VJPKSSJ,'yyyymmddhh24:mi:ss'))*24*60))||'分钟,未到最低30分钟规定！请重新确认';
        raise Ecustom;
    end if;

--    Update YF_JYXXB0 set JYCZY0=nvl(PCZY000,JYCZY0),XJSJ00=to_char(Vsysdate,'HH24:MI:SS'),JYJHM0=nvl(PJYJHM0,JYJHM0)
--     where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(JYRQ00,'20991231')='20991231';
    --2019.09.25
    Update YF_JYXXB0 set XJCZY0=nvl(PCZY000,XJCZY0),XJSJ00=to_char(Vsysdate,'HH24:MI:SS'),JYJHM0=nvl(decode(PJYJHM0,'null',null,PJYJHM0),JYJHM0),FLAG00=POPFLAG
     where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and XJSJ00 is null;
    if sql%notfound then
        PTSXX00:='条码【'||PTXM000||'】已'||VCZLXMC||'过,请重新确认';
        raise Ecustom;
    end if;
  end if;

--2019.09.25 qks 没用注释掉
--if POPFLAG = '3.2' then
--
--    select max(JPKSRQ),max(JPKSSJ) into VLSRQ00,VJPKSSJ from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;
--    if VLSRQ00 is null then
--        PTSXX00:='没找到'||VBQLXMC||'条码【'||PTXM000||'】';
--        raise Ecustom;
--    elsif VLSRQ00='20991231' then
--        PTSXX00:='条码【'||PTXM000||'】未进行'||VCZLXMC_P||'过,请重新确认';
--        raise Ecustom;
--    end if;
--
--    Update YF_JYXXB0 set JYCZY0=nvl(PCZY000,JYCZY0),JYKSSJ=to_char(Vsysdate,'HH24:MI:SS'),JYJHM0=nvl(PJYJHM0,JYJHM0)
--     where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(JYRQ00,'20991231')='20991231';
--    if sql%notfound then
--        PTSXX00:='条码【'||PTXM000||'】已'||VCZLXMC||'过,请重新确认';
--        raise Ecustom;
--    end if;
--  end if;


if POPFLAG = '3.3' then

    select max(JYRQ00),max(JYKSSJ) into VLSRQ00,VJPKSSJ from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;
    if VLSRQ00 is null then
        PTSXX00:='没找到'||VBQLXMC||'条码【'||PTXM000||'】';
        raise Ecustom;
    elsif VLSRQ00='20991231' then
        PTSXX00:='条码【'||PTXM000||'】未进行'||VCZLXMC_P||'过,请重新确认';
        raise Ecustom;
    end if;

    --后入 必须在头煎开始 10分钟 之后进行;
    if Floor((Vsysdate-to_date(VLSRQ00||VJPKSSJ,'yyyymmddhh24:mi:ss'))*24*60)<10 then
        PTSXX00:=VBQLXMC||'条码【'||PTXM000||'】'||VCZLXMC_P||to_char(Floor((Vsysdate-to_date(VLSRQ00||VJPKSSJ,'yyyymmddhh24:mi:ss'))*24*60))||'分钟,未到最低10分钟规定！请重新确认';
        raise Ecustom;
    end if;

--    Update YF_JYXXB0 set JYCZY0=nvl(PCZY000,JYCZY0),JYRQ00=to_char(Vsysdate,'YYYYMMDD'),JYKSSJ=to_char(Vsysdate,'HH24:MI:SS'),JYJHM0=nvl(PJYJHM0,JYJHM0)
--     where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(JYRQ00,'20991231')='20991231';
    --2019.09.25
    Update YF_JYXXB0 set HRCZY0=nvl(PCZY000,HRCZY0),HRSJ00=to_char(Vsysdate,'HH24:MI:SS'),JYJHM0=nvl(decode(PJYJHM0,'null',null,PJYJHM0),JYJHM0),FLAG00=POPFLAG
     where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and HRSJ00 is null;
    if sql%notfound then
        PTSXX00:='条码【'||PTXM000||'】已'||VCZLXMC||'过,请重新确认';
        raise Ecustom;
    end if;
  end if;

  if POPFLAG = '4' then
    select max(JYRQ00),max(JYKSSJ),max(XJSJ00),max(HRSJ00) into VLSRQ00,VJPKSSJ,VXJSJ00,VHRSJ00 from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;
    if VLSRQ00 is null then
        PTSXX00:='没找到'||VBQLXMC||'条码【'||PTXM000||'】';
        raise Ecustom;
    elsif VLSRQ00='20991231' then
        PTSXX00:='条码【'||PTXM000||'】未进行'||VCZLXMC_P||'过,请重新确认';
        raise Ecustom;
    end if;

    --二煎 必须在 先煎或后入 10分钟之后进行;
    --注释原因，先煎后，都要进入头煎20分钟才有二煎
/*    if VXJSJ00 is not null  then
      if Floor((Vsysdate-to_date(VLSRQ00||VXJSJ00,'yyyymmddhh24:mi:ss'))*24*60)<10 then
         PTSXX00:=VBQLXMC||'条码【'||PTXM000||'】'||'先煎'||to_char(Floor((Vsysdate-to_date(VLSRQ00||VXJSJ00,'yyyymmddhh24:mi:ss'))*24*60))||'分钟,未到最低10分钟规定！请重新确认';
       raise Ecustom;
      end if;
    elsif VHRSJ00 is not null then*/
    if VHRSJ00 is not null then
      if Floor((Vsysdate-to_date(VLSRQ00||VHRSJ00,'yyyymmddhh24:mi:ss'))*24*60)<10 then
        PTSXX00:=VBQLXMC||'条码【'||PTXM000||'】'||'后入'||to_char(Floor((Vsysdate-to_date(VLSRQ00||VHRSJ00,'yyyymmddhh24:mi:ss'))*24*60))||'分钟,未到最低10分钟规定！请重新确认';
        raise Ecustom;
      end if;
    --二煎 必须在 头煎 20分钟之后进行;
    elsif Floor((Vsysdate-to_date(VLSRQ00||VJPKSSJ,'yyyymmddhh24:mi:ss'))*24*60)<20 then
        PTSXX00:=VBQLXMC||'条码【'||PTXM000||'】'||VCZLXMC_P||to_char(Floor((Vsysdate-to_date(VLSRQ00||VJPKSSJ,'yyyymmddhh24:mi:ss'))*24*60))||'分钟,未到最低20分钟规定！请重新确认';
        raise Ecustom;
    end if;

    Update YF_JYXXB0 set EJCZY0=nvl(PCZY000,EJCZY0),ECJYRQ=to_char(Vsysdate,'YYYYMMDD'),ECJYSJ=to_char(Vsysdate,'HH24:MI:SS'),FLAG00=POPFLAG
     where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(ECJYRQ,'20991231')='20991231';

    if sql%notfound then
        PTSXX00:='条码【'||PTXM000||'】已'||VCZLXMC||'过,请重新确认';
        raise Ecustom;
    end if;

  end if;

  --==============================



  if POPFLAG = '5' then
    select max(ECJYRQ),max(ECJYSJ) into VLSRQ00,VJPKSSJ from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;
    if VLSRQ00 is null then
        PTSXX00:='没找到'||VBQLXMC||'条码【'||PTXM000||'】';
        raise Ecustom;
    elsif VLSRQ00='20991231' then
        PTSXX00:='条码【'||PTXM000||'】未进行'||VCZLXMC_P||'过,请重新确认';
        raise Ecustom;
    end if;
    --结束 必须在 二煎 15分钟之后进行;
    if Floor((Vsysdate-to_date(VLSRQ00||VJPKSSJ,'yyyymmddhh24:mi:ss'))*24*60)<15 then
        PTSXX00:=VBQLXMC||'条码【'||PTXM000||'】'||VCZLXMC_P||to_char(Floor((Vsysdate-to_date(VLSRQ00||VJPKSSJ,'yyyymmddhh24:mi:ss'))*24*60))||'分钟,未到最低15分钟规定！请重新确认';
        raise Ecustom;
    end if;

      Update YF_JYXXB0 set JSCZY1=nvl(PCZY000,JSCZY1),JYJSRQ=to_char(Vsysdate,'YYYYMMDD'),JYJSSJ=to_char(Vsysdate,'HH24:MI:SS'),FLAG00=POPFLAG
       where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(JSRQ00,'20991231')='20991231';
    if sql%notfound then
        PTSXX00:='条码【'||PTXM000||'】已'||VCZLXMC||'过,请重新确认';
        raise Ecustom;
    end if;
    begin
       execute immediate 'begin SP_WLZS_JSSJTB(0,1,:v2); end;' using in VCFLSH0;
    exception
       when others then
          VCOUNT0:=0;
    end;
  end if;
  if POPFLAG = '6' then
    select max(JYJSRQ) ,max(JYJSSJ) into VLSRQ00,VJYJSSJ from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;

    --if VLSRQ00 is null then
     --   PTSXX00:='没找到'||VBQLXMC||'条码【'||PTXM000||'】';
     --   raise Ecustom;
    --elsif VLSRQ00='20991231' then
     --   PTSXX00:='条码【'||PTXM000||'】未进行'||VCZLXMC_P||'过,请重新确认';
    --    raise Ecustom;
    --end if;
    --打包

      Update YF_JYXXB0 set DBCZY0=nvl(PCZY000,DBCZY0),DBRQ00=to_char(Vsysdate,'YYYYMMDD'),DBSJ00=to_char(Vsysdate,'HH24:MI:SS'),BZJHM0=nvl(PBZJHM0,BZJHM0),FLAG00=POPFLAG
       where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(DBRQ00,'20991231')='20991231';


    if sql%notfound then
        PTSXX00:='条码【'||PTXM000||'】已'||VCZLXMC||'过,请重新确认';
        raise Ecustom;
    end if;


  end if;



  --=================================

  if POPFLAG = '7' then
  --送药 必须在 二煎 10分钟之后进行;
  --select max(DBRQ00),max(DBSJ00) into VLSRQ00,VJPKSSJ from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;
    --if VLSRQ00 is null then
    --    PTSXX00:='没找到'||VBQLXMC||'条码【'||PTXM000||'】';
    --    raise Ecustom;
    --elsif VLSRQ00='20991231' then
    --    PTSXX00:='条码【'||PTXM000||'】未进行'||VCZLXMC_P||'过,请重新确认';
    --    raise Ecustom;
   -- end if;
  --  select max(ECJYRQ),max(ECJYSJ) into VLSRQ00,VJPKSSJ from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;

  --  select max(ECJYRQ),nvl(max(SYRQ00),'20991231'),count(1) into
  --  VLSRQ00,VSYRQ00,VCOUNT0 from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;
   -- if VCOUNT0=0 then SP_YF_ZYDJDJCZ
    --      (
    --        '1' ,  --操作标志，1:接收 2:浸泡 3:头煎 4:二煎 5:送药 6:药房或病区接收
    --        PTXM000 ,  --条码:门诊='1'+YF_MZCF00.CFLSH0,住院='0'+YF_YZYPSQ.YZID00
    --        PCZY000 ,  --操作员
    --        PXTCZY0 ,  --系统操作员
    --        PCZYKS0 ,  --操作员科室
    --        'N' ,  --是否提交
    --        PZXCGBZ ,  --执行成功标志 1成功 0失败
    --        PTSXX00 ,  --执行结果提示信息，成功的情况下也可能有警告信息
    --        PXXXX00 ,  --执行结果详细信息，成功的情况下也可能有警告信息，一般用来显示参数等指标
    --        PSFQX00 ,  --是否取消操作
    --        PBZ0000  --备注

  --
    --      );
        --PTSXX00:='没找到'||VBQLXMC||'条码【'||PTXM000||'】';
        --raise Ecustom;

--    elsif VLSRQ00='20991231' then
--        PTSXX00:='条码【'||PTXM000||'】未进行'||VCZLXMC_P||'过,请重新确认';
--        raise Ecustom;
 --   end if;
    update YF_JYXXB0 set BZ0000=nvl(PBZ0000,BZ0000) where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;
    select SFCYDY,SFQS00 into VSFCYDY,VSFQS00 from YF_JYXXB0 where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ;
    if PSFQX00 = 'N' then
      --2019.09.25 使用FLAG00='6'
      Update YF_JYXXB0 set SYCZY0=null,SYRQ00=null,SYSJ00=null,FLAG00='7'
         where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(SYRQ00,'20991231')<>'20991231';
                      
    
      if VMZZYBZ='1' then
        if (VSFCYDY='0' and VSFQS00='0') and PZXRQ00 is not null then
          update BQ_YZCFMX set SYRQ00 = to_char(sysdate, 'YYYYMMDD'),SYSJ00=to_char(sysdate, 'HH24:MI:SS'),SYCZY0=PCZY000
            where YZID00 = VCFLSH0 and ZXRQ00 = PZXRQ00;
        else
          update BQ_YZCFMX set SYRQ00 = to_char(sysdate, 'YYYYMMDD'),SYSJ00=to_char(sysdate, 'HH24:MI:SS'),SYCZY0=PCZY000
            where YZID00 = VCFLSH0;
      Update YF_JYXXB0 set SYCZY0=nvl(PCZY000,SYCZY0),SYRQ00=to_char(Vsysdate,'YYYYMMDD'),SYSJ00=to_char(Vsysdate,'HH24:MI:SS'),FLAG00=POPFLAG
         where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(SYRQ00,'20991231')='20991231';
        end if;
      else
        Update YF_JYXXB0 set SYCZY0=nvl(PCZY000,SYCZY0),SYRQ00=to_char(Vsysdate,'YYYYMMDD'),SYSJ00=to_char(Vsysdate,'HH24:MI:SS'),FLAG00=POPFLAG
         where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(SYRQ00,'20991231')='20991231';
      end if;
    else



      if VMZZYBZ='1' then
        Update YF_JYXXB0 set SYCZY0=nvl(PCZY000,SYCZY0),SYRQ00=to_char(Vsysdate,'YYYYMMDD'),SYSJ00=to_char(Vsysdate,'HH24:MI:SS'),FLAG00=POPFLAG
          where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(SYRQ00,'20991231')='20991231';
        if (VSFCYDY='0' and VSFQS00='0') and PZXRQ00 is not null then
          update BQ_YZCFMX set SYRQ00 = to_char(sysdate, 'YYYYMMDD'),SYSJ00=to_char(sysdate, 'HH24:MI:SS'),SYCZY0=PCZY000
            where YZID00 = VCFLSH0 and ZXRQ00 = PZXRQ00;
        else
          update BQ_YZCFMX set SYRQ00 = to_char(sysdate, 'YYYYMMDD'),SYSJ00=to_char(sysdate, 'HH24:MI:SS'),SYCZY0=PCZY000
            where YZID00 = VCFLSH0;
      Update YF_JYXXB0 set SYCZY0=nvl(PCZY000,SYCZY0),SYRQ00=to_char(Vsysdate,'YYYYMMDD'),SYSJ00=to_char(Vsysdate,'HH24:MI:SS'),FLAG00=POPFLAG
         where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(SYRQ00,'20991231')='20991231';
        end if;
      else
        Update YF_JYXXB0 set SYCZY0=nvl(PCZY000,SYCZY0),SYRQ00=to_char(Vsysdate,'YYYYMMDD'),SYSJ00=to_char(Vsysdate,'HH24:MI:SS'),FLAG00=POPFLAG
         where CFLSH0 = VCFLSH0 and MZZYBZ=VMZZYBZ and nvl(SYRQ00,'20991231')='20991231';
        --lxl 2023.06.06 改为PDA扫描送药后发送（只发送门诊患者） YF9-20231224-001
       begin
           execute immediate 'begin SP_WLZS_JSSJTB(0,1,:v2); end;' using in VCFLSH0;
       exception
           when others then
          VCOUNT0:=0;
       end;
      end if;
    end if;
    if sql%notfound then
        PTSXX00:='条码【'||PTXM000||'】已'||VCZLXMC||'过,请重新确认';
        raise Ecustom;
    end if;
  end if;

  PZXCGBZ := 1;
  if PCOMMIT = 'Y' then
     commit;
  end if;
exception
  when No_Data_Found then
    raise_application_error(-20001, substrb('没有找到数据！*'||sqlerrm||PTSXX00, 1, 240));
  when ECUSTOM then
    raise_application_error(-20010, substrb(PTSXX00||'！*', 1, 240));
  when others then
    raise_application_error(-20002, substrb(nvl(sqlerrm, '原因不明出错！*'), 1, 240));
end;
