create or replace procedure SP_YF_MZCF00_EDIT
-- modification history
-- Person      Date        Comments
-- lintj       2012.06.15  create
-- qks         2015.07.24  增加入参Psfksjc
-- linshu      2021.09.02  指定科室开出的处方性质都为零(YF_MZCF00.CFXZ00=0) SSMZ9-20210902-001
-- chenxz      2022.04.29  新增入参PZDMC00、PZDM000 诊断名称和诊断码 for SSMZ9-20220303-001
-- linshu      2022.08.26  生成毒麻处方序列号; for YF9-20220718-002
-- linzetao    2022.11.16  增加入参Pdbrzjh(代办人证件号), 保存到YF_MZCF00.DBRZJH   SSMZ9-20221111-001
-- linminghan  2023.04.23  修改处方时对YF_MZCF00.CFDGS0进行更新;
(
  Popflag  in     number               ,             --操作标志 0：增加  1：修改  2：删除
  Pcflsh0  in out varchar2             ,             --处方流水号
  Pyfbmbh  in     number               ,             --药房部门编号
  Pghid00  in     number               ,             --病人挂号流水号
  Pysgzh0  in     number               ,             --医师工作号
  Pssks00  in     number               ,             --所属科室
  Pghks00  in     number               ,             --开方科室
  Pzyts00  in     number               ,             --中药贴数
  Pypdlbh  in     char                 ,             --药品大类编号
  Pcfxz00  in     char                 ,             --处方性质
  Pbzid00  in     number               ,             --病症ID号
  Psrrq00  in     char                 ,             --输入日期
  Psrsj00  in     char                 ,             --输入时间
  Pbrdz00  in     varchar2             ,             --病人地址,用于毒麻
  Pbz0000  in     varchar2             ,             --备注
  Pbccfh0  in     number               ,             --被冲销处方流水号
  Psfksly  in     char                 ,             --是否科室领药
  Pcfsrbz  in     char                 ,             --处方输入标志
  Pzydjs0  in     number               ,             --中药代煎数
  Pczy000  in     number               ,             --操作员
  Pczyks0  in     number               ,             --操作员科室
  Pcommit  in     varchar2 default 'N' ,             --是否提交
  Pzxcgbz  out    number               ,             --执行成功标志 1成功 0失败
  Ptsxx00  out    varchar2             ,             --执行结果提示信息，成功的情况下也可能有警告信息
  Pxxxx00  out    varchar2             ,             --执行结果详细信息，成功的情况下也可能有警告信息，一般用来显示参数等指标
  Psfksjc  in     varchar2 default 'N' ,             --是否科室寄存
  Pzdmc00  in     varchar2 default ''  ,             --诊断名称
  Pzdm000  in     varchar2 default ''  ,             --诊断码
  Pdbrzjh  in     varchar2 default ''                --代办人证件编号
)
as
  Vsysdate    date;                                --日期变量
  Vcounter    number(14,4);                        --计数器变量
  Ecustom     exception;
  Vmzcf00     YF_MZCF00%rowtype;
  v_BFXSCS    XT_XTCS00.VALUE0%type;
  Vghh000     SF_BRXXB0.GHH000%type;
  Vbrid00     SF_BRXXB0.BRID00%type;
  Vxm0000     SF_BRXXB0.XM0000%type;
  Vxb0000     SF_BRXXB0.XB0000%type;
  Vcsrq00     SF_BRXXB0.CSRQ00%type;
  Vfbbh00     SF_BRXXB0.FBBH00%type;
  Vysxm00     BM_YGBM00.ZWXM00%type;
  MZPY_JZHSFZJFY XT_XTCS00.VALUE0%type;--取号机是否直接发药
  Vyjdjh0     YJ_YW0000.YJDJH0%type;
  Vcfdgs0     YF_MZCF00.CFDGS0%type;
  Vpygzls     YF_MZCF00.PYGZLS%type;
  Vbzmc00     BM_DMZD00.BZMC00%type;
  vCFXZ00     YF_MZCF00.CFXZ00%type;
  vCFXZW0     XT_XTCS00.NAME00%type;
  vDMCFBH     YF_MZCF00.DMCFBH%type;

/*  cursor c_MZCFMX IS
       select YPDLBH,YPNM00,YPMC00,YPGG00,CFDW00,ZHL000,GJDJ00,CXCFID,LSDJ00,SFSLYK,ROUND(SUM(YPZSL0),2) YPZSL0
        from YF_MZCFMX
       where CFLSH0=Pcflsh0 and SFZBY0='N'
       Group BY YPDLBH,YPNM00,YPMC00,YPGG00,CFDW00,ZHL000,GJDJ00,CXCFID,LSDJ00,SFSLYK;*/
