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,CREATE_DATE,
PLAN_ORGID, PLAN_NAME, CREATE_TYPE,change_time,Change_Userid)
VALUES (
p_new_plancode, 2,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||p_new_plancode||',组织机构ID:'||v_mi_org_id||',器具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_org_id||',器具id:'||v_mi_id;
end if;
END LOOP;
insert into a_meas_miinsp_log(CREATE_DATE,BODY,EXECUTE,LOGTYPE) values(sysdate,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;