create or replace procedure SP_ZY_GJYBRZ_ZYJSSF(
  PYBJKID   in     varchar2                     ,--[1]输入:医保接口ID
  PJKCJBM   in     varchar2                     ,--[2]输入:接口场景编码
  PBRID00   in     number                       ,--[3]输入:BRID00
  PZYID00   in     number                       ,--[4]输入:ZYID000
  PZYH000   in     varchar2                     ,--[5]输入:ZYH000
  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
-- linzetao    2022.02.24    created   by   【2304】住院结算_日志生成    ZYSF9-20220223-001
-- linzetao    2022.03.11    优化就诊凭证类型=03时医保卡号取值处理   ZYSF9-20220311-001
-- linzetao    2022.03.12    优化acct_used_flag节点取值处理   ZYSF9-20220312-005
-- linzetao    2022.03.23    处理按床日结算在扩展节点(exp_content)上传天数      ZYSF9-20220323-002
-- zhangyc     2022.03.24    insuplc_admdvs,mdtrtarea_admvs取ZY_YBJKRZ表 by ZYSF9-20220326-001
-- linzetao    2022.03.29    调整CDRG结算psn_setlway(个人结算方式)节点发送02  ZYSF9-20220329-001
-- linzetao    2022.04.19    处理新生儿信息     ZYSF9-20220419-001
-- linzetao    2023.01.11    调整psn_setlway(个人结算方式)取值处理   ZYSF9-20230110-004
-- zhangyc     2023.02.25    增加获取dise_codg,dise_name by ZYSF9-20230228-005
-- nichaolin   2023.05.08    修改优先获取住院病人信息表的数据     ZYSF13-20230505-001
-- linzetao    2024.11.01    调整psn_setlway(个人结算方式)节点取值，优先按med_type(医疗类别)节点对应取值    ZYSF9-20241101-002
-- linzetao    2025.04.17    增加节点minpacunt_drug_trac_cnt(药品追溯码数量)和mcs_trac_cnt(耗材追溯码数量)处理     ZYSF9-20250417-001
-- linzetao    2025.04.21    节点minpacunt_drug_trac_cnt(药品追溯码数量)和mcs_trac_cnt(耗材追溯码数量)按整数处理   MZSF9-20250421-002
-- linzetao    2025.04.30    psn_setlway(个人结算方式)节点增加按床位结算(09002)处理         ZYSF9-20250429-002
-- zhangyc     2025.05.23    med_type改为函数SF_ZY_NationYBSXZD统一处理 by  ZYSF9-20250523-001
-- linzetao    2025.05.30    调整CDRG结算expcontent节点取值     ZYSF9-20250530-001
-- ---------   ----------    ------- 
  LS_COUNT0        number(12,2);
  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        ZY_YBJKMX.JKFSZ0%type;--接口发送值
  LS_JKFHZ0        ZY_YBJKMX.JKFHZ0%type;--接口返回值
  LS_VALUE0        ZY_YBJKMX.VALUE0%type;--请求报文字符串
  LS_HISCS0        ZY_YBJKRZ.HISCS0%type;--HIS前台请求参数
  LS_QQDATA        ZY_YBJKRZ.QQDATA%type;--请求报文
  LS_MainJson      ZY_YBJKRZ.QQDATA%type;--主项Json
  LS_detailJson    ZY_YBJKRZ.QQDATA%type;--明细Json
  LS_inputJson     ZY_YBJKRZ.QQDATA%type;--input输入

  LS_YBKH00    BM_BRXXB0.YBKH00%type;    --医保卡号
  LS_YBID00    BM_BRXXB0.YBID00%type;    --医保ID
  LS_BRXM00    BM_BRXXB0.BRXM00%type;    --病人姓名
  LS_XZQH00    BM_BRXXB0.XZQH00%type;    --行政区号
  LS_YBKSBM    BM_BRXXB0.YBKSBM%type;    --医保卡识别码
  LS_AAC999    BM_BRXXB0.AAC999%type;    --个人管理码
  LS_AAE140    BM_BRXXB0.AAE140%type;    --险种类型
  LS_YBZYH0    ZY_BRXXB0.YBZYH0%type;    --医保挂号号
  LS_FFBZBM    ZY_BRXXB0.FFBZBM%type;    --付费病种编码
  LS_CFXMSL    number(12,2);             --明细项目数量
  LS_HJJE00    number(12,2);             --医疗费总金额
  LS_ID0000    varchar2(50);             --中间表ID
  LS_YBCYRQ    varchar2(30);             --医保出院日期
  LS_YBCYSJ    varchar2(30);             --医保出院时间
  LS_JSTYPE    varchar2(10);             --结算类型
  LS_PTBCTS    varchar2(10);             --普通病床天数
  ls_CDRGJK                     XT_XTCS00.VALUE0%type;    --住院收费医保结算是否启用CDRG流程 0:不启用 1:启用，默认0
  LS_CDRGFS                     ZY_BRXXB0.CDRGFS%type;    --CDRG发送标识    
  LS_SFXSE0                     varchar2(10);             --是否新生儿
  LS_YEXM00                     varchar2(50);             --婴儿姓名
  LS_PSN_NO_XSE                 ZY_YBJKRZ.PSN_NO%type;        --人员编号(婴儿)
  LS_INSUTYPE_XSE               ZY_YBJKRZ.INSUTYPE%type;      --险种类型(婴儿)
  LS_INSUPLC_ADMDVS_XSE         ZY_YBJKRZ.INSUPLC_ADMDVS%type;--参保地行政区划
  LS_MDTRT_ID_XSE               ZY_YBJKRZ.MDTRT_ID%type;      --就诊id
  LD_minpacunt_drug_trac_cnt    ZY_YBFSMX.minpacunt_drug_trac_cnt%type;   --minpacunt_drug_trac_cnt: 本次结算应上传最小包装药品追溯码数量
  LD_mcs_trac_cnt               ZY_YBFSMX.mcs_trac_cnt%type;              --mcs_trac_cnt: 本次结算应上传耗材追溯码数量
  RTYCJ                     BM_TYCJBM%rowtype;
  LS_psn_no                     varchar2(30);    --1.psn_no: 人员编号                   字符型(30)
  LS_mdtrt_cert_type_fm_BKE284  varchar2(3);     --2.mdtrt_cert_type: 就诊凭证类型      字符型(3)
  LS_mdtrt_cert_no              varchar2(50);    --3.mdtrt_cert_no: 就诊凭证编号        字符型(50)
  LS_medfee_sumamt              varchar2(20);    --4.medfee_sumamt: 医疗费总额          数值型(16,2)
  LS_psn_setlway                varchar2(6);     --5.psn_setlway: 个人结算方式          字符型(6)
  LS_mdtrt_id_fm_YBZYH0         varchar2(30);    --6.mdtrt_id: 就诊ID                   字符型(30)
  LS_insutype_fm_AAE140         varchar2(6);     --7.insutype: 险种类型                 字符型(6)
  LS_acct_used_flag             varchar2(1);     --8.acct_used_flag: 个人账户使用标志   字符型(1)
  --LS_insuplc_admdvs_fm_XZQH00   varchar2(6);     --9.insuplc_admdvs: 参保地医保区划     字符型(6)
  --LS_mdtrtarea_admvs            varchar2(6);     --10.mdtrtarea_admvs: 就医地医保区划   字符型(6)
  LS_invono                     varchar2(20);    --11.invono: 发票号                    字符型(20)
  LS_mid_setl_flag              varchar2(3);     --12.mid_setl_flag: 中途结算标志       字符型(3)
  LS_fulamt_ownpay_amt          varchar2(20);    --13.fulamt_ownpay_amt: 全自费金额     数值型(16,2)
  LS_overlmt_selfpay            varchar2(20);    --14.overlmt_selfpay: 超限价金额       数值型(16,2)
  LS_preselfpay_amt             varchar2(20);    --15.preselfpay_amt: 先行自付金额      数值型(16,2)
  LS_inscp_scp_amt              varchar2(20);    --16.inscp_scp_amt: 符合政策范围金额   数值型(16,2)
  LS_exp_content                varchar2(4000);  --17.exp_content: 字段扩展              字符型(4000)
  LS_med_type_fm_AKA130         varchar2(10);     --8.med_type: 医疗类别   字符型(6)
  LS_insuplc_admdvs             ZY_YBJKRZ.insuplc_admdvs%type;        --35.insuplc_admdvs: 参保地医保区划   字符型(6)
  LS_mdtrtarea_admvs            ZY_YBJKRZ.mdtrtarea_admvs%type;       --36.mdtrtarea_admvs: 就医地医保区划   字符型(6)
  LS_dise_codg                  ZY_YBJKRZ.dise_codg%type;--dise_codg: 病种编码
  LS_dise_name                  ZY_YBJKRZ.dise_name%type;--dise_name: 病种名称
  LS_med_type_fm_YBJKRZ         ZY_YBJKRZ.med_type%type;     --[国家医保]医疗类别
  LS_minpacunt_drug_trac_cnt    varchar2(12);   --minpacunt_drug_trac_cnt: 本次结算应上传最小包装药品追溯码数量    数值型   8
  LS_mcs_trac_cnt               varchar2(12);   --mcs_trac_cnt: 本次结算应上传耗材追溯码数量   数值型   8
  
  LS_YBSDLB                     IC_YBBRLB.YBSDLB%type;  
 
  --修改医保发送明细表
  cursor CUR_UPDATE_YBJKMX is
    select YBJKID,JKMXID,JDBM00,ZJDBM0,JKSXBM,JKSXMC,JKFSZ0,JKFHZ0,FSLXBZ
      from ZY_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
      from ZY_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
      from ZY_YBJKMX where YBJKID=PYBJKID and FSLXBZ in ('1','3') and CJBM00=PJKCJBM
        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_HISCS0:=substrb(PINXML0,1,4000);
  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 
  if nvl(PZYID00,0) <= 0 then
    PERRMSG := '住院id为空';
    PZXZT00:=0; --执行状态 0:失败 1：成功
    return;
  end if;
  begin
    select * into RTYCJ from BM_TYCJBM where CJBM00=PJKCJBM;
  exception
    when others  then
      PZXZT00 := 0;
      PERRMSG := '未找到有效的场景字典,请联系管理员!';
      return;   
  end;
  --住院收费医保结算是否启用CDRG流程 0:不启用 1:启用，默认0
  select nvl(max(value0),'0')  into ls_CDRGJK    from  XT_XTCS00  where  NAME00 = 'ZY_CDRGJKQY';  
  begin
    select decode(nvl(b.YBKH00,'0'),'0',A.YBKH00,b.YBKH00) YBKH00,
           decode(nvl(b.SHBZH0,'0'),'0',A.YBID00,b.SHBZH0) YBID00,
           b.XM0000,A.XZQH00,A.YBKSBM,A.AAC999,
           decode(nvl(b.AAE140,'0'),'0',A.AAE140,b.AAE140) AAE140,
           B.YBZYH0,B.FFBZBM,
           nvl(b.PSN_NO,A.PSN_NO) PSN_NO,
           B.CDRGFS
      into LS_YBKH00,LS_YBID00,LS_BRXM00,LS_XZQH00,LS_YBKSBM,
           LS_AAC999,LS_AAE140,LS_YBZYH0,LS_FFBZBM,LS_PSN_NO,
           LS_CDRGFS
      from BM_BRXXB0 A,ZY_BRXXB0 B
        where A.BRID00=B.BRID00
          and B.ZYID00=PZYID00;
  exception
    when others  then
      PERRMSG := '未找到病人住院信息(ZYID00='||PZYID00||')';
      PZXZT00:=0; --执行状态 0:失败 1：成功
      return;
  end;
 --查找日志表
  begin
    select a.mdtrt_cert_type,a.mdtrt_cert_no,A.insuplc_admdvs,A.mdtrtarea_admvs,
           nvl(a.psn_setlway,SF_XT_GETYBSXZD('Med_type_To_Psn_setlway',a.med_type,'3','3',-1)),A.dise_codg,A.dise_name,a.med_type 
      into LS_mdtrt_cert_type_fm_bke284,LS_mdtrt_cert_no,LS_insuplc_admdvs,LS_mdtrtarea_admvs,
           LS_psn_setlway,LS_dise_codg,LS_dise_name,LS_med_type_fm_YBJKRZ
      from ZY_YBJKRZ a where YBJKID=PYBJKID;
  exception
    when others then
      LS_mdtrt_cert_type_fm_bke284 := '';
      LS_mdtrt_cert_no := '';
      LS_insuplc_admdvs := '';
      LS_mdtrtarea_admvs := '';
      LS_psn_setlway := '';
      LS_med_type_fm_YBJKRZ := null;
  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;

  --解析报文中 中间表ID
  LS_ID0000 := substrb(SF_XT_GETXMLSTR('id0000',PINXML0),1,50);
  --解析前端传入的医保出院日期和时间
  --LS_YBCYRQ := substrb(SF_XT_GETXMLSTR('ybcyrq',PINXML0),1,30); --医保出院日期
  --LS_YBCYSJ := substrb(SF_XT_GETXMLSTR('ybcysj',PINXML0),1,30); --医保出院时间
  --解析前端传入的结算类型, '0'  中途结算  '1'  出院结算 '2' 挂账结算 '3' 年度结算 '4'; 挂账留院结算
  LS_JSTYPE := substrb(SF_XT_GETXMLSTR('jstype',PINXML0),1,10); --结算类型
  --解析前端传入的普通病床天数
  LS_PTBCTS := substrb(SF_XT_GETXMLSTR('ptbcts',PINXML0),1,10); --普通病床天数

  --处方项目数量, 项目金额
  select count(1),sum(XMJE00)--,sum(trunc(minpacunt_drug_trac_cnt)),sum(trunc(mcs_trac_cnt))
    into LS_CFXMSL,LS_HJJE00--,LD_minpacunt_drug_trac_cnt,LD_mcs_trac_cnt
    from ZY_YBFSMX where ID0000=LS_ID0000;
    
  select sum(trunc(minpacunt_drug_trac_cnt)),sum(trunc(mcs_trac_cnt))
    into LD_minpacunt_drug_trac_cnt,LD_mcs_trac_cnt
    from(select XMBH00,sum(minpacunt_drug_trac_cnt) as minpacunt_drug_trac_cnt,sum(mcs_trac_cnt) as mcs_trac_cnt
           from ZY_YBFSMX where ID0000=LS_ID0000 group by XMBH00);
           
           
  LS_med_type_fm_AKA130 :=substrb(nvl(LS_med_type_fm_YBJKRZ,LS_med_type_fm_AKA130),1,10);--8.med_type: 医疗类别            
  LS_psn_no := substrb(LS_PSN_NO,1,30);                                    --1.psn_no: 人员编号
  if LS_mdtrt_cert_type_fm_bke284 is null then
    LS_mdtrt_cert_type_fm_bke284 := substrb(SF_XT_GETXMLSTR('mdtrt_cert_type',PINXML0),1,3);   --2.mdtrt_cert_type: 就诊凭证类型
    if LS_mdtrt_cert_type_fm_bke284 is null then
      LS_mdtrt_cert_type_fm_bke284 := substrb(SF_XT_GETXMLSTR('bke284',PINXML0),1,3);   --2.mdtrt_cert_type: 就诊凭证类型
    end if;
  end if;
  if LS_mdtrt_cert_no is null then
    LS_mdtrt_cert_no := substrb(SF_XT_GETXMLSTR('mdtrt_cert_no',PINXML0),1,50);       --3.mdtrt_cert_no: 就诊凭证编号
  end if;
  if LS_mdtrt_cert_no is null and LS_mdtrt_cert_type_fm_bke284 in ('03') then
    LS_mdtrt_cert_no := substrb(LS_YBKH00,1,50);
  end if;
  LS_medfee_sumamt := substrb(trim(to_char(LS_HJJE00,'fm9999999999999990.09')),1,16);--4.medfee_sumamt: 医疗费总额
  
  --增加病人ic_ybbrlb.ybsdlb判断，异地医保按项目结算，本地医保上传CDRG按定额结算
   begin
   	 select nvl(c.ybsdlb, 0)
      into LS_YBSDLB
      from bm_brxxb0 a, zy_brxxb0 b, ic_ybbrlb c
     where a.brid00 = b.brid00
       and a.yblb00 = c.yblb00
       and a.fbbh00=c.fbbh00
       and b.zyid00 = PZYID00;
   exception
     when others then
       LS_YBSDLB := '0';
   end;
  if LS_psn_setlway is null then
    if trim(LS_PTBCTS) is not null and trim(LS_PTBCTS) not in ('0') then
      LS_psn_setlway := '09002';
    elsif LS_FFBZBM is not null then                                                      --6.psn_setlway: 个人结算方式
      LS_psn_setlway := '0201';     --按单病种结算
    elsif LS_CDRGJK='1' and LS_CDRGFS = '1' and LS_YBSDLB ='0' then
      LS_psn_setlway := '02';
    else
      LS_psn_setlway := '01';       --按项目结算
    end if;
  end if;
  
  LS_mdtrt_id_fm_YBZYH0 := substrb(LS_YBZYH0,1,30);                                  --7.mdtrt_id: 就诊ID
  LS_insutype_fm_AAE140 := substrb(LS_AAE140,1,6);                                   --10.insutype: 险种类型
  LS_acct_used_flag := substrb(SF_XT_GETXMLSTR('acct_used_flag',PINXML0),1,6);       --9.acct_used_flag: 个人账户使用标志
  if LS_acct_used_flag is null then
    LS_acct_used_flag := nvl(substrb(SF_XT_GETXMLSTR('bkeg23',PINXML0),1,6),'1');    --9.acct_used_flag: 个人账户使用标志
  end if;
  if LS_acct_used_flag not in ('0','1') then
    if LS_acct_used_flag in ('N') then
      LS_acct_used_flag := '0';
    else
      LS_acct_used_flag := '1';
    end if;
  end if;
  if LS_insuplc_admdvs is null then 
    LS_insuplc_admdvs := substrb(LS_XZQH00,1,6);                                 --9.insuplc_admdvs: 参保地医保区划
  end if;
  if LS_mdtrtarea_admvs is null then 
   LS_mdtrtarea_admvs := SF_XT_TYZD00('医保新接口参数设置','mdtrtarea_admvs');            --10.mdtrtarea_admvs: 就医地医保区划
  end if;
  LS_invono := null;
  if LS_JSTYPE in ('0','3','4') then    --11.mid_setl_flag: 中途结算标志
    LS_mid_setl_flag := '1';
  else
    LS_mid_setl_flag := '0';
  end if;
  LS_fulamt_ownpay_amt := 0;                                                         --12.fulamt_ownpay_amt: 全自费金额
  LS_overlmt_selfpay := 0;                                                           --13.overlmt_selfpay: 超限价金额
  LS_preselfpay_amt := 0;                                                            --14.preselfpay_amt: 先行自付金额
  LS_inscp_scp_amt := 0;                                                             --15.inscp_scp_amt: 符合政策范围金额
  if trim(LS_PTBCTS) is not null and trim(LS_PTBCTS) not in ('0') then
    LS_exp_content := '{\"iptDaysInput\":\"'||LS_PTBCTS||'\"}';
  elsif LS_CDRGJK='1' and LS_CDRGFS = '1' then
    LS_exp_content := '{\"psn_setlway_desc\":\"C-DRG 收付费\"}';
  else
    LS_exp_content := null;                                                             --16.exp_content: 字段扩展
  end if;
  LS_minpacunt_drug_trac_cnt := substrb(trim(to_char(LD_minpacunt_drug_trac_cnt,'fm999999990')),1,16);--19.minpacunt_drug_trac_cnt: 本次结算应上传最小包装药品追溯码数量
  LS_mcs_trac_cnt := substrb(trim(to_char(LD_mcs_trac_cnt,'fm999999990')),1,16);   --20.mcs_trac_cnt: 本次结算应上传耗材追溯码数量

  LS_SFXSE0 := trim(substrb(SF_XT_GETXMLSTR('SFXSE0',PINXML0),1,10));--是否新生儿
  LS_YEXM00 := trim(substrb(SF_XT_GETXMLSTR('YEXM00',PINXML0),1,50));--婴儿姓名
  if nvl(LS_SFXSE0,'0') = '1' and nvl(LS_YEXM00,'全部') not in ('全部','母亲') then
    select max(psn_no),max(insutype),max(insuplc_admdvs),max(YBZYH0)
      into LS_PSN_NO_XSE,LS_INSUTYPE_XSE,LS_INSUPLC_ADMDVS_XSE,LS_MDTRT_ID_XSE
      from BQ_JSXX00 where ZYID00=PZYID00 and YEXM00=LS_YEXM00;
    LS_psn_no := nvl(LS_PSN_NO_XSE,LS_psn_no);   --人员编号
    LS_insutype_fm_AAE140 := nvl(LS_INSUTYPE_XSE,LS_insutype_fm_AAE140); --险种类型
    LS_mdtrt_id_fm_YBZYH0 := nvl(LS_MDTRT_ID_XSE,LS_mdtrt_id_fm_YBZYH0); --就诊id
    LS_insuplc_admdvs := nvl(LS_INSUPLC_ADMDVS_XSE,LS_insuplc_admdvs);  --参保地行政区划
    LS_psn_setlway := '01';   --按项目结算
  end if;
  
  --插入表ZY_YBJKMX  
  insert into ZY_YBJKMX(YBJKID,JKMXID,JDBM00,ZJDBM0,ZDBM00,ZDMC00,
                        JKSXBM,JKSXMC,JKFSZ0,JKFHZ0,FSLXBZ,ZDLX00,BZ0000,CJBM00)
                 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
                   from BM_TYCJZD A 
                   where A.CJBM00=PJKCJBM and A.JDBM00 not in ('setldetail')
                     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 = 'psn_no' then
       LS_JKFSZ0:=substrb(LS_psn_no,1,250);                --1.psn_no: 人员编号
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='mdtrt_cert_type' then            --2.mdtrt_cert_type: 就诊凭证类型
       LS_JKFSZ0:=substrb(LS_mdtrt_cert_type_fm_bke284,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='mdtrt_cert_no' then              --3.mdtrt_cert_no: 就诊凭证编号
       LS_JKFSZ0:=substrb(LS_mdtrt_cert_no,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='medfee_sumamt' then              --4.medfee_sumamt: 医疗费总额
       LS_JKFSZ0:=substrb(LS_medfee_sumamt,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='psn_setlway' then                --5.psn_setlway: 个人结算方式          字符型(6)
       LS_JKFSZ0:=substrb(LS_psn_setlway,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='mdtrt_id' then                   --6.mdtrt_id: 就诊ID
       LS_JKFSZ0:=substrb(LS_mdtrt_id_fm_YBZYH0,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='insutype' then                   --7.insutype: 险种类型
       LS_JKFSZ0:=substrb(LS_insutype_fm_aae140,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='acct_used_flag' then             --8.acct_used_flag: 个人账户使用标志
       LS_JKFSZ0:=substrb(LS_acct_used_flag,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='insuplc_admdvs' then             --9.insuplc_admdvs: 参保地医保区划
       LS_JKFSZ0:=substrb(LS_insuplc_admdvs,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='mdtrtarea_admvs' then            --10.mdtrtarea_admvs: 就医地医保区划
       LS_JKFSZ0:=substrb(LS_mdtrtarea_admvs,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='invono' then                     --11.invono: 发票号
       LS_JKFSZ0:=substrb(LS_invono,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='mid_setl_flag' then              --12.mid_setl_flag: 中途结算标志
       LS_JKFSZ0:=substrb(LS_mid_setl_flag,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='fulamt_ownpay_amt' then          --13.fulamt_ownpay_amt: 全自费金额
       LS_JKFSZ0:=substrb(LS_fulamt_ownpay_amt,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='overlmt_selfpay' then            --14.overlmt_selfpay: 超限价金额
       LS_JKFSZ0:=substrb(LS_overlmt_selfpay,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='preselfpay_amt' then             --15.preselfpay_amt: 先行自付金额
       LS_JKFSZ0:=substrb(LS_preselfpay_amt,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='inscp_scp_amt' then              --16.inscp_scp_amt: 符合政策范围金额
       LS_JKFSZ0:=substrb(LS_inscp_scp_amt,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='exp_content' then                 --17.exp_content: 字段扩展
       LS_JKFSZ0:=substrb(LS_exp_content,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='dise_codg' then               --dise_codg: 病种编码
       LS_JKFSZ0:=substrb(LS_dise_codg,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID; 
     elsif C_YBFS.JKSXBM='dise_name' then               --dise_name: 病种名称
       LS_JKFSZ0:=substrb(LS_dise_name,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='med_type' then              --8.med_type: 医疗类别
       LS_JKFSZ0:=substrb(LS_med_type_fm_AKA130,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;               
     elsif C_YBFS.JKSXBM='minpacunt_drug_trac_cnt' then             --19.minpacunt_drug_trac_cnt: 本次结算应上传最小包装药品追溯码数量
       LS_JKFSZ0:=substrb(LS_minpacunt_drug_trac_cnt,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     elsif C_YBFS.JKSXBM='mcs_trac_cnt' then             --20.mcs_trac_cnt: 本次结算应上传耗材追溯码数量
       LS_JKFSZ0:=substrb(LS_mcs_trac_cnt,1,250);
       update ZY_YBJKMX set JKFSZ0=LS_JKFSZ0,JKFHZ0=LS_JKFHZ0,VALUE0=LS_VALUE0 where YBJKID=PYBJKID and JKSXBM=C_YBFS.JKSXBM and JKMXID=C_YBFS.JKMXID;
     end if;
   end loop;
   
   --查询明细数据
   for C_detail in CUR_QUERY_detail loop
     LS_detailJson := SF_XT_GETJSONSTR(LS_detailJson,C_detail.JKSXBM,C_detail.JKFSZ0,'0');
   end loop;

   if LS_detailJson is null then 
     PZXZT00:=0;
     PERRMSG:='明细报文不能为空!';
     return;
   end if;

   --明细项加标题
   LS_detailJson := SF_XT_GETJSONSTR(null,'data',LS_detailJson,'1');
   --input输入
   LS_inputJson := SF_XT_GETJSONSTR(LS_inputJson,'input',LS_detailJson,'1');
   --拼接整个报文, http服务拼接arg0, 非http直接拼接
   if RTYCJ.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 ZY_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('住院医保接口交互生成失败!原因:'||sqlerrm,1,200);
    rollback;
  when others   then
    PZXZT00:=0;
    PERRMSG:=substrb('住院医保接口交互生成失败!原因:'||sqlerrm,1,200);
    rollback;   
end;