begin
  Pzxcgbz := 0;
  Vsysdate := sysdate;
  Pxxxx00 :=nvl(to_char(Popflag),'null')||','||nvl(to_char(Pcflsh0),'null')||','||nvl(to_char(Pyfbmbh),'null')||','||
            nvl(to_char(Pghid00),'null')||','||nvl(to_char(Pysgzh0),'null')||','||nvl(to_char(Pssks00),'null')||','||
            nvl(to_char(Pzyts00),'null')||','||nvl(to_char(Pypdlbh),'null')||','||
            nvl(to_char(Pcfxz00),'null')||','||nvl(to_char(Pbzid00),'null')||','||
            nvl(to_char(Psrrq00),'null')||','||nvl(to_char(Psrsj00),'null')||','||
            nvl(to_char(Pbrdz00),'null')||','||nvl(to_char(Pbz0000),'null')||','||
            nvl(to_char(Pbccfh0),'null')||','||nvl(to_char(Psfksly),'null')||','||nvl(to_char(Pcfsrbz),'null')||','||
            nvl(to_char(Pzydjs0),'null')||','||nvl(to_char(Pczy000),'null')||','||nvl(to_char(Pczyks0),'null')||','||
            nvl(to_char(Pcommit),'null')||','||nvl(to_char(Psfksjc),'null');
  SP_TransLog(Vsysdate,'SP_YF_MZCF00_EDIT',Pczy000,Pczyks0,Pxxxx00);

  Ptsxx00:='';

  --取出参数:某些费别在出发发药时状态为审核
  select nvl(max(trim(VALUE0)),'N') into v_BFXSCS from XT_XTCS00 where NAME00='YF_BDYCFFB';

  select nvl(max(trim(VALUE0)),'N') into MZPY_JZHSFZJFY from XT_XTCS00 where NAME00='MZPY_JZHSFZJFY';

  select GHH000,BRID00,XM0000,XB0000,CSRQ00,FBBH00 into Vghh000,Vbrid00,Vxm0000,Vxb0000,Vcsrq00,Vfbbh00
    from SF_BRXXB0 where GHID00=Pghid00;

  select ZWXM00 into Vysxm00 from BM_YGBM00 where YGBH00=Pysgzh0;

  select nvl(max(trim(BZMC00)),'') into Vbzmc00 from BM_DMZD00 where BZID00 = Pbzid00;

  --指定科室开出的处方，处方性质都为0   SSMZ9-20210902-001
  select nvl(max(trim(VALUE0)),'0') into vcfxzw0 from XT_XTCS00 where NAME00='SS_CFHJCFXZW0';
  if ( instrb(','||vcfxzw0||',',','||to_char(Pghks00)||',') > 0 ) then
    vcfxz00 := '0';
  else
    vcfxz00 := Pcfxz00;
  end if;
  
  --生成毒麻处方序列号
  if nvl(Pcfxz00,'0') <> '0' then
    begin 
      select  substrb('00000000'||to_char(to_number(nvl(max(A.DMCFBH),'00000000')) + 1),length('00000000'||to_char(to_number(nvl(max(A.DMCFBH),'00000000')) + 1))-7,8)
           into vDMCFBH 
      from YF_MZCF00 A where A.DMCFBH is not null and length(trim(A.DMCFBH))='8'; 
    exception
      when others then
        vDMCFBH := '00000001'; 
    end; 
  end if;  


  if Popflag = 0 then--增加
    select count(*) into Vcounter from SF_BRXXB0 where GHID00=Pghid00 and GHLB00= -99999;
    if Vcounter>0 then
      Ptsxx00:='病人挂号为预挂号,不能开具处方!';
      raise Ecustom;
    end if;

    if Pcflsh0 is null then
        select SF_YK_GETLSH('MZYF_MZCFH') into Pcflsh0 from dual;
    end if;

    insert into YF_MZCF00(
        CFLSH0,             --处方流水号
        YFBMBH,             --药房部门编号
        GHID00,             --病人挂号流水号
        GHH000,             --挂号号
        BRID00,             --病人ID号
        BRXM00,             --病人姓名
        XB0000,             --性别
        CSRQ00,             --出生日期
        YSGZH0,             --医师工作号
        YSXM00,             --医师姓名
        SSKS00,             --所属科室(医师)
        GHKS00,             --开方科室(病人)
        ZYTS00,             --中药贴数
        YPDLBH,             --药品大类编号
        CFXZ00,             --处方性质
        BZID00,             --病症ID号
        BZMC00,             --病症名称
        CFZT00,             --处方状态
        SRRQ00,             --输入日期
        SRSJ00,             --输入时间
        BRDZ00,             --病人地址,用于毒麻
        BZ0000,             --备注
        BCCFH0,             --被冲销处方流水号
        SFKSLY,             --是否科室领药
        CFSRBZ,             --处方输入标志
        ZYDJS0,             --中药代煎数
        CZY000,             --操作员
        SFKSJC,             --是否科室寄存
        ZDMC00,             --诊断名称
        ZDM000,             --诊断码
        DMCFBH,             --毒码处方编号
        DBRZJH              --代办人证件号
    ) values(
        Pcflsh0,
        Pyfbmbh,
        Pghid00,
        Vghh000,
        Vbrid00,
        Vxm0000,
        Vxb0000,
        Vcsrq00,
        Pysgzh0,
        Vysxm00,
        Pssks00,
        Pghks00,
        Pzyts00,
        Pypdlbh,
        vCFXZ00,
        Pbzid00,
        Vbzmc00,
        '0',
        nvl(Psrrq00,to_char(sysdate,'YYYYMMDD')),
        nvl(Psrsj00,to_char(sysdate,'HH24:MI:ss')),
        Pbrdz00,
        Pbz0000,
        Pbccfh0,
        Psfksly,
        Pcfsrbz,
        Pzydjs0,
        Pczy000,
        Psfksjc,
        Pzdmc00,
        Pzdm000,
        vDMCFBH,
        Pdbrzjh
    );

    SP_YJ_MZJYFSQ(
      0,
      Pcflsh0,             --处方流水号
      Vyjdjh0,             --医技单据号 (yjdjh1,yjdjh2)
      Pczy000,             --输入：操作员编码
      Pczyks0,             --操作员科室
      'N',             --是否提交
      Pzxcgbz,             --执行成功标志 1成功 0失败
      Ptsxx00,             --执行结果提示信息，成功的情况下也可能有警告信息
      Pxxxx00              --执行结果详细信息，成功的情况下也可能有警告信息，一般用来显示参数等指标
    );

    if v_BFXSCS<>'N' then
      select instr(','||v_BFXSCS||',', ','||to_char(Vfbbh00)||',') into Vcounter from dual;
      if Vcounter > 0 then
          update YF_MZCF00 set CFZT00 = '4' where CFLSH0 = Pcflsh0;
      end if;
    end if;

    select SF_YF_JudgeCFDGS(0, Pcflsh0) into Vcfdgs0 from dual;
    select SF_YF_JudgePYGZLS(Pcflsh0) into Vpygzls from dual;
    update YF_MZCF00 set CFDGS0 = Vcfdgs0, PYGZLS = Vpygzls where CFLSH0 = Pcflsh0;
  end if;

  if Popflag = 1 then
      select * into Vmzcf00 from YF_MZCF00 where CFLSH0 = Pcflsh0;
      update YF_MZCF00 set
          CFLSH0 = nvl(Pcflsh0,CFLSH0),
          YFBMBH = nvl(Pyfbmbh,YFBMBH),
          YSGZH0 = nvl(Pysgzh0,YSGZH0),
          YSXM00 = nvl(Vysxm00,YSXM00),
          SSKS00 = nvl(Pssks00,SSKS00),
          ZYTS00 = nvl(Pzyts00,ZYTS00),
          YPDLBH = nvl(Pypdlbh,YPDLBH),
          CFXZ00 = nvl(Pcfxz00,CFXZ00),
          BZID00 = nvl(Pbzid00,BZID00),
          BZMC00 = nvl(Vbzmc00,BZMC00),
          SRRQ00 = nvl(Psrrq00,SRRQ00),
          SRSJ00 = nvl(Psrsj00,SRSJ00),
          BRDZ00 = nvl(Pbrdz00,BRDZ00),
          BZ0000 = nvl(Pbz0000,BZ0000),
          BCCFH0 = nvl(Pbccfh0,BCCFH0),
          SFKSLY = nvl(Psfksly,SFKSLY),
          CFSRBZ = nvl(Pcfsrbz,CFSRBZ),
          ZYDJS0 = nvl(Pzydjs0,ZYDJS0),
          CZY000 = nvl(Pczy000,CZY000),
          SFKSJC = nvl(Psfksjc,SFKSJC),
          ZDMC00 = nvl(Pzdmc00,ZDMC00),
          ZDM000 = nvl(Pzdm000,ZDM000),
          DMCFBH = nvl(DMCFBH,vDMCFBH),
          DBRZJH = nvl(Pdbrzjh,DBRZJH)
      where CFLSH0 = Pcflsh0; 
  
    select SF_YF_JudgeCFDGS(0, Pcflsh0) into Vcfdgs0 from dual;
    update YF_MZCF00 set CFDGS0 = Vcfdgs0 where CFLSH0 = Pcflsh0;       
  end if;

  if Popflag = 2 then
      if MZPY_JZHSFZJFY='N' then
        update YF_CFDL00 set ZT0000='3' where CFLSH0=Pcflsh0;
      end if;
      SP_YJ_MZJYFSQ(
        1,
        Pcflsh0,             --处方流水号
        Vyjdjh0,             --医技单据号 (yjdjh1,yjdjh2)
        Pczy000,             --输入：操作员编码
        Pczyks0,             --操作员科室
        'N',             --是否提交
        Pzxcgbz,             --执行成功标志 1成功 0失败
        Ptsxx00,             --执行结果提示信息，成功的情况下也可能有警告信息
        Pxxxx00              --执行结果详细信息，成功的情况下也可能有警告信息，一般用来显示参数等指标
      );
      delete from YF_MZCF00 where CFLSH0=Pcflsh0;
  end if;

  Pzxcgbz:=1;
  if pcommit='Y' then
     commit;
  end if;
exception
  when no_data_found then
    raise_application_error(-20001,substrb('没有找到数据！*'||sqlerrm,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;