mysql存儲過程-積分計算

create procedure scoreRule()
begin
declare v_count int default 0 ;
declare has_row int default 0;
declare v_agency_id int default 0;
declare v_static_time varchar(20);
declare v_oper_type int default 1;
declare v_money float default 0;
declare v_rate float ;
declare v_score_rule int;
declare v_create_time datetime ;
declare v_score int default 0;
declare v_total_score int default 0;
declare v_old_score int default 0;
declare vc_agency_id int default 0;
declare vc_create_time varchar(10);
declare vc_consume_type int default 0;
declare vc_area_id int default 0;
declare cur_agency_trans cursor for select min(f.agency_id) as agencyID
from t_ebiz_fund_transaction r ,t_ebiz_fund f where r.fund_inst_id = f.fund_inst_id and f.fund_type =2 and r.oper_type in ('1','2','7' ) and
DATE_FORMAT(r.create_Time,'%Y%m%d') =DATE_FORMAT(STR_TO_DATE(vc_create_time,'%Y%m%d'),'%Y%m%d') group by f.agency_id order by f.agency_id desc;
declare continue handler for not found set has_row = 1;
set v_create_time = now() ;
set vc_create_time =DATE_FORMAT(date_add(now(),interval -1 day),'%Y%m%d');
-- set vc_create_time='20120320';
/*遍歷代理商錢包流水錶的代理商錢包數據,往表t_ebiz_score_info中插入數據*/
open cur_agency_trans ;
agency_rec:loop
fetch cur_agency_trans into vc_agency_id ;
/* 如果沒有找到記錄,就跳出循環*/
if has_row =1 then
leave agency_rec;
end if;
/*計算下一個代理商,重置總積分和網點最近日期的積分值*/
set v_total_score = 0;
set v_old_score =0;
/*根據代理商iID,獲取代理商的地市ID,增加地市ID選取積分規則*/
select t.area_id into vc_area_id from t_ebiz_agency_info t where t.agency_id = vc_agency_id ;
begin
declare has_detail_row int default 0;
declare cur_detail_trans cursor for select min(r.oper_type) as operType,-1*sum(r.amount) as money
from t_ebiz_fund_transaction r ,t_ebiz_fund f where r.fund_inst_id = f.fund_inst_id and f.fund_type =2 and r.oper_type in ('1','2','7' ) and f.agency_id = vc_agency_id and DATE_FORMAT(r.create_Time,'%Y%m%d') = vc_create_time group by
DATE_FORMAT(r.create_Time,'%Y%m%d'),f.agency_id ,r.oper_type;
declare continue handler for not found set has_detail_row =1;
/* 通過指定的代理商和日期,遍歷代理商錢包流水記錄表,往獲取積分明細表插入數據*/
open cur_detail_trans;
detail_rec:loop
fetch cur_detail_trans into v_oper_type,v_money;
/* 如果沒有找到記錄,就跳出循環*/
if has_detail_row =1 then
leave detail_rec;
end if;
set v_count = v_count + 1;
/*根據錢包的類型,獲取積分計算規則的類型*/
if v_oper_type =1 || v_oper_type =7 then
set vc_consume_type = 1 ;
end if;
if v_oper_type =2 then
set vc_consume_type =2;
end if;
/*獲取積分比率和積分規則ID,地市id根據網點的地市id爲先,要是網點對應的地市沒有配置規則,就用默認的地市規則area_id =1 */
select r.rate,r.score_rule_seq into v_rate,v_score_rule from t_ebiz_score_rule r where r.consume_type = vc_consume_type and( abs(v_money)/100 between r.section_start and r.section_end) and r.area_id in( vc_area_id,'1') order by r.area_id desc limit 1;
set v_score = v_money * v_rate / 100;
insert into t_ebiz_get_score_detail (score_rule_seq,agency_id,create_time,create_staff,consume,score_amount)VALUES (v_score_rule,vc_agency_id,v_create_time,"system",v_money,v_score);
set v_total_score = v_total_score + v_score;
commit;
end loop detail_rec;
close cur_detail_trans;
end;
/*往表代理商積分信息表中插入記錄 其中網點的積分值=網點最近日期的積分值 + 當天獲取的積分值*/
select t.current_score into v_old_score from t_ebiz_score_info t where t.agency_id = vc_agency_id order by t.static_time desc limit 1;
insert into t_ebiz_score_info(agency_id,static_time,total_score,current_score) values(vc_agency_id,v_create_time,v_total_score,v_total_score+v_old_score);
commit;
end loop agency_rec;
close cur_agency_trans;
end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章