Browse Source

BiAuditGenerate - generowanie extra refów

Mariusz Muszyński 8 years ago
parent
commit
d12d9e1d2c
1 changed files with 67 additions and 103 deletions
  1. 67 103
      SE/se-lib/Route/UrlAction/BiAuditGenerate.php

+ 67 - 103
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -26,6 +26,7 @@ class Route_UrlAction_BiAuditGenerate extends RouteBase {
 	private $powiazanie = null;
 	private $SOURCE = null;
 	private $REFERER;
+	private $refTables;
 
 	public function handleAuth() {
 		if (!User::logged()) {
@@ -33,6 +34,10 @@ class Route_UrlAction_BiAuditGenerate extends RouteBase {
 		}
 	}
 
+	public function __construct() {
+		$this->refTables = new BiAuditRefTables();
+	}
+
 	private static function getDirectory($table, $id) {
 		$folderConf = FoldersConfig::getAll($table.'_COLUMN');
 		$record = new stdClass;
@@ -943,8 +948,6 @@ function validateCompany(source) {
 				default: SE_Layout::alert('danger', 'Błąd parametru');
 			}
 		}
-		elseif(($ID = V::get('doReloadCache', 0, $_GET, 'string')) == 'full')  $this->doReloadCache('full');
-		elseif(($ID = V::get('doReloadCache', 0, $_GET, 'string'))) $this->doReloadCache();
 		else SE_Layout::alert('danger', 'Błąd parametru');
 		SE_Layout::dol();
 	}
@@ -1765,46 +1768,54 @@ SQL;
 
 		];
 		
-		$RefExtraTables = [
-								'BI_audit_MSIG_person' => [
-										'refTablesTo' => [
-												'BI_audit_KRS_person','BI_audit_MSIG','BI_audit_CEIDG','BI_audit_ENERGA_PRACOWNICY',
-														 ],
-								],					
-								'BI_audit_MSIG' => [
-											'refTablesTo' => [
-												'BI_audit_KRS','BI_audit_ENERGA_RUM_KONTRAHENCI',
-											],
-								],
-								'BI_audit_MSIG_address' => [
-									'refTablesTo' => [
-										'BI_audit_KRS_address', 'BI_audit_CEIDG', 'BI_audit_ENERGA_PRACOWNICY_adresy','BI_audit_MSIG_address',
-										],
-								
-								],	
-								'BI_audit_KRS_person' => [
-									'refTablesTo' => [
-										'BI_audit_KRS','BI_audit_ENERGA_PRACOWNICY','BI_audit_MSIG_person',
-										],
-								],	
-								'BI_audit_MSIG_company' => [
-									'refTablesTo' => [
-										'BI_audit_MSIG',
-										],
-								],	
-								'BI_audit_KRS' => [
-									'refTablesTo' => [
-										'BI_audit_MSIG', 'BI_audit_ENERGA_RUM_KONTRAHENCI',
-										],
-								],	
-								'BI_audit_CEIDG' => [
-									'refTablesTo' => [
-										'BI_audit_ENERGA_PRACOWNICY', 'BI_audit_ENERGA_RUM_KONTRAHENCI',
-										],
-								],	
-								
-								
-							];
+		$extraRefTablesConf = [
+			'BI_audit_MSIG_person' => [
+				'refTablesTo' => [
+					'BI_audit_KRS_person',
+					'BI_audit_MSIG',
+					'BI_audit_CEIDG',
+					'BI_audit_ENERGA_PRACOWNICY',
+				],
+			],					
+			'BI_audit_MSIG' => [
+				'refTablesTo' => [
+					'BI_audit_KRS',
+					'BI_audit_ENERGA_RUM_KONTRAHENCI',
+				],
+			],
+			'BI_audit_MSIG_address' => [
+				'refTablesTo' => [
+					'BI_audit_KRS_address',
+					'BI_audit_CEIDG',
+					'BI_audit_ENERGA_PRACOWNICY_adresy',
+					'BI_audit_MSIG_address',
+				],
+			],	
+			'BI_audit_KRS_person' => [
+				'refTablesTo' => [
+					'BI_audit_KRS',
+					'BI_audit_ENERGA_PRACOWNICY',
+					'BI_audit_MSIG_person',
+				],
+			],	
+			'BI_audit_MSIG_company' => [
+				'refTablesTo' => [
+					'BI_audit_MSIG',
+				],
+			],	
+			'BI_audit_KRS' => [
+				'refTablesTo' => [
+					'BI_audit_MSIG',
+					'BI_audit_ENERGA_RUM_KONTRAHENCI',
+				],
+			],	
+			'BI_audit_CEIDG' => [
+				'refTablesTo' => [
+					'BI_audit_ENERGA_PRACOWNICY',
+					'BI_audit_ENERGA_RUM_KONTRAHENCI',
+				],
+			],	
+		];
 		
 		$sourceTables = ['BI_audit_ENERGA_PRACOWNICY'];
 
