Kaynağa Gözat

Implementacja refów all to all dla BiAuditGenerate

Mariusz Muszyński 8 yıl önce
ebeveyn
işleme
d74e8232b5
1 değiştirilmiş dosya ile 27 ekleme ve 0 silme
  1. 27 0
      SE/se-lib/Route/UrlAction/BiAuditGenerate.php

+ 27 - 0
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -1769,6 +1769,7 @@ SQL;
 				foreach ($refTableConf['refTablesTo'] as $refTableTo) {
 					$refTable = ACL::getRefTable("default_db/{$refTableFrom}/{$refTableFrom}", "default_db__x3A__{$refTableTo}:{$refTableTo}");
 					$queries[] = "update `{$refTable}` set `A_STATUS` = 'WAITNG' where `A_STATUS` = 'NORMAL'";
+					$queryes[] = "delete from `{$refTable}` where `TRANSACTION_ID` = '-1'";
 				}
 			}
 		}
@@ -1955,6 +1956,7 @@ SQL;
 
 class BiAuditRelations {
 	private $RELATIONS_ID = [];
+	private $refTables;
 
 	private $relations = [
 		'nip' => ['nip'],
@@ -1968,6 +1970,7 @@ class BiAuditRelations {
 	private $sourceTables = ['BI_audit_ENERGA_PRACOWNICY', 'BI_audit_ENERGA_PRACOWNICY_adresy'];
 
 	public function __construct() {
+		$this->refTables = new BiAuditRefTables();
 		$query = "select * from BI_audit_ALL_ref_RELATIONS";
 		$result = DB::getPDO()->fetchAll($query);
 		foreach ($result as $row) $this->RELATIONS_ID[$row['RELATION']] = $row['ID'];
@@ -1984,6 +1987,14 @@ class BiAuditRelations {
 		return $this->RELATIONS_ID[$name];
 	}
 
+	private function addToRefTable($fromTable, $toTable, $fromID, $toID) {
+		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']);
+			}
+		}
+	}
+
 	public function findRelations($ID) {
 		$query = "select * from BI_audit_ALL where ID = '{$ID}'";
 		if (!($result = DB::getPDO()->fetchAll($query))) return false;
@@ -2020,16 +2031,32 @@ class BiAuditRelations {
 			$end2 = (in_array($row['REMOTE_TABLE_2'], $this->destTables) ? 1 : 0);
 			if (!($start2 || $end1)) {
 				$query = "insert into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, END, BASE) values ({$ID}, {$row['ID']}, '{$relationID}', {$end2}, {$row['BASE']})";
+				$this->addToRefTable($row['REMOTE_TABLE_1'], $row['REMOTE_TABLE_2'], $ID, $row['ID']);
 				DB::getPDO()->query($query);
 			}
 			if (!($start1 || $end2)) {
 				$query = "insert into BI_audit_ALL_ref (ID2, ID1, RELATION_ID, END, BASE) values ({$ID}, {$row['ID']}, '{$relationID}', {$end1}, {$row['BASE']})";
+				$this->addToRefTable($row['REMOTE_TABLE_2'], $row['REMOTE_TABLE_1'], $row['ID'], $ID);
 				DB::getPDO()->query($query);
 			}
 		}
 	}
 }
 
+class BiAuditRefTables {
+	private $refTables = [];
+
+	public function getRefTable($tableFrom, $tableTo) {
+		if (!isset($this->refTables[$tableFrom][$tableTo])) {
+			try {
+				$this->refTables[$tableFrom][$tableTo] = ACL::getRefTable("default_db/{$tableFrom}/{$tableFrom}", "default_db__x3A__{$tableTo}:{$tableTo}");
+			} catch (Exception $e) {
+				$this->refTables[$tableFrom][$tableTo] = false;
+			}
+		}
+		return $this->refTables[$tableFrom][$tableTo];
+	}
+}
 
 class BiAuditPowiazania {