Explorar el Código

BiAuditGenerate - wszystko działa na refach

Mariusz Muszyński hace 8 años
padre
commit
ebf0dce708
Se han modificado 1 ficheros con 75 adiciones y 33 borrados
  1. 75 33
      SE/se-lib/Route/UrlAction/BiAuditGenerate.php

+ 75 - 33
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -6,6 +6,7 @@ Lib::loadClass('FileUploader');
 Lib::loadClass('ProcesHelper');
 Lib::loadClass('SchemaFactory');
 Lib::loadClass('Core_AclHelper');
+Lib::loadClass('Core_AclSimpleSchemaBase');
 
 class Route_UrlAction_BiAuditGenerate extends RouteBase {
 
@@ -64,7 +65,6 @@ class Route_UrlAction_BiAuditGenerate extends RouteBase {
 		$acl = Core_AclHelper::getAclByNamespace('default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY');
 		$queryFeatures = $acl->buildQuery(['limit' => 0]);
 		$pracownicy = $queryFeatures->getItems();
-//echo "<pre>"; print_r($pracownicy); echo "</pre>";
 		foreach ($pracownicy as $pracownik) {
 ?>
             <tr>
@@ -122,10 +122,11 @@ function toggle(source) {
 		$sqlArr = [
 			'ID' => $this->powiazanie['ID'],
 			'FILE_STATUS' => 'TO_GENERATE',
-			'BI_analiza_employees' => implode(',', $prID),	// TODO do wyjebania
 		];
 		if ($reloadCache == 'on') $sqlArr['BI_analiza_reloadCache'] = 'Y';
 
+		$this->truncatePowiazaniaFromDB($this->powiazanie['ID']);
+
 		$refPowiazaniaToPracownicy = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA',
 			'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY');
 		foreach ($prID as $v) DB::getPDO()->insert($refPowiazaniaToPracownicy, ['PRIMARY_KEY' => $this->powiazanie['ID'], 'REMOTE_PRIMARY_KEY' => $v]);
@@ -219,31 +220,70 @@ function toggle(source) {
 		SE_Layout::dol();
 	}
 
-	public function doGenerate() {
-
-		function getRefsTables($namespace) {
-			try {
-				$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
-				if (!($item['hasStruct'] && $item['isStructInstalled'] && $item['isObjectActive'])) throw new Exception("Object error");
-				$fields = $item['field'];
-				$refsTables = [];
-				foreach ($fields as $field) {
-					list($type, $child) = explode(":", $field['xsdType'], 2);
-					if ($type == "ref" && $field['isActive'] && (!$field['isLocal'])) $refsTables[] = ACL::getRefTable($ns, $child);
-				}
-				return $refsTables;
-			} catch (Exception $e) {
-				echo "{$ns} - {$e->getMessage}";
-				return [];
+	private function getRefsTables($namespace) {
+		try {
+			$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
+			if (!($item['hasStruct'] && $item['isStructInstalled'] && $item['isObjectActive'])) throw new Exception("Object error");
+			$fields = $item['field'];
+
+			$refsTables = [];
+			foreach ($fields as $field) {
+				list($type, $child) = explode(":", $field['xsdType'], 2);
+				if ($type == "ref" && $field['idZasob'] && $field['isActive'] && (!$field['isLocal'])) $refsTables[] = ACL::getRefTable($namespace, $child);
 			}
+			return $refsTables;
+		} catch (Exception $e) {
+			echo "{$namespace} - {$e->getMessage}";
+			return [];
 		}
+	}
 
-		function deleteResultsFromDB($ID) {
-//DUPA
-			$query = "delete from row, ref using BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row row join `CRM__#REF_TABLE__25` ref on row.ID=ref.REMOTE_PRIMARY_KEY where ref.PRIMARY_KEY='{$ID}'";
-			DB::getPDO()->query($query);
+	private function truncatePowiazaniaFromDB($ID) {
+		$refsTables = $this->getRefsTables('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA');
+		if (!$refsTables) return null;
+		$refPowiazaniaToPowiazaniaRow = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA',
+			'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row');
+		$refsTables = array_diff($refsTables, [$refPowiazaniaToPowiazaniaRow]);
+
+		$query = "delete from `" . implode("`, `", $refsTables) . "` using `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA`";
+		foreach ($refsTables as $refTable) $query .= " left join `{$refTable}` on `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA`.`ID` = `{$refTable}`.`PRIMARY_KEY`";
+
+		$query .= " where `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA`.`ID` = '{$ID}'";
+		DB::getPDO()->query($query);
+	}
+
+	private function deleteResultsFromDB($ID) {
+		$refPowiazaniaToPowiazaniaRow = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA',
+			'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row');
+		$refPowiazaniaRowToPowiazaniaRowObject = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
+			'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object');
+		$refsTables = $this->getRefsTables('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object');
+
+		$sqlDeleteFrom = "delete from
+`{$refPowiazaniaToPowiazaniaRow}`,
+`BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row`,
+`{$refPowiazaniaRowToPowiazaniaRowObject}`,
+`BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object`";
+		$sqlUsing = "
+using `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA`
+join `{$refPowiazaniaToPowiazaniaRow}` on `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA`.`ID` = `{$refPowiazaniaToPowiazaniaRow}`.`PRIMARY_KEY`
+join `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row` on `{$refPowiazaniaToPowiazaniaRow}`.`REMOTE_PRIMARY_KEY` = `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row`.`ID`
+join `{$refPowiazaniaRowToPowiazaniaRowObject}` on `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row`.ID = `{$refPowiazaniaRowToPowiazaniaRowObject}`.`PRIMARY_KEY`
+join `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object` on `{$refPowiazaniaRowToPowiazaniaRowObject}`.`REMOTE_PRIMARY_KEY` = `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object`.ID";
+		$sqlWhere = "
+where `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA`.`ID` = '{$ID}'";
+
+		foreach ($refsTables as $refTable) {
+			$sqlDeleteFrom .= ",\n`{$refTable}`";
+			$sqlUsing .= "\nleft join `{$refTable}` on `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object`.`ID` = `{$refTable}`.`PRIMARY_KEY`";
 		}
 
+		$query = $sqlDeleteFrom . $sqlUsing . $sqlWhere;
+		DB::getPDO()->query($query);
+	}
+
+	public function doGenerate() {
+
 		function generatePhpScript($function) {
 			return '<?php
 ini_set("memory_limit", "4G");
@@ -360,7 +400,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 
 			### Generowanie powiązań
 			if ($doGenerate) {
-				$query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where FILE_STATUS = 'TO_GENERATE' and ID = 1"; //TODO usunac and ID = 1
+				$query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where FILE_STATUS = 'TO_GENERATE'"; //TODO TEST: and ID = 1
 				$result = DB::getPDO()->fetchAll($query);
 				foreach ($result as $row) {
 					$sqlArr = [
@@ -369,7 +409,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 						'FILE_STATUS_info' => 'W trakcie generowania powiązań',
 					];
 					DB::getDB()->UPDATE_OBJ('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', $sqlArr);
-					deleteResultsFromDB($row['ID']);
+					$this->deleteResultsFromDB($row['ID']);
 
 					$generatePowiazaniaResultFile = "{$tasksDirLocation}/generatePowiazania-{$row['ID']}.result";
 					$generatePowiazaniaPhpFile = "{$tasksDirLocation}/generatePowiazania-{$row['ID']}.php";
@@ -538,7 +578,8 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 		$queries[] = "update BI_audit_ALL set nrDomu = substring(nrDomu, 1, position('/' in nrDomu) - 1), nrLokalu = substring(nrDomu, position('/' in nrDomu ) + 1) where nrDomu rlike '^[[:digit:]]+.*/[[:digit:]]+.*$' and nrLokalu is null and BASE = 0";
 		$queries[] = "set @var = (select round(coalesce(max(ID), '0.5')) from BI_audit_ALL_ref_RELATIONS)";
 		$queries[] = "insert into BI_audit_ALL_ref_RELATIONS (ID, RELATION) select @var:=@var*2, a.RELATION from (select a.REMOTE_TABLE as RELATION from BI_audit_ALL a where a.REMOTE_TABLE not in ('BI_audit_ENERGA_RUM_KONTRAHENCI', 'BI_audit_KW_requested_person') group by a.REMOTE_TABLE) as a left join BI_audit_ALL_ref_RELATIONS r on a.RELATION = r.RELATION where r.RELATION is null";
-		$queries[] = "insert into BI_audit_ALL_ref (ID1, ID2, RELATION_ID) select all1.ID, all2.ID, rel.ID from BI_audit_ALL as all1 join `CRM__#REF_TABLE__27` as ref on all1.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY_adresy' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID join BI_audit_ALL_ref_RELATIONS rel on all1.REMOTE_TABLE = rel.RELATION";
+		$refPracownicyToPracownicyAdresy = ACL::getRefTable('default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY', "default_db__x3A__BI_audit_ENERGA_PRACOWNICY_adresy:BI_audit_ENERGA_PRACOWNICY_adresy");
+		$queries[] = "insert into BI_audit_ALL_ref (ID1, ID2, RELATION_ID) select all1.ID, all2.ID, rel.ID from BI_audit_ALL as all1 join `{$refPracownicyToPracownicyAdresy}` as ref on all1.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY_adresy' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID join BI_audit_ALL_ref_RELATIONS rel on all1.REMOTE_TABLE = rel.RELATION";
 
 		foreach ($queries as $query) DB::getPDO()->query($query);
 		$BiAuditRelations = new BiAuditRelations();
@@ -554,7 +595,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 		$reloadCacheResultFile = $tasksDirLocation . "/reloadCache.result";
 		file_put_contents($reloadCachePidFile, getmypid());
 		try {
-//			$this->reloadCache_reTeryt();	// TODO remove comment
+			$this->reloadCache_reTeryt();
 			$this->reloadCache_updateAll();
 			file_put_contents($reloadCacheResultFile, "ok");
 		} catch (Exception $e) {
@@ -736,14 +777,12 @@ class BiAuditPowiazania {
 
 	public function __construct($ID = 0) {
 		if (!$ID) throw new Exception("Wrong ID parameter");
-		$query = "select BI_analiza_depth, BI_analiza_employees from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where ID = '{$ID}' and FILE_STATUS = 'IN_PROGRESS' and BI_analiza_reloadCache != 'Y'";
+		$query = "select BI_analiza_depth from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where ID = '{$ID}' and FILE_STATUS = 'IN_PROGRESS' and BI_analiza_reloadCache != 'Y'";
 		$result = DB::getPDO()->fetchAll($query);
 		if (!$result) throw new Exception("Błąd danych");
-		$powiazanie = $result[0];
-		$this->DEPTH = (int) $powiazanie['BI_analiza_depth'];
+		$this->DEPTH = (int) $result[0]['BI_analiza_depth'];
 		$this->ID = $ID;
 
-		// TODO przerobic na refy
 		$subQueries = [];
 		foreach ($this->destTables as $destTable) {
 			$refTable = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', "default_db__x3A__{$destTable}:{$destTable}");
@@ -809,6 +848,9 @@ class BiAuditPowiazania {
 		$kontrahenci = [];
 		$kw_person = [];
 
+		$refUmowyToKontrahenci = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_UMOWY/BI_audit_ENERGA_RUM_UMOWY', 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI');
+		$refKwRequestedToKwRequestedPerson = ACL::getRefTable('default_db/BI_audit_KW_requested/BI_audit_KW_requested', 'default_db__x3A__BI_audit_KW_requested_person:BI_audit_KW_requested_person');
+
 		foreach ($this->results as $result) {
 			$item_results = [];
 			foreach ($result as $ID => $rel) {
@@ -821,7 +863,7 @@ class BiAuditPowiazania {
 				$item_results['object'][] = $item_result;
 				if ($row['REMOTE_TABLE'] == "BI_audit_ENERGA_RUM_KONTRAHENCI" && (!in_array($row['REMOTE_ID'], $kontrahenci))) {
 					$kontrahenci[] = $row['REMOTE_ID'];
-					$query = "select umowy.* from BI_audit_ENERGA_RUM_UMOWY umowy join `CRM__#REF_TABLE__23` ref on umowy.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
+					$query = "select umowy.* from BI_audit_ENERGA_RUM_UMOWY umowy join `{$refUmowyToKontrahenci}` ref on umowy.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
 					$res = DB::query($query);
 					if (mysql_num_rows($res)) {
 						$umowy = [];
@@ -832,7 +874,7 @@ class BiAuditPowiazania {
 					}
 				} elseif ($row['REMOTE_TABLE'] == "BI_audit_KW_requested_person" && (!in_array($row['REMOTE_ID'], $kw_person))) {
 					$kw_person[] = $row['REMOTE_ID'];
-					$query = "select kw.* from BI_audit_KW_requested kw join `CRM__#REF_TABLE__24` ref on kw.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
+					$query = "select kw.* from BI_audit_KW_requested kw join `{$refKwRequestedToKwRequestedPerson}` ref on kw.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
 					$res = DB::query($query);
 					if (mysql_num_rows($res)) {
 						$kw = [];
@@ -861,7 +903,7 @@ class BiAuditPowiazania {
 			foreach ($result as $ID => $rel) {
 				$query = "select REMOTE_TABLE, REMOTE_ID from BI_audit_ALL where ID = '{$ID}'";
 				$object = DB::getPDO()->fetchFirst($query);
-				$idPowiazaniaRowObject = DB::getPDO()->insert('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object', ['relation_from' => $rel]);
+				$idPowiazaniaRowObject = DB::getPDO()->insert('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object', ['relation_from' => $this->relations[$rel]]);
 				DB::getPDO()->insert($refPowiazaniaRowToPowiazaniaRowObject, ['PRIMARY_KEY' => $IdPowiazaniaRow, 'REMOTE_PRIMARY_KEY' => $idPowiazaniaRowObject]);
 				$ref = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
 					"default_db__x3A__{$object['REMOTE_TABLE']}:{$object['REMOTE_TABLE']}");