--获取部门树
procedure:
create or replace procedure P_UTIL_TREE_ALL(P_APPL_NAME in VARCHAR2,
P_HIERARCHY_TYP in VARCHAR2,
TREETYPE in varchar2,
P_CUR OUT SYS_REFCURSOR) is
begin
OPEN P_CUR FOR
with v_util_tree as
(select h.EQ_NAME, h.PARENT_EQ_NAME, e.Typ, e.Dscr, h.seq
from IP_EQUIP_HIERARCHY h
inner join IP_EQ e
on h.Eq_Name = e.Eq_Name
where APPL_NAME = P_APPL_NAME
AND HIERARCHY_TYP = P_HIERARCHY_TYP
and e.typ <> 'TANK'
and e.typ <> 'LGTANK'
and e.typ <> 'POST'
union all
select 'NO_ASSIGNED', '$ASSET', null, '未分配', NULL as seq
from dual
union all
select EQ_NAME, '0', Typ, Dscr, 0 as seq
from IP_EQ
where EQ_NAME = '$ASSET')
SELECT DSCR as TEXT, EQ_NAME as ID, PARENT_EQ_NAME, TYP,SEQ
FROM v_util_tree A
START WITH A.PARENT_EQ_NAME = TREETYPE
CONNECT BY prior A.EQ_NAME = A.PARENT_EQ_NAME
order by SEQ;
end P_UTIL_TREE_ALL;
CREATE OR REPLACE PROCEDURE A_MEAS_MIINSP_PLAN_UPDATE
AS
vs_msg VARCHAR2(4000);
log_body VARCHAR2(400);
log_body2 VARCHAR2(400);
updatecount number;
plancodes varchar2(1000);
codeday VARCHAR2(40); --前几天
cur_1 SYS_REFCURSOR; --声明游标变量
v_plan_code VARCHAR2(400); --当前plancode
v_plan_name VARCHAR2(400); --当前planname
BEGIN
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'开始执行A_MEAS_MIINSP_PLAN_UPDATE存储过程','execute','A_MEAS_MIINSP_PLAN_UPDATE');
--定义游标(简单的说就是一个可以遍历的结果集)
select A_DICS.Code into codeday
from A_DICS
inner join a_Diccategory on A_DICS.Categoryid = a_Diccategory.Keyid
where A_DICS.Status = 1 and a_Diccategory.Code = 'MEAS_RecycleDay' and A_DICS.title ='检定';
if(codeday is null)
then
begin
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'数据字典中不存在配置单据回收天数','execute','A_MEAS_MIINSP_PLAN_UPDATE');
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'结束执行A_MEAS_MIINSP_PLAN_UPDATE存储过程','execute','A_MEAS_MIINSP_PLAN_UPDATE');
commit;
return;
end;
end if;
open cur_1 for
--CURSOR cur_1 IS
select plan_code,plan_name from a_meas_miinsp_plan
where
--create_date < to_date(to_char(sysdate - interval codeday day,'yyyy-mm-dd'),'yyyy-mm-dd')
to_date(to_char(create_date,'yyyy-mm-dd'),'yyyy-mm-dd') <= to_date(to_char(sysdate -codeday,'yyyy-mm-dd'),'yyyy-mm-dd')
and report_state in (1,2);
loop
FETCH cur_1 into v_plan_code,v_plan_name;
EXIT WHEN cur_1%NOTFOUND;
update a_meas_instru_ccount a set
a.mi_status=(
select b.old_mi_status from a_meas_miinsp_detail b where
b.plan_code=v_plan_code and
b.mi_id=a.mi_id )
,operatedate=sysdate,operateuser='sysadmin'
where exists (
select b.old_mi_status from a_meas_miinsp_detail b where
b.plan_code=v_plan_code and
b.mi_id=a.mi_id);
log_body :='修改'||v_plan_name||'检定计划下器具状态为原来状态.';
plancodes:=plancodes||v_plan_code||',';
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,log_body,'update','A_MEAS_MIINSP_PLAN_UPDATE+A_MEAS_INSTRU_CCOUNT');
END LOOP;
select count(*) into updatecount from a_meas_miinsp_plan
where
--create_date < to_date(to_char(sysdate - interval codeday day,'yyyy-mm-dd'),'yyyy-mm-dd')
to_date(to_char(create_date,'yyyy-mm-dd'),'yyyy-mm-dd') <= to_date(to_char(sysdate -codeday,'yyyy-mm-dd'),'yyyy-mm-dd')
and report_state in (1,2) ;
if(updatecount>0)
then
--更新检定计划表中的三天前的编制状态记录为 已回收 状态
--update a_meas_miinsp_plan set report_state=7 where create_date < to_date(to_char(sysdate - interval codeday day,'yyyy-mm-dd'),'yyyy-mm-dd');
update a_meas_miinsp_plan set report_state=7,change_time=sysdate,change_userid='sysadmin' where to_date(to_char(create_date,'yyyy-mm-dd'),'yyyy-mm-dd') <= to_date(to_char(sysdate -codeday,'yyyy-mm-dd'),'yyyy-mm-dd')
and report_state in (1,2) ;
--添加日志
log_body2 :='修改'||updatecount||'条编制状态的检定计划:'||plancodes;
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,log_body2,'update','A_MEAS_MIINSP_PLAN_UPDATE+A_MEAS_MIINSP_PLAN');
end if;
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'结束执行A_MEAS_MIINSP_PLAN_UPDATE存储过程','execute','A_MEAS_MIINSP_PLAN_UPDATE');
COMMIT;
EXCEPTION
WHEN OTHERS THEN
vs_msg := 'ERROR IN A_MEAS_MIINSP_PLAN_UPDATE '||SUBSTR(SQLERRM,1,500);
ROLLBACK;
--添加日志
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,vs_msg,'update','A_MEAS_MIINSP_PLAN_UPDATE');
COMMIT;
RETURN;
End;
CREATE OR REPLACE PROCEDURE A_MEAS_MIINSP_PLAN_CREATE
AS
vs_msg VARCHAR2(4000);
p_PERIODTYPE number; --周期类型
p_ISAUTOCRATEPLAN number; --是否自动创建
p_PERIODVALUE number; --周期值
p_LASTEXECURTDATE DATE; --上次执行日期
p_NEXTEXECURTDATE DATE; --下次执行日期
p_count number; --自动创建的器具数量
p_current_ym varchar2(50); --当前年月
p_maxnum number; --检定计划最新的编号
p_new_plancode number; --检定计划新编号
p_msg varchar2(1000); --日志信息
v_instrun SYS_REFCURSOR; --声明游标变量
v_group_org_id_cursor SYS_REFCURSOR;
v_group_org_id varchar2(100);
v_mi_id number;
v_MI_STATUS varchar2(100);
v_mi_org_id varchar2(100);
v_insdate DATE; --检定日期
p_updatemiids varchar2(1000);
p_insertplancode varchar2(1000);
config_count number; --配置表数量
current_dscr varchar2(200); --当前单位
new_plan_name varchar2(200); --当前新计划名称
begin
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'开始执行A_MEAS_MIINSP_PLAN_CREATE存储过程','execute','A_MEAS_MIINSP_PLAN_CREATE');
select count(*) into config_count from a_meas_miinsp_config;
--判断是否自动创建
if(config_count<1) then
begin
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'A_MEAS_MIINSP_CONFIG不存在记录','execute','A_MEAS_MIINSP_PLAN_CREATE');
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'结束执行A_MEAS_MIINSP_PLAN_CREATE存储过程','execute','A_MEAS_MIINSP_PLAN_CREATE');
commit;
return;
end;
end if;
--获取配置表中的数据
select PERIODTYPE,ISAUTOCRATEPLAN,PERIODVALUE,LASTEXECURTDATE into p_PERIODTYPE, p_ISAUTOCRATEPLAN,p_PERIODVALUE, p_LASTEXECURTDATE from a_meas_miinsp_config;
if( p_ISAUTOCRATEPLAN=0) then
Begin
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'A_MEAS_MIINSP_CONFIG表记录配置不需要自动创建记录','execute','A_MEAS_MIINSP_PLAN_CREATE');
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'结束执行A_MEAS_MIINSP_PLAN_CREATE存储过程','execute','A_MEAS_MIINSP_PLAN_CREATE');
commit;
return;
end;
end if;
if(p_PERIODTYPE=1)
then
begin
--获取下次执行日期
if(p_LASTEXECURTDATE is null)
then
--设置最后更新的日期为当前时间,和下次更新时间
p_LASTEXECURTDATE:=to_date(to_char(SYSDATE,'yyyy-mm-dd') ,'yyyy-mm-dd');
p_NEXTEXECURTDATE:=to_date(to_char(ADD_MONTHS(trunc(sysdate,'mm'), p_PERIODVALUE),'yyyy-mm-dd') ,'yyyy-mm-dd');
else
p_NEXTEXECURTDATE:=to_date( to_char(ADD_MONTHS(p_LASTEXECURTDATE, p_PERIODVALUE),'yyyy-MM-dd'),'yyyy-mm-dd');
--SELECT to_char(ADD_MONTHS(p_LASTEXECURTDATE, p_PERIODVALUE),'yyyy-MM-dd') INTO p_NEXTEXECURTDATE FROM DUAL;
end if;
end;
end if;
p_count:=0;
--判断当前时间是否大于最后执行的时间
if(p_LASTEXECURTDATE <=to_date(to_char(SYSDATE,'yyyy-mm-dd') ,'yyyy-mm-dd'))
then
begin
--定义游标
--declare v_instrun_1 cursor for select mi_id,MI_STATUS,mi_org_id from a_meas_instru_ccount where VERIFICTION_DATE BETWEEN p_LASTEXECURTDATE AND p_NEXTEXECURTDATE AND MI_STATUS IN ('1','4');
--定义游标
OPEN v_instrun FOR
select mi_id,MI_STATUS,mi_org_id,VERIFICTION_DATE from a_meas_instru_ccount where VERIFICTION_DATE BETWEEN p_LASTEXECURTDATE AND p_NEXTEXECURTDATE AND MI_STATUS IN ('1','4') AND VERIFICTION_PLANT=0;
--定义游标
--declare v_group_org_id_cursor_1 cursor for select mi_org_id from a_meas_instru_ccount where VERIFICTION_DATE BETWEEN p_LASTEXECURTDATE AND p_NEXTEXECURTDATE AND MI_STATUS IN ('1','4')
-- group by mi_org_id ;
open v_group_org_id_cursor FOR
select mi_org_id from a_meas_instru_ccount where VERIFICTION_DATE BETWEEN p_LASTEXECURTDATE AND p_NEXTEXECURTDATE AND MI_STATUS IN ('1','4') AND VERIFICTION_PLANT=0
group by mi_org_id ;
--获取总记录数
select count(*) into p_count from a_meas_instru_ccount where VERIFICTION_DATE BETWEEN p_LASTEXECURTDATE AND p_NEXTEXECURTDATE AND MI_STATUS IN ('1','4') AND VERIFICTION_PLANT=0;
end;
else
begin
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'本次检定周期时间大于当前时间','execute','A_MEAS_MIINSP_PLAN_CREATE');
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'结束执行A_MEAS_MIINSP_PLAN_CREATE存储过程','execute','A_MEAS_MIINSP_PLAN_CREATE');
commit;
return;
end;
end if;
if(p_count>0)
then
p_updatemiids:='更新器具台帐表的状态:';
p_insertplancode:='新增检定计划明细表:';
loop
FETCH v_group_org_id_cursor into v_group_org_id;
EXIT WHEN v_group_org_id_cursor%NOTFOUND;
p_current_ym := TO_CHAR(SYSDATE,'yyyymm');
select max(substr(plan_code,length(plan_code)-3,4))+1 into p_maxnum
from A_MEAS_MIINSP_PLAN
where plan_code like '%'||p_current_ym||'%';
if(p_maxnum is null)
then
p_new_plancode:= p_current_ym||'0001';
elsif (length(p_maxnum)=1)
then
p_new_plancode:= p_current_ym||'000'||p_maxnum;
elsif (length(p_maxnum)=2)
then
p_new_plancode:= p_current_ym||'00'||p_maxnum;
elsif (length(p_maxnum)=3)
then
p_new_plancode:= p_current_ym||'0'||p_maxnum;
elsif (length(p_maxnum)=4)
then
p_new_plancode:= p_current_ym||p_maxnum;
end if;
--新增检定计划表
SELECT dscr into current_dscr FROM V_MEAS_DEPT where EQ_NAME = v_group_org_id;
new_plan_name:='自动创建计划:'||current_dscr;
INSERT INTO A_MEAS_MIINSP_PLAN (
PLAN_CODE, REPORT_STATE,STYPE,CREATE_DATE,
PLAN_ORGID, PLAN_NAME, CREATE_TYPE,change_time,Change_Userid)
VALUES (
p_new_plancode, 2,1,sysdate,
v_group_org_id,new_plan_name,1 ,sysdate,'sysadmin');
p_msg:='新增检定计划表:'||p_new_plancode||',组织机构ID:'||v_group_org_id;
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,p_msg,'insert','A_MEAS_MIINSP_PLAN_CREATE+A_MEAS_MIINSP_PLAN');
p_insertplancode:='';
p_updatemiids:='';
open v_instrun for
select mi_id,MI_STATUS,mi_org_id,VERIFICTION_DATE from a_meas_instru_ccount where VERIFICTION_DATE BETWEEN p_LASTEXECURTDATE AND p_NEXTEXECURTDATE AND MI_STATUS IN ('1','4') AND VERIFICTION_PLANT=0;
loop
FETCH v_instrun into v_mi_id,v_MI_STATUS,v_mi_org_id,v_insdate;
EXIT WHEN v_instrun%NOTFOUND;
if(v_group_org_id=v_mi_org_id)
then
--新增检定计划明细表
INSERT INTO A_MEAS_MIINSP_DETAIL (
PLAN_CODE, MI_ID, CREATE_DATE,
OLD_MI_STATUS,INS_DATE
) VALUES (
p_new_plancode,v_mi_id,sysdate,
v_MI_STATUS,v_insdate
);
p_insertplancode:= p_insertplancode||',器具id:'||v_mi_id||',';
--更新器具台帐表的状态
UPDATE A_MEAS_INSTRU_CCOUNT SET MI_STATUS = 2,operatedate=sysdate,operateuser='sysadmin' WHERE MI_ID =v_mi_id;
p_updatemiids:=p_updatemiids||'器具id:'||v_mi_id||',';
end if;
END LOOP;
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'计划编号:'||p_new_plancode||','||p_insertplancode,'insert','A_MEAS_MIINSP_PLAN_CREATE+A_MEAS_MIINSP_DETAIL');
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,p_updatemiids,'update','A_MEAS_MIINSP_PLAN_CREATE+A_MEAS_INSTRU_CCOUNT');
END LOOP;
CLOSE v_instrun; --关闭游标变量
CLOSE v_group_org_id_cursor; --关闭游标变量
--更新配置表中最后的更新日期
update a_meas_miinsp_config set LASTEXECURTDATE=p_NEXTEXECURTDATE;
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'更新计量模块检定计划配置表的下次检定时间:'||p_NEXTEXECURTDATE,'execute','A_MEAS_MIINSP_PLAN_CREATE+A_MEAS_MIINSP_CONFIG');
end if;
--添加日志
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,'结束执行A_MEAS_MIINSP_PLAN_CREATE存储过程','execute','A_MEAS_MIINSP_PLAN_CREATE');
COMMIT;
EXCEPTION
WHEN OTHERS THEN
vs_msg := 'ERROR IN A_MEAS_MIINSP_PLAN_CREATE '||SUBSTR(SQLERRM,1,500);
ROLLBACK;
--添加日志
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,vs_msg,'execute','A_MEAS_MIINSP_PLAN_CREATE');
COMMIT;
RETURN;
end;
function:
create or replace function FN_GET_ROLES(v_user_id varchar2) return varchar2 is
type zy_emp_cursor is ref cursor;
v_emp_cursor zy_emp_cursor;
v_role_dscr varchar2(2000);
v_emp_dscr varchar2(64);
v_index number;
begin
open v_emp_cursor for SELECT A_ROLES.ROLE_DSCR
FROM A_PT_ROLES_USERS A_USERS
INNER JOIN A_PT_ROLES A_ROLES
ON A_USERS.ROLE_ID = A_ROLES.ROLE_ID
WHERE A_USERS.USER_ID = v_user_id;
v_index := 0;
loop
fetch v_emp_cursor into v_emp_dscr;
exit when v_emp_cursor%notfound;
if v_index = 0 then
begin
v_role_dscr := v_emp_dscr;
end;
else
v_role_dscr := v_role_dscr || ',' || v_emp_dscr;
end if;
v_index := v_index + 1;
end loop;
close v_emp_cursor;
return v_role_dscr;
end;
调用function:
--未分配此角色的用户
SELECT * FROM (
SELECT ROW_NUMBER() OVER (
order by U.USER_ID ASC
) AS TROW,U.*,T.DSCR, FN_GET_ROLES(U.USER_ID) AS ROLE_DSCR
FROM A_PT_USERS U
LEFT JOIN V_IP_EQ T
ON U.ORG=T.EQ_NAME
WHERE (org is null or org='')
AND NOT EXISTS (SELECT A_R_U.USER_ID
FROM A_PT_ROLES_USERS A_R_U
WHERE TRIM(UPPER(A_R_U.USER_ID)) = TRIM(UPPER(U.USER_ID))
AND A_R_U.ROLE_ID = 'sa')
) TT
WHERE TT.TROW BETWEEN {0} AND {1};