寫過的合理驗證oracle存儲過程PROCEDUREprocedure

CREATE OR REPLACE PROCEDURE gwbs_check(vpcbh in t_batch_gwbs_check.pcbh%type) IS

  CURSOR c_tocheck IS
    SELECT * FROM t_batch_gwbs_check c where c.pcbh=vpcbh;--獲取待校驗的數據
   errorStr varchar2(500);--來拼接每條數據的錯誤提示
   gw_rows number;
   police_rows number;
   cxfs_rows number;
   cph_rows number;
   qwms_rows number;
   police_deploy_rows number;
   police_leave_rows number;
   todeploy_timeerror_rows number;

   real_qssjd t_batch_gwbs_check.qssj_d%type;
   real_jssjd t_batch_gwbs_check.jssj_d%type;
   
   qssjd t_gwxx_gwsd.qssj%type;
   jssjd t_gwxx_gwsd.jssj%type;
   
   gwsd_rows number;

BEGIN
  DBMS_OUTPUT.PUT_LINE('循環開始時間:'||to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'));
  --待校驗遊標循環
  FOR c_tocheck_r IN c_tocheck LOOP
    errorStr := '';
    /*根據開始結束時間和崗位時段情況來賦值真正的開始時間和結束時間*/
    if c_tocheck_r.qssj_d is not null and c_tocheck_r.jssj_d is not null and c_tocheck_r.gwsdid is not null
      then 
        errorStr := concat(errorStr,'起始結束時間和崗位時段必須二選一;');
        goto updateTable;
    elsif c_tocheck_r.qssj_d is null and c_tocheck_r.jssj_d is null and c_tocheck_r.gwsdid is null
      then errorStr := concat(errorStr,'起始結束時間和崗位時段必須二選一;');
        goto updateTable;
    elsif (c_tocheck_r.qssj_d is null and c_tocheck_r.jssj_d is not null) or (c_tocheck_r.qssj_d is not null and c_tocheck_r.jssj_d is null)
      then errorStr := concat(errorStr,'起始時間和結束時間不能只能選一個;');
        goto updateTable;
    elsif c_tocheck_r.qssj_d is not null and c_tocheck_r.jssj_d is not null and c_tocheck_r.gwsdid is null
      then 
        real_qssjd := c_tocheck_r.qssj_d;
        real_jssjd := c_tocheck_r.jssj_d;
     elsif c_tocheck_r.qssj_d is null and c_tocheck_r.jssj_d is null and c_tocheck_r.gwsdid is not null
       then
         select count(*) into gwsd_rows from t_gwxx_gwsd where id = c_tocheck_r.gwsdid;
         if gwsd_rows <= 0
           then
             errorStr := concat(errorStr,'崗位時段編號:'||c_tocheck_r.gwsdid||'不存在;');
         else
           select qssj into qssjd from t_gwxx_gwsd where id = c_tocheck_r.gwsdid;
           select jssj into jssjd from t_gwxx_gwsd where id = c_tocheck_r.gwsdid;
           real_qssjd := to_date(to_char(c_tocheck_r.sgrq,'yyyy-mm-dd')||' '||substr(qssjd,3),'yyyy-mm-dd hh24:mi:ss');
           if instr(jssjd,'次日') > 0
             then
               real_jssjd := to_date(to_char(c_tocheck_r.sgrq,'yyyy-mm-dd')||' '||substr(jssjd,3),'yyyy-mm-dd hh24:mi:ss')+1;
           else
             real_jssjd := to_date(to_char(c_tocheck_r.sgrq,'yyyy-mm-dd')||' '||substr(jssjd,3),'yyyy-mm-dd hh24:mi:ss');
           end if;
         end if;
    end if;
     
--校驗崗位編號
    if c_tocheck_r.gwbh is null
      then
         errorStr := concat(errorStr,'崗位編號不能爲空;');
     else
      SELECT count(*) into gw_rows FROM znjtpost.t_src_gwxx g where g.id=c_tocheck_r.gwbh;
      if gw_rows <= 0
      then
         errorStr := concat(errorStr,'崗位編號: '||c_tocheck_r.gwbh||' 不存在;');
      end if;
    end if;
--校驗警員號
    if c_tocheck_r.jyh is null
      then
        errorStr := concat(errorStr,'警員號不能爲空;');
    else
      SELECT count(*) into police_rows FROM znjtresource.t_src_police p where p.policeno=c_tocheck_r.jyh;
      if police_rows <= 0
      then
        errorStr := concat(errorStr,'警員號: '||c_tocheck_r.jyh||' 不合法;');
      end if;
    end if;
--校驗上崗日期
    if c_tocheck_r.sgrq is null
      then
         errorStr := concat(errorStr,'上崗日期不能爲空;');
     else
       if trunc(sysdate) - c_tocheck_r.sgrq > 0
       then
         errorStr := concat(errorStr,'上崗日期:'||to_char(c_tocheck_r.sgrq,'yyyy-mm-dd')||'不能選今天之前的日期;');
       end if;
    end if;
--校驗出行方式
    if c_tocheck_r.cxfs is null
      then
         errorStr := concat(errorStr,'出行方式不能爲空;');
    else
     select count(*) into cxfs_rows from znjtcode.d_post_cxfs c where c.code = c_tocheck_r.cxfs;
      if cxfs_rows <= 0
      then
        errorStr := concat(errorStr,'出行方式: '||c_tocheck_r.cxfs||' 不合法;');
      end if;
    end if;
  if c_tocheck_r.cxfs='5'
    then
      --校驗車牌號
      if c_tocheck_r.cph is null
        then
        errorStr :=concat(errorStr,'車牌號: '||c_tocheck_r.cph||' 不能爲空;');
      else
       select count(*) into cph_rows from znjtresource.t_src_car c where c.plateno = c_tocheck_r.cph;
       if cph_rows <= 0
       then
         errorStr :=concat(errorStr,'車牌號: '||c_tocheck_r.cph||' 不合法;');
       end if;
     end if;
  end if;
--校驗勤務模式
    if c_tocheck_r.qwms is null
      then
         errorStr := concat(errorStr,'勤務模式不能爲空;');
    else
      select count(*) into qwms_rows from znjtcode.d_post_mode m where m.code = c_tocheck_r.qwms;
      if qwms_rows <= 0
      then
        errorStr := concat(errorStr,'勤務模式: '||c_tocheck_r.qwms||' 不合法;');
      end if;
    end if;
--校驗上崗開始時間    
      if /*c_tocheck_r.sgrq != trunc(c_tocheck_r.qssj_d)*/c_tocheck_r.sgrq != trunc(real_qssjd)
        then
         errorStr := concat(errorStr,'上崗日期:'||to_char(c_tocheck_r.sgrq,'yyyy-mm-dd')||'和上崗起始日期'||to_char(real_qssjd,'yyyy-mm-dd hh24:mi:ss')||'不是同一天;');
         goto updateTable;
      end if;
--校驗上崗結束時間
    if trunc(real_jssjd) - c_tocheck_r.sgrq > 1
      then
         errorStr := concat(errorStr,'上崗結束日期:'||to_char(real_jssjd,'yyyy-mm-dd hh24:mi:ss')||'最晚只能是上崗日期:'||to_char(c_tocheck_r.sgrq,'yyyy-mm-dd')||'的次日;');
         goto updateTable;
    elsif  real_jssjd < real_qssjd
      then
         errorStr := concat(errorStr,'上崗結束時間:'||to_char(real_jssjd,'yyyy-mm-dd hh24:mi:ss')||'不能早於上崗開始時間'||to_char(real_qssjd,'yyyy-mm-dd hh24:mi:ss')||';');
         goto updateTable;
    end if;
--判斷該警員在該時間內是否已經佈崗
     select count(*) into police_deploy_rows from znjtpost.t_gw_gwbsxx g where g.jyh = c_tocheck_r.jyh and (g.qssj_d < real_jssjd and g.jssj_d > real_qssjd);
     if police_deploy_rows > 0
     then
       errorStr := concat(errorStr,'警員:'||c_tocheck_r.jyh||'在時間'||to_char(real_qssjd,'yyyy-mm-dd hh24:mi:ss')||'到'||to_char(real_jssjd,'yyyy-mm-dd hh24:mi:ss')||'期間已經佈崗;');
     end if;
--判斷是否請假
     select count(*) into police_leave_rows from t_src_jyabsent j where j.policeno = c_tocheck_r.jyh and (j.starttime <= real_jssjd and j.endtime >= real_qssjd);
     if police_leave_rows > 0
      then
        errorStr := concat(errorStr,'警員:'||c_tocheck_r.jyh||'在時間'||to_char(real_qssjd,'yyyy-mm-dd hh24:mi:ss')||'到'||to_char(real_jssjd,'yyyy-mm-dd hh24:mi:ss')||'期間請假中;');
     end if;
--校驗即將部署的某個警員之間的上崗時間段內是否衝突
     select count(*) into todeploy_timeerror_rows from t_batch_gwbs_check c where   (c.qssj_d < real_jssjd and c.jssj_d > real_qssjd)
      and c.id != c_tocheck_r.id and c.jyh = c_tocheck_r.jyh  and c.pcbh=c_tocheck_r.pcbh;
   if todeploy_timeerror_rows > 0
      then
         errorStr := concat(errorStr,'警員:'||c_tocheck_r.jyh||'在時間'||to_char(real_qssjd,'yyyy-mm-dd hh24:mi:ss')||'到'||to_char(real_jssjd,'yyyy-mm-dd hh24:mi:ss')||'期間,該批次中有其它時間交叉;');
    end if;
 <<updateTable>>
--更新校驗結果
   if errorStr = '' or  errorStr is null
     then
      UPDATE t_batch_gwbs_check C SET C.JYJG='通過',C.STATUS = '0' WHERE C.ID = c_tocheck_r.Id;
    else
      UPDATE t_batch_gwbs_check C SET C.JYJG = errorStr,C.STATUS = '2' WHERE C.ID = c_tocheck_r.Id;
    end if;
    commit;
  END LOOP;
   DBMS_OUTPUT.PUT_LINE('循環結束時間:'||to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'));
EXCEPTION
    WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('PROCEDURE_ERROR:'||SQLERRM);
      ROLLBACK;
END gwbs_check;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章