Oracle,Sql,procedure 感受本身寫的很棒的一個存儲過程

感受本身寫的很棒的一個Oracle存儲過程,(其實想說很叼^,^).sql

集成了一堆操做數據的功能(至少幾十), 包括存儲過程執行異常信息輸出幫助診斷.ide

亮點不少, 好比`over(partition by t.u_id  order by t.u_id)`, 哈哈.oop

是個不少客戶都在用的穩定關鍵存儲過程!(客戶沒有一千也有三五百,而且還在不停的增長)fetch

天天都要運行 無數次。由於每一個客戶的坐席每次打電話都要用到,包括導入客戶資料,幾萬,幾十萬的批量導入,速度都不錯,每次執行都有日誌記錄。優化

  1 create or replace procedure NAG_CUSTOMER_TOPOOLW(
  2 --客戶ID集(如:"10001,10002,10003,") (最後一位逗號,)
  3 --或者是 客戶資料表的where條件(and xx=xxx and yyy=yy) (無最後一位識別)
  4 --或者是 數據池數據ID集(如:"10001,10002,10003,;")  (最後一位是分號;)
  5 --或者是 數據池和 客戶資料的條件("and xc=xc and dpd_dp_id=id!") (最後一位是感嘆號!)
  6 --或者是 select 語句,只有一個字段DPD_ID) (最後一位是井號#)
  7 --或者是 自動外撥的,從臨時表取數據,最後一位是 符號@ (如'來自自動外撥(預測外撥)@')
  8 V_IDW in clob,
  9 
 10 V_FromDPID in varchar2,--從原來哪一個池轉出
 11 V_ToDPID in varchar2,--轉到哪一個池
 12 
 13 V_KH in number,--保留歸屬人
 14 V_DF in number,--從原數據池中刪除
 15 V_KCS in number,--保留撥打狀態
 16 
 17 V_ToUID in number:=0,--轉給誰
 18 V_ActUID in number,--誰操做的
 19 
 20  OutRow out number, --影響的行數; -999999表示滿了,數據量已達上限,未作分配;
 21 V_GID in number:=null
 22 ) Authid Current_User is
 23 /*批量的數據流轉
 24 Auth: Lzpong 2014/05/28
 25 + 幾乎徹底從新寫 2014/10/24
 26 已經在這個池的數據不會被操做
 27 兼容傳入ID集 或者 客戶表的where的條件 +或者 DPD_ID集(分號結尾) 2014/10/24
 28 若是傳入的是ID集,末尾必須有逗號(,),若是是池數據ID集再加個分號(;)
 29 +實現了平均分配 2014/10/24 +2014/11/12修正重寫
 30 +實現了數據限制 2014/10/24 +2014/11/12修正重寫
 31 +修正流轉數據不齊全 2014/12/01
 32 +修正了 臨時表去重致使的 慢問題,並目標池標記刪除的不刪除從新插入了 2014/12/19
 33 +insert/update 使用merge 大大提升速度 2014/12/23
 34 +自動外撥轉數據到數據池,用到了臨時表 "NAG_FIXTABLE_TEMP" 2015/01/29
 35 +客戶資料在數據池中惟一檢查 2015/04/20
 36 +數據池轉移檢查 轉入和轉出池的指定轉移規則屬性  2015/04/20
 37 +刪除原池數據前前加入確認檢查已轉入目標池 2015/05/06
 38 +共享,關注 流轉後刪除/更新 ;更改分配前在臨時表,最後一次性操做數據表,加強效率  2015/05/18
 39 +修復 低權限人可操做全部數據bug; 全局數據惟一設置使數據彙總流轉不能流轉 2015/05/22
 40 +修復 條件轉移 原池不刪除 2015/05/22
 41 +修復 數據限制判斷部分 慢(部分改了)
 42 +限制 審覈狀態的不能流轉 2015/06/08
 43 +日誌 輸出完整的調用日誌和調試輸出信息 2015/06/09
 44 +重大修改整理 徹底整理操做流程 ~2015/06/17
 45 +爲了給其餘功能(導入,未接來電轉移等)通用,爲where條件時,客戶資料操做轉移,徹底由查詢條件限定的(如數據規則限制) 2015/07/07
 46 +客戶資料到數據池 和 自動外撥 到數據池 也檢查 數據惟一性 2015/07/07
 47 +修復剩餘量小於0的不限制狀況 2015/07/08
 48 +(池流轉)刪除源數據時,改成僅更新DPD_DP_ID 2015/07/13
 49 +更新/修復/優化    改刪除原池數據爲更新池ID等 2015/07/24
 50 +限制 非超管 須要業務規則容許才能單池間流轉多條池數據 2015/08/05
 51 +全部入池轉移均根據惟一設置檢查或不檢查數據惟一性 2015/08/11
 52 +數據轉移時清除組ID
 53 +加快表(datapooldata)數據量大時更新數據執行速度,原merge慢 2015/11/17
 54 +指定到人的轉移,超過限制的不轉移 2015/12/24
 55 +修改第一次進池 update 很慢的問題  xy  2016/3/1
 56 +除去客戶姓名入池,避免客戶姓名太長致使出錯  2016/04/16
 57 +修復V_ToUID==null時不能平均分配; 優化日誌老是所有輸出 2016/04/26
 58 +增長入池到組功能,只有不保留歸屬且不指定到人才能到指定組;不然組ID爲空(包括不正確的組ID,保留歸屬,指定到人) lzp 2016/04/29
 59 +加入功能: 在啓用同類型檢查惟一性的時候,(字典)池類型代碼 小於1才檢查 lzp 2016/06/12-
 60 +不保留歸屬,轉到人的,清除組ID; 返回不一樣的錯誤代碼  lzpong 2016/09/09
 61 +不保留撥打狀態入池後,非未撥打的改成callBack  lzpong 2016/12/23
 62 */
 63 /*指定要使用的臨時表
 64 NAG_IDS_TEMP
 65 NAG_IDS1_TEMP
 66 NAG_ALLIDS_TEMP
 67 NAG_FIXTABLE_TEMP (自動外撥轉數據到數據池時)
 68 返回的錯誤代碼:
 69 -900001 : 參數錯誤
 70 -900002 : 目標池已結束, 或設置不容許轉入
 71 -900099 : 可操做流轉的數據量爲0
 72 -900012 : 沒有任何的比例用來分配
 73 -900013 : 沒有可按比例分配的用戶
 74 -999999 : 目標池用戶數據量達到限制, 或用戶數據總量達到限制; 超級管理員在池的上限也受限
 75 -999888 : 不容許流轉多條到池數據,沒有流轉多條的權限
 76 */
 77 tType number:=0; --轉數據的類型 1:客戶表到池 1.5:自動外撥到池 2:單池到單池 3:多池到單池 4:來自數據池(池查詢)(混合查詢) 5:來自數據池(池查詢結果)(池查詢)
 78 fenp number; --目標池的分配類型:1自動抽取,2平均分配,3自定義分配
 79 --isLimit number; --目標池是否有數據限制
 80 str1 varchar2(5000);
 81 znum number;
 82 xnum number;--數據狀態 0:未啓動  1:已啓動  2:已結束,不能操做數據了
 83 ynum number;--是否容許轉入 0:不容許  1:容許
 84 tnum number;
 85 pnum number;
 86 unum number;
 87 cursor dusers is select * from nag_allids_temp; --用戶
 88 cursor newdatas is select * from nag_ids_temp where flag=0 and u_id is null; --未分配數據
 89 strFrom varchar2(200);
 90 dpname varchar2(200):=0; --數據池名
 91 pici varchar(22):=to_char(systimestamp,'yyyymmdd_hh24missxff3'); --分配批次
 92 onlyOne number; --客戶資料在數據池中惟一?
 93 d_DF number:=V_DF; --數據池轉出是否刪除數據
 94 d_ToUID number;--指定的轉給誰,V_ToUID 檢查
 95 GM number;--操做人是不是超管(>0),(流轉多條池數據限制)
 96 gid number:=V_GID;
 97 --調試信息輸出日誌
 98 bgDate date:=sysdate;
 99 --調試信息記錄輸出
