存储过程2

create or replace procedure bps_special_marketing(TABYEAR in number, SYSDEPTCODE in char, DEPTCODE in char) is
v_item_code number(20) default 1;
v_id number(20) default 10; --DXP_29表中的id列
v_sort_no number(6) default 10;--DXP_29表总的sort_no列,用来排序显示

v_item_name varchar2(200);--第一列,指标名称
v_project_id varchar2(255);--项目id
v_project_name varchar2(255);--项目名称
v_category_name varchar2(255);--项目类别
v_impl_dept varchar2(255);--实施单位
v_build_volumn varchar2(255);--建设规模
--工程立项状态(0:项建申请,1:项建审批,2:可研申请,3:可研驳回,4:可研审批,5:初设申请,6:初设驳回,7:初设审批)
v_init_status number(10);--数字型立项状态
v_init_status_show varchar2(20); --显示时的立项状态,
--下达状态(0:未下达;1:已下达)
v_issue_status number(10);--数字型下达状态
v_issue_status_show varchar2(10);--显示时的立项状态,
v_source_name varchar2(255);--资金来源
v_start_date date;
v_finish_date date;
v_total_invest varchar2(255);--总投资
v_continued number(10);--续建状态,对应project表中的INTEGRATIVE_PLAN_ISSUED_STATUS字段,已下达(1)代表续建,未下达(0)代表新建
v_invest_amount number(19,2) default 0;--已下达投资
v_exec_amount number(19,2) default 0;--已下达用款

--查询电网基建的二级分类
v_second_id varchar2(255);--二级分类id
v_second_name varchar2(255);--二级分类名称
cursor v_second_category_record is select a.id, a.category_name
							from bps_project_category a
						where a.category_level=2
							and a.father_code_id=(select distinct id
														from bps_project_category
													where category_name='营销专项' and category_level=1);
v_dept_record report_package.bps_cursor;--自定义游标,用于查询保存哪些单位有当前分类的信息
v_dept_id varchar2(255);
v_dept_name varchar2(255);

v_project_record report_package.bps_cursor;--自定义游标,用于查询保存某单位下某个分类的项目信息

--用于单位小计的变量
v_dept_total_invest varchar2(255);--单位小计总投资


begin

delete from DXP_29 where tab_year=TABYEAR and dept_code = DEPTCODE and sys_dept_code=SYSDEPTCODE;
dbms_output.put_line(sql%rowcount);
dbms_output.put_line(TABYEAR||','||DEPTCODE||','||SYSDEPTCODE);
commit;

