create or replace function func_get_user_by_msisdn(msisdn in number)
------------------------------------------------------------------------------
---功能描述:通过用户的电话号码,调用局方存储过程 获取用户的基本信息, ---
--- 以遍更新card_user_info本地数据表。 ---
---参数: msisdn in number 代表用户的电话号码(SIM卡) ---
---时间:2008-09-05 ---
---作者:zhouyq ---
---单位:厦门新科技软件股份有限公司 ---
------------------------------------------------------------------------------
return pkg_gps_audit.user_record
is
user_record_info pkg_gps_audit.user_record; --用户基本信息类型
v_success number := 1; --成功标志
begin
--首先获取正常用户的基本信息
begin
pkg_gps_audit.proc_get_userinfo(msisdn,user_record_info);
v_success := 1;
exception when others then
v_success := 0;
end;
--其次,如果正常用户获取不到,再查离线用户信息
if v_success < 1 then
begin
pkg_gps_audit.proc_get_cancel_userinfo(msisdn,user_record_info);
v_success := 1;
exception when others then
v_success := 0;
end;
end if;
if v_success > 0 then
return user_record_info;
else
raise no_data_found;
end if;
return user_record_info;
end func_get_user_by_msisdn;
create or replace function func_get_user_id(msisdn_bak in number)
------------------------------------------------------------------------------
---功能描述:通过用户的电话号码获取用户的ID,(有保证数据及时性) ---
---参数: msisdn in number 代表用户的电话号码(SIM卡) ---
---时间:2008-09-05 ---
---作者:zhouyq ---
---单位:厦门新科技软件股份有限公司 ---
------------------------------------------------------------------------------
return number
is
user_info pkg_gps_audit.user_record; --用户基本信息
card_info card_user_info%rowtype; --用户基本信息表结构
type base_cursor is ref cursor;
cr base_cursor;
user_id number(15); --用户ID;
begin
begin
--如果本地card_user_info表可以找到数据,先在本地查找。
open cr for select * from card_user_info where msisdn = msisdn_bak;
fetch cr into card_info;
if cr%found then
--返回用户ID
return user_id;
exception
when others then
raise no_data_found; --抛出异常
end;
end;
create or replace function func_is_first_day
------------------------------------------------------------------------------
---功能描述:判断今天是否是该月的第一天(1号) ---
---返回参数:1代表是,0代表不是 ---
---时间:2008-09-05 ---
---作者:zhouyq ---
---单位:厦门新科技软件股份有限公司 ---
------------------------------------------------------------------------------
return number
is
v_result number := 0;
v_day varchar2(2);
begin
select to_char(sysdate,‘dd‘) into v_day from dual;
if v_day = ‘01‘ then
v_result := 1;
else
v_result := 0;
end if;
return(v_result);
end func_is_first_day;
create or replace procedure proc_delete_day_fee_info
------------------------------------------------------------------------------
---功能描述: 只保留一个月得数据(8月的日账单10月份删除) ---
---时间:2008-09-08 ---
---作者:zhouyq ---
---单位:厦门新科技软件股份有限公司 ---
------------------------------------------------------------------------------
is
type base_cursor is ref cursor;
cf base_cursor;
v_fee_id number(18);
begin
begin
open cf for select a.fee_id from user_fee_info a ,day_fee_info b where a.fee_id = b.fee_id and to_char(b.start_time,‘yyyy-mm‘) = to_char(add_months(sysdate,0),‘yyyy-mm‘);
fetch cf into v_fee_id;
while cf%found loop
delete from user_fee_info where fee_id = v_fee_id;
delete from day_fee_info where fee_id = v_fee_id;
commit;
fetch cf into v_fee_id;
end loop;
close cf;
exception when others then
rollback;
return;
end;
end proc_delete_day_fee_info;
create or replace procedure proc_delete_month_fee_info
------------------------------------------------------------------------------
---功能描述: 只保留6个月得数据(8月的日账单1月份删除) ---
---时间:2008-09-08 ---
---作者:zhouyq ---
---单位:厦门新科技软件股份有限公司 ---
------------------------------------------------------------------------------
is
type base_cursor is ref cursor;
cf base_cursor;
v_fee_id number(18);
begin
begin
open cf for select a.fee_id from user_fee_info a ,month_fee_info b where a.fee_id = b.fee_id and to_char(b.start_time,‘yyyy-mm‘) = to_char(add_months(sysdate,0),‘yyyy-mm‘);
fetch cf into v_fee_id;
while cf%found loop
delete from user_fee_info where fee_id = v_fee_id;
delete from month_fee_info where fee_id = v_fee_id;
commit;
begin
begin
--打开group_sims游标
open group_sims_ref for select * from group_sims where (groupsim is null or length(ltrim(groupsim)) = 0);
fetch group_sims_ref into group_sims_info;
--开始遍历该游标
while group_sims_ref%found loop
--首先判断是否是虚拟号码,
if group_sims_info.simtype = 0 then
--首先通过虚拟号码,获取对应的SIM号码;
begin
pkg_gps_audit.proc_get_payed_msisdns(group_sims_info.sim,msisdn_table_temp,msisdn_len);
if msisdn_len > 0 then
for iLen in 1 .. msisdn_len loop
--首先更新原来group_sims表中同一个虚拟卡所对应的sim卡(先删后插)
delete from group_sims t where t.simtype = 1 and to_number(t.groupsim) = group_sims_info.sim and t.groupuserid = group_sims_info.groupuserid;
for xLen in 1..msisdn_len loop
begin
-----通过msisdn获取用户基本信息
user_record_info := func_get_user_by_msisdn(msisdn_table_temp(xLen));
exception when others then
dbms_output.put_line(‘通过msisdn获取用户基本信息出现了异常!‘);
v_success := 0;
end;
if v_success > 0 then
insert into group_sims(id,sim,groupsim,groupuserid,simtype,updatetime)values(
SEQ_GROUP_SIMS.Nextval,to_char(msisdn_table_temp(xLen)),group_sims_info.sim,group_sims_info.groupuserid,
1,sysdate);
--更新原来在card_user_info里面的数据(先删除已经存在的再插入新的数据)
delete from card_user_info where msisdn = msisdn_table_temp(xLen);
insert into card_user_info(user_id,msisdn,brand_name,deal_name,service_status,stop_time,status)
values(user_record_info.user_id,user_record_info.msisdn,user_record_info.brand_name,user_record_info.deal_name
,user_record_info.service_status,user_record_info.stop_time,1);
end if;
--提交数据
commit;
end loop;
end loop;
end if;
--没有与该虚拟卡对应的SIM号码
if msisdn_len <= 0 then
--首先更新原来group_sims表中同一个虚拟卡所对应的sim卡(先删后插)
delete from group_sims t where t.simtype = 1 and to_number(t.groupsim) = group_sims_info.sim and t.groupuserid = group_sims_info.groupuserid;
commit;
end if;
exception when others then
--回滚数据
rollback;
dbms_output.put_line(‘首先通过虚拟号码,获取对应的SIM号码出现了异常!‘);
end;
end if;
--如果是真实号码,并且没有附属虚拟号码,则直接更新card_user_info表
if group_sims_info.simtype > 0 and (group_sims_info.groupsim is null or length(group_sims_info.groupsim) = 0 ) then
begin
user_record_info := func_get_user_by_msisdn(group_sims_info.sim);
--更新原来在card_user_info里面的数据(先删除已经存在的再插入新的数据)
delete from card_user_info where msisdn = group_sims_info.sim ;
insert into card_user_info(user_id,msisdn,brand_name,deal_name,service_status,stop_time,status)
values(user_record_info.user_id,user_record_info.msisdn,user_record_info.brand_name,user_record_info.deal_name
,user_record_info.service_status,user_record_info.stop_time,1);
--提交数据
commit;
exception when others then
--回滚数据
rollback;
dbms_output.put_line(‘更新card_user_info表出现了异常‘);
end;
end if;
--遍历游标
fetch group_sims_ref into group_sims_info;
end loop;
begin
begin
--打开group_sims游标
open group_sims_ref for select * from group_sims where (groupsim is null or length(ltrim(groupsim)) = 0);
fetch group_sims_ref into group_sims_info;
--开始遍历该游标
while group_sims_ref%found loop
--首先判断是否是虚拟号码,
if group_sims_info.simtype = 0 then
--首先通过虚拟号码,获取对应的SIM号码;
begin
pkg_gps_audit.proc_get_payed_msisdns(group_sims_info.sim,msisdn_table_temp,msisdn_len);
if msisdn_len > 0 then --有数据
for iLen in 1 .. msisdn_len loop
begin
--通过用户ID,获取远程的换补卡用户信息集合
pkg_gps_audit.proc_get_simcard_his(func_get_user_id(msisdn_table_temp(iLen)),simcard_table_temp,simcard_len);
--更新原来在change_card里面的数据(先删除已经存在的再插入新的数据)
if simcard_len > 0 then
--这个步骤的删除条件 有待确认。??
delete from change_card where msisdn = group_sims_info.sim ;
for ilen in 1 .. simcard_len loop
insert into change_card(user_id,msisdn,accept_time,accept_memo,status) values(
simcard_table_temp(ilen).user_id,simcard_table_temp(ilen).msisdn,simcard_table_temp(ilen).accept_time,
simcard_table_temp(ilen).accept_memo,1);
end loop;
end if;
if simcard_len = 0 then
dbms_output.put_line(‘通过用户IfffffffffD,获取远程的换补卡用户信息集合,出现了异常!‘);
end if;
commit;
exception when others then
dbms_output.put_line(‘通过用户ID,获取远程的换补卡用户信息集合,出现了异常!‘);
rollback;
end;
end loop;
end if;
exception when others then
--回滚数据
rollback;
dbms_output.put_line(‘首先通过虚拟号码,获取对应的SIM号码出现了异常!‘);
end;
end if;
if group_sims_info.simtype > 0 and (group_sims_info.groupsim is null or length(group_sims_info.groupsim) = 0 ) then --如果是真实号码,并且没有附属虚拟号码,则直接更新change_card表
begin
--通过用户ID,获取远程的换补卡用户信息集合
pkg_gps_audit.proc_get_simcard_his(func_get_user_id(group_sims_info.sim),simcard_table_temp,simcard_len);
--更新原来在change_card里面的数据(先删除已经存在的再插入新的数据)
if simcard_len > 0 then
--这个步骤的删除条件 有待确认。??
delete from change_card where msisdn = group_sims_info.sim ;
for ilen in 1 .. simcard_len loop
insert into change_card(user_id,msisdn,accept_time,accept_memo,status) values(
simcard_table_temp(ilen).user_id,simcard_table_temp(ilen).msisdn,simcard_table_temp(ilen).accept_time,
simcard_table_temp(ilen).accept_memo,1);
end loop;
end if;
--提交数据
commit;
exception when others then
--回滚数据
rollback;
dbms_output.put_line(‘更新change_card表出现了异常‘);
end;
end if;
--遍历游标
fetch group_sims_ref into group_sims_info;
end loop;
--打开group_sims游标
open group_sims_ref for select * from group_sims where (groupsim is null or length(ltrim(groupsim)) = 0);
fetch group_sims_ref into group_sims_info;
--开始遍历该游标
while group_sims_ref%found loop
--首先判断是否是虚拟号码,
if group_sims_info.simtype = 0 then
--首先通过虚拟号码,获取对应的SIM号码;
begin
pkg_gps_audit.proc_get_payed_msisdns(group_sims_info.sim,msisdn_table_temp,msisdn_len);
if msisdn_len > 0 then --有数据
for iLen in 1 .. msisdn_len loop
--判断当前日期是否是每月1号
is_first_day := func_is_first_day();
if is_first_day = 1 then --1号
-----(begin)保存到user_fee_info,day_fee_info表--------------
begin
-----计算出上个月的总和
begin
select user_id,
sum(fee_base),sum(fee_pkg_month),sum(fee_keep),sum(fee_oth_month),sum(fee_vpn),
sum(fee_shift),sum(fee_local),sum(fee_long),sum(fee_inprov),sum(fee_inprov_long),
sum(fee_outprov),sum(fee_outprov_long),sum(fee_inter),sum(fee_inter_long),
sum(fee_cmnet),sum(fee_trust),sum(fee_ptp_sms),sum(fee_mms),sum(fee_magazine),
sum(fee_gprs),sum(fee_wap),sum(fee_data_month),
sum(fee_data),sum(fee_ring),sum(fee_display),sum(fee_ext),sum(fee_other),
sum(fee_all),sum(fee_for_others),sum(fee_by_others)
into user_fee_info_temp from user_fee_info
where user_id = base_fee.user_id
and fee_id in (select fee_id from day_fee_info where to_char(start_time,‘yyyy-mm‘) = to_char(add_months(sysdate,-1),‘yyyy-mm‘)) group by user_id ;
insert into day_fee_info(id,fee_id,start_time) values(seq_day_fee_info.nextval,user_fee_info_nextval,sysdate-1);
commit;
exception when others then
rollback;
dbms_output.put_line(‘保存到user_fee_info,day_fee_info表出现了异常‘);
end;
-------------(end)保存到user_fee_info,day_fee_info表-------------------------------------
-------------------(begin)保存到user_fee_info,month_fee_info表---------------------------------
begin
begin
----首先删除再更新原来的user_fee_info,month_fee_info
select a.fee_id into v_fee_id from month_fee_info a, user_fee_info b where a.fee_id = b.fee_id and b.user_id = base_fee.user_id and to_char(a.start_time,‘yyyy-mm‘) = to_char(add_months(sysdate,-1),‘yyyy-mm‘);
delete from user_fee_info x where x.fee_id = v_fee_id;
delete from month_fee_info y where y.fee_id = v_fee_id;
commit;
insert into month_fee_info(id,fee_id,start_time) values(seq_month_fee_info.nextval,user_fee_info_nextval,sysdate-1);
commit;
exception when others then
rollback;
dbms_output.put_line(‘保存到user_fee_info,month_fee_info表出现了异常‘);
end;
----------------(end)保存到user_fee_info,month_fee_info表--------------------------------------
else --不是本月第一天
-------------------(begin)保存到user_fee_info,day_fee_info表-------------------
begin
begin
--计算出本月的总和
select user_id,
sum(fee_base),sum(fee_pkg_month),sum(fee_keep),sum(fee_oth_month),sum(fee_vpn),
sum(fee_shift),sum(fee_local),sum(fee_long),sum(fee_inprov),sum(fee_inprov_long),
sum(fee_outprov),sum(fee_outprov_long),sum(fee_inter),sum(fee_inter_long),
sum(fee_cmnet),sum(fee_trust),sum(fee_ptp_sms),sum(fee_mms),sum(fee_magazine),
sum(fee_gprs),sum(fee_wap),sum(fee_data_month),
sum(fee_data),sum(fee_ring),sum(fee_display),sum(fee_ext),sum(fee_other),
sum(fee_all),sum(fee_for_others),sum(fee_by_others)
into user_fee_info_temp from user_fee_info
where user_id = base_fee.user_id
and fee_id in (select fee_id from day_fee_info where to_char(start_time,‘yyyy-mm‘) = to_char(sysdate,‘yyyy-mm‘)) group by user_id ;
insert into day_fee_info(id,fee_id,start_time) values(seq_day_fee_info.nextval,user_fee_info_nextval,sysdate-1);
commit;
exception when others then
rollback;
dbms_output.put_line(‘保存到user_fee_info,day_fee_info表出现了异常‘);
end;
------------------(end)保存到user_fee_info,day_fee_info表----------------------------------
----------------(begin)保存到user_fee_info,month_fee_info表----------------------
begin
begin
----首先删除再更新原来的user_fee_info,month_fee_info
select a.fee_id into v_fee_id from month_fee_info a, user_fee_info b where a.fee_id = b.fee_id and b.user_id = base_fee.user_id and to_char(a.start_time,‘yyyy-mm‘) = to_char(sysdate,‘yyyy-mm‘) ;
delete from user_fee_info x where x.fee_id = v_fee_id;
delete from month_fee_info y where y.fee_id = v_fee_id;
commit;
exception when others then
rollback;
end;
insert into month_fee_info(id,fee_id,start_time) values(seq_month_fee_info.nextval,user_fee_info_nextval,sysdate);
commit;
exception when others then
rollback;
dbms_output.put_line(‘保存到user_fee_info,month_fee_info表出现了异常‘);
end;
----------------(end)保存到user_fee_info,month_fee_info表----------------------
end if;
end loop;
end if;
exception when others then
--回滚数据
rollback;
dbms_output.put_line(‘首先通过虚拟号码,获取对应的SIM号码出现了异常!‘);
end;
end if;
--如果是真实号码,并且没有附属虚拟号码,则直接更新day_fee_info,month_fee_info表
if group_sims_info.simtype > 0 and (group_sims_info.groupsim is null or length(group_sims_info.groupsim) = 0 ) then
begin
--首先获取该用户每日消费记录,如果今天刚好是1号,那么获取到的数据刚好是上个月1-30号的总和
--如果不是1号,那么获取到的是本月前几天的总和
pkg_gps_audit.proc_get_userfee(func_get_user_id(group_sims_info.sim),base_fee);
--判断当前日期是否是每月1号
is_first_day := func_is_first_day();
if is_first_day = 1 then
----------------------(begin)-------------------
begin
begin
--计算出上个月的总和
select user_id,
sum(fee_base),sum(fee_pkg_month),sum(fee_keep),sum(fee_oth_month),sum(fee_vpn),
sum(fee_shift),sum(fee_local),sum(fee_long),sum(fee_inprov),sum(fee_inprov_long),
sum(fee_outprov),sum(fee_outprov_long),sum(fee_inter),sum(fee_inter_long),
sum(fee_cmnet),sum(fee_trust),sum(fee_ptp_sms),sum(fee_mms),sum(fee_magazine),
sum(fee_gprs),sum(fee_wap),sum(fee_data_month),
sum(fee_data),sum(fee_ring),sum(fee_display),sum(fee_ext),sum(fee_other),
sum(fee_all),sum(fee_for_others),sum(fee_by_others)
into user_fee_info_temp from user_fee_info
where user_id = func_get_user_id(group_sims_info.sim)
and fee_id in (select fee_id from day_fee_info where to_char(start_time,‘yyyy-mm‘) = to_char(add_months(sysdate,-1),‘yyyy-mm‘))
group by user_id ;
insert into day_fee_info(id,fee_id,start_time) values(seq_day_fee_info.nextval,user_fee_info_nextval,sysdate-1);
commit;
exception when others then
rollback;
dbms_output.put_line(‘保存到user_fee_info,day_fee_info表出现了异常‘);
end;
------------------(end)-------------------------------
-------------------(begin)-----------------------------------------
begin
--保存到user_fee_info,month_fee_info表
begin
----首先删除再更新原来的user_fee_info,month_fee_info
select a.fee_id into v_fee_id from month_fee_info a, user_fee_info b where a.fee_id = b.fee_id and b.user_id = base_fee.user_id and to_char(a.start_time,‘yyyy-mm‘) = to_char(add_months(sysdate,-1),‘yyyy-mm‘) ;
delete from user_fee_info x where x.fee_id = v_fee_id;
delete from month_fee_info y where y.fee_id = fee_id;
commit;
insert into month_fee_info(id,fee_id,start_time) values(seq_month_fee_info.nextval,user_fee_info_nextval,sysdate-1);
commit;
exception when others then
rollback;
dbms_output.put_line(‘保存到user_fee_info,month_fee_info表出现了异常‘);
end;
----------------------(end)-----------------------------------------------
else --不是本月第一天
------------------------(begin)-------------------------------------
begin
--保存到user_fee_info,day_fee_info表
begin
--计算出本月的总和
select user_id,
sum(fee_base),sum(fee_pkg_month),sum(fee_keep),sum(fee_oth_month),sum(fee_vpn),
sum(fee_shift),sum(fee_local),sum(fee_long),sum(fee_inprov),sum(fee_inprov_long),
sum(fee_outprov),sum(fee_outprov_long),sum(fee_inter),sum(fee_inter_long),
sum(fee_cmnet),sum(fee_trust),sum(fee_ptp_sms),sum(fee_mms),sum(fee_magazine),
sum(fee_gprs),sum(fee_wap),sum(fee_data_month),
sum(fee_data),sum(fee_ring),sum(fee_display),sum(fee_ext),sum(fee_other),
sum(fee_all),sum(fee_for_others),sum(fee_by_others)
into user_fee_info_temp from user_fee_info
where user_id = func_get_user_id(group_sims_info.sim)
and fee_id in (select fee_id from day_fee_info where to_char(start_time,‘yyyy-mm‘) = to_char(sysdate,‘yyyy-mm‘))
group by user_id ;
exception when others then
user_fee_info_temp.user_id := base_fee.user_id;