update_proces_init_idx_rec.sql 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. CREATE DEFINER=`root`@`localhost` PROCEDURE `update_proces_init_idx_rec`()
  2. BEGIN
  3. SET @enabled = FALSE;
  4. SET @conf_last_exec_lock_key = 'tbl_indexer_CRM_PROCES_last_exec_lock_date';
  5. SET @conf_last_exec_start_key = 'tbl_indexer_CRM_PROCES_last_exec_start';
  6. SET @conf_last_exec_end_key = 'tbl_indexer_CRM_PROCES_last_exec_end';
  7. -- if already runnig mysql will throw 'Commands out of sync; you cant run this command now'
  8. IF (select count(1) from `CRM_CONFIG` where `conf_key`=@conf_last_exec_lock_key and `conf_val`!='')>0 THEN
  9. UPDATE `P5-MSG:Route_FixCrmProcesInitIdx:ERROR: Already executed` SET x=1;
  10. ELSE BEGIN
  11. -- CONF.1--set-start-time-in-config
  12. replace into `CRM_CONFIG` (`conf_key`, `conf_val`) values (@conf_last_exec_lock_key, NOW()), (@conf_last_exec_start_key, NOW());
  13. -- I.1--truncate-table
  14. truncate table `CRM_PROCES_idx_TEMP`;
  15. -- I.2--fill-CRM_PROCES_idx_TEMP
  16. insert into `CRM_PROCES_idx_TEMP` ( `ID_PROCES`, `TYPE`, `PARENT_ID`, `idx_PROCES_INIT_ID`, `idx_MAIN_PROCES_INIT_ID` )
  17. select p.`ID` as `ID_PROCES`
  18. , p.`TYPE` as `TYPE`
  19. , p.`PARENT_ID` as `PARENT_ID`
  20. , IF( p.`TYPE` = 'PROCES_INIT', p.`ID`, 0 ) as `idx_PROCES_INIT_ID`
  21. , IF( p.`TYPE` = 'PROCES_INIT', p.`ID`, 0 ) as `idx_MAIN_PROCES_INIT_ID`
  22. from `CRM_PROCES` p
  23. where p.`A_STATUS` in ( 'NORMAL', 'WAITING' )
  24. ;
  25. -- G.1--before-while-2-set-initial-proc_with_groups_id
  26. update `CRM_PROCES_idx_TEMP` i
  27. join `CRM_WSKAZNIK` as wsk on ( wsk.`ID_PROCES` = i.`ID_PROCES`
  28. and wsk.`A_STATUS` in ( 'NORMAL', 'WAITING' )
  29. )
  30. join `CRM_LISTA_ZASOBOW` as z on ( z.`ID` = wsk.`ID_ZASOB`
  31. and z.`TYPE` in ( 'STANOWISKO', 'PODMIOT', 'DZIAL' )
  32. and z.`A_STATUS` in ( 'NORMAL', 'WAITING' )
  33. )
  34. join `CRM_PRZYPADEK` przyp on ( przyp.`ID` = wsk.`ID_PRZYPADEK`
  35. and przyp.`FORM_TREAT` & 8 -- has perm X
  36. )
  37. set
  38. i.`idx_PROCES_WITH_GROUPS_ID` = i.`ID_PROCES`
  39. where i.`idx_PROCES_INIT_ID` > 0
  40. ;
  41. -- G.2--while-2-loop-update-proc_with_groups_id-from-parent
  42. SET @i = 0;
  43. SET @loopLomit = 100;
  44. SET @groupCnt = 1;
  45. WHILE @i < @loopLomit and @groupCnt > 0 DO
  46. update `CRM_PROCES_idx_TEMP` as p
  47. join `CRM_PROCES_idx_TEMP` as pp on (
  48. pp.`ID_PROCES` = p.`PARENT_ID`
  49. and pp.`idx_PROCES_WITH_GROUPS_ID` is not null
  50. and pp.`idx_PROCES_WITH_GROUPS_ID` > 0
  51. )
  52. set p.`idx_PROCES_WITH_GROUPS_ID` = pp.`idx_PROCES_WITH_GROUPS_ID`
  53. where p.`idx_PROCES_WITH_GROUPS_ID` is null
  54. ;
  55. SET @groupCnt = ROW_COUNT();
  56. SET @i = @i + 1;
  57. END WHILE;
  58. -- P_INIT_ID.1--fill_idx_INIT
  59. SET @i = 0;
  60. SET @loopLomit = 100;
  61. SET @pinitCnt = 1;
  62. WHILE @i < @loopLomit and @pinitCnt > 0 DO
  63. update `CRM_PROCES_idx_TEMP` p
  64. join `CRM_PROCES_idx_TEMP` pp on ( pp.`ID_PROCES` = p.`PARENT_ID` )
  65. set p.`idx_PROCES_INIT_ID` = IF( p.`idx_PROCES_INIT_ID` > 0, p.`idx_PROCES_INIT_ID`, pp.`idx_PROCES_INIT_ID` )
  66. , p.`idx_MAIN_PROCES_INIT_ID` = pp.`idx_MAIN_PROCES_INIT_ID`
  67. -- // TODO:?: update here `idx_MAIN_PROCES_INIT_ID`
  68. where p.`idx_PROCES_INIT_ID` = 0 and pp.`idx_PROCES_INIT_ID`>0
  69. ;
  70. SET @pinitCnt = ROW_COUNT();
  71. SET @i = @i + 1;
  72. END WHILE;
  73. -- GOTO_AND_RET.1--from-7-fill-goto_and_return
  74. insert ignore into `CRM_PROCES_idx_TEMP` ( `ID_PROCES`, `TYPE`, `PARENT_ID`, `idx_PROCES_INIT_ID`, `idx_MAIN_PROCES_INIT_ID`, `idx_PROCES_WITH_GROUPS_ID` )
  75. select g.`ID`
  76. , 'GOTO_AND_RETURN' as `TYPE`
  77. , p.`ID` as `PARENT_ID`
  78. , g.`ID` as `idx_PROCES_INIT_ID`
  79. , i.`idx_MAIN_PROCES_INIT_ID` as `idx_MAIN_PROCES_INIT_ID`
  80. , IF( i.`idx_PROCES_WITH_GROUPS_ID` is null, gi.`idx_PROCES_WITH_GROUPS_ID`, i.`idx_PROCES_WITH_GROUPS_ID` ) as `idx_PROCES_WITH_GROUPS_ID`
  81. from `CRM_PROCES` p
  82. join `CRM_PROCES_idx_TEMP` i on ( i.`ID_PROCES` = p.`ID` )
  83. join `CRM_PROCES` g on ( g.`ID` = p.`IF_TRUE_GOTO`
  84. and g.`TYPE` = 'PROCES_INIT'
  85. and g.`A_STATUS` in ( 'NORMAL', 'WAITING' )
  86. and p.`IF_TRUE_GOTO_FLAG` = 'GOTO_AND_RETURN'
  87. and p.`IF_TRUE_GOTO` > 0
  88. )
  89. join `CRM_PROCES_idx_TEMP` gi on ( gi.`ID_PROCES` = g.`ID` )
  90. where p.`A_STATUS` in ( 'NORMAL', 'WAITING' )
  91. ;
  92. -- GOTO_AND_RET.2--fill-childrens-of-goto_and_return
  93. insert ignore into `CRM_PROCES_idx_TEMP` ( `ID_PROCES`, `TYPE`, `PARENT_ID`, `idx_PROCES_INIT_ID`, `idx_MAIN_PROCES_INIT_ID`, `idx_PROCES_WITH_GROUPS_ID` )
  94. select ic.`ID_PROCES`
  95. , 'GOTO_AND_RETURN_CHILD' as `TYPE`
  96. , ic.`PARENT_ID` as `PARENT_ID`
  97. , ic.`idx_PROCES_INIT_ID` as `idx_PROCES_INIT_ID`
  98. , i.`idx_MAIN_PROCES_INIT_ID` as `idx_MAIN_PROCES_INIT_ID`
  99. , IF( ic.`idx_PROCES_WITH_GROUPS_ID` is null, i.`idx_PROCES_WITH_GROUPS_ID`, ic.`idx_PROCES_WITH_GROUPS_ID` ) as `idx_PROCES_WITH_GROUPS_ID`
  100. from `CRM_PROCES_idx_TEMP` i
  101. join `CRM_PROCES_idx_TEMP` ic on (
  102. ic.`idx_PROCES_INIT_ID` = i.`ID_PROCES`
  103. and ic.`ID_PROCES` != i.`ID_PROCES`
  104. )
  105. where i.`TYPE` = 'GOTO_AND_RETURN'
  106. ;
  107. -- GOTO_AND_RET.3--fill-goto_and_return_lvl2
  108. -- gc p(5105) is GOTO_AND_RETURN_CHILD pID(5061) pI(5039) pIM(5040) pG(5040)
  109. -- gc p(5105) is GOTO_AND_RETURN_CHILD pID(5061) pI(5039) pIM(5100) pG(5100)
  110. -- gg p(5103) is GOTO_AND_RETURN pID(5105) pI(5103) pIM(5039) pG(NULL)
  111. -- + p(5103) is GOTO_AND_RETURN_LVL2 pID(5105) pI(5103) pIM(5040) pG(5040)
  112. -- + p(5103) is GOTO_AND_RETURN_LVL2 pID(5105) pI(5103) pIM(5100) pG(5100)
  113. insert ignore into `CRM_PROCES_idx_TEMP` ( `ID_PROCES`, `TYPE`, `PARENT_ID`, `idx_PROCES_INIT_ID`, `idx_MAIN_PROCES_INIT_ID`, `idx_PROCES_WITH_GROUPS_ID` )
  114. select gg.`ID_PROCES` as `ID_PROCES`
  115. , 'GOTO_AND_RETURN_LVL2' as `TYPE`
  116. , gg.`PARENT_ID` as `PARENT_ID`
  117. , gg.`idx_PROCES_INIT_ID` as `idx_PROCES_INIT_ID`
  118. , gc.`idx_MAIN_PROCES_INIT_ID` as `idx_MAIN_PROCES_INIT_ID`
  119. , IF( gg.`idx_PROCES_WITH_GROUPS_ID` is null, gc.`idx_PROCES_WITH_GROUPS_ID`, gg.`idx_PROCES_WITH_GROUPS_ID` ) as `idx_PROCES_WITH_GROUPS_ID`
  120. -- , 'gc', gc.*, 'gg', gg.*
  121. from `CRM_PROCES_idx_TEMP` gg
  122. join `CRM_PROCES_idx_TEMP` gc on ( gc.`ID_PROCES` = gg.`PARENT_ID` )
  123. where gg.`TYPE` = 'GOTO_AND_RETURN'
  124. and gc.`TYPE` = 'GOTO_AND_RETURN_CHILD'
  125. ;
  126. -- GOTO_AND_RET.3--fill-childrens-of-goto_and_return_lvl2
  127. -- i p(5103) is GOTO_AND_RETURN_LVL2 pID(5105) pI(5103) pIM(5040) pG(5040)
  128. -- i p(5103) is GOTO_AND_RETURN_LVL2 pID(5105) pI(5103) pIM(5100) pG(5100)
  129. -- ic p(5104) is GOTO_AND_RETURN_CHILD pID(5103) pI(5103) pIM(5039) pG(NULL)
  130. -- ic p(5104) is GOTO_AND_RETURN_CHILD pID(5103) pI(5103) pIM(5039) pG(NULL)
  131. -- + p(5104) is GOTO_AND_RETURN_CHILD_LVL2 pID(5103) pI(5103) pIM(5100) pG(5100)
  132. -- + p(5104) is GOTO_AND_RETURN_CHILD_LVL2 pID(5103) pI(5103) pIM(5040) pG(5040)
  133. insert ignore into `CRM_PROCES_idx_TEMP` ( `ID_PROCES`, `TYPE`, `PARENT_ID`, `idx_PROCES_INIT_ID`, `idx_MAIN_PROCES_INIT_ID`, `idx_PROCES_WITH_GROUPS_ID` )
  134. select ic.`ID_PROCES`
  135. , 'GOTO_AND_RETURN_CHILD_LVL2' as `TYPE`
  136. , ic.`PARENT_ID` as `PARENT_ID`
  137. , ic.`idx_PROCES_INIT_ID` as `idx_PROCES_INIT_ID`
  138. , i.`idx_MAIN_PROCES_INIT_ID` as `idx_MAIN_PROCES_INIT_ID`
  139. , IF( ic.`idx_PROCES_WITH_GROUPS_ID` is null, i.`idx_PROCES_WITH_GROUPS_ID`, ic.`idx_PROCES_WITH_GROUPS_ID` ) as `idx_PROCES_WITH_GROUPS_ID`
  140. -- , 'i', i.*, 'ic', ic.*
  141. from `CRM_PROCES_idx_TEMP` i
  142. join `CRM_PROCES_idx_TEMP` ic on (
  143. ic.`idx_PROCES_INIT_ID` = i.`ID_PROCES`
  144. and ic.`ID_PROCES` != i.`ID_PROCES`
  145. and ic.`TYPE` = 'GOTO_AND_RETURN_CHILD'
  146. )
  147. where i.`TYPE` = 'GOTO_AND_RETURN_LVL2'
  148. ;
  149. -- IDX.1--clear-CRM_PROCES_idx
  150. truncate table `CRM_PROCES_idx`;
  151. -- IDX.2--update-CRM_PROCES_idx-from-temp
  152. insert into `CRM_PROCES_idx` ( `ID_PROCES`, `PARENT_ID`, `TYPE`, `idx_PROCES_INIT_ID`, `idx_MAIN_PROCES_INIT_ID`, `idx_PROCES_WITH_GROUPS_ID` )
  153. select i.`ID_PROCES`, i.`PARENT_ID`, i.`TYPE`, i.`idx_PROCES_INIT_ID`, i.`idx_MAIN_PROCES_INIT_ID`, i.`idx_PROCES_WITH_GROUPS_ID`
  154. from `CRM_PROCES_idx_TEMP` i
  155. where i.`idx_PROCES_INIT_ID` > 0 and i.`idx_PROCES_WITH_GROUPS_ID` is not null
  156. ;
  157. -- TODO OFF: time ~0.1 P.1--update-CRM_PROCES-idx_PROCES_INIT_ID
  158. -- update `CRM_PROCES` p
  159. -- set p.`idx_PROCES_INIT_ID`=coalesce((select i.`idx_PROCES_INIT_ID` from `CRM_PROCES_idx` i where i.`ID_PROCES`=p.`ID` limit 1), 0)
  160. -- ;
  161. -- GP.1--clear-CRM_PROCES_idx_GROUP_to_PROCES
  162. truncate table `CRM_PROCES_idx_GROUP_to_PROCES`;
  163. -- GP.2--fill-CRM_PROCES_idx_GROUP_to_PROCES
  164. insert ignore into `CRM_PROCES_idx_GROUP_to_PROCES` ( `ID_PROCES`, `ID_GROUP` )
  165. select i.`ID_PROCES` as ID_PROCES, z.`ID` as ID_GROUP
  166. from `CRM_PROCES_idx` i
  167. join `CRM_WSKAZNIK` wsk on ( wsk.`ID_PROCES` = i.`idx_PROCES_WITH_GROUPS_ID`
  168. and wsk.`A_STATUS` in ( 'NORMAL', 'WAITING' )
  169. )
  170. join `CRM_LISTA_ZASOBOW` as z on ( z.`ID` = wsk.`ID_ZASOB`
  171. and z.`TYPE` in ( 'STANOWISKO', 'PODMIOT', 'DZIAL' )
  172. and z.`A_STATUS` in ( 'NORMAL', 'WAITING' )
  173. )
  174. join `CRM_PRZYPADEK` przyp on ( przyp.`ID` = wsk.`ID_PRZYPADEK`
  175. and przyp.`FORM_TREAT` & 8 -- has perm X
  176. )
  177. -- where i.`TYPE` in('PROCES_INIT', 'GOTO_AND_RETURN')
  178. ;
  179. -- GP.2--clear-CRM_PROCES_idx_GROUP_to_PROCES_PERM
  180. truncate table `CRM_PROCES_idx_GROUP_to_PROCES_PERM`;
  181. -- GP.3--fill-CRM_PROCES_idx_GROUP_to_PROCES_PERM
  182. insert into `CRM_PROCES_idx_GROUP_to_PROCES_PERM` ( `ID_PROCES`, `ID_GROUP`, `HAS_PERM_R`, `HAS_PERM_X` )
  183. select i.`ID_PROCES` as `_ID_PROCES`
  184. , z.`ID` as `_ID_GROUP`
  185. , IF( przyp.`FORM_TREAT` & 2, 1, 0 ) as `_HAS_PERM_R`
  186. , IF( przyp.`FORM_TREAT` & 8, 1, 0 ) as `_HAS_PERM_X`
  187. from `CRM_PROCES_idx` i
  188. join `CRM_WSKAZNIK` wsk on( wsk.`ID_PROCES` = i.`idx_PROCES_WITH_GROUPS_ID`
  189. and wsk.`A_STATUS` in ( 'NORMAL', 'WAITING' )
  190. )
  191. join `CRM_LISTA_ZASOBOW` as z on ( z.`ID` = wsk.`ID_ZASOB`
  192. and z.`TYPE` in ( 'STANOWISKO', 'PODMIOT', 'DZIAL' )
  193. and z.`A_STATUS` in ( 'NORMAL', 'WAITING' )
  194. )
  195. join `CRM_PRZYPADEK` przyp on ( przyp.`ID` = wsk.`ID_PRZYPADEK` )
  196. -- where i.`TYPE` in('PROCES_INIT', 'GOTO_AND_RETURN')
  197. ON DUPLICATE KEY UPDATE `HAS_PERM_R` = GREATEST( `HAS_PERM_R`, IF( przyp.`FORM_TREAT` & 2, 1, 0 ) )
  198. , `HAS_PERM_X` = GREATEST( `HAS_PERM_X`, IF( przyp.`FORM_TREAT` & 8, 1, 0 ) )
  199. ;
  200. -- CONF.2--set-end-time-in-config
  201. replace into `CRM_CONFIG` ( `conf_key`, `conf_val` ) values ( @conf_last_exec_lock_key, '' ), ( @conf_last_exec_end_key, NOW() );
  202. -- clear msg 'Update perms' from Msgs system
  203. CALL CRM_UI_MSGS__markTableEveryoneAsExecuted( 'update_proces_init_idx_rec', 'FixCrmProcesInitIdx', 'Update perms', 'CRM_PROCES' );
  204. END ; END IF ;
  205. END ;