--插入总计一行
insert into DXP_29(item_name, id, dept_code, sys_dept_code, tab_year, sort_no)
	values('总计', v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
for i in 1..2 loop
insert into DXP_29(id, dept_code, sys_dept_code, tab_year, sort_no)
		values(v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
end loop;


open v_second_category_record;
loop
fetch v_second_category_record into v_second_id, v_second_name;
exit when v_second_category_record%notfound;
--依次插入二级分类
insert into DXP_29(item_name, id, dept_code, sys_dept_code, tab_year, sort_no)
	values(v_second_name, v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
end loop;
close v_second_category_record;

--再次打开此二级分类游标
open v_second_category_record;
loop
fetch v_second_category_record into v_second_id, v_second_name;--二级分类
exit when v_second_category_record%notfound;
--此后每行二级分类前插入一个伪空行
insert into DXP_29(id, dept_code, sys_dept_code, tab_year, sort_no)
		values(v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
--依次插入二级分类,每插入一次二级分类,就插入该二级分类下的每个单位和该单位下的项目
insert into DXP_29(item_code, item_name, id, dept_code, sys_dept_code, tab_year, sort_no)
	values(v_item_code, v_second_name, v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_item_code:=v_item_code+1;
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;

--根据该二级分类,查询当年有该二级分类项目计划的单位,即哪些单位下有该二级分类的项目计划,有项目,但是没计划的也不取,而且是激活状态的计划
--1,查询该二级分类的子级分类,如果没有子级分类,就直接查该分类的单位
open v_dept_record for select distinct a.bps_department_id,
								c.department_name
						from bps_project a,
							bps_project_planning b,
							bps_department c
					where a.bps_department_id is not null
						and a.bps_department_id = c.id
						and b.project_id = a.id
						and b.version_active_status = 1
						and b.fiscal_year = TABYEAR
						and (a.bps_category_id=v_second_id
								or a.bps_category_id in (select b.id
															from bps_project_category b
														where b.father_code_id=v_second_id));
loop
fetch v_dept_record into v_dept_id, v_dept_name;
exit when v_dept_record%notfound;
--插入该二级分类下的单位,插入该单位所属的二级分类的id
--此后每行二级分类前插入一个伪空行
insert into DXP_29(id, dept_code, sys_dept_code, tab_year, sort_no)
			values(v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
insert into DXP_29(item_name, id, dept_code, sys_dept_code, tab_year, sort_no, second_id)
		values(v_dept_name, v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no, v_second_id);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;

--插入该单位下的属于该二级分类的项目信息
open v_project_record for select distinct b.id,
								b.project_name,
						(select department_name from bps_department where id=b.bps_impl_dept_id) bps_impl_dept_name,
						b.total_volumn,
						b.project_initialize_status,
						c.issue_status,
						(select source_name from bps_investment_source, bps_prj_inves_relations
							where bps_prj_inves_relations.bps_project_id=b.id
								and bps_investment_source.id = bps_prj_inves_relations.bps_investment_source_id) source_name,
						b.start_date,
						b.finish_date,
						b.total_investment,
						b.integrative_plan_issued_status,
						c.adjust_planning_amount,
								c.adjust_execution_amount
							from bps_department       a,
						bps_project          b,
						bps_project_planning c
					 where b.bps_department_id = v_dept_id	--当前单位
						and b.bps_department_id = a.id
						and c.project_id = b.id
						and c.version_active_status = 1
						and c.fiscal_year = TABYEAR
						and (b.bps_category_id = v_second_id
								or b.bps_category_id in(select e.id from bps_project_category e where e.father_code_id=v_second_id)
							);
loop
fetch v_project_record into v_project_id, v_project_name, v_impl_dept
				, v_build_volumn, v_init_status, v_issue_status, v_source_name, v_start_date
				, v_finish_date, v_total_invest, v_continued, v_invest_amount, v_exec_amount;
exit when v_project_record%notfound;
--判断当前取出来的项目是否续建,如果是续建,根据次项目id就把之前年份的有关投资额和用款额也计算出来

--工程立项状态(0:项建申请,1:项建审批,2:可研申请,3:可研驳回,4:可研审批,5:初设申请,6:初设驳回,7:初设审批)
if v_init_status=0 then
	v_init_status_show:='项建申请';
elsif v_init_status=1 then
	v_init_status_show:='项建审批';
elsif v_init_status=2 then
	v_init_status_show:='可研申请';
elsif v_init_status=3 then
	v_init_status_show:='可研驳回';
elsif v_init_status=4 then
	v_init_status_show:='可研审批';
elsif v_init_status=5 then
	v_init_status_show:='初设申请';
elsif v_init_status=6 then
	v_init_status_show:='初设驳回';
elsif v_init_status=7 then
	v_init_status_show:='初设审批';
else
	v_init_status_show:='';
end if;
--下达状态(0:未下达;1:已下达)
if v_issue_status=0 then
	v_issue_status_show:='未下达';
elsif v_issue_status=1 then
	v_issue_status_show:='已下达';
else
	v_issue_status_show:='';
end if;

insert into DXP_29(id, dept_code, sys_dept_code, tab_year, sort_no
				, project_name, category_name, impl_dept, build_volumn, init_status, issue_status
				, source_name, start_date, finish_date, total_invest, invest_amount, exec_amount, second_id, dept_id)
			values(v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no
				, v_project_name, v_second_name, v_impl_dept, v_build_volumn, v_init_status_show, v_issue_status_show
				, v_source_name, v_start_date, v_finish_date, v_total_invest, v_invest_amount, v_exec_amount, v_second_id, v_dept_id);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
end loop;
close v_project_record;

--单位小计
update DXP_29 set total_invest=(select sum(to_number(total_invest)) from DXP_29 where dept_id=v_dept_id and second_id=v_second_id
													and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
			,invest_amount=(select sum(invest_amount) from DXP_29 where dept_id=v_dept_id and second_id=v_second_id
													and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
			,exec_amount=(select sum(exec_amount) from DXP_29 where dept_id=v_dept_id and second_id=v_second_id
													and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
		where item_name=v_dept_name and second_id=v_second_id
				and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE;
end loop;
close v_dept_record;
--二级分类小计
update DXP_29 set total_invest=(select sum(to_number(total_invest)) from DXP_29 where second_id=v_second_id and dept_id is null
												and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
		,invest_amount=(select sum(invest_amount) from DXP_29 where second_id=v_second_id and dept_id is null
												and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
		,exec_amount=(select sum(exec_amount) from DXP_29 where second_id=v_second_id and dept_id is null
												and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
	where item_name=v_second_name and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE;

end loop;
close v_second_category_record;

--总计
update DXP_29 set total_invest=(select sum(to_number(total_invest)) from DXP_29 where item_code is not null
											and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
	, invest_amount=(select sum(invest_amount) from DXP_29 where item_code is not null
											and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
	, exec_amount=(select sum(exec_amount) from DXP_29 where item_code is not null
											and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
where item_name='总计' and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE;

commit;
end;
/



你可能感兴趣的:(存储过程)