create or replace procedure SP_YF_GJYBRZ_MZSPTH(
  PYBJKID   in     varchar2                     ,--[1]输入:医保接口ID
  PJKCJBM   in     varchar2                     ,--[2]输入:接口场景编码
  PBRID00   in     number                       ,--[3]输入:BRID00
  PGHID00   in     number                       ,--[4]输入:GHID00
  PGHH000   in     varchar2                     ,--[5]输入:GHH000
  PCZY000   in     number                       ,--[6]输入:操作员编码
  PCZYXM0   in     varchar2                     ,--[7]输入:操作员姓名
  PCZYKS0   in     number                       ,--[8]输入:操作员科室
  PQQCSID   in     varchar2  default ''         ,--[9]输入:参数中间ID
  PINXML0   in     varchar2  default ''         ,--[10]输入:请求参数(XML)
  POTHER0   in     varchar2  default ''         ,--[11]输入:备用参数
  PCOMMIT   in     varchar2  default 'N'        ,--[12]输入:是否提交事务 Y:提交 N:不提交
  PZXZT00   out    number                       ,--[13]输出:执行状态 0:失败 1：成功
  POUTXML   out    varchar2                     ,--[14]输出:出参(XML)
  PERRMSG   out    varchar2                      --[15]输出:错误信息
)
as
-- MODIFICATION HISTORY
-- Person      Date          Comments
-- wac         2024.06.17    created 移植his9脚本到his13 【3506A】门诊进销存管理商品销售退货A_日志生成    YF13-20240614-001
-- wac         2024.08.29    从HIS9归档追溯码的医保报文相关存储过程 YF13-20240829-001
-- wac         2025.04.07    从HIS9归档追溯码的医保报文相关存储过程 YF13-20250327-001
-- ---------   ----------    -------
  LS_COUNT0                   number(12,2);
  LS_COUNT1                   number(12);
  LS_CZRQ00                   char(8);
  LS_CZSJ00                   char(8);
  LS_DateTimeStr              varchar2(20); --日期时间型:格式为：yyyy-MM-dd HH:mm:ss
  LS_DateStr                  varchar2(10); --日期型:格式为：yyyy-MM-dd
  LS_TimeStr                  varchar2(10); --时间型:格式为：HH:mm:ss
  LS_JKFSZ0                   SF_YBJKMX.JKFSZ0%type;--接口发送值
  LS_JKFHZ0                   SF_YBJKMX.JKFHZ0%type;--接口返回值
  LS_VALUE0                   SF_YBJKMX.VALUE0%type;--请求报文字符串
  LS_HISCS0                   SF_YBJKRZ.HISCS0%type;--HIS前台请求参数
  LS_QQDATA                   SF_YBJKRZ.QQDATA%type;--请求报文
  LS_MainJson                 SF_YBJKRZ.QQDATA%type;--主项Json
  LS_detailJson               SF_YBJKRZ.QQDATA%type;--明细Json
  LS_inputJson                SF_YBJKRZ.QQDATA%type;--input输入
  LS_selinfoDetailJson        SF_YBJKRZ.QQDATA%type;--input输入

  LS_CFLSH0                   YF_MZCF00.CFLSH0%type;    --处方流水号

  LS_YBZXLB                   IC_YBBRLB.YBZXLB%type;    --医保中心类别
  LS_BRZJLX                   BM_BRXXB0.BRZJLX%type;    --病人证件类型
  LS_ZJLXBH                   BM_BRXXB0.ZJLXBH%type;    --病人证件类型编号
  LS_BRZJBH                   BM_BRXXB0.BRZJBH%type;    --病人证件编号
  LS_BRXM00                   BM_BRXXB0.BRXM00%type;    --病人姓名
  ls_YBBRLB                   BM_BRXXB0.YBBRLB%type;    --医保病人类别
  ls_FBBH00                   BM_BRXXB0.FBBH00%type;    --病人费别
  ls_YBLB00                   BM_BRXXB0.YBLB00%type;    --医保类别
  ls_SFBZDJ                   XT_YYXX00.SFBZDJ%type;    --收费标准等级
  ls_YBXMBH                   BM_YBSFXM.XMBH00%type;    --医保收费/药品项目编号
  LS_MZZYBZ                   BM_YBSFDY.MZZYBZ%type;    --门诊住院标志0限门诊1限住院2不限制
  LS_BKEA96                   BM_YBSFDY.BKEA96%type;
  LS_BKEH83                   SF_YBFSMX.BKEH83%type;    --国家标准对应编码

  LS_psn_no_bm                BM_BRXXB0.psn_no%type;
  LS_psn_no_jzb               SF_JZB000.psn_no%type;
  LS_psn_no_rz                SF_YBJKRZ.psn_no%type;    --psn_no:国家医保]人员编号

  in_ZSID00                   varchar2(20);             --追溯id

  LD_JZDH00                   SF_JZB000.JZDH00%type;    --结账单号
  LS_YBDJH0                   SF_JZB000.YBDJH0%type;    --医保单据号
  LS_YBGHH0                   SF_JZB000.YBGHH0%type;    --医保挂号号
  LD_GHID00                   SF_JZB000.MZID00%type;    --挂号id
  LD_ZSID00                   YF_YBZSRZ.ZSID00%type;    --追溯id
  LS_DJH000                   YF_MZCF00.DJH000%type;    --收费单据号
  LS_TYCFH0                   YF_MZCF00.CFLSH0%type;    --退药处方号
  LS_TYRQ00                   YF_MZCF00.FYRQ00%type;    --发药日期
  LS_TYSJ00                   YF_MZCF00.FYSJ00%type;    --发药时间
  LS_TYCZXM                   BM_YGBM00.ZWXM00%type;    --退药操作员

  LS_SCPH00                   YK_RKZSM0.SCPH00%type;    --生产批号
  LS_SCRQ00                   YK_RKZSM0.SCRQ00%type;    --生产日期
  LS_YXQ000                   YK_RKZSM0.YXQ000%type;    --有效期
  LS_YKRKPC                   YK_RKZSM0.YKRKPC%type;    --药品入库批次
  --LS_WBPCID                   YK_RKZSM0.WBPCID%type;    --外部批次id
  LS_LB0000                   YF_YBZSRZ.LB0000%type;    --类别
  LS_SFLB00                   YF_YBZSRZ.SFLB00%type;    --收费类别
  LD_YPZSL0                   number(16,4);             --药品总数量

  LS_SFOTC0                   BM_YD0000.SFOTC0%type;    --是否OTC用药 0 处方药 1 OTC用药
  LS_ZJMZCF                   BM_YD0000.ZJMZCF%type;    --中间门诊拆分 0:可拆分 1:不可拆分
  LS_ZSCLBZ                   BM_YD0000.ZSCLBZ%type;    --追溯码拆零标志，0不可拆零 1门诊拆零 2住院拆零 3门诊住院都拆零

  LS_FYB000                   BM_TYZD00.MC0000%type;    --非医保结算时医保中心配置, 字典分为三段, 用“|”隔开, 默认“3||”: 1段为费别编号, 填写BM_BRFBB0.FBBH00;2段为医保类别, 填写IC_YBBRLB.YBLB00;3段为医保病人类别, 填写IC_YBBRLB.YBBRLB

  LS_fixmedinsName            SF_YBJKRZ.fixmedins_name%type;    --fixmedinsName: 定点医疗机构名称   字符型(200)
  LS_fixmedinsCode            SF_YBJKRZ.fixmedins_code%type;    --fixmedinsCode: 定点医疗机构编号   字符型(20)    --上面已定义，本处不需要再次定义

  LS_Change_bkeh83_bke045     BM_TYZD00.MC0000%type;         --字典Change_bkeh83_bke045

  LS_TYCJ00                   BM_TYCJBM%rowtype;
  LS_drugtracinfoJson         varchar2(4000);

  LS_med_list_codg            varchar2(50);     --1.med_list_codg: 医疗目录编码   字符型(50)
  LS_fixmedins_hilist_id      varchar2(30);     --2.fixmedins_hilist_id: 定点医药机构目录编号   字符型(30)
  LS_fixmedins_hilist_name    varchar2(200);    --3.fixmedins_hilist_name: 定点医药机构目录名称   字符型(200)
  LS_fixmedins_bchno          varchar2(30);     --4.fixmedins_bchno: 定点医药机构批次流水号   字符型(30)
  LS_setl_id                  varchar2(30);     --5.setl_id: 结算ID   字符型(30)
  LS_psn_no                   varchar2(30);     --6.psn_no: 人员编号   字符型(30)
  LS_psn_cert_type            varchar2(6);      --7.psn_cert_type: 人员证件类型   字符型(6)
  LS_certno                   varchar2(50);     --8.certno: 证件号码   字符型(50)
  LS_psn_name                 varchar2(50);     --9.psn_name: 人员姓名   字符型(50)
  LS_manu_lotnum              varchar2(30);     --10.manu_lotnum: 生产批号   字符型(30)
  LS_manu_date                varchar2(20);     --11.manu_date: 生产日期   日期型()
  LS_expy_end                 varchar2(20);     --12.expy_end: 有效期止   日期型()
  LS_rx_flag                  varchar2(3);      --13.rx_flag: 处方药标志   字符型(3)
  LS_trdn_flag                varchar2(3);      --14.trdn_flag: 拆零标志   字符型(3)
  LS_finl_trns_pric           varchar2(20);     --15.finl_trns_pric: 最终成交单价   数值型(16,6)
  LS_sel_retn_cnt             varchar2(20);     --16.sel_retn_cnt: 销售/退货数量   数值型(16,4)
  LS_sel_retn_time            varchar2(20);     --17.sel_retn_time: 销售/退货时间   日期时间型()
  LS_sel_retn_opter_name      varchar2(50);     --18.sel_retn_opter_name: 销售/退货经办人姓名   字符型(50)
  LS_memo                     varchar2(500);    --19.memo: 备注   字符型(500)
  LS_medins_prod_sel_no       varchar2(50);     --20.medins_prod_sel_no: 商品销售流水号   字符型(50)
  LS_mdtrt_sn                 varchar2(30);     --21.mdtrt_sn: 就医流水号   字符型(30)
  LS_drugtracinfo             varchar2(2000);   --22.drugtracinfo: 溯源码节点信息   ()

  LS_drug_trac_codg           varchar2(100);    --1.drug_trac_codg: 药品追溯码   字符型(100)

  LS_retRslt                  varchar2(6);      --1.retRslt: 返回结果   字符型(6)
  LS_msgRslt                  varchar2(2000);   --2.msgRslt: 返回信息   字符型(2000)

  cursor CUR_UPDATE_YBJKMX is
    select YBJKID,JKMXID,JDBM00,ZJDBM0,JKSXBM,JKSXMC,JKFSZ0,JKFHZ0,FSLXBZ,ZDLX00
      from SF_YBJKMX where YBJKID=PYBJKID and FSLXBZ in ('1','3')
        order by JKMXID;

  --查询主项数据
  cursor CUR_QUERY_Main is
    select YBJKID,JKMXID,JDBM00,ZJDBM0,JKSXBM,JKSXMC,JKFSZ0,JKFHZ0,FSLXBZ,ZDLX00
      from SF_YBJKMX where YBJKID=PYBJKID and FSLXBZ in ('1','3') and CJBM00 in ('AllIn')
        order by JKMXID;

  --查询明细数据
  cursor CUR_QUERY_detail is
    select YBJKID,JKMXID,JDBM00,ZJDBM0,JKSXBM,JKSXMC,JKFSZ0,JKFHZ0,FSLXBZ,ZDLX00,VALUE0
      from SF_YBJKMX where YBJKID=PYBJKID and FSLXBZ in ('1','3') and CJBM00=PJKCJBM and XSXH00=LS_COUNT1
        order by JKMXID;

