create or replace function product_analyze_day_stat(p_stat_day timestamp) returns void as $$ /** *货品分析日报表 */ declare v_db_info varchar(1000) := '172.17.209.165;bi_edw;u_retail_gms'; v_function_name varchar(100) := 'product_analyze_day_stat'; v_step integer := 1; v_start_time timestamp; v_end_time timestamp; v_curr_price record; begin --1.初始化工作 begin if p_stat_day is null then v_start_time := date_trunc('day',now()); v_end_time := v_start_time + interval '1 day'; else v_start_time := date_trunc('day',p_stat_day); v_end_time := v_start_time + interval '1 day'; end if; --删除重复统计的数据 delete from item_analysis_report iar where iar.stat_day = v_start_time; perform write_log(v_db_info,v_function_name,1,'初始化工作'); exception when others then perform write_log(v_db_info,v_function_name,1,concat('初始化工作异常:',sqlerrm)); end; --2.基础数据规总(订货单位、商品编码、大类、季节、年份、风格) begin insert into item_analysis_report(stat_day,order_unit_no,item_no,item_code,item_name,category_no,sell_season,years,style) select v_start_time,ou.order_unit_no,rmt.item_no,rmt.code,rmt.name,rmt.category_no,rmt.sell_season,rmt.years,rmt.style from retail_mdm_order_unit ou,retail_mdm_order_brand_store_rel obsr,retail_mdm_item rmt where ou.order_unit_no = obsr.order_unit_no and obsr.brand_no = rmt.brand_no; perform write_log(v_db_info,v_function_name,2,'基础数据规总'); exception when others then perform write_log(v_db_info,v_function_name,2,concat('基础数据规总异常:',sqlerrm)); end; --3.牌价计算 begin update item_analysis_report iar set iar.tag_price = ( select rmpql.tag_price from retail_mps_price_quotation_list rmpql where rmpql.organ_id = iar.order_unit_no and rmpql.brand_no = iar.brand_no ); perform write_log(v_db_info,v_function_name,3,'牌价计算'); exception when others then perform write_log(v_db_info,v_function_name,3,concat('牌价计算异常:',sqlerrm)); end; --4.现价计算,折扣 begin for v_curr_price in( select iar.id,rppcl.price from retail_pms_price_change_list rppcl, item_analysis_report iar where rppcl.organ_id = iar.order_unit_no and rppcl.brand_no = iar.brand_no ) loop update item_analysis_report iar set iar.tag_price = coalesce(v_curr_price.price,iar.tag_price), iar.disc_rate = round(coalesce(v_curr_price.price,iar.tag_price)/coalesce(iar.tag_price,1),4) where iar.id = v_curr_price.id; end loop; perform write_log(v_db_info,v_function_name,4,'现价计算,折扣'); exception when others then perform write_log(v_db_info,v_function_name,4,concat('现价计算,折扣异常:',sqlerrm)); end; --5.上柜日 begin perform write_log(v_db_info,v_function_name,5,'上柜日'); exception when others then perform write_log(v_db_info,v_function_name,5,concat('上柜日异常:',sqlerrm)); end; --6.订货量、补货量 begin perform write_log(v_db_info,v_function_name,6,'订货量、补货量'); exception when others then perform write_log(v_db_info,v_function_name,6,concat('订货量、补货量异常:',sqlerrm)); end; --7.进货量 begin perform write_log(v_db_info,v_function_name,7,'进货量'); exception when others then perform write_log(v_db_info,v_function_name,7,concat('进货量异常:',sqlerrm)); end; --8.转入量、入在途、转出量、退残量 begin perform write_log(v_db_info,v_function_name,8,'转入量、转出量、退残量'); exception when others then perform write_log(v_db_info,v_function_name,8,concat('转入量、转出量、退残量异常:',sqlerrm)); end; --9.退残量 begin perform write_log(v_db_info,v_function_name,9,'退残量'); exception when others then perform write_log(v_db_info,v_function_name,9,concat('退残量异常:',sqlerrm)); end; --10.销售量 begin perform write_log(v_db_info,v_function_name,10,'销售量'); exception when others then perform write_log(v_db_info,v_function_name,10,concat('销售量异常:',sqlerrm)); end; --11.周销售 begin perform write_log(v_db_info,v_function_name,11,'周销售'); exception when others then perform write_log(v_db_info,v_function_name,11,concat('周销售异常:',sqlerrm)); end; --12.财务存 begin perform write_log(v_db_info,v_function_name,12,'财务存'); exception when others then perform write_log(v_db_info,v_function_name,12,concat('财务存异常:',sqlerrm)); end; --13.周销售 begin perform write_log(v_db_info,v_function_name,13,'周销售'); exception when others then perform write_log(v_db_info,v_function_name,13,concat('周销售异常:',sqlerrm)); end; --14.第一次折扣日、第二次折扣日、最后一次折扣日、一次折价、二次折价、最后一次折价 begin perform write_log(v_db_info,v_function_name,14,'第一次折扣日、第二次折扣日、最后一次折扣日、一次折价、二次折价、最后一次折价'); exception when others then perform write_log(v_db_info,v_function_name,14,concat('第一次折扣日、第二次折扣日、最后一次折扣日、一次折价、二次折价、最后一次折价异常:',sqlerrm)); end; exception when others then perform write_log(v_db_info,v_function_name,99,sqlerrm); end; $$ language plpgsql; create or replace function write_log(p_db_info varchar,p_function_name varchar,step_num integer,step_desc varchar) returns void as $$ /** *过程运行日志+异常日志 */ declare begin insert into t_runtime_step_log(db_info,function_name,run_step,step_desc) values(p_db_info,p_function_name,step_num,step_desc); end; $$ language plpgsql;