@@ -1816,14 +1827,22 @@ SQL;
 			foreach ($refTablesConf as $refTableFrom => $refTableConf) {
 				if ($refTableConf['base']) continue;
 				foreach ($refTableConf['refTablesTo'] as $refTableTo) {
-					$refTable = ACL::getRefTable("default_db/{$refTableFrom}/{$refTableFrom}", "default_db__x3A__{$refTableTo}:{$refTableTo}");
+					$refTable = $this->refTables->getRefTable($refTableFrom, $refTableTo);
+					$queries[] = "delete from `{$refTable}` where `TRANSACTION_ID` = '-1'";
 					$queries[] = "update `{$refTable}` set `A_STATUS` = 'WAITNG' where `A_STATUS` = 'NORMAL'";
-					$queryes[] = "delete from `{$refTable}` where `TRANSACTION_ID` = '-1'";
+					$queries[] = "insert into `{$refTable}` (`PRIMARY_KEY`, `REMOTE_PRIMARY_KEY`, `TRANSACTION_ID`, `A_STATUS`) select `all1`.`ID`, `all2`.`ID`, '-1', 'NORMAL' from `BI_audit_ALL` as `all1` join `BI_audit_ALL_ref` as `ref` on `all1`.`ID` = `ref`.`ID1` join `BI_audit_ALL` as `all2` on `ref`.`ID2` = `all2`.`ID` where `all1`.`REMOTE_TABLE` = '{$refTableFrom}' and `all2`.`REMOTE_TABLE` = '{$refTableTo}'";
 				}
 			}
-			
 
-			
+			foreach ($extraRefTablesConf as $refTableFrom => $refTableConf) {
+				foreach ($refTableConf['refTablesTo'] as $refTableTo) {
+					if ($refTable = $this->refTables->getRefTable($refTableFrom, $refTableTo)) {
+						$queries[] = "truncate table `{$refTable}`"; // TODO do wywalenia po pierwszym wyzwoleniu
+						//$queries[] = "delete from `{$refTable}` where `TRANSACTION_ID` = '-1'"; // TODO do odhashowania po pierwszym wyzwoleniu
+						$queries[] = "insert into `{$refTable}` (`PRIMARY_KEY`, `REMOTE_PRIMARY_KEY`, `TRANSACTION_ID`, `A_STATUS`) select `all1`.`ID`, `all2`.`ID`, '-1', 'NORMAL' from `BI_audit_ALL` as `all1` join `BI_audit_ALL_ref` as `ref` on `all1`.`ID` = `ref`.`ID1` join `BI_audit_ALL` as `all2` on `ref`.`ID2` = `all2`.`ID` where `all1`.`REMOTE_TABLE` = '{$refTableFrom}' and `all2`.`REMOTE_TABLE` = '{$refTableTo}'";
+					}
+				}
+			}
 		}
 
 		foreach ($tablesConf as $table => $tableConf) {
@@ -1864,9 +1883,7 @@ SQL;
 		}
 //		echo implode(";\n", $queries) . ";\n";
 		foreach ($queries as $query) {
-			echo "
-			#1842 echo base queries SQL: {$query}\n";
-			error_log("\n #1842 error base queries SQL: ".$query);
+			echo "SQL: {$query}\n";
 			DB::getPDO()->query($query);
 		}
 
@@ -1876,43 +1893,6 @@ SQL;
 		while ($row = $result->fetch()) $BiAuditRelations->findRelations($row['ID']);
 		DB::getPDO()->query("update `BI_audit_ALL` set `reffed` = 1");
 //		DB::getPDO()->query("unlock tables");
