create or replace function SF_SF_PAY_DDLSH0
(
 PDDLSH0 varchar2           ,--订单流水号
 PMZZYBZ varchar2           ,--0:门诊 1:住院 2：门诊自助机  3:外联平台 4:医保接口 5:床头卡 88:通用接口 90:集成平台 98   其它
 PJFBZ00 varchar2 default '' --缴费备注:0:交,1:退
)
return varchar2 is
-- MODIFICATION HISTORY
-- Person      Date        Comments
--zhangyc     2017.07.31  created    by   MZSF-20170724-004
--zhangyc     2018.08.01  增加类型3,4   by MZSF-20180801-003
--zhangyc     2019.05.23  增加类型5(床头卡) by ZYSF-20190523-002
--zhangyc     2024.09.14  订单号规则改为（充值xxCZxxx 退款:xxTKxxx）  by MZSF-20240927-003
--zhangyc     2024.11.14  SQ_BM_ZFRZ00_DDLSH0.nextval改为3位 by MZSF-20241108-002 
  LS_DDLSH0  varchar2(50); --订单流水号((门诊MZ 住院ZY 自助机：ZZ)+yyyyMMddHHmmss+xxx)
  LS_DDQZMC  varchar2(10); --订单前缀名称
  LS_JFQZMC  varchar2(10); --缴费前缀名称
  LS_DDTIME  varchar2(20); --订单时间:格式YYYYMMDDHH24MISS
  LS_DDXLH0  varchar2(10); --订单序号  
  LS_CZRQ00  char(8);
  LS_CZSJ00  char(8);
begin
 LS_CZRQ00:=to_char(sysdate,'YYYYMMDD');
 LS_CZSJ00:=to_char(sysdate,'HH24:MI:SS');
 LS_DDTIME:=to_char(sysdate,'YYYYMMDDHH24MISS'); --订单时间:格式YYYYMMDDHH24MISS
 if PDDLSH0 is not null then 
  LS_DDLSH0:=PDDLSH0;
 else
  select lpad(SQ_BM_ZFRZ00_DDLSH0.nextval,3,'0') into LS_DDXLH0 from dual;
  if PMZZYBZ in ('0') then 
    LS_DDQZMC:='MZ';
  elsif PMZZYBZ in ('1') then
    LS_DDQZMC:='ZY';
  elsif PMZZYBZ in ('2') then
    LS_DDQZMC:='ZZ';
  elsif PMZZYBZ in ('3') then
    LS_DDQZMC:='WL';
  elsif PMZZYBZ in ('4') then
    LS_DDQZMC:='YB';
  elsif PMZZYBZ in ('5') then
    LS_DDQZMC:='CTK';
  elsif PMZZYBZ in ('88') then 
    LS_DDQZMC:='TYJK'; 
  elsif PMZZYBZ in ('90') then 
    LS_DDQZMC:='JCPT'; 
  elsif PMZZYBZ in ('98') then 
    LS_DDQZMC:='QT'; 
  elsif PMZZYBZ in ('99') then 
    LS_DDQZMC:='XSJK';                 
  else  
    LS_DDQZMC:='PAY';
  end if;
  if PJFBZ00='0' then  --'0'交预交金,'1'退预交金
    LS_JFQZMC:='CZ';
  elsif PJFBZ00='1' then
    LS_JFQZMC:='TK';
  else
    LS_JFQZMC:='';
   end if;   
   LS_DDLSH0:=substrb(LS_DDQZMC||LS_JFQZMC||LS_DDTIME||LS_DDXLH0,1,32);  
 end if;
--如果订单流水号为空重新生成  
 if LS_DDLSH0 is null then
  LS_DDQZMC  :=null; --订单前缀名称
  LS_JFQZMC  :=null; --缴费前缀名称
  LS_DDTIME  :=to_char(sysdate,'YYYYMMDDHH24MISS'); --订单时间:格式YYYYMMDDHH24MISS
  select lpad(SQ_BM_ZFRZ00_DDLSH0.nextval,3,'0') into LS_DDXLH0 from dual;--订单序号  
  LS_DDLSH0:=substrb(LS_DDQZMC||LS_JFQZMC||LS_DDTIME||LS_DDXLH0,1,32);
 end if;     
 return trim(LS_DDLSH0);
end;