100 v_strMsg long;
101 ---------------------------------比例分配新增的參數------------------------------------------
102 hastemp number;--臨時變量
103 SUM_DPL_RATIOCOUNT number;--目標池比例總和
104 TYPE ref_cursor_type IS REF CURSOR;
105 Prorete_Cursor ref_cursor_type;
106 /*V_DPU_U_ID_TEMP varchar(50);
107 V_U_LEVEL_TEMP varchar(50);
108 V_DPL_RATIOCOUNT_TEMP varchar(50);*/
109 V_DPL_RATIOCOUNT_TEMP number;--用於計算比例基數 比例
110 V_U_COUNT_TEMP number;----用於計算比例基數 人數
111 V_SUMRATIONCOUNT number;--用於計算比例基數 總量
112 V_BASECOUNT number;--基數
113 V_DPU_U_ID_TEMP number;--數據池用戶
114 V_U_ID_GETCOUNT number;--分配量
115 V_U_ID_KFP number;--用戶可分配量(臨時變量)
116 ---------------------------------------------------------------------------------------------
117 begin
118   OutRow:=0;
119 if((V_ToDPID is null) or (V_FromDPID is  null and V_ToDPID is null) or (V_ToDPID=V_FromDPID)) then --執行前置條件檢查
120   OutRow:=-900001;
121   null;
122 else --前置條件知足,開始執行
123 /*begin
124     insert into T_CUSTOPOOL_PARAM( t_idw, t_fromdpid, t_todpid, t_kh, t_df, t_kcs, t_touid, t_actuid, t_outrow, t_date,t_Times,t_msg)
125     values(substr(V_IDW,4000), V_FromDPID, V_ToDPID, V_KH, V_DF, V_KCS, d_ToUID, V_ActUID, OutRow, sysdate, tnum, substr(v_str,1,3999));
126 commit;
127 end;*/
128 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 84  begin Exec ');
129   select to_number(sp_param1)  into onlyOne from NAG_SYSPARAM where sp_id=29;--惟一性檢查開關
130   --select dp_pooltype , nvl(d_datapool,0) into xnum,ynum from nag_datapool left join nag_dictionary on d_id=dp_pooltype where dp_id=V_ToDPID;
131   if(onlyOne=1 /*and ynum<1*/)then --是否刪除原池數據
132     d_DF:=1;
133   end if;
134   select DP_START,DP_TRANSINT into xnum,ynum from nag_datapool where dp_id=V_ToDPID;
135   if(xnum=2 or ynum<1)then --目標池 已結束 或 不容許轉入
136     OutRow:=-900002;
137 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 128 目標池 已結束 或 不容許轉入');
138     return ;
139   end if;
140 
141   --若是是單池轉移,--暫作記錄來源池ID
142   if(V_FromDPID is not null and V_FromDPID>10)then
143     dpname:=V_FromDPID;
144   end if;
145   --確保轉到人存在
146   select count(u_id) into d_ToUID from nag_user where u_id=V_ToUID;
147   if(d_ToUID>0)then
148     d_ToUID:=V_ToUID;
149   end if;
150   --操做人是不是超管(>0)
151   select count(gu_g_id) into GM  from nag_groupuser where gu_u_id=V_ActUID and gu_g_id=10000;
152 
153   --清理臨時表
154   execute immediate 'truncate table NAG_IDS_TEMP';--實際(去重)數據操做表
155   execute immediate 'truncate table NAG_IDS1_TEMP';--臨時表
156   --要操做的ID集合,放到臨時表(NAG_IDS_TEMP),flag='0' 的可用,  並 group by 去重
157   strFrom:=substr(V_IDW,length(V_IDW),1);--判斷數據來源
158   CASE strFrom
159     WHEN chr(44) THEN --',' 爲CM_ID集
160       insert into NAG_IDS1_TEMP(CM_ID,DP_ID,FLAG)--CM_ID 客戶ID,數據池ID,0
161         select COLUMN_VALUE,dpname,0  from table(splitstr(V_IDW))
162         where COLUMN_VALUE!=';' and COLUMN_VALUE is not null;
163       insert into nag_ids_temp(cm_id,flag)--CM_ID 客戶ID,0
164              select cm_id,0 from nag_ids1_temp group by cm_id;
165 
166       if(V_FromDPID is not null and V_FromDPID>10)then
167         update nag_ids_temp t set dp_id=V_FromDPID;
168       end if;
169 
170     WHEN chr(59) THEN --';' 是數據池數據ID(dpd_id),多個池的數據
171       --爲DPD_ID集
172       insert into NAG_IDS1_TEMP(DPD_ID,FLAG)--數據池數據ID,0
173         select COLUMN_VALUE,0  from table(splitstr(V_IDW))
174         where COLUMN_VALUE!=';' and COLUMN_VALUE is not null;
175       --根據dpd_id 更新pd_id,cm_id
176       merge into nag_ids1_temp t
177       using (select d.dpd_cm_id,d.dpd_dp_id,d.dpd_id from nag_datapooldata d where d.dpd_actstate=1) tt on (t.dpd_id=tt.dpd_id)
178       when matched then
179         update set cm_id=tt.dpd_cm_id, dp_id= tt.dpd_dp_id, t.flag=0;
180       --CM_ID去重插入
181       --insert into nag_ids_temp(cm_id,dp_id,flag) --使用merge 貌似耗時差的很少
182       --select cm_id,dp_id,0 from (select cm_id,dp_id,row_number() over(partition by t.cm_id  order by t.cm_id) rnum from nag_ids1_temp t) where rnum<2 ;
183       merge into nag_ids_temp t
184  using(select cm_id,dp_id from (
185         select dpd_id,cm_id,dp_id,row_number() over(partition by t.cm_id  order by t.cm_id) rnum from nag_ids1_temp t) where rnum<2 )Q on (1=2)
186       when not matched then insert (t.cm_id,t.dp_id,t.flag) values(Q.cm_id,Q.dp_id,0);
187       tType:=3; --多池到單池
188       if(V_FromDPID is not null and V_FromDPID>10)then
189         strFrom:='來自數據池('||V_FromDPID||')/勾選';
190       else
191         strFrom:='來自數據池(數據彙總/勾選)';
192       end if;
193 
194     WHEN chr(33) THEN --'!' 混合條件
195       str1:='insert into NAG_IDS1_TEMP(cm_id,dp_id,flag) select cm_id,dpd_dp_id,0 from nag_customer c,nag_datapooldata d where c.cm_id=d.dpd_cm_id and d.dpd_actstate=1 '||substr(V_IDW,0,length(V_IDW)-1)||' group by cm_id,dpd_dp_id';
196       execute immediate  str1;
197       --CMID去重
198       merge into nag_ids_temp t using(select cm_id,dp_id from (
199         select dpd_id,cm_id,dp_id,row_number() over(partition by t.cm_id  order by t.cm_id) rnum from nag_ids1_temp t) where rnum<2 )Q on (1=2)
200       when not matched then insert (t.cm_id,t.dp_id,t.flag) values(Q.cm_id,Q.dp_id,0);
201 
202       tType:=4; --池查詢到單池
203       if(V_FromDPID is not null and V_FromDPID>10)then
204         strFrom:='來自數據池('||V_FromDPID||')/查詢';
205       else
206         strFrom:='來自數據池(數據彙總/查詢)';
207       end if;
208 
209     WHEN chr(35) THEN --'#' select語句
210       execute immediate 'insert into NAG_IDS1_TEMP(DPD_ID) '||substr(V_IDW,0,length(V_IDW)-1);
211       --根據dpd_id 更新pd_id,cm_id
212       merge into nag_ids1_temp t
213       using (select d.dpd_cm_id,d.dpd_dp_id,d.dpd_id from nag_datapooldata d where d.dpd_actstate=1) tt on (t.dpd_id=tt.dpd_id)
214       when matched then
215         update set cm_id=tt.dpd_cm_id, dp_id= tt.dpd_dp_id, t.flag=0;
216       --CM_ID去重插入
217       merge into nag_ids_temp t   using(select cm_id,dp_id from (
218         select dpd_id,cm_id,dp_id,row_number() over(partition by t.cm_id  order by t.cm_id) rnum from nag_ids1_temp t) where rnum<2 )Q on (1=2)
219       when not matched then insert (t.cm_id,t.dp_id,t.flag) values(Q.cm_id,Q.dp_id,0);
220 
221       tType:=5; --池查詢到單池
222       if(V_FromDPID is not null and V_FromDPID>10)then
223         strFrom:='來自數據池('||V_FromDPID||')/查詢結果';
224       else
225         strFrom:='來自數據池(數據彙總/查詢結果)';
226       end if;
227       d_DF:=1;
228 
229     WHEN chr(64) THEN --'@' 來自自動外撥
230       --CMID去重
231       merge into nag_ids_temp t using(select all_id,flag from (
232         select all_id,flag,row_number() over(partition by t.all_id  order by t.all_id) rnum from nag_fixtable_temp t) where rnum<2 )Q on (1=2)
233       when not matched then insert (t.cm_id,t.u_id,t.flag) values(Q.all_id,Q.flag,0);
234       tType:=1.5;
235       strFrom:=substr(V_IDW,0,length(V_IDW)-1);
236 
237     ELSE --爲where的條件
238       str1:='';
239        --修復普通人員能夠流轉全部數據(不選擇時)  2015/05/22
240        --爲了給其餘功能(導入,未接來電轉移等)通用,客戶資料操做轉移 徹底由查詢條件限定的(如數據規則限制) 2015/07/07
241       execute immediate  'insert into NAG_IDS1_TEMP(cm_id,dp_id,flag) select cm_id,'||dpname||',0 from nag_customer left join nag_contact ct on ct_cm_id=cm_id where 1=1 '||V_IDW||str1||' group by cm_id';
242       insert into nag_ids_temp(cm_id,flag) select cm_id,0 from nag_ids1_temp group by cm_id;
243       update nag_customer c set c.CM_HOLDERID=null where c.CM_HOLDERID=0 and exists(select 1 from NAG_IDS1_TEMP t where t.cm_id=c.cm_id);
244       if(V_FromDPID is not null and V_FromDPID>10)then
245         update nag_ids_temp set dp_id=V_FromDPID;
246       end if;
247 
248   END CASE; --來路分析結束
249 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 236 into '||sql%rowcount);
250   --沒有數據轉移
251   select count(cm_id) into OutRow from nag_ids_temp;
252   if(OutRow=0)then
253     OutRow:=-900099;
254 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 243 可操做數量爲0');
255     return;
256   elsif(OutRow>1 and tType>=2 and GM<1 and V_FromDPID>10)then --非超管,流轉多條池數據,檢查流轉(數量)規則
257     select count(ps_g_id) into xnum from nag_permission,nag_groupuser where gu_g_id=ps_g_id and ps_br_id=900 and gu_u_id=V_ActUID;
258     if(xnum<1)then
259       OutRow:=-999888;--沒有流轉多條的權限
260 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 250 沒有流轉多條的權限 ');
261       return;
262     end if;
263   end if;
264   --客戶表到池 / 單池到單池
265   if(tType=0)then --來自多池 V_FromDPID 也爲空,排除
266     if(V_FromDPID is null or V_FromDPID <100)then
267       tType:=1;
268       strFrom:='來自客戶資料';
269     else
270        tType:=2;
271        strFrom:='來自數據池('||V_FromDPID||')/查詢';
272     end if;
273   end if;
274 
275    --全部不須要執行(與目標池客戶重複)的打上標識:'1'
276   if(tType>=2)then
277     --除去在自己在目標池的
278     delete from nag_ids_temp where dp_id=V_ToDPID;
279     delete from nag_ids1_temp where dp_id=V_ToDPID;
280     --審覈狀態的不能流轉 標記:1
281     update nag_ids_temp t set flag=1
282       where exists(select dpd_id from nag_datapooldata d where d.dpd_cm_id=t.cm_id and d.dpd_dp_id=t.dp_id and d.dpd_actstate=1 and d.dpd_auditstate=1);
283   end if;
284   --數據惟一檢查:數據自己在目標池 不須要執行的 標記:1
285   update nag_ids_temp set flag=1 where  dp_id=V_ToDPID ;
286   update nag_ids1_temp set flag=1 where  dp_id=V_ToDPID ;
287   --數據惟一檢查:與目標池數據重複 標記:1
288   update nag_ids_temp set flag=1
289     where exists (select dpd_cm_id from nag_datapooldata where dpd_dp_id=V_ToDPID and dpd_cm_id=cm_id and dpd_actstate=1);
290   --數據池中惟一檢查:與目標池同類型池惟一(排除原池) 重複標記:1
291   if(onlyOne>0)then
292     --select dp_pooltype into xnum from nag_datapool where dp_id=V_ToDPID;
293     select dp_pooltype , nvl(d_datapool,0) into xnum,ynum from nag_datapool left join nag_dictionary on d_id=dp_pooltype where dp_id=V_ToDPID;
294      --數據惟一檢查:此類型的池是否檢查惟一性
295     if(ynum<1)then
296       if(tType>=2)then--來自數據池
297         update nag_ids_temp t set flag=1
298           where exists (select dpd_cm_id from nag_datapooldata d,nag_datapool p where p.dp_id!=t.dp_id and p.dp_id=d.dpd_dp_id and p.dp_pooltype=xnum and d.dpd_cm_id=t.cm_id and d.dpd_actstate=1);
299       else --來自客戶資料(沒有池ID)
300         update nag_ids_temp t set flag=1
301           where exists (select dpd_cm_id from nag_datapooldata d,nag_datapool p where p.dp_id!=V_ToDPID and p.dp_id=d.dpd_dp_id and p.dp_pooltype=xnum and d.dpd_cm_id=t.cm_id and d.dpd_actstate=1);
302       end if;
303     end if;
304   end if;
305 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3')||' 285 update '||sql%rowcount);
306   --===========================================若是沒有可轉移數據,跳到結束執行====================================================--
307   select count(cm_id) into OutRow from nag_ids_temp where flag=0;
308   if(OutRow=0)then
309     OutRow:=-900099;
310 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3')||' 290 可操做數量爲0');
311     goto endExc;
312   end if;
313 
314   --數據池類型:1自動抽取,2平均分配,3自定義分配,4資源分配,5自定義抽取
315   select dp_dataassign into fenp from nag_datapool where dp_id=V_ToDPID;
316   select dp_name into dpname from nag_datapool where dp_id=V_ToDPID;
317 
318   execute immediate 'truncate table nag_allids_temp';
319   --用戶的可分配數量          (uid,可分配數,已分配數)  --v_userdatadistribute 代替
320   insert into nag_allids_temp(all_id,tag5,tag6)
321     select u_id,leftnum, 0   from v_userdatadistribute where dp_id=V_ToDPID;
322 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 299 into '||sql%rowcount);
323 
324   --=============================================上面數據已準備,下面開始分配======================================================--
325 
326   --須要平均分配: 目標池爲平均分配,且 不保留歸屬而且沒有指定目標用戶
327   if(fenp=2 and (tType<2 or (V_KH=0 and (V_ToUID<100 or V_ToUID is null)))) then --平均分配
328   begin
329     gid:=null;
330     --根據目標池成員刪除臨時表非目標池成員的數據,刪除非池成員
331     delete from nag_allids_temp a where not exists(select pu.dpu_u_id from nag_datapooluser pu where pu.dpu_dp_id=V_ToDPID and pu.dpu_u_id=a.all_id);
332     --有歸屬的更新數量,防止超限
333     update nag_allids_temp a set tag5=(select a.tag5+count(cm_id) from nag_ids_temp t where t.u_id=a.all_id and flag=0);
334     --用戶數
335     select count(*) into ynum from nag_allids_temp;
336     --可分配總量
337     select sum(tag5) into xnum from nag_allids_temp;
338 
339     --循環分配
340     znum:=0; --第幾輪
341     for cur in newdatas loop
342       exit when xnum=0 and ynum<1;
343       unum:=0;
344       --找第znum輪還有沒分配的,或者下一輪的
345       select count(*) into pnum from nag_allids_temp where tag6=znum and tag5>0;
346 
347       if(pnum>0)then --本輪有
348         select all_id into unum from nag_allids_temp where rownum<2 and tag6=znum and tag5>0;
349 
350       else --得進入下一輪
351         znum:=znum+1;
352         select count(*) into pnum from nag_allids_temp where tag6=znum and tag5>0;
353 
354         if(pnum<1)then exit; end if;--沒人可分配了,退出
355         select all_id into unum from nag_allids_temp where rownum<2 and tag6=znum and tag5>0;
356 
357       end if;
358 
359       --沒人要分配了,退出
360       if(unum=0)then exit; end if;
361 
362       --分配,在臨時表,並記錄
363       update nag_allids_temp set tag6=tag6+1,tag5=tag5-1 where all_id=unum; --記錄,並進入下一輪待命
364       update nag_ids_temp set u_id=unum where cm_id=cur.cm_id;
365 
366       xnum:=xnum-1;
367 
368     end loop;
369 
370     --操做日誌/消息
371     for ur in dusers loop
372     if(ur.tag6>0)then
373       --操做日誌
374       insert into nag_system_log(sl_u_id,sl_type,sl_module,sl_content)
375         values(V_ActUID,'數據轉移','平均分配('||dpname||'-池:ID-'||V_ToDPID||'),'||strFrom,'因平均分配規則,給用戶['||ur.all_id||']分配了'||ur.tag6||'條數據');
376 
377       --消息(系統消息)
378       insert into nag_messages(m_type,m_from,m_to,m_msg)
379         values(4,V_ActUID,ur.all_id,'你的平均分配數據池('||dpname||')新增了'||ur.tag6||'條數據<a href=''#'' onclick=\"parent.window.OpenTab('''||dpname||''',''/DataPool/DataPoolShow?FNID=3000'||'&'||'DPID='||V_ToDPID||'&'||'BATCH='||pici||''',true)\">查看</a>');
380     end if;
381 
382     end loop;
383 
384     --有剩餘數據的操做日誌/消息
385     select count(*) into xnum from nag_ids_temp where u_id is not null and flag=0;--已分配量
386     select count(*) into tnum from nag_ids_temp where u_id is null and flag=0;--剩餘量
387     if(tnum>0)then
388       insert into nag_system_log(sl_u_id,sl_type,sl_module,sl_content)
389         values(V_ActUID,'數據轉移','平均分配池('||dpname||' ID='||V_ToDPID||'),'||strFrom,'由平均分配規則,給('||dpname||')的'||ynum||'個用戶分配了:'||xnum||'條數據,剩餘:'||tnum||'條數據未分配,成爲公共數據');
390     end if;
391 
392     --給執行者報告
393     insert into nag_messages(m_type,m_from,m_to,m_msg)
394         values(4,V_ActUID,V_ActUID,'你給平均分配池('||dpname||' ID='||V_ToDPID||')的'||ynum||'個用戶分配了:'||xnum||'條數據,剩餘:'||tnum||'條數據未分配,成爲公共數據');
395 
396   end;--平均分配塊結束
397     ----------------------------------------------------比例分配-------------------------------------
398     --須要比例分配: 目標池爲比例分配,且 不保留歸屬而且沒有指定目標用戶
399     --數據池中-轉入數據池-保留數據歸屬(不打勾),而且是清除數據數據歸屬是才知足比例分配
400     elsif (fenp=6 and (V_KH=0 and (V_ToUID<100 or V_ToUID is null))) then
401   begin
402       --比例分配
403         select count(*) into hastemp from user_tables where table_name=upper('TEMP_PRORATE_'||V_ActUID);
404         if(hastemp)>0 then
405       execute immediate 'drop table  TEMP_PRORATE_'||V_ActUID||' purge ';
406     end if;
407         select count(*) into hastemp from user_tables where table_name=upper('TEMP_PRORATE_EXT'||V_ActUID);
408         if(hastemp>0)    then
409             execute immediate 'drop table  TEMP_PRORATE_EXT'||V_ActUID||' purge ';
410     end if;
411         --刪除沒有分配量的用戶
412         delete from  nag_allids_temp where tag5<1;
413         --用戶,等級,比例
414         execute immediate 'create table TEMP_PRORATE_'||V_ActUID||'(DPU_U_ID,U_LEVEL,DPL_RATIOCOUNT)
415          as select DPU_U_ID,U_LEVEL,DPL_RATIOCOUNT from nag_datapooluser  inner join nag_user on dpu_u_id=u_id
416 left join NAG_DATAPOOLLIMIT on dpl_dp_id=dpu_dp_id and dpl_level=U_LEVEL
417 where dpu_dp_id='||V_ToDPID||' and exists(select 1 from nag_allids_temp atp where atp.all_id=dpu_u_id) ';
418     --等級,比例,成員數
419     execute immediate 'create table TEMP_PRORATE_EXT'||V_ActUID||'(DPL_LEVEL,DPL_RATIOCOUNT,U_COUNT) as select DPL_LEVEL,DPL_RATIOCOUNT,(select count(1) from TEMP_PRORATE_'||V_ActUID||' tee where tee.U_LEVEL=nmt.DPL_LEVEL) as U_COUNT from NAG_DATAPOOLLIMIT nmt where dpl_dp_id='||V_ToDPID||'';
420     --查看有成員數的比例是否是大於0
421     execute immediate ' select sum(DPL_RATIOCOUNT)  from TEMP_PRORATE_EXT'||V_ActUID||' where U_COUNT>0' into SUM_DPL_RATIOCOUNT;
422     if(SUM_DPL_RATIOCOUNT<1)then--沒有任何的比例用來分配
423       OutRow:=-900012;
424       goto endExc;
425     end if;
426     --刪除沒有比例的用戶
427     execute immediate 'delete from TEMP_PRORATE_'||V_ActUID||' where DPL_RATIOCOUNT<1';
428     execute immediate 'select count(1) from TEMP_PRORATE_'||V_ActUID||' ' into hastemp;
429     if hastemp<1 then
430       OutRow:=-900013;
431       goto endExc;
432     end if;
433     --刪除沒有比例的等級
434     execute immediate 'delete from TEMP_PRORATE_EXT'||V_ActUID||' where U_COUNT<1 or DPL_RATIOCOUNT<0.00000001 ';
435     V_SUMRATIONCOUNT:=0;
436     --開始計算總量
437     open Prorete_Cursor for 'select DPL_RATIOCOUNT,U_COUNT from TEMP_PRORATE_EXT'||V_ActUID||'';
438     loop
439     fetch Prorete_Cursor into V_DPL_RATIOCOUNT_TEMP,V_U_COUNT_TEMP;
440       exit when Prorete_Cursor%notfound;
441       V_SUMRATIONCOUNT:=V_SUMRATIONCOUNT+(V_DPL_RATIOCOUNT_TEMP*V_U_COUNT_TEMP);
442     end loop;
443     close Prorete_Cursor;
444     --臨時表可分配的數量
445     select count(1) into hastemp from nag_ids_temp where flag=0;
446     --計算出比例最小基數
447     V_BASECOUNT:=hastemp/V_SUMRATIONCOUNT;
448 
449     open Prorete_Cursor for 'select DPU_U_ID,DPL_RATIOCOUNT from TEMP_PRORATE_'||V_ActUID||'';
450     loop
451     fetch Prorete_Cursor into V_DPU_U_ID_TEMP,V_DPL_RATIOCOUNT_TEMP;
452       exit when Prorete_Cursor%notfound or hastemp<1;
453       V_U_ID_GETCOUNT:=V_DPL_RATIOCOUNT_TEMP*V_BASECOUNT;
454       --判斷是否小數點進1
455       if(V_U_ID_GETCOUNT>trunc(V_U_ID_GETCOUNT)) then
456         V_U_ID_GETCOUNT:=trunc(V_U_ID_GETCOUNT)+1;
457       end if;
458       --用戶可分配量寫入臨時變量
459       select tag5 into V_U_ID_KFP from nag_allids_temp where all_id=V_DPU_U_ID_TEMP;
460       if V_U_ID_GETCOUNT>V_U_ID_KFP then
461         V_U_ID_GETCOUNT:=V_U_ID_KFP;
462       end if;
463       update nag_ids_temp set u_id=V_DPU_U_ID_TEMP where rownum<V_U_ID_GETCOUNT+1 and flag=0 and u_id is null;
464       hastemp:=hastemp-V_U_ID_GETCOUNT;
465     end loop;
466     close Prorete_Cursor;
467 
468       /*select * from v_userdatadistribute*/
469       /*insert into strtable values('比例分配'||(select count(1) from nag_ids_temp where flag=1) );*/
470     end;
471     --------------------------------------比例分配結束-----------------------------------------------
472   else --=================================非平均分配=============================================--
473   begin
474     if(V_KH=0)then --不保留歸屬
475     begin
476       update nag_ids_temp set u_id=null;--清除歸屬
477 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3')||' 365 update '||sql%rowcount);
478       --指定到人的話,數據限制適配
479       if(d_ToUID>100)then
480           select count(*) into tnum from nag_allids_temp t where t.tag5>0 and t.all_id=d_ToUID;
481           if(tnum=0)then
482             OutRow:=-999999; --數據量達到限額
483 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 370 數據量達到限額 ');
484             rollback;
485             return;
486           end if;
487         --分配,不超過剩餘可分配數
488         select tag5 into tnum from nag_allids_temp where all_id=d_ToUID;
489         update nag_ids_temp set u_id=d_ToUID where flag=0 and rownum<=tnum;
490         tnum:=sql%rowcount;
491 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3')||' 378 update '||sql%rowcount);
492         --超過的數據量,不轉移
493         --delete from nag_ids_temp where u_id is null;
494          update nag_ids_temp set flag=2 where  u_id is null;
495 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3')||' 380 delete '||sql%rowcount);
496         --發消息(系統消息)
497         insert into nag_messages(m_type,m_from,m_to,m_msg)
498           values(4,V_ActUID,d_ToUID,'你的數據池('||dpname||')新增了'||tnum||'條數據<a href=''#'' onclick=\"parent.window.OpenTab('''||dpname||''',''/DataPool/DataPoolShow?FNID=3000'||'&'||'DPID='||V_ToDPID||'&'||'BATCH='||pici||''',true)\">查看</a>');
499       end if;
500 
501       --操做日誌
502       insert into nag_system_log(sl_u_id,sl_type,sl_module,sl_content)
503         values(V_ActUID,'數據轉移','指定分配('||dpname||'-池:ID='||V_ToDPID||'),'||strFrom,'給用戶[ID='||d_ToUID||']分配了:'||tnum||'條數據,總數爲:'||xnum||'');
504 
505     end;--不保留歸屬塊結束
506     else --保留歸屬
507     begin
508       gid:=null;
509       if(tType<2)then --來自客戶表(或自動外撥)
510         if(tType=1)then
511           --或許已經有歸屬了,就保留歸屬轉入池
512           update nag_customer c set cm_holderid=(
513             select u.u_id from nag_user u where c.cm_holder = u.u_agentid
514           )where c.cm_holderid is null and exists(select 1 from nag_ids_temp t where  c.cm_id=t.cm_id);
515 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3')||' 397 update '||sql%rowcount);
516           --客戶歸屬有的,保留其歸屬
517           update (select t.u_id,c.cm_holderid from nag_ids_temp t,nag_customer c where t.cm_id=c.cm_id) set u_id=cm_holderid;
518 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3')||' 400 update '||sql%rowcount);
519         end if;
520         --若是是第一次入池,記錄
521 /*        update nag_customer set CM_DATAPOOLID=V_ToDPID
522           where CM_DATAPOOLID is null and cm_id in (select cm_id from nag_ids_temp where flag=0);*/
523        --edit person xy 2016/3/1
524              update nag_customer nr set CM_DATAPOOLID=V_ToDPID
525              where CM_DATAPOOLID is null and exists(select 1 from nag_ids_temp where flag=0 and cm_id=nr.cm_id);
526 
527       else --來自數據池
528         update nag_ids_temp t set t.u_id=(select dpd_u_id from nag_datapooldata d where t.cm_id=d.dpd_cm_id and t.dp_id=d.dpd_dp_id and d.dpd_actstate=1 and rownum<2);
529 /*
530         merge into nag_ids_temp t
531         using nag_datapooldata d on(t.cm_id=d.dpd_cm_id and t.dp_id=d.dpd_dp_id and d.dpd_actstate=1)
532         when matched then
533           update set t.u_id=d.dpd_u_id;
534 */
535       end if;
536 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 413 insert pool '||sql%rowcount);
537 
538     end;--保留歸屬塊結束
539     end if;--是否保留歸屬
540 
541   end;--非平均分配塊結束
542   end if; --是否須要平均分配
543 
544   --超限的不轉移 (修復剩餘量小於0的不限制狀況)
545   update nag_ids_temp tt set flag=2
546     where exists(--是否存在超限,或已超限
547       select cm_id from (
548         select t.cm_id,t.u_id,a.tag5,row_number() over(partition by t.u_id  order by t.u_id) rnum
549         from nag_ids_temp t,nag_allids_temp a where t.u_id=a.all_id) A
550       where (A.rnum>A.tag5 or A.tag5<1) and A.u_id=tt.u_id and A.cm_id=tt.cm_id
551     ) and tt.u_id is not null;
552 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3')||' 429 update '||sql%rowcount);
553 
554   --對保留歸屬(且有歸屬)的單條轉移進行特殊判斷
555   select count(cm_id) into xnum from nag_ids_temp;
556   if(xnum=1 and V_KH=1)then
557     select count(cm_id) into xnum from nag_ids_temp where u_id is not null;--有歸屬數量
558     select count(cm_id) into ynum from nag_ids_temp where u_id is not null and flag=0;--有歸屬且能轉移數量
559     if( xnum>0 and ynum=0)then
560       OutRow:=-999999;
561 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3')||' 439 可操做數量爲0');
562       goto endExc;
563     end if;
564   end if;
565 
566 select count(*) into ynum from nag_group where g_id=V_GID;
567 if(ynum<1)then --正確的組ID
568   gid:=null;
569 end if;
570   --=============================================上面數據已準備,下面開始轉移======================================================--
571 
572   --刪除標記已刪除的數據 , 無論刪除狀態(dpd_actstate=0)了會有問題
573   --delete from nag_datapooldata dd where dd.dpd_actstate=0 and dd.dpd_dp_id=V_ToDPID
574   --  and exists (select cm_id from nag_ids_temp where cm_id=dd.dpd_cm_id and flag=0);
575 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3')||' 447 delete '||sql%rowcount);
576   --已分配的數據進池,未分配/剩餘的也一塊兒進池
577   if(tType<2)then --來自客戶表
578     merge  into nag_datapooldata
579     using (select pici PICI, t.cm_id ,t.u_id,u.u_agentid,u.u_agentname,(select wm_concat(g_name) from nag_group,nag_groupuser where gu_g_id=g_id and gu_u_id=t.u_id and g_pid>-1 group by gu_u_id) u_agentpartment
580       from nag_customer cm, nag_user u, nag_ids_temp t
581       where cm.cm_id=t.cm_id and t.u_id=u.u_id(+) and t.flag=0
582     )tt on (1=0)
583     when not matched then
584       insert (dpd_batch,dpd_createdate,dpd_dp_id,dpd_cm_id,dpd_u_id,DPD_AGENTID,DPD_AGENTNAME,dpd_agentpartment,dpd_gid)
585       values (tt.PICI,sysdate,V_ToDPID,tt.cm_id ,tt.u_id,tt.u_agentid,tt.u_agentname,u_agentpartment, gid);
586 
587   else --來自數據池
588     if(d_DF>0)then --刪除原池數據的
589       if(V_KH=1)then --保留歸屬的話
590         update nag_datapooldata d --2015/11/17 lzp 加快表數據量大時執行速度,原merge慢
591         set d.dpd_dp_id=V_ToDPID,d.dpd_batch=pici,d.dpd_createdate=sysdate
592         ,d.dpd_dialstate=0,d.dpd_dialstatestr='未撥打',d.dpd_dialstarttime=null,d.dpd_dialendtime=null,d.dpd_callresult=null,d.dpd_dialtimes=0
593         ,d.dpd_agentpartment=(select wm_concat(g_name) from nag_group,nag_groupuser where gu_g_id=g_id and gu_u_id=d.dpd_u_id and g_pid>-1 group by gu_u_id)
594         where DPD_ACTSTATE=1 and exists(select 1 from nag_ids_temp tt where tt.cm_id=d.dpd_cm_id and tt.flag=0 and d.dpd_dp_id=tt.dp_id);
595 
596       else --不保留歸屬
597        update nag_datapooldata d --2015/11/17 lzp 加快表數據量大時執行速度,原merge慢
598         set d.dpd_dp_id=V_ToDPID,d.dpd_batch=pici,d.dpd_createdate=sysdate,d.dpd_allocdate=sysdate,d.DPD_ALLOCDAYNUM=1,d.dpd_gid=gid
599         ,d.dpd_dialstate=0,d.dpd_dialstatestr='未撥打',d.dpd_dialstarttime=null,d.dpd_dialendtime=null,d.dpd_callresult=null,d.dpd_dialtimes=0
600         ,(d.dpd_u_id,d.dpd_agentid,d.dpd_agentname,d.dpd_agentpartment)
601         =(select u.u_id,u.u_agentid,u.u_agentname,wm_concat(g_name) from nag_group g,nag_groupuser,nag_ids_temp t
602           left join nag_user u on t.u_id=u.u_id
603          where gu_g_id=g.g_id and gu_u_id=t.u_id  and t.cm_id=d.dpd_cm_id and d.dpd_dp_id=t.dp_id and g.g_pid>-1 group by u.u_id,u.u_agentid,u.u_agentname)
604         where DPD_ACTSTATE=1 and  exists(select 1 from (select t.cm_id,t.dp_id,t.u_id,u.u_agentid,u.u_agentname from nag_ids_temp t
605           left join nag_user u on t.u_id=u.u_id  where t.flag=0)tt where tt.cm_id=d.dpd_cm_id and d.dpd_dp_id=tt.dp_id);
606       end if;
607 
608     else --不刪除原池數據
609       merge into nag_datapooldata
610       using (select pici PICI,t.cm_id ,t.u_id ,u.u_agentid ,u.u_agentname ,d.dpd_dialtel,(select wm_concat(g_name) from nag_group,nag_groupuser where gu_g_id=g_id and gu_u_id=t.u_id and g_pid>-1 group by gu_u_id) dpd_agentpartment,d.dpd_dialstate,d.dpd_dialstatestr,d.dpd_dialstarttime,d.dpd_dialendtime,d.dpd_recordfile,d.dpd_talklength,d.dpd_officiallength ,d.dpd_nextcalltime,d.dpd_callresult,d.dpd_summary,d.dpd_dialtimes,d.dpd_string1,d.dpd_string2,d.dpd_string3,d.dpd_string4,d.dpd_string5,d.dpd_string6,d.dpd_string7,d.dpd_string8,d.dpd_string9,d.dpd_string10,d.dpd_number1,d.dpd_number2,d.dpd_number3,d.dpd_number4,d.dpd_number5,d.dpd_number6,d.dpd_date1,d.dpd_date2,d.dpd_date3,d.dpd_date4,d.dpd_date5,d.dpd_comment1,d.dpd_comment2,d.dpd_comment3,d.dpd_comment4,d.dpd_comment5, d.dpd_actstate,d.dpd_act_u_id,d.dpd_actdate,d.dpd_allocdate,d.DPD_ALLOCDAYNUM
611         from  nag_customer cm, nag_user u ,nag_datapooldata d, nag_ids_temp t
612         where cm.cm_id=t.cm_id and t.u_id=u.u_id(+) and t.flag=0  and t.dp_id=d.dpd_dp_id(+) and t.cm_id=d.dpd_cm_id(+)
613       )tt on (1=0)
614       when not matched then
615         insert (dpd_batch,dpd_dp_id,dpd_cm_id,dpd_u_id,DPD_AGENTID,DPD_AGENTNAME  ,dpd_dialtel,dpd_agentpartment,dpd_dialstate,dpd_dialstatestr,dpd_dialstarttime,dpd_dialendtime,dpd_recordfile,dpd_talklength,dpd_officiallength ,dpd_nextcalltime,dpd_callresult,dpd_summary,dpd_dialtimes,dpd_string1,dpd_string2,dpd_string3,dpd_string4,dpd_string5,dpd_string6,dpd_string7,dpd_string8,dpd_string9,dpd_string10,dpd_number1,dpd_number2,dpd_number3,dpd_number4,dpd_number5,dpd_number6,dpd_date1,dpd_date2,dpd_date3,dpd_date4,dpd_date5,dpd_comment1,dpd_comment2,dpd_comment3,dpd_comment4,dpd_comment5,dpd_createdate,dpd_actstate,dpd_act_u_id,dpd_actdate,dpd_allocdate,DPD_ALLOCDAYNUM, dpd_gid)
616         values (tt.PICI,V_ToDPID,tt.cm_id ,tt.u_id ,tt.u_agentid ,tt.u_agentname ,tt.dpd_dialtel,tt.dpd_agentpartment,tt.dpd_dialstate,tt.dpd_dialstatestr,tt.dpd_dialstarttime,tt.dpd_dialendtime,tt.dpd_recordfile,tt.dpd_talklength,tt.dpd_officiallength ,tt.dpd_nextcalltime,tt.dpd_callresult,tt.dpd_summary,tt.dpd_dialtimes,tt.dpd_string1,tt.dpd_string2,tt.dpd_string3,tt.dpd_string4,tt.dpd_string5,tt.dpd_string6,tt.dpd_string7,tt.dpd_string8,tt.dpd_string9,tt.dpd_string10,tt.dpd_number1,tt.dpd_number2,tt.dpd_number3,tt.dpd_number4,tt.dpd_number5,tt.dpd_number6,tt.dpd_date1,tt.dpd_date2,tt.dpd_date3,tt.dpd_date4,tt.dpd_date5,tt.dpd_comment1,tt.dpd_comment2,tt.dpd_comment3,tt.dpd_comment4,tt.dpd_comment5,sysdate,tt.dpd_actstate,tt.dpd_act_u_id,tt.dpd_actdate,tt.dpd_allocdate,tt.DPD_ALLOCDAYNUM, gid);
617     end if;
618     --不保留歸屬,轉到人的,清除組ID lzpong 2015/09/09
619     if (V_KH=0 and V_ToUID>100) then
620       update nag_datapooldata set dpd_gid=null
621       where dpd_dp_id=V_ToDPID and dpd_gid is not null and dpd_u_id is not null and dpd_actstate=1
622         and exists(select 1 from nag_ids_temp t where t.u_id=dpd_u_id and t.cm_id=dpd_cm_id);
623     end if;
624   end if;--數據流轉
625   OutRow:=sql%rowcount;
626 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 487 merge into pool '||sql%rowcount);
627 
628   --=============================================數據已轉移成功,下面後續動做======================================================--
629 
630 if(OutRow>0)then
631   --不保留撥打狀態,非未撥打的改成callBack
632   if(V_KCS=0 and tType>1)then
633     update nag_datapooldata set DPD_DIALSTATE=2,DPD_DIALSTATESTR='callback',dpd_callresult=null where dpd_batch=pici and DPD_DIALSTATE>0;
634 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3')||' 495 merge '||sql%rowcount);
635   end if;
636   --不保留歸屬 更新分配時間/持有天數
637   if(V_KH=0)then
638     update nag_datapooldata set dpd_allocdate=null,DPD_ALLOCDAYNUM=null where dpd_batch=pici and DPD_U_ID is null; --(無歸屬)
639     update nag_datapooldata set dpd_allocdate=sysdate,DPD_ALLOCDAYNUM=1 where dpd_batch=pici and dpd_u_id is not null;--(有歸屬)
640   end if;
641 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 502 update '||sql%rowcount);
642   if(d_DF=1)then
643     --更新流轉記錄歷史(刪除)
644     update nag_dataallcationhistory set dah_isdelete=1
645       where exists (select /*+ ORDERED USE_NL(nag_ids1_temp)*/ t1.cm_id from nag_ids1_temp t1,nag_ids_temp t where t1.dp_id=DAH_OUTCALLID and DAH_CUSTOMERID=t1.cm_id and t1.cm_id=t.cm_id and t.flag=0);
646 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 507 update '||sql%rowcount);
647   end if;
648   --添加流轉到記錄(入池)
649   merge into NAG_DATAALLCATIONHISTORY
650   using(select t.cm_id,u.u_agentid,u.u_agentname,u.U_ID from nag_ids_temp t
651     left join nag_user u on t.u_id=u.u_id   where t.flag=0
652   )tt on (1=0)
653   when not matched then
654     insert (DAH_OUTCALLID, DAH_OUTCALLNAME, DAH_CUSTOMERID, DAH_AGENTID, DAH_AGENTNAME,DAH_CREATEBY,DAH_TYPE)
655     values (V_ToDPID,dpname,tt.cm_id,tt.u_agentid,tt.u_agentname,V_ActUID,strFrom);
656 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 517 merge '||sql%rowcount);
657 
658   --操做日誌/消息
659   if(fenp=1 and OutRow>1)then --是公共池,並防止撥打自動流轉產生垃圾消息
660     insert into nag_system_log(sl_u_id,sl_type,sl_module,sl_content)
661       values(V_ActUID,'數據轉移','自動抽取出池','給自動抽取池('||dpname||'),增長數據量:'||OutRow);
662 
663     insert into nag_messages(m_type,m_from,m_to,m_msg)
664       select 4,V_ActUID,dpu_u_id,dpname||',迎來了一波公共數據,<a href=''#'' onclick=\"parent.window.OpenTab('''||dpname||''',''/DataPool/DataPoolShow?FNID=3000'||'&'||'DPID='||V_ToDPID||''',true)\">當即前往抽取數據</a>' from nag_datapooluser where dpu_dp_id=V_ToDPID;
665 
666   --elsif(fenp=3)then --自定義池
667   --  null; --通知誰去分配數據
668   end if;
669   --操做日誌/消息,分配到組的
670   if(gid is not null)then
671      select g_name into str1 from nag_group where g_id=gid;
672      select u_agentname||'轉入數據池'||dpname||'['||OutRow||'] 條數據,並分配給組['||str1||']成爲組公共數據; '||strFrom into str1 from nag_user where u_id=V_ActUID;
673 
674      insert into nag_system_log(sl_u_id,sl_type,sl_module,sl_content)
675        values( V_ActUID, '新組公共數據', '數據轉移', str1);
676 
677      insert into nag_messages(m_type,m_from,m_to,m_title,m_msg)
678        select 5,V_ActUID, gm_u_id ,'新數據到組(組公共數據)',str1 from nag_groupmanger where gm_g_id=gid;
679 
680      insert into NAG_DATATOGROUPUSERHISTORY
681        values (V_ToDPID,gid,null,OutRow,sysdate,V_ActUID);
682   end if;
683 
684   --若是流轉成功,更新關注共享?
685   if(tType>=2)then
686     --更新關注?用戶在目標池內
687     select sp_param1 into xnum from nag_sysparam where sp_id=16;--數據流轉時更新關注
688         if xnum is not null and xnum=1
689         then
690       merge into NAG_CUSTOMERATTENTION using nag_ids_temp on(flag=0 and ca_cm_id=cm_id and ca_dp_id=dp_id)
691       when matched then update set ca_u_id=0 delete where 1=1;
692 
693       merge into NAG_CUSTOMERATTENTIONHISTORY using nag_ids_temp on(flag=0 and cah_cm_id=cm_id and cah_dp_id=dp_id)
694       when matched then update set CAH_ENDDATETIME=sysdate;
695         end if;
696     --存在共享,更新共享?
697     select sp_param1 into xnum from nag_sysparam where sp_id=15;--池中已共享的數據流轉時更新共享
698         if xnum is not null and xnum=1
699         then
700       merge into NAG_CUSTOMERSHARE using nag_ids_temp on(flag=0 and cs_cm_id=cm_id and cs_dp_id=dp_id)
701       when matched then update set cs_from_u_id=0 delete where 1=1;
702 
703       merge into NAG_CUSTOMERSHAREHISTORY using nag_ids_temp on(flag=0 and csh_cm_id=cm_id and csh_dp_id=dp_id)
704       when matched then update set CSH_ENDDATETIME=sysdate;
705         end if;
706 v_strMsg:=v_strMsg||chr(10)||(to_char(systimestamp,'hh24:mi:ssxff3') ||' 562 merge '||sql%rowcount);
707   end if; --更新關注共享
708 
709 end if; --if(OutRow>0)
710   commit;
711 -----------------最後,提交更改-----------------
712 <<endExc>>
713   --記錄執行日誌
714   tnum:=(sysdate-bgDate)*48600;
715   --if ((tnum>0 and OutRow>1)) then
716     v_strMsg:=strFrom||chr(10)||v_strMsg;
717     insert into T_CUSTOPOOL_PARAM( t_idw, t_fromdpid, t_todpid, t_kh, t_df, t_kcs, t_touid, t_actuid, t_outrow, t_date,t_Times,t_msg)
718     values(substr(V_IDW,1,3985)||'...', V_FromDPID, V_ToDPID, V_KH, V_DF, V_KCS, d_ToUID, V_ActUID, OutRow, sysdate, tnum, substr(v_strMsg,1,3999));
719     commit;
720     dbms_output.put_line(v_strMsg);
721   --end if;
722 
723 end if;
724 
725 exception
726   when others then
727     OutRow:=SQLCODE;
728     IF(OutRow>0)THEN OutRow:=-OutRow;END IF;
729     dbms_output.put_line(SQLCODE||'---'||SQLERRM);
730     rollback;
731   --調用日誌記錄
732   v_strMsg:=strFrom||chr(10)||'SQL ERRCODE:'||SQLERRM||chr(10)||v_strMsg||chr(10)||dbms_utility.format_error_backtrace;
733   insert into T_CUSTOPOOL_PARAM( t_idw, t_fromdpid, t_todpid, t_kh, t_df, t_kcs, t_touid, t_actuid, t_outrow, t_date,t_Msg,t_Times)
734   values(substr(V_IDW,1,3985)||'...', V_FromDPID, V_ToDPID, V_KH, V_DF, V_KCS, d_ToUID, V_ActUID, OutRow,sysdate,substr(v_strMsg,1,3999),(sysdate-bgDate)*48600);
735   commit;
736   dbms_output.put_line(v_strMsg);
737 
738 end NAG_CUSTOMER_TOPOOLW;

