oralce 存储过程自备样例

clear buffer
set define off
set sqlblanklines on
spool m_200_sproc.log
--connect
--ISMP V4 Order通用鉴权批价(电信版本) 实现功能:订购鉴权批价
--订购入库、退订入库、产品免费期提醒鉴权、暂停/激活产品订购关系鉴权
--暂停/激活产品订购关系更新

--订购鉴权批价
prompt zxdbm_200.slp_200_telicomsubreq
create or replace procedure zxdbm_200.slp_200_telicomsubreq
(

i_oa                    in          varchar2,            --发起方/赠送方用户号码
i_ua                    in          varchar2,            --使用方/受赠方用户号码
i_oatype                in          number,              --发起方地址类型
i_datype                in          number,              --使用方地址类型
i_servicetype           in          number,              --业务能力类型
i_serviceflowtype       in          number,              --业务流程类型
i_srvprodid             in          varchar2,            --业务/产品代码
i_actiontype            in          number,              --动作类型
i_cmdchannel            in          number,              --动作来源
i_countrycode           in          varchar2,            --国家码
i_spaccesscode          in          varchar2,            --antuprice消息中的SP接入码
i_isthird               in          number,              --0:普通订购 1:赠送
i_silent                in          number,              --沉默去激活开关
i_needauthuser          in          number,              --是否需要鉴权用户 0-不需要 1-需要
i_direct                in          number,              --订购方向 0:正向订购 1:SP反向定购
i_supportproductid      in          number,              --同步SP、SP下发使用service还是productid 0-serviceid 1-productid
i_prepaidtype           in          number,              --预付费接口类型 1-OCS 2-非OCS 3-根据号段选择是OCS或者非OCS
i_monthconsumedlimit    in          number,              --系统级月消费限制金额
i_isnondreplay          in          number,              --是否需要二次确认,1-不需要,0-需要
i_checkpseudocode       in          number,              --系统开关:是否支持伪码   0-不支持   1-支持
i_provinceorder         in          number,              --系统开关:是否支持跨省赠送   0-不支持   1-支持
i_rentday               in          number,              --账期截止日期
i_mutidbmode            in          number,              --是否多DB环境 0:否 1:是
i_isfromnmsc            in          number,              --是否nmsc过来的消息 0:否 1:是
i_spordernoticeuser     in          number,              --sp反向订购是否与用户交互(通知用户,二次确认) 0:否 1:是
i_freemodule            in          number,              --免费期计费方式:   0:正常方式  1:优惠方式
i_eventid               in          varchar2,            --事件ID
i_channelid             in          varchar2,            --渠道商ID
--added by youqunxia 20130805  Order3.02.01
i_webgwcnfmswitch       in          number,              --用来控制来自WEBGW的请求,是否进行二次确认,0-不需要 1-需要
i_startcmdchannel       in          number,              --二次确认时的最初来源动作来源
i_isexperience          in          number,              --订购类型:0:正常订购 1:免费体验 2:积分兑换 3:体验转正式 默认为0
i_fullmonthmode         in          number,              --退订使用足月模式 0-可用到订购日 1-可用到订购日前一天
i_effectiveswitch       in          number,              --VSOP发起的订购退订立即生效开关是否打开,0-关闭,1-打开

o_retcode               out         number,              --鉴权返回码 0代表成功,否则代表错误码
o_debugpos              out         number,              --出口顺序标识
o_debuginfo             out         varchar2,            --调试信息
o_srcmsisdn             out         varchar2,            --去掉前缀的发起方/赠送方用户号码
o_usemsisdn             out         varchar2,            --去掉前缀的使用方/受赠方用户号码
o_paymsisdn             out         varchar2,            --去掉前缀的计费方号码
o_srcmsisdntype         out         number,              --发起方号码类型,1:msisdn,2:phs,3:pstn
o_usemsisdntype         out         number,              --使用方号码类型,1:msisdn,2:phs,3:pstn
o_userindex             out         number,              --使用方号码序号
o_payuserindex          out         number,              --付费方号码序号
o_needreplay            out         number,              --产品订购是否需要用户二次确认,0-不需要,1-需要
o_paytype               out         number,              --计费号码的付费类型 0:后付费 1:预付费 2:准预付费
o_oppaytype             out         number,              --话单专用付费类型
o_scptype               out         number,              --对预付费扣费的设备类型 1:OCS;2:非OCS
o_subfee                out         number,              --本次订购需要扣取的费用
o_colortype             out         number,              --使用方号码名单类型   0-普通用户,1-黑名单,2-白名单,   3-灰名单,4-红名单
o_usertype              out         number,              --使用方类型  1-普通用户  2-运营商测试用户  3-SP测试用户
o_producttype           out         number,              --产品类型   1-正常产品 2-测试产品
o_cpid                  out         varchar2,            --CP/SP代码
o_cpindex               out         number,              --CP/SP序号
o_cpcnshortname         out         varchar2,            --CP/SP名称
o_cpsubscribeurl        out         varchar2,            --CP/SP订购URL
o_cptype                out         number,              --CP/SP类型 0:本省 1:是全国业务
o_protocoltype          out         number,              --定购同步协议类型0-ismp(wbs方式),1-smgp2.0(cngp1.0),2-smgp3.0
o_msgtosp               out         varchar2,            --SMGP/CNGP同步sp的消息内容
o_cnfmmsgtosp           out         varchar2,            --SMGP2.0/CNGP1.0向sp发送同步确认消息内容
o_customersrvtel        out         varchar2,            --CP/SP客服电话
o_serviceid             out         varchar2,            --业务代码
o_serviceindex          out         number,              --业务序号
o_servicename           out         varchar2,            --业务名称
o_productid             out         varchar2,            --产品代码
o_productindex          out         number,              --产品序号
o_productname           out         varchar2,            --产品名称
o_smschargingcn         out         varchar2,            --产品资费短信描述
o_autocontinue          out         number,              --周期性订购是否自动续订
o_servicetype           out         number,              --产品业务能力类型
o_subcapability         out         number,              --产品业务能力子类型
o_srvtypeid             out         varchar2,            --业务能力编码,对应ssys_service_type表中的srvtypeid
o_corpid                out         varchar2,            --集团代码
o_corpindex             out         number,              --集团序号
o_corpcnshortname       out         varchar2,            --集团名称
o_paymode               out         varchar2,            --集团付费模式 集团付费/个人付费
o_freetime              out         varchar2,            --产品订购关系免费期
o_nextrentdate          out         varchar2,            --产品订购关系下次扣租日期
o_substarttime          out         varchar2,            --产品订购关系生效时间
o_subendtime            out         varchar2,            --产品订购关系失效时间
o_substatus             out         number,              --产品订购关系状态
o_lastrenttime          out         varchar2,            --产品订购关系上次扣租时间
o_payfordate            out         varchar2,            --月租已付日期
o_fixusetimes           out         number,              --产品包量值
o_chargetype            out         number,              --产品计费类型
o_cyctype               out         number,              --产品周期类型
o_linknum               out         varchar2,            --业务话单中同一个流程中二次确认前后话单的关联
o_maxlimitfee           out         varchar2,            --用户月消费限额
o_pseudocode            out         varchar2,            --伪码
o_pcodemsisdnswitch     out         number,              --伪码和真实号码之间是否有转换:  0否   1真实号码转化伪码 2伪码转化为真实号码
o_chanelplayerstate     out         number,              --cp/sp状态,0-申请,1-正常,2-暂停(暂停代收费),3-预注销,4-注销,5-冻结,21-暂停端口
o_eventstate            out         number,              --0,正常 1,注销
o_ordertype             out         number,              --定购类型,1-定购,2-点播,3-不需要定购点播
o_spaccesscode          out         varchar2,            --SP接入码
o_cancelaccesscode      out         varchar2,            --产品退订接入码
o_maincapability        out         number,              --组合业务主业务能力类型
o_submaincapability     out         number,              --组合业务主业务能力子类型
o_cpsrvtype             out         number,              --CP服务类型:0:CP+SP,1:CP,2:SP ;3:自营业务SP
o_cancelproductid       out         varchar2,            --被189替换的产品代码
o_issecondproduct       out         number,              --订购流程子类型 0:普通订购 1:业务订购 2:待失效订购关系订购 3:纯包量重复订购 6:可变价的重复订购 7:体验转正式的重复订购
o_cancelfeaturecode     out         varchar2,            --短信,彩信退订指令
o_provincesend          out         number,              --是否跨省赠送  0.否 1.跨省赠送  2.跨省受增
o_cdrbegintime          out         varchar2,            --话单计费开始时间
o_cdrendtime            out         varchar2,            --话单计费结束时间
o_cnfmtype              out         number,               --产品二次确认类型 0:普通  1:0资费  2:免费期
o_validmode             out         number,               --产品属性:订购生效方式:0-立即生效,1-下一帐期生效,2-N周期后生效,3-指定日期生效
o_mutiluserexit         out         number,               --用户存在情况: 0 多DB分库用户不存在  1 多DB分库用户存在或者单DB用户存在
o_popustop              out         varchar2,             --推广期结束时间
o_popuexist             out         number,               --临时存放订购时是否存在推广期:  0目前存在免费推广期  1.目前存在计费推广期  2.目前不在推广期或无推广期 3. 订购时间在推广期,生效时间在推广期之外;
o_freenoticenow         out         number,               --临时存放免费期提醒短信立刻下发(首次订购免费期为1天的产品):  0否  1是
o_freedays              out         number,               --临时存放免费期提醒短信立刻下发(首次订购免费期为1天的产品):  0否  1是
o_productpkgname        out         varchar2,             --套餐名称
o_freenotitype          out         number,               --免费期提醒方式 dwFreeNotiType 0:调用免费n天的短息模板,1:免费到月底 2:免费n小时 3:试用到足月  默认为0
o_needcharge            out         number,               --是否需要扣费 0否  1是 (只用于赠送情况下的替换)
o_cityid                out         varchar2,             --用户表中的cityid

--added by youqunxia 2013.02.18
o_freenotify            out         number,                --免费试用期到处理方式,0-到期自动收费,1-到期需用户确认订购,2-到期自动取消,3-到期用户确认退订
--added by youqunxia 20130818  Order3.02.01
o_hassubscription       out         number,                --可变价订购时,是否有订购关系,0-否,1-是
o_rentfee               out         number,                --周期性费用金额,0-无此类费用
o_haswebgw              out         number,                --是否是签约WEBGW业务能力的SP,0-否,1-是
o_lastvarifee           out         number,                --WEBGW可变价时,当存在订购关系时,保存上次订购关系表中的可变价格
o_issynctooidd          out         number,                --是否同步订购关系给OIDD,0-否,1-是
o_isexperience          out         varchar2,              --订购类型:0:正常订购 1:免费体验 2:积分兑换 3:体验转正式 默认为0
o_chargetime            out         varchar2               --首次扣费时间

)
as

--新增下面几个关于时间的参数,尽量减少sysdate的调用,所有用到sysdate的地方,以to_date(v_sysdate14,'yyyymmddhh24miss')进行代替
v_sysdate14             char(14)    := to_char(sysdate ,'yyyymmddhh24miss');
v_sysdate8              char(8)     := substr(v_sysdate14,1,8);
v_sysmonth6             char(6)     := substr(v_sysdate14,1,6);
v_curmonth              number(2)   := to_number(substr(v_sysdate14,5,2));        --临时存放当前月份
v_curstrmonth           char(2)     := lpad(to_char(v_curmonth),2,'0');           --临时存放当前月份,字符型
v_curmonthendtime       char(14)    := to_char(last_day(to_date(v_sysdate14,'yyyymmddhh24miss')),'yyyymmdd')||'235959';
v_freenoticetime        char(14)    := '00000000000000';
v_unsubenddate          char(8)     := substr(v_sysdate14,1,8);

v_maincapability        number(10)                                          := 0;                                       --临时存放组合业务主业务能力类型
v_submaincapability     number(10)                                          := 0;                                       --临时存放组合业务主业务能力子类型
v_countrycode           varchar2(5)                                         := i_countrycode;                           --临时存放国家码
v_tempitem              number(10)                                          := 0;                                       --临时存放0或者推广期的计费子序号
v_devexist              number(3)                                           := 1;                                       --临时存放是否存在对应设备
v_isservicesub          number(3)                                           := 0;                                       --临时存放是否业务订购(189邮箱),是否根据serviceid查找定购关系,0:否,1:是
v_hasrecord             number(3)                                           := 0;                                       --临时存放产品订购关系是否存在
v_leftdays              number(3)                                           := 0;                                       --临时存放当月所剩天数
v_monthdays             number(3)                                           := 30;                                      --当月总天数
v_subfee                number(10)                                          := 0;                                       --临时存放本次订购需要扣除的费用
v_sql                   varchar2(3072)                                      := ' ';                                     --临时存放动态sql
v_usedamounts           number(10)                                          := 0;                                       --临时存放累计金额
v_tmpint                number(10)                                          := 0;                                       --临时存放临时数值
v_spaccesscode          varchar2(40)                                        := ' ';                                     --临时存放sp接入码
v_nextrentfee           number(10)                                          := 0;                                       --临时存放待生效包月费
v_rentfeevalidate       char(6)                                             := ' ';                                     --临时存放待生效包月费率生效月
v_tag                   number(3)                                           := 0;                                       --临时存放标志位 ,退订补款产品本月订购过  0:否 1:是
v_totaldctemplate       varchar2(500)                                       :=' ';                                      --临时存放解析模板:产品分档费率
v_dealtemplate          varchar2(500)                                       :=' ';                                      --临时存放解析模板:待解析的分档段
v_totaldclength         number(3)                                           := 0;                                       --临时存放解析模板: 分档字符串的长度
v_totaldcsum            number(10)                                          := 0;                                       --临时存放解析模板: 分档总费用
v_index0                number(3)                                           := 0;                                       --临时存放解析模板: 分档字符串中&的位置
v_index1                number(3)                                           := 0;                                       --临时存放解析模板: 分档字符串中;的位置
v_popuexist             number(3)                                           := 2;                                       --临时存放订购时是否存在推广期:  0目前存在免费推广期  1.目前存在计费推广期  2.目前不在推广期或无推广期 3. 订购时间在推广期,生效时间在推广期之外;
v_testuser              number(3)                                           := 0;                                       --临时存放产品属性:是否试用,0-否,1-是   仅400用户使用
v_freeuseflag           number(3)                                           := 0;                                       --sms,mms,wap,mvs产品含有该字段,免费体验标识0:无试用期1:有,试用到月底,2:按照freedays取值,单位为小时,3:按照freedays取值,单位为月,4:按照freedays取值,单位为天。其他不含改字段的产品只表示免费天数
v_freenotitype          number(3)                                           := 0;                                       --存放流媒体产品免费期通知类型
v_rentday               number(3)                                           := i_rentday;                               --临时存放帐期时间,因为要转换,所以申请临时变量
v_ismutidb              number(3,0)                                         := 0;                                        ---0表示为单DB模式 1表示为多DB模式
v_dblink                varchar2(40)                                        := ' ';                                      ---仅在多DB模式下,返回对应号码的有效DBLINK

v_oa                    zxdbm_ismp.susr_basic.usercode%type                 := ' ';                                     --临时存放发起方/赠送方用户号码
v_ua                    zxdbm_ismp.susr_basic.usercode%type                 := i_ua;                                     --临时存放使用方/受赠方用户号码
v_srcmsisdn             zxdbm_ismp.susr_basic.usercode%type                 := ' ';                                     --临时存放发起方/赠送方用户号码
v_usemsisdn             zxdbm_ismp.susr_basic.usercode%type                 := ' ';                                     --临时存放使用方/受赠方用户号码
v_srcuserindex          zxdbm_ismp.susr_basic.userindex%type                := 0;                                       --临时存放发起方用户序号
v_srcusertype           zxdbm_ismp.susr_basic.usertype%type                 := 1;                                       --临时存放发起方用户类型
v_srcpaytype            zxdbm_ismp.susr_basic.paytype%type                  := 0;                                       --临时存放发起方用户付费类型
v_srcblacklist          zxdbm_ismp.susr_basic.blacklist%type                := 0;                                       --临时存放发起方是否进入黑名单
v_srcwhitelist          zxdbm_ismp.susr_basic.whitelist%type                := 0;                                       --临时存放发起方是否进入白名单
v_srcredlist            zxdbm_ismp.susr_basic.redlist%type                  := 0;                                       --临时存放发起方是否进入红名单
v_srcblacklevel         zxdbm_ismp.susr_basic.blacklevel%type               := 0;                                       --临时存放发起方黑名单级别
v_srcwhitelevel         zxdbm_ismp.susr_basic.whitelevel%type               := 0;                                       --临时存放发起方白名单级别
v_srcmsisdntype         zxdbm_ismp.susr_basic.msisdntype%type               := 1;                                       --临时存放发起方号码类型
v_srcuserstatus         zxdbm_ismp.susr_basic.status%type                   := 0;                                       --临时存放发起方状态
v_srcactivestatus       zxdbm_ismp.susr_basic.activestatus%type             := 0;                                       --临时存放发起方激活状态
v_srcoppaytype          zxdbm_ismp.susr_basic.oppaytype%type                := 0;                                       --临时存放发起方话单专用付费类型
v_srccorpindex          zxdbm_ismp.susr_basic.corpindex%type                := 0;                                       --临时存放发起方集团序号
v_srcusetime            zxdbm_ismp.susr_basic.lastusetime%type              := '00000000000000';                        --临时存放发起方上次使用时间
v_srcmaxlimitfee        zxdbm_ismp.susr_basic.maxlimitfee%type              := 0;                                       --临时存放发起方最大消费限额
v_srcgraylist           zxdbm_ismp.susr_basic.graylist%type                 := 0;                                       --临时存放发起方是否进入灰名单
v_srcgraylevel          zxdbm_ismp.susr_basic.graylevel%type                := 0;                                       --临时存放发起方灰名单级别
v_useuserindex          zxdbm_ismp.susr_basic.userindex%type                := 0;                                       --临时存放使用方用户序号
v_useusertype           zxdbm_ismp.susr_basic.usertype%type                 := 1;                                       --临时存放使用方用户类型
v_usepaytype            zxdbm_ismp.susr_basic.paytype%type                  := 0;                                       --临时存放使用方用户付费类型
v_useblacklist          zxdbm_ismp.susr_basic.blacklist%type                := 0;                                       --临时存放使用方是否进入黑名单
v_usewhitelist          zxdbm_ismp.susr_basic.whitelist%type                := 0;                                       --临时存放使用方是否进入白名单
v_useredlist            zxdbm_ismp.susr_basic.redlist%type                  := 0;                                       --临时存放使用方是否进入红名单
v_useblacklevel         zxdbm_ismp.susr_basic.blacklevel%type               := 0;                                       --临时存放使用方黑名单级别
v_usewhitelevel         zxdbm_ismp.susr_basic.whitelevel%type               := 0;                                       --临时存放使用方白名单级别
v_usemsisdntype         zxdbm_ismp.susr_basic.msisdntype%type               := 1;                                       --临时存放使用方号码类型
v_useuserstatus         zxdbm_ismp.susr_basic.status%type                   := 0;                                       --临时存放使用方状态
v_useactivestatus       zxdbm_ismp.susr_basic.activestatus%type             := 0;                                       --临时存放使用方激活状态
v_useoppaytype          zxdbm_ismp.susr_basic.oppaytype%type                := 0;                                       --临时存放使用方话单专用付费类型
v_usecorpindex          zxdbm_ismp.susr_basic.corpindex%type                := 0;                                       --临时存放使用方集团序号
v_useusetime            zxdbm_ismp.susr_basic.lastusetime%type              := '00000000000000';                        --临时存放使用方上次使用时间
v_usemaxlimitfee        zxdbm_ismp.susr_basic.maxlimitfee%type              := 0;                                       --临时存放使用方最大消费限额
v_usegraylist           zxdbm_ismp.susr_basic.graylist%type                 := 0;                                       --临时存放使用方是否进入灰名单
v_usegraylevel          zxdbm_ismp.susr_basic.graylevel%type                := 0;                                       --临时存放使用方灰名单级别
v_paymsisdn             zxdbm_ismp.susr_basic.usercode%type                 := ' ';                                     --临时存放计费方
v_paymsisdntype         zxdbm_ismp.susr_basic.msisdntype%type               := 1;                                       --临时存放计费方号码类型
v_cardtype              zxdbm_ismp.susr_basic.paytype%type                  := 0;                                       --临时存放用户付费类型0-后付费,1-预付费,2-准预付费
v_srcusercityid         zxdbm_ismp.susr_basic.cityid%type                   := ' ';                                       --临时存放计费方号码类型
v_useusercityid         zxdbm_ismp.susr_basic.cityid%type                   := ' ';                                       --临时存放计费方号码类型

v_srctestcpindex        zxdbm_ismp.susr_testgroup_member.cpindex%type       := 0;                                       --临时存放CP/SP序号
v_usetestcpindex        zxdbm_ismp.susr_testgroup_member.cpindex%type       := 0;                                       --临时存放CP/SP序号
v_srccorpid             zxdbm_ismp.susr_corp.corpid%type                    := ' ';                                     --临时存放集团代码
v_srccorpstatus         zxdbm_ismp.susr_corp.status%type                    := 0;                                       --临时存放集团状态
v_srccorppaymsisdn      zxdbm_ismp.susr_corp.paymsisdn%type                 := ' ';                                     --临时存放集团付费号码
v_corpname              zxdbm_ismp.susr_corp.corpcnshortname%type           := ' ';                                     --临时存放集团中文名简写

v_productindex          zxdbm_200.s200_product.productindex%type            := 0;                                       --临时存放产品序号
v_cancelproductindex    zxdbm_200.s200_product.productindex%type            := 0;                                       --临时存放被189替换的产品序号
v_needreplay            zxdbm_200.s200_product.needreplay%type              := 0;                                       --临时存放是否需要二次确认,0-不需要,1-需要
v_validmode             zxdbm_200.s200_product.validmode%type               := 0;                                       --临时存放定购生效方式,0-立即生效,1-下一帐期生效,2-n周期后生效,3-指定日期生效
v_validcyctype          zxdbm_200.s200_product.validcyctype%type            := 0;                                       --临时存放周期方式0-非周期,1-天,2-周,3-月,定购生效方式为2时生效
v_validcycparam         zxdbm_200.s200_product.validcycparam%type           := 0;                                       --临时存放周期参数,定购生效方式为2时生效
v_validdate             zxdbm_200.s200_product.validdate%type               := v_sysdate14;                             --临时存放指定生效日期
v_subfeemode            zxdbm_200.s200_product.subfeemode%type              := 1;                                       --临时存放定购当月收费方式
v_cancelsubfeemode      zxdbm_200.s200_product.subfeemode%type              := 1;                                       --临时存放被替换定购当月收费方式
v_subfeedays            zxdbm_200.s200_product.subfeedays%type              := 0;                                       --临时存放收费日期取值范围为0~28,0表示本月全部免费,其他表示 n号之后免费。
v_freedays              zxdbm_200.s200_product.freedays%type                := 0;                                       --临时存放免费试用天数
v_freenotify            zxdbm_200.s200_product.freenotify%type              := 0;                                       --临时存放免费试用期到处理方式,0-到期自动收费,1-到期需用户确认,2-到期自动取消
v_popustart             zxdbm_200.s200_product.popularizestart%type         := v_sysdate8;                              --临时存放推广期开始时间
v_popustop              zxdbm_200.s200_product.popularizestop%type          := v_sysdate8;                              --临时存放推广期结束时间
v_popuitem              zxdbm_200.s200_product.popularizeitem%type          := 0;                                       --临时存放推广期计费方案,0表示免费,1-推广期方案
v_cancelpopustart       zxdbm_200.s200_product.popularizestart%type         := v_sysdate8;                              --临时存放被替换产品推广期开始时间
v_cancelpopustop        zxdbm_200.s200_product.popularizestop%type          := v_sysdate8;                              --临时存放被替换产品推广期结束时间
v_cancelpopuitem        zxdbm_200.s200_product.popularizeitem%type          := 0;                                       --临时存放被替换产品推广期计费方案,0表示免费,1-推广期方案
v_needcharge            zxdbm_200.s200_product.needcharge%type              := 1;                                       --临时存放是否需要计费,0-免费,1-需要计费
v_cancelneedcharge      zxdbm_200.s200_product.needcharge%type              := 1;                                       --临时存放是否需要计费,0-免费,1-需要计费
v_productname           zxdbm_200.s200_product.productname%type             := ' ';                                     --临时存放产品名称
v_productstatus         zxdbm_200.s200_product.status%type                  := 0;                                       --临时存放产品状态
v_onlyforcorp           zxdbm_200.s200_product.onlyforcorp%type             := 0;                                       --临时存放是否集团专用产品0:个人用户专用1:集团专用
v_productstarttime      zxdbm_200.s200_product.starttime%type               := v_sysdate14;                             --临时存放产品生效时间
v_productendtime        zxdbm_200.s200_product.endtime%type                 := '99991231235959';                        --临时存放产品终止时间
v_workflow              zxdbm_200.s200_product.workflow%type                := 0;                                       --临时存放工作流状态
v_workflowlife          zxdbm_200.s200_product.workflowlife%type            := 0;                                       --临时存放工作流流程状态
v_cpindex               zxdbm_200.s200_product.cpindex%type                 := 0;                                       --临时存放CP/SP序号
v_serviceindex          zxdbm_200.s200_product.serviceindex%type            := 0;                                       --临时存放业务序号
v_smschargingcn         zxdbm_200.s200_product.smschargingcn%type           := ' ';                                     --临时存放产品资费短信描述
v_canbepresent          zxdbm_200.s200_product.canbepresent%type            := 0;                                       --临时存放是否可赠送,0-不可赠送,1-可赠送
v_servicetype           zxdbm_200.s200_product.servicetype%type             := 0;                                       --临时存放业务能力类型
v_subcapability         zxdbm_200.s200_product.subcapability%type           := 1;                                       --临时存放业务能力子类型1-3G,2-PHS,3-ISAG,默认为3g业务能力(仅电信使用,其他营运商皆默认为1)
v_srvprodid             zxdbm_200.s200_product.productid%type               := i_srvprodid;                             --临时存放业务产品代码
v_productid             zxdbm_200.s200_product.productid%type               := ' ';                                     --临时存放产品代码
v_cancelproductid       zxdbm_200.s200_product.productid%type               := ' ';                                     --临时存放被189替换的产品代码
v_onlyforpkg            zxdbm_200.s200_product.onlyforpkg%type              := 0;                                       --临时存放是否套餐专用产品
v_ordertype             zxdbm_200.s200_product.ordertype%type               := 1;                                       --临时存放产品订购类型
v_backfeemode           zxdbm_200.s200_product.backfeemode%type             := 0;                                       --临时存放退定补款方式,0-不补款,1-按帐期剩余实际天数补款
v_productsource         zxdbm_200.s200_product.productsource%type           := 0;                                       --产品来源0:ISMP 发布的产品1:同步自全国SPMS 的产品2:提供给漫游用户的产品
v_invalidmode           zxdbm_200.s200_product.invalidmode%type             := 0;                                       --临时存放退订生效方式,0 立即生效,1-下一帐期生效 2-退定后可使用足月

v_chargetype            zxdbm_200.s200_product_fee.chargetype%type          := 0;                                       --临时存放计费类型
v_cancelchargetype      zxdbm_200.s200_product_fee.chargetype%type          := 0;                                       --临时存放被替换产品计费类型
v_jbchargetype          zxdbm_200.s200_product_fee.chargetype%type          := 0;                                       --临时存放推广期产品的基本计费费率的计费类型
v_subchargetype         zxdbm_200.s200_product_fee.subchargetype%type       := 0;                                       --临时存放计费子类型
v_cyctype               zxdbm_200.s200_product_fee.cyctype%type             := 0;                                       --临时存放周期类型0-非周期,1-周期性
v_cycunit               zxdbm_200.s200_product_fee.cycunit%type             := 0;                                       --临时存放周期单位 0-无,1-天,2-周,3-月,4-半年,5-年,6-小时,7-分钟,8-秒
v_cycparam              zxdbm_200.s200_product_fee.cycparam%type            := 0;                                       --临时存放周期参数
v_jbcyctype             zxdbm_200.s200_product_fee.cyctype%type             := 0;                                       --临时存放推广期产品的基本计费费率的周期类型0-非周期,1-周期性
v_jbcycunit             zxdbm_200.s200_product_fee.cycunit%type             := 0;                                       --临时存放推广期产品的基本计费费率的周期单位 0-无,1-天,2-周,3-月,4-半年,5-年,6-小时,7-分钟,8-秒
v_jbcycparam            zxdbm_200.s200_product_fee.cycparam%type            := 0;                                       --临时存放推广期产品的基本计费费率的周期参数
v_rentfee               zxdbm_200.s200_product_fee.rentfee%type             := 0;                                       --临时存放周期性费用金额
v_fixfee                zxdbm_200.s200_product_fee.fixfee%type              := 0;                                       --临时存放包量费用
v_cancelfixfee          zxdbm_200.s200_product_fee.fixfee%type              := 0;                                       --临时存放被替换产品包量费用
v_fixusetimes           zxdbm_200.s200_product_fee.fixusetimes%type         := 0;                                       --临时存放包量使用值,0-无限制
v_autocontinue          zxdbm_200.s200_product_fee.autocontinue%type        := 0;                                       --临时存放周期性定购,是否自动续订
v_minlimitfee           zxdbm_200.s200_product_fee.minlimitfee%type         := 0;                                       --临时存放保底费用,0-无保底费
v_usecomfee             zxdbm_200.s200_product_fee.usecomfee%type           := 1;                                       --临时存放限制(量、时间)外使用方式,1-使用基本费率,0-不允许继续使用
v_totaldiscount         zxdbm_200.s200_product_fee.totaldiscount%type       := ' ';                                     --临时存放限制(量、时间)外使用方式,1-使用基本费率,0-不允许继续使用
v_cancelrentfee         zxdbm_200.s200_product_fee.rentfee%type             := 0;                                       --临时存放业务订购原产品的周期性费用金额
v_cancelminlimitfee     zxdbm_200.s200_product_fee.minlimitfee%type         := 0;                                       --临时存放业务订购原产品的保底费用,0-无保底费
v_description           zxdbm_200.s200_product_fee.description%type         := 0;                                       --产品费率描述
v_serviceid             zxdbm_200.s200_service.serviceid%type               := ' ';                                     --临时存放业务代码
v_servicestatus         zxdbm_200.s200_service.status%type                  := 0;                                       --临时存放业务状态
v_srvcolorlist          zxdbm_200.s200_service.colorlist%type               := 0;                                       --临时存放是否有颜色列表,0-无,1-黑名单,2-白名单
v_servicename           zxdbm_200.s200_service.servicename%type             := ' ';                                     --临时存放业务名称
v_subsrvtype            zxdbm_200.s200_service.subsrvtype%type              := 0;                                       --临时存放业务子类型
v_orderupdateurl        zxdbm_200.s200_service.orderupdateurl%type          := ' ';                                     --临时存放业务表中订购关系更新通知URL
v_srcopenstatus         zxdbm_200.s200_service_useropen.status%type         := 1;                                       --临时存放发起方个人能力开通,0-关闭,1-开通
v_useopenstatus         zxdbm_200.s200_service_useropen.status%type         := 1;                                       --临时存放使用方个人能力开通,0-关闭,1-开通
v_protocoltype          zxdbm_200.s200_sp_service_type.protocoltype%type    := 0;                                       --临时存放定购同步协议类型0-ismp(wbs方式),1-smgp2.0(cngp1.0),2-smgp3.0(cngp1.0扩展)
--added by youqunxia 20130817  Order3.02.01
v_spsrvstatus           zxdbm_200.s200_sp_service_type.status%type          := 1;                                       --临时存放SP业务能力状态
v_spsrvstarttime        zxdbm_200.s200_sp_service_type.starttime%type       := '00000000000000';                        --临时存放业务能力签约生效时间
v_spsrvendtime          zxdbm_200.s200_sp_service_type.endtime%type         := '99991231235959';                        --临时存放业务能力签约失效时间
v_servicesort           zxdbm_200.s200_service.servicesort%type             := 0;                                       --业务归属 0:普通业务、1:属于OIDD 2:属于定位
v_isexperience          zxdbm_200.s200_user_subscription.isexperience%type  := to_char(i_isexperience);                 --订购类型:0:正常订购 1:免费体验 2:积分兑换 3:体验转正式 默认为0
v_chargetime            zxdbm_200.s200_user_subscription.chargetime%type;                                               --首次扣费时间
v_isexperience1         zxdbm_200.s200_user_subscription.isexperience%type  := '0';                                     --订购类型:0:正常订购 1:免费体验 2:积分兑换 3:体验转正式 默认为0

v_freetime              zxdbm_200.s200_user_subscription.freetime%type      := '00000000000000';                        --临时存放订购关系免费截止期
v_substarttime          zxdbm_200.s200_user_subscription.starttime%type     := v_sysdate14;                             --临时存放订购关系生效时间
v_subendtime            zxdbm_200.s200_user_subscription.endtime%type       := '99991231235959';                        --临时存放订购关系失效时间
v_substatus             zxdbm_200.s200_user_subscription.status%type        := 0;                                       --临时存放订购关系状态
v_cursubstatus          zxdbm_200.s200_user_subscription.status%type        := 0;                                       --临时存放当前订购关系状态
v_lastrenttime          zxdbm_200.s200_user_subscription.lastrenttime%type  := '00000000000000';                        --临时存放上次扣租时间
v_cancellastrenttime    zxdbm_200.s200_user_subscription.lastrenttime%type  := '00000000000000';                        --临时存放被189替换的产品上次扣租时间
v_cancelnextrentdate    zxdbm_200.s200_user_subscription.lastrenttime%type  := '99991231';                              --临时存放被189替换的产品下次扣租日期
v_payfordate            zxdbm_200.s200_user_subscription.payfordate%type    := '00000000';                              --临时存放月租已付日期
v_cancelpayfordate      zxdbm_200.s200_user_subscription.payfordate%type    := '00000000';                              --临时存放月租已付日期
v_productpkgindex       zxdbm_200.s200_user_subscription.productpkgindex%type   := 0;                                   --临时存放套餐序号
v_cancelfreetime        zxdbm_200.s200_user_subscription.freetime%type      := '00000000000000';                        --临时存放订购关系免费截止期
--added by youqunxia20130907 order3.02.01
v_lastvarifee           zxdbm_200.s200_user_subscription.varifee%type       := '';                                      --临时存放当前订购关系中的前一次可变价格                                                                                                             
v_nextrentdate          zxdbm_200.s200_user_subscription.nextrentdate%type  := '99991231';                              --临时存放下次扣租日期
v_cancelisexperience    zxdbm_200.s200_user_subscription.isexperience%type  := '0';                                     --临时存放被替换的产品订购类型  

v_cpid                  zxdbm_ismp.scp_basic.cpid%type                      := ' ';                                     --临时存放CP/SP代码
v_cpstatus              zxdbm_ismp.scp_basic.status%type                    := 0;                                       --临时存放CP/SP代码
v_cpsubscribeurl        zxdbm_ismp.scp_basic.cpsubscribeurl%type            := ' ';                                     --临时存放CP/SP订购URL
v_hasblacklist          zxdbm_ismp.scp_basic.hasblacklist%type              := 0;                                       --临时存放是否有黑名单控制 0-无,1-有
v_cptype                zxdbm_ismp.scp_basic.cptype%type                    := 2;                                       --临时存放CP/SP类型
v_cpsrvtype             zxdbm_ismp.scp_basic.cpsrvtype%type                 := 0;                                       --临时存放CP服务类型
v_insubblacklist        zxdbm_ismp.scp_basic.insubblacklist%type            := 0;                                      --临时存放限制订购SP黑名单标识
v_cpcnshortname         zxdbm_ismp.scp_basic.cpcnshortname%type             := ' ';                                     --临时存放CP/SP中文名简写
v_customersrvtel        zxdbm_ismp.scp_basic.customersrvtel%type            := ' ';                                     --临时存放CP/SP客服电话

v_perssrvopen           zxdbm_ismp.ssys_service_type.perssrvopen%type       := 1;                                       --临时存放个人能力缺省开通,0-关闭,1-开通
v_srvtypeid             zxdbm_ismp.ssys_service_type.srvtypeid%type         := ' ';                                     --临时存放话单中的srvtypeid

v_productsubmode        zxdbm_ismp.ssrv_corp_product.subscribemode%type     := 0;                                       --临时存放定购方式,0-必须集团管理员定购,1-集团管理员与集团成员均可定购
v_productpayparty       zxdbm_ismp.ssrv_corp_product.payparty%type          := 2;                                       --临时存放集团专用产品付费方 1-集团付费 2-个人付费
v_corpproductstatus     zxdbm_ismp.ssrv_corp_product.status%type            := 0;                                       --临时存放集团与产品关联状态 0-正常;1-删除
v_phoneindex            zxdbm_ismp.ssys_phone.phoneindex%type               := 0;                                       --临时存放号段序号
v_phoneindexa           zxdbm_ismp.ssys_phone.phoneindex%type               := 0;                                       --临时存放A省号段序号,跨省赠送
v_phoneindexb           zxdbm_ismp.ssys_phone.phoneindex%type               := 0;                                       --临时存放B省号段序号,跨省赠送
v_attach                zxdbm_ismp.ssys_phone.attach%type                   := 0;                                       --临时存放预付费归属,0-未知,1-OCS,2-RTBP
v_expiredate            zxdbm_ismp.ssrv_user_totalamount.expiredate%type    := to_number(substr(v_curmonthendtime,1,8));--临时存放过期日期

v_srctypesrvopen        zxdbm_ismp.ssrv_user_perssrvopen.perssrvopen%type   := 1;                                       --临时存放发起方个人能力开通,0-关闭,1-开通
v_usetypesrvopen        zxdbm_ismp.ssrv_user_perssrvopen.perssrvopen%type   := 1;                                       --临时存放使用方个人能力开通,0-关闭,1-开通

begin

--出参初始化
o_retcode               := 0;
o_debugpos              := 0;
o_debuginfo             := ' ';
o_srcmsisdn             := ' ';
o_usemsisdn             := ' ';
o_paymsisdn             := ' ';
o_srcmsisdntype         := 1;
o_usemsisdntype         := 1;
o_userindex             := 0;
o_payuserindex          := 0;
o_needreplay            := 0;
o_paytype               := 0;
o_oppaytype             := 0;
o_scptype               := 2;
o_subfee                := 0;
o_colortype             := 0;
o_usertype              := 0;
o_producttype           := 1;
o_cpid                  := ' ';
o_cpindex               := 0;
o_cpcnshortname         := ' ';
o_cpsubscribeurl        := ' ';
o_cptype                := 0;
o_protocoltype          := 0;
o_msgtosp               := ' ';
o_cnfmmsgtosp           := ' ';
o_customersrvtel        := ' ';
o_serviceid             := ' ';
o_serviceindex          := 0;
o_servicename           := ' ';
o_productid             := ' ';
o_productindex          := 0;
o_productname           := ' ';
o_smschargingcn         := ' ';
o_autocontinue          := 0;
o_servicetype           := 0;
o_subcapability         := 0;
o_srvtypeid             := ' ';
o_corpid                := ' ';
o_corpindex             := 0;
o_corpcnshortname       := ' ';
o_paymode               := '个人付费';
o_freetime              := v_sysdate14;
o_nextrentdate          := '99991231';
o_substarttime          := v_sysdate14;
o_subendtime            := '99991231235959';
o_substatus             := 0;
o_lastrenttime          := '00000000000000';
o_payfordate            := '00000000';
o_fixusetimes           := 0;
o_chargetype            := 0;
o_cyctype               := 0;
o_linknum               := ' ';
o_maxlimitfee           := '0';
o_pseudocode            := '';
o_pcodemsisdnswitch     := 0;
o_chanelplayerstate     := 0;
o_eventstate            := 1;
o_ordertype             := 1;
o_spaccesscode          := i_spaccesscode;
o_maincapability        := 0;
o_submaincapability     := 0;
o_cpsrvtype             := 0;
o_cancelproductid       := ' ';
o_issecondproduct       := 0;
o_cancelfeaturecode     := ' ';
o_cancelaccesscode      := ' ';
o_provincesend          := 0;
o_cdrbegintime          := v_sysdate14;
o_cdrendtime            := v_curmonthendtime;
o_cnfmtype              := 0;
o_validmode             := 0;
o_mutiluserexit         := 1;
o_popustop              := v_popustop;
o_popuexist             := v_popuexist;
o_freenoticenow         := 0;
o_freedays              := 0;
o_productpkgname        := ' ';
o_freenotitype          := 0;
o_needcharge            := 0;
o_cityid                := '';

--added by youqunxia 2013.02.18
o_freenotify            := 1;
--added by youqunxia 20130818  Order3.02.01
o_hassubscription       := 0;
o_rentfee               := 0;
o_haswebgw              := 1;
o_lastvarifee           := '';
o_issynctooidd          := 0;
o_isexperience          := '0';
o_chargetime            := '';

/
--part-- --订购-- 预处理
*/

--批量操作不进行鉴权,由业务从用户表中查询处理
--modied by youqunxia20130530 order2.07.10
if i_oatype not in (1,2,3,4) then
    o_retcode   := 107;
    o_debugpos := 1;
    o_debuginfo := 'i_oatype is not valid.';
    return;
end if;

if v_rentday = 0 then
    v_rentday := to_number(substr(v_sysdate8,7,2));
    if v_rentday >= 27 then
        v_rentday := v_rentday - 18;
    end if;
end if;

--发起方、使用方用户号码去前缀
v_oa := ltrim(rtrim(nvl(i_oa,'0')));
v_ua := ltrim(rtrim(nvl(i_ua,'0')));
if v_oa like ('+' || v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 2);
elsif v_oa like ('00' || v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 3);
elsif v_oa like (v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 1);
end if;
if (substr(v_oa,1,1) <> '0') and (i_oatype = 4) then
    v_oa := '0' || v_oa;
end if;
if v_ua like ('+' || v_countrycode || '%') then
    v_ua := substr(v_ua, length(v_countrycode) + 2);
elsif v_ua like ('00' || v_countrycode || '%') then
    v_ua := substr(v_ua, length(v_countrycode) + 3);
elsif v_ua like (v_countrycode || '%') then
    v_ua := substr(v_ua, length(v_countrycode) + 1);
end if;
v_srcmsisdn := v_oa;
if (i_isthird = 1) then
    v_usemsisdn  := v_ua;
else
    v_usemsisdn  := v_oa;
end if;
o_srcmsisdn  := v_srcmsisdn;
o_usemsisdn  := v_usemsisdn;
o_paymsisdn  := v_srcmsisdn;
if (v_srcmsisdn = '0') or (v_usemsisdn = '0') then
    o_retcode   := 1001;
    o_debugpos := 2;
    o_debuginfo := 'v_srcmsisdn or v_usemsisdn is not valid.';
    return;
end if;

/
--part-- --订购-- 是否跨省赠送的鉴权(根据号段鉴权是否本省用户)

--赠送他省不对DA鉴权,来自他省赠送不对OA鉴权

/

if i_provinceorder = 1 then
    if i_actiontype in (1,17) and (i_isfromnmsc = 1) then
        begin
            select phoneindex
            into   v_phoneindexa
            from   zxdbm_ismp.ssys_phone
            where  startprefix <= substr(v_srcmsisdn,1,length(startprefix))
            and    endprefix >= substr(v_srcmsisdn,1,length(endprefix));
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 3;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        begin
            select phoneindex
            into   v_phoneindexb
            from   zxdbm_ismp.ssys_phone
            where  startprefix <= substr(v_usemsisdn,1,length(startprefix))
            and    endprefix >= substr(v_usemsisdn,1,length(endprefix));
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 4;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        if v_phoneindexa <> 0 and v_phoneindexb = 0 then
            o_provincesend := 1;
            v_usemsisdn := v_srcmsisdn;
        elsif v_phoneindexa = 0 and v_phoneindexb <> 0 then
            o_provincesend := 2;
            v_srcmsisdn := v_usemsisdn;
        end if;
    end if;
end if;

/
--part-- --订购-- 发起方和使用方鉴权
/

if (i_needauthuser = 1) then
    if (i_isthird = 1) and (v_srcmsisdn <> v_usemsisdn) then
        --OA鉴权
        begin
            select userindex,usertype,paytype,blacklist,whitelist,
                   redlist,blacklevel,whitelevel,msisdntype,status,
                   activestatus,oppaytype,corpindex,lastusetime,maxlimitfee,
                   graylist,graylevel,cityid
            into   v_srcuserindex,v_srcusertype,v_srcpaytype,v_srcblacklist,v_srcwhitelist,
                   v_srcredlist,v_srcblacklevel,v_srcwhitelevel,v_srcmsisdntype,v_srcuserstatus,
                   v_srcactivestatus,v_srcoppaytype,v_srccorpindex,v_srcusetime,v_srcmaxlimitfee,
                   v_srcgraylist,v_srcgraylevel,v_srcusercityid
            from   zxdbm_ismp.susr_basic
            where  usercode = v_srcmsisdn;
        exception when no_data_found then
            o_retcode   := 1001;    --用户不存在
            o_debugpos := 5;
            o_debuginfo := 'v_srcmsisdn do not exist;';
            if (i_oatype = 4) then
                o_srcmsisdntype := 2;
            else
                o_srcmsisdntype := 1;
            end if;
            o_usemsisdntype := 1;
            return;
        when others then
            o_retcode   := 506; --数据库错误
            o_debugpos := 6;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            if (i_oatype = 4) then
                o_srcmsisdntype := 2;
            else
                o_srcmsisdntype := 1;
            end if;
            o_usemsisdntype := 1;
            return;
        end;
        o_srcmsisdntype := v_srcmsisdntype;

-- o_usemsisdntype := v_usemsisdntype;
-- o_userindex := v_useuserindex;

        o_payuserindex  := v_srcuserindex;
        o_paytype       := v_srcpaytype;
        o_oppaytype     := v_srcoppaytype;
        o_cityid        := trim(v_srcusercityid);

-- o_usertype := v_useusertype;

        if (i_cmdchannel in (7,32)) then
            if v_srcmsisdntype not in (1,2,3) then
                o_retcode   := 100;
                o_debugpos := 7;
                o_debuginfo := 'v_srcmsisdntype is not valid in database.';
                return;
            end if;
        end if;
        --OA更新用户表字段
        if (v_srcactivestatus = 0) then
        --待激活用户激活操作
            begin
                update  zxdbm_ismp.susr_basic
                set     activestatus = 1,
                        activetime = v_sysdate14
                where   userindex = v_srcuserindex;
                if (sql%rowcount = 0) then
                    o_retcode   := 506;
                    o_debugpos := 8;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    rollback;
                    return;
                end if;
                commit;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 9;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
        else
        --已激活用户更新最近一次使用时间
            if (i_silent = 1) then
                if (substr(v_srcusetime,1,8) <> v_sysdate8) then
                    begin
                        update  zxdbm_ismp.susr_basic
                        set     lastusetime = v_sysdate14
                        where   userindex = v_srcuserindex;
                        if (sql%rowcount = 0) then
                            o_retcode   := 506;
                            o_debugpos := 10;
                            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                            rollback;
                            return;
                        end if;
                        commit;
                    exception when others then
                        o_retcode   := 506;
                        o_debugpos := 11;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        return;
                    end;
                end if;
            end if;
        end if;
        --OA用户装他鉴权
        if v_srcuserstatus in (1,3) then
            o_retcode   := 1002;
            o_debugpos := 12;
            o_debuginfo := 'v_srcuserstatus='||v_srcuserstatus||', The subscriber status is 1 or 3.';
            return;
        elsif v_srcuserstatus = 4 then
            o_retcode   := 1008;
            o_debugpos := 13;
            o_debuginfo := 'v_srcuserstatus='||v_srcuserstatus||', The subscriber status is 4.';
            return;
        end if;

        --DA鉴权
        --******************************************************
        begin
            select userindex,usertype,paytype,blacklist,whitelist,
                   redlist,blacklevel,whitelevel,msisdntype,status,
                   activestatus,oppaytype,corpindex,lastusetime,maxlimitfee,
                   graylist,graylevel,cityid
            into   v_useuserindex,v_useusertype,v_usepaytype,v_useblacklist,v_usewhitelist,
                   v_useredlist,v_useblacklevel,v_usewhitelevel,v_usemsisdntype,v_useuserstatus,
                   v_useactivestatus,v_useoppaytype,v_usecorpindex,v_useusetime,v_usemaxlimitfee,
                   v_usegraylist,v_usegraylevel,v_useusercityid
            from   zxdbm_ismp.susr_basic
            where  usercode = v_usemsisdn;
        exception when no_data_found then
            if i_mutidbmode = 1 then
                o_mutiluserexit := 0;
                zxdbm_ismp.sp_getdblink(v_usemsisdn,v_usemsisdntype,v_ismutidb,v_dblink);
                v_sql := 'select userindex,usertype,paytype,blacklist,whitelist,'||
                         'redlist,blacklevel,whitelevel,msisdntype,status,'||
                         'activestatus,oppaytype,corpindex,lastusetime,maxlimitfee,'||
                         'graylist,graylevel,cityid'||
                         ' from zxdbm_ismp.susr_basic'||'@'||v_dblink||
                         ' where usercode = :p1';
                begin
                    execute immediate v_sql
                    into  v_useuserindex,v_useusertype,v_usepaytype,v_useblacklist,v_usewhitelist,
                          v_useredlist,v_useblacklevel,v_usewhitelevel,v_usemsisdntype,v_useuserstatus,
                          v_useactivestatus,v_useoppaytype,v_usecorpindex,v_useusetime,v_usemaxlimitfee,
                          v_usegraylist,v_usegraylevel,v_useusercityid
                    using v_usemsisdn;
                exception when no_data_found then
                    o_retcode   := 1001;    --用户不存在
                    o_debugpos := 16;
                    o_debuginfo := 'v_usemsisdn do not exist;';
                    return;
                when others then
                    o_retcode   := 506; --数据库错误
                    o_debugpos := 17;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
            else
                o_retcode   := 1001;    --用户不存在
                o_debugpos := 18;
                o_debuginfo := 'v_usemsisdn do not exist;';
                return;
            end if;
        when others then
            o_retcode   := 506; --数据库错误
            o_debugpos := 19;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        o_usemsisdntype := v_usemsisdntype;
        o_userindex     := v_useuserindex;
        o_usertype      := v_useusertype;
        if (i_cmdchannel in (7,32)) then
            if (v_srcmsisdntype not in (1,2,3)) or (v_usemsisdntype not in (1,2,3)) then
                o_retcode   := 100;
                o_debugpos := 20;
                o_debuginfo := 'v_srcmsisdntype or v_usemsisdntype is not valid in database.';
                return;
            end if;
        end if;
        if o_mutiluserexit = 1 then
            --更新用户激活时间
            if (v_useactivestatus = 0) then
            --待激活用户激活操作
                begin
                    update  zxdbm_ismp.susr_basic
                    set     activestatus = 1,
                            activetime = v_sysdate14
                    where   userindex = v_useuserindex;
                    if (sql%rowcount = 0) then
                        o_retcode   := 506;
                        o_debugpos := 21;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        rollback;
                        return;
                    end if;
                    commit;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 22;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
            else
            --已激活用户更新最近一次使用时间
                if (i_silent = 1) then
                    if (substr(v_useusetime,1,8) <> v_sysdate8) then
                        begin
                            update  zxdbm_ismp.susr_basic
                            set     lastusetime = v_sysdate14
                            where   userindex = v_useuserindex;
                            if (sql%rowcount = 0) then
                                o_retcode   := 506;
                                o_debugpos := 23;
                                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                                rollback;
                                return;
                            end if;
                            commit;
                        exception when others then
                            o_retcode   := 506;
                            o_debugpos := 24;
                            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                            return;
                        end;
                    end if;
                end if;
            end if;
            --使用方状态鉴权
            if v_useuserstatus in (1,3) then
                o_retcode   := 1002;
                o_debugpos := 25;
                o_debuginfo := 'v_useuserstatus='||v_useuserstatus||', The user status is 1 or 3.';
                return;
            elsif v_useuserstatus = 4 then
                o_retcode   := 1008;
                o_debugpos := 26;
                o_debuginfo := 'v_useuserstatus='||v_useuserstatus||', The user status is 4.';
                return;
            end if;
        end if;
    else
        begin
            select userindex,usertype,paytype,blacklist,whitelist,
                   redlist,blacklevel,whitelevel,msisdntype,status,
                   activestatus,oppaytype,corpindex,lastusetime,maxlimitfee,
                   graylist,graylevel,cityid
            into   v_srcuserindex,v_srcusertype,v_srcpaytype,v_srcblacklist,v_srcwhitelist,
                   v_srcredlist,v_srcblacklevel,v_srcwhitelevel,v_srcmsisdntype,v_srcuserstatus,
                   v_srcactivestatus,v_srcoppaytype,v_srccorpindex,v_srcusetime,v_srcmaxlimitfee,
                   v_srcgraylist,v_srcgraylevel,v_srcusercityid
            from   zxdbm_ismp.susr_basic
            where  usercode = v_srcmsisdn;
        exception when no_data_found then
            o_retcode   := 1001;    --用户不存在
            o_debugpos := 27;
            o_debuginfo := 'v_srcmsisdn='||v_srcmsisdn||', user do not exist.';
            if (i_oatype = 4) then
                o_srcmsisdntype := 2;
            else
                o_srcmsisdntype := 1;
            end if;
            o_usemsisdntype := o_srcmsisdntype;
            return;
        when others then
            o_retcode   := 506; --数据库错误
            o_debugpos := 28;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            if (i_oatype = 4) then
                o_srcmsisdntype := 2;
            else
                o_srcmsisdntype := 1;
            end if;
            o_usemsisdntype := o_srcmsisdntype;
            return;
        end;
        v_useuserindex    := v_srcuserindex;
        v_useusertype     := v_srcusertype;
        v_usepaytype      := v_srcpaytype;
        v_useblacklist    := v_srcblacklist;
        v_usewhitelist    := v_srcwhitelist;
        v_useredlist      := v_srcredlist;
        v_useblacklevel   := v_srcblacklevel;
        v_usewhitelevel   := v_srcwhitelevel;
        v_usemsisdntype   := v_srcmsisdntype;
        v_useuserstatus   := v_srcuserstatus;
        v_useactivestatus := v_srcactivestatus;
        v_useoppaytype    := v_srcoppaytype;
        v_usecorpindex    := v_srccorpindex;
        v_useusetime      := v_srcusetime;
        v_usemaxlimitfee  := v_srcmaxlimitfee;
        v_usegraylist     := v_srcgraylist;
        v_usegraylevel    := v_srcgraylevel;

        o_srcmsisdntype := v_srcmsisdntype;
        o_usemsisdntype := v_usemsisdntype;
        o_userindex     := v_useuserindex;
        o_payuserindex  := v_srcuserindex;
        o_paytype       := v_srcpaytype;
        o_oppaytype     := v_srcoppaytype;
        o_usertype      := v_useusertype;
        o_cityid        := trim(v_srcusercityid);
        if (i_cmdchannel in (7,32)) then
            if v_srcmsisdntype not in (1,2,3) then
                o_retcode   := 107;
                o_debugpos := 29;
                o_debuginfo := 'v_srcmsisdntype is not valid.';
                return;
            end if;
        end if;
        if (v_srcactivestatus = 0) then
        --待激活用户激活操作
            begin
                update  zxdbm_ismp.susr_basic
                set     activestatus = 1,
                        activetime = v_sysdate14
                where   userindex = v_srcuserindex;
                if (sql%rowcount = 0) then
                    o_retcode   := 506;
                    o_debugpos := 30;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    rollback;
                    return;
                end if;
                commit;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 31;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
        else
        --已激活用户更新最近一次使用时间
            if (i_silent = 1) then
                if (substr(v_srcusetime,1,8) <> v_sysdate8) then
                    begin
                        update  zxdbm_ismp.susr_basic
                        set     lastusetime = v_sysdate14
                        where   userindex = v_srcuserindex;
                        if (sql%rowcount = 0) then
                            o_retcode   := 506;
                            o_debugpos := 32;
                            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                            rollback;
                            return;
                        end if;
                        commit;
                    exception when others then
                        o_retcode   := 506;
                        o_debugpos := 33;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        return;
                    end;
                end if;
            end if;
        end if;
        --发起方状态鉴权
        if v_srcuserstatus in (1,3) then
            o_retcode   := 1002;
            o_debugpos := 34;
            o_debuginfo := 'v_srcuserstatus='||v_srcuserstatus||', subscriber status is 1 or 3.';
            return;
        elsif v_srcuserstatus = 4 then
            o_retcode   := 1008;
            o_debugpos := 35;
            o_debuginfo := 'v_srcuserstatus='||v_srcuserstatus||', subscriber status is 4.';
            return;
        end if;
    end if;
end if;

/
--part-- --订购-- 产品,产品费率鉴权
/

--产品存在性鉴权
if (i_direct = 1) and (i_supportproductid = 0) then
--反向订购,并且支持业务代码
    if i_servicetype = 6 then  --400用户的表结构不同,具有testuser、freeuseflag字段
        v_sql := 'select productindex,needreplay,validmode,validcyctype,invalidmode,'||
                 'validcycparam,validdate,subfeemode,subfeedays,freedays,freeuseflag,freenotify,'||
                 'popularizestart,popularizestop,popularizeitem,needcharge,'||
                 'productname,status,onlyforcorp,starttime,endtime,'||
                 'workflow,workflowlife,cpindex,serviceindex,smschargingcn,canbepresent,'||
                 'servicetype,subcapability,onlyforpkg,productid,ordertype,backfeemode,testuser,productsource '||
                 'from zxdbm_200.s200_product where serviceid = :p1';
        begin
            execute immediate v_sql
            into   v_productindex,v_needreplay,v_validmode,v_validcyctype,v_invalidmode,
                   v_validcycparam,v_validdate,v_subfeemode,v_subfeedays,v_freedays,v_freeuseflag,v_freenotify,
                   v_popustart,v_popustop,v_popuitem,v_needcharge,
                   v_productname,v_productstatus,v_onlyforcorp,v_productstarttime,v_productendtime,
                   v_workflow,v_workflowlife,v_cpindex,v_serviceindex,v_smschargingcn,v_canbepresent,
                   v_servicetype,v_subcapability,v_onlyforpkg,v_productid,v_ordertype,v_backfeemode,v_testuser,v_productsource
            using v_srvprodid;
        exception when no_data_found then
            o_retcode   := 2100;
            o_debugpos := 36;
            o_debuginfo := 'The product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 37;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    --modied by youqunxia20130530 order2.07.10
    elsif i_servicetype in (1,2,3,5,33,98) then  --200、300、500、600、1900、9800用户的表结构具有freeuseflag字段
        v_sql := 'select productindex,needreplay,validmode,validcyctype,invalidmode,'||
                 'validcycparam,validdate,subfeemode,subfeedays,freedays,freeuseflag,freenotify,'||
                 'popularizestart,popularizestop,popularizeitem,needcharge,'||
                 'productname,status,onlyforcorp,starttime,endtime,'||
                 'workflow,workflowlife,cpindex,serviceindex,smschargingcn,canbepresent,'||
                 'servicetype,subcapability,onlyforpkg,productid,ordertype,backfeemode,testuser,productsource '||
                 'from zxdbm_200.s200_product where serviceid = :p1';
        begin
            execute immediate v_sql
            into   v_productindex,v_needreplay,v_validmode,v_validcyctype,v_invalidmode,
                   v_validcycparam,v_validdate,v_subfeemode,v_subfeedays,v_freedays,v_freeuseflag,v_freenotify,
                   v_popustart,v_popustop,v_popuitem,v_needcharge,
                   v_productname,v_productstatus,v_onlyforcorp,v_productstarttime,v_productendtime,
                   v_workflow,v_workflowlife,v_cpindex,v_serviceindex,v_smschargingcn,v_canbepresent,
                   v_servicetype,v_subcapability,v_onlyforpkg,v_productid,v_ordertype,v_backfeemode,v_testuser,v_productsource
            using v_srvprodid;
        exception when no_data_found then
            o_retcode   := 2100;
            o_debugpos := 38;
            o_debuginfo := 'The product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 39;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;

    else
        v_sql := 'select productindex,needreplay,validmode,validcyctype,invalidmode,'||
                 'validcycparam,validdate,subfeemode,subfeedays,freedays,freenotify,'||
                 'popularizestart,popularizestop,popularizeitem,needcharge,'||
                 'productname,status,onlyforcorp,starttime,endtime,'||
                 'workflow,workflowlife,cpindex,serviceindex,smschargingcn,canbepresent,'||
                 'servicetype,subcapability,onlyforpkg,productid,ordertype,backfeemode,productsource '||
                 'from zxdbm_200.s200_product where serviceid = :p1';
        begin
            execute immediate v_sql
            into   v_productindex,v_needreplay,v_validmode,v_validcyctype,v_invalidmode,
                   v_validcycparam,v_validdate,v_subfeemode,v_subfeedays,v_freedays,v_freenotify,
                   v_popustart,v_popustop,v_popuitem,v_needcharge,
                   v_productname,v_productstatus,v_onlyforcorp,v_productstarttime,v_productendtime,
                   v_workflow,v_workflowlife,v_cpindex,v_serviceindex,v_smschargingcn,v_canbepresent,
                   v_servicetype,v_subcapability,v_onlyforpkg,v_productid,v_ordertype,v_backfeemode,v_productsource
            using v_srvprodid;
        exception when no_data_found then
            o_retcode   := 2100;
            o_debugpos := 40;
            o_debuginfo := 'The product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 41;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
else
    if i_servicetype = 6 then  --400用户的表结构不同,具有testuser、freeuseflag字段
        v_sql := 'select productindex,needreplay,validmode,validcyctype,invalidmode,'||
                 'validcycparam,validdate,subfeemode,subfeedays,freedays,freeuseflag,freenotify,'||
                 'popularizestart,popularizestop,popularizeitem,needcharge,'||
                 'productname,status,onlyforcorp,starttime,endtime,'||
                 'workflow,workflowlife,cpindex,serviceindex,smschargingcn,canbepresent,'||
                 'servicetype,subcapability,productid,ordertype,backfeemode,onlyforpkg,testuser,productsource '||
                 'from zxdbm_200.s200_product where productid = :p1';
        begin
            execute immediate v_sql
            into   v_productindex,v_needreplay,v_validmode,v_validcyctype,v_invalidmode,
                   v_validcycparam,v_validdate,v_subfeemode,v_subfeedays,v_freedays,v_freeuseflag,v_freenotify,
                   v_popustart,v_popustop,v_popuitem,v_needcharge,
                   v_productname,v_productstatus,v_onlyforcorp,v_productstarttime,v_productendtime,
                   v_workflow,v_workflowlife,v_cpindex,v_serviceindex,v_smschargingcn,v_canbepresent,
                   v_servicetype,v_subcapability,v_productid,v_ordertype,v_backfeemode,v_onlyforpkg,v_testuser,v_productsource
            using v_srvprodid;
        exception when no_data_found then
            o_retcode   := 2100;
            o_debugpos := 42;
            o_debuginfo := 'The product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 43;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    --modied by youqunxia20130530   order2.07.10
    elsif i_servicetype in (1,2,3,5,33,98) then  --200、300、500、600、1900、9800用户的表结构具有freeuseflag字段
        v_sql := 'select productindex,needreplay,validmode,validcyctype,invalidmode,'||
                 'validcycparam,validdate,subfeemode,subfeedays,freedays,freeuseflag,freenotify,'||
                 'popularizestart,popularizestop,popularizeitem,needcharge,'||
                 'productname,status,onlyforcorp,starttime,endtime,'||
                 'workflow,workflowlife,cpindex,serviceindex,smschargingcn,canbepresent,'||
                 'servicetype,subcapability,productid,ordertype,backfeemode,onlyforpkg,testuser,productsource '||
                 'from zxdbm_200.s200_product where productid = :p1';
        begin
            execute immediate v_sql
            into   v_productindex,v_needreplay,v_validmode,v_validcyctype,v_invalidmode,
                   v_validcycparam,v_validdate,v_subfeemode,v_subfeedays,v_freedays,v_freeuseflag,v_freenotify,
                   v_popustart,v_popustop,v_popuitem,v_needcharge,
                   v_productname,v_productstatus,v_onlyforcorp,v_productstarttime,v_productendtime,
                   v_workflow,v_workflowlife,v_cpindex,v_serviceindex,v_smschargingcn,v_canbepresent,
                   v_servicetype,v_subcapability,v_productid,v_ordertype,v_backfeemode,v_onlyforpkg,v_testuser,v_productsource
            using v_srvprodid;
        exception when no_data_found then
            o_retcode   := 2100;
            o_debugpos := 44;
            o_debuginfo := 'The product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 45;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    else
        v_sql := 'select productindex,needreplay,validmode,validcyctype,invalidmode,'||
                 'validcycparam,validdate,subfeemode,subfeedays,freedays,freenotify,'||
                 'popularizestart,popularizestop,popularizeitem,needcharge,'||
                 'productname,status,onlyforcorp,starttime,endtime,'||
                 'workflow,workflowlife,cpindex,serviceindex,smschargingcn,canbepresent,'||
                 'servicetype,subcapability,productid,ordertype,backfeemode,onlyforpkg,productsource '||
                 'from zxdbm_200.s200_product where productid = :p1';
        begin
            execute immediate v_sql
            into   v_productindex,v_needreplay,v_validmode,v_validcyctype,v_invalidmode,
                   v_validcycparam,v_validdate,v_subfeemode,v_subfeedays,v_freedays,v_freenotify,
                   v_popustart,v_popustop,v_popuitem,v_needcharge,
                   v_productname,v_productstatus,v_onlyforcorp,v_productstarttime,v_productendtime,
                   v_workflow,v_workflowlife,v_cpindex,v_serviceindex,v_smschargingcn,v_canbepresent,
                   v_servicetype,v_subcapability,v_productid,v_ordertype,v_backfeemode,v_onlyforpkg,v_productsource
            using v_srvprodid;
        exception when no_data_found then
            o_retcode   := 2100;
            o_debugpos := 46;
            o_debuginfo := 'The product does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 47;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
end if;
o_productid      := v_productid;
o_productname    := v_productname;
o_productindex   := v_productindex;
o_serviceindex   := v_serviceindex;
o_cpindex        := v_cpindex;
o_smschargingcn  := v_smschargingcn;
o_servicetype    := v_servicetype;
o_subcapability  := v_subcapability;
o_ordertype      := v_ordertype;
o_popustop       := v_popustop;
o_freedays       := v_freedays;

--added by youqunxia20130531 设置免费期短信提示语 order2.07.10

if i_servicetype in (1,2,3,5,6,33,98) and v_freeuseflag <> 0 then
    if v_freeuseflag = 1 then
         v_freenotitype := 1;
         --sms,mms,wap,mvs,dls,pim,brew业务订购,将免费期到月底转换为免费时长(小时),以方便后续的判断
         v_freedays := floor((to_date(v_curmonthendtime,'yyyymmddhh24miss')-to_date(v_sysdate14,'yyyymmddhh24miss'))*24);
    elsif v_freeuseflag = 2 then
         v_freenotitype := 2;
    elsif v_freeuseflag = 3 then
         v_freenotitype := 3;
    elsif v_freeuseflag = 4 then
         v_freenotitype := 0;
    end if;
end if;
o_freenotitype := v_freenotitype;

--订购非订购类产品鉴权
if v_ordertype <> 1 then
    if i_cmdchannel in (9,35,39,40) then
         o_retcode   := 1001227;
         o_debugpos := 48;
         o_debuginfo := 'v_ordertype='||v_ordertype||', the product can not be subscriberd.';
         return;
    else
         o_retcode   := 2100;
         o_debugpos := 49;
         o_debuginfo := 'v_ordertype='||v_ordertype||', the product can not be subscriberd.';
         return;
    end if;
end if;

--产品状态鉴权
if (((v_productsource <> 3) and (v_productstatus in (0,2)) and (v_workflowlife = 3)) or ((v_productsource = 3) and (v_productstatus = 0))) then
    o_producttype := 2;
elsif (v_productstatus <> 1) then
    o_retcode   := 2101;
    o_debugpos := 50;
    o_debuginfo := 'productid='||v_srvprodid||', The Product status is abnormal.';
    return;
end if;

--产品生效终止时间判断
if (v_sysdate14 < v_productstarttime) or (v_sysdate14 > v_productendtime) then
    o_retcode   := 1011405;
    o_debugpos := 51;
    o_debuginfo := 'product is not valid any more.';
    return;
end if;

--获取产品的计费信息
if (v_popuitem = 2) then  --无推广期
    v_tempitem := 0;
elsif v_popuitem in (0,1) then  --推广期免费或者推广期计费
    if (v_sysdate8 >= v_popustart) and (v_sysdate8 <= v_popustop) then
        if v_popuitem = 0 then  --推广期免费
            v_needcharge := 0;
            v_popuexist := 0;
        else
            v_needcharge := 1;
            v_popuexist  := 1;
            v_tempitem   := v_popuitem;
        end if;
    else
        v_tempitem := 0;
    end if;
end if;
o_needcharge := v_needcharge;

if (v_needcharge = 1) then
    --欠费用户只能订购免费产品

-- if (o_mutiluserexit = 1) and (v_srcuserstatus = 2) then

    if v_srcuserstatus = 2 then
        o_retcode   := 1003;
        o_debugpos := 52;
        o_debuginfo := 'The user status is 2, do not allow to subscribe the product.';
        return;
    end if;
    --费率存在性鉴权
    if i_servicetype in (1,2) then
        v_sql := 'select chargetype,subchargetype,cyctype,cycunit,cycparam,rentfee,fixfee,fixusetimes,autocontinue,minlimitfee,usecomfee,nextrentfee,rentfeevalidate,totaldiscount,description '||
            'from   zxdbm_200.s200_product_fee where  productindex = :p1 and itemindex = :p2';
        begin
            execute immediate v_sql into v_chargetype,v_subchargetype,v_cyctype,v_cycunit,v_cycparam,v_rentfee,v_fixfee,v_fixusetimes,v_autocontinue,v_minlimitfee,v_usecomfee,v_nextrentfee,v_rentfeevalidate,v_totaldiscount,v_description using v_productindex, v_tempitem;
        exception when no_data_found then
            o_retcode   := 2100;
            o_debugpos := 53;
            o_debuginfo := 'The product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 54;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    else
        begin
            select chargetype,subchargetype,cyctype,cycunit,cycparam,rentfee,fixfee,fixusetimes,
                   autocontinue,minlimitfee,usecomfee,totaldiscount,description
            into   v_chargetype,v_subchargetype,v_cyctype,v_cycunit,v_cycparam,v_rentfee,v_fixfee,v_fixusetimes,
                   v_autocontinue,v_minlimitfee,v_usecomfee,v_totaldiscount,v_description
            from   zxdbm_200.s200_product_fee
            where  productindex = v_productindex and itemindex = v_tempitem;
        exception when no_data_found then
            o_retcode   := 3103;
            o_debugpos := 55;
            o_debuginfo := 'The product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 56;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    o_chargetype   := v_chargetype;
    o_cyctype      := v_cyctype;
    o_fixusetimes  := v_fixusetimes;
    o_autocontinue := v_autocontinue;
    o_rentfee      := v_rentfee;
end if;

--推广期免费或者推广期计费,需要查询基本计费的费率信息
if v_popuexist in (0,1) then
    o_smschargingcn := v_description;
    begin
        select chargetype,cyctype,cycunit,cycparam
        into   v_jbchargetype,v_jbcyctype,v_jbcycunit,v_jbcycparam
        from   zxdbm_200.s200_product_fee
        where  productindex = v_productindex and itemindex = 0;
    exception when no_data_found then
        o_retcode   := 3103;
        o_debugpos := 57;
        o_debuginfo := 'The product fee does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 58;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;
--0资费判断,needcharge
if v_needcharge = 0 then
    o_cnfmtype := 1;
else
   if (v_rentfee = 0 and v_fixfee = 0 and v_minlimitfee = 0) then
       v_totaldctemplate := v_totaldiscount;
       loop
           v_index0 := instr(v_totaldctemplate,'&',1,1);
           v_dealtemplate := v_totaldctemplate;
           if v_index0 <> 0 then
               v_totaldclength := length(v_totaldctemplate);
               v_dealtemplate := substr(v_totaldctemplate, 1, v_index0-1);
               v_totaldctemplate := substr(v_totaldctemplate, 1+v_index0, v_totaldclength-v_index0);
           end if;
           v_totaldclength := length(v_dealtemplate);
           v_index1 := instr(v_dealtemplate,';',1,1);
           v_dealtemplate := substr(v_dealtemplate, 1+v_index1, v_totaldclength-v_index1);
           v_totaldclength := length(v_dealtemplate);
           v_index1 := instr(v_dealtemplate,';',1,1);
           v_dealtemplate := substr(v_dealtemplate, 1, v_index1-1);
           v_totaldcsum := to_number(v_dealtemplate);
           exit when (v_totaldcsum > 0 or v_index0 = 0);
       end loop;
       if v_totaldcsum = 0 then
           o_cnfmtype := 1;
       end if;
   end if;
end if;

--产品订购/赠送个性化鉴权
if (v_onlyforpkg = 1) then
    o_retcode   := 2201;
    o_debugpos := 59;
    o_debuginfo := 'The product can only be used by pkg.';
    return;
end if;
--只有非帐期包量/包月/包周期产品和免费产品才可以赠送
if (i_isthird = 1) then
    if (v_canbepresent = 0) then
         o_retcode   := 1001212;
         o_debugpos := 60;
         o_debuginfo := 'The product can not be presented.';
    else
        if (v_needcharge = 1) then
            if (v_chargetype not in (2,3,4,7,8)) or (v_cyctype <> 0) then
                o_retcode   := 1001217;
                o_debugpos := 61;
                o_debuginfo := 'v_chargetype='||v_chargetype||'v_cyctype='||v_cyctype||', The product can not be presented.';
            end if;
        end if;
    end if;
end if;
--赠送失败的短信模板需要下面两个参数
if o_retcode in (1001212,1001217)then
    begin
        select cpcnshortname,customersrvtel into v_cpcnshortname,v_customersrvtel
        from   zxdbm_ismp.scp_basic where cpindex = v_cpindex;
    exception when no_data_found then
        o_retcode   := 2000;
        o_debugpos := 62;
        o_debuginfo := 'v_cpindex='||v_cpindex||', the sp does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 63;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    o_cpcnshortname := v_cpcnshortname;
    o_customersrvtel := v_customersrvtel;
    return;
end if;
--订购指令查询  add by chenfuzhong 2008.12.24
begin
    select featurecode,accesscode
    into   o_cancelfeaturecode,o_cancelaccesscode
    from   zxdbm_ismp.ssrv_srvorder
    where  productid = v_productid and cmdtype = 2 and rownum = 1;
exception when no_data_found then
    null;
when others then
    o_retcode   := 506;
    o_debugpos := 64;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;

/**
--part-- --订购-- 业务鉴权
*/

--业务存在性鉴权(MPSS不需要鉴权)
--modified by chenfuzhong 2008.10.09
if i_servicetype = 101 then
    v_sql := 'select serviceid,status,colorlist,servicename,maincapability,submaincapability,cpid,orderupdateurl '||
             'from zxdbm_9900.s9900_service where serviceindex = :p1';
    begin
        execute immediate v_sql into v_serviceid,v_servicestatus,v_srvcolorlist,v_servicename,v_maincapability,v_submaincapability,v_cpid,v_orderupdateurl using v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 65;
        o_debuginfo := 'serviceindex='||v_serviceindex||', The service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 66;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
elsif i_servicetype in (1,2,3) then
    v_sql := 'select serviceid,status,colorlist,servicename,subsrvtype,cpid,isservicesub,orderupdateurl,servicesort '||
             'from zxdbm_200.s200_service where  serviceindex = :p1';
    begin
        execute immediate v_sql into v_serviceid,v_servicestatus,v_srvcolorlist,v_servicename,v_subsrvtype,
        v_cpid,v_isservicesub,v_orderupdateurl,v_servicesort
        using v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 67;
        o_debuginfo := 'serviceindex='||v_serviceindex||', The service does not exist';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 68;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
elsif i_servicetype <> 14 then
    begin
        select serviceid,status,colorlist,servicename,subsrvtype,cpid,orderupdateurl
        into   v_serviceid,v_servicestatus,v_srvcolorlist,v_servicename,v_subsrvtype,v_cpid,v_orderupdateurl
        from   zxdbm_200.s200_service
        where  serviceindex = v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 69;
        o_debuginfo := 'serviceindex='||v_serviceindex||', The service does not exist';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 70;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

o_serviceid         := v_serviceid;
o_servicename       := v_servicename;
o_maincapability    := v_maincapability;
o_submaincapability := v_submaincapability;
o_cpid              := v_cpid;

--业务状态鉴权,mpss无业务
if (i_servicetype <> 14 and v_servicestatus <> 1) then
    o_retcode   := 1011510;
    o_debugpos := 71;
    o_debuginfo := 'v_servicestatus='||v_servicestatus||', The service status is abnormal.';
    return;
end if;

--added by youqunxia20130906 order3.02.01
--是否同步订购关系给OIDD,0-否,1-是
if v_servicesort = 1 then
    o_issynctooidd := 1;
end if;

/**
--part-- --订购-- CP/SP鉴权
**/

--CP/SP存在性鉴权
begin
    select cpid,status,cpsubscribeurl,hasblacklist,cptype,insubblacklist,cpcnshortname,customersrvtel,cpsrvtype
    into   v_cpid,v_cpstatus,v_cpsubscribeurl,v_hasblacklist,v_cptype,v_insubblacklist,v_cpcnshortname,v_customersrvtel,v_cpsrvtype
    from   zxdbm_ismp.scp_basic
    where  cpindex = v_cpindex;
exception when no_data_found then
    o_retcode   := 2000;
    o_debugpos := 72;
    o_debuginfo := 'v_cpindex='||v_cpindex||', the sp does not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 73;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;

o_cpid           := v_cpid;
o_cptype         := v_cptype;
o_cpcnshortname  := v_cpcnshortname;
o_customersrvtel := v_customersrvtel;
o_cpsrvtype      := v_cpsrvtype;

--按优先统提取同步URL
if (trim(v_orderupdateurl) is null) or (length(trim(v_orderupdateurl)) < 8) then
    o_cpsubscribeurl := v_cpsubscribeurl;
else
    o_cpsubscribeurl := v_orderupdateurl;
end if;

--CP/SP状态鉴权
if (v_cpstatus <> 1) then
    o_retcode   := 2001;
    o_debugpos := 74;
    o_debuginfo := 'v_cpstatus='||v_cpstatus||', The cp/sp status is abnormal.';
    return;
end if;
--限制用户订购CP/SP黑名单鉴权
if (v_insubblacklist <> 0) then
    o_retcode   := 1011406;
    o_debugpos := 75;
    o_debuginfo := 'insubblacklist='||v_insubblacklist||', cp/sp is in limit blacklist';
    return;
end if;

--SP接入码查询
if (i_spaccesscode is null) or (length(trim(i_spaccesscode)) = 0) then
    begin
        select spaccesscode into v_spaccesscode
        from zxdbm_ismp.ssrv_srvorder
        where productindex = v_productindex and cmdtype = i_actiontype and servicetype = i_servicetype and rownum = 1;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 76;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if trim(v_spaccesscode) is null then
        begin
            select accesscode into v_spaccesscode
            from zxdbm_ismp.ssrv_spaccesscode
            where cpid = v_cpid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 77;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    o_spaccesscode := v_spaccesscode;
end if;

/*
--part-- --订购-- 业务能力开通鉴权
*/

--SP开通业务能力鉴权(MVS,MPSS,PIM,CPX不需要鉴权)
if i_servicetype  in (5,14,33,101) then
    o_protocoltype := 0;
else
    begin
        select protocoltype
        into   v_protocoltype
        from   zxdbm_200.s200_sp_service_type
        where  cpindex = v_cpindex and servicetype = v_servicetype and subcapability = v_subcapability;
    exception when no_data_found then
       o_retcode   := 2100;
       o_debugpos := 78;
       o_debuginfo := 'cp/sp serviceCapability is not open.';
       return;
    when others then
       o_retcode   := 506;
       o_debugpos := 79;
       o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
       return;
    end;
    o_protocoltype := v_protocoltype;
end if;

--added by youqunxia 20130817 Order3.02.01  鉴权是否开通webgw业务能力
begin
    select status, starttime, endtime
    into v_spsrvstatus,v_spsrvstarttime,v_spsrvendtime
    from zxdbm_5000.s5000_sp_service_type
    where cpindex = v_cpindex and servicetype = 58;
exception when no_data_found then
    o_haswebgw := 0;
when others then
    o_retcode   := 506;
    o_debugpos := 179;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
if v_spsrvstatus not in (1,3) then
    o_haswebgw := 0;
elsif v_spsrvstarttime > v_sysdate14 or v_spsrvendtime < v_sysdate14 then
    o_haswebgw := 0;
end if;

--系统业务能力开通鉴权(CPX查找主,子业务能力)
if i_servicetype = 101 then
    begin
        select a.perssrvopen,b.srvtypeid
        into   v_perssrvopen,v_srvtypeid
        from   zxdbm_ismp.ssys_service_type a,zxdbm_ismp.ssys_service_type b
        where  a.servicetype = v_servicetype and a.subcapability = v_subcapability
        and    b.servicetype = v_maincapability and b.subcapability = v_submaincapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 80;
        o_debuginfo := 'CPX system serviceCapability is not open.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 81;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
else
    begin
        select perssrvopen,srvtypeid
        into   v_perssrvopen,v_srvtypeid
        from   zxdbm_ismp.ssys_service_type
        where  servicetype = v_servicetype and subcapability = v_subcapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 82;
        o_debuginfo := 'servicetype='||v_servicetype||'subcapability='||v_subcapability||', System serviceCapability is not open.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 83;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;
o_srvtypeid   :=  v_srvtypeid;

/**
--part-- --订购-- 名单鉴权,用户业务能力开通鉴权
**/
-- if (o_mutiluserexit = 1) and (i_needauthuser = 1) then

if i_needauthuser = 1 then
    --使用方名单鉴权
    if (v_useblacklist <> 0) then
        if bitand(v_useblacklevel,1) > 0 then
            --系统级黑名单检查
            v_tmpint    := 0;
            begin
                select count(*)
                into   v_tmpint
                from   zxdbm_ismp.susr_colorlist
                where  userindex = v_useuserindex and colortype = 1 and colorlevel = 1
                and inuretime <= v_sysdate14 and abatetime >= v_sysdate14;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 84;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if (v_tmpint > 0) then
                o_retcode   := 1004;
                o_debugpos := 85;
                o_colortype := 1;
                o_debuginfo := 'colortype = 1 and colorlevel = 1 and userindex = '||v_useuserindex||', The user is in system level blacklist.';
                return;
            end if;
        end if;
        --SP级黑名单检查
        if v_hasblacklist = 1 and bitand(v_useblacklevel,2) > 0 then
            v_tmpint := 0;
            begin
                select count(*)
                into   v_tmpint
                from   zxdbm_ismp.susr_colorlist
                where  userindex = v_useuserindex and colortype = 1 and colorlevel = 2 and colorid = v_cpindex
                and inuretime <= v_sysdate14 and abatetime >= v_sysdate14;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 86;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if v_tmpint > 0 then
                o_retcode   := 1004;
                o_debugpos := 87;
                o_colortype := 1;
                o_debuginfo := 'colortype = 1 and colorlevel = 2 and colorid = '||v_cpindex||', The user is in SP level blacklist.';
                return;
            end if;
        end if;
        --业务能力级黑名单
        if bitand(v_useblacklevel,4) > 0 then
            v_tmpint := 0;
            begin
                select count(*)
                into   v_tmpint
                from   zxdbm_ismp.susr_colorlist
                where  userindex = v_useuserindex and colortype = 1 and colorlevel = 3 and colorid = v_servicetype;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 88;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if v_tmpint > 0 then
                if v_usegraylist > 0 and bitand(v_usegraylevel,2) > 0 then
                    begin
                        select count(*)
                        into   v_tmpint
                        from   zxdbm_ismp.susr_colorlist
                        where  userindex = v_useuserindex and colortype = 3 and colorlevel = 2 and colorid = v_cpindex;
                    exception when others then
                        o_retcode   := 506;
                        o_debugpos := 89;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        return;
                    end;
                    if (v_tmpint = 0) then
                        o_retcode   := 1004;
                        o_debugpos := 90;
                        o_colortype := 1;
                        o_debuginfo := 'colortype = 1 and colorlevel = 3 and colorid = '||v_servicetype||', The user is in serviceCapbility level blacklist.';
                        return;
                    end if;
                else
                    o_retcode   := 1004;
                    o_debugpos := 91;
                    o_debuginfo := 'colortype = 1 and colorlevel = 3 and colorid = '||v_servicetype||', The user is in serviceCapbility level blacklist.';
                    o_colortype := 1;
                    return;
                end if;
            end if;
        end if;
        if v_srvcolorlist = 1 and bitand(v_useblacklevel,8) > 0 then
            --业务级黑名单检查
            v_tmpint := 0;
            begin
                select count(*)
                into   v_tmpint
                from    zxdbm_ismp.susr_colorlist
                where   userindex = v_useuserindex and colortype = 1 and colorlevel = 4 and colorid = v_serviceindex;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 92;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if v_tmpint > 0 then
                o_retcode   := 1004;
                o_debugpos := 93;
                o_debuginfo := 'colortype = 1 and colorlevel = 4 and colorid = '||v_serviceindex||', The user is in service level blacklist.';
                o_colortype := 1;
                return;
            end if;
        end if;
    end if;
    if (v_srvcolorlist = 2) then
        if (v_usewhitelist = 0) then
            o_retcode   := 1100001;
            o_debugpos := 94;
            o_debuginfo := 'v_srvcolorlist = '||v_srvcolorlist||'v_usewhitelist = '||v_usewhitelist||', service has whitelist,but user is not in whitelist.';
            return;
        end if;
        o_colortype := 2;
        v_tmpint := 0;
        begin
            select count(*)
            into   v_tmpint
            from   zxdbm_ismp.susr_colorlist
            where  userindex = v_useuserindex and colortype = 2 and colorlevel = 4 and colorid = v_serviceindex;
        exception when others then
            o_retcode   := 506;
            o_debugpos := 95;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        if v_tmpint = 0 then
            o_retcode   := 1109;
            o_debugpos := 96;
            o_debuginfo := 'userindex = '||v_useuserindex||', colortype = 2 and colorlevel = 4, colorid = '||v_serviceindex||',service has whitelist, but the user is not in service level whitelist.';
            return;
        end if;
    end if;
    if (v_useredlist <> 0) then
        o_colortype := 4;
    end if;
    --使用方开通业务能力鉴权
    begin
      --  select nvl(status,1)
        select status
        into   v_useopenstatus
        from   zxdbm_200.s200_service_useropen
        where  userindex = v_useuserindex and servicetype = v_servicetype;
    exception when no_data_found then
        v_useopenstatus := 1;
        begin
    --        select nvl(perssrvopen,1) into v_usetypesrvopen
            select perssrvopen into v_usetypesrvopen
            from zxdbm_ismp.ssrv_user_perssrvopen
            where servicetype = v_servicetype and subcapability = v_subcapability and msisdntype =v_usemsisdntype;
        exception when no_data_found then
            v_usetypesrvopen  := 1;
            if v_perssrvopen = 0 then
                o_retcode  := 1100;
                o_debugpos    := 97;
                o_debuginfo   := 'the user service capabilty is closed.';
                return;
            end if;
        when others then
            o_retcode  := 506;
            o_debugpos    := 98;
            o_debuginfo   := 'the user service capabilty is closed.';
            return;
        end;
    when others then
        o_retcode   := 506;
        o_debugpos := 99;
        return;
    end;
    if v_useopenstatus = 0 or v_usetypesrvopen = 0 then
        if i_cmdchannel in (9,35,39,40) then
            o_retcode   := 1001225;
        else
            o_retcode   := 1100;
        end if;
        o_debugpos := 100;
        o_debuginfo := 'v_useopenstatus = '||v_useopenstatus||', the user service capabilty is closed.';
        return;
    end if;
    if (v_srcmsisdn <> v_usemsisdn) then
    --发起方名单鉴权
        if (v_srcblacklist <> 0) then
            if bitand(v_srcblacklevel,1) > 0 then
                --系统级黑名单检查
                v_tmpint    := 0;
                begin
                    select count(*)
                    into   v_tmpint
                    from   zxdbm_ismp.susr_colorlist
                    where  userindex = v_useuserindex and colortype = 1 and colorlevel = 1
                    and inuretime <= v_sysdate14 and abatetime >= v_sysdate14;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 101;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
                if (v_tmpint > 0) then
                    o_retcode   := 1004;
                    o_debugpos := 102;
                    o_debuginfo := 'userindex = '||v_useuserindex||', colortype = 1 and colorlevel = 1'||', The user is in system level blacklist.';
                    return;
                end if;
            end if;
            --SP级黑名单检查
            if v_hasblacklist = 1 and bitand(v_srcblacklevel,2) > 0 then
                v_tmpint := 0;
                begin
                    select count(*)
                    into   v_tmpint
                    from   zxdbm_ismp.susr_colorlist
                    where  userindex = v_srcuserindex and colortype = 1 and colorlevel = 2 and colorid = v_cpindex
                    and inuretime <= v_sysdate14 and abatetime >= v_sysdate14;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 103;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
                if v_tmpint > 0 then
                    o_retcode   := 1004;
                    o_debugpos := 104;
                    o_debuginfo := 'userindex = '||v_srcuserindex||', colortype = 1 and colorlevel = 2'||', colorid = '||v_cpindex||', the user is in SP level blacklist.';
                    return;
                end if;
            end if;
            --业务能力级黑名单
            if bitand(v_srcblacklevel,4) > 0 then
                v_tmpint := 0;
                begin
                    select count(*)
                    into   v_tmpint
                    from   zxdbm_ismp.susr_colorlist
                    where  userindex = v_srcuserindex and colortype = 1 and colorlevel = 3 and colorid = v_servicetype;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 105;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
                if v_tmpint > 0 then
                    if v_srcgraylist > 0 and bitand(v_srcgraylevel,2) > 0 then
                        begin
                            select count(*)
                            into   v_tmpint
                            from   zxdbm_ismp.susr_colorlist
                            where  userindex = v_srcuserindex and colortype = 3 and colorlevel = 2 and colorid = v_cpindex;
                        exception when others then
                            o_retcode   := 506;
                            o_debugpos := 106;
                            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                            return;
                        end;
                        if (v_tmpint = 0) then
                            o_retcode   := 1004;
                            o_debugpos := 107;
                            o_debuginfo := 'userindex = '||v_srcuserindex||', colortype = 1 and colorlevel = 3'||', colorid = '||v_servicetype||', the user is in service capbility level blacklist.';
                            o_colortype := 1;
                            return;
                        end if;
                    else
                        o_retcode   := 1004;
                        o_debugpos := 108;
                        o_debuginfo := 'userindex = '||v_srcuserindex||', colortype = 1 and colorlevel = 3'||', colorid = '||v_servicetype||', The user is in service capbility level blacklist.';
                        o_colortype := 1;
                        return;
                    end if;
                end if;
            end if;
            if v_srvcolorlist = 1 and bitand(v_srcblacklevel,8) > 0 then
                --业务级黑名单检查
                v_tmpint := 0;
                begin
                    select count(*)
                    into   v_tmpint
                    from    zxdbm_ismp.susr_colorlist
                    where   userindex = v_srcuserindex and colortype = 1 and colorlevel = 4 and colorid = v_serviceindex;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 109;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
                if v_tmpint > 0 then
                    o_retcode   := 1004;
                    o_debugpos := 110;
                    o_debuginfo := 'userindex = '||v_srcuserindex||', colortype = 1 and colorlevel = 4'||', colorid = '||v_serviceindex||', the user is in service level blacklist.';
                    return;
                end if;
            end if;
        end if;
        if (v_srvcolorlist = 2) then
            if (v_srcwhitelist = 0) then
                o_retcode   := 1100001;
                o_debugpos := 111;
                o_debuginfo := 'v_srvcolorlist = '||v_srvcolorlist||', v_srcwhitelist = '||v_srcwhitelist||', the service has whitelist, but the user is not in whitelist.';
                return;
            end if;
            v_tmpint := 0;
            begin
                select count(*)
                into   v_tmpint
                from   zxdbm_ismp.susr_colorlist
                where  userindex = v_srcuserindex and colortype = 2 and colorlevel = 4 and colorid = v_serviceindex;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 112;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if v_tmpint = 0 then
                o_retcode   := 1109;
                o_debugpos := 113;
                o_debuginfo := 'v_srvcolorlist = '||v_srvcolorlist||', the service has whitelist, but the user is not in service level whitelist.';
                return;
            end if;
        end if;
        --发起方开通业务能力鉴权
        begin

-- select nvl(status,1)

            select status
            into   v_srcopenstatus
            from   zxdbm_200.s200_service_useropen
            where  userindex = v_srcuserindex and servicetype = v_servicetype;
        exception when no_data_found then
            v_srcopenstatus := 1;
            begin
    --            select nvl(perssrvopen,1) into v_srctypesrvopen
                select perssrvopen into v_srctypesrvopen
                from zxdbm_ismp.ssrv_user_perssrvopen
                where servicetype = v_servicetype and subcapability = v_subcapability and msisdntype =v_srcmsisdntype;
            exception when no_data_found then
                v_srctypesrvopen := 1;
                if v_perssrvopen = 0 then
                    o_retcode  := 1100;
                    o_debugpos    := 114;
                    o_debuginfo   := 'The user service capability is not open and system capability is closed';
                    return;
                end if;
            when others then
                o_retcode  := 506;
                o_debugpos    := 115;
                o_debuginfo   := 'Failed to query user ssrv_user_perssrvopen';
                return;
            end;
        when others then
            o_retcode   := 506;
            o_debugpos := 116;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        if v_srcopenstatus = 0 or v_srctypesrvopen = 0 then
            if i_cmdchannel in (9,35,39,40) then
                o_retcode  := 1001225;
            else
                o_retcode  := 1100;
            end if;
            o_debugpos := 117;
            o_debuginfo := 'userindex = '||v_srcuserindex||'servicetype = '||v_servicetype||', the user service capabilty is closed.';
            return;
        end if;
    end if;
end if;

/
--part-- --订购-- 测试产品,测试用户鉴权
*/

--测试产品鉴权

-- if (o_mutiluserexit = 1) and (((v_productsource <> 3) and (v_productstatus in (0,2)) and (v_workflowlife = 3)) or

if (v_productsource <> 3 and v_productstatus in (0,2) and v_workflowlife = 3) or (v_productsource = 3 and v_productstatus = 0) then
    if (v_srcusertype <> 2) or (v_useusertype <> 2) then
        o_retcode   := 1100;
        o_debugpos := 118;
        o_debuginfo := 'The normal user can not use test product.';
        return;
    end if;
    --SP测试用户鉴权
    --测试产品、测试用户(发起方、使用方)相关鉴权
    begin
        select cpindex
        into   v_srctestcpindex
        from   zxdbm_ismp.susr_testgroup_member
        where  userindex = v_srcuserindex;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 119;
        o_debuginfo := 'userindex = '||v_srcuserindex||', the test product is not open to the subscriber.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 120;
        return;
    end;
    if (v_srctestcpindex > 0) then
        if (v_srctestcpindex <> v_cpindex) then
            o_retcode   := 1001223;
            o_debugpos := 121;
            o_debuginfo := 'The test-user belongs to SP-A can not use test-product belonged to SP-B.';
            return;
        end if;
    end if;
    begin
        select cpindex
        into   v_usetestcpindex
        from   zxdbm_ismp.susr_testgroup_member
        where  userindex = v_useuserindex;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 122;
        o_debuginfo := 'userindex = '||v_useuserindex||', the test product is not open to the subscriber.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 123;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if (v_usetestcpindex > 0) then
        if (v_usetestcpindex <> v_cpindex) then
            o_retcode   := 1001223;
            o_debugpos := 124;
            o_debuginfo := 'The test-user belongs to SP-A can not use test-product belonged to SP-B.';
            return;
        end if;
        o_usertype := 3;
    end if;
end if;

/*
--part-- --订购-- 集团相关鉴权(分DB用户不存在,不鉴权)
/

--集团产品、集团用户相关鉴权

-- if (o_mutiluserexit = 1) and (v_onlyforcorp = 1) then

if v_onlyforcorp = 1 then
    --集团产品不允许赠送
    if (i_isthird = 1) then
        o_retcode    := 1011407;
        o_debugpos  := 125;
        o_debuginfo := 'v_onlyforcorp = '||v_onlyforcorp||'i_isthird = '||i_isthird||', the corp product can not to be presented.';
        return;
    end if;
    --普通用户不能订购一个集团专用产品
    if (v_srccorpindex <> v_usecorpindex) then
        o_retcode   := 1100;
        o_debugpos := 126;
        o_debuginfo := 'v_onlyforcorp = '||v_onlyforcorp||'v_srccorpindex = '||v_srccorpindex||', the normal user can not subscrib corp product.';
        return;
    end if;
    if (v_srccorpindex = 0) then
        o_retcode   := 1204;
        o_debugpos := 127;
        o_debuginfo := 'v_onlyforcorp = '||v_onlyforcorp||'v_srccorpindex = '||v_srccorpindex||', the normal user can not subscrib corp product.';
        return;
    end if;
    begin
        select  corpid,status,paymsisdn,corpcnshortname
        into    v_srccorpid,v_srccorpstatus,v_srccorppaymsisdn,v_corpname
        from    zxdbm_ismp.susr_corp
        where   corpindex = v_srccorpindex;
    exception when no_data_found then
        o_retcode   := 1001301;  --集团不存在
        o_debugpos := 128;
        o_debuginfo := 'v_srccorpindex = '||v_srccorpindex||', the corp do not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 129;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;

    o_corpcnshortname  := v_corpname;
    o_corpid           := v_srccorpid;
    o_corpindex        := v_srccorpindex;

    if (v_srccorpstatus <> 1) then
        o_retcode   := 1001302;
        o_debugpos := 130;
        o_debuginfo := 'v_srccorpindex = '||v_srccorpindex||', the corp status is abnormal.';
        return;
    end if;
    --一个集团的产品只能由本集团的用户订购/赠送
    begin
        select subscribemode,payparty,status
        into   v_productsubmode,v_productpayparty,v_corpproductstatus
        from   zxdbm_ismp.ssrv_corp_product
        where  corpindex = v_srccorpindex and groupindex = 0 and productindex = v_productindex;
    exception when no_data_found then
        o_retcode   := 1011300;
        o_debugpos := 131;
        o_debuginfo := 'productindex = '||v_productindex||', corpindex = '||v_srccorpindex||', the product is not binded with the corp.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 132;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if (v_corpproductstatus <> 0) then
        o_retcode   := 1011408;
        o_debugpos := 133;
        o_debuginfo := 'productindex = '||v_productindex||', corpindex = '||v_srccorpindex||', the binding relationship between the product and the corp is deleted.';
        return;
    end if;
    if (v_productsubmode = 0) then
    --必须集团管理员订购
        if (i_cmdchannel <> 4) then
            o_retcode   := 1001216;    --无权定购
            o_debugpos := 134;
            o_debuginfo := 'i_cmdchannel = '||i_cmdchannel||', v_productsubmode = '||v_productsubmode||', the corp product can only be subscribed by corp managers.';
            return;
        end if;
    end if;
end if;

/*
--part-- --订购-- 计费方、预付费系统鉴权
*/

--if o_mutiluserexit = 1 then
    if (v_productpayparty = 1) then
        v_paymsisdn     := v_srccorpid;
        v_cardtype      := 0;
        v_paymsisdntype := 1;
        o_paymsisdn     := v_paymsisdn;
        o_paytype       := v_cardtype;
        o_payuserindex  := v_srccorpindex;
        o_paymode       := '集团付费';
        o_cityid        := trim(' ');
    else
        v_paymsisdn     := v_srcmsisdn;
        v_cardtype      := v_srcpaytype;
        v_paymsisdntype := v_srcmsisdntype;
        o_paymsisdn     := v_paymsisdn;
        o_paytype       := v_cardtype;
        o_payuserindex  := v_srcuserindex;
        o_paymode       := '个人付费';

        if (i_prepaidtype = 1) then
            o_scptype := 1;
        elsif i_prepaidtype = 2 then
            o_scptype := 2;
        else
            --attach 预付费归属,0-未知,1-OCS,2-RTBP
            begin
                select phoneindex,attach
                into   v_phoneindex,v_attach
                from   zxdbm_ismp.ssys_phone
                where  startprefix <= substr(v_paymsisdn,1,length(startprefix))
                and    endprefix >= substr(v_paymsisdn,1,length(endprefix));
            exception when no_data_found then
                o_scptype  := 2;
                v_devexist := 0; --表示不存在对应号段对应设备配置
            when others then
                o_retcode   := 506;
                o_debugpos := 135;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if (v_devexist = 1) then
                if (v_attach = 1) then
                    o_scptype := 1;
                elsif (v_attach = 2) then
                    o_scptype := 2;
                else
                    begin
                        select 1
                        into   o_scptype
                        from   zxdbm_ismp.sdev_phone
                        where  phoneindex = v_phoneindex
                        and    devicetype = 5;
                    exception when no_data_found then
                        o_scptype := 2;
                    when others then
                        o_retcode   := 506;
                        o_debugpos := 136;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        return;
                    end;
                end if;
             end if;
        end if;
    end if;

-- end if;
/*
--part-- --订购-- 功能:订购关系鉴权(产品订购/业务订购)

  说明:跨省赠送,赠送消息不鉴权订购关系/多DB赠送,不在OA库鉴权订购关系

*/

if (i_mutidbmode = 1 and i_isthird = 1) or (i_actiontype = 17 and i_isfromnmsc = 1) then
    null;
else
    v_hasrecord := 1;
    begin      --产品订购
        select status,productid,productindex,lastrenttime,nextrentdate,payfordate,productpkgindex,endtime,
               varifee,isexperience
        into v_cursubstatus,v_cancelproductid,v_cancelproductindex,v_cancellastrenttime,v_cancelnextrentdate,v_cancelpayfordate,v_productpkgindex,v_subendtime,
             v_lastvarifee,v_isexperience1
        from zxdbm_200.s200_user_subscription
        where usercode = v_usemsisdn and productid = v_productid;
    exception when no_data_found then
        v_hasrecord := 0;
    when others then
        o_retcode   := 506;
        o_debugpos := 137;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if (v_hasrecord > 0) then
        if v_productpkgindex <> 0 then
            begin
                select trim(productpkgname)
                into  o_productpkgname
                from zxdbm_ismp.ssrv_product_pkg
                where productpkgindex = v_productpkgindex;
            exception when no_data_found then
                o_retcode   := 2100;
                o_debugpos := 138;
                o_debuginfo := 'v_productpkgindex = '||v_productpkgindex||', the pkg do not exit.';
                return;
            when others then
                o_retcode   := 506;
                o_debugpos := 139;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            o_retcode   := 1200;   --该产品属于套餐,且用户已订购该套餐
            o_debugpos := 140;
            o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_productid = '||v_productid||', the user has subscribed pkg including this product.';
            return;
        end if;
        if v_cursubstatus <> 4 and v_chargetype <> 4 then    --(待失效订购关系 或 纯包量)可以重复订购
            --added by youqunxia20130818 order3.02.01
            if i_startcmdchannel = 45 then
                o_hassubscription := 1;
                 --6、可变价的重复订购
                o_issecondproduct := 6;
            elsif (i_cmdchannel = 40) then
                --added by youqunxia20131015 order3.02.01
                v_isexperience1 := trim(v_isexperience1);
                if (v_isexperience1 = '1') and (i_isexperience = 3)then
                    v_isexperience := '0';
                    --7:体验转正式的重复订购
                    o_issecondproduct := 7;
                else
                    o_retcode   := 1200;
                    o_debugpos  := 178;
                    o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_productid = '||v_productid||', the user has subscribed the product.';
                    return;
                end if;
            else 
                o_retcode   := 1200;
                o_debugpos := 141;
                o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_productid = '||v_productid||', the user has subscribed the product.';
                return;
            end if;
        elsif v_chargetype = 4 then  --判断顺序不能颠倒
            o_issecondproduct  := 3;
        --modied by youqunxia20130918 order3.02.01 
        --待失效订购关系的订购,不支持退订使用足月的产品待失效产品的订购
        elsif v_cursubstatus = 4 and v_chargetype in (2,5,7) and v_invalidmode <> 2 then
            o_issecondproduct  := 2;
        else
            o_retcode   := 1200;
            o_debugpos  := 181;
            o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_productid = '||v_productid||', the user has subscribed the product.';
            return;
        end if;
    end if;
    --业务订购(内容类业务有BREW、DLS、MVS)
    if i_servicetype not in (5,6,98) and v_hasrecord = 0 then
        o_issecondproduct  := 1;
        v_hasrecord := 1;
        begin
            select productid,productindex,lastrenttime,payfordate,productpkgindex,
                   freetime,isexperience
            into   v_cancelproductid,v_cancelproductindex,v_cancellastrenttime,v_cancelpayfordate,v_productpkgindex,
                   v_cancelfreetime,v_cancelisexperience
            from   zxdbm_200.s200_user_subscription
            where  usercode = v_usemsisdn and cpid = v_cpid and serviceid = v_serviceid;
        exception when no_data_found then
            o_issecondproduct := 0;
            v_hasrecord := 0;
        when others then
            o_retcode   := 506;
            o_debugpos := 142;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        if (i_cmdchannel = 40) then
            --added by youqunxia20131018 order3.02.01
            --订购激活请求中OperType为2:替换已有产品,则ISMP正常处理,将产品的订购关系进行替换,计费正常
            --体验转正式,则ISMP正常处理,将产品订购关系转为正常,计费正常
            v_cancelisexperience := trim(v_cancelisexperience);
            if (v_cancelisexperience = '1') and (i_isexperience = 0 or i_isexperience = 3)then
                v_isexperience := '0';
            end if;
        end if;
        if v_hasrecord > 0 then
            if v_productpkgindex <> 0 then
                o_retcode   := 1001210;                          --不能替换产品组合包含的产品
                o_debugpos := 143;
                o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_productpkgindex = '||v_productpkgindex||', can not replace product in the pkg.';
                return;
            end if;
            if(v_cancelproductid <> v_productid) then
                if i_servicetype in (1,2,3) and v_isservicesub = 1 then       --sms,mms,wap业务订购
                    null;
                else
                    o_retcode   := 1001210;
                    o_debugpos  := 144;
                    o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', serviceid = '||v_serviceid||', the user has subscribed a product in the same service.';
                    return;
                end if;
            end if;
        end if;
    end if;
    o_cancelproductid := v_cancelproductid;
    o_lastvarifee     := v_lastvarifee;
end if;

--业务订购/待失效订购关系只支持包月类产品--??应该在鉴权了订购关系的情况下,才有下面的鉴权,T02改
if o_issecondproduct in (1,2) then
    if i_servicetype = 5 and o_issecondproduct = 2 and v_chargetype in (3,6,8) then
        --MVS的包周期产品在订购关系待失效状态时可以续订
        null;
    else
        if v_chargetype in (2,5,7) or v_needcharge = 0 then
            --包月类产品、免费产品可以替换
            null;
        else
            o_retcode   := 9999;
            o_debugpos := 145;
            o_debuginfo := 'v_chargetype='||v_chargetype||', No_month_Product can not update subscription.';
        end if;
    end if;
end if;
--added by youqunxia2013112 order3.02.01
if v_isexperience = '3' then
    o_retcode   := 1009023;
    o_debugpos  := 180;
    o_debuginfo := 'There is not free user subscription.';
end if;
o_isexperience  := v_isexperience;

/
--part-- --订购-- 设置订购关系生效时间、状态
--0-立即生效,1-下月生效,2-n周期后生效,3-指定日期生效
/

v_substatus    := 0;
--added by youqunxia20130910 order3.02.01
--开关打开,VSOP侧发起捆绑类套餐、增值产品的订购激活、变更激活及退订,ISMP中增值子产品订购关系立即生效(包括订购、退订及变更),不以增值产品属性判断生效时间;
if (i_cmdchannel = 40) and (i_effectiveswitch = 1)then
    v_validmode := 0;
end if;
if (v_validmode = 0) or (v_validmode = 3 and v_validdate <= v_sysdate14) then
    v_substarttime := v_sysdate14;
    v_validmode := 0;
    v_substatus := 1;
elsif (v_validmode = 1) then
    v_substarttime := to_char((to_date(substr(v_curmonthendtime,1,8),'yyyymmdd') + 1),'yyyymmddhh24miss');
    v_substatus    := 0;
elsif (v_validmode = 2) then
--周期方式0-非周期方式,1-天,2-周,3-月
    if (v_validcyctype = 1) then
        v_substarttime := to_char(to_date(v_sysdate14,'yyyymmddhh24miss') + v_validcycparam,'yyyymmddhh24miss');
        v_substatus    := 0;
    elsif (v_validcyctype = 2) then
        v_substarttime := to_char(to_date(v_sysdate14,'yyyymmddhh24miss') + 7 * v_validcycparam,'yyyymmddhh24miss');
        v_substatus    := 0;
    elsif (v_validcyctype = 3) then
        v_substarttime := to_char(add_months(to_date(v_sysdate14,'yyyymmddhh24miss'),v_validcycparam),'yyyymmddhh24miss');
        v_substatus    := 0;
    else
        v_substarttime := v_sysdate14;
        v_substatus    := 1;
    end if;
elsif (v_validmode = 3) and (v_validdate > v_sysdate14) then
    v_substarttime := v_validdate;
    v_substatus    := 0;
else
    v_substarttime := v_sysdate14;
    v_substatus    := 1;
end if;

--modied by youqunxia20130601   order2.07.10 
if i_servicetype = 5 and v_freeuseflag in (1,2) then
    --流媒体产品(天冀视讯业务)用户首次订购享受免费期的时候,需要将订购关系置为立即生效
    v_substarttime := v_sysdate14;
    v_validmode := 0;
    v_substatus := 1;
    --将之到期处理字段值置为0,以对代码进行保护(天翼视讯MVS此字段门户应限制为0)
    v_freenotify := 0;
end if;
--生效时间在推广期之外
if v_popuexist in (0,1) and (to_char(to_date(v_substarttime, 'yyyymmddhh24miss'), 'yyyymmdd') >= v_popustop) then
    v_popuexist := 3;
end if;
o_popuexist := v_popuexist;

/**
--part-- --订购-- --计算产品订购关系免费期、产品订购关系下次扣租日期、产品订购关系上次扣租时间、月租已扣日期

--从本月历史表中查询有没有退定记录

*/

v_hasrecord := 1;
begin
    v_sql := 'select freetime,lastrenttime,payfordate from (select freetime,lastrenttime,payfordate from zxdbm_200.s200_user_subs_hist' || v_curstrmonth ||
             ' where usercode = :p1 and productid = :p2 and substr(canceltime,1,6) = :p3 order by subscriptionindex desc) temp where rownum=1';
    execute immediate v_sql into v_freetime,v_lastrenttime,v_payfordate using v_usemsisdn, v_productid, substr(v_sysdate14,1,6);
exception when no_data_found then
    v_hasrecord := 0;
when others then
    o_retcode   := 506;
    o_debugpos := 147;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;

--免费期产品和使用产品历史订购关系的查询
if (v_freedays > 0 or v_testuser = 1) and v_hasrecord = 0 then
    begin
        select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist01 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
    exception when no_data_found then
        v_hasrecord := 0;
    when others then
        o_retcode   := 506;
        o_debugpos := 148;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if v_hasrecord = 0 then
        begin
            select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist02 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 149;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    if v_hasrecord = 0 then
        begin
            select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist03 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 150;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    if v_hasrecord = 0 then
        begin
            select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist04 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 151;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    if v_hasrecord = 0 then
        begin
            select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist05 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 152;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    if v_hasrecord = 0 then
        begin
            select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist06 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 153;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    if v_hasrecord = 0 then
        begin
            select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist07 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 154;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    if v_hasrecord = 0 then
        begin
            select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist08 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 155;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    if v_hasrecord = 0 then
        begin
            select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist09 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 156;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    if v_hasrecord = 0 then
        begin
            select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist10 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 157;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    if v_hasrecord = 0 then
        begin
            select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist11 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 158;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    if v_hasrecord = 0 then
        begin
            select count(1) into v_hasrecord from zxdbm_200.s200_user_subs_hist12 where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 159;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
end if;
if v_testuser = 1 and v_hasrecord = 1 then
    o_retcode   := 1001226;
    o_debugpos := 160;
    o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_productid = '||v_productid||', the user has subscribed the test_product before.';
    return;
end if;
o_lastrenttime := v_lastrenttime;
o_payfordate   := v_payfordate;

--有免费期且为纯包量时,查询订购关系表
if v_freedays > 0 and v_chargetype = 4 then
    begin
        select freetime into v_freetime
        from zxdbm_200.s200_user_subscription
        where usercode = v_usemsisdn and productid = v_productid and rownum = 1;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 161;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    --超过免费期订购时不享受免费期
    if v_sysdate14 > v_freetime and v_freetime <> '00000000000000' then
        v_freedays := 0;
    end if;
end if;

--modied by youqunxia20130530 计算免费期到期日期  order2.07.10
if (v_freedays > 0) and (v_hasrecord = 1) and (v_chargetype <> 4) then
    v_freetime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') - 1,'yyyymmdd') || '235959';
else
    --sms,mms,wap、mvs、dls、pim、brew业务到期日期计算
    if i_servicetype in (1,2,3,5,6,33,98) then
        if v_freeuseflag = 1 then
            --试用到月底
            v_freetime := to_char(last_day(to_date(v_substarttime,'yyyymmddhh24miss')),'yyyymmdd') || '235959';
        elsif v_freeuseflag = 2 then
            --试用N小时
            v_freetime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + v_freedays/24 ,'yyyymmddhh24miss');
        elsif v_freeuseflag = 3 then
            --试用足月
            v_freetime := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'), v_freedays ) - 1,'yyyymmdd') || '235959';
        elsif v_freeuseflag = 4 then
            --试用N天
            if (v_freedays = 3) then
                v_freetime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + 3,'yyyymmddhh24miss');
            else
                v_freetime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + v_freedays - 1,'yyyymmdd') || '235959';
            end if;
            if (v_freedays > 0) and (i_freemodule = 1) and (v_freenotify in (0,1)) and (v_chargetype in (2,5,7)) then  --包月类免费期 优惠方式
                v_freetime := to_char(last_day(to_date(v_freetime,'yyyymmddhh24miss')),'yyyymmdd') || '235959';
            end if;
        end if;
    else
    --其他业务能力计算免费日期
    --added by youqunxia 2013.02.18
        if (v_freedays = 3) then
            v_freetime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + 3,'yyyymmddhh24miss');
        else
            v_freetime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + v_freedays - 1,'yyyymmdd') || '235959';
        end if;
        if (v_freedays > 0) and (i_freemodule = 1) and (v_freenotify in (0,1)) and (v_chargetype in (2,5,7)) then  --包月类免费期 优惠方式
            v_freetime := to_char(last_day(to_date(v_freetime,'yyyymmddhh24miss')),'yyyymmdd') || '235959';
        end if;
    end if;
end if;
o_freetime := v_freetime;

if (v_freetime > v_substarttime) and (v_hasrecord = 0) and (o_issecondproduct not in (1,2)) then    --免费期
    if v_freenotify in (1,2) then
        --取默认值'99991231';
        null;
    else
        v_nextrentdate := to_char(to_date(o_freetime,'yyyymmddhh24miss') + 1,'yyyymmdd');
    end if;
elsif v_popuexist in (0,1,3) then      --推广期
    if v_popuexist = 0 then           --推广期免费
        v_nextrentdate := to_char(to_date(v_popustop,'yyyymmdd') + 1,'yyyymmdd');
    elsif v_popuexist = 1 then       --推广期计费
        if v_chargetype = 1 then     --按次
            v_nextrentdate := to_char(to_date(v_popustop,'yyyymmdd') + 1,'yyyymmdd');
        elsif v_chargetype = 2 then  --包月
            if v_validmode = 0 then
                if v_popustop = to_char(last_day(to_date(v_substarttime,'yyyymmddhh24miss')),'yyyymmdd') then
                    if v_jbchargetype in (2,5,7) then
                        v_nextrentdate := to_char(last_day(to_date(v_substarttime,'yyyymmddhh24miss')) + v_rentday,'yyyymmdd');
                    else
                        v_nextrentdate := to_char(to_date(v_popustop,'yyyymmdd') + 1,'yyyymmdd');
                    end if;
                else
                    v_nextrentdate := to_char(last_day(to_date(v_substarttime,'yyyymmddhh24miss')) + v_rentday,'yyyymmdd');
                end if;
            else
                v_nextrentdate := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + 1,'yyyymmdd');
            end if;
        end if;
    elsif v_popuexist = 3 then       --订购在推广期内,生效在推广期之外
        v_nextrentdate := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + 1,'yyyymmdd');
    end if;
elsif (v_subchargetype = 1) and (v_cyctype = 1) then
    if v_validmode = 0 then
        if v_chargetype in (2,5,7) then
            --added by youqunxia20130923 order3.02.01
            if v_invalidmode = 2 then
                --上海院需求,退订使用足月的计算规则,退订后可使用到订购日(临界条件特殊处理)
                v_unsubenddate := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),1), 'yyyymmdd');
                if substr(v_substarttime,7,2) < substr(v_unsubenddate,7,2) then
                    --适用2月28日订购(非闰年)类似情景
                    v_unsubenddate :=substr(v_unsubenddate,1,6)||substr(v_substarttime,7,2);
                end if;
                v_nextrentdate := to_char(to_date(v_unsubenddate,'yyyymmdd') + 1,'yyyymmdd');
                
                --广东院需求,退订使用足月的计算规则,退订后使用到订购日前一天(临界条件特殊处理)
                if i_fullmonthmode = 1 then
                    v_nextrentdate := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),1), 'yyyymmdd');
                    if substr(v_substarttime,7,2) < substr(v_nextrentdate,7,2) then
                        --适用2月28日订购(非闰年)类似情景
                        v_nextrentdate :=substr(v_nextrentdate,1,6)||substr(v_substarttime,7,2);
                    elsif substr(v_substarttime,7,2) > substr(v_nextrentdate,7,2) then
                        --适用1月31订购的类似情景,扣租日为下下个月的1号
                        v_nextrentdate := to_char(add_months(to_date(v_nextrentdate,'yyyymmdd'),1), 'yyyymm')||'01';
                    end if;
                end if;
            else
                v_nextrentdate := to_char(last_day(to_date(v_substarttime,'yyyymmddhh24miss')) + v_rentday,'yyyymmdd');
            end if;
        elsif v_chargetype in (3,6,8) then
            if (v_cycunit = 1) then    --包天
                if i_servicetype = 5 and o_issecondproduct = 2 then  --周期性MVS包天产品,待失效再次订购时调整下次扣租日期
                    v_nextrentdate := to_char(to_date(v_cancelnextrentdate,'yyyymmdd') + v_cycparam,'yyyymmdd');
                else
                    v_nextrentdate := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + 1 + v_cycparam,'yyyymmdd');
                end if;
            elsif (v_cycunit = 2) then --包周
                v_nextrentdate := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + v_cycparam * 7,'yyyymmdd');
            elsif (v_cycunit = 3) then --包月
                v_nextrentdate := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),v_cycparam),'yyyymmdd');
            elsif (v_cycunit = 4) then --包半年
                v_nextrentdate := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),v_cycparam * 6),'yyyymmdd');
            elsif (v_cycunit = 5) then --包年
                v_nextrentdate := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),v_cycparam * 12),'yyyymmdd');
            end if;
        end if;
    else
        v_nextrentdate := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + 1,'yyyymmdd');
    end if;
elsif (v_subchargetype = 1) and (v_cyctype = 0) then
    if v_chargetype in (3,6,8) then
    --非立即生效,非周期性产品,在订购关系开始后进行扣租
        if v_validmode <> 0 then
            v_nextrentdate := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + 1,'yyyymmdd');
        end if;
    end if;
elsif v_subchargetype in (2,3) then
    v_nextrentdate := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + 1,'yyyymmdd');
end if;
o_nextrentdate := v_nextrentdate;

/**
--part-- --订购-- 计算本次需扣费用 (业务下多个产品订购or订购关系待失效产品的订购 / 单个产品订购)
*/

if o_issecondproduct in (1,2) then
    --计算被189替换的原产品/订购关系待失效产品,本月是否扣租
    begin
        select subfeemode,popularizestart,popularizestop,popularizeitem,needcharge
        into v_cancelsubfeemode,v_cancelpopustart,v_cancelpopustop,v_cancelpopuitem,v_cancelneedcharge
        from   zxdbm_200.s200_product
        where  productindex = v_cancelproductindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 162;
        o_debuginfo := 'v_cancelproductindex='||v_cancelproductindex||', The product does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 163;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;

    --获取产品的计费信息
    if (v_cancelpopuitem = 2) then  --无推广期
        v_tempitem := 0;
    elsif v_cancelpopuitem in (0,1) then  --推广期免费或者推广期计费
        if (v_sysdate8 >= v_cancelpopustart) and (v_sysdate8 <= v_cancelpopustop) then
            if v_cancelpopuitem = 0 then  --推广期免费
                o_retcode   := 9999;
                o_debugpos := 164;
                o_debuginfo := 'v_popuitem='||v_popuitem||', popu_free_product can not update the subscription.';
            else
                v_tempitem := v_cancelpopuitem;
            end if;
        else
            v_tempitem := 0;
        end if;
    end if;

    if i_servicetype in (1,2) and v_cancelneedcharge <> 0 then
        v_sql := 'select rentfee,minlimitfee,chargetype,fixfee,nextrentfee,rentfeevalidate '||
                 'from zxdbm_200.s200_product_fee where  productindex = :p1 and itemindex = :p2';
        begin
            execute immediate v_sql into v_cancelrentfee,v_cancelminlimitfee,v_cancelchargetype,v_cancelfixfee,v_nextrentfee,v_rentfeevalidate using v_cancelproductindex,v_tempitem;
        exception when no_data_found then
            o_retcode   := 2100;
            o_debugpos := 165;
            o_debuginfo := 'The product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 166;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    elsif v_cancelneedcharge <> 0 then
        begin
            select rentfee,minlimitfee,chargetype,fixfee into v_cancelrentfee,v_cancelminlimitfee,v_cancelchargetype,v_cancelfixfee
            from   zxdbm_200.s200_product_fee
            where  productindex = v_cancelproductindex and itemindex = v_tempitem;
        exception when no_data_found then
            o_retcode   := 3103;
            o_debugpos := 167;
            o_debuginfo := 'The product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 168;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;

    if o_issecondproduct = 1 then  --订购待失效产品前面已经鉴过权,此外不需求重复再进行鉴权,否则会导致MVS业务续订出错
        --订购待失效产品前面已经鉴过权,此外不需求重复再进行鉴权,否则会导致MVS业务续订出错
        if v_cancelchargetype in (2,5,7) or v_cancelneedcharge = 0 then
            --包月类产品、免费产品可以替换
            null;
        else
            o_retcode   := 9999;
            o_debugpos := 169;
            o_debuginfo := 'v_cancelchargetype='||v_cancelchargetype||', No_month_Product can not update the subscription.';
        end if;
    end if;

    if v_cancelchargetype in (2,5,7) then
        if v_cancelfreetime < v_sysdate14 then
            if (substr(v_cancellastrenttime,1,6) = v_sysmonth6) then
                --本月租金已扣除
                o_subfee := 0;
                o_lastrenttime := v_cancellastrenttime;
                o_payfordate   := v_cancelpayfordate;
            elsif (substr(v_cancellastrenttime,1,6) < v_sysmonth6) then
                --本月租金未扣除
                v_subfee := v_cancelfixfee;
                if i_servicetype in (1,2) then
                    if (v_cancelrentfee <> v_nextrentfee) and (substr(v_rentfeevalidate,1,6) = v_sysmonth6) then
                        v_cancelrentfee := v_nextrentfee;
                    end if;
                end if;
                if (v_cancelsubfeemode = 1) then      --收取本帐期全部费用
                    v_subfee := v_subfee + v_cancelrentfee;
                elsif (v_cancelsubfeemode = 2) then   --上半月订购收取全月费用,下半月订购收取半月费用
                    if (to_number(substr(v_sysdate8,7,2)) - 15 > 0) then
                        v_subfee := v_subfee + v_cancelrentfee/2;
                    else
                        v_subfee := v_subfee + v_cancelrentfee;
                    end if;
                elsif (v_cancelsubfeemode = 3) then   --n日全费含n日,n日后免费
                    if (v_subfeedays = 0) then    --0表示本月全部免费,其他表示 N号之后免费。
                        null;
                    elsif (to_number(substr(v_sysdate8,7,2)) - v_subfeedays <= 0) then
                        v_subfee := v_subfee + v_cancelrentfee;
                    else
                        null;
                    end if;
                elsif (v_cancelsubfeemode = 4) then   --实际天数收费
                    v_leftdays  := to_number(last_day(to_date(v_sysdate14,'yyyymmddhh24miss')) - to_date(v_sysdate14,'yyyymmddhh24miss')) + 1;
                    v_monthdays := to_number(substr(v_curmonthendtime,7,2));
                    v_subfee    := v_subfee + v_cancelrentfee*(v_leftdays/v_monthdays);
                end if;
                o_subfee := v_subfee;
                if o_subfee < v_cancelminlimitfee then
                    o_subfee := v_cancelminlimitfee;
                end if;
                o_lastrenttime := v_sysdate14;
                o_payfordate   := v_sysdate8;
            end if;
        end if;
    else
        if i_servicetype = 5 and v_cancelchargetype in (3,6,8) then --MVS可以继续订购待失效状态的产品,只有MVS非包月的费率可以在待失效状态订购
            o_subfee := v_cancelrentfee + v_cancelfixfee;
            o_lastrenttime := v_sysdate14;
            o_payfordate   := v_sysdate8;
        end if;
    end if;
else
    --计算本次订购需要扣取的费用
    if (v_needcharge = 1) and ((o_freetime < v_sysdate14) or (v_freedays > 0 and v_hasrecord = 1)) and (v_validmode = 0) then
        if v_chargetype not in (2,3,4,5,6,7,8) then
            v_subfee := 0;
        else
            v_subfee := v_fixfee;
            if v_chargetype in (3,6,8) then                              --包周期产品
                v_subfee := v_subfee + v_rentfee;
            elsif  v_chargetype in (2,5,7) then                          --包月类产品
                --added by youqunxia20130923  order3.02.01
                --包月类产品,退订使用足月产品收取本月全部费用,与首月属性无关
                if v_invalidmode = 2 then
                    v_subfee := v_subfee + v_rentfee;
                else
                    if v_subchargetype <> 1 then
                        v_subfee := 0;
                    elsif v_chargetype in (2,5) and (v_hasrecord = 1 and v_backfeemode = 0 and substr(v_lastrenttime,1,6) = v_sysmonth6) then         --(包月,包月+单位费率)本月已经订购过
                        v_subfee := 0;
                    elsif (v_chargetype = 7) and (v_hasrecord = 1 and v_backfeemode = 0 and substr(v_lastrenttime,1,6) = v_sysmonth6) then            --(包月+包量)本月已经订购过
                       null;
                    else
                        if i_servicetype in (1,2) then
                            if (v_rentfee <> v_nextrentfee) and (substr(v_rentfeevalidate,1,6) = v_sysmonth6) then
                                v_rentfee := v_nextrentfee;
                            end if;
                        end if;
                        if (v_hasrecord = 1 and v_backfeemode = 1 and substr(v_lastrenttime,1,6) = v_sysmonth6) then     --退订需要补款产品本月已经订购过
                            v_tag := 1;
                            v_leftdays  := to_number(last_day(to_date(v_sysdate14,'yyyymmddhh24miss')) - to_date(v_sysdate14,'yyyymmddhh24miss')) + 1;
                            v_monthdays := to_number(substr(v_curmonthendtime,7,2));
                            v_subfee    := v_subfee + v_rentfee*(v_leftdays/v_monthdays);
                        end if;
                        if (v_subfeemode = 1) then      --收取本帐期全部费用
                            if v_tag = 0 then
                                v_subfee := v_subfee + v_rentfee;
                            end if;
                        elsif (v_subfeemode = 2) then   --上半月订购收取全月费用,下半月订购收取半月费用
                            if v_tag = 0 then
                                if (to_number(substr(v_sysdate8,7,2)) - 15 > 0) then
                                    v_subfee := v_subfee + v_rentfee/2;
                                else
                                    v_subfee := v_subfee + v_rentfee;
                                end if;
                            end if;
                        elsif (v_subfeemode = 3) then   --n日全费含n日,n日后免费
                            if (v_subfeedays = 0) then    --0表示本月全部免费,其他表示 N号之后免费。
                                null;
                            elsif (to_number(substr(v_sysdate8,7,2)) - v_subfeedays <= 0) then
                                if v_tag = 0 then
                                    v_subfee := v_subfee + v_rentfee;
                                end if;
                            else
                                null;
                            end if;
                        elsif (v_subfeemode = 4) then   --实际天数收费
                            if v_tag = 0 then
                                v_leftdays  := to_number(last_day(to_date(v_sysdate14,'yyyymmddhh24miss')) - to_date(v_sysdate14,'yyyymmddhh24miss')) + 1;
                                v_monthdays := to_number(substr(v_curmonthendtime,7,2));
                                v_subfee    := v_subfee + v_rentfee*(v_leftdays/v_monthdays);
                            end if;
                        end if;
                    end if;
                end if;
            end if;
        end if;
    end if;
    o_subfee := v_subfee;
    if o_subfee < v_minlimitfee then
        o_subfee := v_minlimitfee;
    end if;
    if o_subfee > 0 then
        o_lastrenttime := v_sysdate14;
        o_payfordate   := v_sysdate8;
        if v_cyctype = 0 then
            o_nextrentdate := '99991231';
        end if;
    end if;
end if;

--added by youqunxia 20130922 Order3.02.01
if (i_cmdchannel = 40) then
    --免费体验和积分兑换,不填写chargetime
    if i_isexperience in(1,2) then
        null;
    --费用>0,填写系统时间
    elsif (o_subfee > 0) then 
        v_chargetime := v_sysdate14;
    --费用=0,填写下次扣租日
    else
        v_chargetime := o_nextrentdate || '050000';
    end if;
end if;
o_chargetime := v_chargetime;

/*
--part-- --订购-- 消费限额鉴权
*/
-- if (o_mutiluserexit = 1) and (v_subfee > 0) and (v_productpayparty <> 1) then

if (v_subfee > 0) and (v_productpayparty <> 1) then
    if (v_srcmaxlimitfee = 0) then
        v_srcmaxlimitfee := i_monthconsumedlimit;
    end if;
    if (v_srcmaxlimitfee > 0) then
        begin
            v_sql := 'select sum(usedamounts) from zxdbm_ismp.ssrv_user_totalamount' || v_curstrmonth ||
                     ' where userindex = :p1 and amounttype = 8 and amountindex = 0 and productindex = 0 and expiredate = :p2';
            execute immediate v_sql into v_usedamounts using v_srcuserindex,v_expiredate;
        exception when no_data_found then
              v_usedamounts := 0;
        when others then
            o_retcode   := 506;
            o_debugpos := 171;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        if (v_usedamounts > v_srcmaxlimitfee) then
            o_maxlimitfee := to_char(v_srcmaxlimitfee);
            o_retcode     := 1006;          --用户已达消费限额
            o_debugpos   := 172;
            o_debuginfo := 'v_usedamounts = '||v_usedamounts||', v_srcmaxlimitfee = '||v_srcmaxlimitfee||', the usr run out the limit fee.';
            return;
        end if;
    end if;
end if;

/*
--part-- --订购-- 设置订购关系结束时间
*/

if v_popuexist in (0,1,3) then                                                      --推广期基本计费费率
    v_chargetype := v_jbchargetype;
    v_cycunit := v_jbcycunit;
    v_cycparam := v_jbcycparam;
    v_cyctype := v_jbcyctype;
end if;

if  (v_freedays > 0) and (v_hasrecord = 0) and (v_freenotify in (1,2)) then
--modied by youqunxia2013.04.18
    v_subendtime := o_freetime;
else
    if v_chargetype in (1,4) then
        v_subendtime := v_productendtime;
    elsif v_chargetype in (2,5,7) then
        if v_cyctype = 1 then
            v_subendtime := v_productendtime;
        --added by youqunxia20130924 order3.02.01
        elsif v_invalidmode = 2 then
            --上海院需求,退订使用足月的计算规则,退订后可使用到订购日(临界条件特殊处理)
            v_unsubenddate := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),1), 'yyyymmdd');
            if substr(v_substarttime,7,2) < substr(v_unsubenddate,7,2) then
                --适用2月28日订购(非闰年)类似情景
                v_unsubenddate :=substr(v_unsubenddate,1,6)||substr(v_substarttime,7,2);
            end if;
            v_nextrentdate := to_char(to_date(v_unsubenddate,'yyyymmdd') + 1,'yyyymmdd');

            --广东院需求,退订使用足月的计算规则,退订后使用到订购日前一天(临界条件特殊处理)
            if i_fullmonthmode = 1 then
                v_nextrentdate := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),1), 'yyyymmdd');
                if substr(v_substarttime,7,2) < substr(v_nextrentdate,7,2) then
                --适用2月28日订购(非闰年)类似情景
                v_nextrentdate :=substr(v_nextrentdate,1,6)||substr(v_substarttime,7,2);
                elsif substr(v_substarttime,7,2) > substr(v_nextrentdate,7,2) then
                --适用1月31订购的类似情景
                v_nextrentdate := to_char(add_months(to_date(v_nextrentdate,'yyyymmdd'),1), 'yyyymm')||'01';
                end if;
            end if;
            v_subendtime := to_char(to_date(v_nextrentdate,'yyyymmdd') -1,'yyyymmdd')||'235959';
        else
            v_subendtime := to_char(last_day(to_date(v_substarttime,'yyyymmddhh24miss')),'yyyymmdd') || '235959';
        end if;
    elsif v_chargetype in (3,6,8,9) then
        if (v_cycunit = 1) then --包天
            --周期性
            if v_cyctype = 1 then
                v_subendtime := v_productendtime;
            else
                if v_popuexist in (0,1) then  --推广期存在
                    v_subendtime := to_char(to_date(v_popustop,'yyyymmdd') + v_cycparam, 'yyyymmdd')||'235959';
                elsif (v_freedays > 0) and (v_hasrecord = 0) and (i_servicetype = 5) then  --MVS业务包天非周期有免费期的场景
                    v_subendtime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + v_freedays/24 + v_cycparam,'yyyymmddhh24miss');
                elsif (v_freedays > 0) and (v_hasrecord = 0) then   --含免费期首次订购
                    v_subendtime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + v_freedays + v_cycparam - 1,'yyyymmdd')||'235959';
                else
                    if i_servicetype = 5 and v_cursubstatus = 4 then --MVS业务待失效重复订购时,在原有结束时上基本上进行周期累加
                        v_subendtime := to_char(to_date(v_subendtime,'yyyymmddhh24miss') + v_cycparam,'yyyymmdd')||'235959';
                    else
                        v_subendtime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + v_cycparam,'yyyymmdd')||'235959';
                    end if;
                end if;
            end if;
        elsif (v_cycunit = 2) then --包周
            --周期性
            if v_cyctype = 1 then
                v_subendtime := v_productendtime;
            else
                if v_popuexist in (0,1) then
                    v_subendtime := to_char(to_date(v_popustop,'yyyymmdd') + v_cycparam*7, 'yyyymmdd')||'235959';
                elsif (v_freedays > 0) and (v_hasrecord = 0) then
                    v_subendtime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + v_freedays + v_cycparam*7-1,'yyyymmdd')||'235959';
                else
                    v_subendtime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + v_cycparam*7-1,'yyyymmdd')||'235959';
                end if;
            end if;
        elsif (v_cycunit = 3) then --包月
            --周期性
            if v_cyctype = 1 then
                v_subendtime := v_productendtime;
            else
                if v_popuexist in (0,1) then
                    v_subendtime := to_char(add_months(to_date(v_popustop,'yyyymmdd'),v_cycparam), 'yyyymmdd')||'235959';
                elsif (v_freedays > 0) and (v_hasrecord = 0) then
                    v_subendtime := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),v_cycparam) + v_freedays -1,'yyyymmdd')||'235959';
                else
                    v_subendtime := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),v_cycparam) -1,'yyyymmdd')||'235959';
                end if;
            end if;
        elsif (v_cycunit = 4) then --包半年
            --周期性
            if v_cyctype = 1 then
                v_subendtime := v_productendtime;
            else
                if v_popuexist in (0,1) then
                    v_subendtime := to_char(add_months(to_date(v_popustop,'yyyymmdd'),v_cycparam*6), 'yyyymmdd')||'235959';
                elsif (v_freedays > 0) and (v_hasrecord = 0) then
                    v_subendtime := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),v_cycparam*6) + v_freedays - 1,'yyyymmdd')||'235959';
                else
                    v_subendtime := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),v_cycparam*6)-1,'yyyymmdd')||'235959';
                end if;
            end if;
        elsif (v_cycunit = 5) then --包年
            --周期性
            if v_cyctype = 1 then
                v_subendtime := v_productendtime;
            else
                if v_popuexist in (0,1) then
                    v_subendtime := to_char(add_months(to_date(v_popustop,'yyyymmdd'),v_cycparam*12), 'yyyymmdd')||'235959';
                elsif (v_freedays > 0) and (v_hasrecord = 0) then
                    v_subendtime := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),v_cycparam*12)+ v_freedays - 1,'yyyymmdd')||'235959';
                else
                    v_subendtime := to_char(add_months(to_date(v_substarttime,'yyyymmddhh24miss'),v_cycparam*12)-1,'yyyymmdd')||'235959';
                end if;
            end if;
        end if;
    else
        v_subendtime := v_productendtime;     --订购终止时间,暂时定为产品终止时间
    end if;
end if;

o_substarttime := v_substarttime;
o_subendtime   := v_subendtime;
o_substatus    := v_substatus;

-- o_cdrbegintime := v_substarttime;

--added by youqunxia 2013.02.18
o_freenotify  := v_freenotify;
--added by youqunxia 20130314
o_validmode   := v_validmode;

/*
--part-- --订购-- 设置话单中的begintime、endtime
*/

--added by youqunxia20130924 order3.02.01
--退订使用足月产品:话单计费结束时间为下次扣租前一天的"235959"
if v_chargetype in (2,5,7) and v_nextrentdate <= substr(v_subendtime,1,8) then
    if v_invalidmode = 2 then
        o_cdrendtime := to_char(to_date(v_nextrentdate,'yyyymmdd')-1,'yyyymmdd')||'235959';
    end if;
end if;

--免费期产品订购
if (o_freetime > o_substarttime) and (o_subfee = 0) then
    o_cnfmtype := 2;
end if;

--EventID chanelPlayerID 状态查询
if i_actiontype = 1 then
    begin
        select status into o_chanelplayerstate from zxdbm_ismp.channel_basic where cpid = i_channelid;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 173;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;

    begin
        select status into o_eventstate from zxdbm_ismp.ssys_event where eventid = i_eventid;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 174;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

--伪码处理(支持伪码开关打开,非自营sp)--支持业务LCS,SMS,MVS,DLS,MMS,WAP,CPX
--MOAT时,AuthPriceReq消息中OA真实号码转化为伪码,同步SP使用

-- if (o_mutiluserexit = 1) and i_servicetype in (1,2,3,4,41,5,6,101) then

if i_servicetype in (1,2,3,4,41,5,6,101) then
    if i_checkpseudocode = 1 and v_cpsrvtype <> 3 then
        if i_serviceflowtype = 1 then
            begin
                select pseudocode into o_pseudocode from zxdbm_ismp.susr_basic where usercode = i_oa;
            exception when no_data_found then
                o_retcode   := 1001;
                o_debugpos := 175;
                o_debuginfo := 'i_oa = '||i_oa||', the user does not exist.';
                return;
            when others then
                o_retcode   := 506;
                o_debugpos := 176;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            o_pcodemsisdnswitch := 1;
        end if;
    end if;
end if;

--是否需进行二次确认
if (i_isnondreplay = 1) then
    o_needreplay := 0;
else
    o_needreplay := v_needreplay;
    if (i_cmdchannel = 45) then
        if (i_webgwcnfmswitch = 1) and (v_cpsrvtype <> 3) then
            o_needreplay := v_needreplay;
        else
            o_needreplay := 0;
        end if;
    end if;
end if;

--是否立即下发免费期提醒确认短息设置
--added by youqunxia20130603 order2.07.10
if (v_freetime > v_substarttime) and (v_hasrecord = 0) then
    if (i_actiontype in (1,17,102) and v_needreplay = 0) or i_actiontype = 10 then
        v_freenoticetime := to_char(to_date(v_substarttime,'yyyymmddhh24miss') + 1,'yyyymmdd') || '080000';
        if (v_freetime < v_freenoticetime) then
            o_freenoticenow := 1;
        end if;
    end if;
end if;

--组装同步给SP的消息
if (o_needreplay = 0) then
    if (o_protocoltype <> 0) then
     if (i_supportproductid = 1) then
        --使用产品代码同步
        if (o_protocoltype = 2) then
            o_msgtosp := 'DG' || ' ' || v_productid || '  ' || v_usemsisdn;
        elsif (o_protocoltype = 1) then
            --平台向sp发送的同步消息
            o_msgtosp     := '1001' || ' ' || v_productid || ' ' || v_usemsisdn || ' ' || v_paymsisdn;
            --平台向sp发送的确认消息
            o_cnfmmsgtosp := '1000' || ' ' || v_productid || ' ' || v_usemsisdn || ' ' || v_paymsisdn;
        end if;
     else
        --使用业务代码同步
        if (o_protocoltype = 2) then
           o_msgtosp := 'DG' || ' ' || v_serviceid || '  ' || v_usemsisdn;
        elsif (o_protocoltype = 1) then
           --平台向sp发送的同步消息
           o_msgtosp     := '1001' || ' ' || v_serviceid || '  ' || v_usemsisdn || ' ' || v_paymsisdn;
           --平台向sp发送的确认消息
           o_cnfmmsgtosp := '1000' || ' ' || v_serviceid || '  ' || v_usemsisdn || ' ' || v_paymsisdn;
        end if;
     end if;
  end if;
else
    --生成linknum
    begin
        v_tmpint  := dbms_random.value(1,9999999999);
        o_linknum := v_sysdate14 || '655380' || lpad(to_char(v_tmpint),10,'0');
    exception when others then
        o_retcode   := 506;
        o_debugpos := 177;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

end slp_200_telicomsubreq;
/

prompt zxdbm_200.slp_200_telicomsubresp
create or replace procedure zxdbm_200.slp_200_telicomsubresp
(

i_srcmsisdn             in          varchar2,            --发起方/赠送方用户号码
i_usemsisdn             in          varchar2,            --使用方/受赠方用户号码
i_paymsisdn             in          varchar2,            --计费方用户号码
i_payuserindex          in          number,              --计费方号码序号
i_mutidbmode            in          number,              --系统参数:是否多DB环境 0:否 1:是
i_cmdchannel            in          number,              --动作来源
i_isthird               in          number,              --0:普通订购 1:赠送
i_servicetype           in          number,              --产品业务能力类型
i_subcapability         in          number,              --产品业务能力子类型
i_serviceindex          in          number,              --业务序号
i_serviceid             in          varchar2,            --业务代码
i_productindex          in          number,              --产品序号
i_productid             in          varchar2,            --产品代码
i_chargetype            in          number,              --产品计费类型
i_cyctype               in          number,              --产品周期类型
i_corpindex             in          number,              --集团序号
i_corpid                in          varchar2,            --集团代码
i_cpindex               in          number,              --CP/SP序号
i_cpid                  in          varchar2,            --CP/SP代码
i_substatus             in          number,              --产品订购关系状态
i_autocontinue          in          number,              --周期性订购是否自动续订
i_substarttime          in          varchar2,            --产品订购关系生效时间
i_subendtime            in          varchar2,            --产品订购关系失效时间
i_lastrenttime          in          varchar2,            --产品订购关系上次扣租时间
i_nextrentdate          in          varchar2,            --产品订购关系下次扣租日期
i_payfordate            in          varchar2,            --月租已付日期
i_fixusetimes           in          number,              --产品包量值
i_freetime              in          varchar2,            --产品订购关系免费期
i_opid                  in          varchar2,            --操作员工号
i_subfee                in          number,              --本次订购需要扣取的费用
i_usemsisdntype         in          number,              --使用方号码类型,1:msisdn,2:phs,3:pstn
i_pseudocode            in          varchar2,            --用户伪码
i_pcodemsisdnswitch     in          number,              --若!=0,则将伪码入订购关系表
i_cancelproductid       in          varchar2,            --被189替换的产品代码
i_issecondproduct       in          number,              --订购流程子类型 0:普通订购 1:业务订购 2:待失效订购关系订购 3:纯包量重复订购 6:可变价的重复订购
i_needcharge            in          number,              --产品是否需要计费(只用于多db的跨db赠送)
--addded by youqunxia20130604  order2.07.10
i_freenoticenow         in          number,              --是否立即下发过免费期提醒短信 0-否,1-是
i_pproductofferid       in          varchar2,            --增值产品对应的捆绑类套餐ID
--added by youqunxia20130818 order3.02.01
i_varifee               in          varchar2,            --可变价格
i_eventid               in          varchar2,            --事件ID
i_isexperience          in          varchar2,            --订购类型:0:正常订购 1:免费体验 2:积分兑换 3:体验转正式 默认为0
i_subscribetype         in          varchar2,            --订购方式:0:非批量受理 1: 批量受理
i_chargetime            in          varchar2,            --首次扣费时间
o_retcode               out         number,              --鉴权返回码 0代表成功,否则代表错误码
o_debugpos              out         number,              --出口顺序标识
o_debuginfo             out         varchar2,            --调试信息
o_productsubid          out         varchar2,            --产品订购关系代码
o_porductsubindex       out         number               --产品订购关系序号

)
as

--新增下面几个关于时间的参数,尽量减少sysdate的调用,所有用到sysdate的地方,以to_date(v_sysdate14,'yyyymmddhh24miss')进行代替
v_sysdate14                   char(14)    := to_char(sysdate ,'yyyymmddhh24miss');
v_sysdate8                    char(8)     := substr(v_sysdate14,1,8);
v_plantime                    char(8)     := substr(v_sysdate14,1,8);
v_sysmonth6                   char(6)     := substr(v_sysdate14,1,6);
v_curmonth                    number(2)   := to_number(substr(v_sysdate14,5,2));        --临时存放当前月份
v_curstrmonth                 char(2)     := lpad(to_char(v_curmonth),2,'0');           --临时存放当前月份,字符型
v_curmonthendtime             char(14)    := to_char(last_day(to_date(v_sysdate14,'yyyymmddhh24miss')),'yyyymmdd')||'235959';
v_freenoticetime1             char(14)    := '00000000000000';

v_updateflag                  number(3)                                                  := 0;                                              --临时存放更新成功标志
v_retvalue                    number(10)                                                 := 0;                                              --临时存放序列值
v_freemsisdntype              number(3)                                                  := 1;                                              --临时存放免费期提醒的号码类型
v_sql                         varchar2(3072)                                             := ' ';                                            --临时存放动态sql
v_hasrecord                   number(3)                                                  := 0;                                              --临时存放产品订购关系是否存在
v_issecondproduct             number(3)                                                  := i_issecondproduct;                              --订购流程子类型 0:普通订购 1:业务订购 2:待失效订购关系订购 3:纯包量重复订购 6:可变价的重复订购 7:体验转正式的重复订购
v_isservicesub                number(3)                                                  := 0;                                              --临时存放是否业务订购(189邮箱)
v_temp                        number(10)                                                 := 0;                                              --临时存放变量

v_productsubindex             zxdbm_200.s200_user_subscription.subscriptionindex%type    := 0;                                              --临时存放订购关系序号
v_productsubid                zxdbm_200.s200_user_subscription.subscriptionid%type       := ' ';                                            --临时存放订购关系代码
v_servicetype                 zxdbm_200.s200_user_subscription.servicetype%type          := i_servicetype;                                  --临时存放业务能力类型
v_subcapability               zxdbm_200.s200_user_subscription.subcapability%type        := i_subcapability;                                --临时存放业务能力子类型
v_serviceindex                zxdbm_200.s200_user_subscription.serviceindex%type         := i_serviceindex;                                 --临时存放业务序号
v_serviceid                   zxdbm_200.s200_user_subscription.serviceid%type            := i_serviceid;                                    --临时存放业务代码
v_productindex                zxdbm_200.s200_user_subscription.productindex%type         := i_productindex;                                 --临时存放产品序号
v_productid                   zxdbm_200.s200_user_subscription.productid%type            := i_productid;                                    --临时存放产品代码
v_propkgindex                 zxdbm_200.s200_user_subscription.productpkgindex%type      := 0;                                              --临时存放套餐序号
v_propkgid                    zxdbm_200.s200_user_subscription.productpkgid%type         := ' ';                                            --临时存放套餐代码
v_contentindex                zxdbm_200.s200_user_subscription.contentindex%type         := 0;                                              --临时存放内容序号
v_contentid                   zxdbm_200.s200_user_subscription.contentid%type            := ' ';                                            --临时存放内容代码
v_corpindex                   zxdbm_200.s200_user_subscription.corpindex%type            := i_corpindex;                                    --临时存放集团序号
v_corpid                      zxdbm_200.s200_user_subscription.corpid%type               := i_corpid;                                       --临时存放集团代码
v_cpindex                     zxdbm_200.s200_user_subscription.cpindex%type              := i_cpindex;                                      --临时存放CP/SP序号
v_cpid                        zxdbm_200.s200_user_subscription.cpid%type                 := i_cpid;                                         --临时存放CP/SP代码
v_srcmsisdn                   zxdbm_200.s200_user_subscription.subscriber%type           := i_srcmsisdn;                                    --发起方/赠送方用户号码
v_usemsisdn                   zxdbm_200.s200_user_subscription.usercode%type             := i_usemsisdn;                                    --使用方/受赠方用户号码
v_paymsisdn                   zxdbm_200.s200_user_subscription.payuser%type              := i_paymsisdn;                                    --计费方用户号码
v_subscribemode               zxdbm_200.s200_user_subscription.subscribemode%type        := 1;                                              --临时存放订购方式
v_subscribechannel            zxdbm_200.s200_user_subscription.subscribechannel%type     := 3;                                              --临时存放订购渠道
v_subscribeoptype             zxdbm_200.s200_user_subscription.subscribeoptype%type      := 0;                                              --临时存放订购操作
v_cancelmode                  zxdbm_200.s200_user_subscription.cancelmode%type           := 0;                                              --临时存放订购方式
v_cancelchannel               zxdbm_200.s200_user_subscription.cancelchannel%type        := 0;                                              --临时存放订购渠道
v_canceloptype                zxdbm_200.s200_user_subscription.canceloptype%type         := 0;                                              --临时存放订购操作
v_pausemode                   zxdbm_200.s200_user_subscription.pausemode%type            := 0;                                              --临时存放产品暂停原因
v_resumemode                  zxdbm_200.s200_user_subscription.resumemode%type           := 0;                                              --临时存放产品恢复原因
v_substatus                   zxdbm_200.s200_user_subscription.status%type               := i_substatus;                                    --临时存放产品订购关系状态
v_autocontinue                zxdbm_200.s200_user_subscription.autocontinue%type         := i_autocontinue;                                 --临时存放周期性定购,是否自动续订
v_pushid                      zxdbm_200.s200_user_subscription.pushid%type               := ' ';                                            --临时存放推荐者ID
v_substarttime                zxdbm_200.s200_user_subscription.starttime%type            := i_substarttime;                                 --临时存放定购生效时间
v_subendtime                  zxdbm_200.s200_user_subscription.endtime%type              := i_subendtime;                                   --临时存放定购失效时间
v_lastrenttime                zxdbm_200.s200_user_subscription.lastrenttime%type         := i_lastrenttime;                                 --临时存放上次扣租时间
v_nextrentdate                zxdbm_200.s200_user_subscription.nextrentdate%type         := i_nextrentdate;                                 --临时存放下次扣租日期
v_payfordate                  zxdbm_200.s200_user_subscription.payfordate%type           := i_payfordate;                                   --临时存放月租已付日期
v_returnvalues                zxdbm_200.s200_user_subscription.returnvalues%type         := 0;                                              --临时存放下一周期返还值
v_returntype                  zxdbm_200.s200_user_subscription.returntype%type           := 1;                                              --临时存放返还量类型
v_returnstartdate             zxdbm_200.s200_user_subscription.returnstartdate%type      := '00000000';                                     --临时存放返还值开始日期
v_returnenddate               zxdbm_200.s200_user_subscription.returnenddate%type        := '00000000';                                     --临时存放返还值结束日期
v_fixusetimes                 zxdbm_200.s200_user_subscription.fixusetimes%type          := i_fixusetimes;                                  --临时存放非周期包量允许使用值
v_chargeparty                 zxdbm_200.s200_user_subscription.chargeparty%type          := 0;                                              --临时存放定购时填入,扣月租使用,计费方 0:用户;1:SP;2:集团
v_freetime                    zxdbm_200.s200_user_subscription.freetime%type             := i_freetime;                                     --临时存放产品订购免费日期
v_canceltime                  zxdbm_200.s200_user_subscription.canceltime%type           := '00000000000000';                               --临时存放用户退定时间
v_firstusetime                zxdbm_200.s200_user_subscription.firstusetime%type         := '00000000000000';                               --临时存放用户首次使用时间
v_lastusetime                 zxdbm_200.s200_user_subscription.lastusetime%type          := '00000000000000';                               --临时存放用户上次使用时间
v_tastestarttime              zxdbm_200.s200_user_subscription.tastestarttime%type       := '00000000000000';                               --临时存放体验期开始时间
v_tasteendtime                zxdbm_200.s200_user_subscription.tasteendtime%type         := '00000000000000';                               --临时存放体验期结束时间
v_pausetime                   zxdbm_200.s200_user_subscription.pausetime%type            := '00000000000000';                               --临时存放暂停时间
v_resumetime                  zxdbm_200.s200_user_subscription.resumetime%type           := '00000000000000';                               --临时存放暂停恢复时间
v_subscribeopid               zxdbm_200.s200_user_subscription.subscribeopid%type        := i_opid;                                         --临时存放定购操作员工号
v_cancelopid                  zxdbm_200.s200_user_subscription.cancelopid%type           := ' ';                                            --临时存放退定操作员工号
v_cancelreason                zxdbm_200.s200_user_subscription.cancelreason%type         := ' ';                                            --临时存放退定原因描述
v_lastrent                    zxdbm_200.s200_user_subscription.lastrent%type             := i_subfee;                                       --临时存放上次扣租金额
v_msisdntype                  zxdbm_200.s200_user_subscription.msisdntype%type           := i_usemsisdntype;                                --临时存放用户号码类型 1-msisdn,2-phs,3-pstn,4-IPTV接入号,5-宽带接入号,6-固定IP
v_beupdatedproductid          zxdbm_200.s200_user_subscription.productid%type            := i_cancelproductid;                              --临时存放产品代码
v_mdbsubstatus                zxdbm_200.s200_user_subscription.status%type               := 0;                                              --临时存放产品订购关系状态
v_productpkgindex             zxdbm_200.s200_user_subscription.productpkgindex%type      := 0;                                              --临时存放产品订购关系状态
v_pproductofferid             zxdbm_200.s200_user_subscription.pproductofferid%type      := ' ';                                            --临时存放增值产品对应的捆绑类套餐ID
--added by youqunxia20130818 order3.02.01
v_varifee                     zxdbm_200.s200_user_subscription.varifee%type              := '';                                             --临时存放当前订购关系中的可变价格
v_eventid                     zxdbm_200.s200_user_subscription.eventid%type              := '';                                             --临时存放事件ID
v_isexperience                zxdbm_200.s200_user_subscription.isexperience%type         := i_isexperience;                                 --订购类型:0:正常订购 1:免费体验 2:积分兑换 3:体验转正式 默认为0
v_subscribetype               zxdbm_200.s200_user_subscription.subscribetype%type        := i_subscribetype;                                --订购方式:0:非批量受理 1: 批量受理 默认为0
v_chargetime                  zxdbm_200.s200_user_subscription.chargetime%type           := '00000000000000';                               --首次扣费时间

--被189替换的订购关系
v_cancelproductsubindex       zxdbm_200.s200_user_subscription.subscriptionindex%type    := 0;                                              --临时存放订购关系序号
v_cancelproductsubid          zxdbm_200.s200_user_subscription.subscriptionid%type       := ' ';                                            --临时存放订购关系代码
v_cancelservicetype           zxdbm_200.s200_user_subscription.servicetype%type          := i_servicetype;                                  --临时存放业务能力类型
v_cancelsubcapability         zxdbm_200.s200_user_subscription.subcapability%type        := i_subcapability;                                --临时存放业务能力子类型
v_cancelserviceindex          zxdbm_200.s200_user_subscription.serviceindex%type         := i_serviceindex;                                 --临时存放业务序号
v_cancelserviceid             zxdbm_200.s200_user_subscription.serviceid%type            := i_serviceid;                                    --临时存放业务代码
v_cancelproductindex          zxdbm_200.s200_user_subscription.productindex%type         := i_productindex;                                 --临时存放产品序号
v_cancelproductid             zxdbm_200.s200_user_subscription.productid%type            := i_productid;                                    --临时存放产品代码
v_cancelpropkgindex           zxdbm_200.s200_user_subscription.productpkgindex%type      := 0;                                              --临时存放套餐序号
v_cancelpropkgid              zxdbm_200.s200_user_subscription.productpkgid%type         := ' ';                                            --临时存放套餐代码
v_cancelcontentindex          zxdbm_200.s200_user_subscription.contentindex%type         := 0;                                              --临时存放内容序号
v_cancelcontentid             zxdbm_200.s200_user_subscription.contentid%type            := ' ';                                            --临时存放内容代码
v_cancelcorpindex             zxdbm_200.s200_user_subscription.corpindex%type            := i_corpindex;                                    --临时存放集团序号
v_cancelcorpid                zxdbm_200.s200_user_subscription.corpid%type               := i_corpid;                                       --临时存放集团代码
v_cancelcpindex               zxdbm_200.s200_user_subscription.cpindex%type              := i_cpindex;                                      --临时存放CP/SP序号
v_cancelcpid                  zxdbm_200.s200_user_subscription.cpid%type                 := i_cpid;                                         --临时存放CP/SP代码
v_cancelsubscriber            zxdbm_200.s200_user_subscription.subscriber%type           := i_srcmsisdn;                                    --发起方/赠送方用户号码
v_cancelusercode              zxdbm_200.s200_user_subscription.usercode%type             := i_usemsisdn;                                    --使用方/受赠方用户号码
v_cancelpayuser               zxdbm_200.s200_user_subscription.payuser%type              := i_paymsisdn;                                    --计费方用户号码
v_cancelsubscribemode         zxdbm_200.s200_user_subscription.subscribemode%type        := 1;                                              --临时存放订购方式
v_cancelsubscribechannel      zxdbm_200.s200_user_subscription.subscribechannel%type     := 3;                                              --临时存放订购渠道
v_cancelsubscribeoptype       zxdbm_200.s200_user_subscription.subscribeoptype%type      := 0;                                              --临时存放订购操作
v_cancelcancelmode            zxdbm_200.s200_user_subscription.cancelmode%type           := 0;                                              --临时存放订购方式
v_cancelcancelchannel         zxdbm_200.s200_user_subscription.cancelchannel%type        := 0;                                              --临时存放订购渠道
v_cancelpausemode             zxdbm_200.s200_user_subscription.pausemode%type            := 0;                                              --临时存放产品暂停原因
v_cancelresumemode            zxdbm_200.s200_user_subscription.resumemode%type           := 0;                                              --临时存放产品恢复原因
v_cancelsubstatus             zxdbm_200.s200_user_subscription.status%type               := i_substatus;                                    --临时存放产品订购关系状态
v_cancelstatustime            zxdbm_200.s200_user_subscription.statustime%type           := '00000000000000';
v_cancelautocontinue          zxdbm_200.s200_user_subscription.autocontinue%type         := i_autocontinue;                                 --临时存放周期性定购,是否自动续订
v_cancelpushid                zxdbm_200.s200_user_subscription.pushid%type               := ' ';                                            --临时存放推荐者ID
v_cancelsubstarttime          zxdbm_200.s200_user_subscription.starttime%type            := i_substarttime;                                 --临时存放定购生效时间
v_cancelsubendtime            zxdbm_200.s200_user_subscription.endtime%type              := i_subendtime;                                   --临时存放定购失效时间
v_cancellastrenttime          zxdbm_200.s200_user_subscription.lastrenttime%type         := i_lastrenttime;                                 --临时存放上次扣租时间
v_cancelnextrentdate          zxdbm_200.s200_user_subscription.nextrentdate%type         := i_nextrentdate;                                 --临时存放下次扣租日期
v_cancelpayfordate            zxdbm_200.s200_user_subscription.payfordate%type           := i_payfordate;                                   --临时存放月租已付日期
v_cancelreturnvalues          zxdbm_200.s200_user_subscription.returnvalues%type         := 0;                                              --临时存放下一周期返还值
v_cancelreturntype            zxdbm_200.s200_user_subscription.returntype%type           := 1;                                              --临时存放返还量类型
v_cancelreturnstartdate       zxdbm_200.s200_user_subscription.returnstartdate%type      := '00000000';                                     --临时存放返还值开始日期
v_cancelreturnenddate         zxdbm_200.s200_user_subscription.returnenddate%type        := '00000000';                                     --临时存放返还值结束日期
v_cancelfixusetimes           zxdbm_200.s200_user_subscription.fixusetimes%type          := i_fixusetimes;                                  --临时存放非周期包量允许使用值
v_cancelchargeparty           zxdbm_200.s200_user_subscription.chargeparty%type          := 0;                                              --临时存放定购时填入,扣月租使用,计费方 0:用户;1:SP;2:集团
v_cancelfreetime              zxdbm_200.s200_user_subscription.freetime%type             := i_freetime;                                     --临时存放产品订购免费日期
v_cancelcreatetime            zxdbm_200.s200_user_subscription.freetime%type             := v_sysdate14;                                    --临时存放产品订购创建日期
v_cancelcanceltime            zxdbm_200.s200_user_subscription.canceltime%type           := '00000000000000';                               --临时存放用户退定时间
v_cancelfirstusetime          zxdbm_200.s200_user_subscription.firstusetime%type         := '00000000000000';                               --临时存放用户首次使用时间
v_cancellastusetime           zxdbm_200.s200_user_subscription.lastusetime%type          := '00000000000000';                               --临时存放用户上次使用时间
v_canceltastestarttime        zxdbm_200.s200_user_subscription.tastestarttime%type       := '00000000000000';                               --临时存放体验期开始时间
v_canceltasteendtime          zxdbm_200.s200_user_subscription.tasteendtime%type         := '00000000000000';                               --临时存放体验期结束时间
v_cancelpausetime             zxdbm_200.s200_user_subscription.pausetime%type            := '00000000000000';                               --临时存放暂停时间
v_cancelresumetime            zxdbm_200.s200_user_subscription.resumetime%type           := '00000000000000';                               --临时存放暂停恢复时间
v_cancelsubscribeopid         zxdbm_200.s200_user_subscription.subscribeopid%type        := i_opid;                                         --临时存放定购操作员工号
v_cancelcancelopid            zxdbm_200.s200_user_subscription.cancelopid%type           := ' ';                                            --临时存放退定操作员工号
v_cancelcancelreason          zxdbm_200.s200_user_subscription.cancelreason%type         := ' ';                                            --临时存放退定原因描述
v_cancellastrent              zxdbm_200.s200_user_subscription.lastrent%type             := i_subfee;                                       --临时存放上次扣租金额
v_cancelmsisdntype            zxdbm_200.s200_user_subscription.msisdntype%type           := i_usemsisdntype;                                --临时存放用户号码类型 1-msisdn,2-phs,3-pstn,4-IPTV接入号,5-宽带接入号,6-固定IP
v_cancelpseudocode            zxdbm_200.s200_user_subscription.pseudocode%type           := ' ';                                            --临时存放用户号码类型 1-msisdn,2-phs,3-pstn,4-IPTV接入号,5-宽带接入号,6-固定IP
v_canceloldproductid          zxdbm_200.s200_user_subscription.oldproductid%type         := ' ';                                            --临时存放被替换产品ID
v_cancelpproductofferid       zxdbm_200.s200_user_subscription.pproductofferid%type      := ' ';                                            --临时存放增值产品对应的捆绑类套餐ID

--added by youqunxia20130818 order3.02.01
v_cancelvarifee               zxdbm_200.s200_user_subscription.varifee%type              := '';                                      --临时存放可变价格 
v_canceleventid               zxdbm_200.s200_user_subscription.eventid%type              := '';                                       --临时存放事件ID
v_cancelisexperience          zxdbm_200.s200_user_subscription.isexperience%type         := i_isexperience;                                 --订购类型:0:正常订购 1:免费体验 2:积分兑换 3:体验转正式 默认为0
v_cancelsubscribetype         zxdbm_200.s200_user_subscription.subscribetype%type        := i_subscribetype;                                --订购方式:0:非批量受理 1: 批量受理
v_cancelchargetime            zxdbm_200.s200_user_subscription.chargetime%type           := '00000000000000';                                   --首次扣费时间

v_unitvol                     zxdbm_200.s200_product_fee.unitvol%type                    := 10;                                             --临时存放相对于目前最小计量单位的量,时长为百毫秒,流量为byte
v_chargetype                  zxdbm_200.s200_product_fee.chargetype%type                 := 0;                                              --临时存放基本计费类型

v_popuitem                    zxdbm_200.s200_product.popularizeitem%type                 := 2;                                              --临时推广期计费类型
v_popustart                   zxdbm_200.s200_product.popularizestart%type                := v_sysdate8;                                     --临时推广期计费类型
v_popustop                    zxdbm_200.s200_product.popularizestop%type                 := v_sysdate8;                                     --临时推广期计费类型
v_needcharge                  zxdbm_200.s200_product.needcharge%type                     := 1;                                              --临时是否需要计费

v_fixtotalres                 zxdbm_ismp.ssrv_user_totalamount.fixtotalres%type          := 0;                                              --临时存放包量最大允许使用资源,0-无限制
v_pseudocode                  zxdbm_ismp.susr_basic.pseudocode%type                       := ' ';
v_payuserindex                zxdbm_ismp.susr_basic.userindex%type                       := i_payuserindex;                                 --临时存放计费方用户序号

begin

--出参初始化
o_retcode               := 0;
o_debugpos              := 0;
o_debuginfo             := ' ';
o_productsubid          := ' ';
o_porductsubindex       := 0;

/
多DB,赠送,订购关系鉴权 此部分鉴权可能有问题,后续再检查修改
*/

if (i_mutidbmode = 1 and i_isthird = 1) then
    v_hasrecord := 1;
    begin
        select status,productpkgindex
        into   v_mdbsubstatus,v_productpkgindex
        from   zxdbm_200.s200_user_subscription
        where  usercode = v_usemsisdn and productid = v_productid;
    exception when no_data_found then
        v_hasrecord := 0;
    when others then
        o_retcode   := 506;
        o_debugpos := 1;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if (v_hasrecord > 0) then
        if v_productpkgindex <> 0 then
            o_retcode   := 1200;   --该产品属于套餐,且用户已订购该套餐
            o_debugpos := 2;
            o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_productid = '||v_productid||', the user has subscribed pkg including this product.';
            return;
        end if;
        if v_mdbsubstatus <> 4 and i_chargetype <> 4 then --(待失效订购关系 或 纯包量)可以重复订购
            o_retcode   := 1200;        --用户已经定购该产品
            o_debugpos := 3;
            o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_productid = '||v_productid||' mutidbmode, the user has subscribed the product.';
            return;
        elsif i_chargetype = 4 then  --判断顺序不能颠倒
            v_issecondproduct := 3;
        elsif v_mdbsubstatus = 4 then
            v_issecondproduct := 2;    --待失效订购关系可以订购
        end if;
    end if;

    --检查该产品所属的业务是否是互斥的(只有短彩WAP的该字段可能为1)
    begin
        select isservicesub into v_isservicesub
        from  zxdbm_200.s200_service
        where  serviceindex = i_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 4;
        o_debuginfo := 'i_serviceindex='||i_serviceindex||' mutidbmode, the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 5;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    --业务订购关系鉴权(目前仅MVS,DLS,BREW为内容类业务)
    if i_servicetype not in (5,6,98) and v_hasrecord = 0 then
        v_issecondproduct := 1;
        v_hasrecord := 0;
        begin
            select productid,productpkgindex
            into   v_beupdatedproductid,v_productpkgindex
            from   zxdbm_200.s200_user_subscription
            where  usercode = v_usemsisdn and cpid = v_cpid and serviceid = v_serviceid;
        exception when no_data_found then
            v_issecondproduct := 0;
            v_hasrecord := 0;
        when others then
            o_retcode   := 506;
            o_debugpos := 6;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        if v_hasrecord > 0 then
            if v_productpkgindex <> 0 then
                o_retcode   := 1001210;                          --不能替换产品组合包含的产品
                o_debugpos := 7;
                o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_productpkgindex = '||v_productpkgindex||', can not replace product in the pkg.';
                return;
            end if;
            if v_beupdatedproductid <> v_productid then
                if i_servicetype in (1,2,3) and v_isservicesub = 1 then       --sms,mms,wap业务订购
                    null;
                else
                    o_retcode   := 1001210;
                    o_debugpos := 8;
                    o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', serviceid = '||v_serviceid||' mutidbmode, the user has subscribed a product in the same service.';
                    return;
                end if;
            end if;
        end if;
    end if;

    --业务订购/待失效订购关系只支持包月类产品
    if v_issecondproduct in (1,2) then
        if v_issecondproduct = 2 and i_servicetype = 5 and i_chargetype in (3,6,8) then
            --MVS的包周期产品在订购关系待失效状态时可以续订
            null;
        else
            if i_chargetype in (2,5,7) or i_needcharge =0  then
                null;
            else
                o_retcode   := 9999;
                o_debugpos := 9;
                o_debuginfo := 'i_chargetype='||i_chargetype||' mutidbmode, No_month_Product can not update this subscription.';
            end if;
        end if;
    end if;
end if;

if (i_pcodemsisdnswitch <> 0) then
    v_pseudocode := i_pseudocode;        --用户伪码
end if;

/
--part-- --订购2-- 订购渠道、订购方式
*/

--设置订购方式
if (i_isthird = 1) then
    v_subscribemode := 4;
elsif (i_cmdchannel = 4) then
    v_subscribemode := 2;
elsif (i_cmdchannel = 0) then
    v_subscribemode := 5;
elsif (i_cmdchannel = 27) then
    v_subscribemode := 3;
else
    v_subscribemode := 1;
end if;
--查询订购渠道
begin
    select portalchannel into v_subscribechannel
    from zxdbm_ismp.ssrv_channel_map
    where innerchannel = i_cmdchannel;
exception when no_data_found then
    v_subscribechannel := i_cmdchannel * 1000;
when others then
    o_retcode   := 506;
    o_debugpos := 11;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
--设置计费方
if (v_paymsisdn = v_corpid) then
    v_chargeparty  := 2;
else
    v_chargeparty  := 0;
end if;
--处理定购操作员工号
if v_subscribeopid is null then
    v_subscribeopid := ' ';
end if;

/**
设置订购时发生订购关系替换时,填历史订购关系表中的字段
**/

--设置退订渠道
v_cancelcancelchannel := v_subscribechannel;
--设置退订方式
if (i_cmdchannel = 6) then
    v_cancelcancelmode := 4;
elsif (i_cmdchannel in (0,20)) then
    v_cancelcancelmode := 3;
elsif (i_cmdchannel = 4) then
    v_cancelcancelmode := 5;
else
    v_cancelcancelmode := 2;
end if;

/**
--part-- --订购2-- 事务开始
**/

begin
    if v_issecondproduct not in (1,2,6,7) then
    --0:普通订购,3:纯包量重复订购
    --产品订购落订购关系
        if i_chargetype = 4 and i_cyctype = 0 and v_issecondproduct = 3 then --纯包量的订购关系更新
            --将订购关系转入历史表
           /*
           20110525wyl
           1.删除从订购关系表中读出cancelmode,cancelchannel两个字段。(在前面已经赋值,再次读出会覆盖)
           */
            begin
                select subscriptionindex,subscriptionid,servicetype,subcapability,
                       serviceindex,serviceid,productindex,productid,productpkgindex,
                       productpkgid,contentindex,contentid,corpindex,corpid,cpindex,
                       cpid,subscribemode,subscribechannel,subscribeoptype,
                       pausemode,resumemode,status,statustime,autocontinue,subscriber,usercode,
                       payuser,pushid,starttime,endtime,lastrenttime,
                       nextrentdate,payfordate,returnvalues,returntype,
                       returnstartdate,returnenddate,fixusetimes,chargeparty,
                       freetime,createtime,canceltime,firstusetime,lastusetime,
                       tastestarttime,tasteendtime,pausetime,resumetime,
                       subscribeopid,cancelopid,cancelreason,lastrent,msisdntype,pseudocode,oldproductid,
                       pproductofferid,varifee,eventid,isexperience,subscribetype,chargetime
                into   v_cancelproductsubindex,v_cancelproductsubid,v_cancelservicetype,v_cancelsubcapability,
                       v_cancelserviceindex,v_cancelserviceid,v_cancelproductindex,v_cancelproductid,v_cancelpropkgindex,
                       v_cancelpropkgid,v_cancelcontentindex,v_cancelcontentid,v_cancelcorpindex,v_cancelcorpid,v_cancelcpindex,
                       v_cancelcpid,v_cancelsubscribemode,v_cancelsubscribechannel,v_cancelsubscribeoptype,
                       v_cancelpausemode,v_cancelresumemode,v_cancelsubstatus,v_cancelstatustime,v_cancelautocontinue,v_cancelsubscriber,v_cancelusercode,
                       v_cancelpayuser,v_cancelpushid,v_cancelsubstarttime,v_cancelsubendtime,v_cancellastrenttime,
                       v_cancelnextrentdate,v_cancelpayfordate,v_cancelreturnvalues,v_cancelreturntype,
                       v_cancelreturnstartdate,v_cancelreturnenddate,v_cancelfixusetimes,v_cancelchargeparty,
                       v_cancelfreetime,v_cancelcreatetime,v_cancelcanceltime,v_cancelfirstusetime,v_cancellastusetime,
                       v_canceltastestarttime,v_canceltasteendtime,v_cancelpausetime,v_cancelresumetime,
                       v_cancelsubscribeopid,v_cancelcancelopid,v_cancelcancelreason,v_cancellastrent,v_cancelmsisdntype,v_cancelpseudocode,v_canceloldproductid,
                       v_cancelpproductofferid,v_cancelvarifee,v_canceleventid,v_cancelisexperience,v_cancelsubscribetype,v_cancelchargetime
                from   zxdbm_200.s200_user_subscription
                where  usercode = v_usemsisdn and productid = v_beupdatedproductid;
            exception when no_data_found then
                o_retcode   := 1201;
                o_debugpos := 12;
                o_debuginfo := 'the subscription dose not exist.';
                return;
            when others then
                o_retcode   := 506;
                o_debugpos := 13;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;

-- v_cancelcancelmode := 6;
-- v_cancelcancelchannel := 110;

            v_cancelcanceltime := v_sysdate14;
            v_sql := 'insert into zxdbm_200.s200_user_subs_hist' || v_curstrmonth ||
            ' (subscriptionindex,subscriptionid,servicetype,subcapability,'||
            'serviceindex,serviceid,productindex,productid,contentindex,contentid,'||
            'productpkgindex,productpkgid,corpindex,corpid,cpindex,cpid,subscribemode,'||
            'subscribechannel,subscribeoptype,cancelmode,cancelchannel,canceloptype,'||
            'pausemode,resumemode,status,statustime,autocontinue,subscriber,usercode,'||
            'payuser,pushid,starttime,endtime,lastrenttime,nextrentdate,payfordate,'||
            'returnvalues,returntype,returnstartdate,returnenddate,fixusetimes,'||
            'chargeparty,freetime,createtime,canceltime,firstusetime,lastusetime,'||
            'tastestarttime,tasteendtime,pausetime,resumetime,subscribeopid,'||
            'cancelopid,cancelreason,lastrent,msisdntype,pseudocode,oldproductid,pproductofferid,varifee,eventid,'||
            'isexperience,subscribetype,chargetime) '||
            'values(:p1,:p2,:p3,:p4,'||
            ':p5,:p6,:p7,:p8,:p9,:p10,'||
            ':p11,:p12,:p13,:p14,:p15,:p16,:p17,'||
            ':p18,:p19,:p20,:p21,:p22,'||
            ':p23,:p24,:p25,:p26,:p27,:p28,'||
            ':p29,:p30,:p31,:p32,:p33,:p34,:p35,'||
            ':p36,:p37,:p38,:p39,:p40,'||
            ':p41,:p42,:p43,:p44,:p45,:p46,'||
            ':p47,:p48,:p49,:p50,:p51,'||
            ':p52,:p53,:p54,:p55,:p56,:p57,:p58,:p59,:p60,:p61,'||
            ':p62,:p63,:p64)';
            begin
            --插入历史订购关系
                execute immediate v_sql
                using   v_cancelproductsubindex,v_cancelproductsubid,v_cancelservicetype,v_cancelsubcapability,
                        v_cancelserviceindex,v_cancelserviceid,v_cancelproductindex,v_cancelproductid,v_cancelcontentindex,v_cancelcontentid,
                        v_cancelpropkgindex,v_cancelpropkgid,v_cancelcorpindex,v_cancelcorpid,v_cancelcpindex,v_cancelcpid,v_cancelsubscribemode,
                        v_cancelsubscribechannel,v_cancelsubscribeoptype,v_cancelcancelmode,v_cancelcancelchannel,v_canceloptype,
                        v_cancelpausemode,v_cancelresumemode,v_cancelsubstatus,v_cancelstatustime,v_cancelautocontinue,v_cancelsubscriber,v_cancelusercode,
                        v_cancelpayuser,v_cancelpushid,v_cancelsubstarttime,v_cancelsubendtime,v_cancellastrenttime,v_cancelnextrentdate,v_cancelpayfordate,
                        v_cancelreturnvalues,v_cancelreturntype,v_cancelreturnstartdate,v_cancelreturnenddate,v_cancelfixusetimes,
                        v_cancelchargeparty,v_cancelfreetime,v_cancelcreatetime,v_cancelcanceltime,v_cancelfirstusetime,v_cancellastusetime,
                        v_canceltastestarttime,v_canceltasteendtime,v_cancelpausetime,v_cancelresumetime,v_cancelsubscribeopid,
                        v_cancelcancelopid,v_cancelcancelreason,v_cancellastrent,v_cancelmsisdntype,v_cancelpseudocode,v_canceloldproductid,
                        v_cancelpproductofferid,v_cancelvarifee,v_canceleventid,v_cancelisexperience,v_cancelsubscribetype,v_cancelchargetime;
                if (sql%rowcount = 0) then
                    o_retcode   := 506;
                    o_debugpos := 14;
                    o_debuginfo := 'no subs exist.';
                    rollback;
                    return;
                end if;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 15;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                rollback;
                return;
            end;
            --对于非周期包量的产品,直接更新订购关系
            --生成新的订购关系ID
            zxdbm_ismp.sp_getmaxvalue ('slp_order_prdindex',1,v_productsubindex);
            if (v_productsubindex = 0) then
                o_retcode   := 506;
                o_debugpos := 16;
                o_debuginfo := 'get slp_order_prdindex failed.';
                rollback;
                return;
            end if;
            begin
                select trunc(dbms_random.value(10000,99999)) into v_retvalue from dual;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 17;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                rollback;
                return;
            end;
            v_productsubid := v_usemsisdn || to_char(v_retvalue);
            begin
                update zxdbm_200.s200_user_subscription
                set    subscriptionindex    = v_productsubindex,
                       subscriptionid       = v_productsubid,
                       --fixusetimes          = fixusetimes + v_fixusetimes,
                       starttime            = v_substarttime,
                       endtime              = v_subendtime,
                       status               = v_substatus,
                       --freetime             = v_freetime,
                       createtime           = v_sysdate14,
                       lastusetime          = v_lastusetime,
                       statustime           = v_sysdate14,
                       lastrenttime         = decode(v_lastrent,0,lastrenttime,v_sysdate14),
                       lastrent             = v_lastrent,
                       subscribemode        = v_subscribemode,
                       subscribechannel     = v_subscribechannel,
                       subscribeoptype      = v_subscribeoptype,
                       subscribeopid        = v_subscribeopid
                where  usercode = v_usemsisdn and productid = v_productid
                returning subscriptionindex,subscriptionid into v_productsubindex,v_productsubid;
                if (sql%rowcount = 0) then
                    v_updateflag := 0;
                else
                    v_updateflag := 1;
                end if;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 18;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                rollback;
                return;
            end;
        end if;

        if (v_updateflag = 0) then
        --生成新的订购关系
            zxdbm_ismp.sp_getmaxvalue ('slp_order_prdindex',1,v_productsubindex);
            if (v_productsubindex = 0) then
                o_retcode   := 506;
                o_debugpos := 19;
                o_debuginfo := 'get slp_order_prdindex failes.';
                rollback;
                return;
            end if;
            begin
                select trunc(dbms_random.value(10000,99999)) into v_retvalue from dual;
            exception when others then
                o_retcode   := 506;
                o_debugpos  := 20;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                rollback;
                return;
            end;
            v_productsubid := v_usemsisdn || to_char(v_retvalue);
            --added by youqunxia20130605  order2.07.10
            --对于VSOP发起的订购请求,若ExtC5非空,则将其填入订购关系表
            if trim(i_pproductofferid) is null then
                v_pproductofferid := ' ';
            else
                v_pproductofferid := i_pproductofferid;
            end if;
            --added by youqunxia 20130818 Order3.02.01
            --如果fee值非空,将其填入订购关系表
            v_varifee := to_number(i_varifee);
            --如果eventid值非空,将其填入订购关系表
            if trim(i_eventid) is not null then
                v_eventid := i_eventid;
            else
                v_eventid := '';
            end if;
            --如果chargetime值非空,将其填入订购关系表
            if trim(i_chargetime) is not null then
                v_chargetime := i_chargetime;
            else
                v_chargetime := '00000000000000';
            end if;
            begin
                insert into zxdbm_200.s200_user_subscription
                (subscriptionindex,subscriptionid,servicetype,subcapability,
                serviceindex,serviceid,productindex,productid,productpkgindex,
                productpkgid,contentindex,contentid,corpindex,corpid,cpindex,cpid,
                subscribemode,subscribechannel,subscribeoptype,
                cancelmode,cancelchannel,canceloptype,pausemode,
                resumemode,status,statustime,autocontinue,subscriber,
                usercode,payuser,pushid,starttime,endtime,lastrenttime,
                nextrentdate,payfordate,returnvalues,returntype,
                returnstartdate,returnenddate,fixusetimes,chargeparty,
                freetime,createtime,canceltime,firstusetime,
                lastusetime,tastestarttime,tasteendtime,pausetime,resumetime,
                subscribeopid,cancelopid,cancelreason,lastrent,msisdntype,
                pseudocode,pproductofferid,varifee,eventid,isexperience,subscribetype,chargetime)
                values
                (v_productsubindex,v_productsubid,v_servicetype,v_subcapability,
                v_serviceindex,v_serviceid,v_productindex,v_productid,v_propkgindex,
                v_propkgid,v_contentindex,v_contentid,v_corpindex,v_corpid,v_cpindex,v_cpid,
                v_subscribemode,v_subscribechannel,v_subscribeoptype,
                v_cancelmode,v_cancelchannel,v_canceloptype,v_pausemode,
                v_resumemode,v_substatus,v_sysdate14,v_autocontinue,v_srcmsisdn,
                v_usemsisdn,v_paymsisdn,v_pushid,v_substarttime,v_subendtime,v_lastrenttime,
                v_nextrentdate,v_payfordate,v_returnvalues,v_returntype,
                v_returnstartdate,v_returnenddate,0,v_chargeparty,
                v_freetime,v_sysdate14,v_canceltime,v_firstusetime,
                v_lastusetime,v_tastestarttime,v_tasteendtime,v_pausetime,v_resumetime,
                v_subscribeopid,v_cancelopid,v_cancelreason,v_lastrent,v_msisdntype,
                v_pseudocode,v_pproductofferid,v_varifee,v_eventid,v_isexperience,v_subscribetype,v_chargetime);
            exception when others then
                o_retcode   := 506;
                o_debugpos := 21;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                rollback;
                return;
            end;
        end if;
    --added by youqunxia 20130818  Order3.02.01
    --可变价的重复订购订购,有定购关系时,按变更价格处理,更新订购关系
    elsif v_issecondproduct = 6 then
         v_varifee := to_number(i_varifee);
        --如果eventid值非空,将其填入订购关系表
        if trim(i_eventid) is not null then
            v_eventid := i_eventid;
        else
            v_eventid := '';
        end if;
        begin
            update zxdbm_200.s200_user_subscription
            set    status                     = v_substatus,
                   statustime                 = v_sysdate14,
                   starttime                  = v_substarttime,
                   endtime                    = v_subendtime,
                   lastrenttime               = v_lastrenttime,
                   nextrentdate               = v_nextrentdate,
                   payfordate                 = v_payfordate,
                   freetime                   = v_freetime,
                   subscribeopid              = v_subscribeopid,
                   lastrent                   = v_lastrent,
                   varifee                    = v_varifee,
                   eventid                    = v_eventid
            where  usercode = v_usemsisdn and productid = v_productid;
        exception when others then
            o_retcode  := 506;
            o_debugpos := 38;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            rollback;
            return;
        end;
    --added by youqunxia 20131015  Order3.02.01
    --体验转正式的重复订购,更新订购关系
    elsif v_issecondproduct = 7 then
        --对于VSOP发起的订购请求,若ExtC5非空,则将其填入订购关系表
        if trim(i_pproductofferid) is null then
            v_pproductofferid := ' ';
        else
            v_pproductofferid := i_pproductofferid;
        end if;
        v_varifee := to_number(i_varifee);
        --如果eventid值非空,将其填入订购关系表
        if trim(i_eventid) is not null then
            v_eventid := i_eventid;
        else
            v_eventid := '';
        end if;
        --如果chargetime值非空,将其填入订购关系表
        if trim(i_chargetime) is not null then
            v_chargetime := i_chargetime;
        else
            v_chargetime := '00000000000000';
        end if;
        begin
            update zxdbm_200.s200_user_subscription
            set    status                     = v_substatus,
                   statustime                 = v_sysdate14,
                   starttime                  = v_substarttime,
                   endtime                    = v_subendtime,
                   lastrenttime               = v_lastrenttime,
                   nextrentdate               = v_nextrentdate,
                   payfordate                 = v_payfordate,
                   freetime                   = v_freetime,
                   subscribeopid              = v_subscribeopid,
                   lastrent                   = v_lastrent,
                   pproductofferid            = v_pproductofferid,
                   varifee                    = v_varifee,
                   eventid                    = v_eventid,
                   isexperience               = v_isexperience,
                   subscribetype              = v_subscribetype,
                   chargetime                 = v_chargetime
            where  usercode = v_usemsisdn and productid = v_productid;
        exception when others then
            o_retcode  := 506;
            o_debugpos := 39;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            rollback;
            return;
        end;
    else
    --产品替换:1:业务订购 2:待失效订购关系订购
    /*
    20110525wyl
    1.删除从订购关系表中读出cancelmode,cancelchannel两个字段。(在前面已经赋值,再次读出会覆盖)
    */
        begin
            select subscriptionindex,subscriptionid,servicetype,subcapability,
                   serviceindex,serviceid,productindex,productid,productpkgindex,
                   productpkgid,contentindex,contentid,corpindex,corpid,cpindex,
                   cpid,subscribemode,subscribechannel,subscribeoptype,
                   pausemode,resumemode,status,statustime,autocontinue,subscriber,usercode,
                   payuser,pushid,starttime,endtime,lastrenttime,
                   nextrentdate,payfordate,returnvalues,returntype,
                   returnstartdate,returnenddate,fixusetimes,chargeparty,
                   freetime,createtime,canceltime,firstusetime,lastusetime,
                   tastestarttime,tasteendtime,pausetime,resumetime,
                   subscribeopid,cancelopid,cancelreason,lastrent,msisdntype,pseudocode,oldproductid,
                   pproductofferid,varifee,eventid,isexperience,subscribetype,chargetime
            into   v_cancelproductsubindex,v_cancelproductsubid,v_cancelservicetype,v_cancelsubcapability,
                   v_cancelserviceindex,v_cancelserviceid,v_cancelproductindex,v_cancelproductid,v_cancelpropkgindex,
                   v_cancelpropkgid,v_cancelcontentindex,v_cancelcontentid,v_cancelcorpindex,v_cancelcorpid,v_cancelcpindex,
                   v_cancelcpid,v_cancelsubscribemode,v_cancelsubscribechannel,v_cancelsubscribeoptype,
                   v_cancelpausemode,v_cancelresumemode,v_cancelsubstatus,v_cancelstatustime,v_cancelautocontinue,v_cancelsubscriber,v_cancelusercode,
                   v_cancelpayuser,v_cancelpushid,v_cancelsubstarttime,v_cancelsubendtime,v_cancellastrenttime,
                   v_cancelnextrentdate,v_cancelpayfordate,v_cancelreturnvalues,v_cancelreturntype,
                   v_cancelreturnstartdate,v_cancelreturnenddate,v_cancelfixusetimes,v_cancelchargeparty,
                   v_cancelfreetime,v_cancelcreatetime,v_cancelcanceltime,v_cancelfirstusetime,v_cancellastusetime,
                   v_canceltastestarttime,v_canceltasteendtime,v_cancelpausetime,v_cancelresumetime,
                   v_cancelsubscribeopid,v_cancelcancelopid,v_cancelcancelreason,v_cancellastrent,v_cancelmsisdntype,v_cancelpseudocode,v_canceloldproductid,
                   v_cancelpproductofferid,v_cancelvarifee,v_canceleventid,v_cancelisexperience,v_cancelsubscribetype,v_cancelchargetime
            from   zxdbm_200.s200_user_subscription
            where  usercode = v_usemsisdn and productid = v_beupdatedproductid;
        exception when no_data_found then
            o_retcode   := 1201;
            o_debugpos := 22;
            o_debuginfo := 'the subscription dose not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 23;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        v_cancelcanceltime := v_sysdate14;
        v_sql := 'insert into zxdbm_200.s200_user_subs_hist' || v_curstrmonth ||
        ' (subscriptionindex,subscriptionid,servicetype,subcapability,'||
        'serviceindex,serviceid,productindex,productid,contentindex,contentid,'||
        'productpkgindex,productpkgid,corpindex,corpid,cpindex,cpid,subscribemode,'||
        'subscribechannel,subscribeoptype,cancelmode,cancelchannel,canceloptype,'||
        'pausemode,resumemode,status,statustime,autocontinue,subscriber,usercode,'||
        'payuser,pushid,starttime,endtime,lastrenttime,nextrentdate,payfordate,'||
        'returnvalues,returntype,returnstartdate,returnenddate,fixusetimes,'||
        'chargeparty,freetime,createtime,canceltime,firstusetime,lastusetime,'||
        'tastestarttime,tasteendtime,pausetime,resumetime,subscribeopid,'||
        'cancelopid,cancelreason,lastrent,msisdntype,pseudocode,oldproductid,'||
        'pproductofferid,varifee,eventid,isexperience,subscribetype,chargetime) '||
        'values(:p1,:p2,:p3,:p4,'||
        ':p5,:p6,:p7,:p8,:p9,:p10,'||
        ':p11,:p12,:p13,:p14,:p15,:p16,:p17,'||
        ':p18,:p19,:p20,:p21,:p22,'||
        ':p23,:p24,:p25,:p26,:p27,:p28,'||
        ':p29,:p30,:p31,:p32,:p33,:p34,:p35,'||
        ':p36,:p37,:p38,:p39,:p40,'||
        ':p41,:p42,:p43,:p44,:p45,:p46,'||
        ':p47,:p48,:p49,:p50,:p51,'||
        ':p52,:p53,:p54,:p55,:p56,:p57,:p58,'||
        ':p59,:p60,:p61,:p62,:p63,:p64)';
        begin
        --插入历史订购关系
            execute immediate v_sql
            using   v_cancelproductsubindex,v_cancelproductsubid,v_cancelservicetype,v_cancelsubcapability,
                    v_cancelserviceindex,v_cancelserviceid,v_cancelproductindex,v_cancelproductid,v_cancelcontentindex,v_cancelcontentid,
                    v_cancelpropkgindex,v_cancelpropkgid,v_cancelcorpindex,v_cancelcorpid,v_cancelcpindex,v_cancelcpid,v_cancelsubscribemode,
                    v_cancelsubscribechannel,v_cancelsubscribeoptype,v_cancelcancelmode,v_cancelcancelchannel,v_canceloptype,
                    v_cancelpausemode,v_cancelresumemode,v_cancelsubstatus,v_cancelstatustime,v_cancelautocontinue,v_cancelsubscriber,v_cancelusercode,
                    v_cancelpayuser,v_cancelpushid,v_cancelsubstarttime,v_cancelsubendtime,v_cancellastrenttime,v_cancelnextrentdate,v_cancelpayfordate,
                    v_cancelreturnvalues,v_cancelreturntype,v_cancelreturnstartdate,v_cancelreturnenddate,v_cancelfixusetimes,
                    v_cancelchargeparty,v_cancelfreetime,v_cancelcreatetime,v_cancelcanceltime,v_cancelfirstusetime,v_cancellastusetime,
                    v_canceltastestarttime,v_canceltasteendtime,v_cancelpausetime,v_cancelresumetime,v_cancelsubscribeopid,
                    v_cancelcancelopid,v_cancelcancelreason,v_cancellastrent,v_cancelmsisdntype,v_cancelpseudocode,v_canceloldproductid,
                    v_cancelpproductofferid,v_cancelvarifee,v_canceleventid,v_cancelisexperience,v_cancelsubscribetype,v_cancelchargetime;
            if (sql%rowcount = 0) then
                o_retcode   := 506;
                o_debugpos := 24;
                o_debuginfo := 'no subs exist.';
                rollback;
                return;
            end if;
        exception when others then
            o_retcode   := 506;
            o_debugpos := 25;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            rollback;
            return;
        end;

        --生成新的订购关系ID
        zxdbm_ismp.sp_getmaxvalue ('slp_order_prdindex',1,v_productsubindex);
        if (v_productsubindex = 0) then
            o_retcode   := 506;
            o_debugpos := 26;
            o_debuginfo := 'get slp_order_prdindex failed.';
            rollback;
            return;
        end if;
        begin
            select trunc(dbms_random.value(10000,99999)) into v_retvalue from dual;
        exception when others then
            o_retcode   := 506;
            o_debugpos := 27;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            rollback;
            return;
        end;
        v_productsubid := v_usemsisdn || to_char(v_retvalue);
        --added by youqunxia20130605  order2.07.10
        --对于VSOP发起的订购请求,若ExtC5非空,则将其填入订购关系表
        if trim(i_pproductofferid) is null then
            v_pproductofferid := ' ';
        else
            v_pproductofferid := i_pproductofferid;
        end if;
        --added by youqunxia 20130814 Order3.02.01
        v_varifee := to_number(i_varifee);
        --如果eventid值非空,将其填入订购关系表
        if trim(i_eventid) is not null then
            v_eventid := i_eventid;
        else
            v_eventid := '';
        end if;
        --如果chargetime值非空,将其填入订购关系表
        if trim(i_chargetime) is not null then
            v_chargetime := i_chargetime;
        else
            v_chargetime := '00000000000000';
        end if;
        begin
            update zxdbm_200.s200_user_subscription
            set    subscriptionindex          = v_productsubindex,
                   subscriptionid             = v_productsubid,
                   servicetype                = v_servicetype,
                   subcapability              = v_subcapability,
                   serviceindex               = v_serviceindex,
                   serviceid                  = v_serviceid,
                   productindex               = v_productindex,
                   productid                  = v_productid,
                   productpkgindex            = v_propkgindex,
                   productpkgid               = v_propkgid,
                   contentindex               = v_contentindex,
                   contentid                  = v_contentid,
                   corpindex                  = v_corpindex,
                   corpid                     = v_corpid,
                   cpindex                    = v_cpindex,
                   cpid                       = v_cpid,
                   subscribemode              = v_subscribemode,
                   subscribechannel           = v_subscribechannel,
                   subscribeoptype            = v_subscribeoptype,
                   cancelmode                 = v_cancelmode,
                   cancelchannel              = v_cancelchannel,
                   canceloptype               = v_canceloptype,
                   pausemode                  = v_pausemode,
                   resumemode                 = v_resumemode,
                   status                     = v_substatus,
                   statustime                 = v_sysdate14,
                   autocontinue               = v_autocontinue,
                   subscriber                 = v_srcmsisdn,
                   --usercode                   = v_usemsisdn,
                   payuser                    = v_paymsisdn,
                   pushid                     = v_pushid,
                   starttime                  = v_substarttime,
                   endtime                    = v_subendtime,
                   lastrenttime               = v_lastrenttime,
                   nextrentdate               = v_nextrentdate,
                   payfordate                 = v_payfordate,
                   returnvalues               = v_returnvalues,
                   returntype                 = v_returntype,
                   returnstartdate            = v_returnstartdate,
                   returnenddate              = v_returnenddate,
                   --fixusetimes                = v_fixusetimes,
                   chargeparty                = v_chargeparty,
                   freetime                   = v_freetime,
                   createtime                 = v_sysdate14,
                   canceltime                 = v_canceltime,
                   firstusetime               = v_firstusetime,
                   lastusetime                = v_lastusetime,
                   tastestarttime             = v_tastestarttime,
                   tasteendtime               = v_tasteendtime,
                   pausetime                  = v_pausetime,
                   resumetime                 = v_resumetime,
                   subscribeopid              = v_subscribeopid,
                   cancelopid                 = v_cancelopid,
                   cancelreason               = v_cancelreason,
                   lastrent                   = v_lastrent,
                   msisdntype                 = v_msisdntype,
                   pseudocode                 = v_pseudocode,
                   oldproductid               = v_cancelproductid,
                   pproductofferid            = v_pproductofferid,
                   varifee                    = v_varifee,
                   eventid                    = v_eventid,
                   isexperience               = v_isexperience,
                   subscribetype              = v_subscribetype,
                   chargetime                 = v_chargetime
            where  usercode = v_usemsisdn and productid = v_beupdatedproductid;
        exception when others then
                o_retcode   := 506;
                o_debugpos  := 28;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                rollback;
                return;
        end;
    end if;
    if v_lastrent > 0 then
    --累计处理【消费累计;产品累计(纯包量)】
        v_sql := 'update zxdbm_ismp.ssrv_user_totalamount' || v_curstrmonth ||
                 ' set usedamounts  = usedamounts + :p1 where userindex = :p2' ||
                 ' and amounttype = 8 and amountindex = 0 and productindex = 0  and expiredate = :p3 and rownum = 1';
        begin
            execute immediate v_sql using v_lastrent,v_payuserindex,to_number(substr(v_curmonthendtime,1,8));
            if sql%rowcount = 0 then
                v_sql := 'insert into zxdbm_ismp.ssrv_user_totalamount' || v_curstrmonth ||
                         '(userindex,payuserindex,amounttype,usedamounts,amountindex,productindex,expiredate)' ||
                         ' values(:p1,:p2,8,:p3,0,0,:p4)';
                begin
                    execute immediate v_sql using v_payuserindex,v_payuserindex,v_lastrent,to_number(substr(v_curmonthendtime,1,8));
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 29;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    rollback;
                    return;
                end;
            end if;
        exception when others then
            o_retcode   := 506;
            o_debugpos := 30;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            rollback;
            return;
        end;
    end if;

/*
推广期内订购/免费期内订购,基本计费为包量/查询产品累计,有累计,则跳过。无累计新增一条记录
*/

    begin
        select popularizeitem,popularizestart,popularizestop,needcharge
        into v_popuitem,v_popustart,v_popustop,v_needcharge
        from zxdbm_200.s200_product
        where productindex = v_productindex;
    exception when others then
        o_retcode   := 506;
        o_debugpos := 31;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if v_needcharge = 1 then
        begin
            select chargetype,fixusetimes,unitvol
            into v_chargetype,v_fixusetimes,v_unitvol
            from zxdbm_200.s200_product_fee
            where productindex = v_productindex and itemindex = 0;
        exception when others then
            o_retcode   := 506;
            o_debugpos := 32;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            rollback;
            return;
        end;
        --基本计费为纯包量(包括含推广期)
        if (i_chargetype = 4) or (v_chargetype = 4) then
            v_fixtotalres := v_fixusetimes * v_unitvol;
            if (i_freetime > v_sysdate14) or (v_popuitem in (0,1) and (v_sysdate8 >= v_popustart) and (v_sysdate8 <= v_popustop)) then
                begin
                    select count(1)
                    into v_temp
                    from zxdbm_ismp.ssrv_user_totalamount
                    where  userindex = v_payuserindex and amounttype = 0 and amountindex = v_productindex and productindex = 0;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 33;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
            end if;
            if v_temp = 0 then
                begin
                    update zxdbm_ismp.ssrv_user_totalamount
                    set    fixtotalres = fixtotalres + v_fixtotalres,
                           expiredate = to_number(to_char(to_date(v_subendtime,'yyyymmddhh24miss'),'yyyymmdd'))
                    where  userindex = v_payuserindex and amounttype = 0 and amountindex = v_productindex and productindex = 0;
                    if (sql%rowcount = 0) then
                        begin
                            insert into zxdbm_ismp.ssrv_user_totalamount(userindex,amounttype,amountindex,productindex,payuserindex,fixtotalres,servicetype,expiredate)
                            values(v_payuserindex,0,v_productindex,0,v_payuserindex,v_fixtotalres,v_servicetype,to_number(to_char(to_date(v_subendtime,'yyyymmddhh24miss'),'yyyymmdd')));
                        exception when others then
                            o_retcode   := 506;
                            o_debugpos := 34;
                            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                            return;
                        end;
                    end if;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 35;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    rollback;
                    return;
                end;
            end if;
        end if;
    end if;
    commit;
    --事务结束
end;
o_productsubid          := v_productsubid;
o_porductsubindex       := v_productsubindex;

--added by youqunxia20130604  order2.07.10
if (v_freetime > v_substarttime) and (i_freenoticenow = 0) then
    --设置定时任务表中的号码类型
    if (v_msisdntype <> 1) then
        v_freemsisdntype := '4';
    end if;
    v_freenoticetime1 := to_char(to_date(v_freetime,'yyyymmddhh24miss'),'yyyymmdd') || '080000';
    if (v_freetime < v_freenoticetime1) then
        v_plantime := to_char(to_date(v_freetime,'yyyymmddhh24miss') - 1,'yyyymmdd');
        begin
            --清除定时任务表原有记录
            delete from zxdbm_ismp.freenotice_task
            where  usercode = v_usemsisdn and productid = v_productid;
            --插入定时任务
            insert into zxdbm_ismp.freenotice_task(plantime,createtime,usercode,msisdntype,spid,serviceid,productid)
            values(v_plantime, v_sysdate14, v_usemsisdn, v_freemsisdntype, v_cpid, v_serviceid, v_productid);
        exception when others then
            o_retcode   := 506;
            o_debugpos  := 36;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        commit;
    else
        begin
            --清除定时任务表原有记录
            delete from zxdbm_ismp.freenotice_task
            where  usercode = v_usemsisdn and productid = v_productid;
            --插入定时任务
            insert into zxdbm_ismp.freenotice_task(plantime,createtime,usercode,msisdntype,spid,serviceid,productid)
            values(substr(v_freetime,1,8), v_sysdate14, v_usemsisdn, v_freemsisdntype, v_cpid, v_serviceid, v_productid); 
        exception when others then
            o_retcode   := 506;
            o_debugpos  := 37;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        commit;
    end if;
end if;

end slp_200_telicomsubresp;
/

prompt zxdbm_200.slp_200_telicomusercheck
create or replace procedure zxdbm_200.slp_200_telicomusercheck
(

i_srcmsisdn             in          varchar2,            --发起方/赠送方用户号码
i_usemsisdn             in          varchar2,            --使用方/受赠方用户号码
i_oatype                in          number,              --发起方地址类型
i_servicetype           in          number,              --业务能力类型
i_cmdchannel            in          number,              --动作来源
i_needauthuser          in          number,              --是否需要鉴权用户 0-不需要 1-需要
i_isthird               in          number,              --0:普通订购 1:赠送
i_supportproductid      in          number,              --同步SP、SP下发使用service还是productid 0-serviceid 1-productid
i_srvprodid             in          varchar2,            --业务/产品代码
i_direct                in          number,              --订购方向 0:正向订购 1:SP反向定购
i_prepaidtype           in          number,              --预付费接口类型 1-OCS 2-非OCS 3-根据号段选择是OCS或者非OCS
i_checkpseudocode       in          number,              --系统开关:是否支持伪码   0-不支持   1-支持
i_serviceflowtype       in          number,              --业务流程类型
o_retcode               out         number,              --鉴权返回码 0代表成功,否则代表错误码
o_debugpos             out         number,              --出口顺序标识
o_debuginfo             out         varchar2,            --调试信息
o_srcmsisdntype         out         number,              --发起方号码类型,1:msisdn,2:phs,3:pstn
o_usemsisdntype         out         number,              --使用方号码类型,1:msisdn,2:phs,3:pstn
o_userindex             out         number,              --使用方号码序号
o_payuserindex          out         number,              --付费方号码序号
o_paytype               out         number,              --计费号码的付费类型 0:后付费 1:预付费 2:准预付费
o_oppaytype             out         number,              --话单专用付费类型
o_colortype             out         number,              --使用方号码名单类型   0-普通用户,1-黑名单,2-白名单,   3-灰名单,4-红名单
o_usertype              out         number,              --使用方类型  1-普通用户  2-运营商测试用户  3-SP测试用户
o_scptype               out         number,              --对预付费扣费的设备类型 1:OCS;2:非OCS
o_paymode               out         varchar2,            --集团付费模式 集团付费/个人付费
o_corpid                out         varchar2,            --集团代码
o_corpindex             out         number,              --集团序号
o_corpcnshortname       out         varchar2,            --集团名称
o_paymsisdn             out         varchar2,            --去掉前缀的计费方号码
o_pcodemsisdnswitch     out         number,               --伪码和真实号码之间是否有转换:  0否   1真实号码转化伪码 2伪码转化为真实号码
o_pseudocode            out         varchar2            --伪码

)
as

--新增下面几个关于时间的参数,尽量减少sysdate的调用,所有用到sysdate的地方,以to_date(v_sysdate14,'yyyymmddhh24miss')进行代替
v_sysdate14             char(14)     := to_char(sysdate ,'yyyymmddhh24miss');
v_sysdate8              char(8)      := substr(v_sysdate14,1,8);
v_curmonth              number(2)    := to_number(substr(v_sysdate14,5,2));     --临时存放当前月份
v_curstrmonth           char(2)      := lpad(to_char(v_curmonth),2,'0');        --临时存放当前月份,字符型

v_maincapability        number(10)                                          := 0;                                       --临时存放组合业务主业务能力类型
v_submaincapability     number(10)                                          := 0;                                       --临时存放组合业务主业务能力子类型
v_devexist              number(3)                                           := 1;                                       --临时存放是否存在对应设备
v_sql                   varchar2(3072)                                      := ' ';                                     --临时存放动态sql
v_tmpint                number(10)                                          := 0;                                       --临时存放临时数值

v_srcmsisdn             zxdbm_ismp.susr_basic.usercode%type                 := i_srcmsisdn;                                     --临时存放发起方/赠送方用户号码
v_usemsisdn             zxdbm_ismp.susr_basic.usercode%type                 := i_usemsisdn;                                     --临时存放使用方/受赠方用户号码
v_srcuserindex          zxdbm_ismp.susr_basic.userindex%type                := 0;                                       --临时存放发起方用户序号
v_srcusertype           zxdbm_ismp.susr_basic.usertype%type                 := 1;                                       --临时存放发起方用户类型
v_srcpaytype            zxdbm_ismp.susr_basic.paytype%type                  := 0;                                       --临时存放发起方用户付费类型
v_srcblacklist          zxdbm_ismp.susr_basic.blacklist%type                := 0;                                       --临时存放发起方是否进入黑名单
v_srcwhitelist          zxdbm_ismp.susr_basic.whitelist%type                := 0;                                       --临时存放发起方是否进入白名单
v_srcredlist            zxdbm_ismp.susr_basic.redlist%type                  := 0;                                       --临时存放发起方是否进入红名单
v_srcblacklevel         zxdbm_ismp.susr_basic.blacklevel%type               := 0;                                       --临时存放发起方黑名单级别
v_srcwhitelevel         zxdbm_ismp.susr_basic.whitelevel%type               := 0;                                       --临时存放发起方白名单级别
v_srcmsisdntype         zxdbm_ismp.susr_basic.msisdntype%type               := 1;                                       --临时存放发起方号码类型
v_srcuserstatus         zxdbm_ismp.susr_basic.status%type                   := 0;                                       --临时存放发起方状态
v_srcactivestatus       zxdbm_ismp.susr_basic.activestatus%type             := 0;                                       --临时存放发起方激活状态
v_srcoppaytype          zxdbm_ismp.susr_basic.oppaytype%type                := 0;                                       --临时存放发起方话单专用付费类型
v_srccorpindex          zxdbm_ismp.susr_basic.corpindex%type                := 0;                                       --临时存放发起方集团序号
v_srcusetime            zxdbm_ismp.susr_basic.lastusetime%type              := '00000000000000';                        --临时存放发起方上次使用时间
v_srcmaxlimitfee        zxdbm_ismp.susr_basic.maxlimitfee%type              := 0;                                       --临时存放发起方最大消费限额
v_srcgraylist           zxdbm_ismp.susr_basic.graylist%type                 := 0;                                       --临时存放发起方是否进入灰名单
v_srcgraylevel          zxdbm_ismp.susr_basic.graylevel%type                := 0;                                       --临时存放发起方灰名单级别
v_useuserindex          zxdbm_ismp.susr_basic.userindex%type                := 0;                                       --临时存放使用方用户序号
v_useusertype           zxdbm_ismp.susr_basic.usertype%type                 := 1;                                       --临时存放使用方用户类型
v_usepaytype            zxdbm_ismp.susr_basic.paytype%type                  := 0;                                       --临时存放使用方用户付费类型
v_useblacklist          zxdbm_ismp.susr_basic.blacklist%type                := 0;                                       --临时存放使用方是否进入黑名单
v_usewhitelist          zxdbm_ismp.susr_basic.whitelist%type                := 0;                                       --临时存放使用方是否进入白名单
v_useredlist            zxdbm_ismp.susr_basic.redlist%type                  := 0;                                       --临时存放使用方是否进入红名单
v_useblacklevel         zxdbm_ismp.susr_basic.blacklevel%type               := 0;                                       --临时存放使用方黑名单级别
v_usewhitelevel         zxdbm_ismp.susr_basic.whitelevel%type               := 0;                                       --临时存放使用方白名单级别
v_usemsisdntype         zxdbm_ismp.susr_basic.msisdntype%type               := 1;                                       --临时存放使用方号码类型
v_useuserstatus         zxdbm_ismp.susr_basic.status%type                   := 0;                                       --临时存放使用方状态
v_useactivestatus       zxdbm_ismp.susr_basic.activestatus%type             := 0;                                       --临时存放使用方激活状态
v_useoppaytype          zxdbm_ismp.susr_basic.oppaytype%type                := 0;                                       --临时存放使用方话单专用付费类型
v_usecorpindex          zxdbm_ismp.susr_basic.corpindex%type                := 0;                                       --临时存放使用方集团序号
v_useusetime            zxdbm_ismp.susr_basic.lastusetime%type              := '00000000000000';                        --临时存放使用方上次使用时间
v_usemaxlimitfee        zxdbm_ismp.susr_basic.maxlimitfee%type              := 0;                                       --临时存放使用方最大消费限额
v_usegraylist           zxdbm_ismp.susr_basic.graylist%type                 := 0;                                       --临时存放使用方是否进入灰名单
v_usegraylevel          zxdbm_ismp.susr_basic.graylevel%type                := 0;                                       --临时存放使用方灰名单级别
v_paymsisdn             zxdbm_ismp.susr_basic.usercode%type                 := ' ';                                     --临时存放计费方
v_paymsisdntype         zxdbm_ismp.susr_basic.msisdntype%type               := 1;                                       --临时存放计费方号码类型
v_cardtype              zxdbm_ismp.susr_basic.paytype%type                  := 0;                                       --临时存放用户付费类型0-后付费,1-预付费,2-准预付费
v_srctestcpindex        zxdbm_ismp.susr_testgroup_member.cpindex%type       := 0;                                       --临时存放CP/SP序号
v_usetestcpindex        zxdbm_ismp.susr_testgroup_member.cpindex%type       := 0;                                       --临时存放CP/SP序号
v_srccorpid             zxdbm_ismp.susr_corp.corpid%type                    := ' ';                                     --临时存放集团代码
v_srccorpstatus         zxdbm_ismp.susr_corp.status%type                    := 0;                                       --临时存放集团状态
v_srccorppaymsisdn      zxdbm_ismp.susr_corp.paymsisdn%type                 := ' ';                                     --临时存放集团付费号码
v_corpname              zxdbm_ismp.susr_corp.corpcnshortname%type           := ' ';                                     --临时存放集团中文名简写

v_productindex          zxdbm_200.s200_product.productindex%type            := 0;                                       --临时存放产品序号
v_needcharge            zxdbm_200.s200_product.needcharge%type              := 1;                                       --临时存放是否需要计费,0-免费,1-需要计费
v_onlyforcorp           zxdbm_200.s200_product.onlyforcorp%type             := 0;                                       --临时存放是否集团专用产品0:个人用户专用1:集团专用
v_cpindex               zxdbm_200.s200_product.cpindex%type                 := 0;                                       --临时存放CP/SP序号
v_serviceindex          zxdbm_200.s200_product.serviceindex%type            := 0;                                       --临时存放业务序号
v_servicetype           zxdbm_200.s200_product.servicetype%type             := 0;                                       --临时存放业务能力类型
v_subcapability         zxdbm_200.s200_product.subcapability%type           := 1;                                       --临时存放业务能力子类型1-3G,2-PHS,3-ISAG,默认为3g业务能力(仅电信使用,其他营运商皆默认为1)
v_srvprodid             zxdbm_200.s200_product.productid%type               := i_srvprodid;                             --临时存放业务产品代码
v_productstatus         zxdbm_200.s200_product.status%type                  := 0;                                       --临时存放产品状态
v_onlyforpkg            zxdbm_200.s200_product.onlyforpkg%type              := 0;                                       --临时存放是否套餐专用产品
v_ordertype             zxdbm_200.s200_product.ordertype%type               := 1;                                       --临时存放产品订购类型
v_workflowlife          zxdbm_200.s200_product.workflowlife%type            := 0;                                       --临时存放工作流流程状态
v_productsource         zxdbm_200.s200_product.productsource%type           := 0;                                       --产品来源0:ISMP 发布的产品1:同步自全国SPMS 的产品2:提供给漫游用户的产品

v_serviceid             zxdbm_200.s200_service.serviceid%type               := ' ';                                     --临时存放业务代码
v_servicestatus         zxdbm_200.s200_service.status%type                  := 0;                                       --临时存放业务状态
v_srvcolorlist          zxdbm_200.s200_service.colorlist%type               := 0;                                       --临时存放是否有颜色列表,0-无,1-黑名单,2-白名单
v_servicename           zxdbm_200.s200_service.servicename%type             := ' ';                                     --临时存放业务名称
v_subsrvtype            zxdbm_200.s200_service.subsrvtype%type              := 0;                                       --临时存放业务子类型
v_orderupdateurl        zxdbm_200.s200_service.orderupdateurl%type          := ' ';                                     --临时存放业务表中订购关系更新通知URL
v_useopenstatus         zxdbm_200.s200_service_useropen.status%type         := 1;                                       --临时存放使用方个人能力开通,0-关闭,1-开通
v_perssrvopen           zxdbm_ismp.ssys_service_type.perssrvopen%type       := 1;                                       --临时存放个人能力缺省开通,0-关闭,1-开通
v_srvtypeid             zxdbm_ismp.ssys_service_type.srvtypeid%type         := ' ';                                     --临时存放话单中的srvtypeid
v_srcopenstatus         zxdbm_200.s200_service_useropen.status%type         := 1;                                       --临时存放发起方个人能力开通,0-关闭,1-开通

v_productsubmode        zxdbm_ismp.ssrv_corp_product.subscribemode%type     := 0;                                       --临时存放定购方式,0-必须集团管理员定购,1-集团管理员与集团成员均可定购
v_productpayparty       zxdbm_ismp.ssrv_corp_product.payparty%type          := 2;                                       --临时存放集团专用产品付费方 1-集团付费 2-个人付费
v_corpproductstatus     zxdbm_ismp.ssrv_corp_product.status%type            := 0;                                       --临时存放集团与产品关联状态 0-正常;1-删除
v_phoneindex            zxdbm_ismp.ssys_phone.phoneindex%type               := 0;                                       --临时存放号段序号
v_phoneindexa           zxdbm_ismp.ssys_phone.phoneindex%type               := 0;                                       --临时存放A省号段序号,跨省赠送
v_phoneindexb           zxdbm_ismp.ssys_phone.phoneindex%type               := 0;                                       --临时存放B省号段序号,跨省赠送
v_attach                zxdbm_ismp.ssys_phone.attach%type                   := 0;                                       --临时存放预付费归属,0-未知,1-OCS,2-RTBP
v_cpid                  zxdbm_ismp.scp_basic.cpid%type                      := ' ';                                     --临时存放CP/SP代码
v_cpstatus              zxdbm_ismp.scp_basic.status%type                    := 0;                                       --临时存放CP/SP代码
v_cpsubscribeurl        zxdbm_ismp.scp_basic.cpsubscribeurl%type            := ' ';                                     --临时存放CP/SP订购URL
v_hasblacklist          zxdbm_ismp.scp_basic.hasblacklist%type              := 0;                                       --临时存放是否有黑名单控制 0-无,1-有
v_cptype                zxdbm_ismp.scp_basic.cptype%type                    := 2;                                       --临时存放CP/SP类型
v_cpsrvtype             zxdbm_ismp.scp_basic.cpsrvtype%type                 := 0;                                       --临时存放CP服务类型
v_insubblacklist        zxdbm_ismp.scp_basic.insubblacklist%type            := 0;                                      --临时存放限制订购SP黑名单标识
v_cpcnshortname         zxdbm_ismp.scp_basic.cpcnshortname%type             := ' ';                                     --临时存放CP/SP中文名简写
v_customersrvtel        zxdbm_ismp.scp_basic.customersrvtel%type            := ' ';                                     --临时存放CP/SP客服电话

begin

--出参初始化
o_retcode               := 0;
o_debugpos             := 0;
o_debuginfo             := ' ';
o_paymsisdn             := ' ';
o_srcmsisdntype         := 1;
o_usemsisdntype         := 1;
o_userindex             := 0;
o_payuserindex          := 0;
o_paytype               := 0;
o_oppaytype             := 0;
o_scptype               := 2;
o_colortype             := 0;
o_usertype              := 0;
o_corpid                := ' ';
o_corpindex             := 0;
o_corpcnshortname       := ' ';
o_paymode               := '个人付费';
o_pcodemsisdnswitch     := 0;
o_pseudocode            := '';

/
*1.用户鉴权
*/

--发起方和使用方存在性鉴权
if (i_needauthuser = 1) then
    if (i_isthird = 1) and (v_srcmsisdn <> v_usemsisdn) then
        begin
            select a.userindex,a.usertype,a.paytype,a.blacklist,a.whitelist,a.redlist,a.blacklevel,a.whitelevel,a.msisdntype,a.status,a.activestatus,a.oppaytype,a.corpindex,a.lastusetime,a.maxlimitfee,a.graylist,a.graylevel,
                   b.userindex,b.usertype,b.paytype,b.blacklist,b.whitelist,b.redlist,b.blacklevel,b.whitelevel,b.msisdntype,b.status,b.activestatus,b.oppaytype,b.corpindex,b.lastusetime,b.maxlimitfee,b.graylist,b.graylevel
            into   v_srcuserindex,v_srcusertype,v_srcpaytype,v_srcblacklist,v_srcwhitelist,v_srcredlist,v_srcblacklevel,v_srcwhitelevel,v_srcmsisdntype,v_srcuserstatus,v_srcactivestatus,v_srcoppaytype,v_srccorpindex,v_srcusetime,v_srcmaxlimitfee,v_srcgraylist,v_srcgraylevel,
                   v_useuserindex,v_useusertype,v_usepaytype,v_useblacklist,v_usewhitelist,v_useredlist,v_useblacklevel,v_usewhitelevel,v_usemsisdntype,v_useuserstatus,v_useactivestatus,v_useoppaytype,v_usecorpindex,v_useusetime,v_usemaxlimitfee,v_usegraylist,v_usegraylevel
            from   zxdbm_ismp.susr_basic a, zxdbm_ismp.susr_basic b
            where  a.usercode = v_srcmsisdn and b.usercode = v_usemsisdn;
        exception when no_data_found then
            o_retcode   := 1001;    --用户不存在
            o_debugpos := 1;
            o_debuginfo := 'v_srcmsisdn or v_usemsisdn is not exist.';
            if (i_oatype = 4) then
                o_srcmsisdntype := 2;
            else
                o_srcmsisdntype := 1;
            end if;
            o_usemsisdntype := 1;
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 2;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            if (i_oatype = 4) then
                o_srcmsisdntype := 2;
            else
                o_srcmsisdntype := 1;
            end if;
            o_usemsisdntype := 1;
            return;
        end;
        o_srcmsisdntype := v_srcmsisdntype;
        o_usemsisdntype := v_usemsisdntype;
        o_userindex     := v_useuserindex;
        o_payuserindex  := v_srcuserindex;
        o_paytype       := v_srcpaytype;
        o_oppaytype     := v_srcoppaytype;
        o_usertype      := v_useusertype;

       --批量操作用户号码类型由用户表查询后鉴权
        if (i_cmdchannel in (7,32)) then
            if (v_srcmsisdntype not in (1,2,3)) or  (v_usemsisdntype not in (1,2,3)) then
                o_retcode   := 107;
                o_debugpos := 3;
                o_debuginfo := 'v_srcmsisdntype='||v_srcmsisdntype||', v_usemsisdntype='||v_usemsisdntype||', v_srcmsisdntype or v_usemsisdntype not in (1,2,3,4).';
                return;
            end if;
        end if;
        --发起方状态鉴权
        if v_srcuserstatus in (1,3) then
            o_retcode   := 1002;
            o_debugpos := 4;
            o_debuginfo := 'v_srcuserstatus='||v_srcuserstatus||', the subscriber status is 1 or 3.';
            return;
        elsif v_srcuserstatus = 4 then
            o_retcode   := 1008;
            o_debugpos := 5;
            o_debuginfo := 'v_srcuserstatus='||v_srcuserstatus||', the subscriber status is 4.';
            return;
        end if;
        --使用方状态鉴权
        if v_useuserstatus in (1,3) then
            o_retcode   := 1110;
            o_debugpos := 6;
            o_debuginfo := 'v_useuserstatus='||v_useuserstatus||', the user status is 1 or 3.';
            return;
        elsif v_useuserstatus = 4 then
            o_retcode   := 1110;
            o_debugpos := 7;
            o_debuginfo := 'v_useuserstatus='||v_useuserstatus||', the user status is 4.';
            return;
        end if;
    else
        begin
            select userindex,usertype,paytype,blacklist,whitelist,redlist,blacklevel,whitelevel,msisdntype,status,activestatus,oppaytype,corpindex,lastusetime,maxlimitfee,graylist,graylevel
            into   v_srcuserindex,v_srcusertype,v_srcpaytype,v_srcblacklist,v_srcwhitelist,v_srcredlist,v_srcblacklevel,v_srcwhitelevel,v_srcmsisdntype,v_srcuserstatus,v_srcactivestatus,v_srcoppaytype,v_srccorpindex,v_srcusetime,v_srcmaxlimitfee,v_srcgraylist,v_srcgraylevel
            from   zxdbm_ismp.susr_basic
            where  usercode = v_srcmsisdn;
        exception when no_data_found then
            o_retcode   := 1001;    --用户不存在
            o_debugpos := 8;
            o_debuginfo := 'v_srcmsisdn='||v_srcmsisdn||', the user is not exist.';
            if (i_oatype = 4) then
                o_srcmsisdntype := 2;
            else
                o_srcmsisdntype := 1;
            end if;
            o_usemsisdntype := o_srcmsisdntype;
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 9;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            if (i_oatype = 4) then
                o_srcmsisdntype := 2;
            else
                o_srcmsisdntype := 1;
            end if;
            o_usemsisdntype := o_srcmsisdntype;
            return;
        end;
        v_useuserindex    := v_srcuserindex;
        v_useusertype     := v_srcusertype;
        v_usepaytype      := v_srcpaytype;
        v_useblacklist    := v_srcblacklist;
        v_usewhitelist    := v_srcwhitelist;
        v_useredlist      := v_srcredlist;
        v_useblacklevel   := v_srcblacklevel;
        v_usewhitelevel   := v_srcwhitelevel;
        v_usemsisdntype   := v_srcmsisdntype;
        v_useuserstatus   := v_srcuserstatus;
        v_useactivestatus := v_srcactivestatus;
        v_useoppaytype    := v_srcoppaytype;
        v_usecorpindex    := v_srccorpindex;
        v_useusetime      := v_srcusetime;
        v_usemaxlimitfee  := v_srcmaxlimitfee;
        v_usegraylist     := v_srcgraylist;
        v_usegraylevel    := v_srcgraylevel;

        o_srcmsisdntype := v_srcmsisdntype;
        o_usemsisdntype := v_usemsisdntype;
        o_userindex     := v_useuserindex;
        o_payuserindex  := v_srcuserindex;
        o_paytype       := v_srcpaytype;
        o_oppaytype     := v_srcoppaytype;
        o_usertype      := v_useusertype;

        --add by cfz 2008.08.20 批量操作用户号码类型由用户表查询后鉴权
        if (i_cmdchannel in (7,32)) then
            if v_srcmsisdntype not in (1,2,3) then
                o_retcode   := 107;
                o_debugpos := 10;
                o_debuginfo := 'v_srcmsisdntype='||v_srcmsisdntype||', v_srcmsisdntype is not valid.';
                return;
            end if;
        end if;
       --发起方状态鉴权
        if v_srcuserstatus in (1,3) then
            o_retcode   := 1002;
            o_debugpos := 11;
            o_debuginfo := 'v_srcuserstatus='||v_srcuserstatus||', the user status is 1 or 3.';
            return;
        elsif v_srcuserstatus = 4 then
            o_retcode   := 1008;
            o_debugpos := 12;
            o_debuginfo := 'v_srcuserstatus='||v_srcuserstatus||', the user status is 4.';
            return;
        end if;
    end if;
end if;

/
2.产品,产品费率鉴权
/

--产品存在性鉴权
if (i_direct = 1) and (i_supportproductid = 0) then
--反向订购,并且支持业务代码
    begin
        select productindex,needcharge,onlyforcorp,cpindex,serviceindex,
               servicetype,subcapability,onlyforpkg,status,workflowlife,productsource
        into   v_productindex,v_needcharge,v_onlyforcorp,v_cpindex,v_serviceindex,
               v_servicetype,v_subcapability,v_onlyforpkg,v_productstatus,v_workflowlife,v_productsource
        from   zxdbm_200.s200_product
        where  serviceid = v_srvprodid and ordertype = 1;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 13;
        o_debuginfo := 'serviceid='||v_srvprodid||', the product does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 14;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
else
    begin
        select productindex,needcharge,onlyforcorp,cpindex,serviceindex,
               servicetype,subcapability,onlyforpkg,status,workflowlife,productsource
        into   v_productindex,v_needcharge,v_onlyforcorp,v_cpindex,v_serviceindex,
               v_servicetype,v_subcapability,v_onlyforpkg,v_productstatus,v_workflowlife,v_productsource
        from   zxdbm_200.s200_product
        where  productid = v_srvprodid and ordertype = 1;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 15;
        o_debuginfo := 'productid='||v_srvprodid||', the product does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 16;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

if (v_needcharge = 1) then
    --欠费用户只能订购免费产品
    if (v_srcuserstatus = 2) then
        o_retcode   := 1003;
        o_debugpos := 17;
        o_debuginfo := 'v_srcuserstatus='||v_srcuserstatus||', the user owes money, does not allow to subscribe the product.';
        return;
    end if;
end if;

/**
3.业务鉴权
*/

--业务存在性鉴权(MPSS不需要鉴权)
--modified by chenfuzhong 2008.10.09
if i_servicetype = 101 then
    v_sql := 'select serviceid,status,colorlist,servicename,maincapability,submaincapability,cpid,orderupdateurl '||
             'from zxdbm_9900.s9900_service where  serviceindex = :p1';
    begin
        execute immediate v_sql into v_serviceid,v_servicestatus,v_srvcolorlist,v_servicename,v_maincapability,v_submaincapability,v_cpid,v_orderupdateurl using v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 18;
        o_debuginfo := 'serviceindex='||v_serviceindex||', the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 19;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
elsif i_servicetype in (1,2,3) then
    v_sql := 'select serviceid,status,colorlist,servicename,subsrvtype,cpid,orderupdateurl '||
             'from zxdbm_200.s200_service where  serviceindex = :p1';
    begin
        execute immediate v_sql into v_serviceid,v_servicestatus,v_srvcolorlist,v_servicename,v_subsrvtype,v_cpid,v_orderupdateurl using v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 20;
        o_debuginfo := 'serviceindex='||v_serviceindex||', the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 21;
        return;
    end;
elsif i_servicetype <> 14 then
    begin
        select serviceid,status,colorlist,servicename,subsrvtype,cpid,orderupdateurl
        into   v_serviceid,v_servicestatus,v_srvcolorlist,v_servicename,v_subsrvtype,v_cpid,v_orderupdateurl
        from   zxdbm_200.s200_service
        where  serviceindex = v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 22;
        o_debuginfo := 'serviceindex='||v_serviceindex||', the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 23;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

/**
4.CP/SP鉴权
**/

--CP/SP存在性鉴权
begin
    select cpid,status,cpsubscribeurl,hasblacklist,cptype,insubblacklist,cpcnshortname,customersrvtel,cpsrvtype
    into   v_cpid,v_cpstatus,v_cpsubscribeurl,v_hasblacklist,v_cptype,v_insubblacklist,v_cpcnshortname,v_customersrvtel,v_cpsrvtype
    from   zxdbm_ismp.scp_basic
    where  cpindex = v_cpindex;
exception when no_data_found then
    o_retcode   := 2000;
    o_debugpos := 24;
    o_debuginfo := 'serviceindex='||v_serviceindex||', the service does not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 25;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;

/*
5.业务能力开通鉴权
*/

if i_servicetype = 101 then
    begin
        select a.perssrvopen,b.srvtypeid
        into   v_perssrvopen,v_srvtypeid
        from   zxdbm_ismp.ssys_service_type a,zxdbm_ismp.ssys_service_type b
        where  a.servicetype = v_servicetype and a.subcapability = v_subcapability
        and    b.servicetype = v_maincapability and b.subcapability = v_submaincapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 26;
        o_debuginfo := 'servicetype='||v_servicetype||', subcapability='||v_subcapability||', maincapability='||v_maincapability||', submaincapability='||v_submaincapability||', CPX system serviceCapability is not open.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 27;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
else
    begin
        select perssrvopen,srvtypeid
        into   v_perssrvopen,v_srvtypeid
        from   zxdbm_ismp.ssys_service_type
        where  servicetype = v_servicetype and subcapability = v_subcapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 28;
        o_debuginfo := 'servicetype='||v_servicetype||', subcapability='||v_subcapability||', System serviceCapability is not open.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 29;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

/**
6.名单鉴权,用户业务能力开通鉴权
**/

if (i_needauthuser = 1) then
    --使用方名单鉴权
    if (v_useblacklist <> 0) then
        if bitand(v_useblacklevel,1) > 0 then
            --系统级黑名单检查
            v_tmpint    := 0;
            begin
                select count(*)
                into   v_tmpint
                from   zxdbm_ismp.susr_colorlist
                where  userindex = v_useuserindex and colortype = 1 and colorlevel = 1
                and inuretime <= v_sysdate14 and abatetime >= v_sysdate14;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 30;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if (v_tmpint > 0) then
                o_retcode   := 1004;
                o_debugpos := 31;
                o_colortype := 1;
                o_debuginfo := 'userindex = '||v_useuserindex||', the user is in system level blacklist.';
                return;
            end if;
        end if;
        --SP级黑名单检查
        if v_hasblacklist = 1 and bitand(v_useblacklevel,2) > 0 then
            v_tmpint := 0;
            begin
                select count(*)
                into   v_tmpint
                from   zxdbm_ismp.susr_colorlist
                where  userindex = v_useuserindex and colortype = 1 and colorlevel = 2 and colorid = v_cpindex
                and inuretime <= v_sysdate14 and abatetime >= v_sysdate14;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 32;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if v_tmpint > 0 then
                o_retcode   := 1004;
                o_debugpos := 33;
                o_colortype := 1;
                o_debuginfo := 'colorid = '||v_cpindex||', the user is in SP level blacklist.';
                return;
            end if;
        end if;
        --业务能力级黑名单
        if bitand(v_useblacklevel,4) > 0 then
            v_tmpint := 0;
            begin
                select count(*)
                into   v_tmpint
                from   zxdbm_ismp.susr_colorlist
                where  userindex = v_useuserindex and colortype = 1 and colorlevel = 3 and colorid = v_servicetype;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 34;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if v_tmpint > 0 then
                if v_usegraylist > 0 and bitand(v_usegraylevel,2) > 0 then
                    begin
                        select count(*)
                        into   v_tmpint
                        from   zxdbm_ismp.susr_colorlist
                        where  userindex = v_useuserindex and colortype = 3 and colorlevel = 2 and colorid = v_cpindex;
                    exception when others then
                        o_retcode   := 506;
                        o_debugpos := 35;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        return;
                    end;
                    if (v_tmpint = 0) then
                        o_retcode   := 1004;
                        o_debugpos := 36;
                        o_colortype := 1;
                        o_debuginfo := 'colorid = '||v_servicetype||', the user is in serviceCapbility level blacklist.';
                        return;
                    end if;
                else
                    o_retcode   := 1004;
                    o_debugpos := 37;
                    o_debuginfo := 'colorid = '||v_servicetype||', the user is in serviceCapbility level blacklist.';
                    o_colortype := 1;
                    return;
                end if;
            end if;
        end if;
        if v_srvcolorlist = 1 and bitand(v_useblacklevel,8) > 0 then
            --业务级黑名单检查
            v_tmpint := 0;
            begin
                select count(1)
                into v_tmpint
                from zxdbm_ismp.susr_colorlist
                where userindex = v_useuserindex and colortype = 1 and colorlevel = 4 and colorid = v_serviceindex;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 38;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if v_tmpint > 0 then
                o_retcode   := 1004;
                o_debugpos := 39;
                o_debuginfo := 'colorid = '||v_serviceindex||', the user is in service level blacklist.';
                o_colortype := 1;
                return;
            end if;
        end if;
    end if;
    if (v_srvcolorlist = 2) then
        if (v_usewhitelist = 0) then
            o_retcode   := 1100001;
            o_debugpos := 40;
            o_debuginfo := 'v_srvcolorlist = '||v_srvcolorlist||', v_usewhitelist = '||v_usewhitelist||', user is not in whitelist.';
            return;
        end if;
        o_colortype := 2;
        v_tmpint := 0;
        begin
            select count(1)
            into   v_tmpint
            from   zxdbm_ismp.susr_colorlist
            where  userindex = v_useuserindex and colortype = 2 and colorlevel = 4 and colorid = v_serviceindex;
        exception when others then
            o_retcode   := 506;
            o_debugpos := 41;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        if v_tmpint = 0 then
            o_retcode   := 1109;
            o_debugpos := 42;
            o_debuginfo := 'userindex = '||v_useuserindex||', colorid = '||v_serviceindex||', user is not in service level whitelist.';
            return;
        end if;
    end if;
    if (v_useredlist <> 0) then
        o_colortype := 4;
    end if;
    --使用方开通业务能力鉴权
    begin
        select nvl(status,1)
        into   v_useopenstatus
        from   zxdbm_200.s200_service_useropen
        where  userindex = v_useuserindex and servicetype = v_servicetype;
    exception when no_data_found then
        if (v_perssrvopen = 0) then
            o_retcode   := 1100;
            o_debugpos := 43;
            o_debuginfo := 'v_useopenstatus = '||v_useopenstatus||', v_perssrvopen = '||v_perssrvopen||', the service capabilty is closed.';
            return;
        end if;
    when others then
        o_retcode   := 506;
        o_debugpos := 44;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if (v_useopenstatus = 0) then
        if i_cmdchannel in (9,35,39,40) then
            o_retcode  := 1001225;
        else
            o_retcode  := 1100;
        end if;
        o_debugpos := 45;
        o_debuginfo := 'v_useopenstatus = '||v_useopenstatus||', the user service capabilty is closed.';
        return;
    end if;
    if (v_srcmsisdn <> v_usemsisdn) then
    --发起方名单鉴权
        if (v_srcblacklist <> 0) then
            if bitand(v_srcblacklevel,1) > 0 then
                --系统级黑名单检查
                v_tmpint    := 0;
                begin
                    select count(*)
                    into   v_tmpint
                    from   zxdbm_ismp.susr_colorlist
                    where  userindex = v_useuserindex and colortype = 1 and colorlevel = 1
                    and inuretime <= v_sysdate14 and abatetime >= v_sysdate14;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 46;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
                if (v_tmpint > 0) then
                    o_retcode   := 1004;
                    o_debugpos := 47;
                    o_debuginfo := 'userindex = '||v_useuserindex||', the user is in system level blacklist.';
                    return;
                end if;
            end if;
            --SP级黑名单检查
            if v_hasblacklist = 1 and bitand(v_srcblacklevel,2) > 0 then
                v_tmpint := 0;
                begin
                    select count(*)
                    into   v_tmpint
                    from   zxdbm_ismp.susr_colorlist
                    where  userindex = v_srcuserindex and colortype = 1 and colorlevel = 2 and colorid = v_cpindex
                    and inuretime <= v_sysdate14 and abatetime >= v_sysdate14;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 48;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
                if v_tmpint > 0 then
                    o_retcode   := 1004;
                    o_debugpos := 49;
                    o_debuginfo := 'userindex = '||v_srcuserindex||', colorid = '||v_cpindex||', the user is in SP level blacklist.';
                    return;
                end if;
            end if;
            --业务能力级黑名单
            if bitand(v_srcblacklevel,4) > 0 then
                v_tmpint := 0;
                begin
                    select count(*)
                    into   v_tmpint
                    from   zxdbm_ismp.susr_colorlist
                    where  userindex = v_srcuserindex and colortype = 1 and colorlevel = 3 and colorid = v_servicetype;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 50;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
                if v_tmpint > 0 then
                    if v_srcgraylist > 0 and bitand(v_srcgraylevel,2) > 0 then
                        begin
                            select count(*)
                            into   v_tmpint
                            from   zxdbm_ismp.susr_colorlist
                            where  userindex = v_srcuserindex and colortype = 3 and colorlevel = 2 and colorid = v_cpindex;
                        exception when others then
                            o_retcode   := 506;
                            o_debugpos := 51;
                            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                            return;
                        end;
                        if (v_tmpint = 0) then
                            o_colortype := 1;
                            o_retcode   := 1004;
                            o_debugpos := 52;
                            o_debuginfo := 'userindex = '||v_srcuserindex||', colorid = '||v_servicetype||', te user is in service capbility level blacklist.';
                            return;
                        end if;
                    else
                        o_retcode   := 1004;
                        o_debugpos := 53;
                        o_debuginfo := 'userindex = '||v_srcuserindex||', colorid = '||v_servicetype||', the user is in service capbility level blacklist.';
                        o_colortype := 1;
                        return;
                    end if;
                end if;
            end if;
            if v_srvcolorlist = 1 and bitand(v_srcblacklevel,8) > 0 then
                --业务级黑名单检查
                v_tmpint := 0;
                begin
                    select count(1)
                    into v_tmpint
                    from  zxdbm_ismp.susr_colorlist
                    where userindex = v_srcuserindex and colortype = 1 and colorlevel = 4 and colorid = v_serviceindex;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 54;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
                if v_tmpint > 0 then
                    o_retcode   := 1004;
                    o_debugpos := 55;
                    o_debuginfo := 'userindex = '||v_srcuserindex||', colorid = '||v_serviceindex||', the user is in service level blacklist.';
                    return;
                end if;
            end if;
        end if;
        if (v_srvcolorlist = 2) then
            if (v_srcwhitelist = 0) then
                o_retcode   := 1100001;
                o_debugpos := 56;
                o_debuginfo := 'v_srvcolorlist = '||v_srvcolorlist||', v_srcwhitelist = '||v_srcwhitelist||', the user is not in whitelist.';
                return;
            end if;
            v_tmpint := 0;
            begin
                select count(*)
                into   v_tmpint
                from   zxdbm_ismp.susr_colorlist
                where  userindex = v_srcuserindex and colortype = 2 and colorlevel = 4 and colorid = v_serviceindex;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 57;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if v_tmpint = 0 then
                o_retcode   := 1109;
                o_debugpos := 58;
                o_debuginfo := 'v_srvcolorlist = '||v_srvcolorlist||',the user is not in service level whitelist.';
                return;
            end if;
        end if;
        --发起方开通业务能力鉴权
        begin
            select nvl(status,1)
            into   v_srcopenstatus
            from   zxdbm_200.s200_service_useropen
            where  userindex = v_srcuserindex and servicetype = v_servicetype;
        exception when no_data_found then
            if (v_perssrvopen = 0) then
                o_retcode   := 1100;
                o_debugpos := 59;
                o_debuginfo := 'userindex = '||v_srcuserindex||', servicetype = '||v_servicetype||', the user service capabilty is not exist, and  system service capabiltyis is closed.';
                return;
            end if;
        when others then
            o_retcode   := 506;
            o_debugpos := 60;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        if (v_srcopenstatus = 0) then
            if i_cmdchannel in (9,35,39,40) then
                o_retcode  := 1001225;
            else
                o_retcode  := 1100;
            end if;
            o_debugpos := 61;
            o_debuginfo := 'userindex = '||v_srcuserindex||', servicetype = '||v_servicetype||', the user service capabilty is closed.';
            return;
        end if;
    end if;
end if;

/
*测试产品,测试用户鉴权
*/

--测试产品鉴权
-- if (v_productstatus in (0,2)) and (v_workflowlife = 3) then
if (((v_productsource <> 3) and (v_productstatus in (0,2)) and (v_workflowlife = 3)) or ((v_productsource = 3) and (v_productstatus = 0))) then
    if (v_srcusertype <> 2) or (v_useusertype <> 2) then
        o_retcode   := 1100;
        o_debugpos := 62;
        o_debuginfo := 'The normal user can not use test product.';
        return;
    end if;
    --SP测试用户鉴权
    --测试产品、测试用户(发起方、使用方)相关鉴权
    begin
        select cpindex
        into   v_srctestcpindex
        from   zxdbm_ismp.susr_testgroup_member
        where  userindex = v_srcuserindex;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 63;
        o_debuginfo := 'userindex = '||v_srcuserindex||', the test product is not open to the subscriber.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 64;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if (v_srctestcpindex > 0) then
        if (v_srctestcpindex <> v_cpindex) then
            o_retcode   := 1001223;
            o_debugpos := 65;
            o_debuginfo := 'The test-user belongs to SP-A can not use test-product belonged to SP-B.';
            return;
        end if;
    end if;
    begin
        select cpindex
        into   v_usetestcpindex
        from   zxdbm_ismp.susr_testgroup_member
        where  userindex = v_useuserindex;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 66;
        o_debuginfo := 'userindex = '||v_useuserindex||', the test product is not open to the subscriber.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 67;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if (v_usetestcpindex > 0) then
        if (v_usetestcpindex <> v_cpindex) then
            o_retcode   := 1001223;
            o_debugpos := 68;
            o_debuginfo := 'The test-user belongs to SP-A can not use test-product belonged to SP-B.';
            return;
        end if;
        o_usertype := 3;
    end if;
end if;

/*
集团相关鉴权
/

--集团产品、集团用户相关鉴权
if (v_onlyforcorp = 1) then
    --集团产品不允许赠送
    if (i_isthird = 1) then
        o_retcode    := 1011407;
        o_debugpos  := 69;
        o_debuginfo := 'v_onlyforcorp = '||v_onlyforcorp||', i_isthird = '||i_isthird||',the corp product can not to be presented.';
        return;
    end if;
    --普通用户不能订购一个集团专用产品
    if (v_srccorpindex <> v_usecorpindex) then
        o_retcode   := 1100;
        o_debugpos := 70;
        o_debuginfo := 'v_onlyforcorp = '||v_onlyforcorp||', v_srccorpindex = '||v_srccorpindex||', the normal user can not subscribe corp product.';
        return;
    end if;
    if (v_srccorpindex = 0) then
        o_retcode   := 1204;
        o_debugpos := 71;
        o_debuginfo := 'v_onlyforcorp = '||v_onlyforcorp||', v_srccorpindex = '||v_srccorpindex||', the normal user can not subscribe corp product.';
        return;
    end if;
    begin
        select  corpid,status,paymsisdn,corpcnshortname
        into    v_srccorpid,v_srccorpstatus,v_srccorppaymsisdn,v_corpname
        from    zxdbm_ismp.susr_corp
        where   corpindex = v_srccorpindex;
    exception when no_data_found then
        o_retcode   := 1001301;  --集团不存在
        o_debugpos := 72;
        o_debuginfo := 'v_srccorpindex = '||v_srccorpindex||', the corp is not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 73;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;

    o_corpcnshortname  := v_corpname;
    o_corpid           := v_srccorpid;
    o_corpindex        := v_srccorpindex;

    if (v_srccorpstatus <> 1) then
        o_retcode   := 1001302;
        o_debugpos := 74;
        o_debuginfo := 'v_srccorpindex = '||v_srccorpindex||', the Corp status is abnormal.';
        return;
    end if;
    --一个集团的产品只能由本集团的用户订购/赠送
    begin
        select subscribemode,payparty,status
        into   v_productsubmode,v_productpayparty,v_corpproductstatus
        from   zxdbm_ismp.ssrv_corp_product
        where  corpindex = v_srccorpindex and groupindex = 0 and productindex = v_productindex;
    exception when no_data_found then
        o_retcode   := 1011300;
        o_debugpos := 75;
        o_debuginfo := 'productindex = '||v_productindex||', corpindex = '||v_srccorpindex||', the product is not binded with the corp.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 76;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if (v_corpproductstatus <> 0) then
        o_retcode   := 1011408;
        o_debugpos := 77;
        o_debuginfo := 'productindex = '||v_productindex||', corpindex = '||v_srccorpindex||', the binding relationship between the product and the corp is deleted.';
        return;
    end if;
    if (v_productsubmode = 0) then
    --必须集团管理员订购
        if (i_cmdchannel <> 4) then
            o_retcode   := 1001216;    --无权定购
            o_debugpos := 78;
            o_debuginfo := 'v_productsubmode = '||v_productsubmode||', the corp product can only be subscribed by Corp managers.';
            return;
        end if;
    end if;
end if;

/*
--计费方、预付费系统鉴权
*/

if (v_productpayparty = 1) then
    v_paymsisdn     := v_srccorpid;
    v_cardtype      := 0;
    v_paymsisdntype := 1;
    o_paymsisdn     := v_paymsisdn;
    o_paytype       := v_cardtype;
    o_payuserindex  := v_srccorpindex;
    o_paymode       := '集团付费';
else
    v_paymsisdn     := v_srcmsisdn;
    v_cardtype      := v_srcpaytype;
    v_paymsisdntype := v_srcmsisdntype;
    o_paymsisdn     := v_paymsisdn;
    o_paytype       := v_cardtype;
    o_payuserindex  := v_srcuserindex;
    o_paymode       := '个人付费';

    if (i_prepaidtype = 1) then
        o_scptype := 1;
    elsif i_prepaidtype = 2 then
        o_scptype := 2;
    else
        --attach 预付费归属,0-未知,1-OCS,2-RTBP
        begin
            select phoneindex,attach
            into   v_phoneindex,v_attach
            from   zxdbm_ismp.ssys_phone
            where  startprefix <= substr(v_paymsisdn,1,length(startprefix))
            and    endprefix >= substr(v_paymsisdn,1,length(endprefix));
        exception when no_data_found then
            o_scptype  := 2;
            v_devexist := 0; --表示不存在对应号段对应设备配置
        when others then
            o_retcode   := 506;
            o_debugpos := 79;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        if (v_devexist = 1) then
            if (v_attach = 1) then
                o_scptype := 1;
            elsif (v_attach = 2) then
                o_scptype := 2;
            else
                begin
                    select 1
                    into   o_scptype
                    from   zxdbm_ismp.sdev_phone
                    where  phoneindex = v_phoneindex
                    and    devicetype = 5;
                exception when no_data_found then
                    o_scptype := 2;
                when others then
                    o_retcode   := 506;
                    o_debugpos := 80;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
            end if;
         end if;
    end if;
end if;

--伪码处理(支持伪码开关打开,非自营sp)--支持业务LCS,SMS,MVS,DLS,MMS,WAP,CPX
--MOAT时,AuthPriceReq消息中OA真实号码转化为伪码,同步SP使用
begin
    select cpsrvtype into v_cpsrvtype
    from zxdbm_ismp.scp_basic
    where cpindex = v_cpindex;
exception when no_data_found then
    o_retcode   := 2000;
    o_debugpos := 81;
    o_debuginfo := 'v_cpindex='||v_cpindex||', the sp does not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 82;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;

if i_servicetype in (1,2,3,4,41,5,6,101) then
    if i_checkpseudocode = 1 and v_cpsrvtype <> 3 then
        if i_serviceflowtype = 1 then
            begin
                select pseudocode into o_pseudocode from zxdbm_ismp.susr_basic where usercode = i_srcmsisdn;
            exception when no_data_found then
                o_retcode   := 1001;
                o_debugpos := 83;
                o_debuginfo := 'i_srcmsisdn = '||i_srcmsisdn||', the user does not exist.';
                return;
            when others then
                o_retcode   := 506;
                o_debugpos := 84;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            o_pcodemsisdnswitch := 1;
        end if;
    end if;
end if;

end slp_200_telicomusercheck;
/

--多DB,免费期提醒定时任务,用户更新时间处理。
prompt zxdbm_200.slp_200_telicommaindb
create or replace procedure zxdbm_200.slp_200_telicommaindb
(

i_srcmsisdn             in          varchar2,            --发起方/赠送方用户号码
i_usemsisdn             in          varchar2,            --使用方/受赠方用户号码
i_usemsisdntype         in          number,              --使用方号码类型,1:msisdn,2:phs,3:pstn
i_actiontype            in          number,              --动作类型
i_startactiontype       in          number,              --二次确认流程中的第一次动作类型
i_cmdchannel            in          number,              --动作来源
i_productid             in          varchar2,            --产品代码
i_serviceid             in          varchar2,            --业务代码
i_cpid                  in          varchar2,            --CP/SP代码
i_freetime              in          varchar2,            --产品订购关系免费期
i_rollback              in          number,              --是否回滚订购关系 0-不回滚 1-回滚
i_isthird               in          number,              --0:普通订购 1:赠送
i_silent                in          number,              --沉默去激活开关
i_needauthuser          in          number,              --是否需要鉴权用户 0-不需要 1-需要
i_provinceorder         in          number,              --系统开关:是否支持跨省赠送   0-不支持   1-支持
i_isfromnmsc            in          number,              --是否nmsc过来的消息 0:否 1:是
i_substarttime          in          varchar2,            --产品订购关系生效时间
o_retcode               out         number,              --鉴权返回码 0代表成功,否则代表错误码
o_debugpos             out         number,              --出口顺序标识
o_debuginfo             out         varchar2             --调试信息

)
as

--新增下面几个关于时间的参数,尽量减少sysdate的调用,所有用到sysdate的地方,以to_date(v_sysdate14,'yyyymmddhh24miss')进行代替
v_sysdate14             char(14)     := to_char(sysdate ,'yyyymmddhh24miss');
v_sysdate8              char(8)      := substr(v_sysdate14,1,8);
v_curmonth              number(2)    := to_number(substr(v_sysdate14,5,2));     --临时存放当前月份
v_curstrmonth           char(2)      := lpad(to_char(v_curmonth),2,'0');        --临时存放当前月份,字符型

v_freetime              zxdbm_200.s200_user_subscription.freetime%type             := i_freetime;                                     --临时存放产品订购免费日期
v_srcmsisdn             zxdbm_200.s200_user_subscription.subscriber%type           := i_srcmsisdn;                                    --发起方/赠送方用户号码
v_usemsisdn             zxdbm_200.s200_user_subscription.usercode%type             := i_usemsisdn;                                    --使用方/受赠方用户号码
v_productid             zxdbm_200.s200_user_subscription.productid%type            := i_productid;                                    --临时存放产品代码
v_msisdntype            zxdbm_200.s200_user_subscription.msisdntype%type           := i_usemsisdntype;                                --临时存放用户号码类型 1-msisdn,2-phs,3-pstn,4-IPTV接入号,5-宽带接入号,6-固定IP
v_cpid                  zxdbm_200.s200_user_subscription.cpid%type                 := i_cpid;                                         --临时存放CP/SP代码
v_serviceid             zxdbm_200.s200_user_subscription.serviceid%type            := i_serviceid;                                    --临时存放业务代码
v_phoneindex            zxdbm_ismp.ssys_phone.phoneindex%type                      := 0;                                              --临时存放号段序号
v_phoneindexa           zxdbm_ismp.ssys_phone.phoneindex%type                      := 0;                                              --临时存放A省号段序号,跨省赠送
v_phoneindexb           zxdbm_ismp.ssys_phone.phoneindex%type                      := 0;                                              --临时存放B省号段序号,跨省赠送
v_attach                zxdbm_ismp.ssys_phone.attach%type                          := 0;                                              --临时存放预付费归属,0-未知,1-OCS,2-RTBP
v_srcuserindex          zxdbm_ismp.susr_basic.userindex%type                       := 0;                                              --临时存放发起方用户序号
v_srcuserstatus         zxdbm_ismp.susr_basic.status%type                          := 0;                                              --临时存放发起方状态
v_srcactivestatus       zxdbm_ismp.susr_basic.activestatus%type                    := 0;                                              --临时存放发起方激活状态
v_srcusetime            zxdbm_ismp.susr_basic.lastusetime%type                     := '00000000000000';                               --临时存放发起方上次使用时间
v_useuserindex          zxdbm_ismp.susr_basic.userindex%type                       := 0;                                              --临时存放使用方用户序号
v_useuserstatus         zxdbm_ismp.susr_basic.status%type                          := 0;                                              --临时存放使用方状态
v_useactivestatus       zxdbm_ismp.susr_basic.activestatus%type                    := 0;                                              --临时存放使用方激活状态
v_useusetime            zxdbm_ismp.susr_basic.lastusetime%type                     := '00000000000000';                               --临时存放使用方上次使用时间

begin

--出参初始化
o_retcode               := 0;
o_debugpos             := 0;
o_debuginfo             := ' ';
--订购流程,更新用户上次使用时间
/*
if (i_actiontype in (1,17,102)) or (i_startactiontype in (1,17,102)) then
    --是否跨省赠送的鉴权(根据号段鉴权是否本省用户)
    --赠送他省不对DA鉴权,来自他省赠送不对OA鉴权
    if i_provinceorder = 1 then
        if i_actiontype in (1,17) and (i_isfromnmsc = 1) then
            begin
                select phoneindex
                into   v_phoneindexa
                from   zxdbm_ismp.ssys_phone
                where  startprefix <= substr(v_srcmsisdn,1,length(startprefix))
                and    endprefix >= substr(v_srcmsisdn,1,length(endprefix));
            exception when no_data_found then
                null;
            when others then
                o_retcode   := 506;
                o_debugpos := 1;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            begin
                select phoneindex
                into   v_phoneindexb
                from   zxdbm_ismp.ssys_phone
                where  startprefix <= substr(v_usemsisdn,1,length(startprefix))
                and    endprefix >= substr(v_usemsisdn,1,length(endprefix));
            exception when no_data_found then
                null;
            when others then
                o_retcode   := 506;
                o_debugpos := 2;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if v_phoneindexa <> 0 and v_phoneindexb = 0 then
                v_usemsisdn := v_srcmsisdn;
            elsif v_phoneindexa = 0 and v_phoneindexb <> 0 then
                v_srcmsisdn := v_usemsisdn;
            end if;
        end if;
    end if;
    --发起方和使用方存在性鉴权
    if i_needauthuser = 1 then
        if (i_isthird = 1) and (v_srcmsisdn <> v_usemsisdn) then
            begin
                select a.userindex,a.status,a.activestatus,a.lastusetime,
                       b.userindex,b.status,b.activestatus,b.lastusetime
                into   v_srcuserindex,v_srcuserstatus,v_srcactivestatus,v_srcusetime,
                       v_useuserindex,v_useuserstatus,v_useactivestatus,v_useusetime
                from   zxdbm_ismp.susr_basic a, zxdbm_ismp.susr_basic b
                where  a.usercode = v_srcmsisdn and b.usercode = v_usemsisdn;
            exception when no_data_found then
                o_retcode   := 1001;    --用户不存在
                o_debugpos := 3;
                o_debuginfo := 'user do not exist.';
                return;
            when others then
                o_retcode   := 506;
                o_debugpos := 4;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if (v_srcactivestatus = 0) then
            --待激活用户激活操作
                begin
                    update  zxdbm_ismp.susr_basic
                    set     activestatus = 1,
                            activetime = v_sysdate14
                    where   userindex = v_srcuserindex;
                    if (sql%rowcount = 0) then
                        o_retcode   := 506;
                        o_debugpos := 5;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        rollback;
                        return;
                    end if;
                    commit;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 6;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                  end;
            else
            --已激活用户更新最近一次使用时间
                if (i_silent = 1) then
                    if (substr(v_srcusetime,1,8) <> v_sysdate8) then
                        begin
                            update  zxdbm_ismp.susr_basic
                            set     lastusetime = v_sysdate14
                            where   userindex = v_srcuserindex;
                            if (sql%rowcount = 0) then
                                o_retcode   := 506;
                                o_debugpos := 7;
                                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                                rollback;
                                return;
                            end if;
                            commit;
                        exception when others then
                            o_retcode   := 506;
                            o_debugpos := 8;
                            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                            return;
                        end;
                    end if;
                end if;
            end if;
            if (v_useactivestatus = 0) then
            --待激活用户激活操作
                begin
                    update  zxdbm_ismp.susr_basic
                    set     activestatus = 1,
                            activetime = v_sysdate14
                    where   userindex = v_useuserindex;
                    if (sql%rowcount = 0) then
                        o_retcode   := 506;
                        o_debugpos := 9;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        rollback;
                        return;
                    end if;
                    commit;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 10;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
            else
            --已激活用户更新最近一次使用时间
                if (i_silent = 1) then
                    if (substr(v_useusetime,1,8) <> v_sysdate8) then
                        begin
                            update  zxdbm_ismp.susr_basic
                            set     lastusetime = v_sysdate14
                            where   userindex = v_useuserindex;
                            if (sql%rowcount = 0) then
                                o_retcode   := 506;
                                o_debugpos := 11;
                                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                                rollback;
                                return;
                            end if;
                            commit;
                        exception when others then
                            o_retcode   := 506;
                            o_debugpos := 12;
                            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                            return;
                        end;
                    end if;
                end if;
            end if;
            --发起方状态鉴权
        else
            begin
                select userindex,status,activestatus,lastusetime
                into   v_srcuserindex,v_srcuserstatus,v_srcactivestatus,v_srcusetime
                from   zxdbm_ismp.susr_basic
                where  usercode = v_srcmsisdn;
            exception when no_data_found then
                o_retcode   := 1001;    --用户不存在
                o_debugpos := 13;
                o_debuginfo := 'user do not exist.';
                return;
            when others then
                o_retcode   := 506; --数据库错误
                o_debugpos := 14;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            if (v_srcactivestatus = 0) then
            --待激活用户激活操作
                begin
                    update  zxdbm_ismp.susr_basic
                    set     activestatus = 1,
                            activetime = v_sysdate14
                    where   userindex = v_srcuserindex;
                    if (sql%rowcount = 0) then
                        o_retcode   := 506;
                        o_debugpos := 15;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        rollback;
                        return;
                    end if;
                    commit;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 16;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
            else
            --已激活用户更新最近一次使用时间
                if (i_silent = 1) then
                    if (substr(v_srcusetime,1,8) <> v_sysdate8) then
                        begin
                            update  zxdbm_ismp.susr_basic
                            set     lastusetime = v_sysdate14
                            where   userindex = v_srcuserindex;
                            if (sql%rowcount = 0) then
                                o_retcode   := 506;
                                o_debugpos := 17;
                                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                                rollback;
                                return;
                            end if;
                            commit;
                        exception when others then
                            o_retcode   := 506;
                            o_debugpos := 18;
                            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                            return;
                        end;
                    end if;
                end if;
            end if;
        end if;
    end if;
end if;
*/
--退订流程,更新退订方上次使用时间,删除免费期定时任务

-- if (i_actiontype in (2,18,103)) or (i_startactiontype in (2,18,103)) then

    if (i_needauthuser = 1) then
        begin
            select userindex,status,activestatus,lastusetime
            into   v_useuserindex,v_useuserstatus,v_useactivestatus,v_useusetime
            from   zxdbm_ismp.susr_basic
            where  usercode = v_usemsisdn;
        exception when no_data_found then
            o_retcode   := 1001;
            o_debugpos := 19;
            o_debuginfo := 'user do not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 20;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        if (v_useactivestatus = 0) then
            --待激活用户激活操作
            begin
                update  zxdbm_ismp.susr_basic
                set     activestatus = 1,
                        activetime = v_sysdate14
                where   userindex = v_useuserindex;
                if (sql%rowcount = 0) then
                    o_retcode   := 506;
                    o_debugpos := 21;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    rollback;
                    return;
                end if;
                commit;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 22;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
        else
            --已激活用户更新最近一次使用时间
            if (i_silent = 1) then
                if (substr(v_useusetime,1,8) <> v_sysdate8) then
                    begin
                        update  zxdbm_ismp.susr_basic
                        set     lastusetime = v_sysdate14
                        where   userindex = v_useuserindex;
                        if (sql%rowcount = 0) then
                            o_retcode   := 506;
                            o_debugpos := 23;
                            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                            rollback;
                            return;
                        end if;
                        commit;
                    exception when others then
                        o_retcode   := 506;
                        o_debugpos := 24;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        return;
                    end;
                end if;
            end if;
        end if;
    end if;

-- end if;
end slp_200_telicommaindb;
/

--退订入库
prompt zxdbm_200.slp_200_telicomcancelreq
create or replace procedure zxdbm_200.slp_200_telicomcancelreq
(

i_oa                    in          varchar2,            --发起方用户号码
i_oatype                in          number,              --发起方地址类型
i_srvprodid             in          varchar2,            --业务/产品代码
i_serviceid             in          varchar2,            --业务代码
i_actiontype            in          number,              --动作类型
i_cmdchannel            in          number,              --动作来源
i_lallindex             in          number,              --流程分支:1鉴权批价 91二次确认超时
i_servicetype           in          number,              --业务能力类型
i_countrycode           in          varchar2,            --国家码
i_spaccesscode          in          varchar2,            --antuprice消息中的SP接入码
i_silent                in          number,              --沉默去激活开关
i_needauthuser          in          number,              --是否需要鉴权用户 0-不需要 1-需要
i_needreplay            in          number,              --产品退订是否需要二次确认
i_direct                in          number,              --订购方向 0:正向退订 1:SP反向退订
i_supportproductid      in          number,              --同步SP、SP下发使用service还是productid 0-serviceid 1-productid
i_prepaidtype           in          number,              --预付费接口类型 1-OCS 2-非OCS 3-根据号段选择是OCS或者非OCS
i_opid                  in          varchar2,            --操作员工号
i_rollback              in          number,              --是否回滚订购关系 0-不回滚 1-回滚
i_catchflag             in          number,              --二次确认类型 1-订购  2,3-退订 4-改号
i_checkpseudocode       in          number,              --系统开关:是否支持伪码   0-不支持   1-支持
i_mutidbmode            in          number,              --是否多DB环境 0:否 1:是
i_eventid               in          varchar2,            --事件ID
i_channelid             in          varchar2,            --渠道商ID
i_cancelproductid       in          varchar2,            --被替换的产品代码
i_issecondproduct       in          number,              --订购流程子类型 0:普通订购 1:业务订购 2:待失效订购关系订购 3:纯包量重复订购 6:可变价的重复订购 7:体验转正式的重复订购
--added by youqunxia 20131010  Order3.02.01
i_effectiveswitch       in          number,              --VSOP发起的订购退订立即生效开关是否打开,0-关闭,1-打开
o_retcode               out         number,              --鉴权返回码 0代表成功,否则代表错误码
o_debugpos             out         number,              --出口顺序标识
o_debuginfo             out         varchar2,            --调试信息
o_usemsisdn             out         varchar2,            --去掉前缀的使用方用户号码
o_paymsisdn             out         varchar2,            --去掉前缀的计费方号码
o_usemsisdntype         out         number,              --使用方号码类型,1:msisdn,2:phs,3:pstn
o_paymsisdntype         out         number,              --付费方号码类型,1:msisdn,2:phs,3:pstn
o_userindex             out         number,              --使用方号码序号
o_paytype               out         number,              --计费号码的付费类型 0:后付费 1:预付费 2:准预付费
o_oppaytype             out         number,              --话单专用付费类型
o_scptype               out         number,              --对预付费扣费的设备类型 1:OCS;2:非OCS
o_subfee                   out         number,              --本次退订需要扣取/补回的费用
o_feemode               out         number,              --1:扣费  2:补款
o_colortype             out         number,              --使用方号码名单类型   0-普通用户,1-黑名单,2-白名单,3-灰名单,4-红名单
o_cpid                  out         varchar2,            --CP/SP代码
o_cpindex               out         number,              --CP/SP序号
o_cpcnshortname         out         varchar2,            --CP/SP名称
o_cpunsubscribeurl      out         varchar2,            --CP/SP退订URL
o_cptype                out         number,              --CP/SP类型 0:本省 1:是全国业务
o_protocoltype          out         number,              --定购同步协议类型0-ismp(wbs方式),1-smgp2.0(cngp1.0),2-smgp3.0
o_msgtosp               out         varchar2,            --SMGP/CNGP同步sp的消息内容
o_customersrvtel        out         varchar2,            --CP/SP客服电话
o_serviceid             out         varchar2,            --业务代码
o_serviceindex          out         number,              --业务序号
o_servicename           out         varchar2,            --业务名称
o_productid             out         varchar2,            --产品代码
o_productindex          out         number,              --产品序号
o_productname           out         varchar2,            --产品名称
o_smschargingcn         out         varchar2,            --产品资费短信描述
o_servicetype           out         number,              --产品业务能力类型
o_subcapability         out         number,              --产品业务能力子类型
o_srvtypeid             out         varchar2,            --业务能力编码,对应ssys_service_type表中的srvtypeid
o_corpid                out         varchar2,            --集团代码
o_corpindex             out         number,              --集团序号
o_corpcnshortname       out         varchar2,            --集团名称
o_subendtime            out         varchar2,            --产品订购关系失效时间
o_linknum               out         varchar2,            --业务话单中同一个流程中二次确认前后话单的关联
o_productsubid          out         varchar2,            --产品订购关系代码
o_productsubindex       out         number,              --产品订购关系序号
o_neednotifysp          out         number,               --是否有必要通知SP
o_pseudocode            out         varchar2,            --伪码
o_pcodemsisdnswitch     out         number,              --伪码和真实号码之间是否有转换:  0否   1真实号码转化伪码 2伪码转化为真实号码
o_chanelplayerstate     out         number,              --cp/sp状态,0-申请,1-正常,2-暂停(暂停代收费),3-预注销,4-注销,5-冻结,21-暂停端口
o_eventstate            out         number,              --0,正常 1,注销
o_spaccesscode          out         varchar2,              --SP接入码
o_maincapability        out         number,              --组合业务主业务能力类型
o_submaincapability     out         number,              --组合业务主业务能力子类型
o_cpsrvtype             out         number,              --CP服务类型:0:CP+SP,1:CP,2:SP ;3:自营业务SP
o_orderfeaturecode      out         varchar2,            --短信,彩信订购指令
o_cdrbegintime          out         varchar2,            --话单计费开始时间
o_cdrendtime            out         varchar2,             --话单计费结束时间
o_producttype           out         number,               --产品类型   1-正常产品 2-测试产品
o_cnfmtype              out         number,               --产品零扣费类型 0:非零扣费  1:产品免费或者费率设置为0  2:免费期无费用
o_invalidmode           out         number,               --退订生效方式,0 立即生效,1-下一帐期生效 2-退定后可使用足月
o_cityid                out         varchar2,             --用户表中的cityid
--added by youqunxia 2013.01.17
o_orderaccesscode       out         varchar2,             --短信,彩信订购接入码
--added by youqunxia20130906 Order3.02.01
o_issynctooidd          out         number                --是否同步订购关系给OIDD,0-否,1-是

)
as

--新增下面几个关于时间的参数,尽量减少sysdate的调用,所有用到sysdate的地方,以to_date(v_sysdate14,'yyyymmddhh24miss')进行代替
v_sysdate14             char(14)     := to_char(sysdate ,'yyyymmddhh24miss');
v_sysdate8              char(8)      := substr(v_sysdate14,1,8);
v_sysmonth6             char(6)      := substr(v_sysdate14,1,6);

v_curmonth              number(2)    := to_number(substr(v_sysdate14,5,2));     --临时存放当前月份
v_curstrmonth           char(2)      := lpad(to_char(v_curmonth),2,'0');        --临时存放当前月份,字符型
v_curmonthendtime       char(14)     := to_char(last_day(to_date(v_sysdate14,'yyyymmddhh24miss')),'yyyymmdd')||'235959';
v_nextmonthstarttime    char(14)     := to_char(last_day(to_date(v_sysdate14,'yyyymmddhh24miss')) + 1,'yyyymmdd')||'000000';

v_maincapability        number(10)                                                 := 0;                       --临时存放组合业务主业务能力类型
v_submaincapability     number(10)                                                 := 0;                       --临时存放组合业务主业务能力子类型
v_countrycode           varchar2(5)                                                := i_countrycode;           --临时存放国家码
v_tempitem              number(10)                                                 := 0;                       --临时存放0或者推广期的计费子序号
v_cmdchannel            number(10)                                                 := i_cmdchannel;            --临时存放动作来源
v_tmpint                number(10)                                                 := 0;                       --临时存放临时数值
v_devexist              number(3)                                                  := 1;                       --临时存放是否存在对应设备
v_leftdays              number(10)                                                 := 0;                       --本月剩余天数
v_monthdays             number(10)                                                 := 0;                       --本月有效收费天数
v_sql                   varchar2(3072)                                             := ' ';                     --临时存放动态sql
v_isservicesub          number(3)                                                  := 0;                       --临时存放是否业务订购
v_spaccesscode          varchar2(40)                                               := ' ';                     --临时存放sp接入码
v_silentdays            number(10)                                                 := 0;                       --沉默用户天数
v_nextrentfee           number(10)                                                 := 0;                       --临时存放待生效包月费
v_rentfeevalidate       char(6)                                                    := ' ';                     --临时存放待生效包月费率生效月
v_totaldctemplate       varchar2(500)                                              := ' ';                     --临时存放解析模板:产品分档费率
v_dealtemplate          varchar2(500)                                              := ' ';                     --临时存放解析模板:待解析的分档段
v_totaldclength         number(3)                                                  := 0;                       --临时存放解析模板: 分档字符串的长度
v_totaldcsum            number(10)                                                 := 0;                       --临时存放解析模板: 分档总费用
v_index0                number(3)                                                  := 0;                       --临时存放解析模板: 分档字符串中&的位置
v_index1                number(3)                                                  := 0;                       --临时存放解析模板: 分档字符串中;的位置
v_nextrentdatecycle     char(14)                                                   := v_sysdate14;             --临时存放包周期产品的下次计划扣租时间
v_freeuseflag           number(3)                                                  := 0;                       --临时存入流媒体业务的试用期属性
v_hasrecord             number(3)                                                  := 0;                       --存放需要退订的产品是否包含在捆绑销售品中(0,不包含;1,包含)
v_templockmonth         number(5,2)                                                := 0;                       --临时存放退订时与订购时相差的月份数

v_oa                    zxdbm_ismp.susr_basic.usercode%type                        := ' ';                     --临时存放发起方用户号码
v_usemsisdn             zxdbm_ismp.susr_basic.usercode%type                        := ' ';                     --临时存放使用方用户号码
v_paymsisdn             zxdbm_ismp.susr_basic.usercode%type                        := ' ';                     --临时存放计费方用户号码
v_useuserindex          zxdbm_ismp.susr_basic.userindex%type                       := 0;                       --临时存放使用方用户序号
v_payuserindex          zxdbm_ismp.susr_basic.userindex%type                       := 0;                       --临时存放计费方用户序号
v_useusertype           zxdbm_ismp.susr_basic.usertype%type                        := 1;                       --临时存放使用方用户类型
v_usepaytype            zxdbm_ismp.susr_basic.paytype%type                         := 0;                       --临时存放使用方用户付费类型
v_paypaytype            zxdbm_ismp.susr_basic.paytype%type                         := 0;                       --临时存放付费方用户付费类型
v_useblacklist          zxdbm_ismp.susr_basic.blacklist%type                       := 0;                       --临时存放使用方是否进入黑名单
v_usewhitelist          zxdbm_ismp.susr_basic.whitelist%type                       := 0;                       --临时存放使用方是否进入白名单
v_useredlist            zxdbm_ismp.susr_basic.redlist%type                         := 0;                       --临时存放使用方是否进入红名单
v_useblacklevel         zxdbm_ismp.susr_basic.blacklevel%type                      := 0;                       --临时存放使用方黑名单级别
v_usewhitelevel         zxdbm_ismp.susr_basic.whitelevel%type                      := 0;                       --临时存放使用方白名单级别
v_usemsisdntype         zxdbm_ismp.susr_basic.msisdntype%type                      := 1;                       --临时存放使用方号码类型
v_paymsisdntype         zxdbm_ismp.susr_basic.msisdntype%type                      := 1;                       --临时存放付费方号码类型
v_useuserstatus         zxdbm_ismp.susr_basic.status%type                          := 0;                       --临时存放使用方状态
v_useactivestatus       zxdbm_ismp.susr_basic.activestatus%type                    := 0;                       --临时存放使用方激活状态
v_useoppaytype          zxdbm_ismp.susr_basic.oppaytype%type                       := 0;                       --临时存放使用方话单专用付费类型
v_payoppaytype          zxdbm_ismp.susr_basic.oppaytype%type                       := 0;                       --临时存放付费方话单专用付费类型
v_usecorpindex          zxdbm_ismp.susr_basic.corpindex%type                       := 0;                       --临时存放使用方集团序号
v_useusetime            zxdbm_ismp.susr_basic.lastusetime%type                     := '00000000000000';        --临时存放使用方上次使用时间
v_useusercityid         zxdbm_ismp.susr_basic.cityid%type                          := ' ';                                       --临时存放计费方号码类型
v_payusercityid         zxdbm_ismp.susr_basic.cityid%type                          := ' ';                                       --临时存放计费方号码类型

v_usecorpid             zxdbm_ismp.susr_corp.corpid%type                           := ' ';                     --临时存放集团代码
v_productindex          zxdbm_200.s200_product.productindex%type                   := 0;                       --临时存放产品序号
v_invalidmode           zxdbm_200.s200_product.invalidmode%type                    := 0;                       --退订生效方式,0 立即生效,1-下一帐期生效 2-退定后可使用足月
v_validcyctype          zxdbm_200.s200_product.validcyctype%type                   := 0;                       --临时存放周期方式0-非周期,1-天,2-周,3-月,定购生效方式为2时生效
v_validcycparam         zxdbm_200.s200_product.validcycparam%type                  := 0;                       --临时存放周期参数,定购生效方式为2时生效
v_validdate             zxdbm_200.s200_product.validdate%type                      := v_sysdate14;             --临时存放指定生效日期
v_subfeemode            zxdbm_200.s200_product.subfeemode%type                     := 1;                       --临时存放定购当月收费方式
v_subfeedays            zxdbm_200.s200_product.subfeedays%type                     := 0;                       --临时存放收费日期取值范围为0~28,0表示本月全部免费,其他表示 n号之后免费。
v_freedays              zxdbm_200.s200_product.freedays%type                       := 0;                       --临时存放免费试用天数
v_popustart             zxdbm_200.s200_product.popularizestart%type                := v_sysdate8;              --临时存放推广期开始时间
v_popustop              zxdbm_200.s200_product.popularizestop%type                 := v_sysdate8;              --临时存放推广期结束时间
v_popuitem              zxdbm_200.s200_product.popularizeitem%type                 := 0;                       --临时存放推广期计费方案,0表示免费,1-推广期方案
v_needcharge            zxdbm_200.s200_product.needcharge%type                     := 1;                       --临时存放是否需要计费,0-免费,1-需要计费
v_productname           zxdbm_200.s200_product.productname%type                    := ' ';                     --临时存放产品名称
v_productstatus         zxdbm_200.s200_product.status%type                         := 0;                       --临时存放产品状态
v_onlyforcorp           zxdbm_200.s200_product.onlyforcorp%type                    := 0;                       --临时存放是否集团专用产品0:个人用户专用1:集团专用
v_productstarttime      zxdbm_200.s200_product.starttime%type                      := v_sysdate14;             --临时存放产品生效时间
v_productendtime        zxdbm_200.s200_product.endtime%type                        := '99991231235959';        --临时存放产品终止时间
v_workflow              zxdbm_200.s200_product.workflow%type                       := 0;                       --临时存放工作流状态
v_workflowlife          zxdbm_200.s200_product.workflowlife%type                   := 0;                       --临时存放工作流流程状态
v_cpindex               zxdbm_200.s200_product.cpindex%type                        := 0;                       --临时存放CP/SP序号
v_serviceindex          zxdbm_200.s200_product.serviceindex%type                   := 0;                       --临时存放业务序号
v_smschargingcn         zxdbm_200.s200_product.smschargingcn%type                  := ' ';                     --临时存放产品资费短信描述
v_canbepresent          zxdbm_200.s200_product.canbepresent%type                   := 0;                       --临时存放是否可赠送,0-不可赠送,1-可赠送
v_servicetype           zxdbm_200.s200_product.servicetype%type                    := 0;                       --临时存放业务能力类型
v_subcapability         zxdbm_200.s200_product.subcapability%type                  := 1;                       --临时存放业务能力子类型1-3G,2-PHS,3-ISAG,默认为3g业务能力(仅电信使用,其他营运商皆默认为1)
v_srvprodid             zxdbm_200.s200_product.productid%type                      := i_srvprodid;             --临时存放业务产品代码
v_productid             zxdbm_200.s200_product.productid%type                      := ' ';                     --临时存放产品代码
v_onlyforpkg            zxdbm_200.s200_product.onlyforpkg%type                     := 0;                       --临时存放是否套餐专用产品
v_backfeemode           zxdbm_200.s200_product.backfeemode%type                    := 0;                       --临时存放退定补款方式,0-不补款,1-按帐期剩余实际天数补款
--added by youqunxia20130924 order3.02.01
v_lockmonth             zxdbm_200.s200_product.lockmonth%type                      := 0;                       --临时存产品放协议期,单位月份,为0时按无协议期处理

v_chargetype            zxdbm_200.s200_product_fee.chargetype%type                 := 0;                       --临时存放计费类型
v_subchargetype         zxdbm_200.s200_product_fee.subchargetype%type              := 0;                       --临时存放计费子类型
v_cyctype               zxdbm_200.s200_product_fee.cyctype%type                    := 0;                       --临时存放周期类型0-非周期,1-周期性
v_cycunit               zxdbm_200.s200_product_fee.cycunit%type                    := 0;                       --临时存放周期单位 0-无,1-天,2-周,3-月,4-半年,5-年,6-小时,7-分钟,8-秒
v_cycparam              zxdbm_200.s200_product_fee.cycparam%type                   := 0;                       --临时存放周期参数
v_rentfee               zxdbm_200.s200_product_fee.rentfee%type                    := 0;                       --临时存放周期性费用金额
v_fixfee                zxdbm_200.s200_product_fee.fixfee%type                     := 0;                       --临时存放包量费用
v_fixusetimes           zxdbm_200.s200_product_fee.fixusetimes%type                := 0;                       --临时存放包量使用值,0-无限制
v_unitvol               zxdbm_200.s200_product_fee.unitvol%type                    := 10;                      --临时存放相对于目前最小计量单位的量,时长为百毫秒,流量为byte
v_autocontinue          zxdbm_200.s200_product_fee.autocontinue%type               := 0;                       --临时存放周期性定购,是否自动续订
v_minlimitfee           zxdbm_200.s200_product_fee.minlimitfee%type                := 0;                       --临时存放保底费用,0-无保底费
v_usecomfee             zxdbm_200.s200_product_fee.usecomfee%type                  := 1;                       --临时存放限制(量、时间)外使用方式,1-使用基本费率,0-不允许继续使用
v_totaldiscount         zxdbm_200.s200_product_fee.totaldiscount%type              := ' ';                            --临时存放限制(量、时间)外使用方式,1-使用基本费率,0-不允许继续使用
v_cpid                  zxdbm_ismp.scp_basic.cpid%type                             := ' ';                     --临时存放CP/SP代码
v_cpstatus              zxdbm_ismp.scp_basic.status%type                           := 0;                       --临时存放CP/SP代码
v_cpsubscribeurl        zxdbm_ismp.scp_basic.cpsubscribeurl%type                   := ' ';                     --临时存放CP/SP订购URL
v_cpunsubscribeurl      zxdbm_ismp.scp_basic.cpunsubscribeurl%type                 := ' ';                     --临时存放CP/SP退订URL
v_hasblacklist          zxdbm_ismp.scp_basic.hasblacklist%type                     := 0;                       --临时存放是否有黑名单控制 0-无,1-有
v_cptype                zxdbm_ismp.scp_basic.cptype%type                           := 2;                       --临时存放CP/SP类型
v_cpsrvtype             zxdbm_ismp.scp_basic.cpsrvtype%type                        := 0;                       --临时存放CP服务类型
v_insubblacklist        zxdbm_ismp.scp_basic.insubblacklist%type                   := 0;                       --临时存放限制订购SP黑名单标识
v_cpcnshortname         zxdbm_ismp.scp_basic.cpcnshortname%type                    := ' ';                     --临时存放CP/SP中文名简写
v_customersrvtel        zxdbm_ismp.scp_basic.customersrvtel%type                   := ' ';                     --临时存放CP/SP客服电话
v_protocoltype          zxdbm_200.s200_sp_service_type.protocoltype%type           := 0;                       --临时存放定购同步协议类型0-ismp(wbs方式),1-smgp2.0(cngp1.0),2-smgp3.0(cngp1.0扩展)
v_srvtypeid             zxdbm_ismp.ssys_service_type.srvtypeid%type                := ' ';                     --临时存放话单中的srvtypeid
v_productcancelmode     zxdbm_ismp.ssrv_corp_product.unsubscribemode%type          := 0;                       --临时存放退订方式,0-必须集团管理员退订,1-集团管理员与集团成员均可退订
v_productpayparty       zxdbm_ismp.ssrv_corp_product.payparty%type                 := 2;                       --临时存放集团专用产品付费方 1-集团付费 2-个人付费
v_productsubindex       zxdbm_200.s200_user_subscription.subscriptionindex%type    := 0;                       --临时存放订购关系序号
v_productsubid          zxdbm_200.s200_user_subscription.subscriptionid%type       := ' ';                     --临时存放订购关系代码
v_serviceid             zxdbm_200.s200_user_subscription.serviceid%type            := i_serviceid;             --临时存放业务代码
v_propkgindex           zxdbm_200.s200_user_subscription.productpkgindex%type      := 0;                       --临时存放套餐序号
v_propkgid              zxdbm_200.s200_user_subscription.productpkgid%type         := ' ';                     --临时存放套餐代码
v_contentindex          zxdbm_200.s200_user_subscription.contentindex%type         := 0;                       --临时存放内容序号
v_contentid             zxdbm_200.s200_user_subscription.contentid%type            := ' ';                     --临时存放内容代码
v_corpindex             zxdbm_200.s200_user_subscription.corpindex%type            := 0;                       --临时存放集团序号
v_corpid                zxdbm_200.s200_user_subscription.corpid%type               := ' ';                     --临时存放集团代码
v_subscriber            zxdbm_200.s200_user_subscription.subscriber%type           := ' ';                     --发起方用户号码
v_usercode              zxdbm_200.s200_user_subscription.usercode%type             := ' ';                     --使用方用户号码
v_payuser               zxdbm_200.s200_user_subscription.payuser%type              := ' ';                     --计费方用户号码
v_subscribemode         zxdbm_200.s200_user_subscription.subscribemode%type        := 1;                       --临时存放订购方式
v_subscribechannel      zxdbm_200.s200_user_subscription.subscribechannel%type     := 3;                       --临时存放订购渠道
v_subscribeoptype       zxdbm_200.s200_user_subscription.subscribeoptype%type      := 0;                       --临时存放订购操作
v_cancelmode            zxdbm_200.s200_user_subscription.cancelmode%type           := 0;                       --临时存放订购方式
v_cancelchannel         zxdbm_200.s200_user_subscription.cancelchannel%type        := 0;                       --临时存放订购渠道
v_canceloptype          zxdbm_200.s200_user_subscription.canceloptype%type         := 0;                       --临时存放订购操作
v_pausemode             zxdbm_200.s200_user_subscription.pausemode%type            := 0;                       --临时存放产品暂停原因
v_resumemode            zxdbm_200.s200_user_subscription.resumemode%type           := 0;                       --临时存放产品恢复原因
v_substatus             zxdbm_200.s200_user_subscription.status%type               := 0;                       --临时存放产品订购关系状态
v_pushid                zxdbm_200.s200_user_subscription.pushid%type               := ' ';                     --临时存放推荐者ID
v_substarttime          zxdbm_200.s200_user_subscription.starttime%type            := v_sysdate14;             --临时存放定购生效时间
v_cursubendtime         zxdbm_200.s200_user_subscription.endtime%type              := v_sysdate14;             --临时存放当前定购关系失效时间
v_subendtime            zxdbm_200.s200_user_subscription.endtime%type              := '99991231235959';        --临时存放定购失效时间
v_lastrenttime          zxdbm_200.s200_user_subscription.lastrenttime%type         := '00000000000000';        --临时存放上次扣租时间
v_nextrentdate          zxdbm_200.s200_user_subscription.nextrentdate%type         := v_sysdate8;              --临时存放下次扣租日期
v_nextnextrentdate      zxdbm_200.s200_user_subscription.nextrentdate%type         := v_sysdate8;              --临时存放下下次扣租日期
v_payfordate            zxdbm_200.s200_user_subscription.payfordate%type           := '00000000';              --临时存放月租已付日期
v_returnvalues          zxdbm_200.s200_user_subscription.returnvalues%type         := 0;                       --临时存放下一周期返还值
v_returntype            zxdbm_200.s200_user_subscription.returntype%type           := 1;                       --临时存放返还量类型
v_returnstartdate       zxdbm_200.s200_user_subscription.returnstartdate%type      := '00000000';              --临时存放返还值开始日期
v_returnenddate         zxdbm_200.s200_user_subscription.returnenddate%type        := '00000000';              --临时存放返还值结束日期
v_chargeparty           zxdbm_200.s200_user_subscription.chargeparty%type          := 0;                       --临时存放定购时填入,扣月租使用,计费方 0:用户;1:SP;2:集团
v_freetime              zxdbm_200.s200_user_subscription.freetime%type             := '00000000000000';        --临时存放产品订购免费日期
v_createtime            zxdbm_200.s200_user_subscription.createtime%type           := '00000000000000';        --临时存放用户订购时间
v_canceltime            zxdbm_200.s200_user_subscription.canceltime%type           := '00000000000000';        --临时存放用户退定时间
v_firstusetime          zxdbm_200.s200_user_subscription.firstusetime%type         := '00000000000000';        --临时存放用户首次使用时间
v_lastusetime           zxdbm_200.s200_user_subscription.lastusetime%type          := '00000000000000';        --临时存放用户上次使用时间
v_tastestarttime        zxdbm_200.s200_user_subscription.tastestarttime%type       := '00000000000000';        --临时存放体验期开始时间
v_tasteendtime          zxdbm_200.s200_user_subscription.tasteendtime%type         := '00000000000000';        --临时存放体验期结束时间
v_pausetime             zxdbm_200.s200_user_subscription.pausetime%type            := '00000000000000';        --临时存放暂停时间
v_resumetime            zxdbm_200.s200_user_subscription.resumetime%type           := '00000000000000';        --临时存放暂停恢复时间
v_subscribeopid         zxdbm_200.s200_user_subscription.subscribeopid%type        := ' ';                     --临时存放定购操作员工号
v_cancelopid            zxdbm_200.s200_user_subscription.cancelopid%type           := i_opid;                  --临时存放退定操作员工号
v_cancelreason          zxdbm_200.s200_user_subscription.cancelreason%type         := ' ';                     --临时存放退定原因描述
v_lastrent              zxdbm_200.s200_user_subscription.lastrent%type             := 0;                       --临时存放上次扣租金额
v_rollbacklastrent      zxdbm_200.s200_user_subscription.lastrent%type             := 0;                       --临时存放上次扣租金额(订购关系待失效产品的订购失败回滚)
v_msisdntype            zxdbm_200.s200_user_subscription.msisdntype%type           := 1;                       --临时存放用户号码类型 1-msisdn,2-phs,3-pstn,4-IPTV接入号,5-宽带接入号,6-固定IP
v_productsubstatus      zxdbm_200.s200_user_subscription.status%type               := 0;
v_pseudocode            zxdbm_200.s200_user_subscription.pseudocode%type           := ' ';                     --临时存放订购关系表中伪码
v_oldproductid          zxdbm_200.s200_user_subscription.oldproductid%type         := ' ';                     --临时存放产品代码
--added by youqunxia20130608 order2.07.10
v_pproductofferid       zxdbm_200.s200_user_subscription.pproductofferid%type      := ' ';                     --临时存放增值产品对应的捆绑类套餐ID
--added by youqunxia20130906 order3.02.01
v_varifee               zxdbm_200.s200_user_subscription.varifee%type              := '';                      --临时存放当前订购关系中的可变价格                                    
v_eventid               zxdbm_200.s200_user_subscription.eventid%type              := '';                      --临时存放事件ID    
v_servicesort           zxdbm_200.s200_service.servicesort%type                    := 0;                       --业务归属 0:普通业务、1:属于OIDD 2:属于定位
v_servicename           zxdbm_200.s200_service.servicename%type                    := ' ';                     --临时存放业务表中的业务名称
v_isexperience          zxdbm_200.s200_user_subscription.isexperience%type         := '0';                     --订购类型:0:正常订购 1:免费体验 2:积分兑换 3:体验转正式 默认为0
v_subscribetype         zxdbm_200.s200_user_subscription.subscribetype%type        := '0';                     --订购方式:0:非批量受理 1: 批量受理 默认为0
v_chargetime            zxdbm_200.s200_user_subscription.chargetime%type           := '00000000000000';        --首次扣费时间

v_orderupdateurl        zxdbm_200.s200_service.orderupdateurl%type                 := ' ';                     --临时存放业务表中
v_subsrvtype            zxdbm_200.s200_service.subsrvtype%type                     := 1;                       --临时存放业务子类型,1-wap pull,2-wap push,3-op wap push,4-wap pull 下载,101-试用业务(也表示 wap pull试用业务) ;102:wap push 试用业务;1001 - 基本通道费;1002
v_phoneindex            zxdbm_ismp.ssys_phone.phoneindex%type                      := 0;                       --临时存放号段序号
v_attach                zxdbm_ismp.ssys_phone.attach%type                          := 0;                       --临时存放预付费归属,0-未知,1-OCS,2-RTBP



begin

--出参初始化
o_retcode               := 0;
o_debugpos             := 0;
o_debuginfo             := ' ';
o_usemsisdn             := ' ';
o_paymsisdn             := ' ';
o_usemsisdntype         := 1;
o_paymsisdntype         := 1;
o_userindex             := 0;
o_paytype               := 0;
o_oppaytype             := 0;
o_scptype               := 2;
o_subfee                   := 0;
o_feemode               := 1;
o_colortype             := 0;
o_cpid                  := ' ';
o_cpindex               := 0;
o_cpcnshortname         := ' ';
o_cpunsubscribeurl      := ' ';
o_cptype                := 0;
o_protocoltype          := 0;
o_msgtosp               := ' ';
o_customersrvtel        := ' ';
o_serviceid             := ' ';
o_serviceindex          := 0;
o_servicename           := ' ';
o_productid             := ' ';
o_productindex          := 0;
o_productname           := ' ';
o_smschargingcn         := ' ';
o_servicetype           := 0;
o_subcapability         := 1;
o_srvtypeid             := ' ';
o_corpid                := ' ';
o_corpindex             := 0;
o_corpcnshortname       := ' ';
o_subendtime            := '99991231235959';
o_linknum               := ' ';
o_productsubid          := ' ';
o_productsubindex       := 0;
o_neednotifysp          := 0;
o_pseudocode            :='';
o_pcodemsisdnswitch     :=0;
o_chanelplayerstate     :=0;
o_eventstate            :=1;
o_spaccesscode          := i_spaccesscode;
o_maincapability        := 0;
o_submaincapability     := 0;
o_cpsrvtype             :=0;
o_orderfeaturecode      := ' ';
o_cdrbegintime          := substr(v_sysdate8,1,6) || '01000000';
o_cdrendtime            := v_curmonthendtime;
o_producttype           := 1;
o_cnfmtype              := 0;
o_invalidmode           := 0;
o_cityid                := '';

--added by youqunxia 2013.01.17
o_orderaccesscode       := ' ';

o_issynctooidd          := 0;

if v_cancelopid is null then
    v_cancelopid := ' ';
end if;

/**
--part-- --telicomcancelreq-- 1.预鉴权

 发起方地址类型
 发起方用户号码去前缀

**/

--发起方地址类型鉴权 现网局点可考虑注释.
--批量操作不进行鉴权,由业务从用户表中查询处理
--modied by youqunxia20130709 Order2.07.10
if (i_actiontype <> 17 and i_lallindex <> 91) then
    if i_oatype not in (1,2,3,4) then
        o_retcode   := 107;
        o_debugpos := 1;
        o_debuginfo := 'i_oatype='||i_oatype||', not in (1,2,3,4).';
        return;
    end if;
end if;

--发起方用户号码去前缀
v_oa := ltrim(rtrim(nvl(i_oa,'0')));
if v_oa like ('+' || v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 2);
elsif v_oa like ('00' || v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 3);
elsif v_oa like (v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 1);
end if;
if (substr(v_oa,1,1) <> '0') and (i_oatype = 4) then
    v_oa := '0' || v_oa;
end if;
v_usemsisdn := v_oa;
o_usemsisdn  := v_usemsisdn;
if (v_usemsisdn = '0') then
    o_retcode   := 1001;
    o_debugpos := 2;
    o_debuginfo := 'v_usemsisdn='||v_usemsisdn||', v_usemsisdn is 0.';
    return;
end if;

/**
--part-- --telicomcancelreq-- 2.使用方号码存在性鉴权
**/

if (i_needauthuser = 1) then
    begin
        select userindex,usertype,paytype,blacklist,whitelist,
               redlist,blacklevel,whitelevel,msisdntype,status,
               activestatus,oppaytype,corpindex,lastusetime,cityid
        into   v_useuserindex,v_useusertype,v_usepaytype,v_useblacklist,v_usewhitelist,
               v_useredlist,v_useblacklevel,v_usewhitelevel,v_usemsisdntype,v_useuserstatus,
               v_useactivestatus,v_useoppaytype,v_usecorpindex,v_useusetime,v_useusercityid
        from   zxdbm_ismp.susr_basic
        where  usercode = v_usemsisdn;
    exception when no_data_found then
        o_retcode   := 1001;
        o_debugpos := 3;
        o_debuginfo := 'usercode do not exist.';
        if (i_oatype = 4) then
            v_usemsisdntype    := 2;
        else
            v_usemsisdntype    := 1;
        end if;
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 4;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        if (i_oatype = 4) then
            o_usemsisdntype    := 2;
        else
            o_usemsisdntype    := 1;
        end if;
        return;
    end;
    v_paymsisdn     := v_usemsisdn;
    v_paymsisdntype := v_usemsisdntype;
    v_payuserindex  := v_useuserindex;
    o_paymsisdn     := v_paymsisdn;
    o_usemsisdntype := v_usemsisdntype;
    o_paymsisdntype := v_paymsisdntype;
    o_userindex     := v_useuserindex;
    o_paytype       := v_usepaytype;
    o_oppaytype     := v_useoppaytype;
    o_corpindex     := v_usecorpindex;
    o_cityid        := trim(v_useusercityid);

    --批量操作用户号码类型由用户表查询后鉴权
    if (i_cmdchannel in (7,32)) then
         if v_usemsisdntype not in (1,2,3) then
             o_retcode   := 100;
             o_debugpos := 5;
             o_debuginfo := 'v_usemsisdntype='||v_usemsisdntype||', not in (1,2,3);';
             return;
         end if;
    end if;

    if (v_useredlist = 1) then
        o_colortype := 4;
    else
        o_colortype := 0;
    end if;
    if i_mutidbmode = 0 then
        if (v_useactivestatus = 0) then
            --待激活用户激活操作
            begin
                update  zxdbm_ismp.susr_basic
                set     activestatus = 1,
                        activetime = v_sysdate14
                where   userindex = v_useuserindex;
                if (sql%rowcount = 0) then
                    o_retcode   := 506;
                    o_debugpos := 6;
                    o_debuginfo := 'usercode do not exist.';
                    rollback;
                    return;
                end if;
                commit;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 7;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
        else
            --已激活用户更新最近一次使用时间
            if (i_silent = 1) then
                if (substr(v_useusetime,1,8) <> v_sysdate8) then
                    begin
                        update  zxdbm_ismp.susr_basic
                        set     lastusetime = v_sysdate14
                        where   userindex = v_useuserindex;
                        if (sql%rowcount = 0) then
                            o_retcode   := 506;
                            o_debugpos := 8;
                            o_debuginfo := 'usercode do not exist.';
                            rollback;
                            return;
                        end if;
                        commit;
                    exception when others then
                        o_retcode   := 506;
                        o_debugpos := 9;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        return;
                    end;
                end if;
            end if;
        end if;
    end if;
end if;

/**
--part-- --telicomcancelreq-- 3.产品存在性鉴权
**/

--产品互斥时,ESMGP传21个0在productid字段当中,同时传进来业务ID,此时以业务ID来获取真实的产品ID
if v_srvprodid = '000000000000000000000' then
    v_sql := 'select isservicesub from zxdbm_200.s200_service where serviceid = :p1';
    begin
        execute immediate v_sql into v_isservicesub using v_serviceid;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 10;
        o_debuginfo := 'serviceindex='||v_serviceid||', the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 11;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if v_isservicesub <> 1 then
        o_retcode   := 505;
        o_debugpos := 12;
        o_debuginfo := 'esmgp parse error.';
        return;
    end if;
    --获取真实的产品ID,用于后续的操作
    begin
        select productid
        into   v_srvprodid
        from   zxdbm_200.s200_user_subscription
        where  usercode = v_usemsisdn and serviceid = v_serviceid;
    exception when no_data_found then
        o_retcode   := 1201;
        o_debugpos := 13;
        o_debuginfo := o_debuginfo ||'v_usemsisdn = '||v_usemsisdn||'v_serviceid = '||v_serviceid||'the subscription dose not exist;';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 14;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

if (i_direct = 1 and i_supportproductid = 0) then
--反向订购并且支持业务ID的退订
    begin
        select productindex,invalidmode,validcyctype,
               validcycparam,validdate,subfeemode,subfeedays,freedays,
               popularizestart,popularizestop,popularizeitem,needcharge,
               productname,status,onlyforcorp,starttime,endtime,
               workflow,workflowlife,cpindex,serviceindex,smschargingcn,canbepresent,
               servicetype,subcapability,onlyforpkg,productid,backfeemode,lockmonth
        into   v_productindex,v_invalidmode,v_validcyctype,
               v_validcycparam,v_validdate,v_subfeemode,v_subfeedays,v_freedays,
               v_popustart,v_popustop,v_popuitem,v_needcharge,
               v_productname,v_productstatus,v_onlyforcorp,v_productstarttime,v_productendtime,
               v_workflow,v_workflowlife,v_cpindex,v_serviceindex,v_smschargingcn,v_canbepresent,
               v_servicetype,v_subcapability,v_onlyforpkg,v_productid,v_backfeemode,v_lockmonth
        from  zxdbm_200.s200_product
        where serviceid = v_srvprodid and ordertype = 1;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 15;
        o_debuginfo := 'serviceid='||v_srvprodid||', ordertype = 1'||', the product does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 16;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
else
    begin
        select productindex,invalidmode,validcyctype,
               validcycparam,validdate,subfeemode,subfeedays,freedays,
               popularizestart,popularizestop,popularizeitem,needcharge,
               productname,status,onlyforcorp,starttime,endtime,
               workflow,workflowlife,cpindex,serviceindex,smschargingcn,canbepresent,
               servicetype,subcapability,productid,backfeemode,serviceid,lockmonth
        into   v_productindex,v_invalidmode,v_validcyctype,
               v_validcycparam,v_validdate,v_subfeemode,v_subfeedays,v_freedays,
               v_popustart,v_popustop,v_popuitem,v_needcharge,
               v_productname,v_productstatus,v_onlyforcorp,v_productstarttime,v_productendtime,
               v_workflow,v_workflowlife,v_cpindex,v_serviceindex,v_smschargingcn,v_canbepresent,
               v_servicetype,v_subcapability,v_productid,v_backfeemode,v_serviceid,v_lockmonth
        from   zxdbm_200.s200_product
        where  productid = v_srvprodid;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 17;
        o_debuginfo   := 'productid='||v_srvprodid||', the product does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 18;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;
o_productid      := v_productid;
o_productname    := v_productname;
o_productindex   := v_productindex;
o_serviceindex   := v_serviceindex;
o_serviceid      := v_serviceid;
o_cpindex        := v_cpindex;
o_smschargingcn  := v_smschargingcn;
o_servicetype    := v_servicetype;
o_subcapability  := v_subcapability;
if i_servicetype = 5 then
    v_sql := 'select freeuseflag,freedays from zxdbm_200.s200_product where productindex = :p1';
    begin
        execute immediate v_sql into v_freeuseflag,v_freedays using v_productindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 19;
        o_debuginfo   := 'The product does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 20;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;
--产品状态鉴权
if (v_productstatus in (0,2) and (v_workflowlife = 3)) then
    o_producttype := 2;
end if;

--added by youqunxia20130925 order3.02.01
--产品状态为注销/预注销时,按正常处理,产品注销的优先级高于协议期
v_templockmonth := months_between(sysdate,to_date(v_productstarttime,'yyyymmddhh24miss'));
if v_productstatus in (3,4) then
    null;
elsif v_templockmonth < v_lockmonth then
    --产品协议期内不允许退订
    o_retcode   := 1009017;
    o_debugpos  := 89;
    o_debuginfo := 'The product is in the bargaining,can not cancel.';
    return;
else
    null;
end if;

--获取产品的计费信息
if (v_popuitem = 2) then  --无推广期
    v_tempitem := 0;
elsif v_popuitem in (0,1) then  --推广期免费或者推广期计费
    if (v_sysdate8 >= v_popustart) and (v_sysdate8 <= v_popustop) then
        if v_popuitem = 0 then  --推广期免费
            v_needcharge := 0;
        else
            v_needcharge := 1;
            v_tempitem := v_popuitem;
        end if;
    else
        v_tempitem := 0;
    end if;
end if;

if (v_needcharge = 1) then
    --费率存在性鉴权
    if i_servicetype in (1,2) then
        v_sql := 'select chargetype,subchargetype,cyctype,cycunit,cycparam,rentfee,fixfee,fixusetimes,unitvol,autocontinue,minlimitfee,usecomfee,nextrentfee,rentfeevalidate,totaldiscount '||
                 'from zxdbm_200.s200_product_fee where  productindex = :p1 and itemindex = :p2';
        begin
            execute immediate v_sql into v_chargetype,v_subchargetype,v_cyctype,v_cycunit,v_cycparam,v_rentfee,v_fixfee,v_fixusetimes,v_unitvol,v_autocontinue,v_minlimitfee,v_usecomfee,v_nextrentfee,v_rentfeevalidate,v_totaldiscount using v_productindex, v_tempitem;
        exception when no_data_found then
            o_retcode   := 3103;
            o_debugpos := 21;
            o_debuginfo := 'productindex='||v_productindex||', itemindex='||v_tempitem||', the product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 22;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    else
        begin
            select chargetype,subchargetype,cyctype,cycunit,cycparam,
                   rentfee,fixfee,fixusetimes,unitvol,autocontinue,minlimitfee,usecomfee,totaldiscount
            into   v_chargetype,v_subchargetype,v_cyctype,v_cycunit,v_cycparam,
                   v_rentfee,v_fixfee,v_fixusetimes,v_unitvol,v_autocontinue,v_minlimitfee,v_usecomfee,v_totaldiscount
            from   zxdbm_200.s200_product_fee
            where  productindex = v_productindex and itemindex = v_tempitem;
        exception when no_data_found then
            o_retcode   := 3103;
            o_debugpos := 23;
            o_debuginfo := 'productindex='||v_productindex||', itemindex='||v_tempitem||', the product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 24;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;

    --0资费判断,needcharge
    if v_needcharge = 0 then
        o_cnfmtype := 1;
    else
       if (v_rentfee = 0 and v_fixfee = 0 and v_minlimitfee = 0) then
            v_totaldctemplate := v_totaldiscount;
            loop
                v_index0 := instr(v_totaldctemplate,'&',1,1);
                v_dealtemplate := v_totaldctemplate;
                if v_index0 <> 0 then
                    v_totaldclength := length(v_totaldctemplate);
                    v_dealtemplate := substr(v_totaldctemplate, 1, v_index0-1);
                    v_totaldctemplate := substr(v_totaldctemplate, 1+v_index0, v_totaldclength-v_index0);
                end if;
                v_totaldclength := length(v_dealtemplate);
                v_index1 := instr(v_dealtemplate,';',1,1);
                v_dealtemplate := substr(v_dealtemplate, 1+v_index1, v_totaldclength-v_index1);
                v_totaldclength := length(v_dealtemplate);
                v_index1 := instr(v_dealtemplate,';',1,1);
                v_dealtemplate := substr(v_dealtemplate, 1, v_index1-1);
                v_totaldcsum := to_number(v_dealtemplate);
                exit when (v_totaldcsum > 0 or v_index0 = 0);
            end loop;
            if v_totaldcsum = 0 then
                o_cnfmtype := 1;
            end if;
       end if;
    end if;
end if;

--是否业务退订
if i_servicetype in (1,2,3) then
    v_sql := 'select isservicesub,servicesort from zxdbm_200.s200_service where  serviceindex = :p1';
    begin
        execute immediate v_sql into v_isservicesub,v_servicesort using v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 25;
        o_debuginfo := 'serviceindex='||v_serviceindex||', the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 26;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

--added by youqunxia20130906 order3.02.01
--是否同步订购关系给OIDD,0-否,1-是
if v_servicesort = 1 then
    o_issynctooidd := 1;
end if;

if i_servicetype <> 14 then
    begin
        select orderupdateurl,subsrvtype,servicename into v_orderupdateurl,v_subsrvtype,v_servicename
        from   zxdbm_200.s200_service
        where  serviceindex = v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 27;
        o_debuginfo := 'serviceindex='||v_serviceindex||', the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 28;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;
o_servicename := v_servicename;

if i_servicetype = 101 then
    v_sql := 'select maincapability,submaincapability '||
             'from zxdbm_9900.s9900_service where serviceindex = :p1';
    begin
        execute immediate v_sql into v_maincapability,v_submaincapability using v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 29;
        o_debuginfo   := 'serviceindex='||v_serviceindex||', the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 30;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    o_maincapability    := v_maincapability;
    o_submaincapability := v_submaincapability;
end if;

--包月类退订方式处理
if v_needcharge = 1 and v_chargetype in (2,5,7) then
    if i_cmdchannel in (1,2,36,14) then
        if i_servicetype in (1,2) or (i_servicetype = 3 and v_subsrvtype = 2) then
            v_invalidmode := 0;
        elsif i_servicetype = 3 and v_subsrvtype = 1 then
            v_invalidmode := 1;
        else
            null;
        end if;
    end if;
end if;

/*
--part-- --telicomcancelreq-- 4.系统业务能力鉴权
*/

--系统业务能力鉴权
if i_servicetype = 101 then
    begin
        select srvtypeid
        into   v_srvtypeid
        from   zxdbm_ismp.ssys_service_type
        where  servicetype = v_maincapability and subcapability = v_submaincapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 31;
        o_debuginfo   := 'servicetype='||v_maincapability||', subcapability='||v_submaincapability||', the system service capbility does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 32;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
else
    begin
        select srvtypeid
        into   v_srvtypeid
        from   zxdbm_ismp.ssys_service_type
        where  servicetype = v_servicetype and subcapability = v_subcapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 33;
        o_debuginfo   := 'servicetype='||v_servicetype||', subcapability='||v_subcapability||', the system service capbility does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 34;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;
o_srvtypeid   :=  v_srvtypeid;

/*
--part-- --telicomcancelreq-- 5.CP/SP存在性及sp相关鉴权
*/

--CP/SP存在性鉴权
begin
    select cpid,cpsrvtype,status,cpsubscribeurl,cpunsubscribeurl,hasblacklist,cptype,insubblacklist,cpcnshortname,customersrvtel
    into   v_cpid,v_cpsrvtype,v_cpstatus,v_cpsubscribeurl,v_cpunsubscribeurl,v_hasblacklist,v_cptype,v_insubblacklist,v_cpcnshortname,v_customersrvtel
    from   zxdbm_ismp.scp_basic
    where  cpindex = v_cpindex;
exception when no_data_found then
    o_retcode   := 2000;
    o_debugpos := 35;
    o_debuginfo   := 'cpindex='||v_cpindex||', the cp/sp does not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 36;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
if (v_cpunsubscribeurl is null) or (v_cpunsubscribeurl = ' ') then
    v_cpunsubscribeurl := v_cpsubscribeurl;
end if;
o_cpid             := v_cpid;
o_cptype           := v_cptype;
o_cpsrvtype        := v_cpsrvtype;
o_cpcnshortname    := v_cpcnshortname;
o_customersrvtel   := v_customersrvtel;
if (trim(v_orderupdateurl) is null) or (length(trim(v_orderupdateurl)) < 8) then
    o_cpunsubscribeurl := v_cpunsubscribeurl;
else
    o_cpunsubscribeurl := v_orderupdateurl;
end if;

--SP开通业务能力鉴权(MVS,MPSS,PIM,CPX不需要鉴权)
if i_servicetype in (5,14,33,101) then
    o_protocoltype := 0;
else
    begin
        select protocoltype
        into   v_protocoltype
        from   zxdbm_200.s200_sp_service_type
        where  cpindex = v_cpindex and servicetype = v_servicetype and subcapability = v_subcapability;
    exception when no_data_found then
       o_retcode   := 2100;
       o_debugpos := 37;
       o_debuginfo   := 'v_cpindex='||v_cpindex||', v_servicetype='||v_servicetype||', v_subcapability='||v_subcapability||', the opened service capability of the SP does not exist.';
       return;
    when others then
       o_retcode   := 506;
       o_debugpos := 38;
       o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
       return;
    end;
    o_protocoltype := v_protocoltype;
end if;

--SP接入码查询
if (i_spaccesscode is null) or (length(trim(i_spaccesscode)) = 0) then
    begin
        select spaccesscode into v_spaccesscode
        from zxdbm_ismp.ssrv_srvorder
        where productindex = v_productindex and cmdtype = i_actiontype and servicetype = i_servicetype and rownum = 1;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 39;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if trim(v_spaccesscode) is null then
        begin
            select accesscode into v_spaccesscode
            from zxdbm_ismp.ssrv_spaccesscode
            where cpid = v_cpid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 40;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    o_spaccesscode := v_spaccesscode;
end if;

--订购指令查询
begin
--modified by youqunxia 2013.01.17  added accesscode
    select featurecode,accesscode
    into   o_orderfeaturecode,o_orderaccesscode
    from   zxdbm_ismp.ssrv_srvorder
    where  productid = v_productid and cmdtype = 1 and rownum = 1;
exception when no_data_found then
    null;
when others then
    o_retcode   := 506;
    o_debugpos := 41;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;

--EventID和ChanelPlayerID 状态查询
if i_actiontype = 2 then
    begin
        select status into o_chanelplayerstate from zxdbm_ismp.channel_basic where cpid = i_channelid;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 42;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    begin
        select status into o_eventstate from zxdbm_ismp.ssys_event where eventid = i_eventid;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 43;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

--伪码处理(支持伪码开关打开,非自营sp)--支持业务LCS,SMS,MVS,DLS,MMS,WAP,CPX
--退订时,AuthPriceReq消息中OA(DA)真实号码转化为伪码,同步SP
--这里订购业务的回滚订购关系不做处理

if i_servicetype in (1,2,3,4,41,5,6,101) then
    if i_actiontype <> 1 and i_catchflag <> 1 then
        if i_checkpseudocode = 1 and v_cpsrvtype <> 3 then
            begin
                select pseudocode into o_pseudocode from zxdbm_ismp.susr_basic where usercode = i_oa;
            exception when no_data_found then
                o_retcode   := 1001;
                o_debugpos := 44;
                o_debuginfo := 'i_oa = '||i_oa||', the user dose not exist.';
                return;
            when others then
                o_retcode   := 506;
                o_debugpos := 45;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            o_pcodemsisdnswitch := 1;
        end if;
    end if;
end if;

--如果集团专用产品属性为:必须集团管理员退定,则必须在集团门户上退定
if (v_onlyforcorp = 1) then
    begin
        select  corpid
        into    v_usecorpid
        from    zxdbm_ismp.susr_corp
        where   corpindex = v_usecorpindex;
    exception when no_data_found then
        o_retcode   := 1001301;  --集团不存在
        o_debugpos := 46;
        o_debuginfo := 'v_usecorpindex = '||v_usecorpindex||', the corp dose not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 47;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    o_corpid  := v_usecorpid;
    --本集团的产品只能由本集团的用户退订
    begin
        select unsubscribemode,payparty
        into   v_productcancelmode,v_productpayparty
        from   zxdbm_ismp.ssrv_corp_product
        where  corpindex = v_usecorpindex and groupindex = 0 and productindex = v_productindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 48;
        o_debuginfo := 'corpindex = '||v_usecorpindex||', productindex = '||v_productindex||', the product of the corp dose not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 49;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if (v_productcancelmode = 0 and i_rollback = 0) then
        --必须集团管理员退订
        if (v_cmdchannel <> 4) then
            o_retcode   := 1001214;    --无权退订
            o_debugpos := 50;
            o_debuginfo := 'v_productcancelmode = '||v_productcancelmode||', v_cmdchannel = '||v_cmdchannel||', the operator has no privilege to cancel the subscription.';
            return;
        end if;
    end if;
else
    v_usecorpindex  := 0;
    o_corpindex     := v_usecorpindex;
end if;

/*
--part-- --telicomcancelreq-- 产品订购关系存在性鉴权(是否业务退订)
/

if i_issecondproduct = 1 or v_isservicesub = 1 then
    begin
        select subscriptionindex,subscriptionid,servicetype,subcapability,
               serviceindex,serviceid,productindex,productid,productpkgindex,
               productpkgid,contentindex,contentid,corpindex,corpid,cpindex,
               cpid,subscribemode,subscribechannel,subscribeoptype,
               pausemode,resumemode,status,autocontinue,subscriber,usercode,
               payuser,pushid,starttime,endtime,lastrenttime,
               nextrentdate,payfordate,returnvalues,returntype,
               returnstartdate,returnenddate,fixusetimes,chargeparty,
               freetime,createtime,canceltime,firstusetime,lastusetime,
               tastestarttime,tasteendtime,pausetime,resumetime,
               subscribeopid,cancelreason,lastrent,msisdntype,pseudocode,
               oldproductid,pproductofferid,varifee,eventid,
               isexperience,subscribetype,chargetime
        into   v_productsubindex,v_productsubid,v_servicetype,v_subcapability,
               v_serviceindex,v_serviceid,v_productindex,v_productid,v_propkgindex,
               v_propkgid,v_contentindex,v_contentid,v_corpindex,v_corpid,v_cpindex,
               v_cpid,v_subscribemode,v_subscribechannel,v_subscribeoptype,
               v_pausemode,v_resumemode,v_productsubstatus,v_autocontinue,v_subscriber,v_usercode,
               v_payuser,v_pushid,v_substarttime,v_cursubendtime,v_lastrenttime,
               v_nextrentdate,v_payfordate,v_returnvalues,v_returntype,
               v_returnstartdate,v_returnenddate,v_fixusetimes,v_chargeparty,
               v_freetime,v_createtime,v_canceltime,v_firstusetime,v_lastusetime,
               v_tastestarttime,v_tasteendtime,v_pausetime,v_resumetime,
               v_subscribeopid,v_cancelreason,v_lastrent,v_msisdntype,v_pseudocode,
               v_oldproductid,v_pproductofferid,v_varifee,v_eventid,
               v_isexperience,v_subscribetype,v_chargetime
        from   zxdbm_200.s200_user_subscription
        where  usercode = v_usemsisdn and serviceid = v_serviceid;
    exception when no_data_found then
        o_retcode   := 1201;
        o_debugpos := 51;
        o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_serviceid = '||v_serviceid||', the subscription dose not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 52;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
else
    begin
        select subscriptionindex,subscriptionid,servicetype,subcapability,
               serviceindex,serviceid,productindex,productid,productpkgindex,
               productpkgid,contentindex,contentid,corpindex,corpid,cpindex,
               cpid,subscribemode,subscribechannel,subscribeoptype,
               pausemode,resumemode,status,autocontinue,subscriber,usercode,
               payuser,pushid,starttime,endtime,lastrenttime,
               nextrentdate,payfordate,returnvalues,returntype,
               returnstartdate,returnenddate,fixusetimes,chargeparty,
               freetime,createtime,canceltime,firstusetime,lastusetime,
               tastestarttime,tasteendtime,pausetime,resumetime,
               subscribeopid,cancelreason,lastrent,msisdntype,pseudocode,
               oldproductid,pproductofferid,varifee,eventid,
               isexperience,subscribetype,chargetime
        into   v_productsubindex,v_productsubid,v_servicetype,v_subcapability,
               v_serviceindex,v_serviceid,v_productindex,v_productid,v_propkgindex,
               v_propkgid,v_contentindex,v_contentid,v_corpindex,v_corpid,v_cpindex,
               v_cpid,v_subscribemode,v_subscribechannel,v_subscribeoptype,
               v_pausemode,v_resumemode,v_productsubstatus,v_autocontinue,v_subscriber,v_usercode,
               v_payuser,v_pushid,v_substarttime,v_cursubendtime,v_lastrenttime,
               v_nextrentdate,v_payfordate,v_returnvalues,v_returntype,
               v_returnstartdate,v_returnenddate,v_fixusetimes,v_chargeparty,
               v_freetime,v_createtime,v_canceltime,v_firstusetime,v_lastusetime,
               v_tastestarttime,v_tasteendtime,v_pausetime,v_resumetime,
               v_subscribeopid,v_cancelreason,v_lastrent,v_msisdntype,v_pseudocode,
               v_oldproductid,v_pproductofferid,v_varifee,v_eventid,
               v_isexperience,v_subscribetype,v_chargetime
        from   zxdbm_200.s200_user_subscription
        where  usercode = v_usemsisdn and productid = v_productid;
    exception when no_data_found then
        o_retcode   := 1201;
        o_debugpos := 53;
        o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_productid = '||v_productid||', the subscription dose not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 54;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

if v_productsubstatus in (4,5) then
    o_retcode   := 1202;
    o_debugpos := 55;
    o_debuginfo := 'status of the subscription is abnormal.';
    return;
elsif (v_productsubstatus = 6) or (v_cursubendtime < v_sysdate14) then
    o_retcode   := 1201;
    o_debugpos := 56;
    o_debuginfo := 'The subscription does not exist.';
    return;
end if;

--added by youqunxia20130609   order2.07.10
--判断是否属于捆绑品
if trim(v_pproductofferid) is not null and i_cmdchannel <> 40 then
    if i_actiontype = 10 then
        null;
    else
        o_retcode   := 1001229;
        o_debugpos  := 58;
        o_debuginfo := 'The product is binding,unsubscribe illegal .';
        return;
    end if;
end if;

if  (v_freetime > v_substarttime) and (v_freetime > v_sysdate14) then
    v_invalidmode := 0;
elsif (i_servicetype = 5) then
    v_invalidmode := 1; --MVS业务门户上不显示该字段,缺省退订都为下帐期生效
end if;

--added by youqunxia20130910 order3.02.01
--VSOP侧发起捆绑类套餐、增值产品的订购激活、变更激活及退订,ISMP中增值子产品订购关系立即生效(包括订购、退订及变更),不以增值产品属性判断生效时间;          
if (i_cmdchannel = 40) and (i_effectiveswitch = 1)then
    v_invalidmode := 0;
end if;
o_invalidmode := v_invalidmode;

--沉默用户清理订购关系判断
if i_cmdchannel = 20 then
    if v_lastusetime = '00000000000000' then
        v_silentdays := to_date(v_sysdate14,'yyyymmddhh24miss') - to_date(v_substarttime,'yyyymmddhh24miss');
    else
        v_silentdays := to_date(v_sysdate14,'yyyymmddhh24miss') - to_date(v_lastusetime,'yyyymmddhh24miss');
    end if;

    if (v_silentdays < 30) then
        o_retcode   := 1001224;        --不满足沉默用户清理订购关系条件
        o_debugpos := 59;
        o_debuginfo := 'v_lastusetime = '||v_lastusetime||', it is not allowed to delete subscription of silent user.';
        return;
    end if;
end if;

--(除CRM发起的退订)套餐下的产品不允许单独退订
if  (v_propkgindex <> 0 and i_cmdchannel not in (9,35,39,40)) then
    o_retcode   := 1001214;
    o_debugpos := 60;
    o_debuginfo := 'the product in pkg is not allowed to be unsubscribed alone.';
    return;
end if;
o_paymsisdn  :=  v_payuser;
o_productsubindex := v_productsubindex;
o_productsubid    := v_productsubid;

--根据i_needreplay决定鉴权是否继续
if (i_needreplay = 1) then
    begin
        v_tmpint  := dbms_random.value(1,9999999999);
        o_linknum := v_sysdate14 || '655380' || lpad(to_char(v_tmpint),10,'0');
    exception when others then
        o_retcode   := 506;
        o_debugpos := 61;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    return;
end if;

/*
--part-- --telicomcancelreq-- 6.计费方设置
*/

if (v_productpayparty = 1) then
    v_paymsisdn     := v_usecorpid;
    o_paytype       := 0;
    o_paymsisdn     := v_paymsisdn;
    v_payuserindex  := v_corpindex;
    o_cityid        := trim(' ');
else
    --赠送时,订购关系只能由使用方退订,此时需要将付费方的信息带出存储过程
    if (v_payuser <> v_usemsisdn) then
        v_paymsisdn := v_payuser;
        o_paymsisdn := v_paymsisdn;
        begin
            select msisdntype,paytype,oppaytype,cityid
            into   v_paymsisdntype,v_paypaytype,v_payoppaytype,v_payusercityid
            from   zxdbm_ismp.susr_basic
            where  usercode = v_payuser;
        exception when no_data_found then
            o_retcode   := 1001;
            o_debugpos := 62;
            o_debuginfo := 'v_payuser = '||v_payuser||', user dose not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 63;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        o_paytype        := v_paypaytype;
        o_oppaytype      := v_payoppaytype;
        o_paymsisdntype  := v_paymsisdntype;
        o_cityid         := trim(v_payusercityid);
    end if;
    if (i_prepaidtype = 1) then
        o_scptype := 1;
    elsif i_prepaidtype = 2 then
        o_scptype := 2;
    else
        --attach 预付费归属,0-未知,1-OCS,2-RTBP
        begin
            select phoneindex,attach
            into   v_phoneindex,v_attach
            from   zxdbm_ismp.ssys_phone
            where  startprefix <= substr(v_paymsisdn,1,length(startprefix))
            and    endprefix >= substr(v_paymsisdn,1,length(endprefix));
        exception when no_data_found then
            o_scptype  := 2;
            v_devexist := 0; --表示不存在对应号段对应设备配置
        when others then
            o_retcode   := 506;
            o_debugpos := 64;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        if (v_devexist = 1) then
            if (v_attach = 1) then
                o_scptype := 1;
            elsif (v_attach = 2) then
                o_scptype := 2;
            else
                begin
                    select 1
                    into   o_scptype
                    from   zxdbm_ismp.sdev_phone
                    where  phoneindex = v_phoneindex
                    and    devicetype = 5;
                exception when no_data_found then
                    o_scptype := 2;
                when others then
                    o_retcode   := 506;
                    o_debugpos := 65;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    return;
                end;
            end if;
         end if;
    end if;
end if;

/**
--part-- --telicomcancelreq 本次退订需要扣取或者补回的费用,退订立即生效订购业务才扣费
**/

if v_chargetype in (3,6,8) then
    --包周期补款;(包周期产品退订不需要扣费,业务或者月租在周期第一天已经扣取)
    --门户申请包周期的产品时,退订都是立即生效的,所以没有对退订生效方式进行判断
    if (v_backfeemode = 1) and (v_lastrenttime <> '00000000000000') then
        if v_cycunit = 1 then
            v_nextrentdatecycle := to_char(to_date(v_lastrenttime,'YYYYMMDDHH24MISS') + v_cycparam,'YYYYMMDDHH24MISS');
        elsif v_cycunit = 2 then
            v_nextrentdatecycle := to_char(to_date(v_lastrenttime,'YYYYMMDDHH24MISS') + v_cycparam * 7,'YYYYMMDDHH24MISS');
        elsif v_cycunit = 3 then
            v_nextrentdatecycle := to_char(add_months(to_date(v_lastrenttime,'YYYYMMDDHH24MISS'), v_cycparam),'YYYYMMDDHH24MISS');
        elsif v_cycunit = 4 then
            v_nextrentdatecycle := to_char(add_months(to_date(v_lastrenttime,'YYYYMMDDHH24MISS'), v_cycparam * 6),'YYYYMMDDHH24MISS');
        elsif v_cycunit = 5 then
            v_nextrentdatecycle := to_char(add_months(to_date(v_lastrenttime,'YYYYMMDDHH24MISS'), v_cycparam * 12),'YYYYMMDDHH24MISS');
        end if;

        if v_nextrentdatecycle > v_sysdate14 then
            v_monthdays := floor(to_date(v_nextrentdatecycle, 'YYYYMMDDHH24MISS') - to_date(v_lastrenttime, 'YYYYMMDDHH24MISS'));
            v_leftdays  := floor(to_date(v_nextrentdatecycle, 'YYYYMMDDHH24MISS') - to_date(v_sysdate14, 'YYYYMMDDHH24MISS'));
        end if;
        o_subfee     := v_lastrent * v_leftdays / v_monthdays;
        o_feemode := 2;
    end if;
elsif v_chargetype in (2,5,7) then
    if (substr(v_lastrenttime,1,6) = v_sysmonth6) then
        --本月租金已扣除
        --对包月类产品和纯包量产品都会补款
        if v_backfeemode = 1 then
            if (v_productsubstatus in (0,2) or (v_productsubstatus in (1,3) and v_invalidmode = 0)) then
                --0-待生效;1-正常;2:暂停+待生效;3:暂停;4:待失效;5:暂停+待失效;6:注销
                v_leftdays  := to_number(substr(v_curmonthendtime,1,8)) - to_number(v_sysdate8);
                if (substrb(v_sysdate14,1,6) = substrb(v_substarttime,1,6)) then

-- v_monthdays := to_number(substr(v_curmonthendtime,1,8)) - to_number(substr(v_substarttime,1,8)) + 1;

                    v_monthdays := to_number(substr(v_curmonthendtime,7,2));
                    if v_subfeemode in (1,-1) then
                        o_subfee  := v_lastrent * v_leftdays / v_monthdays;
                        o_feemode := 2;
                    end if;
                else
                    v_monthdays := to_number(substr(v_curmonthendtime,7,2));
                    o_subfee  := v_lastrent * v_leftdays / v_monthdays;
                    o_feemode := 2;
                end if;
               /*
                v_leftdays  := to_number(substr(v_curmonthendtime,1,8)) - to_number(v_sysdate8);
                if (substrb(v_sysdate14,1,6) = substrb(v_substarttime,1,6)) then
                    v_monthdays := to_number(substr(v_curmonthendtime,1,8)) - to_number(substr(v_substarttime,1,8)) + 1;
                else
                    v_monthdays := to_number(substr(v_curmonthendtime,7,2));
                end if;
                o_subfee     := v_lastrent * v_leftdays / v_monthdays;
                o_feemode := 2;
                */
            end if;
        end if;
    elsif (substr(v_lastrenttime,1,6) < v_sysmonth6) then
        --本月租金未扣除
        o_feemode := 1;
        if (v_productsubstatus in (0,2) or (v_productsubstatus in (1,3) and v_invalidmode = 0)) then
            if (substr(v_substarttime,1,6) = v_sysmonth6) then
                --产品订购时间在当月
                if (v_needcharge = 1) and (v_freetime < v_sysdate14) and (v_substarttime < v_sysdate14) then
                    if v_chargetype not in (2,3,4,5,6,7,8) or (v_isservicesub = 1 and trim(v_oldproductid) is not null and v_productid <> v_oldproductid) then
                        o_subfee := 0;
                    else
                        o_subfee := v_fixfee;
                        if v_chargetype in (2,5,7) then
                            if i_servicetype in (1,2) then
                                if (v_rentfee <> v_nextrentfee) and (substr(v_rentfeevalidate,1,6) = v_sysmonth6) then
                                    v_rentfee := v_nextrentfee;
                                end if;
                            end if;
                            if v_backfeemode = 1 and v_subfeemode = 1 then
                                v_subfeemode := 4;
                            end if;
                            if (v_subfeemode = 1) then
                                --收取本帐期全部费用
                                o_subfee := o_subfee + v_rentfee;
                            elsif (v_subfeemode = 2) then
                                --上半月订购收取全月费用,下半月订购收取半月费用
                                if (to_number(substr(v_substarttime,7,2)) - 15 > 0) then
                                    o_subfee := o_subfee + v_rentfee/2;
                                else
                                    o_subfee := o_subfee + v_rentfee;
                                end if;
                            elsif (v_subfeemode = 3) then
                                --n日全费含n日,n日后免费
                                if (v_subfeedays = 0) then
                                    --0表示本月全部免费,其他表示 N号之后免费。
                                    null;
                                elsif (to_number(substr(v_substarttime,7,2)) - v_subfeedays <= 0) then
                                    o_subfee := o_subfee + v_rentfee;
                                else
                                    null;
                                end if;
                            elsif (v_subfeemode = 4) then
                                --实际天数收费
                                v_leftdays  := floor(to_number(to_date(v_sysdate14,'yyyymmddhh24miss') - to_date(v_freetime,'yyyymmddhh24miss')))+1;
                                v_monthdays := to_number(substr(v_curmonthendtime,7,2));
                                o_subfee       := o_subfee + v_rentfee*(v_leftdays/v_monthdays);
                            end if;
                        end if;
                    end if;
                end if;
            else
                if (v_needcharge = 1) and (v_freetime < v_sysdate14) and (v_substarttime < v_sysdate14) then
                    if v_chargetype not in (2,3,4,5,6,7,8) then
                        o_subfee := 0;
                    else
                        o_subfee := v_fixfee;
                        if v_chargetype in (2,5,7) then
                            --产品订购时间不在当月
                            if i_servicetype in (1,2) then
                                if (v_rentfee <> v_nextrentfee) and (substr(v_rentfeevalidate,1,6) = v_sysmonth6) then
                                    v_rentfee := v_nextrentfee;
                                end if;
                            end if;
                            if (v_backfeemode = 1) then
                                v_leftdays  := to_number(substr(v_sysdate8,7,2));
                                v_monthdays := to_number(substr(v_curmonthendtime,7,2));
                                o_subfee := o_subfee + v_rentfee * v_leftdays / v_monthdays;
                            else
                                o_subfee := o_subfee + v_rentfee;
                            end if;
                        end if;
                    end if;
                end if;
            end if;
        end if;
    end if;
else
    if (substr(v_lastrenttime,1,6) = v_sysmonth6) then
        --本月租金已扣除
        --对包月类产品和纯包量产品都会补款
        if (v_backfeemode = 1) then
            if (v_productsubstatus in (0,2) or (v_productsubstatus in (1,3) and v_invalidmode = 0)) then
                --0-待生效;1-正常;2:暂停+待生效;3:暂停;4:待失效;5:暂停+待失效;6:注销
                v_leftdays  := to_number(substr(v_curmonthendtime,1,8)) - to_number(v_sysdate8);
                if (substrb(v_sysdate14,1,6) = substrb(v_substarttime,1,6)) then
                    v_monthdays := to_number(substr(v_curmonthendtime,1,8)) - to_number(substr(v_substarttime,1,8)) + 1;
                else
                    v_monthdays := to_number(substr(v_curmonthendtime,7,2));
                end if;
                o_subfee     := v_lastrent * v_leftdays / v_monthdays;
                o_feemode := 2;
            end if;
        end if;
    elsif (substr(v_lastrenttime,1,6) < v_sysmonth6) then
        --本月租金未扣除
        o_feemode := 1;
        if (v_productsubstatus in (0,2) or (v_productsubstatus in (1,3) and v_invalidmode = 0)) then
            if (substr(v_substarttime,1,6) = v_sysmonth6) then
                --产品订购时间在当月
                if (v_needcharge = 1) and (v_freetime < v_sysdate14) and (v_substarttime < v_sysdate14) then
                    if v_chargetype not in (2,3,4,5,6,7,8) then
                        o_subfee := 0;
                    else
                        o_subfee := v_fixfee;
                        if v_chargetype in (2,5,7) then
                            if i_servicetype in (1,2) then
                                if (v_rentfee <> v_nextrentfee) and (substr(v_rentfeevalidate,1,6) = v_sysmonth6) then
                                    v_rentfee := v_nextrentfee;
                                end if;
                            end if;
                            if (v_backfeemode = 1) then
                                v_subfeemode := 4;
                            end if;
                            if (v_subfeemode = 1) then      --收取本帐期全部费用
                                o_subfee := o_subfee + v_rentfee;
                            elsif (v_subfeemode = 2) then   --上半月订购收取全月费用,下半月订购收取半月费用
                                if (to_number(substr(v_substarttime,7,2)) - 15 > 0) then
                                    o_subfee := o_subfee + v_rentfee/2;
                                else
                                    o_subfee := o_subfee + v_rentfee;
                                end if;
                            elsif (v_subfeemode = 3) then   --n日全费含n日,n日后免费
                                if (v_subfeedays = 0) then    --0表示本月全部免费,其他表示 N号之后免费。
                                    null;
                                elsif (to_number(substr(v_substarttime,7,2)) - v_subfeedays <= 0) then
                                    o_subfee := o_subfee + v_rentfee;
                                else
                                    null;
                                end if;
                            elsif (v_subfeemode = 4) then   --实际天数收费
                                v_leftdays  := floor(to_number(to_date(v_sysdate14,'yyyymmddhh24miss') - to_date(v_freetime,'yyyymmddhh24miss')))+1;
                                v_monthdays := to_number(substr(v_curmonthendtime,7,2));
                                o_subfee       := o_subfee + v_rentfee*(v_leftdays/v_monthdays);
                            end if;
                        end if;
                    end if;
                end if;
            else
                if (v_needcharge = 1) and (v_freetime < v_sysdate14) and (v_substarttime < v_sysdate14) then
                    if v_chargetype not in (2,3,4,5,6,7,8) then
                        o_subfee := 0;
                    else
                        o_subfee := v_fixfee;
                        if v_chargetype in (2,5,7) then
                            --产品订购时间不在当月
                            if i_servicetype in (1,2) then
                                if (v_rentfee <> v_nextrentfee) and (substr(v_rentfeevalidate,1,6) = v_sysmonth6) then
                                    v_rentfee := v_nextrentfee;
                                end if;
                            end if;
                            if (v_backfeemode = 1) then
                                v_leftdays  := to_number(substr(v_sysdate8,7,2));
                                v_monthdays := to_number(substr(v_curmonthendtime,7,2));
                                o_subfee := o_subfee + v_rentfee * v_leftdays / v_monthdays;
                            else
                                o_subfee := o_subfee + v_rentfee;
                            end if;
                        end if;
                    end if;
                end if;
            end if;
        end if;
    end if;
end if;
--首次使用扣费但是未使用/在线包月但是本月未使用的情况不扣费
if (substr(v_lastrenttime,1,6) < v_sysmonth6) then
    if (v_subchargetype = 2 and v_firstusetime = '00000000000000') or (v_subchargetype = 3 and substr(v_lastusetime,1,6) <> v_sysmonth6) then
         o_subfee := 0;
    end if;
end if;
if (o_subfee < v_minlimitfee) and (o_feemode = 1) then
     o_subfee := v_minlimitfee;
end if;
--用户状态为用户主动停机(用户状态为1)、用户主动停机及欠费停机(用户状态为3)两种时,不计费
if v_useuserstatus in (1,3) then
    o_subfee := 0;
    o_feemode := 1;
end if;
if (o_subfee > 0) and (o_feemode = 1) and (v_chargetype in (2,5,7)) then
    v_lastrenttime := v_sysdate14;
    v_payfordate   := v_sysdate8;
end if;
if (v_freetime > v_sysdate14 and o_subfee = 0) then
    o_cnfmtype := 2;
end if;

/**
--part-- --telicomcancelreq-- 方式、退订渠道
**/

v_canceloptype  := 0;
--设置退订方式
if (i_cmdchannel = 6) then
    v_cancelmode := 4;
elsif (i_cmdchannel in (0,20)) then
    v_cancelmode := 3;
elsif (i_cmdchannel = 4) then
    v_cancelmode := 5;
else
    v_cancelmode := 2;
end if;

--设置退订渠道
begin
    select portalchannel into v_cancelchannel
    from zxdbm_ismp.ssrv_channel_map
    where innerchannel = i_cmdchannel;
exception when no_data_found then
    v_cancelchannel := i_cmdchannel * 1000;
when others then
    o_retcode   := 506;
    o_debugpos := 66;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;

/**
--part-- --telicomcancelreq-- 订购关系结束时间
**/

if i_servicetype = 5 then
    --对MVS业务的订购关系失效时间做特殊处理
    if v_chargetype in (3,6,8) and v_cyctype = 0 then --如果为非周期性
        v_subendtime := v_cursubendtime;
    elsif v_chargetype in (3,6,8) then
        --如果为周期性(MVS的包周期计费只存在包天方式)
        v_subendtime := to_char(to_date(v_nextrentdate,'yyyymmdd') - 1,'yyyymmdd') || '235959';
    elsif v_chargetype in (2,5,7) then
        if v_invalidmode = 0 then
            v_subendtime := v_sysdate14;
        elsif v_invalidmode = 1 then
            v_subendtime := to_char(last_day(to_date(v_sysdate14,'yyyymmddhh24miss')),'yyyymmdd') || '235959';
        end if;
    end if;
else
    if (v_invalidmode = 0) then
        v_subendtime := v_sysdate14;
    --对于非立即生效的产品按照v_chargetype分别处理
    elsif v_chargetype in (2,5,7) then
        --added by youqunxia20130924 order3.02.01
        if v_invalidmode = 2 and v_cyctype = 1 then
            if v_lastrenttime ='00000000000000' and v_freetime > v_substarttime then
                --如果为免费期内退订,已将v_invalidmode置0,此处处理免费期后,扣租前的退订的时间计算
                v_nextnextrentdate := to_char(add_months(to_date(v_nextrentdate,'yyyymmdd') ,1 ),'yyyymmdd');
                if substr(v_nextrentdate,7,2) < substr(v_nextnextrentdate,7,2) then
                    v_nextnextrentdate := substr(v_nextnextrentdate,1,6) || substr(v_nextrentdate,7,2);
                elsif substr(v_nextrentdate,7,2) > substr(v_nextnextrentdate,7,2) then
                    v_nextnextrentdate := to_char(add_months(to_date(v_nextnextrentdate,'yyyymmdd') ,1 ),'yyyymm')||'01';
                end if;
                v_subendtime := to_char(to_date(v_nextnextrentdate,'YYYYMMDD') - 1,'YYYYMMDD')||'235959';
            else
                v_subendtime := to_char((to_date(v_nextrentdate,'YYYYMMDD') -1 ),'YYYYMMDD')||'235959';
            end if;
        elsif v_invalidmode = 2 and v_cyctype = 0 then
            v_subendtime := v_cursubendtime;
        else
            v_subendtime := v_curmonthendtime;
        end if;
    elsif v_chargetype in (3,6,8) then
        if v_cyctype = 0 then
            v_subendtime := v_cursubendtime;
        elsif v_cyctype = 1 then
            if v_lastrenttime ='00000000000000' and v_freetime > v_substarttime then
                --如果为免费期内退订,已将v_invalidmode置0,此处处理免费期后,扣租前的退订的时间计算
                --包周期的产品的试用期都在账期外
                --免费产品是没有试用期的
                if (v_cycunit = 1) then
                    --包天
                    v_nextnextrentdate := to_char(to_date(v_nextrentdate,'yyyymmddhh24miss') + v_cycparam,'yyyymmdd');
                elsif (v_cycunit = 2) then
                    --包周
                    v_nextnextrentdate := to_char(to_date(v_nextrentdate,'yyyymmddhh24miss') + v_cycparam * 7,'yyyymmdd');
                elsif (v_cycunit = 3) then
                    --包月(如果目的月份没有31号,add_month函数会自动取月最后一天)
                    v_nextnextrentdate := to_char(add_months(to_date(v_nextrentdate,'yyyymmddhh24miss'),v_cycparam),'yyyymmdd');
                elsif (v_cycunit = 4) then
                    --包半年
                    v_nextnextrentdate := to_char(add_months(to_date(v_nextrentdate,'yyyymmddhh24miss'),v_cycparam * 6),'yyyymmdd');
                elsif (v_cycunit = 5) then
                    --包年
                    v_nextnextrentdate := to_char(add_months(to_date(v_nextrentdate,'yyyymmddhh24miss'),v_cycparam * 12),'yyyymmdd');
                elsif (v_cycunit = 9) then
                    --包季度
                    v_nextnextrentdate := to_char(add_months(to_date(v_nextrentdate,'yyyymmddhh24miss'),v_cycparam * 3),'yyyymmdd');
                end if;
                v_subendtime := to_char(to_date(v_nextnextrentdate,'YYYYMMDD') - 1,'YYYYMMDD')||'235959';
            else
                v_subendtime := to_char(to_date(v_nextrentdate,'YYYYMMDD') -1 ,'YYYYMMDD')||'235959';
            end if;
        end if;
        --若本周期的结束时间超过本月,则默认为本月月底生效。
        if v_subendtime > v_curmonthendtime then
            v_subendtime := v_curmonthendtime;
        end if;
    else
        --对于v_chargetype为(0/1/4),如果非立即生效,默认设置为下月生效。
        v_subendtime := v_curmonthendtime;
    end if;
    /*
    if (v_invalidmode = 0) then
        v_subendtime := v_sysdate14;
    else
        v_subendtime := to_char(last_day(to_date(v_sysdate14,'yyyymmddhh24miss')) + 1,'yyyymmdd') || '000000';
    end if;
    */
end if;
o_subendtime := v_subendtime;

/**
--part-- --telicomcancelreq-- 退订后的状态设置
**/

--modied by youqunxia20130924 order3.02.01
if (v_productsubstatus = 1) and (v_invalidmode in (1,2)) then
--正常+下一帐期实效/退定后可使用足月, 直接更新订购关系为待失效
    v_substatus := 4;
elsif (v_productsubstatus = 3) and (v_invalidmode in (1,2)) then
--暂停+下一帐期实效/退定后可使用足月,直接更新订购关系为暂停+待失效
    v_substatus := 5;
else
    v_substatus := 6;
end if;

--added by youqunxia20130820 order3.02.01
--订购关系状态正常和暂停时退订时,把下次扣租日期改为订购关系结束的后一天,避免月租再次扣租
if (v_productsubstatus in (1,3)) and (v_invalidmode = 2) then
    v_nextrentdate := to_char(to_date(v_subendtime,'yyyymmddhh24miss') + 1,'yyyymmdd');
end if;

/**
--part-- --telicomcancelreq-- 设置话单中的begintime、endtime
**/

if substr(v_substarttime,1,6) = substr(v_sysdate8,1,6) then
    o_cdrbegintime := v_substarttime;
else
    if(v_subendtime < o_cdrendtime) then
        o_cdrendtime := v_subendtime;
    end if;
end if;
v_subscribeoptype := 3;

/**
--part-- --telicomcancelreq-- 事务开始
**/

if (v_productsubstatus in(1,3)) and (v_invalidmode in(1,2)) and (i_rollback = 0) then
--处于正常或者暂停状态,并且下一帐期失效的产品订购关系:更新订购关系
    begin
        update zxdbm_200.s200_user_subscription
        set    endtime       = v_subendtime,
               status        = v_substatus,
               subscribeoptype = v_subscribeoptype,
               canceltime    = v_sysdate14,
               statustime    = v_sysdate14,
               cancelmode    = v_cancelmode,
               cancelchannel = v_cancelchannel,
               canceloptype  = v_canceloptype,
               cancelopid    = v_cancelopid,
               nextrentdate  = v_nextrentdate
        where usercode = v_usemsisdn and productid = v_productid;
        if (sql%rowcount = 0) then
            o_retcode   := 506;
            o_debugpos := 67;
            o_debuginfo := 'v_usemsisdn='||v_usemsisdn||', v_productid='||v_productid||', the record of user_subscription do not exist.';
            rollback;
            return;
        end if;
    exception when others then
        o_retcode   := 506;
        o_debugpos := 68;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        rollback;
        return;
    end;
    commit;
    o_neednotifysp := 1;
else
    begin
        --事务开始
        if (i_rollback = 0) then
            v_sql := 'insert into zxdbm_200.s200_user_subs_hist' || v_curstrmonth ||
            '(subscriptionindex,subscriptionid,servicetype,subcapability,
            serviceindex,serviceid,productindex,productid,contentindex,contentid,
            productpkgindex,productpkgid,corpindex,corpid,cpindex,cpid,subscribemode,
            subscribechannel,subscribeoptype,cancelmode,cancelchannel,canceloptype,
            pausemode,resumemode,status,statustime,autocontinue,subscriber,usercode,
            payuser,pushid,starttime,endtime,lastrenttime,nextrentdate,payfordate,
            returnvalues,returntype,returnstartdate,returnenddate,fixusetimes,
            chargeparty,freetime,createtime,canceltime,firstusetime,lastusetime,
            tastestarttime,tasteendtime,pausetime,resumetime,subscribeopid,
            cancelopid,cancelreason,lastrent,msisdntype,pseudocode,pproductofferid,
            varifee,eventid,isexperience,subscribetype,chargetime)
            values(:p1,:p2,:p3,:p4,
            :p5,:p6,:p7,:p8,:p9,:p10,
            :p11,:p12,:p13,:p14,:p15,:p16,:p17,
            :p18,:p19,:p20,:p21,:p22,
            :p23,:p24,:p25,:p26,:p27,:p28,
            :p29,:p30,:p31,:p32,:p33,:p34,:p35,
            :p36,:p37,:p38,:p39,:p40,
            :p41,:p42,:p43,:p44,:p45,:p46,
            :p47,:p48,:p49,:p50,:p51,
            :p52,:p53,:p54,:p55,:p56,:p57,:p58,
            :p59,:p60,:p61,:p62,:p63)';
            begin
            --插入历史订购关系
                execute immediate v_sql
                using   v_productsubindex,v_productsubid,v_servicetype,v_subcapability,
                        v_serviceindex,v_serviceid,v_productindex,v_productid,v_contentindex,v_contentid,
                        v_propkgindex,v_propkgid,v_corpindex,v_corpid,v_cpindex,v_cpid,v_subscribemode,
                        v_subscribechannel,v_subscribeoptype,v_cancelmode,v_cancelchannel,v_canceloptype,
                        v_pausemode,v_resumemode,v_substatus,v_sysdate14,v_autocontinue,v_subscriber,v_usercode,
                        v_payuser,v_pushid,v_substarttime,v_subendtime,v_lastrenttime,v_nextrentdate,v_payfordate,
                        v_returnvalues,v_returntype,v_returnstartdate,v_returnenddate,v_fixusetimes,
                        v_chargeparty,v_freetime,v_createtime,v_sysdate14,v_firstusetime,v_lastusetime,
                        v_tastestarttime,v_tasteendtime,v_pausetime,v_resumetime,v_subscribeopid,
                        v_cancelopid,v_cancelreason,v_lastrent,v_msisdntype,v_pseudocode,v_pproductofferid,
                        v_varifee,v_eventid,v_isexperience,v_subscribetype,v_chargetime;
                if (sql%rowcount = 0) then
                    o_retcode   := 506;
                    o_debugpos := 69;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    rollback;
                    return;
                end if;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 70;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                rollback;
                return;
            end;
        end if;

        if (i_rollback = 1) and (i_issecondproduct in (1,2,3)) then
         --业务订购/待失效/纯包量订购关系产品的订购--回滚
            v_rollbacklastrent  := v_lastrent;
            v_sql := 'select subscriptionindex,subscriptionid,servicetype,subcapability,'||
                'serviceindex,serviceid,productindex,productid,contentindex,contentid,'||
                'productpkgindex,productpkgid,corpindex,corpid,cpindex,cpid,subscribemode,'||
                'subscribechannel,subscribeoptype,cancelmode,cancelchannel,canceloptype,'||
                'pausemode,resumemode,status,statustime,autocontinue,subscriber,usercode,'||
                'payuser,pushid,starttime,endtime,lastrenttime,nextrentdate,payfordate,'||
                'returnvalues,returntype,returnstartdate,returnenddate,fixusetimes,'||
                'chargeparty,freetime,createtime,canceltime,firstusetime,lastusetime,'||
                'tastestarttime,tasteendtime,pausetime,resumetime,subscribeopid,'||
                'cancelopid,cancelreason,lastrent,msisdntype,pseudocode,pproductofferid,'||
                'varifee,eventid,isexperience,subscribetype,chargetime'||
                ' from zxdbm_200.s200_user_subs_hist' || v_curstrmonth ||
                ' where usercode = :p1 and productid = :p2 and to_number(statustime) in '||
                '(select max(to_number(statustime)) from zxdbm_200.s200_user_subs_hist'|| v_curstrmonth ||
                ' where usercode = :p3 and productid = :p4)';
            begin
                execute immediate v_sql into
                v_productsubindex,v_productsubid,v_servicetype,v_subcapability,
                v_serviceindex,v_serviceid,v_productindex,v_productid,v_contentindex,v_contentid,
                v_propkgindex,v_propkgid,v_corpindex,v_corpid,v_cpindex,v_cpid,v_subscribemode,
                v_subscribechannel,v_subscribeoptype,v_cancelmode,v_cancelchannel,v_canceloptype,
                v_pausemode,v_resumemode,v_substatus,v_sysdate14,v_autocontinue,v_subscriber,v_usercode,
                v_payuser,v_pushid,v_substarttime,v_subendtime,v_lastrenttime,v_nextrentdate,v_payfordate,
                v_returnvalues,v_returntype,v_returnstartdate,v_returnenddate,v_fixusetimes,
                v_chargeparty,v_freetime,v_createtime,v_canceltime,v_firstusetime,v_lastusetime,
                v_tastestarttime,v_tasteendtime,v_pausetime,v_resumetime,v_subscribeopid,
                v_cancelopid,v_cancelreason,v_lastrent,v_msisdntype,v_pseudocode,v_pproductofferid,
                v_varifee,v_eventid,v_isexperience,v_subscribetype,v_chargetime
                using v_usercode,i_cancelproductid,v_usercode,i_cancelproductid;
            exception when no_data_found then
                o_retcode   := 1201;
                o_debugpos := 71;
                o_debuginfo := 'v_usercode='||v_usercode||', i_cancelproductid='||i_cancelproductid||', user_subs_hist does not exist.';
                return;
            when others then
                o_retcode   := 506;
                o_debugpos := 72;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;

            --更新订购关系表,删除历史订购关系表中同一业务的原订购关系
            if i_issecondproduct = 1 then
                --业务订购失败后的回滚
                v_canceltime := '00000000000000';
                begin
                    update zxdbm_200.s200_user_subscription
                    set    subscriptionindex          = v_productsubindex,
                           subscriptionid             = v_productsubid,
                           servicetype                = v_servicetype,
                           subcapability              = v_subcapability,
                           serviceindex               = v_serviceindex,
                           --serviceid                  = v_serviceid,
                           productindex               = v_productindex,
                           productid                  = v_productid,
                           productpkgindex            = v_propkgindex,
                           productpkgid               = v_propkgid,
                           contentindex               = v_contentindex,
                           contentid                  = v_contentid,
                           corpindex                  = v_corpindex,
                           corpid                     = v_corpid,
                           cpindex                    = v_cpindex,
                           cpid                       = v_cpid,
                           subscribemode              = v_subscribemode,
                           subscribechannel           = v_subscribechannel,
                           subscribeoptype            = v_subscribeoptype,
                           cancelmode                 = v_cancelmode,
                           cancelchannel              = v_cancelchannel,
                           canceloptype               = v_canceloptype,
                           pausemode                  = v_pausemode,
                           resumemode                 = v_resumemode,
                           status                     = v_substatus,
                           statustime                 = v_sysdate14,
                           autocontinue               = v_autocontinue,
                           subscriber                 = v_subscriber,
                           usercode                   = v_usercode,
                           payuser                    = v_payuser,
                           pushid                     = v_pushid,
                           starttime                  = v_substarttime,
                           endtime                    = v_subendtime,
                           lastrenttime               = v_lastrenttime,
                           nextrentdate               = v_nextrentdate,
                           payfordate                 = v_payfordate,
                           returnvalues               = v_returnvalues,
                           returntype                 = v_returntype,
                           returnstartdate            = v_returnstartdate,
                           returnenddate              = v_returnenddate,
                           fixusetimes                = v_fixusetimes,
                           chargeparty                = v_chargeparty,
                           freetime                   = v_freetime,
                           createtime                 = v_createtime,
                           canceltime                 = v_canceltime,
                           firstusetime               = v_firstusetime,
                           lastusetime                = v_lastusetime,
                           tastestarttime             = v_tastestarttime,
                           tasteendtime               = v_tasteendtime,
                           pausetime                  = v_pausetime,
                           resumetime                 = v_resumetime,
                           subscribeopid              = v_subscribeopid,
                           cancelopid                 = v_cancelopid,
                           cancelreason               = v_cancelreason,
                           lastrent                   = v_lastrent,
                           msisdntype                 = v_msisdntype,
                           pseudocode                 = v_pseudocode,
                           pproductofferid            = v_pproductofferid,
                           varifee                    = v_varifee,
                           eventid                    = v_eventid,
                           isexperience               = v_isexperience,
                           subscribetype              = v_subscribetype,
                           chargetime                 = v_chargetime
                    where  usercode = v_usemsisdn and serviceid = v_serviceid;
                    if (sql%rowcount = 0) then
                        o_retcode   := 506;
                        o_debugpos := 73;
                        o_debuginfo := 'usercode='||v_useuserindex||', serviceid='||v_serviceid||', user subsription do not exist.';
                        rollback;
                        return;
                    end if;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 74;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    rollback;
                    return;
                end;
            elsif i_issecondproduct in (2,3) then
                --待失效订购关系订购失败后的回滚 以及 纯包量产品再次订购失改后的回滚
                begin
                    update zxdbm_200.s200_user_subscription
                    set    subscriptionindex          = v_productsubindex,
                           subscriptionid             = v_productsubid,
                           servicetype                = v_servicetype,
                           subcapability              = v_subcapability,
                           serviceindex               = v_serviceindex,
                           serviceid                  = v_serviceid,
                           productindex               = v_productindex,
                       --  productid                  = v_productid,
                           productpkgindex            = v_propkgindex,
                           productpkgid               = v_propkgid,
                           contentindex               = v_contentindex,
                           contentid                  = v_contentid,
                           corpindex                  = v_corpindex,
                           corpid                     = v_corpid,
                           cpindex                    = v_cpindex,
                           cpid                       = v_cpid,
                           subscribemode              = v_subscribemode,
                           subscribechannel           = v_subscribechannel,
                           subscribeoptype            = v_subscribeoptype,
                           cancelmode                 = v_cancelmode,
                           cancelchannel              = v_cancelchannel,
                           canceloptype               = v_canceloptype,
                           pausemode                  = v_pausemode,
                           resumemode                 = v_resumemode,
                           status                     = v_substatus,
                           statustime                 = v_sysdate14,
                           autocontinue               = v_autocontinue,
                           subscriber                 = v_subscriber,
                           usercode                   = v_usercode,
                           payuser                    = v_payuser,
                           pushid                     = v_pushid,
                           starttime                  = v_substarttime,
                           endtime                    = v_subendtime,
                           lastrenttime               = v_lastrenttime,
                           nextrentdate               = v_nextrentdate,
                           payfordate                 = v_payfordate,
                           returnvalues               = v_returnvalues,
                           returntype                 = v_returntype,
                           returnstartdate            = v_returnstartdate,
                           returnenddate              = v_returnenddate,
                           fixusetimes                = v_fixusetimes,
                           chargeparty                = v_chargeparty,
                           freetime                   = v_freetime,
                           createtime                 = v_createtime,
                           canceltime                 = v_canceltime,
                           firstusetime               = v_firstusetime,
                           lastusetime                = v_lastusetime,
                           tastestarttime             = v_tastestarttime,
                           tasteendtime               = v_tasteendtime,
                           pausetime                  = v_pausetime,
                           resumetime                 = v_resumetime,
                           subscribeopid              = v_subscribeopid,
                           cancelopid                 = v_cancelopid,
                           cancelreason               = v_cancelreason,
                           lastrent                   = v_lastrent,
                           msisdntype                 = v_msisdntype,
                           pseudocode                 = v_pseudocode,
                           pproductofferid            = v_pproductofferid,
                           varifee                    = v_varifee,
                           eventid                    = v_eventid,
                           isexperience               = v_isexperience,
                           subscribetype              = v_subscribetype,
                           chargetime                 = v_chargetime
                    where  usercode = v_usemsisdn and productid = i_cancelproductid;
                    if (sql%rowcount = 0) then
                        o_retcode   := 506;
                        o_debugpos := 75;
                        o_debuginfo := 'usercode='||v_usemsisdn||', productid='||i_cancelproductid||', user subsription do not exist.';
                        rollback;
                        return;
                    end if;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 76;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    rollback;
                    return;
                end;
            end if;

            if i_issecondproduct in (1,2,3) then
            --将订购时扣除的费用累积减去
                v_sql := 'update zxdbm_ismp.ssrv_user_totalamount' || v_curstrmonth ||
                         ' set usedamounts  = usedamounts - :p1 where userindex = :p2' ||
                         ' and amounttype = 8 and amountindex = 0 and productindex = 0';
                begin
                    execute immediate v_sql using v_rollbacklastrent,v_payuserindex;
                    if sql%rowcount = 0 then
                        o_retcode   := 506;
                        o_debugpos := 77;
                        o_debuginfo := 'payuserindex='||v_payuserindex||', user is not exist.';
                        rollback;
                        return;
                    end if;
                    commit;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 78;
                    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                    rollback;
                    return;
                end;
            end if;
            if i_issecondproduct = 3 then
                if v_sysdate14 > v_freetime or v_sysdate8 < v_popustart or v_sysdate8 > v_popustop then
                    --将订购时纯包量产生的累积减去(免费期外) 推广期只有单位费率与包月,所以如果有推广期则肯定是免费的
                    begin
                        update zxdbm_ismp.ssrv_user_totalamount
                        set    fixtotalres = greatest(fixtotalres - v_fixusetimes*v_unitvol,0),
                               expiredate = to_number(to_char(to_date(v_subendtime,'yyyymmddhh24miss'),'yyyymmdd'))
                        where  userindex = v_payuserindex and amounttype = 0 and amountindex = v_productindex and productindex = 0;
                        if (sql%rowcount = 0) then
                            o_retcode   := 0;
                            o_debugpos := 79;
                            o_debuginfo := 'produt amount info do not exist.';
                        end if;
                    exception when others then
                        o_retcode   := 506;
                        o_debugpos := 80;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        rollback;
                        return;
                    end;
                end if;
            end if;

            --删除历史表中转移到正式订购关系表后的订购关系
            v_sql := 'delete from zxdbm_200.s200_user_subs_hist' || v_curstrmonth ||
                     ' where to_number(statustime) in (select max(to_number(statustime)) from zxdbm_200.s200_user_subs_hist'|| v_curstrmonth ||
                     ' where usercode = :p1 and productid = :p2)';
            begin
                execute immediate v_sql using v_usercode,i_cancelproductid;
            exception when no_data_found then
                o_retcode   := 2100;
                o_debugpos := 81;
                o_debuginfo := 'serviceindex='||v_serviceindex||', the service does not exist.';
                return;
            when others then
                o_retcode   := 506;
                o_debugpos := 82;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
        --added by youqunxia20131118 order3.02.01 对于预付费用户,体验转正式到OCS扣费失败的处理,把isexperience更新为1
        elsif (i_rollback = 1) and (i_issecondproduct = 7) then
            v_rollbacklastrent  := v_lastrent;
            begin
                update zxdbm_200.s200_user_subscription
                set  isexperience     = 1
                where usercode = v_usemsisdn and productid = v_productid;
                    if (sql%rowcount = 0) then
                        o_retcode   := 506;
                        o_debugpos := 89;
                        o_debuginfo := 'usercode='||v_usemsisdn||', productid='||v_productid||', user subsription do not exist.';
                        rollback;
                        return;
                    end if;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 90;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                rollback;
                return;
            end;
            --将订购时扣除的费用累积减去
            v_sql := 'update zxdbm_ismp.ssrv_user_totalamount' || v_curstrmonth ||
                     ' set usedamounts  = usedamounts - :p1 where userindex = :p2' ||
                     ' and amounttype = 8 and amountindex = 0 and productindex = 0';
            begin
                execute immediate v_sql using v_rollbacklastrent,v_payuserindex;
                if sql%rowcount = 0 then
                    o_retcode   := 506;
                    o_debugpos := 91;
                    o_debuginfo := 'payuserindex='||v_payuserindex||', user is not exist.';
                    rollback;
                    return;
                end if;
                commit;
            exception when others then
               o_retcode   := 506;
               o_debugpos := 92;
               o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
               rollback;
               return;
            end;
        else
            --正常退订或正常订购关系的回滚
            --产品退订或者产品订购回滚或者业务退订,均需要删除订购关系和累计
            if v_isservicesub = 1 then
                begin
                    delete
                    from zxdbm_200.s200_user_subscription
                    where usercode = v_usemsisdn and serviceid = v_serviceid;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 83;
                    o_debuginfo := 'v_usemsisdn='||v_usemsisdn||', v_serviceid='||v_serviceid||', failed to delete user_subscription.';
                    rollback;
                    return;
                end;
            else
                begin
                    delete
                    from zxdbm_200.s200_user_subscription
                    where usercode = v_usemsisdn and productid = v_productid;
                exception when others then
                    o_retcode   := 506;
                    o_debugpos := 84;
                    o_debuginfo := 'v_usemsisdn='||v_usemsisdn||', v_productid='||v_productid||', failed to delete user_subscription.';
                    rollback;
                    return;
                end;
            end if;
            --清除累计
            begin
                delete
                from   zxdbm_ismp.ssrv_user_totalamount
                where  userindex = v_payuserindex and amounttype = 0 and amountindex = v_productindex and productindex = 0;
            exception when others then
                o_retcode := 506;
                o_debugpos := 85;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                rollback;
                return;
            end;
            v_sql := 'delete from zxdbm_ismp.ssrv_user_totalamount'|| v_curstrmonth ||
                    ' where userindex = :p1 and amounttype = 0 and amountindex = :p2 and productindex = 0 ';
            begin
                execute immediate v_sql
                using   v_payuserindex,v_productindex;
                if (sql%rowcount = 0) then
                    o_retcode := 0;
                    o_debugpos := 86;
                    o_debuginfo := 'no data in zxdbm_ismp.ssrv_user_totalamount'|| v_curstrmonth ||'.';
                end if;
            exception when others then
                o_retcode := 506;
                o_debugpos := 87;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                rollback;
                return;
            end;
        end if;
        commit;
    --事务结束
    end;
    o_neednotifysp := 1;
end if;

--订购回滚 或者 退订删除免费期定时任务。
begin
    delete from zxdbm_ismp.freenotice_task
    where  usercode = v_usemsisdn and productid = v_productid;
exception when others then
    o_retcode   := 506;
    o_debugpos := 88;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    rollback;
    return;
end;
commit;
--组装同步给SP的消息
if (v_protocoltype <> 0) then
   if (i_supportproductid = 1) then
      --使用产品代码同步
      if (v_protocoltype = 2) then
          o_msgtosp := 'QX' || ' ' || v_productid || '  ' ||v_usemsisdn;
      elsif (o_protocoltype = 1) then
          --0:正向退订  1:SP反向退订
          if (i_direct = 1) then
              o_msgtosp := '2002' || ' ' || '0' || ' ' || v_productid || ' ' || v_usemsisdn || ' ' || v_paymsisdn;
          else
              o_msgtosp := '1002' || ' ' || v_productid || ' ' || v_usemsisdn || ' ' || v_paymsisdn;
          end if;
      end if;
   else
      --使用业务代码同步
      if (v_protocoltype = 2) then
          o_msgtosp := 'QX' || ' ' || v_serviceid || '  ' || v_usemsisdn;
      elsif (o_protocoltype = 1) then
          --0:正向退订 1:SP反向退订
          if (i_direct = 1) then
              o_msgtosp := '2002' || ' ' || '0' || ' ' || v_serviceid || ' ' || v_usemsisdn || ' ' || v_paymsisdn;
          else
              o_msgtosp := '1002' || ' ' || v_serviceid || ' ' || v_usemsisdn || ' ' || v_paymsisdn;
          end if;
      end if;
   end if;
end if;

end slp_200_telicomcancelreq;
/

--产品免费期提醒
prompt zxdbm_200.slp_200_telicomfreenotify
create or replace procedure zxdbm_200.slp_200_telicomfreenotify
(

i_oa                    in          varchar2,            --发起方用户号码
i_oatype                in          number,              --发起方地址类型
i_servicetype           in          number,              --业务能力类型
i_productid             in          varchar2,            --产品代码
i_actiontype            in          number,              --动作类型
i_countrycode           in          varchar2,            --国家码
i_spaccesscode          in          varchar2,            --antuprice消息中的SP接入码
i_needauthuser          in          number,              --是否需要鉴权用户 0-不需要 1-需要
i_status                in          number,              --取缓存订购关系状态;
i_endtime               in          varchar2,            --取缓存产品订购关系结束时间
i_freenotice            in          number,              --免费期到期自动续订和自动取消是否下发短信:0否 1是;
i_cmdchannel            in          number,              --内部渠道
o_retcode               out         number,              --鉴权返回码 0代表成功,否则代表错误码
o_debugpos              out         number,              --出口顺序标识
o_debuginfo             out         varchar2,            --调试信息
o_usemsisdn             out         varchar2,            --去掉前缀的使用方用户号码
o_paymsisdn             out         varchar2,            --去掉前缀的计费方号码
o_usemsisdntype         out         number,              --使用方号码类型,1:msisdn,2:phs,3:pstn
o_userindex             out         number,              --使用方号码序号
o_freenotify            out         number,              --免费试用期到短信通知方式,0-到期自动收费提醒,1-到期需用户确认订购提醒,2-到期自动取消提醒,3-到期确认订购成功 4-免费期到期自动续订和自动取消不下发短信,5-到期需用户确认退订提醒,6-到期确认退订超时,超时订购成功提醒
o_paytype               out         number,              --计费号码的付费类型 0:后付费 1:预付费 2:准预付费
o_oppaytype             out         number,              --话单专用付费类型
o_colortype             out         number,              --使用方号码名单类型   0-普通用户,1-黑名单,2-白名单,3-灰名单,4-红名单
o_cpid                  out         varchar2,            --CP/SP代码
o_cpindex               out         number,              --CP/SP序号
o_cpcnshortname         out         varchar2,            --CP/SP名称
o_cptype                out         number,              --CP/SP类型
o_customersrvtel        out         varchar2,            --CP/SP客服电话
o_serviceid             out         varchar2,            --业务代码
--o_serviceindex        out         number,              --业务序号
o_servicename           out         varchar2,            --业务名称
o_productid             out         varchar2,            --产品代码
--o_productindex        out         number,              --产品序号
o_productname           out         varchar2,            --产品名称
o_smschargingcn         out         varchar2,            --产品资费短信描述
o_servicetype           out         number,              --产品业务能力类型
o_subcapability         out         number,              --产品业务能力子类型
o_srvtypeid             out         varchar2,            --业务能力编码
o_freetime              out         varchar2,            --产品订购关系免费期
o_linknum               out         varchar2,            --业务话单中同一个流程中二次确认前后话单的关联
o_productsubid          out         varchar2,            --产品订购关系代码
o_productsubindex       out         varchar2,             --产品订购关系序号
o_spaccesscode          out         varchar2,              --SP接入码
o_maincapability        out         number,              --组合业务主业务能力类型
o_submaincapability     out         number,              --组合业务主业务能力子类型
o_cpsrvtype             out         number,              --CP服务类型:0:CP+SP,1:CP,2:SP ;3:自营业务SP
o_buffertime            out         number,              --免费期提醒缓存时间
o_status                out         number,              --订购关系状态;
o_endtime               out         varchar2,             --产品订购关系结束时间
o_cityid                out         varchar2,            --产品订购关系结束时间
--added by youqunxia20130526  order2.07.10
o_cancelaccesscode      out         varchar2,            --产品退订接入码
o_cancelfeaturecode     out         varchar2             --短信,彩信退订指令

)
as

--新增下面几个关于时间的参数,尽量减少sysdate的调用,所有用到sysdate的地方,以to_date(v_sysdate14,'yyyymmddhh24miss')进行代替
v_sysdate14             char(14)                                                   := to_char(sysdate ,'yyyymmddhh24miss');
v_tmpint                number(10)                                                 := 0;                         --临时存放临时数值
v_sql                   varchar2(3072)                                             := ' ';                       --临时存放动态sql
v_maincapability        number(10)                                                 := 0;                                       --临时存放组合业务主业务能力类型
v_submaincapability     number(10)                                                 := 0;                                       --临时存放组合业务主业务能力子类型
v_countrycode           varchar2(5)                                                := i_countrycode;             --临时存放国家码

v_oa                    zxdbm_ismp.susr_basic.usercode%type                        := ' ';                       --临时存放发起方用户号码
v_usemsisdn             zxdbm_ismp.susr_basic.usercode%type                        := ' ';                       --临时存放使用方用户号码
v_useuserindex          zxdbm_ismp.susr_basic.userindex%type                       := 0;                         --临时存放使用方用户序号
v_useusertype           zxdbm_ismp.susr_basic.usertype%type                        := 1;                         --临时存放使用方用户类型
v_usepaytype            zxdbm_ismp.susr_basic.paytype%type                         := 0;                         --临时存放使用方用户付费类型
v_useblacklist          zxdbm_ismp.susr_basic.blacklist%type                       := 0;                         --临时存放使用方是否进入黑名单
v_usewhitelist          zxdbm_ismp.susr_basic.whitelist%type                       := 0;                         --临时存放使用方是否进入白名单
v_useredlist            zxdbm_ismp.susr_basic.redlist%type                         := 0;                         --临时存放使用方是否进入红名单
v_useblacklevel         zxdbm_ismp.susr_basic.blacklevel%type                      := 0;                         --临时存放使用方黑名单级别
v_usewhitelevel         zxdbm_ismp.susr_basic.whitelevel%type                      := 0;                         --临时存放使用方白名单级别
v_usemsisdntype         zxdbm_ismp.susr_basic.msisdntype%type                      := 1;                         --临时存放使用方号码类型
v_useuserstatus         zxdbm_ismp.susr_basic.status%type                          := 0;                         --临时存放使用方状态
v_useactivestatus       zxdbm_ismp.susr_basic.activestatus%type                    := 0;                         --临时存放使用方激活状态
v_useoppaytype          zxdbm_ismp.susr_basic.oppaytype%type                       := 0;                         --临时存放使用方话单专用付费类型
v_usecorpindex          zxdbm_ismp.susr_basic.corpindex%type                       := 0;                         --临时存放使用方集团序号
v_useusercityid         zxdbm_ismp.susr_basic.cityid%type                          := 0;                         --临时存放用户所属的城市ID
v_useusetime            zxdbm_ismp.susr_basic.lastusetime%type                     := '00000000000000';          --临时存放使用方上次使用时间
v_productid             zxdbm_200.s200_product.productid%type                      := i_productid;               --临时存放产品代码
v_productindex          zxdbm_200.s200_product.productindex%type                   := 0;                         --临时存放产品序号
v_productname           zxdbm_200.s200_product.productname%type                    := ' ';                       --临时存放产品名称
v_smschargingcn         zxdbm_200.s200_product.smschargingcn%type                  := ' ';                       --临时存放产品资费短信描述
v_servicetype           zxdbm_200.s200_product.servicetype%type                    := 0;                         --临时存放产品业务能力类型
v_subcapability         zxdbm_200.s200_product.subcapability%type                  := 1;                         --临时存放产品业务能力子类型
v_freenotify            zxdbm_200.s200_product.freenotify%type                     := 0;                         --免费期到是否发送通知,0-不发送,1-发送
v_productendtime        zxdbm_200.s200_product.endtime%type                        := '99991231235959';          --临时存放产品终止时间
v_serviceid             zxdbm_200.s200_service.serviceid%type                      := ' ';                       --临时存放业务代码
v_serviceindex          zxdbm_200.s200_service.serviceindex%type                   := 0;                         --临时存放业务序号
v_servicename           zxdbm_200.s200_service.servicename%type                    := ' ';                       --临时存放业务名称
v_cpid                  zxdbm_ismp.scp_basic.cpid%type                             := ' ';                       --临时存放CP/SP代码
v_cptype                zxdbm_ismp.scp_basic.cptype%type                           := 2;                         --临时存放CP/SP类型
v_cpindex               zxdbm_ismp.scp_basic.cpindex%type                          := 0;                         --临时存放CP/SP序号
v_cpsrvtype             zxdbm_ismp.scp_basic.cpsrvtype%type                        := 0;                         --临时存放CP服务类型
v_cpcnshortname         zxdbm_ismp.scp_basic.cpcnshortname%type                    := ' ';                       --临时存放CP/SP中文名简写
v_customersrvtel        zxdbm_ismp.scp_basic.customersrvtel%type                   := ' ';                       --临时存放CP/SP客服电话
v_productsubindex       zxdbm_200.s200_user_subscription.subscriptionindex%type    := 0;                         --临时存放订购关系序号
v_productsubid          zxdbm_200.s200_user_subscription.subscriptionid%type       := ' ';                       --临时存放订购关系代码
v_status                zxdbm_200.s200_user_subscription.status%type               := 0;                         --订购关系状态状态,0-待生效,1-正常,2-暂停,待失效,注销
v_subscribechannel      zxdbm_200.s200_user_subscription.subscribechannel%type     := 1;                         --临时存放订购关系订购渠道
v_freetime              zxdbm_200.s200_user_subscription.freetime%type             := '00000000000000';          --临时存放产品订购免费日期
v_endtime               zxdbm_200.s200_user_subscription.endtime%type              := '99991231235959';
v_nextrentdate          zxdbm_200.s200_user_subscription.nextrentdate%type         := '99991231';                --临时存放下次扣租日期
v_subendtime            zxdbm_200.s200_user_subscription.endtime%type              := '99991231235959';          --临时存放订购关系失效时间
v_chargetype            zxdbm_200.s200_product_fee.chargetype%type                 := 0;
v_cyctype               zxdbm_200.s200_product_fee.cyctype%type                    := 0;                         --临时存放周期类型0-非周期,1-周期性
v_cycunit               zxdbm_200.s200_product_fee.cycunit%type                    := 0;                         --临时存放周期单位 0-无,1-天,2-周,3-月,4-半年,5-年,6-小时,7-分钟,8-秒
v_cycparam              zxdbm_200.s200_product_fee.cycparam%type                   := 0;                         --临时存放周期参数
v_srvtypeid             zxdbm_ismp.ssys_service_type.srvtypeid%type                := ' ';                       --临时存放话单中的srvtypeid
v_spaccesscode          zxdbm_ismp.ssrv_srvorder.spaccesscode%type                 := ' ';                       --临时存放sp接入码
--added by youqunxia20130526  order2.07.10
v_cancelaccesscode      zxdbm_ismp.ssrv_srvorder.accesscode%type                   := ' ';                       --临时存放退订接入码
v_cancelfeaturecode     zxdbm_ismp.ssrv_srvorder.featurecode%type                  := ' ';                       --临时存放退订特征码

begin

--出参初始化
o_retcode               := 0;
o_debugpos             := 0;
o_debuginfo             := ' ';
o_usemsisdn             := ' ';
o_paymsisdn             := ' ';
o_usemsisdntype         := 1;
o_userindex             := 0;
o_freenotify            := 1;
o_paytype               := 0;
o_oppaytype             := 0;
o_colortype             := 0;
o_cpid                  := ' ';
o_cpindex               := 0;
o_cpcnshortname         := ' ';
o_cptype                := 0;
o_customersrvtel        := ' ';
o_serviceid             := ' ';
--o_serviceindex          := 0;
o_servicename           := ' ';
o_productid             := ' ';
--o_productindex          := 0;
o_productname           := ' ';
o_smschargingcn         := ' ';
o_servicetype           := 0;
o_subcapability         := 1;
o_srvtypeid             := ' ';
o_freetime              := '00000000000000';
o_linknum               := ' ';
o_productsubid          := ' ';
o_productsubindex       := 0;
o_spaccesscode          := i_spaccesscode;
o_maincapability        := 0;
o_submaincapability     := 0;
o_cpsrvtype             := 0;
o_buffertime            := 0;
o_endtime               := v_endtime;
o_cityid                := '';
--added by youqunxia20130526
o_cancelaccesscode      := '';
o_cancelfeaturecode     := '';


--发起方用户号码去前缀
v_oa := ltrim(rtrim(nvl(i_oa,'0')));
if v_oa like ('+' || v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 2);
elsif v_oa like ('00' || v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 3);
elsif v_oa like (v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 1);
end if;
if (substr(v_oa,1,1) <> '0') and (i_oatype = 4) then
    v_oa := '0' || v_oa;
end if;
v_usemsisdn  := v_oa;
o_usemsisdn  := v_usemsisdn;
if (v_usemsisdn = '0') then
    o_retcode   := 1001;
    o_debugpos := 2;
    o_debuginfo := 'v_usemsisdn error.';
    return;
end if;
--使用方号码存在性鉴权
if (i_needauthuser = 1) then
    begin
        select userindex,usertype,paytype,blacklist,whitelist,
               redlist,blacklevel,whitelevel,msisdntype,status,
               activestatus,oppaytype,corpindex,lastusetime,cityid
        into   v_useuserindex,v_useusertype,v_usepaytype,v_useblacklist,v_usewhitelist,
               v_useredlist,v_useblacklevel,v_usewhitelevel,v_usemsisdntype,v_useuserstatus,
               v_useactivestatus,v_useoppaytype,v_usecorpindex,v_useusetime,v_useusercityid
        from   zxdbm_ismp.susr_basic
        where  usercode = v_usemsisdn;
    exception when no_data_found then
        o_retcode   := 1001;
        o_debugpos := 3;
        o_debuginfo := 'user info do not exist.';
        if (i_oatype = 4) then
            v_usemsisdntype    := 2;
        else
            v_usemsisdntype    := 1;
        end if;
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 4;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        if (i_oatype = 4) then
            o_usemsisdntype    := 2;
        else
            o_usemsisdntype    := 1;
        end if;
        return;
    end;
    o_usemsisdntype := v_usemsisdntype;
    o_userindex     := v_useuserindex;
    o_paymsisdn     := v_usemsisdn;
    o_paytype       := v_usepaytype;
    o_oppaytype     := v_useoppaytype;
    o_cityid        := trim(v_useusercityid);
    if (v_useredlist = 1) then
        o_colortype := 4;
    else
        o_colortype := 0;
    end if;
end if;
--产品存在性鉴权
begin
    select productindex,productname,smschargingcn,serviceindex,servicetype,
           subcapability,cpindex,freenotify,endtime
    into   v_productindex,v_productname,v_smschargingcn,v_serviceindex,v_servicetype,
           v_subcapability,v_cpindex,v_freenotify,v_productendtime
    from   zxdbm_200.s200_product
    where  productid = v_productid;
exception when no_data_found then
    o_retcode   := 2100;
    o_debugpos := 5;
    o_debuginfo := 'product info do not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 6;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_productid  := v_productid;
--o_productindex  := v_productindex;
o_productname   := v_productname;
o_smschargingcn := v_smschargingcn;
--o_serviceindex  := v_serviceindex;
o_servicetype   := v_servicetype;
o_subcapability := v_subcapability;
o_cpindex       := v_cpindex;

begin
    select chargetype,cyctype,cycunit,cycparam
    into   v_chargetype,v_cyctype,v_cycunit,v_cycparam
    from   zxdbm_200.s200_product_fee
    where  productindex = v_productindex and itemindex = 0;
exception when no_data_found then
    o_retcode   := 3103;
    o_debugpos := 7;
    o_debuginfo := 'product fee do not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 8;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;

--业务存在性鉴权(MPSS不需要鉴权)
if i_servicetype = 101 then
    v_sql := 'select serviceid,servicename,maincapability,submaincapability '||
             'from zxdbm_9900.s9900_service where serviceindex = :p1';
    begin
        execute immediate v_sql into v_serviceid,v_servicename,v_maincapability,v_submaincapability using v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 9;
        o_debuginfo := 'cpx service do not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 10;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
elsif i_servicetype <> 14 then
    begin
        select serviceid,servicename
        into   v_serviceid,v_servicename
        from   zxdbm_200.s200_service
        where  serviceindex = v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 11;
        o_debuginfo := 'service do not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 12;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

o_serviceid   := v_serviceid;
o_servicename := v_servicename;
o_maincapability    := v_maincapability;
o_submaincapability := v_submaincapability;

--系统业务能力鉴权
if i_servicetype = 101 then
    begin
        select srvtypeid
        into   v_srvtypeid
        from   zxdbm_ismp.ssys_service_type
        where  servicetype = v_maincapability and subcapability = v_submaincapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 13;
        o_debuginfo := 'cpx info in ssys_service_type do not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 14;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
       return;
    end;
else
    begin
        select srvtypeid
        into   v_srvtypeid
        from   zxdbm_ismp.ssys_service_type
        where  servicetype = v_servicetype and subcapability = v_subcapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 15;
        o_debuginfo := 'info in ssys_service_type do not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 16;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;
o_srvtypeid   :=  v_srvtypeid;

--CP/SP存在性鉴权
begin
    select cpid,cptype,cpcnshortname,customersrvtel,cpsrvtype
    into   v_cpid,v_cptype,v_cpcnshortname,v_customersrvtel,v_cpsrvtype
    from   zxdbm_ismp.scp_basic
    where  cpindex = v_cpindex;
exception when no_data_found then
    o_retcode   := 2000;
    o_debugpos := 17;
    o_debuginfo := 'sp do not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 18;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_cpid            := v_cpid;
o_cptype          := v_cptype;
o_cpsrvtype       := v_cpsrvtype;
o_cpcnshortname   := v_cpcnshortname;
o_customersrvtel  := v_customersrvtel;

--added by youqunxia20130526   order2.07.10
--产品退订指令查询
begin
    select featurecode,accesscode
    into   v_cancelfeaturecode,v_cancelaccesscode
    from   zxdbm_ismp.ssrv_srvorder
    where  productid = v_productid and cmdtype = 2 and rownum = 1;
exception when no_data_found then
    null;
when others then
    o_retcode   := 506;
    o_debugpos := 28;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_cancelaccesscode := v_cancelaccesscode;
o_cancelfeaturecode := v_cancelfeaturecode;

--SP接入码查询
if (i_spaccesscode is null) or (length(trim(i_spaccesscode)) = 0) then
    begin
        select spaccesscode into v_spaccesscode
        from zxdbm_ismp.ssrv_srvorder
        where productindex = v_productindex and cmdtype in (1,2) and rownum = 1;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 19;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if trim(v_spaccesscode) is null then
        begin
            select accesscode into v_spaccesscode
            from zxdbm_ismp.ssrv_spaccesscode
            where cpid = v_cpid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 20;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    o_spaccesscode := v_spaccesscode;
end if;

--产品订购关系存在性鉴权
begin
    select subscriptionindex,subscriptionid,freetime,subscribechannel,status,
           endtime
    into   v_productsubindex,v_productsubid,v_freetime,v_subscribechannel,v_status,
           v_endtime
    from   zxdbm_200.s200_user_subscription
    where  usercode = v_usemsisdn and productid = v_productid;
exception when no_data_found then
    o_retcode   := 1201;
    o_debugpos := 21;
    o_debuginfo := 'subscription do not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 22;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_freetime         := v_freetime;
o_productsubindex  := v_productsubindex;
o_productsubid     := v_productsubid;
o_status           := v_status;
o_endtime          := v_endtime;

--设置o_freenotify
--added by youqunxia20130526  order2.07.10
if i_actiontype =10 then
    --到期确认订购成功提醒
    o_freenotify  := 3;
elsif i_cmdchannel = 1001 then
    --到期确认退订超时提醒,订购成功
    o_freenotify  := 6;
else
    o_freenotify  := v_freenotify;
    if v_freenotify = 3 then
        --到期确认退订提醒
        o_freenotify := 5;
    end if;
    if (i_freenotice = 0) and v_freenotify in (0,2) then
        --免费期到期自动续订和自动取消时不下发短信
        o_freenotify := 4;
    end if;
end if;

--免费期确认订购成功,或者确认退订超时订购成功,修改订购关系
if i_actiontype = 10 or i_cmdchannel = 1001 then 
    --修改下次扣租时间和订购关系失效时间
    v_nextrentdate := to_char(to_date(v_freetime,'yyyymmddhh24miss') + 1,'yyyymmdd');
    if v_chargetype in (1,4) then
        v_subendtime := v_productendtime;
    elsif v_chargetype in (2,5,7) then
        if v_cyctype = 1 then
            v_subendtime := v_productendtime;
        else
            v_subendtime := to_char(last_day(to_date(v_sysdate14,'yyyymmddhh24miss')),'yyyymmdd') || '235959';
        end if;
    elsif v_chargetype in (3,6,8,9) then
        if (v_cycunit = 1) then
            --包天
            if v_cyctype = 1 then
                v_subendtime := v_productendtime;
            else
                v_subendtime := to_char(to_date(v_sysdate14,'yyyymmddhh24miss') + v_cycparam,'yyyymmdd')||'235959';
            end if;
        elsif (v_cycunit = 2) then
            --包周
            if v_cyctype = 1 then
                v_subendtime := v_productendtime;
            else
                v_subendtime := to_char(to_date(v_sysdate14,'yyyymmddhh24miss') + v_cycparam*7,'yyyymmdd')||'235959';
            end if;
        elsif (v_cycunit = 3) then
            --包月
            if v_cyctype = 1 then
                v_subendtime := v_productendtime;
            else
                v_subendtime := to_char(add_months(to_date(v_sysdate14,'yyyymmddhh24miss'),v_cycparam),'yyyymmdd')||'235959';
            end if;
        elsif (v_cycunit = 4) then
            --包半年
            if v_cyctype = 1 then
                v_subendtime := v_productendtime;
            else
                v_subendtime := to_char(add_months(to_date(v_sysdate14,'yyyymmddhh24miss'),v_cycparam*6),'yyyymmdd')||'235959';
            end if;
        elsif (v_cycunit = 5) then
            --包年
            if v_cyctype = 1 then
                v_subendtime := v_productendtime;
            else
                v_subendtime := to_char(add_months(to_date(v_sysdate14,'yyyymmddhh24miss'),v_cycparam*12),'yyyymmdd')||'235959';
            end if;
        end if;
    else
        --订购终止时间,暂时定为产品终止时间
        v_subendtime := v_productendtime;
    end if;
    if i_cmdchannel = 46 then
        --查询订购渠道
        begin
            select portalchannel into v_subscribechannel
            from zxdbm_ismp.ssrv_channel_map
            where innerchannel = i_cmdchannel;
        exception when no_data_found then
            v_subscribechannel := i_cmdchannel * 1000;
        when others then
            o_retcode   := 506;
            o_debugpos := 11;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;

    begin
        update  zxdbm_200.s200_user_subscription
        set     endtime = v_subendtime,
                nextrentdate = v_nextrentdate,
                subscribechannel = v_subscribechannel
        where   usercode = v_usemsisdn and productid = v_productid;
        if (sql%rowcount = 0) then
            o_retcode   := 506;
            o_debugpos := 23;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            rollback;
            return;
        end if;
    exception when others then
        o_retcode   := 506;
        o_debugpos := 24;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if v_chargetype = 4 then --对于纯包量有免费期的情况,需要更新累积表的过期时间到新的订购关系结束时间
        begin
            update  zxdbm_ismp.ssrv_user_totalamount
            set     expiredate = to_number(substr(v_subendtime,1,8))
            where   userindex = v_useuserindex and amounttype = 0 and amountindex = v_productindex and rownum = 1;
            if (sql%rowcount = 0) then
                o_retcode   := 506;
                o_debugpos := 25;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                rollback;
                return;
            end if;
        exception when others then
            o_retcode   := 506;
            o_debugpos := 26;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    commit;
end if;

--added by youqunxia20130526  order2.07.10
--免费期到期需用户确认订购/退订 缓存时间设置
if o_freenotify in (1,5) then
    --cache时间计算
    o_buffertime := 3600*24*ABS(to_date('235959', 'HH24MISS') - to_date(substr(v_sysdate14,9,6), 'HH24MISS'));
    --生成o_linknum
    begin
        v_tmpint  := dbms_random.value(1,9999999999);
        o_linknum := v_sysdate14 || '655380' || lpad(to_char(v_tmpint),10,'0');
    exception when others then
        o_retcode   := 506;
        o_debugpos := 27;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

end slp_200_telicomfreenotify;
/

--暂停/激活产品订购关系鉴权
prompt zxdbm_200.slp_200_telicomsubchangereq
create or replace procedure zxdbm_200.slp_200_telicomsubchangereq
(

i_oa                    in          varchar2,            --发起方用户号码
i_oatype                in          number,              --发起方地址类型
i_servicetype           in          number,              --业务能力类型
i_productid             in          varchar2,            --产品代码
i_actiontype            in          number,              --动作类型
i_cmdchannel            in          number,              --动作来源
i_countrycode           in          varchar2,            --国家码
i_spaccesscode          in          varchar2,            --antuprice消息中的SP接入码
i_needauthuser          in          number,              --是否需要鉴权用户 0-不需要 1-需要
i_checkpseudocode       in          number,              --系统开关:是否支持伪码   0-不支持   1-支持
o_retcode               out         number,              --鉴权返回码 0代表成功,否则代表错误码
o_debugpos             out         number,              --出口顺序标识
o_debuginfo             out         varchar2,            --调试信息
o_usemsisdn             out         varchar2,            --去掉前缀的使用方用户号码
o_paymsisdn             out         varchar2,            --去掉前缀的计费方号码
o_usemsisdntype         out         number,              --使用方号码类型,1:msisdn,2:phs,3:pstn
o_userindex             out         number,              --使用方号码序号
o_paytype               out         number,              --计费号码的付费类型 0:后付费 1:预付费 2:准预付费
o_oppaytype             out         number,              --话单专用付费类型
o_colortype             out         number,              --使用方号码名单类型   0-普通用户,1-黑名单,2-白名单,3-灰名单,4-红名单
o_cpid                  out         varchar2,            --CP/SP代码
o_cpindex               out         number,              --CP/SP序号
o_cpcnshortname         out         varchar2,            --CP/SP名称
o_cpsubscribeurl        out         varchar2,            --CP/SP订购URL
o_cptype                out         number,              --CP/SP类型
o_customersrvtel        out         varchar2,            --CP/SP客服电话
o_serviceid             out         varchar2,            --业务代码
o_serviceindex          out         number,              --业务序号
o_servicename           out         varchar2,            --业务名称
o_productid             out         varchar2,            --产品代码
o_productindex          out         number,              --产品序号
o_productname           out         varchar2,            --产品名称
o_smschargingcn         out         varchar2,            --产品资费短信描述
o_servicetype           out         number,              --产品业务能力类型
o_subcapability         out         number,              --产品业务能力子类型
o_srvtypeid             out         varchar2,            --业务能力编码
o_productsubid          out         varchar2,            --产品订购关系代码
o_productsubindex       out         varchar2,            --产品订购关系序号
o_productsubstatus      out         number,              --产品订购关系状态
o_neednotifysp          out         number,               --是否有必要通知SP
o_pseudocode            out         varchar2,             --伪码
o_pcodemsisdnswitch     out         number,               --伪码和真实号码之间是否有转换:  0否   1真实号码转化伪码 2伪码转化为真实号码
o_spaccesscode          out         varchar2,             --SP接入码
o_maincapability        out         number,               --组合业务主业务能力类型
o_submaincapability     out         number,               --组合业务主业务能力子类型
o_cpsrvtype             out         number,               --CP服务类型:0:CP+SP,1:CP,2:SP ;3:自营业务SP
o_cityid                out         varchar2,             --用户所属城市ID
--added by youqunxia20130906 order3.02.01
o_issynctooidd          out         number                --是否同步订购关系给OIDD,0-否,1-是 

)
as

v_sql                   varchar2(3072)                                             := ' ';                    --临时存放动态sql
v_countrycode           varchar2(5)                                                := i_countrycode;          --临时存放国家码
v_maincapability        number(10)                                                 := 0;                      --临时存放组合业务主业务能力类型
v_submaincapability     number(10)                                                 := 0;                      --临时存放组合业务主业务能力子类型

v_oa                    zxdbm_ismp.susr_basic.usercode%type                        := ' ';                    --临时存放发起方用户号码
v_usemsisdn             zxdbm_ismp.susr_basic.usercode%type                        := ' ';                    --临时存放使用方用户号码
v_useuserindex          zxdbm_ismp.susr_basic.userindex%type                       := 0;                      --临时存放使用方用户序号
v_useusertype           zxdbm_ismp.susr_basic.usertype%type                        := 1;                      --临时存放使用方用户类型
v_usepaytype            zxdbm_ismp.susr_basic.paytype%type                         := 0;                      --临时存放使用方用户付费类型
v_useblacklist          zxdbm_ismp.susr_basic.blacklist%type                       := 0;                      --临时存放使用方是否进入黑名单
v_usewhitelist          zxdbm_ismp.susr_basic.whitelist%type                       := 0;                      --临时存放使用方是否进入白名单
v_useredlist            zxdbm_ismp.susr_basic.redlist%type                         := 0;                      --临时存放使用方是否进入红名单
v_useblacklevel         zxdbm_ismp.susr_basic.blacklevel%type                      := 0;                      --临时存放使用方黑名单级别
v_usewhitelevel         zxdbm_ismp.susr_basic.whitelevel%type                      := 0;                      --临时存放使用方白名单级别
v_usemsisdntype         zxdbm_ismp.susr_basic.msisdntype%type                      := 1;                      --临时存放使用方号码类型
v_useuserstatus         zxdbm_ismp.susr_basic.status%type                          := 0;                      --临时存放使用方状态
v_useactivestatus       zxdbm_ismp.susr_basic.activestatus%type                    := 0;                      --临时存放使用方激活状态
v_useoppaytype          zxdbm_ismp.susr_basic.oppaytype%type                       := 0;                      --临时存放使用方话单专用付费类型
v_usecorpindex          zxdbm_ismp.susr_basic.corpindex%type                       := 0;                      --临时存放使用方集团序号
v_useusetime            zxdbm_ismp.susr_basic.lastusetime%type                     := '00000000000000';       --临时存放使用方上次使用时间
v_useusercityid         zxdbm_ismp.susr_basic.cityid%type                          := ' ';                    --临时存放使用方所属城市ID

v_srvtypeid             zxdbm_ismp.ssys_service_type.srvtypeid%type                := ' ';                    --临时存放话单中的srvtypeid
v_cpid                  zxdbm_ismp.scp_basic.cpid%type                             := ' ';                    --临时存放CP/SP代码
v_cptype                zxdbm_ismp.scp_basic.cptype%type                           := 2;                      --临时存放CP/SP类型
v_cpsrvtype             zxdbm_ismp.scp_basic.cpsrvtype%type                        := 0;                                                  --临时存放CP服务类型
v_cpindex               zxdbm_ismp.scp_basic.cpindex%type                          := 0;                      --临时存放CP/SP序号
v_cpcnshortname         zxdbm_ismp.scp_basic.cpcnshortname%type                    := ' ';                    --临时存放CP/SP中文名简写
v_customersrvtel        zxdbm_ismp.scp_basic.customersrvtel%type                   := ' ';                    --临时存放CP/SP客服电话
v_cpsubscribeurl        zxdbm_ismp.scp_basic.cpsubscribeurl%type                   := ' ';                    --临时存放CP/SP订购URL
v_productid             zxdbm_200.s200_product.productid%type                      := i_productid;            --临时存放产品代码
v_productindex          zxdbm_200.s200_product.productindex%type                   := 0;                      --临时存放产品序号
v_productname           zxdbm_200.s200_product.productname%type                    := ' ';                    --临时存放产品名称
v_smschargingcn         zxdbm_200.s200_product.smschargingcn%type                  := ' ';                    --临时存放产品资费短信描述
v_servicetype           zxdbm_200.s200_product.servicetype%type                    := 0;                      --临时存放产品业务能力类型
v_subcapability         zxdbm_200.s200_product.subcapability%type                  := 1;                      --临时存放产品业务能力子类型
v_serviceid             zxdbm_200.s200_service.serviceid%type                      := ' ';                    --临时存放业务代码
v_serviceindex          zxdbm_200.s200_service.serviceindex%type                   := 0;                      --临时存放业务序号
v_servicename           zxdbm_200.s200_service.servicename%type                    := ' ';                    --临时存放业务名称
v_orderupdateurl        zxdbm_200.s200_service.orderupdateurl%type                 := ' ';                    --临时存放业务表中订购关系更新通知URL
v_productsubindex       zxdbm_200.s200_user_subscription.subscriptionindex%type    := 0;                      --临时存放订购关系序号
v_productsubid          zxdbm_200.s200_user_subscription.subscriptionid%type       := ' ';                    --临时存放订购关系代码
v_productsubstatus      zxdbm_200.s200_user_subscription.status%type               := 0;                      --临时存放订购关系状态
v_spaccesscode          zxdbm_ismp.ssrv_srvorder.spaccesscode%type                 := ' ';                    --临时存放sp接入码
--added by youqunxia20130906 order3.02.01
v_servicesort           zxdbm_200.s200_service.servicesort%type                    := 0;                      --业务归属 0:普通业务、1:属于OIDD 2:属于定位

begin

--出参初始化
o_retcode               := 0;
o_debugpos             := 0;
o_debuginfo             := ' ';
o_usemsisdn             := ' ';
o_paymsisdn             := ' ';
o_usemsisdntype         := 1;
o_userindex             := 0;
o_paytype               := 0;
o_oppaytype             := 0;
o_colortype             := 0;
o_cpid                  := ' ';
o_cpindex               := 0;
o_cpcnshortname         := ' ';
o_cpsubscribeurl        := ' ';
o_cptype                := 0;
o_customersrvtel        := ' ';
o_serviceid             := ' ';
o_serviceindex          := 0;
o_servicename           := ' ';
o_productid             := ' ';
o_productindex          := 0;
o_productname           := ' ';
o_smschargingcn         := ' ';
o_servicetype           := 0;
o_subcapability         := 1;
o_srvtypeid             := ' ';
o_productsubid          := ' ';
o_productsubindex       := 0;
o_productsubstatus      := 0;
o_neednotifysp          := 0;
o_pseudocode            :='';
o_pcodemsisdnswitch     :=0;
o_spaccesscode          := i_spaccesscode;
o_maincapability        := 0;
o_submaincapability     := 0;
o_cpsrvtype             := 0;
o_cityid                := '';
o_issynctooidd          := 0;

--发起方地址类型鉴权
if i_oatype not in (1,2,3,4) then
    o_retcode   := 107;
    o_debugpos := 1;
    o_debuginfo := 'i_oatype is not valid.';
    return;
end if;

--发起方用户号码去前缀
v_oa := ltrim(rtrim(nvl(i_oa,'0')));
if v_oa like ('+' || v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 2);
elsif v_oa like ('00' || v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 3);
elsif v_oa like (v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 1);
end if;
if (substr(v_oa,1,1) <> '0') and (i_oatype = 4) then
    v_oa := '0' || v_oa;
end if;
v_usemsisdn  := v_oa;
o_usemsisdn  := v_usemsisdn;
if (v_usemsisdn = '0') then
    o_retcode   := 1001;
    o_debugpos := 2;
    o_debuginfo := 'v_usemsisdn error.';
    return;
end if;
--使用方号码存在性鉴权
if (i_needauthuser = 1) then
    begin
        select userindex,usertype,paytype,blacklist,whitelist,
               redlist,blacklevel,whitelevel,msisdntype,status,
               activestatus,oppaytype,corpindex,lastusetime,cityid
        into   v_useuserindex,v_useusertype,v_usepaytype,v_useblacklist,v_usewhitelist,
               v_useredlist,v_useblacklevel,v_usewhitelevel,v_usemsisdntype,v_useuserstatus,
               v_useactivestatus,v_useoppaytype,v_usecorpindex,v_useusetime,v_useusercityid
        from   zxdbm_ismp.susr_basic
        where  usercode = v_usemsisdn;
    exception when no_data_found then
        o_retcode   := 1001;
        o_debugpos := 3;
        o_debuginfo := 'user info do not exist.';
        if (i_oatype = 4) then
            v_usemsisdntype    := 2;
        else
            v_usemsisdntype    := 1;
        end if;
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 4;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        if (i_oatype = 4) then
            o_usemsisdntype    := 2;
        else
            o_usemsisdntype    := 1;
        end if;
        return;
    end;
    o_usemsisdntype := v_usemsisdntype;
    o_userindex     := v_useuserindex;
    o_paymsisdn     := v_usemsisdn;
    o_paytype       := v_usepaytype;
    o_oppaytype     := v_useoppaytype;
    o_cityid        := trim(v_useusercityid);
    if (v_useredlist = 1) then
        o_colortype := 4;
    else
        o_colortype := 0;
    end if;
end if;
--产品存在性鉴权
begin
    select productindex,productname,smschargingcn,serviceindex,servicetype,
           subcapability,cpindex
    into   v_productindex,v_productname,v_smschargingcn,v_serviceindex,v_servicetype,
           v_subcapability,v_cpindex
    from   zxdbm_200.s200_product
    where  productid = v_productid;
exception when no_data_found then
    o_retcode   := 2100;
    o_debugpos := 5;
    o_debuginfo := 'product info do not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 6;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_productid     := v_productid;
o_productindex  := v_productindex;
o_productname   := v_productname;
o_smschargingcn := v_smschargingcn;
o_serviceindex  := v_serviceindex;
o_servicetype   := v_servicetype;
o_subcapability := v_subcapability;
o_cpindex       := v_cpindex;

--业务存在性鉴权(MPSS不需要鉴权)
if i_servicetype = 101 then
    v_sql := 'select serviceid,servicename,maincapability,submaincapability,orderupdateurl '||
             'from zxdbm_9900.s9900_service where serviceindex = :p1';
    begin
        execute immediate v_sql into v_serviceid,v_servicename,v_maincapability,v_submaincapability,v_orderupdateurl using v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 7;
        o_debuginfo := 'cpx service info do not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 8;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
--added by youqunxia20130906 order3.02.01
elsif i_servicetype in(1,2,3) then
    begin
        select serviceid,servicename,orderupdateurl,servicesort
        into   v_serviceid,v_servicename,v_orderupdateurl,v_servicesort
        from   zxdbm_200.s200_service
        where  serviceindex = v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 26;
        o_debuginfo := 'service info do not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 27;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
elsif i_servicetype <> 14 then
    begin
        select serviceid,servicename,orderupdateurl
        into   v_serviceid,v_servicename,v_orderupdateurl
        from   zxdbm_200.s200_service
        where  serviceindex = v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 9;
        o_debuginfo := 'service info do not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 10;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

o_serviceid   := v_serviceid;
o_servicename := v_servicename;
o_maincapability    := v_maincapability;
o_submaincapability := v_submaincapability;

--added by youqunxia20130906 order3.02.01
--是否同步订购关系给OIDD,0-否,1-是
if v_servicesort = 1 then
    o_issynctooidd := 1;
end if;

--系统业务能力鉴权
if i_servicetype = 101 then
    begin
        select srvtypeid
        into   v_srvtypeid
        from   zxdbm_ismp.ssys_service_type
        where  servicetype = v_maincapability and subcapability = v_submaincapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 11;
        o_debuginfo := 'cpx ssys_service_type do not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 12;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
       return;
    end;
else
    begin
        select srvtypeid
        into   v_srvtypeid
        from   zxdbm_ismp.ssys_service_type
        where  servicetype = v_servicetype and subcapability = v_subcapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 13;
        o_debuginfo := 'ssys_service_type do not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 14;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;
o_srvtypeid   :=  v_srvtypeid;

--CP/SP存在性鉴权
begin
    select cpid,cpsrvtype,cptype,cpcnshortname,customersrvtel,cpsubscribeurl
    into   v_cpid,v_cpsrvtype,v_cptype,v_cpcnshortname,v_customersrvtel,v_cpsubscribeurl
    from   zxdbm_ismp.scp_basic
    where  cpindex = v_cpindex;
exception when no_data_found then
    o_retcode   := 2000;
    o_debugpos := 15;
    o_debuginfo := 'sp info do not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 16;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;

o_cpid            := v_cpid;
o_cptype          := v_cptype;
o_cpsrvtype       := v_cpsrvtype;
o_cpcnshortname   := v_cpcnshortname;
o_customersrvtel  := v_customersrvtel;
if (trim(v_orderupdateurl) is null) or (length(trim(v_orderupdateurl)) < 8) then
    o_cpsubscribeurl := v_cpsubscribeurl;
else
    o_cpsubscribeurl := v_orderupdateurl;
end if;

--SP接入码查询
if (i_spaccesscode is null) or (length(trim(i_spaccesscode)) = 0) then
    begin
        select spaccesscode into v_spaccesscode
        from zxdbm_ismp.ssrv_srvorder
        where productindex = v_productindex and cmdtype in (1,2) and rownum = 1;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 17;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if trim(v_spaccesscode) is null then
        begin
            select accesscode into v_spaccesscode
            from zxdbm_ismp.ssrv_spaccesscode
            where cpid = v_cpid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 18;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
    o_spaccesscode := v_spaccesscode;
end if;

--
--伪码处理(支持伪码开关打开,非自营sp)--支持业务LCS,SMS,MVS,DLS,MMS,WAP,CPX
--退订时,AuthPriceReq消息中OA(DA)真实号码转化为伪码,同步SP
--这里订购业务的回滚订购关系不做处理

if i_servicetype in (1,2,3,4,41,5,6,101) then
    if i_checkpseudocode = 1 and v_cpsrvtype <> 3 then
        begin
            select pseudocode into o_pseudocode from zxdbm_ismp.susr_basic where usercode = v_oa;
        exception when no_data_found then
            o_retcode   := 1001;
            o_debugpos := 19;
            o_debuginfo := 'userinfo do not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 20;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        o_pcodemsisdnswitch := 1;
    end if;
end if;

-- */

--产品订购关系存在性鉴权
begin
    select subscriptionindex,subscriptionid,status
    into   v_productsubindex,v_productsubid,v_productsubstatus
    from   zxdbm_200.s200_user_subscription
    where  usercode = v_usemsisdn and productid = v_productid;
exception when no_data_found then
    o_retcode   := 1201;
    o_debugpos := 21;
    o_debuginfo := 'subscription info do not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 22;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_productsubindex  := v_productsubindex;
o_productsubid     := v_productsubid;
o_productsubstatus := v_productsubstatus;

if (i_actiontype = 3) then
    if v_productsubstatus not in (0,1,4) then
        o_retcode      := 505;
        o_debugpos    := 23;
        o_debuginfo    := 'product status is 0,1,4.';
        o_neednotifysp := 0;
        return;
    elsif v_productsubstatus = 1 then
        o_neednotifysp := 1;
    else
        o_neednotifysp := 0;
    end if;
elsif (i_actiontype = 4) then
    if v_productsubstatus not in (2,3,5) then
        o_retcode      := 505;
        o_debugpos    := 24;
        o_debuginfo    := 'product status is 2,3,5.';
        o_neednotifysp := 0;
        return;
    elsif v_productsubstatus = 3 then
        o_neednotifysp := 1;
    else
        o_neednotifysp := 0;
    end if;
else
    o_retcode      := 505;
    o_debugpos    := 25;
    o_debuginfo    := 'cmd error.';
    o_neednotifysp := 0;
    return;
end if;

end slp_200_telicomsubchangereq;
/

--暂停/激活产品订购关系更新
prompt zxdbm_200.slp_200_telicomsubchangeresp
create or replace procedure zxdbm_200.slp_200_telicomsubchangeresp
(

i_usemsisdn             in          varchar2,            --使用方用户号码
i_productid             in          varchar2,            --产品代码
i_actiontype            in          number,              --动作类型
i_cmdchannel            in          number,              --动作来源
i_productsubstatus      in          number,              --产品订购关系状态
o_retcode               out         number,              --鉴权返回码 0代表成功,否则代表错误码
o_debugpos             out         number,              --出口顺序标识
o_debuginfo             out         varchar2             --调试信息

)
as

--新增下面几个关于时间的参数,尽量减少sysdate的调用,所有用到sysdate的地方,以to_date(v_sysdate14,'yyyymmddhh24miss')进行代替
v_sysdate14             char(14)                                              := to_char(sysdate ,'yyyymmddhh24miss');

v_usemsisdn             zxdbm_ismp.susr_basic.usercode%type                   := i_usemsisdn;                            --临时存放使用方用户号码
v_productid             zxdbm_200.s200_user_subscription.productid%type       := i_productid;                            --临时存放产品代码
v_productsubstatus      zxdbm_200.s200_user_subscription.status%type          := i_productsubstatus;                     --临时存放订购关系状态
v_cancelchannel         zxdbm_200.s200_user_subscription.cancelchannel%type   := 0;                                      --临时存放暂停恢复渠道

begin

--出参初始化
o_retcode       := 0;
o_debugpos     := 0;
o_debuginfo     := ' ';

--设置退订渠道
begin
    select portalchannel into v_cancelchannel
    from zxdbm_ismp.ssrv_channel_map
    where innerchannel = i_cmdchannel;
exception when no_data_found then
    v_cancelchannel := i_cmdchannel * 1000;
when others then
    o_retcode   := 506;
    o_debugpos := 1;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
--设置更新后产品订购关系的状态
if (i_actiontype = 3) then
    if (v_productsubstatus = 0) then
        v_productsubstatus := 2;
    elsif (v_productsubstatus = 1) then
        v_productsubstatus := 3;
    elsif (v_productsubstatus = 4) then
        v_productsubstatus := 5;
    else
        o_retcode    := 505;
        o_debugpos  := 2;
        o_debuginfo  := 'product status error.';
        return;
    end if;
    begin
        update zxdbm_200.s200_user_subscription
        set    status      = v_productsubstatus,
               statustime  = v_sysdate14,
               subscribeoptype = 1,
               pausetime   = v_sysdate14,
               cancelchannel = v_cancelchannel
        where  usercode = v_usemsisdn and productid = v_productid;
        if (sql%rowcount = 0) then
           o_retcode    := 506;
           o_debugpos  := 3;
           o_debuginfo  := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
           rollback;
        end if;
    exception when others then
        o_retcode    := 506;
        o_debugpos  := 4;
        o_debuginfo  := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        rollback;
        return;
    end;
elsif (i_actiontype = 4) then
    if (v_productsubstatus = 2) then
        v_productsubstatus := 0;
    elsif (v_productsubstatus = 3) then
        v_productsubstatus := 1;
    elsif (v_productsubstatus = 5) then
        v_productsubstatus := 4;
    else
        o_retcode    := 505;
        o_debugpos  := 5;
        o_debuginfo  := 'product status error.';
        return;
    end if;
    begin
        update zxdbm_200.s200_user_subscription
        set    status      = v_productsubstatus,
               statustime  = v_sysdate14,
               subscribeoptype = 2,
               resumetime  = v_sysdate14,
               cancelchannel = v_cancelchannel
        where  usercode = v_usemsisdn and productid = v_productid;
        if (sql%rowcount = 0) then
           o_retcode    := 506;
           o_debugpos  := 6;
           o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
           rollback;
        end if;
    exception when others then
        o_retcode    := 506;
        o_debugpos  := 7;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        rollback;
        return;
    end;
else
    o_retcode    := 505;
    o_debugpos  := 8;
    o_debuginfo := 'cmd error.';
    return;
end if;
commit;

end slp_200_telicomsubchangeresp;
/

--修改从号码, msp专用流程
--本存储过程将无需确认的修改从号码和需二次确认的通知流程和确认修改从号码流程合一
prompt zxdbm_200.slp_200_telicommodifysubreq
create or replace procedure zxdbm_200.slp_200_telicommodifysubreq
(

i_msisdn                in          varchar2,      --订购用户
i_productid             in          varchar2,      --产品id
i_countrycode           in          varchar2,      --国家代码
i_prepaidtype           in          number,        --预付费接口类型 1-OCS 2-非OCS 3-根据号段选择是OCS或者非OCS
o_retcode               out         number,        --返回结果 0:成功;9999:透传到下以节点;other:失败
o_debugpos             out         number,        --用来定位错误具体位置
o_debuginfo             out         varchar2,      --调试信息
o_accesscode            out         varchar2,      --消息的源号码;
o_cpsubscribeurl        out         varchar2,      --SP订购url
o_paytype               out         number,        --用户付费类型,0,后付费;1,预付费。
o_oppaytype             out         number,        --用户付费类型(话单)
o_servicename           out         varchar2,      --业务名称
o_cpid                  out         varchar2,      --cp代码
o_cpname                out         varchar2,      --cp名称
o_serviceid             out         varchar2,      --业务ID
o_subscriptionindex     out         number,        -- 订购关系ID
o_scptype               out         number,        --对预付费扣费的设备类型:1:OCS;2:RTBP
o_productname           out         varchar2,      --产品名称
o_customersrvtel        out         varchar2,      --客服电话
o_msisdntype            out         number,        --用户号码类型
o_smschargingcn         out         varchar2,      --产品资费短信描述
o_servicetype           out         number,        --业务能力类型
o_subcapability         out         number,        --业务能力子类型1-3G,2-PHS,3-ISAG
o_subscriptionid        out         varchar2,      --订购关系id
o_cptype                out         number,        --cp/sp类型 0:是本省   1:是全国业务
o_srvtypeid             out         varchar2,       --业务能力编码,ssys_service_type表中的srvtypeid
o_cdrtsuserindex        out         number,        --流程话单中的用户编号
o_cdrtsusercode         out         varchar2,      --流程话单中的用户号码
o_cpindex               out         number,        --cpindex索引
o_corpindex             out         number,        --corpindex索引
o_corpid                out         varchar2,      --corpid
o_cpsrvtype             out         number         --CP服务类型:0:CP+SP,1:CP,2:SP ;3:自营业务SP

)
is

v_dev_exist           number(3,0)            := 1;                                        --是否在平台ssys_phone表中存在号段信息,0:不存在,1:存在

v_msisdn              zxdbm_ismp.susr_basic.usercode%type                        := ' ';
v_userindex           zxdbm_ismp.susr_basic.userindex%type                       := 0;                         --用户号码序号
v_oppaytype           zxdbm_ismp.susr_basic.oppaytype%type                       := 0;
v_paytype             zxdbm_ismp.susr_basic.oppaytype%type                       := 0;
v_msisdntype          zxdbm_ismp.susr_basic.msisdntype%type                      := 1;                          --用户号码类型
v_productid           zxdbm_200.s200_product.productid%type                      := i_productid;
v_productindex        zxdbm_200.s200_product.productindex%type                   := 0;
v_serviceindex        zxdbm_200.s200_product.serviceindex%type                   := 0;                          --业务编码
v_cpindex             zxdbm_200.s200_product.cpindex%type                        := 0;                          --cp/sp编码
v_productname         zxdbm_200.s200_product.productname%type                    := ' ';
v_smschargingcn       zxdbm_200.s200_product.smschargingcn%type                  := ' ';
v_servicetype         zxdbm_200.s200_product.servicetype%type                    := 0;                          --业务能力类型
v_subcapability       zxdbm_200.s200_product.subcapability%type                  := 0;
v_servicename         zxdbm_200.s200_service.servicename%type                    := ' ';                        --业务名称
v_serviceid           zxdbm_200.s200_service.serviceid%type                      := ' ';                        --业务代码
v_orderupdateurl      zxdbm_200.s200_service.orderupdateurl%type                 := ' ';
v_cpcnshortname       zxdbm_ismp.scp_basic.cpcnshortname%type                    := ' ';                        --cp/sp中文名简写
v_cpid                zxdbm_ismp.scp_basic.cpid%type                             := ' ';
v_cpsubscribeurl      zxdbm_ismp.scp_basic.cpsubscribeurl%type                   := ' ';
v_customersrvtel      zxdbm_ismp.scp_basic.customersrvtel%type                   := ' ';
v_cptype              zxdbm_ismp.scp_basic.cptype%type                           := 0;                          --cp/sp类型 1:全网sp  2: 省(本地)  3: 市
v_cpsrvtype           zxdbm_ismp.scp_basic.cpsrvtype%type                        := 0;                          --临时存放CP服务类型
v_subscriptionindex   zxdbm_200.s200_user_subscription.subscriptionindex%type    := 0;                          --订购关系顺序号
v_subscriptionid      zxdbm_200.s200_user_subscription.subscriptionid%type       := ' ';
v_corpindex           zxdbm_200.s200_user_subscription.corpindex%type            := 0;                          --所属集团标识,0-表示不属于任何集团
v_corpid              zxdbm_200.s200_user_subscription.corpid%type               := ' ';
v_payuser             zxdbm_200.s200_user_subscription.payuser%type              := ' ';
v_phoneindex          zxdbm_ismp.ssys_phone.phoneindex%type                      := 0;
v_attach              zxdbm_ismp.ssys_phone.attach%type                          := 0;                          --预付费归属,0-未知,1-OCS,2-RTBP
v_srvtypeid           zxdbm_ismp.ssys_service_type.srvtypeid%type                 := '';
v_accesscode          zxdbm_ismp.ssrv_spaccesscode.accesscode%type                := ' ';

begin

o_retcode                 := 0;
o_debugpos               := 0;
o_debuginfo               := ' ';
o_accesscode              := ' ';
o_cpsubscribeurl          := ' ';
o_paytype                 := 0;
o_oppaytype               := 0;
o_servicename             := ' ';
o_cpid                    := ' ';
o_cpname                  := ' ';
o_serviceid               := ' ';
o_subscriptionindex       := 0;
o_scptype                 := i_prepaidtype;
o_productname             := ' ';
o_customersrvtel          := ' ';
o_msisdntype              := 1;
o_smschargingcn           := ' ';
o_servicetype             := 1;
o_subcapability           := 1;
o_subscriptionid          := ' ';
o_cptype                  := 0;
o_srvtypeid               := ' ';
o_cdrtsuserindex          := 0;
o_cdrtsusercode           := ' ';
o_cpindex                 := 0;
o_corpindex               := 0;
o_corpid                  := ' ';
o_cpsrvtype               := 0;
/****************************************************
 用户鉴权
*****************************************************/
v_msisdn := ltrim(rtrim(nvl(i_msisdn,'0')));
if (substr(v_msisdn,1,lengthb(i_countrycode)) = i_countrycode) then
    v_msisdn := substr(v_msisdn, lengthb(i_countrycode)+1);
elsif substr(v_msisdn,1,lengthb(i_countrycode)+1) = concat('+',i_countrycode) then
    v_msisdn := substr(v_msisdn, lengthb(i_countrycode)+2);
end if;
o_cdrtsusercode := v_msisdn;

--用户鉴权
if (v_msisdn <> '0') then
    begin
       select userindex,oppaytype,paytype,msisdntype
       into   v_userindex,v_oppaytype,v_paytype,v_msisdntype
       from zxdbm_ismp.susr_basic
       where usercode = v_msisdn;
   exception when no_data_found then
       o_retcode := 0;  --1001;
       o_debugpos := 1;
       o_debuginfo := 'user do not exist.';
   when others then
       o_retcode := 506;
       o_debugpos := 2;
       o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
       return;
   end;
end if;
o_cdrtsuserindex := v_userindex;
o_oppaytype := v_oppaytype;
o_paytype := v_paytype;
o_msisdntype := v_msisdntype;

--取产品信息
begin
    select productindex,serviceindex,cpindex,
         productname,smschargingcn,servicetype,subcapability
    into v_productindex,v_serviceindex,v_cpindex,
         v_productname,v_smschargingcn,v_servicetype,v_subcapability
    from zxdbm_200.s200_product
    where productid = v_productid;
exception when no_data_found then
    o_retcode := 2100;
    o_debugpos := 3;
    o_debuginfo := 'product do not exist.';
    return;
when others then
    o_retcode := 506;
    o_debugpos := 4;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_cpindex := v_cpindex;
o_productname := v_productname;
o_smschargingcn := v_smschargingcn;
o_servicetype := v_servicetype;
o_subcapability := v_subcapability;

--话单用外部业务能力编码
begin
    select srvtypeid into v_srvtypeid
    from zxdbm_ismp.ssys_service_type
    where servicetype = v_servicetype and  subcapability = v_subcapability;
exception when others then
    o_retcode := 506;
    o_debugpos := 5;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_srvtypeid := v_srvtypeid;

--获得业务相关信息
begin
    select servicename,serviceid,orderupdateurl
    into   v_servicename,v_serviceid,v_orderupdateurl
    from zxdbm_200.s200_service
    where serviceindex = v_serviceindex;
exception when no_data_found then
    o_retcode := 2100;
    o_debugpos := 6;
    o_debuginfo := 'service do not exist.';
    return;
when others then
    o_retcode :=506;
    o_debugpos := 7;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_servicename:=v_servicename;
o_serviceid := v_serviceid;

--获得cp中文简称
begin
    select cpcnshortname,cpid,cpsubscribeurl,customersrvtel,cptype,cpsrvtype
    into   v_cpcnshortname,v_cpid,v_cpsubscribeurl,v_customersrvtel,v_cptype,v_cpsrvtype
    from zxdbm_ismp.scp_basic
    where cpindex = v_cpindex;
exception when no_data_found then
    o_retcode := 2000;
    o_debugpos := 8;
    o_debuginfo := 'sp do not exist.';
    return;
when others then
    o_retcode :=506;
    o_debugpos := 9;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_cpname :=v_cpcnshortname;
o_cpid := v_cpid;
o_customersrvtel := v_customersrvtel;
o_cpsrvtype := v_cpsrvtype;
o_cptype          := v_cptype;

if (trim(v_orderupdateurl) is null) or (length(trim(v_orderupdateurl)) < 8) then
    o_cpsubscribeurl := v_cpsubscribeurl;
else
    o_cpsubscribeurl := v_orderupdateurl;
end if;

--SP接入码查询
begin
    select spaccesscode into v_accesscode
    from zxdbm_ismp.ssrv_srvorder
    where productindex = v_productindex and rownum = 1;
exception when no_data_found then
    begin
        select accesscode into v_accesscode
        from zxdbm_ismp.ssrv_spaccesscode
        where cpid = v_cpid and rownum = 1;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 10;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
when others then
    o_retcode   := 506;
    o_debugpos := 11;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_accesscode := v_accesscode;

--检查订购关系
begin
   select subscriptionindex,subscriptionid,corpindex,corpid,payuser
    into  v_subscriptionindex,v_subscriptionid,v_corpindex,v_corpid,v_payuser
    from zxdbm_200.s200_user_subscription
    where usercode = v_msisdn and productid = v_productid;
exception when no_data_found then
    o_retcode := 1201;
    o_debugpos := 12;
    o_debuginfo := 'subscription do not exist.';
    return;
when others then
    o_retcode := 506;
    o_debugpos  := 13;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;
o_subscriptionindex := v_subscriptionindex;
o_subscriptionid := v_subscriptionid;
o_corpindex := v_corpindex;
o_corpid := v_corpid;

----根据用户手机所在号段选择扣费设备是ocs还是rtbp ,1:OCS;2:RTBP;
if (i_prepaidtype not in (1,2)) then
    --attach 预付费归属,0-未知,1-OCS,2-RTBP
    begin
        select phoneindex,attach
        into v_phoneindex,v_attach
        from zxdbm_ismp.ssys_phone
        where startprefix <= substr(v_payuser,1,length(startprefix))
        and endprefix >= substr(v_payuser,1,length(endprefix))
        and phonetype = 1;
    exception when no_data_found then
        o_scptype := 2;
        v_dev_exist := 0; --表示不存在对应号段对应设备配置
    when others then
        o_retcode := 506;
        o_debugpos := 14;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if (v_dev_exist = 1) then
        if (v_attach = 1) then
            o_scptype := 1;
        elsif (v_attach = 2) then
            o_scptype := 2;
        else
            begin
                select 1 into o_scptype
                from zxdbm_ismp.sdev_phone
                where phoneindex = v_phoneindex
                and devicetype = 5;
            exception when no_data_found then
                o_scptype := 2;
            when others then
                o_retcode := 506;
                o_debugpos := 15;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
        end if;
    end if;
end if;

end slp_200_telicommodifysubreq;
/

/*
支持退订过程是否先同步订购关系再退订( 0-先退订后同步 1-先同步后退订)两种模式
增加存储过程 slp_200_telicomprecancel
*/

prompt zxdbm_200.slp_200_telicomprecancel
create or replace procedure zxdbm_200.slp_200_telicomprecancel
(

i_oa                    in          varchar2,            --发起方用户号码
i_oatype                in          number,              --发起方地址类型
i_srvprodid             in          varchar2,            --业务/产品代码
i_serviceid             in          varchar2,            --业务代码
i_actiontype            in          number,              --动作类型
i_cmdchannel            in          number,              --动作来源
i_lallindex             in          number,              --流程分支:1鉴权批价 91二次确认超时
i_servicetype           in          number,              --业务能力类型
i_countrycode           in          varchar2,            --国家码
i_spaccesscode          in          varchar2,            --antuprice消息中的SP接入码
i_silent                in          number,              --沉默去激活开关
i_needauthuser          in          number,              --是否需要鉴权用户 0-不需要 1-需要
i_needreplay            in          number,              --产品退订是否需要二次确认
i_direct                in          number,              --订购方向 0:正向退订 1:SP反向退订
i_supportproductid      in          number,              --同步SP、SP下发使用service还是productid 0-serviceid 1-productid
--i_prepaidtype           in          number,              --预付费接口类型 1-OCS 2-非OCS 3-根据号段选择是OCS或者非OCS
i_catchflag             in          number,              --二次确认类型 1-订购  2,3-退订 4-改号
i_checkpseudocode       in          number,              --系统开关:是否支持伪码   0-不支持   1-支持
i_mutidbmode            in          number,              --是否多DB环境 0:否 1:是
i_eventid               in          varchar2,            --事件ID
i_channelid             in          varchar2,            --渠道商ID
--added by youqunxia 20130815  Order3.02.01
--i_effectiveswitch       in          number,              --VSOP发起的订购退订立即生效开关是否打开,0-关闭,1-打开
o_retcode               out         number,              --鉴权返回码 0代表成功,否则代表错误码
o_debugpos             out          number,              --出口顺序标识
o_debuginfo             out         varchar2,            --调试信息
o_usemsisdn             out         varchar2,            --去掉前缀的使用方用户号码
o_paymsisdn             out         varchar2,            --去掉前缀的计费方号码
o_usemsisdntype         out         number,              --使用方号码类型,1:msisdn,2:phs,3:pstn
o_paymsisdntype         out         number,              --付费方号码类型,1:msisdn,2:phs,3:pstn
o_paytype               out         number,              --计费号码的付费类型 0:后付费 1:预付费 2:准预付费
o_oppaytype             out         number,              --话单专用付费类型
o_cpid                  out         varchar2,            --CP/SP代码
o_cpcnshortname         out         varchar2,            --CP/SP名称
o_cpunsubscribeurl      out         varchar2,            --CP/SP退订URL
o_cptype                out         number,              --CP/SP类型 0:本省 1:是全国业务
o_customersrvtel        out         varchar2,            --CP/SP客服电话
o_serviceid             out         varchar2,            --业务代码
o_productid             out         varchar2,            --产品代码
o_productname           out         varchar2,            --产品名称
o_smschargingcn         out         varchar2,            --产品资费短信描述
o_subcapability         out         number,              --产品业务能力子类型
o_srvtypeid             out         varchar2,            --业务能力编码,对应ssys_service_type表中的srvtypeid
o_subendtime            out         varchar2,            --产品订购关系失效时间
o_linknum               out         varchar2,            --业务话单中同一个流程中二次确认前后话单的关联
o_pseudocode            out         varchar2,            --伪码
o_pcodemsisdnswitch     out         number,              --伪码和真实号码之间是否有转换:  0否   1真实号码转化伪码 2伪码转化为真实号码
o_chanelplayerstate     out         number,              --cp/sp状态,0-申请,1-正常,2-暂停(暂停代收费),3-预注销,4-注销,5-冻结,21-暂停端口
o_eventstate            out         number,              --0,正常 1,注销
o_cpsrvtype             out         number,              --CP服务类型:0:CP+SP,1:CP,2:SP ;3:自营业务SP
o_cdrbegintime          out         varchar2,            --话单计费开始时间
o_cdrendtime            out         varchar2,            --话单计费结束时间
o_producttype           out         number,              --产品类型   1-正常产品 2-测试产品
o_cityid                out         varchar2,            --话单计费结束时间
--added by youqunxia20130906 Order3.02.01
o_issynctooidd          out         number                --是否同步订购关系给OIDD,0-否,1-是

)
as

--新增下面几个关于时间的参数,尽量减少sysdate的调用,所有用到sysdate的地方,以to_date(v_sysdate14,'yyyymmddhh24miss')进行代替
v_sysdate14             char(14)     := to_char(sysdate ,'yyyymmddhh24miss');
v_sysdate8              char(8)      := substr(v_sysdate14,1,8);
v_curmonth              number(2)    := to_number(substr(v_sysdate14,5,2));     --临时存放当前月份
v_curstrmonth           char(2)      := lpad(to_char(v_curmonth),2,'0');        --临时存放当前月份,字符型
v_curmonthendtime       char(14)     := to_char(last_day(to_date(v_sysdate14,'yyyymmddhh24miss')),'yyyymmdd')||'235959';

v_maincapability        number(10)                                                 := 0;                       --临时存放组合业务主业务能力类型
v_submaincapability     number(10)                                                 := 0;                       --临时存放组合业务主业务能力子类型
v_countrycode           varchar2(5)                                                := i_countrycode;           --临时存放国家码
v_tempitem              number(10)                                                 := 0;                       --临时存放0或者推广期的计费子序号
v_cmdchannel            number(10)                                                 := i_cmdchannel;            --临时存放动作来源
v_tmpint                number(10)                                                 := 0;                       --临时存放临时数值
v_devexist              number(3)                                                  := 1;                       --临时存放是否存在对应设备
v_leftdays              number(10)                                                 := 0;                       --本月剩余天数
v_monthdays             number(10)                                                 := 0;                       --本月有效收费天数
v_sql                   varchar2(3072)                                             := ' ';                     --临时存放动态sql
v_isservicesub          number(3)                                                  := 0;                       --临时存放是否业务订购
v_spaccesscode          varchar2(40)                                               := ' ';                     --临时存放sp接入码
v_silentdays            number(10)                                                 := 0;                       --沉默用户天数
v_nextrentfee           number(10)                                                 := 0;                       --临时存放待生效包月费
v_rentfeevalidate       char(6)                                                    := ' ';                     --临时存放待生效包月费率生效月
v_totaldctemplate       varchar2(500)                                              := ' ';                     --临时存放解析模板:产品分档费率
v_dealtemplate          varchar2(500)                                              := ' ';                     --临时存放解析模板:待解析的分档段
v_totaldclength         number(3)                                                  := 0;                       --临时存放解析模板: 分档字符串的长度
v_totaldcsum            number(10)                                                 := 0;                       --临时存放解析模板: 分档总费用
v_index0                number(3)                                                  := 0;                       --临时存放解析模板: 分档字符串中&的位置
v_index1                number(3)                                                  := 0;                       --临时存放解析模板: 分档字符串中;的位置
v_nextrentdatecycle     char(14)                                                   := v_sysdate14;             --临时存放包周期产品的下次计划扣租时间
v_freeuseflag           number(3)                                                  := 0;                       --临时存入流媒体业务的试用期属性
v_hasrecord             number(3)                                                  := 0;                       --存放需要退订的产品是否包含在捆绑销售品中(0,不包含;1,包含)

v_oa                    zxdbm_ismp.susr_basic.usercode%type                        := ' ';                     --临时存放发起方用户号码
v_usemsisdn             zxdbm_ismp.susr_basic.usercode%type                        := ' ';                     --临时存放使用方用户号码
v_paymsisdn             zxdbm_ismp.susr_basic.usercode%type                        := ' ';                     --临时存放计费方用户号码
v_useuserindex          zxdbm_ismp.susr_basic.userindex%type                       := 0;                       --临时存放使用方用户序号
v_payuserindex          zxdbm_ismp.susr_basic.userindex%type                       := 0;                       --临时存放计费方用户序号
v_useusertype           zxdbm_ismp.susr_basic.usertype%type                        := 1;                       --临时存放使用方用户类型
v_usepaytype            zxdbm_ismp.susr_basic.paytype%type                         := 0;                       --临时存放使用方用户付费类型
v_paypaytype            zxdbm_ismp.susr_basic.paytype%type                         := 0;                       --临时存放付费方用户付费类型
v_useblacklist          zxdbm_ismp.susr_basic.blacklist%type                       := 0;                       --临时存放使用方是否进入黑名单
v_usewhitelist          zxdbm_ismp.susr_basic.whitelist%type                       := 0;                       --临时存放使用方是否进入白名单
v_useredlist            zxdbm_ismp.susr_basic.redlist%type                         := 0;                       --临时存放使用方是否进入红名单
v_useblacklevel         zxdbm_ismp.susr_basic.blacklevel%type                      := 0;                       --临时存放使用方黑名单级别
v_usewhitelevel         zxdbm_ismp.susr_basic.whitelevel%type                      := 0;                       --临时存放使用方白名单级别
v_usemsisdntype         zxdbm_ismp.susr_basic.msisdntype%type                      := 1;                       --临时存放使用方号码类型
v_paymsisdntype         zxdbm_ismp.susr_basic.msisdntype%type                      := 1;                       --临时存放付费方号码类型
v_useuserstatus         zxdbm_ismp.susr_basic.status%type                          := 0;                       --临时存放使用方状态
v_useactivestatus       zxdbm_ismp.susr_basic.activestatus%type                    := 0;                       --临时存放使用方激活状态
v_useoppaytype          zxdbm_ismp.susr_basic.oppaytype%type                       := 0;                       --临时存放使用方话单专用付费类型
v_payoppaytype          zxdbm_ismp.susr_basic.oppaytype%type                       := 0;                       --临时存放付费方话单专用付费类型
v_usecorpindex          zxdbm_ismp.susr_basic.corpindex%type                       := 0;                       --临时存放使用方集团序号
v_useusetime            zxdbm_ismp.susr_basic.lastusetime%type                     := '00000000000000';        --临时存放使用方上次使用时间
v_useusercityid         zxdbm_ismp.susr_basic.cityid%type                          := ' ';                                       --临时存放计费方号码类型
v_payusercityid         zxdbm_ismp.susr_basic.cityid%type                          := ' ';                                       --临时存放计费方号码类型

v_usecorpid             zxdbm_ismp.susr_corp.corpid%type                           := ' ';                     --临时存放集团代码
v_productindex          zxdbm_200.s200_product.productindex%type                   := 0;                       --临时存放产品序号
v_invalidmode           zxdbm_200.s200_product.invalidmode%type                    := 0;                       --退定 0-立即生效,1-下月生效
v_validcyctype          zxdbm_200.s200_product.validcyctype%type                   := 0;                       --临时存放周期方式0-非周期,1-天,2-周,3-月,定购生效方式为2时生效
v_validcycparam         zxdbm_200.s200_product.validcycparam%type                  := 0;                       --临时存放周期参数,定购生效方式为2时生效
v_validdate             zxdbm_200.s200_product.validdate%type                      := v_sysdate14;             --临时存放指定生效日期
v_subfeemode            zxdbm_200.s200_product.subfeemode%type                     := 1;                       --临时存放定购当月收费方式
v_subfeedays            zxdbm_200.s200_product.subfeedays%type                     := 0;                       --临时存放收费日期取值范围为0~28,0表示本月全部免费,其他表示 n号之后免费。
v_freedays              zxdbm_200.s200_product.freedays%type                       := 0;                       --临时存放免费试用天数
v_popustart             zxdbm_200.s200_product.popularizestart%type                := v_sysdate8;              --临时存放推广期开始时间
v_popustop              zxdbm_200.s200_product.popularizestop%type                 := v_sysdate8;              --临时存放推广期结束时间
v_popuitem              zxdbm_200.s200_product.popularizeitem%type                 := 0;                       --临时存放推广期计费方案,0表示免费,1-推广期方案
v_needcharge            zxdbm_200.s200_product.needcharge%type                     := 1;                       --临时存放是否需要计费,0-免费,1-需要计费
v_productname           zxdbm_200.s200_product.productname%type                    := ' ';                     --临时存放产品名称
v_productstatus         zxdbm_200.s200_product.status%type                         := 0;                       --临时存放产品状态
v_onlyforcorp           zxdbm_200.s200_product.onlyforcorp%type                    := 0;                       --临时存放是否集团专用产品0:个人用户专用1:集团专用
v_productstarttime      zxdbm_200.s200_product.starttime%type                      := v_sysdate14;             --临时存放产品生效时间
v_productendtime        zxdbm_200.s200_product.endtime%type                        := '99991231235959';        --临时存放产品终止时间
v_workflow              zxdbm_200.s200_product.workflow%type                       := 0;                       --临时存放工作流状态
v_workflowlife          zxdbm_200.s200_product.workflowlife%type                   := 0;                       --临时存放工作流流程状态
v_cpindex               zxdbm_200.s200_product.cpindex%type                        := 0;                       --临时存放CP/SP序号
v_serviceindex          zxdbm_200.s200_product.serviceindex%type                   := 0;                       --临时存放业务序号
v_smschargingcn         zxdbm_200.s200_product.smschargingcn%type                  := ' ';                     --临时存放产品资费短信描述
v_canbepresent          zxdbm_200.s200_product.canbepresent%type                   := 0;                       --临时存放是否可赠送,0-不可赠送,1-可赠送
v_servicetype           zxdbm_200.s200_product.servicetype%type                    := 0;                       --临时存放业务能力类型
v_subcapability         zxdbm_200.s200_product.subcapability%type                  := 1;                       --临时存放业务能力子类型1-3G,2-PHS,3-ISAG,默认为3g业务能力(仅电信使用,其他营运商皆默认为1)
v_srvprodid             zxdbm_200.s200_product.productid%type                      := i_srvprodid;             --临时存放业务产品代码
v_productid             zxdbm_200.s200_product.productid%type                      := ' ';                     --临时存放产品代码
v_onlyforpkg            zxdbm_200.s200_product.onlyforpkg%type                     := 0;                       --临时存放是否套餐专用产品
v_backfeemode           zxdbm_200.s200_product.backfeemode%type                    := 0;                       --临时存放退定补款方式,0-不补款,1-按帐期剩余实际天数补款
v_chargetype            zxdbm_200.s200_product_fee.chargetype%type                 := 0;                       --临时存放计费类型
v_subchargetype         zxdbm_200.s200_product_fee.subchargetype%type              := 0;                       --临时存放计费子类型
v_cyctype               zxdbm_200.s200_product_fee.cyctype%type                    := 0;                       --临时存放周期类型0-非周期,1-周期性
v_cycunit               zxdbm_200.s200_product_fee.cycunit%type                    := 0;                       --临时存放周期单位 0-无,1-天,2-周,3-月,4-半年,5-年,6-小时,7-分钟,8-秒
v_cycparam              zxdbm_200.s200_product_fee.cycparam%type                   := 0;                       --临时存放周期参数
v_rentfee               zxdbm_200.s200_product_fee.rentfee%type                    := 0;                       --临时存放周期性费用金额
v_fixfee                zxdbm_200.s200_product_fee.fixfee%type                     := 0;                       --临时存放包量费用
v_fixusetimes           zxdbm_200.s200_product_fee.fixusetimes%type                := 0;                       --临时存放包量使用值,0-无限制
v_unitvol               zxdbm_200.s200_product_fee.unitvol%type                    := 10;                      --临时存放相对于目前最小计量单位的量,时长为百毫秒,流量为byte
v_autocontinue          zxdbm_200.s200_product_fee.autocontinue%type               := 0;                       --临时存放周期性定购,是否自动续订
v_minlimitfee           zxdbm_200.s200_product_fee.minlimitfee%type                := 0;                       --临时存放保底费用,0-无保底费
v_usecomfee             zxdbm_200.s200_product_fee.usecomfee%type                  := 1;                       --临时存放限制(量、时间)外使用方式,1-使用基本费率,0-不允许继续使用
v_totaldiscount         zxdbm_200.s200_product_fee.totaldiscount%type              := ' ';                            --临时存放限制(量、时间)外使用方式,1-使用基本费率,0-不允许继续使用
v_cpid                  zxdbm_ismp.scp_basic.cpid%type                             := ' ';                     --临时存放CP/SP代码
v_cpstatus              zxdbm_ismp.scp_basic.status%type                           := 0;                       --临时存放CP/SP代码
v_cpsubscribeurl        zxdbm_ismp.scp_basic.cpsubscribeurl%type                   := ' ';                     --临时存放CP/SP订购URL
v_cpunsubscribeurl      zxdbm_ismp.scp_basic.cpunsubscribeurl%type                 := ' ';                     --临时存放CP/SP退订URL
v_hasblacklist          zxdbm_ismp.scp_basic.hasblacklist%type                     := 0;                       --临时存放是否有黑名单控制 0-无,1-有
v_cptype                zxdbm_ismp.scp_basic.cptype%type                           := 2;                       --临时存放CP/SP类型
v_cpsrvtype             zxdbm_ismp.scp_basic.cpsrvtype%type                        := 0;                       --临时存放CP服务类型
v_insubblacklist        zxdbm_ismp.scp_basic.insubblacklist%type                   := 0;                       --临时存放限制订购SP黑名单标识
v_cpcnshortname         zxdbm_ismp.scp_basic.cpcnshortname%type                    := ' ';                     --临时存放CP/SP中文名简写
v_customersrvtel        zxdbm_ismp.scp_basic.customersrvtel%type                   := ' ';                     --临时存放CP/SP客服电话
v_protocoltype          zxdbm_200.s200_sp_service_type.protocoltype%type           := 0;                       --临时存放定购同步协议类型0-ismp(wbs方式),1-smgp2.0(cngp1.0),2-smgp3.0(cngp1.0扩展)
v_srvtypeid             zxdbm_ismp.ssys_service_type.srvtypeid%type                := ' ';                     --临时存放话单中的srvtypeid
v_productcancelmode     zxdbm_ismp.ssrv_corp_product.unsubscribemode%type          := 0;                       --临时存放退订方式,0-必须集团管理员退订,1-集团管理员与集团成员均可退订
v_productpayparty       zxdbm_ismp.ssrv_corp_product.payparty%type                 := 2;                       --临时存放集团专用产品付费方 1-集团付费 2-个人付费
v_productsubindex       zxdbm_200.s200_user_subscription.subscriptionindex%type    := 0;                       --临时存放订购关系序号
v_productsubid          zxdbm_200.s200_user_subscription.subscriptionid%type       := ' ';                     --临时存放订购关系代码
v_serviceid             zxdbm_200.s200_user_subscription.serviceid%type            := i_serviceid;             --临时存放业务代码
v_propkgindex           zxdbm_200.s200_user_subscription.productpkgindex%type      := 0;                       --临时存放套餐序号
v_propkgid              zxdbm_200.s200_user_subscription.productpkgid%type         := ' ';                     --临时存放套餐代码
v_contentindex          zxdbm_200.s200_user_subscription.contentindex%type         := 0;                       --临时存放内容序号
v_contentid             zxdbm_200.s200_user_subscription.contentid%type            := ' ';                     --临时存放内容代码
v_corpindex             zxdbm_200.s200_user_subscription.corpindex%type            := 0;                       --临时存放集团序号
v_corpid                zxdbm_200.s200_user_subscription.corpid%type               := ' ';                     --临时存放集团代码
v_subscriber            zxdbm_200.s200_user_subscription.subscriber%type           := ' ';                     --发起方用户号码
v_usercode              zxdbm_200.s200_user_subscription.usercode%type             := ' ';                     --使用方用户号码
v_payuser               zxdbm_200.s200_user_subscription.payuser%type              := ' ';                     --计费方用户号码
v_subscribemode         zxdbm_200.s200_user_subscription.subscribemode%type        := 1;                       --临时存放订购方式
v_subscribechannel      zxdbm_200.s200_user_subscription.subscribechannel%type     := 3;                       --临时存放订购渠道
v_subscribeoptype       zxdbm_200.s200_user_subscription.subscribeoptype%type      := 0;                       --临时存放订购操作
v_pausemode             zxdbm_200.s200_user_subscription.pausemode%type            := 0;                       --临时存放产品暂停原因
v_resumemode            zxdbm_200.s200_user_subscription.resumemode%type           := 0;                       --临时存放产品恢复原因
v_pushid                zxdbm_200.s200_user_subscription.pushid%type               := ' ';                     --临时存放推荐者ID
v_substarttime          zxdbm_200.s200_user_subscription.starttime%type            := v_sysdate14;             --临时存放定购生效时间
v_cursubendtime         zxdbm_200.s200_user_subscription.endtime%type              := v_sysdate14;             --临时存放当前定购关系失效时间
v_subendtime            zxdbm_200.s200_user_subscription.endtime%type              := '99991231235959';        --临时存放定购失效时间
v_lastrenttime          zxdbm_200.s200_user_subscription.lastrenttime%type         := '00000000000000';        --临时存放上次扣租时间
v_nextrentdate          zxdbm_200.s200_user_subscription.nextrentdate%type         := v_sysdate8;              --临时存放下次扣租日期
v_payfordate            zxdbm_200.s200_user_subscription.payfordate%type           := '00000000';              --临时存放月租已付日期
v_returnvalues          zxdbm_200.s200_user_subscription.returnvalues%type         := 0;                       --临时存放下一周期返还值
v_returntype            zxdbm_200.s200_user_subscription.returntype%type           := 1;                       --临时存放返还量类型
v_returnstartdate       zxdbm_200.s200_user_subscription.returnstartdate%type      := '00000000';              --临时存放返还值开始日期
v_returnenddate         zxdbm_200.s200_user_subscription.returnenddate%type        := '00000000';              --临时存放返还值结束日期
v_chargeparty           zxdbm_200.s200_user_subscription.chargeparty%type          := 0;                       --临时存放定购时填入,扣月租使用,计费方 0:用户;1:SP;2:集团
v_freetime              zxdbm_200.s200_user_subscription.freetime%type             := '00000000000000';        --临时存放产品订购免费日期
v_createtime            zxdbm_200.s200_user_subscription.createtime%type           := '00000000000000';        --临时存放用户订购时间
v_canceltime            zxdbm_200.s200_user_subscription.canceltime%type           := '00000000000000';        --临时存放用户退定时间
v_firstusetime          zxdbm_200.s200_user_subscription.firstusetime%type         := '00000000000000';        --临时存放用户首次使用时间
v_lastusetime           zxdbm_200.s200_user_subscription.lastusetime%type          := '00000000000000';        --临时存放用户上次使用时间
v_tastestarttime        zxdbm_200.s200_user_subscription.tastestarttime%type       := '00000000000000';        --临时存放体验期开始时间
v_tasteendtime          zxdbm_200.s200_user_subscription.tasteendtime%type         := '00000000000000';        --临时存放体验期结束时间
v_pausetime             zxdbm_200.s200_user_subscription.pausetime%type            := '00000000000000';        --临时存放暂停时间
v_resumetime            zxdbm_200.s200_user_subscription.resumetime%type           := '00000000000000';        --临时存放暂停恢复时间
v_subscribeopid         zxdbm_200.s200_user_subscription.subscribeopid%type        := ' ';                     --临时存放定购操作员工号
v_cancelreason          zxdbm_200.s200_user_subscription.cancelreason%type         := ' ';                     --临时存放退定原因描述
v_lastrent              zxdbm_200.s200_user_subscription.lastrent%type             := 0;                       --临时存放上次扣租金额
v_rollbacklastrent      zxdbm_200.s200_user_subscription.lastrent%type             := 0;                       --临时存放上次扣租金额(订购关系待失效产品的订购失败回滚)
v_msisdntype            zxdbm_200.s200_user_subscription.msisdntype%type           := 1;                       --临时存放用户号码类型 1-msisdn,2-phs,3-pstn,4-IPTV接入号,5-宽带接入号,6-固定IP
v_productsubstatus      zxdbm_200.s200_user_subscription.status%type               := 0;
v_pseudocode            zxdbm_200.s200_user_subscription.pseudocode%type           := ' ';                     --临时存放订购关系表中伪码
--added by youqunxia20130608   order2.07.10
v_pproductofferid       zxdbm_200.s200_user_subscription.pproductofferid%type      := ' ';                     --临时存放增值产品对应的捆绑类套餐ID
v_varifee               zxdbm_200.s200_user_subscription.varifee%type              := '';                      --临时存放当前订购关系中的可变价格                                    
v_eventid               zxdbm_200.s200_user_subscription.eventid%type              := '';                      --临时存放事件ID
v_isexperience          zxdbm_200.s200_user_subscription.isexperience%type         := 0;                       --订购类型:0:正常订购 1:免费体验 2:积分兑换 3:体验转正式 默认为0
v_subscribetype         zxdbm_200.s200_user_subscription.subscribetype%type        := 0;                       --订购方式:0:非批量受理 1: 批量受理 默认为0
v_chargetime            zxdbm_200.s200_user_subscription.chargetime%type           := '00000000000000';        --首次扣费时间

v_orderupdateurl        zxdbm_200.s200_service.orderupdateurl%type                 := ' ';                     --临时存放业务表中
v_subsrvtype            zxdbm_200.s200_service.subsrvtype%type                     := 1;                       --临时存放业务子类型,1-wap pull,2-wap push,3-op wap push,4-wap pull 下载,101-试用业务(也表示 wap pull试用业务) ;102:wap push 试用业务;1001 - 基本通道费;1002
v_servicesort           zxdbm_200.s200_service.servicesort%type                    := 0;                       --业务归属 0:普通业务、1:属于OIDD 2:属于定位

v_phoneindex            zxdbm_ismp.ssys_phone.phoneindex%type                      := 0;                       --临时存放号段序号
v_attach                zxdbm_ismp.ssys_phone.attach%type                          := 0;                       --临时存放预付费归属,0-未知,1-OCS,2-RTBP

begin

--出参初始化
o_retcode               := 0;
o_debugpos             := 0;
o_debuginfo             := ' ';
o_usemsisdn             := ' ';
o_paymsisdn             := ' ';
o_usemsisdntype         := 1;
o_paymsisdntype         := 1;
o_paytype               := 0;
o_oppaytype             := 0;
o_cpid                  := ' ';
o_cpcnshortname         := ' ';
o_cpunsubscribeurl      := ' ';
o_cptype                := 0;
o_customersrvtel        := ' ';
o_serviceid             := ' ';
o_productid             := ' ';
o_productname           := ' ';
o_smschargingcn         := ' ';
o_subcapability         := 1;
o_srvtypeid             := ' ';
o_subendtime            := '99991231235959';
o_linknum               := ' ';
o_pseudocode            :='';
o_pcodemsisdnswitch     :=0;
o_chanelplayerstate     :=0;
o_eventstate            :=1;
o_cpsrvtype             :=0;
o_cdrbegintime          := substr(v_sysdate8,1,6) || '01000000';
o_cdrendtime            := v_curmonthendtime;
o_producttype           := 1;
o_cityid                := '';
o_issynctooidd          := 0;

/**
1.预鉴权

 发起方地址类型
 发起方用户号码去前缀

**/

--发起方地址类型鉴权 现网局点可考虑注释.
--批量操作不进行鉴权,由业务从用户表中查询处理
--modied by youqunxia20130709 Order2.07.10
if (i_actiontype <> 17 and i_lallindex <> 91) then
    if i_oatype not in (1,2,3,4) then
        o_retcode   := 107;
        o_debugpos := 1;
        o_debuginfo := 'i_oatype='||i_oatype||', not in (1,2,3,4).';
        return;
    end if;
end if;

--发起方用户号码去前缀
v_oa := ltrim(rtrim(nvl(i_oa,'0')));
if v_oa like ('+' || v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 2);
elsif v_oa like ('00' || v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 3);
elsif v_oa like (v_countrycode || '%') then
    v_oa := substr(v_oa, length(v_countrycode) + 1);
end if;
if (substr(v_oa,1,1) <> '0') and (i_oatype = 4) then
    v_oa := '0' || v_oa;
end if;
v_usemsisdn := v_oa;
o_usemsisdn  := v_usemsisdn;
if (v_usemsisdn = '0') then
    o_retcode   := 1001;
    o_debugpos := 2;
    o_debuginfo := 'v_usemsisdn='||v_usemsisdn||', v_usemsisdn is 0.';
    return;
end if;

/**
2.使用方号码存在性鉴权
**/

if (i_needauthuser = 1) then
    begin
        select userindex,usertype,paytype,blacklist,whitelist,
               redlist,blacklevel,whitelevel,msisdntype,status,
               activestatus,oppaytype,corpindex,lastusetime,cityid
        into   v_useuserindex,v_useusertype,v_usepaytype,v_useblacklist,v_usewhitelist,
               v_useredlist,v_useblacklevel,v_usewhitelevel,v_usemsisdntype,v_useuserstatus,
               v_useactivestatus,v_useoppaytype,v_usecorpindex,v_useusetime,v_useusercityid
        from   zxdbm_ismp.susr_basic
        where  usercode = v_usemsisdn;
    exception when no_data_found then
        o_retcode   := 1001;
        o_debugpos := 3;
        o_debuginfo := 'usercode do not exist.';
        if (i_oatype = 4) then
            v_usemsisdntype    := 2;
        else
            v_usemsisdntype    := 1;
        end if;
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 4;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        if (i_oatype = 4) then
            o_usemsisdntype    := 2;
        else
            o_usemsisdntype    := 1;
        end if;
        return;
    end;
    v_paymsisdn     := v_usemsisdn;
    v_paymsisdntype := v_usemsisdntype;
    v_payuserindex  := v_useuserindex;
    o_paymsisdn     := v_paymsisdn;
    o_usemsisdntype := v_usemsisdntype;
    o_paymsisdntype := v_paymsisdntype;
    o_paytype       := v_usepaytype;
    o_oppaytype     := v_useoppaytype;
    o_cityid        := trim(v_useusercityid);

    --批量操作用户号码类型由用户表查询后鉴权
    if (i_cmdchannel in (7,32)) then
         if v_usemsisdntype not in (1,2,3) then
             o_retcode   := 100;
             o_debugpos := 5;
             o_debuginfo := 'v_usemsisdntype='||v_usemsisdntype||', not in (1,2,3);';
             return;
         end if;
    end if;

    if i_mutidbmode = 0 then
        if (v_useactivestatus = 0) then
            --待激活用户激活操作
            begin
                update  zxdbm_ismp.susr_basic
                set     activestatus = 1,
                        activetime = v_sysdate14
                where   userindex = v_useuserindex;
                if (sql%rowcount = 0) then
                    o_retcode   := 506;
                    o_debugpos := 6;
                    o_debuginfo := 'usercode do not exist.';
                    rollback;
                    return;
                end if;
                commit;
            exception when others then
                o_retcode   := 506;
                o_debugpos := 7;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
        else
            --已激活用户更新最近一次使用时间
            if (i_silent = 1) then
                if (substr(v_useusetime,1,8) <> v_sysdate8) then
                    begin
                        update  zxdbm_ismp.susr_basic
                        set     lastusetime = v_sysdate14
                        where   userindex = v_useuserindex;
                        if (sql%rowcount = 0) then
                            o_retcode   := 506;
                            o_debugpos := 8;
                            o_debuginfo := 'usercode do not exist.';
                            rollback;
                            return;
                        end if;
                        commit;
                    exception when others then
                        o_retcode   := 506;
                        o_debugpos := 9;
                        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                        return;
                    end;
                end if;
            end if;
        end if;
    end if;
end if;

/**
3.产品存在性鉴权
**/

--产品互斥时,ESMGP传21个0在productid字段当中,同时传进来业务ID,此时以业务ID来获取真实的产品ID
if v_srvprodid = '000000000000000000000' then
    v_sql := 'select isservicesub from zxdbm_200.s200_service where serviceid = :p1';
    begin
        execute immediate v_sql into v_isservicesub using v_serviceid;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 10;
        o_debuginfo := 'serviceindex='||v_serviceid||', the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 11;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if v_isservicesub <> 1 then
        o_retcode   := 505;
        o_debugpos := 12;
        o_debuginfo := 'esmgp parse error.';
        return;
    end if;
    --获取真实的产品ID,用于后续的操作
    begin
        select productid
        into   v_srvprodid
        from   zxdbm_200.s200_user_subscription
        where  usercode = v_usemsisdn and serviceid = v_serviceid;
    exception when no_data_found then
        o_retcode   := 1201;
        o_debugpos := 13;
        o_debuginfo := o_debuginfo ||'v_usemsisdn = '||v_usemsisdn||'v_serviceid = '||v_serviceid||'the subscription dose not exist;';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 14;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

if (i_direct = 1 and i_supportproductid = 0) then
--反向订购并且支持业务ID的退订
    begin
        select productindex,invalidmode,validcyctype,
               validcycparam,validdate,subfeemode,subfeedays,freedays,
               popularizestart,popularizestop,popularizeitem,needcharge,
               productname,status,onlyforcorp,starttime,endtime,
               workflow,workflowlife,cpindex,serviceindex,smschargingcn,canbepresent,
               servicetype,subcapability,onlyforpkg,productid,backfeemode
        into   v_productindex,v_invalidmode,v_validcyctype,
               v_validcycparam,v_validdate,v_subfeemode,v_subfeedays,v_freedays,
               v_popustart,v_popustop,v_popuitem,v_needcharge,
               v_productname,v_productstatus,v_onlyforcorp,v_productstarttime,v_productendtime,
               v_workflow,v_workflowlife,v_cpindex,v_serviceindex,v_smschargingcn,v_canbepresent,
               v_servicetype,v_subcapability,v_onlyforpkg,v_productid,v_backfeemode
        from  zxdbm_200.s200_product
        where serviceid = v_srvprodid and ordertype = 1;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 15;
        o_debuginfo := 'serviceid='||v_srvprodid||', ordertype = 1'||', the product does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 16;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
else
    begin
        select productindex,invalidmode,validcyctype,
               validcycparam,validdate,subfeemode,subfeedays,freedays,
               popularizestart,popularizestop,popularizeitem,needcharge,
               productname,status,onlyforcorp,starttime,endtime,
               workflow,workflowlife,cpindex,serviceindex,smschargingcn,canbepresent,
               servicetype,subcapability,productid,backfeemode,serviceid
        into   v_productindex,v_invalidmode,v_validcyctype,
               v_validcycparam,v_validdate,v_subfeemode,v_subfeedays,v_freedays,
               v_popustart,v_popustop,v_popuitem,v_needcharge,
               v_productname,v_productstatus,v_onlyforcorp,v_productstarttime,v_productendtime,
               v_workflow,v_workflowlife,v_cpindex,v_serviceindex,v_smschargingcn,v_canbepresent,
               v_servicetype,v_subcapability,v_productid,v_backfeemode,v_serviceid
        from   zxdbm_200.s200_product
        where  productid = v_srvprodid;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 17;
        o_debuginfo   := 'productid='||v_srvprodid||', the product does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 18;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;
o_productid      := v_productid;
o_productname    := v_productname;
o_serviceid      := v_serviceid;
o_smschargingcn  := v_smschargingcn;
o_subcapability  := v_subcapability;
if i_servicetype = 5 then
    v_sql := 'select freeuseflag,freedays from zxdbm_200.s200_product where productindex = :p1';
    begin
        execute immediate v_sql into v_freeuseflag,v_freedays using v_productindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 19;
        o_debuginfo   := 'The product does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 20;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;
--产品状态鉴权
if (v_productstatus in (0,2) and (v_workflowlife = 3)) then
    o_producttype := 2;
end if;

--获取产品的计费信息
if (v_popuitem = 2) then  --无推广期
    v_tempitem := 0;
elsif v_popuitem in (0,1) then  --推广期免费或者推广期计费
    if (v_sysdate8 >= v_popustart) and (v_sysdate8 <= v_popustop) then
        if v_popuitem = 0 then  --推广期免费
            v_needcharge := 0;
        else
            v_needcharge := 1;
            v_tempitem := v_popuitem;
        end if;
    else
        v_tempitem := 0;
    end if;
end if;

if (v_needcharge = 1) then
    --费率存在性鉴权
    if i_servicetype in (1,2) then
        v_sql := 'select chargetype,subchargetype,cyctype,cycunit,cycparam,rentfee,fixfee,fixusetimes,unitvol,autocontinue,minlimitfee,usecomfee,nextrentfee,rentfeevalidate,totaldiscount '||
                 'from zxdbm_200.s200_product_fee where  productindex = :p1 and itemindex = :p2';
        begin
            execute immediate v_sql into v_chargetype,v_subchargetype,v_cyctype,v_cycunit,v_cycparam,v_rentfee,v_fixfee,v_fixusetimes,v_unitvol,v_autocontinue,v_minlimitfee,v_usecomfee,v_nextrentfee,v_rentfeevalidate,v_totaldiscount using v_productindex, v_tempitem;
        exception when no_data_found then
            o_retcode   := 3103;
            o_debugpos := 21;
            o_debuginfo := 'productindex='||v_productindex||', itemindex='||v_tempitem||', the product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 22;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    else
        begin
            select chargetype,subchargetype,cyctype,cycunit,cycparam,
                   rentfee,fixfee,fixusetimes,unitvol,autocontinue,minlimitfee,usecomfee,totaldiscount
            into   v_chargetype,v_subchargetype,v_cyctype,v_cycunit,v_cycparam,
                   v_rentfee,v_fixfee,v_fixusetimes,v_unitvol,v_autocontinue,v_minlimitfee,v_usecomfee,v_totaldiscount
            from   zxdbm_200.s200_product_fee
            where  productindex = v_productindex and itemindex = v_tempitem;
        exception when no_data_found then
            o_retcode   := 3103;
            o_debugpos := 23;
            o_debuginfo := 'productindex='||v_productindex||', itemindex='||v_tempitem||', the product fee does not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 24;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
end if;

--是否业务退订
if i_servicetype in (1,2,3) then
    v_sql := 'select isservicesub,servicesort from zxdbm_200.s200_service where  serviceindex = :p1';
    begin
        execute immediate v_sql into v_isservicesub,v_servicesort using v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 25;
        o_debuginfo := 'serviceindex='||v_serviceindex||', the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 26;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

--added by youqunxia20130906 order3.02.01
--是否同步订购关系给OIDD,0-否,1-是
if v_servicesort = 1 then
    o_issynctooidd := 1;
end if;

if i_servicetype <> 14 then
    begin
        select orderupdateurl,subsrvtype into v_orderupdateurl,v_subsrvtype
        from   zxdbm_200.s200_service
        where  serviceindex = v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 27;
        o_debuginfo := 'serviceindex='||v_serviceindex||', the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 28;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

if i_servicetype = 101 then
    v_sql := 'select maincapability,submaincapability '||
             'from zxdbm_9900.s9900_service where serviceindex = :p1';
    begin
        execute immediate v_sql into v_maincapability,v_submaincapability using v_serviceindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 29;
        o_debuginfo   := 'serviceindex='||v_serviceindex||', the service does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 30;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

--包月类退订方式处理
if v_needcharge = 1 and v_chargetype in (2,5,7) then
    if i_cmdchannel in (1,2,36,14) then
        if i_servicetype in (1,2) or (i_servicetype = 3 and v_subsrvtype = 2) then
            v_invalidmode := 0;
        elsif i_servicetype = 3 and v_subsrvtype = 1 then
            v_invalidmode := 1;
        else
            null;
        end if;
    end if;
end if;

/*
*4.系统业务能力鉴权
*/

--系统业务能力鉴权
if i_servicetype = 101 then
    begin
        select srvtypeid
        into   v_srvtypeid
        from   zxdbm_ismp.ssys_service_type
        where  servicetype = v_maincapability and subcapability = v_submaincapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 31;
        o_debuginfo   := 'servicetype='||v_maincapability||', subcapability='||v_submaincapability||', the system service capbility does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 32;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
else
    begin
        select srvtypeid
        into   v_srvtypeid
        from   zxdbm_ismp.ssys_service_type
        where  servicetype = v_servicetype and subcapability = v_subcapability;
    exception when no_data_found then
        o_retcode   := 1100;
        o_debugpos := 33;
        o_debuginfo   := 'servicetype='||v_servicetype||', subcapability='||v_subcapability||', the system service capbility does not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 34;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;
o_srvtypeid   :=  v_srvtypeid;

/*
*5.CP/SP存在性及sp相关鉴权
*/

--CP/SP存在性鉴权
begin
    select cpid,cpsrvtype,status,cpsubscribeurl,cpunsubscribeurl,hasblacklist,cptype,insubblacklist,cpcnshortname,customersrvtel
    into   v_cpid,v_cpsrvtype,v_cpstatus,v_cpsubscribeurl,v_cpunsubscribeurl,v_hasblacklist,v_cptype,v_insubblacklist,v_cpcnshortname,v_customersrvtel
    from   zxdbm_ismp.scp_basic
    where  cpindex = v_cpindex;
exception when no_data_found then
    o_retcode   := 2000;
    o_debugpos := 35;
    o_debuginfo   := 'cpindex='||v_cpindex||', the cp/sp does not exist.';
    return;
when others then
    o_retcode   := 506;
    o_debugpos := 36;
    o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
    return;
end;

if (v_cpunsubscribeurl is null) or (v_cpunsubscribeurl = ' ') then
    v_cpunsubscribeurl := v_cpsubscribeurl;
end if;
o_cpid             := v_cpid;
o_cptype           := v_cptype;
o_cpsrvtype        := v_cpsrvtype;
o_cpcnshortname    := v_cpcnshortname;
o_customersrvtel   := v_customersrvtel;
if (trim(v_orderupdateurl) is null) or (length(trim(v_orderupdateurl)) < 8) then
    o_cpunsubscribeurl := v_cpunsubscribeurl;
else
    o_cpunsubscribeurl := v_orderupdateurl;
end if;

--SP开通业务能力鉴权(MVS,MPSS,PIM,CPX不需要鉴权)

if i_servicetype in (5,14,33,101) then
    null;

-- o_protocoltype := 0;

else
    begin
        select protocoltype
        into   v_protocoltype
        from   zxdbm_200.s200_sp_service_type
        where  cpindex = v_cpindex and servicetype = v_servicetype and subcapability = v_subcapability;
    exception when no_data_found then
       o_retcode   := 2100;
       o_debugpos := 37;
       o_debuginfo   := 'v_cpindex='||v_cpindex||', v_servicetype='||v_servicetype||', v_subcapability='||v_subcapability||', the opened service capability of the SP does not exist.';
       return;
    when others then
       o_retcode   := 506;
       o_debugpos := 38;
       o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
       return;
    end;

-- o_protocoltype := v_protocoltype;

end if;

--SP接入码查询
if (i_spaccesscode is null) or (length(trim(i_spaccesscode)) = 0) then
    begin
        select spaccesscode into v_spaccesscode
        from zxdbm_ismp.ssrv_srvorder
        where productindex = v_productindex and cmdtype = i_actiontype and servicetype = i_servicetype and rownum = 1;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 39;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if trim(v_spaccesscode) is null then
        begin
            select accesscode into v_spaccesscode
            from zxdbm_ismp.ssrv_spaccesscode
            where cpid = v_cpid and rownum = 1;
        exception when no_data_found then
            null;
        when others then
            o_retcode   := 506;
            o_debugpos := 40;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
    end if;
end if;


--EventID和ChanelPlayerID 状态查询
if i_actiontype = 2 then
    begin
        select status into o_chanelplayerstate from zxdbm_ismp.channel_basic where cpid = i_channelid;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 41;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    begin
        select status into o_eventstate from zxdbm_ismp.ssys_event where eventid = i_eventid;
    exception when no_data_found then
        null;
    when others then
        o_retcode   := 506;
        o_debugpos := 42;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

--伪码处理(支持伪码开关打开,非自营sp)--支持业务LCS,SMS,MVS,DLS,MMS,WAP,CPX
--退订时,AuthPriceReq消息中OA(DA)真实号码转化为伪码,同步SP
--这里订购业务的回滚订购关系不做处理

if i_servicetype in (1,2,3,4,41,5,6,101) then
    if i_actiontype <> 1 and i_catchflag <> 1 then
        if i_checkpseudocode = 1 and v_cpsrvtype <> 3 then
            begin
                select pseudocode into o_pseudocode from zxdbm_ismp.susr_basic where usercode = i_oa;
            exception when no_data_found then
                o_retcode   := 1001;
                o_debugpos := 43;
                o_debuginfo := 'i_oa = '||i_oa||', the user dose not exist.';
                return;
            when others then
                o_retcode   := 506;
                o_debugpos := 44;
                o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
                return;
            end;
            o_pcodemsisdnswitch := 1;
        end if;
    end if;
end if;

--如果集团专用产品属性为:必须集团管理员退定,则必须在集团门户上退定
if (v_onlyforcorp = 1) then
    begin
        select  corpid
        into    v_usecorpid
        from    zxdbm_ismp.susr_corp
        where   corpindex = v_usecorpindex;
    exception when no_data_found then
        o_retcode   := 1001301;  --集团不存在
        o_debugpos := 45;
        o_debuginfo := 'v_usecorpindex = '||v_usecorpindex||', the corp dose not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 46;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;

-- o_corpid := v_usecorpid;

    --本集团的产品只能由本集团的用户退订
    begin
        select unsubscribemode,payparty
        into   v_productcancelmode,v_productpayparty
        from   zxdbm_ismp.ssrv_corp_product
        where  corpindex = v_usecorpindex and groupindex = 0 and productindex = v_productindex;
    exception when no_data_found then
        o_retcode   := 2100;
        o_debugpos := 47;
        o_debuginfo := 'corpindex = '||v_usecorpindex||', productindex = '||v_productindex||', the product of the corp dose not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 48;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    if (v_productcancelmode = 0 ) then
        --必须集团管理员退订
        if (v_cmdchannel <> 4) then
            o_retcode   := 1001214;    --无权退订
            o_debugpos := 49;
            o_debuginfo := 'v_productcancelmode = '||v_productcancelmode||', v_cmdchannel = '||v_cmdchannel||', the operator has no privilege to cancel the subscription.';
            return;
        end if;
    end if;
else
    v_usecorpindex  := 0;
end if;

--产品订购关系存在性鉴权(是否业务退订)
if  v_isservicesub = 1 then
    begin
        select subscriptionindex,subscriptionid,servicetype,subcapability,
               serviceindex,serviceid,productindex,productid,productpkgindex,
               productpkgid,contentindex,contentid,corpindex,corpid,cpindex,
               cpid,subscribemode,subscribechannel,subscribeoptype,
               pausemode,resumemode,status,autocontinue,subscriber,usercode,
               payuser,pushid,starttime,endtime,lastrenttime,
               nextrentdate,payfordate,returnvalues,returntype,
               returnstartdate,returnenddate,fixusetimes,chargeparty,
               freetime,createtime,canceltime,firstusetime,lastusetime,
               tastestarttime,tasteendtime,pausetime,resumetime,
               subscribeopid,cancelreason,lastrent,msisdntype,pseudocode,pproductofferid,
               varifee,eventid,isexperience,subscribetype,chargetime
        into   v_productsubindex,v_productsubid,v_servicetype,v_subcapability,
               v_serviceindex,v_serviceid,v_productindex,v_productid,v_propkgindex,
               v_propkgid,v_contentindex,v_contentid,v_corpindex,v_corpid,v_cpindex,
               v_cpid,v_subscribemode,v_subscribechannel,v_subscribeoptype,
               v_pausemode,v_resumemode,v_productsubstatus,v_autocontinue,v_subscriber,v_usercode,
               v_payuser,v_pushid,v_substarttime,v_cursubendtime,v_lastrenttime,
               v_nextrentdate,v_payfordate,v_returnvalues,v_returntype,
               v_returnstartdate,v_returnenddate,v_fixusetimes,v_chargeparty,
               v_freetime,v_createtime,v_canceltime,v_firstusetime,v_lastusetime,
               v_tastestarttime,v_tasteendtime,v_pausetime,v_resumetime,
               v_subscribeopid,v_cancelreason,v_lastrent,v_msisdntype,v_pseudocode,v_pproductofferid,
               v_varifee,v_eventid,v_isexperience,v_subscribetype,v_chargetime
        from   zxdbm_200.s200_user_subscription
        where  usercode = v_usemsisdn and serviceid = v_serviceid;
    exception when no_data_found then
        o_retcode   := 1201;
        o_debugpos := 50;
        o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_serviceid = '||v_serviceid||', the subscription dose not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 51;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
else
    begin
        select subscriptionindex,subscriptionid,servicetype,subcapability,
               serviceindex,serviceid,productindex,productid,productpkgindex,
               productpkgid,contentindex,contentid,corpindex,corpid,cpindex,
               cpid,subscribemode,subscribechannel,subscribeoptype,
               pausemode,resumemode,status,autocontinue,subscriber,usercode,
               payuser,pushid,starttime,endtime,lastrenttime,
               nextrentdate,payfordate,returnvalues,returntype,
               returnstartdate,returnenddate,fixusetimes,chargeparty,
               freetime,createtime,canceltime,firstusetime,lastusetime,
               tastestarttime,tasteendtime,pausetime,resumetime,
               subscribeopid,cancelreason,lastrent,msisdntype,pseudocode,pproductofferid,
               varifee,eventid,isexperience,subscribetype,chargetime
        into   v_productsubindex,v_productsubid,v_servicetype,v_subcapability,
               v_serviceindex,v_serviceid,v_productindex,v_productid,v_propkgindex,
               v_propkgid,v_contentindex,v_contentid,v_corpindex,v_corpid,v_cpindex,
               v_cpid,v_subscribemode,v_subscribechannel,v_subscribeoptype,
               v_pausemode,v_resumemode,v_productsubstatus,v_autocontinue,v_subscriber,v_usercode,
               v_payuser,v_pushid,v_substarttime,v_cursubendtime,v_lastrenttime,
               v_nextrentdate,v_payfordate,v_returnvalues,v_returntype,
               v_returnstartdate,v_returnenddate,v_fixusetimes,v_chargeparty,
               v_freetime,v_createtime,v_canceltime,v_firstusetime,v_lastusetime,
               v_tastestarttime,v_tasteendtime,v_pausetime,v_resumetime,
               v_subscribeopid,v_cancelreason,v_lastrent,v_msisdntype,v_pseudocode,v_pproductofferid,
               v_varifee,v_eventid,v_isexperience,v_subscribetype,v_chargetime
        from   zxdbm_200.s200_user_subscription
        where  usercode = v_usemsisdn and productid = v_productid;
    exception when no_data_found then
        o_retcode   := 1201;
        o_debugpos := 52;
        o_debuginfo := 'v_usemsisdn = '||v_usemsisdn||', v_productid = '||v_productid||', the subscription dose not exist.';
        return;
    when others then
        o_retcode   := 506;
        o_debugpos := 53;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
end if;

if v_productsubstatus in (4,5) then
    o_retcode   := 1202;
    o_debugpos := 54;
    o_debuginfo := 'status of the subscription is abnormal.';
    return;
elsif (v_productsubstatus = 6) or (v_cursubendtime < v_sysdate14) then
    o_retcode   := 1201;
    o_debugpos := 55;
    o_debuginfo := 'The subscription does not exist.';
    return;
end if;

--added by youqunxia20130609   order2.07.10
--判断是否属于捆绑品
if trim(v_pproductofferid) is not null and i_cmdchannel <> 40 then
    if i_actiontype = 10 then
        null;
    else
        o_retcode   := 1001229;
        o_debugpos  := 58;
        o_debuginfo := 'The product is binding,unsubscribe illegal .';
        return;
    end if;
end if;

--如果流媒体(天翼视讯业务)产品在试用期内退订,需要立即生效;试用期外退订,则按产品属性判断
if (i_servicetype = 5) and v_freeuseflag in (1,2) and (v_freetime > v_sysdate14) then
    v_invalidmode := 0;
elsif (i_servicetype = 5) then
    v_invalidmode := 1; --MVS业务门户上不显示该字段,缺省退订都为下帐期生效
end if;
--added by youqunxia20130910 order3.02.01
--VSOP侧发起捆绑类套餐、增值产品的订购激活、变更激活及退订,ISMP中增值子产品订购关系立即生效(包括订购、退订及变更),不以增值产品属性判断生效时间;          
/*
if (i_cmdchannel = 40) and (i_effectiveswitch = 1)then
    v_invalidmode := 0;
end if;
*/

--沉默用户清理订购关系判断
if i_cmdchannel = 20 then
    if v_lastusetime = '00000000000000' then
        v_silentdays := to_date(v_sysdate14,'yyyymmddhh24miss') - to_date(v_substarttime,'yyyymmddhh24miss');
    else
        v_silentdays := to_date(v_sysdate14,'yyyymmddhh24miss') - to_date(v_lastusetime,'yyyymmddhh24miss');
    end if;

    if (v_silentdays < 30) then
        o_retcode   := 1001224;        --不满足沉默用户清理订购关系条件
        o_debugpos := 58;
        o_debuginfo := 'v_lastusetime = '||v_lastusetime||', it is not allowed to delete subscription of silent user.';
        return;
    end if;
end if;

--(除CRM发起的退订)套餐下的产品不允许单独退订
if  (v_propkgindex <> 0 and i_cmdchannel not in (9,35,39,40)) then
    o_retcode   := 1001214;
    o_debugpos := 59;
    o_debuginfo := 'the product in pkg is not allowed to be unsubscribed alone.';
    return;
end if;
o_paymsisdn  :=  v_payuser;
--根据i_needreplay决定鉴权是否继续
if (i_needreplay = 1) then
    begin
        v_tmpint  := dbms_random.value(1,9999999999);
        o_linknum := v_sysdate14 || '655380' || lpad(to_char(v_tmpint),10,'0');
    exception when others then
        o_retcode   := 506;
        o_debugpos := 60;
        o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
        return;
    end;
    return;
end if;
--获取计费方
if (v_productpayparty = 1) then
    v_paymsisdn     := v_usecorpid;
    o_paytype       := 0;
    o_paymsisdn     := v_paymsisdn;
    v_payuserindex  := v_corpindex;
    o_cityid        := trim(' ');
else
    --赠送时,订购关系只能由使用方退订,此时需要将付费方的信息带出存储过程
    if (v_payuser <> v_usemsisdn) then
        v_paymsisdn := v_payuser;
        o_paymsisdn := v_paymsisdn;
        begin
            select msisdntype,paytype,oppaytype,cityid
            into   v_paymsisdntype,v_paypaytype,v_payoppaytype,v_payusercityid
            from   zxdbm_ismp.susr_basic
            where  usercode = v_payuser;
        exception when no_data_found then
            o_retcode   := 1001;
            o_debugpos := 61;
            o_debuginfo := 'v_payuser = '||v_payuser||', user dose not exist.';
            return;
        when others then
            o_retcode   := 506;
            o_debugpos := 62;
            o_debuginfo := 'db error'||',sqlcode='||to_char(sqlcode)||'.';
            return;
        end;
        o_paytype := v_paypaytype;
        o_oppaytype := v_payoppaytype;
        o_paymsisdntype := v_paymsisdntype;
        o_cityid        := trim(v_payusercityid);
    end if;
end if;

--设置产品订购关系失效时间、产品订购关系状态
if i_servicetype = 5 then
    --对MVS业务的订购关系失效时间做特殊处理
    if v_chargetype in (3,6,8) and v_cyctype = 0 then --如果为非周期性
        v_subendtime := v_cursubendtime;
    elsif v_chargetype in (3,6,8) then
        --如果为周期性(MVS的包周期计费只存在包天方式)
        v_subendtime := to_char(to_date(v_nextrentdate,'yyyymmdd') - 1,'yyyymmdd') || '235959';
    elsif v_chargetype in (2,5,7) then
        if v_invalidmode = 0 then
            v_subendtime := v_sysdate14;
        elsif v_invalidmode = 1 then
            v_subendtime := v_curmonthendtime;
        end if;
    end if;
else
    if (v_invalidmode = 0) then
        v_subendtime := v_sysdate14;
    else
        v_subendtime := v_curmonthendtime;
    end if;
end if;
o_subendtime := v_subendtime;

--设置话单计费开始,结束时间

if substr(v_substarttime,1,6) = substr(v_sysdate8,1,6) then
    o_cdrbegintime := v_substarttime;
else
    if(v_subendtime < o_cdrendtime) then
        o_cdrendtime := v_subendtime;
    end if;
end if;

end slp_200_telicomprecancel;
/

clear buffer;
spool off;
exit;

你可能感兴趣的:(sql)