-		
-		$queries = array();
-		//uzupelnienie extra refow dla dzialania schematow
-		foreach ($RefExtraTables as $refTableFrom => $refTableConf) {
-					foreach ($refTableConf['refTablesTo'] as $refTableTo) {
-						echo "
-						#1858 looking for ACL::getRefTable {$refTableFrom} to {$refTableTo}";
-					$refTable = ACL::getRefTable("default_db/{$refTableFrom}/{$refTableFrom}", "default_db__x3A__{$refTableTo}:{$refTableTo}");
-						echo "
-						#1860 found getRefTable={$getRefTable}";
-					$queries[] = "update `{$refTable}` set `A_STATUS` = 'WAITNG' "; //where `A_STATUS` = 'NORMAL'
-					$queries[] = "delete from `{$refTable}` where `TRANSACTION_ID` = '0' and `A_STATUS` = 'NORMAL' "; //-1 nie moze byc
-					$queries[] = " insert into `{$refTable}` (
-									  `PRIMARY_KEY` ,
-									  `REMOTE_PRIMARY_KEY`
-									) 
-									 select 
-										LT.REMOTE_ID as PRIMARY_KEY, RT.REMOTE_ID as REMOTE_PRIMARY_KEY 
-										from BI_audit_ALL as LT  
-										left join BI_audit_ALL_ref as LT_ref on ( LT_ref.ID1=LT.ID ) 
-										left join BI_audit_ALL as RT on (  LT_ref.ID2 = RT.ID  )
-									 where 
-										LT.REMOTE_TABLE='{$refTableFrom}' 
-										and RT.REMOTE_TABLE='{$refTableTo}' ";
-				}
-		}
-		
-		print_r($queries);
-		
-		foreach ($queries as $ind=>$query) {
-			echo "
-			#1878 echo extra refs SQL:[{$ind}] {$query}\n";
-			error_log("\n 
-			#1878 error extra refs SQL:[{$ind}] ".$query);
-			DB::getPDO()->query($query);
-		}
-		
 	}
 
 	public static function doReloadCache($full = false) {
@@ -1943,10 +1923,6 @@ SQL;
 			else throw new Exception($e->getMessage());
 		}
 	}
-	
-	
-	
-	
 
 	public static function doGeneratePowiazania($ID) {
 		$powiazaniaDirLocation = self::getMainDirectory();
@@ -2089,16 +2065,7 @@ class BiAuditRelations {
 		if ($refTable = $this->refTables->getRefTable($fromTable, $toTable)) {
 			if (!DB::getPDO()->fetchValue("select count(*) from `{$refTable}` where `PRIMARY_KEY` = '{$fromID}' and `REMOTE_PRIMARY_KEY` = '{$toID}' and `A_STATUS` != 'DELETED'")) {
 				DB::getPDO()->insert($refTable, ['PRIMARY_KEY' => $fromID, 'REMOTE_PRIMARY_KEY' => $toID, 'A_STATUS' => 'NORMAL', 'TRANSACTION_ID' => '-1']);
-			} else  { error_log('\n
-				#2000 error DEBUG found some records in REFs for table fromTable:'.$fromTable.' toTable:'.$toTable); 
-				echo '\n
-				#2000 echo DEBUG found some records in REFs for table fromTable:'.$fromTable.' toTable:'.$toTable;
 			}
-		} else { 
-		//error_log('\n
-		//	#2001 error DEBUG not found ref table fromTable:'.$fromTable.' toTable:'.$toTable);
-		//		 echo '\n
-		//		 #2001 echo DEBUG not found ref table fromTable:'.$fromTable.' toTable:'.$toTable ;
 		}
 	}
 
@@ -2122,10 +2089,7 @@ class BiAuditRelations {
 				$items .= ", if(" . implode(" and ", $join) . ", 1, 0) as {$name}";
 			}
 		}
-		if (!$joins) { 
-			//error_log('#2032 not found joins for ');
-			return false;
-		}
+		if (!$joins) return false;
 
 		$query = "select t2.ID as ID, t1.REMOTE_TABLE as REMOTE_TABLE_1, t2.REMOTE_TABLE as REMOTE_TABLE_2 {$items}, t1.BASE * t2.BASE as BASE " .
 			"from BI_audit_ALL t1 join BI_audit_ALL t2 on " . implode(" or ", $joins) . " where t1.ID = {$ID} and (t2.reffed = 1 or t2.ID > {$ID})";