固然了 , 也很大!!ui

不過速度仍是很快的啦!!!spa

 

其中一個視圖 v_userdatadistribute:調試

 1 create or replace view v_userdatadistribute as
 2 with Q1 as(
 3   --U_ID,U_AGENTID,U_AGENTNAME,DP_ID,DP_NAME  --用戶,數據池(已結束除外)
 4   select u_id,u_agentid,u_agentname,dp_id,dp_name,u_level,dp_islimit,u_maxdatacount U_MAXDATANUM from nag_user,nag_datapool where dp_start<2
 5   ),Q2 as(
 6   --DP_ID,UID,DP_TOTALCOUNT  --用戶各池數據總量
 7   select dpd_dp_id dp_id,dpd_u_id u_id,count(dpd_id) DP_TOTALCOUNT  from nag_datapooldata,nag_datapool where dp_id=dpd_dp_id and dpd_u_id>0 and dpd_actstate>0 group by dpd_dp_id,dpd_u_id
 8   ),Q3 as(
 9   --UID,U_LEVEL,DPID,DP_TOTALCOUNT --用戶受限數據池數量限制量
10   select u_id,dpu_dp_id dp_id,dpl_totalcout DPL_DATANUM from nag_user,nag_datapooluser,nag_datapoollimit,nag_datapool where u_level=dpl_level and u_id=dpu_u_id and dpl_dp_id=dpu_dp_id and dpl_dp_id=dp_id and dp_islimit>0
11   ),Q4 as(
12   --U_ID,U_DATACOUNT  --用戶受限池的總數據量
13   --select u_id,sum(DP_TOTALCOUNT) U_TOTALCOUNT from Q2 group by u_id --貌似這個更慢?
14   select dpd_u_id u_id, count(dpd_id) U_TOTALCOUNT  from nag_datapooldata,nag_datapool where dp_id=dpd_dp_id and dp_islimit>0 and dpd_actstate>0 and dpd_u_id>0 group by dpd_u_id
15   ),Q as(
16   select Q1.u_id,Q1.u_agentid,Q1.u_agentname,Q1.dp_id,Q1.dp_name,Q1.u_level,Q1.dp_islimit
17   ,(case when Q1.U_MAXDATANUM is null or Q1.dp_islimit=0 then 9999999999 else Q1.U_MAXDATANUM end) U_MAXDATANUM --非池成員或是沒有相應等級(包括是/否池成員)或非限制池的不限制
18   ,nvl(Q3.DPL_DATANUM,9999999999) DPL_DATANUM --沒有等級的不限制
19   ,nvl(Q4.U_TOTALCOUNT,0) U_TOTALCOUNT --用戶擁有數據量(不包括受限池)
20   ,nvl(Q2.DP_TOTALCOUNT,0) DP_TOTALCOUNT --池擁有數據量
21    from Q1
22    left join Q2 on Q1.u_id=Q2.u_id and Q1.dp_id=Q2.dp_id
23    left join Q3 on Q1.u_id=Q3.u_id and Q1.dp_id=Q3.dp_id
24    left join Q4 on Q1.u_id=Q4.u_id
25    )
26 select Q.U_ID,Q.U_AGENTID,Q.U_AGENTNAME,Q.DP_ID,Q.DP_NAME,Q.U_LEVEL,Q.dp_islimit,Q.U_MAXDATANUM,Q.DPL_DATANUM,Q.U_TOTALCOUNT,Q.DP_TOTALCOUNT
27   ,case when Q.dp_islimit=0 then 9999999999 else Q.U_MAXDATANUM-Q.U_TOTALCOUNT end u_leftnum --用戶剩餘量
28   ,case when Q.dp_islimit=0 then 9999999999 else Q.DPL_DATANUM-Q.DP_TOTALCOUNT end dp_leftnum --池剩餘量
29   ,case when Q.dp_islimit=0 then 9999999999 else least((Q.U_MAXDATANUM-Q.U_TOTALCOUNT),(Q.DPL_DATANUM-Q.DP_TOTALCOUNT)) end leftnum --可用量
30 from Q--這些狀況數據不受限: 池不參與數據限制; 不是池成員; 是池成員可是沒有對應的等級
31 ;
View Code

執行日誌表:日誌

 1 create table T_CUSTOPOOL_PARAM
 2 (
 3   t_idw      VARCHAR2(4000), --條件參數(如下如同)  4   t_fromdpid VARCHAR2(40),
 5   t_todpid   VARCHAR2(40),
 6   t_kh       NUMBER,
 7   t_df       NUMBER,
 8   t_kcs      NUMBER,
 9   t_touid    NUMBER,
10   t_actuid   NUMBER,
11   t_outrow   NUMBER, --結果 12   t_date     DATE default sysdate, --時間 13   t_msg      VARCHAR2(4000), --異常等消息 14   t_times    NUMBER --耗時,毫秒
15 );
相關文章
相關標籤/搜索