begin
  PZXZT00:=0;   --输出:执行状态 0:失败 1：成功
  POUTXML:=null;--输出:出参(XML)
  PERRMSG:=null;--输出:错误信息
  LS_QQDATA:=null;--请求报文
  LS_MainJson:=null;---主项Json
  LS_detailJson:=null;---明细Json
  LS_inputJson:=null;---input输入

  LS_CZRQ00:=to_char(sysdate,'YYYYMMDD');
  LS_CZSJ00:=to_char(sysdate,'HH24:MI:SS');
  LS_DateTimeStr:=to_char(sysdate,'YYYY-MM-DD HH24:MI:SS'); --日期时间型:格式为：yyyy-MM-dd HH:mm:ss
  LS_DateStr:=to_char(sysdate,'YYYY-MM-DD');               --日期型:格式为：yyyy-MM-dd
  LS_TimeStr:=to_char(sysdate,'HH24:MI:SS');               --时间型:格式为：HH:mm:ss

  LS_Change_bkeh83_bke045 := nvl(SF_XT_TYZDMC('医保新接口参数设置','Change_bkeh83_bke045'),'00');

  begin
    select HISCS0 into LS_HISCS0 from  SF_YBJKRZ where YBJKID=PYBJKID;
  exception
    when others  then
      LS_HISCS0:=null;--HIS前台请求参数
  end;

  begin
    select * into LS_TYCJ00 from BM_TYCJBM where CJBM00=PJKCJBM;
  exception
    when others  then
      PZXZT00 := 0;
      PERRMSG := '未找到有效的场景字典,请联系管理员!';
      return;
  end;

  --查询主项数据
  for C_Main in CUR_QUERY_Main loop
    LS_MainJson := SF_XT_GETJSONSTR(LS_MainJson,C_Main.JKSXBM,C_Main.JKFSZ0,'0');
  end loop;

  if LS_MainJson is null then
    PZXZT00:=0;
    PERRMSG:='主项报文不能为空!';
    return;
  end if;

  if nvl(PBRID00,0) <= 0 then
    PERRMSG := '病人id为空';
    PZXZT00:=0; --执行状态 0:失败 1：成功
    return;
  end if;

  if nvl(PGHID00,0) <= 0 then
    PERRMSG := '挂号id为空';
    PZXZT00:=0; --执行状态 0:失败 1：成功
    return;
  end if;

  --获取前端传入的处方流水号
  LS_CFLSH0 := substrb(SF_XT_GETXMLSTR('cflsh0',PINXML0),1,16);
  if LS_CFLSH0 is null then
    LS_CFLSH0 := substrb(SF_XT_GETXMLSTR('cflsh0',LS_HISCS0),1,16);
  end if;

  if LS_CFLSH0 is null then
    PERRMSG := '处方流水号为空';
    PZXZT00 := 0;
    return;
  end if;

  --获取前端传入的追溯id
  ld_ZSID00 := 0;
  in_ZSID00 := substrb(SF_XT_GETXMLSTR('zsid00',PINXML0),1,16);
  if in_ZSID00 is null then
    in_ZSID00 := substrb(SF_XT_GETXMLSTR('zsid00',LS_HISCS0),1,16);
  end if;

  if in_ZSID00 is not null then
    begin
        ld_ZSID00 := to_number(in_ZSID00);
    exception
      when others then
        ld_ZSID00 := 0;
    end;
  end if;

  if ld_ZSID00 = 0 then
    select max(ZSID00) into ld_ZSID00 from YF_YBZSRZ where MZZYBZ='0' and CFLSH0=LS_CFLSH0 and LB0000 in ('1','2');
  end if;

  select LB0000,nvl(SFLB00,'3') into LS_LB0000,LS_SFLB00 from YF_YBZSRZ where ZSID00=LD_ZSID00;

  if LS_LB0000 in ('2') then
    if LS_SFLB00 = '3' then
      begin
          select b.ZWXM00,a.FYRQ00,a.FYSJ00
          into LS_TYCZXM,LS_TYRQ00,LS_TYSJ00
          from YF_MZCF00 a,BM_YGBM00 b
            where a.FYR000 = b.YGBH00(+)
              and CFLSH0=LS_CFLSH0;
      exception
        when others then
          PERRMSG := '待上传的处方('||LS_CFLSH0||')在门诊处方表中不存在';
          PZXZT00 := 0;
          return;
      end;
    else
      begin
        select a.ZXRXM0,a.ZXRQ00,a.ZXSJ00
          into LS_TYCZXM,LS_TYRQ00,LS_TYSJ00
          from YJ_YW0000 a
            where a.YJDJH0=to_number(LS_CFLSH0);
      exception
        when others then
          PERRMSG := '待上传的耗材医技单('||LS_CFLSH0||')在医技业务表中不存在';
          PZXZT00 := 0;
          return;
      end;
    end if;

  else
   /* select max(TYCFH0) into LS_TYCFH0 from YF_YZZSMX a,YF_CFZSMX b where a.MZZYBZ=b.MZZYBZ and a.CFLSH0=b.CFLSH0 and a.ZSM000=b.ZSM000 and a.ZSID00=ld_ZSID00;*/
    select max(B.CFLSH0) into LS_TYCFH0 from YF_YZZSMX a,YF_TYZSMX b where a.MZZYBZ=b.MZZYBZ and a.CFLSH0=b.CXDH00 and a.ZSM000=b.ZSM000 and a.ZSID00=ld_ZSID00;


    if trim(LS_TYCFH0) is not null then
      if LS_SFLB00 = '3' then
        begin
          select a.JZRQ00,a.JZSJ00,b.ZWXM00 into LS_TYRQ00,LS_TYSJ00,LS_TYCZXM
              from YF_MZCF00 a,BM_YGBM00 b
                where a.CZY000=b.YGBH00
                  and a.CFLSH0=LS_TYCFH0;
        exception
          when others then
            PERRMSG := '退药处方号(CFLSH0='||LS_TYCFH0||')在门诊处方表不存在';
            PZXZT00:=0; --执行状态 0:失败 1：成功
            return;
        end;
      else
        begin
            select a.ZXRXM0,a.ZXRQ00,a.ZXSJ00
              into LS_TYCZXM,LS_TYRQ00,LS_TYSJ00
              from YJ_YW0000 a
                where a.YJDJH0=to_number(LS_TYCFH0);
        exception
          when others then
            PERRMSG := '退费的耗材医技单('||LS_CFLSH0||')在医技业务表中不存在';
            PZXZT00 := 0;
            return;
        end;
      end if;
    end if;
  end if;

  select nvl(max(JZDH00),0) into ld_JZDH00 from YF_YBZSRZ where ZSID00=ld_ZSID00;

  begin
    select A.PSN_NO,A.ZJLXBH,A.BRZJLX,A.BRZJBH,A.BRXM00,YBLB00,YBBRLB,A.FBBH00
      into LS_psn_no_bm,LS_ZJLXBH,LS_BRZJLX,LS_BRZJBH,LS_BRXM00,LS_YBLB00,LS_YBBRLB,LS_FBBH00
      from BM_BRXXB0 A
        where A.BRID00=PBRID00;
  exception
    when others  then
      PERRMSG := '未找到病人基本信息(BRID00='||PBRID00||')';
      PZXZT00:=0; --执行状态 0:失败 1：成功
      return;
  end;

  if LS_ZJLXBH is null and LS_BRZJLX is not null then
    LS_ZJLXBH := substrb(SF_XT_GETYBSXZD('psn_cert_type',LS_BRZJLX,'1','1'),1,6);   --人员证件类型
  end if;

  --获取收费等级标准值
  select SF_XT_GETSFDJBYBMBH(Pczyks0)  into ls_SFBZDJ   from dual;

  begin
    select YBZXLB into ls_YBZXLB from IC_YBBRLB where FBBH00=ls_FBBH00 and YBLB00=ls_YBLB00;
  exception
    when others then
      ls_YBZXLB := null;
  end;

   --获取中间表数据
  begin
    select a.psn_no,a.certno,a.fixmedins_code,a.fixmedins_name
      into LS_psn_no_rz,LS_certno,LS_fixmedinsCode,LS_fixmedinsName
      from SF_YBJKRZ a where a.YBJKID=PYBJKID;
  exception
    when others then
      LS_psn_no_rz:=null;--psn_no:国家医保]人员编号
      LS_certno:=null;
      LS_fixmedinsCode:=null;
      LS_fixmedinsName:=null;
  end;

  LS_YBDJH0 := null;
  LS_YBGHH0 := null;
  LS_PSN_NO_JZB := null;
  if LD_JZDH00 > 0 then
    begin
      select a.YBDJH0,a.YBGHH0,PSN_NO
        into LS_YBDJH0,LS_YBGHH0,LS_PSN_NO_JZB
        from SF_JZB000 a where a.BRID00=PBRID00 and a.JZDH00=LD_JZDH00;
    exception
      when others then
        LS_YBDJH0 := null;
        LS_YBGHH0 := null;
        LS_PSN_NO_JZB := null;
    end;
  end if;

  if nvl(LS_YBDJH0,'0') in ('0') or LS_FBBH00 <> 3 then
    LS_FYB000 := SF_XT_TYZD00('医保追溯码配置','FYBJSYBZXPZ');
    begin
      LS_FBBH00 := to_number(SF_XT_RETURNINDEXEH(LS_FYB000,'|',1));
    exception
      when others then
        LS_FBBH00 := 3;
    end;
    LS_YBLB00 := SF_XT_RETURNINDEXEH(LS_FYB000,'|',2);
    LS_YBBRLB := SF_XT_RETURNINDEXEH(LS_FYB000,'|',3);
    begin
      select YBZXLB into ls_YBZXLB from IC_YBBRLB where FBBH00=ls_FBBH00 and YBLB00=ls_YBLB00;
    exception
      when others then
        ls_YBZXLB := null;
    end;
  end if;

  LS_COUNT1 := 0;
  for cur_ypnm in (select distinct a.YPNM00,b.YPMC00,c.YKRKPC,nvl(a.SFLB00,'3') as SFLB00,d.fixmedins_bchno from YF_YZZSMX a,BM_YD0000 b,YK_RKZSM0 c,YF_CFZSMX d
                          where a.YPNM00=b.YPNM00 and a.YPNM00=c.YPNM00 and a.ZSM000=c.ZSM000
                            and a.CFLSH0=d.CFLSH0 and a.YPQLPC=d.YPQLPC and a.YPNM00=d.YPNM00 and a.ZSM000=d.ZSM000 and nvl(a.SFLB00,'3') = nvl(d.SFLB00,'3')
                            and ZSID00=ld_ZSID00 and a.ZSM000 <> '0' and a.ZT0000 in ('0') and nvl(a.SFLB00,'3')='3'
                   union all
                   select distinct a.YPNM00,b.YPMC00,SF_YF_GETCLCFRKPC('0',a.CFLSH0,a.YPNM00,a.YPQLPC) as YKRKPC,nvl(a.SFLB00,'3') as SFLB00,null fixmedins_bchno from YF_YZZSMX a,BM_YD0000 b
                          where a.YPNM00=b.YPNM00 and ZSID00=ld_ZSID00 and a.ZSM000 = '0' and a.ZT0000 in ('0') and nvl(a.SFLB00,'3')='3'
                            and not exists(select 1 from YF_YZZSMX where ZSID00=ld_ZSID00 and YPNM00=a.YPNM00 and ZSM000 <> '0')
                   union all
                   select distinct a.YPNM00,b.XMMC00,c.YKRKPC,nvl(a.SFLB00,'3') as SFLB00,d.fixmedins_bchno from YF_YZZSMX a,BM_YYSFXM b,YK_RKZSM0 c,YF_CFZSMX d
                          where a.YPNM00=b.SFXMID and a.YPNM00=c.YPNM00 and a.ZSM000=c.ZSM000
                            and a.CFLSH0=d.CFLSH0 and a.YPQLPC=d.YPQLPC and a.YPNM00=d.YPNM00 and a.ZSM000=d.ZSM000 and nvl(a.SFLB00,'3') = nvl(d.SFLB00,'3')
                            and ZSID00=ld_ZSID00 and a.ZT0000 in ('0') and nvl(a.SFLB00,'3')<>'3'
                  ) loop

    if nvl(cur_ypnm.SFLB00,'3') = '3' then
      select wm_concat('{"drug_trac_codg":"'||a.ZSM000||'"}'),count(1),sum(a.YPZSL0)
        into LS_drugtracinfoJson,LS_COUNT0,LD_YPZSL0
        from (
           select a.ZSM000,sum(decode(nvl(a.YPZSL0,0),0,1,decode(nvl(f.ZSMZHL,0),0,1,round(a.YPZSL0/f.ZSMZHL,4)))) as YPZSL0
             from YF_YZZSMX a,YF_CFZSMX b,YK_RKZSM0 c,YF_CFZSMX d,BM_YD0000 f
              where a.CFLSH0=b.CFLSH0
                and a.YPNM00=b.YPNM00
                and a.ZSM000=b.ZSM000
                and nvl(a.SFLB00,'3')=nvl(b.SFLB00,'3')
                and a.YPNM00=c.YPNM00
                and a.ZSM000=c.ZSM000
                and nvl(a.SFLB00,'3')=nvl(c.SFLB00,'3')
                and a.CFLSH0=d.CFLSH0
                and a.YPQLPC=d.YPQLPC
                and a.YPNM00=d.YPNM00
                and a.ZSM000=d.ZSM000
                and nvl(a.SFLB00,'3')=nvl(d.SFLB00,'3')
                and a.YPNM00=f.YPNM00
                and a.ZSID00=ld_ZSID00
                and a.YPNM00=cur_ypnm.YPNM00
                and c.YKRKPC=cur_ypnm.YKRKPC
                and nvl(a.SFLB00,'3')=cur_ypnm.SFLB00
                and d.fixmedins_bchno=cur_ypnm.fixmedins_bchno
                and a.ZT0000 in ('0')
              and a.ZSM000 not in ('0')
            group by a.ZSM000
        ) a;
    else
      select wm_concat('{"drug_trac_codg":"'||a.ZSM000||'"}'),count(1),count(1)
        into LS_drugtracinfoJson,LS_COUNT0,LD_YPZSL0
        from (
           select distinct a.ZSM000
             from YF_YZZSMX a,YF_CFZSMX b,YK_RKZSM0 c,YF_CFZSMX d
              where a.CFLSH0=b.CFLSH0
                and a.YPNM00=b.YPNM00
                and a.ZSM000=b.ZSM000
                and nvl(a.SFLB00,'3')=nvl(b.SFLB00,'3')
                and a.YPNM00=c.YPNM00
                and a.ZSM000=c.ZSM000
                and nvl(a.SFLB00,'3')=nvl(c.SFLB00,'3')
                and a.CFLSH0=d.CFLSH0
                and a.YPQLPC=d.YPQLPC
                and a.YPNM00=d.YPNM00
                and a.ZSM000=d.ZSM000
                and nvl(a.SFLB00,'3')=nvl(d.SFLB00,'3')
                and a.ZSID00=ld_ZSID00
                and a.YPNM00=cur_ypnm.YPNM00
                and c.YKRKPC=cur_ypnm.YKRKPC
                and nvl(a.SFLB00,'3')=cur_ypnm.SFLB00
                and d.fixmedins_bchno=cur_ypnm.fixmedins_bchno
                and a.ZT0000 in ('0')
              and a.ZSM000 not in ('0')
            group by a.ZSM000
        ) a;
    end if;

    --LS_drugtracinfoJson := null;
    --LS_COUNT0 := 0;
    --for cur_zsmx in (
    --     select a.ZSM000 from YF_YZZSMX a,YF_CFZSMX b,YK_RKZSM0 c,YF_CFZSMX d
    --        where a.CFLSH0=b.CFLSH0
    --          and a.YPNM00=b.YPNM00
    --          and a.ZSM000=b.ZSM000
    --          and nvl(a.SFLB00,'3')=nvl(b.SFLB00,'3')
    --          and a.YPNM00=c.YPNM00
    --          and a.ZSM000=c.ZSM000
    --          and nvl(a.SFLB00,'3')=nvl(c.SFLB00,'3')
    --          and a.CFLSH0=d.CFLSH0
    --          and a.YPQLPC=d.YPQLPC
    --          and a.YPNM00=d.YPNM00
    --          and a.ZSM000=d.ZSM000
    --          and nvl(a.SFLB00,'3')=nvl(d.SFLB00,'3')
    --          and a.ZSID00=ld_ZSID00
    --          and a.YPNM00=cur_ypnm.YPNM00
    --          and c.YKRKPC=cur_ypnm.YKRKPC
    --          and nvl(a.SFLB00,'3')=cur_ypnm.SFLB00
    --          and d.fixmedins_bchno=cur_ypnm.fixmedins_bchno
    --          and a.ZT0000 in ('0')
    --     ) loop
    --  LS_drug_trac_codg := SF_XT_GETJSONSTR(null,'drug_trac_codg',cur_zsmx.ZSM000,'0');
    --  LS_drugtracinfoJson := SF_XT_GETJSONSTR(LS_drugtracinfoJson,null,LS_drug_trac_codg,'1');
    --  LS_COUNT0 := LS_COUNT0+1;
    --end loop;

    --if LS_drugtracinfoJson is not null then   --存在待上传的追溯码
      LS_COUNT1 := LS_COUNT1+1;
      if LS_SFLB00 = '3' then
        begin
          select a.SFOTC0,a.ZJMZCF,a.ZSCLBZ
            into LS_SFOTC0,LS_ZJMZCF,LS_ZSCLBZ
              from BM_YD0000 a
                where YPNM00=cur_ypnm.YPNM00;
        exception
          when others then
            PERRMSG := SQLERRM||'YPNM00='||to_char(cur_ypnm.YPNM00)||',药品名称='||cur_ypnm.YPMC00||',药典找不到此药品记录!';
            PZXZT00:=0;
            return;
        end;
      else
        LS_SFOTC0 := '0';
        LS_ZJMZCF := '0';
        LS_ZSCLBZ := '0';
      end if;

      begin
        select C.MZYBBH,C.BKEH83
          into ls_YBXMBH,LS_BKEH83
          from BM_YBFPXM A,VW_BM_YBSFDY_DJBZ C,BM_YBSFXM D
            where a.BH0000 = C.YBMZFP
              and a.YBZXLB = C.YBZXLB
              and C.YBBRLB = ls_YBBRLB
              and C.SFXMID = cur_ypnm.YPNM00
              and C.SFYP00 = decode(cur_ypnm.SFLB00,'3','Y','N') --是否药品,'0'费用,'1'零散处方
              and a.YBZXLB = ls_YBZXLB
              and a.SYBZ00 <> '2'        --非住院
              and c.SFBZDJ = ls_SFBZDJ
              and C.FBBH00 = D.FBBH00(+)
              and C.YBZXLB = D.YBZXLB(+)
              and C.MZYBBH = D.XMBH00(+);

        if substrb(LS_Change_bkeh83_bke045,1,1) in ('1','3') and trim(LS_BKEH83) is not null then
          ls_YBXMBH := LS_BKEH83;
        end if;

      exception
        when no_data_found then
          if cur_ypnm.SFLB00 = '3' then
            PERRMSG := SQLERRM||'YPNM00='||to_char(cur_ypnm.YPNM00)||',药品名称='||cur_ypnm.YPMC00||',查找不到药品对应医保记录!';
          else
            PERRMSG := SQLERRM||'SFXMID='||to_char(cur_ypnm.YPNM00)||',材料名称='||cur_ypnm.YPMC00||',查找不到材料对应医保记录!';
          end if;
          PZXZT00:=0;
          return;
        when others then   --说明是药品,注意有多条记录
          if cur_ypnm.SFLB00 = '3' then
            PERRMSG := SQLERRM||'YPNM00='||to_char(cur_ypnm.YPNM00)||',药品名称='||cur_ypnm.YPMC00||',查找医保编号错误!';
          else
            PERRMSG := SQLERRM||'SFXMID='||to_char(cur_ypnm.YPNM00)||',材料名称='||cur_ypnm.YPMC00||',查找医保编号错误!';
          end if;
          PZXZT00:=0;
          return;
      end;

      --取追溯码对应的药库入库批次
      --select max(YKRKPC) into LS_YKRKPC from YK_RKZSM0 a where YPNM00=cur_ypnm.YPNM00 and exists(select 1 from YF_YZZSMX where ZSID00=LD_ZSID00 and YPNM00=cur_ypnm.YPNM00 and ZT0000 in ('0'));
      LS_YKRKPC := cur_ypnm.YKRKPC;
      --LS_WBPCID := trim(cur_ypnm.WBPCID);

      if LS_YKRKPC is null or LS_YKRKPC = 0 then
        if cur_ypnm.SFLB00 = '3' then
          PERRMSG := SQLERRM||'YPNM00='||to_char(cur_ypnm.YPNM00)||',药品名称='||cur_ypnm.YPMC00||',查找不到有效的药品入库批次!';
        else
          PERRMSG := SQLERRM||'SFXMID='||to_char(cur_ypnm.YPNM00)||',材料名称='||cur_ypnm.YPMC00||',查找不到有效的材料入库批次!';
        end if;
        PZXZT00:=0;
        return;
      end if;

      begin
        select SCPH00,SCRQ00,YXQ000 into LS_SCPH00,LS_SCRQ00,LS_YXQ000 from YK_RKZSM0 where YKRKPC=LS_YKRKPC and rownum=1;
      exception
        when others then
          begin
            select SCPH00,SCRQ00,YPSXRQ into LS_SCPH00,LS_SCRQ00,LS_YXQ000 from YK_YPRKMX where YPRKPC=LS_YKRKPC and rownum=1;
          exception
            when others then
              PERRMSG := SQLERRM||'未找到药品'||to_char(cur_ypnm.YPNM00)||'入库批次'||to_char(LS_YKRKPC)||'的生产批号等有关信息!';
              PZXZT00:=0;
              return;
          end;
      end;

      LS_med_list_codg           := substrb(ls_YBXMBH,1,50);                          --1.med_list_codg: 医疗目录编码   字符型(50)    varchar2(50);
      LS_fixmedins_hilist_id     := substrb(cur_ypnm.ypnm00,1,30);                    --2.fixmedins_hilist_id: 定点医药机构目录编号   字符型(30)    varchar2(30);
      LS_fixmedins_hilist_name   := substrb(cur_ypnm.ypmc00,1,200);                   --3.fixmedins_hilist_name: 定点医药机构目录名称   字符型(200)    varchar2(200);
      if cur_ypnm.fixmedins_bchno is null then
        LS_fixmedins_bchno         := substrb(LS_YKRKPC,1,30);                        --4.fixmedins_bchno: 定点医药机构批次流水号   字符型(30)    varchar2(30);
      else
        LS_fixmedins_bchno         := substrb(cur_ypnm.fixmedins_bchno,1,30);         --4.fixmedins_bchno: 定点医药机构批次流水号   字符型(30)    varchar2(30);
      end if;
      if LS_YBDJH0 in ('0') then
        LS_setl_id               := null;                                             --5.setl_id: 结算ID   字符型(30)    varchar2(30);
      else
        LS_setl_id               := substrb(LS_YBDJH0,1,30);                          --5.setl_id: 结算ID   字符型(30)    varchar2(30);
      end if;
      LS_psn_no                  := substrb(nvl(LS_psn_no_jzb,nvl(LS_psn_no_rz,LS_psn_no_bm)),1,30);        --6.psn_no: 人员编号   字符型(30)    varchar2(30);
      LS_psn_cert_type           := substrb(LS_ZJLXBH,1,6);                           --7.psn_cert_type: 人员证件类型   字符型(6)    varchar2(6);
      LS_certno                  := substrb(LS_BRZJBH,1,50);                          --8.certno: 证件号码   字符型(50)    varchar2(50);
      LS_psn_name                := substrb(LS_BRXM00,1,50);                          --9.psn_name: 人员姓名   字符型(50)    varchar2(50);
      LS_manu_lotnum             := substrb(LS_SCPH00,1,30);                          --10.manu_lotnum: 生产批号   字符型(30)    varchar2(30);
      LS_manu_date               := substrb(to_char(to_date(LS_SCRQ00,'YYYYMMDD'),'YYYY-MM-DD'),1,20);        --11.manu_date: 生产日期   日期型()    varchar2(20);
      LS_expy_end                := substrb(to_char(to_date(LS_YXQ000,'YYYYMMDD'),'YYYY-MM-DD'),1,20);        --12.expy_end: 有效期止   日期型()    varchar2(20);
      if LS_SFOTC0 = '0' then
        LS_rx_flag               := substrb('1',1,3);                                 --13.rx_flag: 处方药标志   字符型(3)    varchar2(3);
      else
        LS_rx_flag               := substrb('0',1,3);                                 --13.rx_flag: 处方药标志   字符型(3)    varchar2(3);
      end if;
      if trim(LS_ZSCLBZ) is null then     --未维护追溯码拆零标志，仍按原方式处理
        if LS_ZJMZCF = '1' then
          LS_trdn_flag             := substrb('1',1,3);                                 --14.trdn_flag: 拆零标志   字符型(3)    varchar2(3);
        else
          LS_trdn_flag             := substrb('0',1,3);                                 --14.trdn_flag: 拆零标志   字符型(3)    varchar2(3);
        end if;
      else
        if LS_ZSCLBZ in ('1','3') then    --追溯码拆零标志，0不可拆零 1门诊拆零 2住院拆零 3门诊住院都拆零
          LS_trdn_flag             := substrb('1',1,3);                                 --23.trdn_flag: 拆零标志   字符型(3)    varchar2(3);
        else
          LS_trdn_flag             := substrb('0',1,3);                                 --23.trdn_flag: 拆零标志   字符型(3)    varchar2(3);
        end if;
      end if;
      LS_finl_trns_pric          := substrb(null,1,16);                               --15.finl_trns_pric: 最终成交单价   数值型(16,6)    varchar2(20);
      LS_sel_retn_cnt            := substrb(trim(to_char(LD_YPZSL0,'999999990.9999')),1,16);                 --16.sel_retn_cnt: 销售/退货数量   数值型(16,4)    varchar2(20);
      LS_sel_retn_time           := substrb(to_char(to_date(LS_TYRQ00,'YYYYMMDD'),'YYYY-MM-DD')||' '||LS_TYSJ00,1,20);        --17.sel_retn_time: 销售/退货时间   日期时间型()    varchar2(20);
      LS_sel_retn_opter_name     := substrb(LS_TYCZXM,1,50);                          --18.sel_retn_opter_name: 销售/退货经办人姓名   字符型(50)    varchar2(50);
      LS_memo                    := substrb(null,1,500);                              --19.memo: 备注   字符型(500)    varchar2(500);
      LS_medins_prod_sel_no      := substrb(null,1,50);                               --20.medins_prod_sel_no: 商品销售流水号   字符型(50)    varchar2(50);
      LS_mdtrt_sn                := substrb(nvl(LS_YBGHH0,to_char(LD_GHID00)),1,30);  --21.mdtrt_sn: 就医流水号   字符型(30)    varchar2(30);
      LS_drugtracinfo            := substrb(LS_drugtracinfoJson,1,4000);              --22.drugtracinfo: 溯源码节点信息   ()    varchar2(2000);

      --插入表SF_YBJKMX
      insert into SF_YBJKMX(YBJKID,JKMXID,JDBM00,ZJDBM0,ZDBM00,ZDMC00,
                            JKSXBM,JKSXMC,JKFSZ0,JKFHZ0,FSLXBZ,ZDLX00,BZ0000,CJBM00,XSXH00)
                     select PYBJKID,A.ZDNBID,A.JDBM00,null,substrb(nvl(A.JKDZBM,A.JKSXBM),1,50),substrb(nvl(A.JKDZMC,JKSXMC),1,100),
                            A.JKSXBM,A.JKSXMC,null,null,substrb(a.JKFSLX,1,1),substrb(a.ZDTYPE,1,1),a.BZ0000,A.CJBM00,LS_COUNT1
                       from BM_TYCJZD A
                       where A.CJBM00=PJKCJBM
                         and nvl(A.SFYX00,'1')='1' and A.JKFSLX in ('1','2','3');

      --修改:医保发送变量赋值
      for C_YBFS in CUR_UPDATE_YBJKMX loop
        LS_JKFSZ0:=null;--接口发送值
        LS_JKFHZ0:=null;--接口返回值
        LS_VALUE0:=null;--请求报文字符串
        if C_YBFS.JKSXBM = 'med_list_codg' then
          LS_JKFSZ0:=substrb(LS_med_list_codg,1,250);            --1.med_list_codg: 医疗目录编码   字符型(50)    varchar2(50);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='fixmedins_hilist_id' then           --2.fixmedins_hilist_id: 定点医药机构目录编号   字符型(30)    varchar2(30);
          LS_JKFSZ0:=substrb(LS_fixmedins_hilist_id,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='fixmedins_hilist_name' then         --3.fixmedins_hilist_name: 定点医药机构目录名称   字符型(200)    varchar2(200);
          LS_JKFSZ0:=substrb(LS_fixmedins_hilist_name,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='fixmedins_bchno' then               --4.fixmedins_bchno: 定点医药机构批次流水号   字符型(30)    varchar2(30);
          LS_JKFSZ0:=substrb(LS_fixmedins_bchno,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='setl_id' then                        --5.setl_id: 结算ID   字符型(30)    varchar2(30);
          LS_JKFSZ0:=substrb(LS_setl_id,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='psn_no' then                         --6.psn_no: 人员编号   字符型(30)    varchar2(30);
          LS_JKFSZ0:=substrb(LS_psn_no,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='psn_cert_type' then                  --7.psn_cert_type: 人员证件类型   字符型(6)    varchar2(6);
          LS_JKFSZ0:=substrb(LS_psn_cert_type,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='certno' then                         --8.certno: 证件号码   字符型(50)    varchar2(50);
          LS_JKFSZ0:=substrb(LS_certno,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='psn_name' then                       --9.psn_name: 人员姓名   字符型(50)    varchar2(50);
          LS_JKFSZ0:=substrb(LS_psn_name,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='manu_lotnum' then                    --10.manu_lotnum: 生产批号   字符型(30)    varchar2(30);
          LS_JKFSZ0:=substrb(LS_manu_lotnum,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='manu_date' then                      --11.manu_date: 生产日期   日期型()    varchar2(20);
          LS_JKFSZ0:=substrb(LS_manu_date,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='expy_end' then                       --12.expy_end: 有效期止   日期型()    varchar2(20);
          LS_JKFSZ0:=substrb(LS_expy_end,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='rx_flag' then                        --13.rx_flag: 处方药标志   字符型(3)    varchar2(3);
          LS_JKFSZ0:=substrb(LS_rx_flag,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='trdn_flag' then                      --14.trdn_flag: 拆零标志   字符型(3)    varchar2(3);
          LS_JKFSZ0:=substrb(LS_trdn_flag,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='finl_trns_pric' then                 --15.reptFlag: 复用（多次）使用标志   字符型(3)
          LS_JKFSZ0:=substrb(LS_finl_trns_pric,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='sel_retn_cnt' then                   --16.sel_retn_cnt: 销售/退货数量   数值型(16,4)    varchar2(16);
          LS_JKFSZ0:=substrb(LS_sel_retn_cnt,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='sel_retn_time' then                  --17.sel_retn_time: 销售/退货时间   日期时间型()    varchar2(20);
          LS_JKFSZ0:=substrb(LS_sel_retn_time,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='sel_retn_opter_name' then            --18.sel_retn_opter_name: 销售/退货经办人姓名   字符型(50)    varchar2(50);
          LS_JKFSZ0:=substrb(LS_sel_retn_opter_name,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='memo' then                           --19.memo: 备注   字符型(500)    varchar2(500);
          LS_JKFSZ0:=substrb(LS_memo,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='medins_prod_sel_no' then             --20.medins_prod_sel_no: 就医流水号   字符型(30)    varchar2(30);
          LS_JKFSZ0:=substrb(LS_medins_prod_sel_no,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='mdtrt_sn' then                       --21.mdtrt_sn: 就医流水号   字符型(30)    varchar2(30);
          LS_JKFSZ0:=substrb(LS_mdtrt_sn,1,250);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        elsif C_YBFS.JKSXBM='drugtracinfo' then                   --38.mdtrt_setl_type: 就诊结算类型   VARCHAR(6)    varchar2(6);
          LS_VALUE0:=substrb(LS_drugtracinfo,1,4000);
          update SF_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID and XSXH00=LS_COUNT1;
        end if;
      end loop;

      LS_selinfoDetailJson := null;
      --查询明细数据
      for C_detail in CUR_QUERY_detail loop
        if C_detail.JKSXBM = 'drugtracinfo' then
          LS_selinfoDetailJson := SF_XT_GETJSONSTR(LS_selinfoDetailJson,C_detail.JKSXBM,C_detail.VALUE0,'2');
        else
          LS_selinfoDetailJson := SF_XT_GETJSONSTR(LS_selinfoDetailJson,C_detail.JKSXBM,C_detail.JKFSZ0,'0');
        end if;
      end loop;
      LS_detailJson := SF_XT_GETJSONSTR(LS_detailJson,null,LS_selinfoDetailJson,'1');
    --end if;
  end loop;

  if LS_detailJson is null then
    PZXZT00:=0;
    PERRMSG:='当前处方(处方流水号:'||LS_CFLSH0||')没有待上传的追溯码！';
    return;
  end if;

  --明细项加标题
  LS_detailJson := SF_XT_GETJSONSTR(null,'selinfoDetail',LS_detailJson,'2');
  --input输入
  LS_inputJson := SF_XT_GETJSONSTR(LS_inputJson,'input',LS_detailJson,'1');
  --拼接整个报文, http服务拼接arg0, 非http直接拼接
  if LS_TYCJ00.INFNLX='3' then
    LS_QQDATA := SF_XT_GETJSONSTR(LS_QQDATA,'arg0',LS_MainJson||','||LS_inputJson,'1');
  else
    LS_QQDATA := LS_MainJson||','||LS_inputJson;
  end if;

  LS_QQDATA:='{'||LS_QQDATA||'}';
  --修改请求报文
  update SF_YBJKRZ set  QQDATA=LS_QQDATA where YBJKID=PYBJKID;--请求报文

  if PCOMMIT='Y' then
    commit;
  end if;

  PZXZT00:=1;        --输出:执行状态 0:失败 1：成功
  PERRMSG:=null;     --输出:错误信息
exception
  when no_data_found then
    PZXZT00:=0;
    PERRMSG:=substrb('药房医保接口(进销存管理商品销售退货A)交互生成失败！原因:'||sqlerrm,1,200);
    rollback;
  when others   then
    PZXZT00:=0;
    PERRMSG:=substrb('药房医保接口(进销存管理商品销售退货A)交互生成失败！原因:'||sqlerrm,1,200);
    rollback;
end;
