Created
December 4, 2024 07:13
-
-
Save defrindr/a07e10358e1a9949f27b3b71facf4f83 to your computer and use it in GitHub Desktop.
controllers/api/ToolController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// controllers/api/ToolController.php | |
namespace app\controllers\api; | |
/** | |
* This is the class for REST controller "ELogController". | |
*/ | |
use app\components\Maximo; | |
use app\models\CbmVibrasiImage; | |
use app\models\ELogImage; | |
use app\models\ELogRbdImage; | |
use app\models\Groups; | |
use app\models\GroupSystemAccess; | |
use app\models\UserGroup; | |
use Yii; | |
use yii\filters\AccessControl; | |
use yii\helpers\ArrayHelper; | |
use yii\web\Response; | |
class ToolController extends \yii\rest\Controller | |
{ | |
public function actionIndex() | |
{ | |
try { | |
//code... | |
$conmonKks = Yii::$app->dbConmon->createCommand('select asset_num from asset')->queryColumn(); | |
$conn = Maximo::createConnection(); | |
// $data = Maximo::fetchAll($conn, "SELECT ASSETNUM,PARENT,DESCRIPTION from ASSET | |
// WHERE | |
// \"HIERARCHY\"='EQUIPMENT' | |
// AND | |
// SITEID='TA' | |
// AND ( | |
// DESCRIPTION like 'UNIT 1%' | |
// or DESCRIPTION like 'UNIT 2%' | |
// or DESCRIPTION like 'COMMON%' | |
// )"); | |
$kksList = implode(",", array_map(function ($item) { | |
return "'$item'"; | |
}, $conmonKks)); | |
$query = "SELECT ASSETNUM,PARENT,DESCRIPTION from MAXIMO.ASSET | |
WHERE ASSETNUM in (" . $kksList . ")"; | |
$data = Maximo::fetchAll($conn, $query); | |
$groups = Maximo::fetchAll($conn, "SELECT * FROM MAXIMO.ASSET WHERE ASSETNUM IN (SELECT PARENT from MAXIMO.ASSET | |
WHERE ASSETNUM in (" . $kksList . ") GROUP BY PARENT)"); | |
$parents = []; | |
foreach ($groups['ASSETNUM'] as $index => $assetnum) { | |
$raw_name = $groups['DESCRIPTION'][$index]; | |
$names = explode("-", $raw_name); | |
$highest_haerarki = strtoupper(trim($names[0])); | |
$name = trim($names[1]); | |
if (!in_array($highest_haerarki, ["COMMON", "UNIT 1", "UNIT 2"])) { | |
$spliting_string = strtoupper(substr($highest_haerarki, 0, 6)); | |
if (in_array($spliting_string, ["COMMON", "UNIT 1", "UNIT 2"])) { | |
$highest_haerarki = $spliting_string; | |
$name = trim(substr($highest_haerarki, 6)); | |
} else { | |
$highest_haerarki = "COMMON"; | |
// var_dump($raw_name); | |
// continue; | |
} | |
} | |
if ($assetnum == "19080000PBV02") { | |
$name = trim($names[2]); | |
} | |
if ($assetnum == "19080010HHE44") { | |
$name = trim(substr($raw_name, 6)); | |
} | |
if ($assetnum == "19080010HHE61") { | |
$name = trim(substr($raw_name, 6)); | |
// var_dump($name);die; | |
} | |
if ($assetnum == "19080010HHE11") { | |
$name = trim(explode("-", substr($raw_name, 6))[0]); | |
// var_dump($name); | |
// die; | |
} | |
if ($assetnum == "19080020HHE44") { | |
$name = trim(explode("-", substr($raw_name, 6))[0]); | |
// var_dump($name); | |
// die; | |
} | |
if ($assetnum == "19080020HHE23") { | |
$name = trim(explode("-", substr($raw_name, 6))[0]); | |
} | |
// if ($assetnum == "19080010HHE53") { | |
// // $name = trim(explode("-", substr($raw_name, 6))[0]); | |
// var_dump($raw_name); | |
// die; | |
// } | |
$parents[$assetnum] = [ | |
'no_kks' => $assetnum, | |
'parent' => $highest_haerarki, | |
'name' => $name, | |
"childs" => [] | |
]; | |
} | |
// die; | |
foreach ($data['ASSETNUM'] as $index => $assetnum) { | |
$raw_name = $data['DESCRIPTION'][$index]; | |
$parent_id = $data['PARENT'][$index]; | |
$names = explode("-", $raw_name); | |
$highest_haerarki = strtoupper(trim($names[0])); | |
$name = trim($names[1]); | |
if (!in_array($highest_haerarki, ["COMMON", "UNIT 1", "UNIT 2"])) { | |
$spliting_string = strtoupper(substr($highest_haerarki, 0, 6)); | |
if (in_array($spliting_string, ["COMMON", "UNIT 1", "UNIT 2"])) { | |
$highest_haerarki = $spliting_string; | |
$name = trim(substr($highest_haerarki, 6)); | |
} else { | |
// var_dump($raw_name); | |
$highest_haerarki = "COMMON"; | |
// continue; | |
} | |
} | |
if (isset($parents[$parent_id])) { | |
$parents[$parent_id]['childs'][] = [ | |
'no_kks' => $assetnum, | |
'parent' => $highest_haerarki, | |
'name' => $name, | |
]; | |
} | |
} | |
// die; | |
$list_unit = []; | |
$list_system = []; | |
$list_equipments = []; | |
foreach ($parents as $id_system => $system) { | |
$list_unit[$system['parent']] = $system['parent']; | |
foreach ($system['childs'] as $equipments) { | |
$list_system[$id_system] = [ | |
"reference_id" => $system['parent'], | |
"no_kks" => $system['no_kks'], | |
"name" => $system['name'], | |
]; | |
$list_equipments[$equipments['no_kks']] = [ | |
"reference_id" => $system['no_kks'], | |
"no_kks" => $equipments['no_kks'], | |
"name" => $equipments['name'] | |
]; | |
} | |
} | |
foreach ($list_unit as $index => $unit) { | |
$data = Groups::find()->andWhere(['name' => $unit, 'group_type_id' => Groups::TYPE_UNIT])->one(); | |
if (!$data) { | |
$data = new Groups(); | |
$data->name = $unit; | |
$data->group_type_id = Groups::TYPE_UNIT; | |
$data->save(); | |
} | |
$list_unit[$index] = $data; | |
} | |
foreach ($list_system as $index => $system) { | |
$data = Groups::find()->andWhere(['no_kks' => $system['no_kks'], 'group_type_id' => Groups::TYPE_SYSTEM])->one(); | |
if (!$data) { | |
$data = new Groups(); | |
$data->name = $system['name']; | |
$data->no_kks = $system['no_kks']; | |
$data->reference_id = $list_unit[$system['reference_id']]->id; | |
$data->group_type_id = Groups::TYPE_SYSTEM; | |
if (!$data->save()) { | |
// var_dump($data->no_kks); | |
// var_dump($data->getErrors()); | |
// die; | |
} | |
} | |
$list_system[$index] = $data; | |
} | |
foreach ($list_equipments as $index => $equipment) { | |
$data = Groups::find()->andWhere(['no_kks' => $equipment['no_kks'], 'group_type_id' => Groups::TYPE_EQUIPMENT])->one(); | |
if (!$data) { | |
$data = new Groups(); | |
$data->name = $equipment['name']; | |
$data->no_kks = $equipment['no_kks']; | |
$reference = $list_system[$equipment['reference_id']]; | |
if (!$reference->id) { | |
// $group = Maximo::fetchOne($conn, "SELECT ASSETNUM,DESCRIPTION FROM ASSET WHERE ASSETNUM = '{$equipment['reference_id']}'"); | |
// var_dump($group); | |
// die; | |
// $reference = Groups::find()->andWhere(['no_kks' => $equipment['reference_id']])->one(); | |
// var_dump($reference); | |
// die; | |
// var_dump("SKIP : " . $equipment['no_kks']); | |
continue; | |
} | |
$data->reference_id = $reference->id; | |
$data->group_type_id = Groups::TYPE_EQUIPMENT; | |
$data->save(); | |
} | |
$list_system[$index] = $data; | |
} | |
Yii::$app->response->format = Response::FORMAT_JSON; | |
return ['success' => true]; | |
} catch (\Throwable $th) { | |
throw $th; | |
} | |
} | |
public function actionGetParam($id) | |
{ | |
// "19080000LDN20AP001"; | |
$availablePoints = Maximo::getAvailablePoint($id); | |
var_dump($availablePoints); | |
die; | |
} | |
public function actionAddImage() | |
{ | |
try { | |
$groups = Groups::find()->andWhere( | |
[ | |
'and', | |
['group_type_id' => Groups::TYPE_EQUIPMENT], | |
['not in', 'id', CbmVibrasiImage::find()->select('group_id')->distinct()] | |
] | |
)->all(); | |
// $sql = ""; | |
foreach ($groups as $group) { | |
$sql = " | |
INSERT INTO `cbm_vibrasi_image` (`image_id`, `group_id`, `parameter`, `name`, `function`, `style`) VALUES | |
(2, " . $group->id . ", 'PIH', 'PIH', 'maximo', 'top: 76.39999533691434%;left: 4.674601806614907%;width: 14.691605677932564%;height: 4.999999694824235%;'), | |
(2, " . $group->id . ", 'PIV', 'PIV', 'maximo', 'top: 82.39999497070343%;left: 4.674601806614907%;width: 14.765805706608992%;height: 5.199999682617195%;'), | |
(2, " . $group->id . ", 'POH', 'POH', 'maximo', 'top: 73.3999955200198%;left: 29.976811585276547%;width: 14.91420576396184%;height: 4.999999694824235%;'), | |
(2, " . $group->id . ", 'POV', 'POV', 'maximo', 'top: 79.39999515380889%;left: 30.051011613952976%;width: 14.765805706608989%;height: 5.199999682617204%;'), | |
(2, " . $group->id . ", 'CIH', 'CIH', 'maximo', 'top: 11.99999926757817%;left: 39.47441525585922%;width: 14.54320562057971%;height: 4.399999731445329%;'), | |
(2, " . $group->id . ", 'CIV', 'CIV', 'maximo', 'top: 18.199998889160224%;left: 39.25181516982993%;width: 14.765805706608997%;height: 4.99999969482424%;'), | |
(2, " . $group->id . ", 'COH', 'COH', 'maximo', 'top: 76.79999531250029%;left: 55.279021363938185%;width: 14.765805706608989%;height: 4.199999743652361%;'), | |
(2, " . $group->id . ", 'COV', 'COV', 'maximo', 'top: 82.59999495849641%;left: 55.353221392614614%;width: 14.765805706608989%;height: 4.9999996948242265%;'), | |
(2, " . $group->id . ", 'CIA', 'CIA', 'maximo', 'top: 24.199998522949308%;left: 39.17761514115351%;width: 14.765805706608997%;height: 4.99999969482424%;'), | |
(2, " . $group->id . ", 'MIH', 'MIH', 'maximo', 'top: 12.199999255371141%;left: 61.51182377275807%;width: 14.765805706608989%;height: 4.199999743652358%;'), | |
(2, " . $group->id . ", 'MIV', 'MIV', 'maximo', 'top: 17.999998901367253%;left: 61.43762374408165%;width: 14.765805706608989%;height: 4.799999707031267%;'), | |
(2, " . $group->id . ", 'MOH', 'MOH', 'maximo', 'top: 17.399998937988347%;left: 83.54923228965691%;width: 14.914205763961853%;height: 4.79999970703127%;'), | |
(2, " . $group->id . ", 'MOV', 'MOV', 'maximo', 'top: 23.799998547363373%;left: 83.62343231833333%;width: 14.543205620579707%;height: 4.599999719238298%;');"; | |
Yii::$app->db->createCommand($sql)->execute(); | |
} | |
$groups = Groups::find()->andWhere( | |
[ | |
'and', | |
['group_type_id' => Groups::TYPE_EQUIPMENT], | |
['not in', 'id', ELogRbdImage::find()->select('group_id')->distinct()] | |
] | |
)->all(); | |
// $sql = ""; | |
foreach ($groups as $group) { | |
$elogImage = new ELogRbdImage(); | |
$elogImage->image = 2; | |
$elogImage->group_id = $group->id; | |
$elogImage->save(); | |
} | |
} catch (\Throwable $th) { | |
dd($th); | |
} | |
} | |
public function actionUserGroup() | |
{ | |
$sistem = Groups::find()->andWhere(['group_type_id' => Groups::TYPE_SYSTEM])->all(); | |
foreach ($sistem as $sis) { | |
$user = new GroupSystemAccess(); | |
$user->user_group_id = 1; | |
$user->group_id = $sis->id; | |
$user->save(); | |
} | |
Yii::$app->response->format = Response::FORMAT_JSON; | |
return ['success' => true]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment