华熙生物

<?php

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Domain\Asset\Register\UseCase\CreateAsset;

require_once __DIR__ . '/../../../../api/v1/vendor/autoload.php';
require_once __DIR__ . '/../../../../functions/nestedset_assets.php';

$log = new Logger('log');
$month = date('Y-m');
$log->pushHandler(new StreamHandler(__DIR__ . '/log/22152log' . $month . '.log', Logger::DEBUG));

$post = @file_get_contents('php://input');  //获取请求体,@的作用为屏蔽警告,可去除。
$post =  json_decode($post, true);

$data = $post[0];
$log->info("asset_code的值" . $data['asset_code'] . ";" . "asset_service_start_date 得值:" . $data['asset_service_start_date']);

if (empty($data['asset_code'])) {
    Flight::json(['message' => 'asset_code is empty'], 401);
    exit();
}
if (empty($data['asset_service_start_date'])) {
    Flight::json(['message' => 'asset_service_start_date is empty'], 401);
    exit();
}
if (!isset($data['criticality_code'])) {
    $data['criticality_code'] = 0;
}
if (!isset($data['function_code'])) {
    $data['function_code'] = 0;
}
if (!isset($data['asset_ff_1'])) {
    $data['asset_ff_1'] = '';
}
if (!isset($data['asset_ff_2'])) {
    $data['asset_ff_2'] = '';
}
if (!isset($data['asset_acquisition_price'])) {
    $data['asset_acquisition_price'] = '';
}
if (!isset($data['asset_model'])) {
    $data['asset_model'] = '';
}
if (!isset($data['asset_description'])) {
    $data['asset_description'] = '';
}
if (!isset($data['asset_serial_number'])) {
    $data['asset_serial_number'] = '';
}
if (!isset($data['asset_manufacture_date'])) {
    $data['asset_manufacture_date'] = '';
}
if (!isset($data['asset_installation_date'])) {
    $data['asset_installation_date'] = '';
}
if (!isset($data['asset_warranty_date'])) {
    $data['asset_warranty_date'] = '';
}
if (!isset($data['supplier_code'])) {
    $data['supplier_code'] = '';
}
if (!isset($data['manufacturer_code'])) {
    $data['manufacturer_code'] = '';
}

//criticality_code  换取id
$criticality_query = $DB->prepare("
    SELECT  criticality_id
    FROM    mic_criticality
    WHERE criticality_code = :criticality_code
");
$criticality_query->bindvalue(':criticality_code', $data['criticality_code']);

if (!$criticality_query->execute()) {
    Flight::error(new Exception(errorInfo($criticality_query)));
}

$criticality_line = $criticality_query->fetch(PDO::FETCH_OBJ);
$criticality_id = $criticality_line->criticality_id;

//function_code 换取id
$function_query = $DB->prepare("
        SELECT  function_id
        FROM    mic_function
        WHERE function_code = :function_code
");
$function_query->bindvalue(':function_code', $data['function_code']);

if (!$function_query->execute()) {
    Flight::error(new Exception(errorInfo($function_query)));
}

$function_line = $function_query->fetch(PDO::FETCH_OBJ);
$function_id = $function_line->function_id;

// 用location_code 换取id,
$location_query = $DB->prepare("
    SELECT  location_id
    FROM    asset_location
    WHERE location_code = :location_code
");
$location_query->bindvalue(':location_code', $data['location_code']);

if (!$location_query->execute()) {
    Flight::error(new Exception(errorInfo($location_query)));
}

$location_line = $location_query->fetch(PDO::FETCH_OBJ);
$location_id = $location_line->location_id;

// 大类code换去大类id
$category_query = $DB->prepare("
    SELECT  asset_category_id
    FROM    mic_asset_category
    WHERE asset_category_code = :asset_category_code
");
$category_query->bindvalue(':asset_category_code', $data['asset_category_code']);

if (!$category_query->execute()) {
    Flight::error(new Exception(errorInfo($category_query)));
}
$category_line = $category_query->fetch(PDO::FETCH_OBJ);
$asset_category_id = $category_line->asset_category_id;

// 根据供应商code 获取供应商ID
$pur_supplier_query = $DB->prepare("
        SELECT  supplier_id
        FROM    pur_supplier
        WHERE supplier_code = :supplier_code
");
$pur_supplier_query->bindvalue(':supplier_code', $data['supplier_code']);

if (!$pur_supplier_query->execute()) {
    Flight::error(new Exception(errorInfo($pur_supplier_query)));
}elseif ($pur_supplier_query->rowCount() == 0) {
    $supplier_id =0;
} else {
    $pur_line = $pur_supplier_query->fetch(PDO::FETCH_OBJ);
    $supplier_id = $pur_line->supplier_id;
}

// 制造商code,获取制造商ID
$manufacturer_query = $DB->prepare("
        SELECT  supplier_id
        FROM    pur_supplier
        WHERE  supplier_code = :manufacturer_code
");
$manufacturer_query->bindvalue(':manufacturer_code', $data['manufacturer_code']);

if (!$manufacturer_query->execute()) {
    Flight::error(new Exception(errorInfo($manufacturer_query)));
} elseif ($manufacturer_query->rowCount() == 0) {
    $manufacturer_id = 0;
} else {
    $manufacturer_line = $manufacturer_query->fetch(PDO::FETCH_OBJ);
    $manufacturer_id=$manufacturer_line->supplier_id;
}

// 中类code换取中类ID
$asset_class_query = $DB->prepare("
        SELECT  asset_class_id
        FROM    mic_asset_class
        WHERE   asset_class_code = :asset_class_code
");
$asset_class_query->bindvalue(':asset_class_code', $data['asset_class_code']);

if (!$asset_class_query->execute()) {
    Flight::error(new Exception(errorInfo($asset_class_query)));
}

$asset_class_line = $asset_class_query->fetch(PDO::FETCH_OBJ);
$asset_class_id = $asset_class_line->asset_class_id;


$object = new CreateAsset($DB);
$params =
    [
        'asset_name' => $data['asset_name'],
        'asset_code' => $data['asset_code'],
        'asset_nature' => $data['asset_nature'],
        'location_id' => $location_id,
        'asset_status' => $data['asset_status'],
        'asset_model' => $data['asset_model'],
        'asset_category_id' => $asset_category_id,
        'supplier_id' => $supplier_id,
        'asset_description' => $data['asset_description'],
        'asset_acquisition_price' =>  $data['asset_acquisition_price'],
        'asset_service_start_date' => $data['asset_service_start_date'],
        'asset_ff_1' => $data['asset_ff_1'],
        'asset_ff_2' => $data['asset_ff_2'],
        'function_id' => $function_id,
        'criticality_id' => $criticality_id,
        'asset_primary_code' => $data['asset_primary_code'],
        'asset_serial_number' => $data['asset_serial_number'],
       'asset_class_id' => $asset_class_id,
        "manufacturer_id"=>$manufacturer_line->supplier_id
    ];
$result_son = $object->execute($params);

//进行更改时间 asset_service_start_date
$set_var = $DB->query("SET @eamic_user = 'Automatic';");

$query = $DB->prepare(" 
            UPDATE asset_list
            SET asset_service_start_date = :date,
                asset_manufacture_date=:asset_manufacture_date,
                asset_installation_date=:asset_installation_date,
                asset_warranty_date=:asset_warranty_date,
                asset_primary_code= :asset_primary_code
            WHERE asset_id = :asset_id
   ;");

$query->bindvalue(':date', $data['asset_service_start_date']);
$query->bindvalue(':asset_manufacture_date', $data['asset_manufacture_date']);
$query->bindvalue(':asset_installation_date', $data['asset_installation_date']);
$query->bindvalue(':asset_warranty_date', $data['asset_warranty_date']);
$query->bindvalue(':asset_primary_code', $data['asset_primary_code']);
$query->bindvalue(':asset_id', $result_son['asset_id']);

if (!$query->execute()) {
    Flight::error(new Exception(errorInfo($query)));
} elseif ($query->rowCount() > 0) {
    Flight::json(['message' => '父级asset_service_start_date 修改成功'], 401);
    $log->info('子级修改成功' . 'asset_id:' . $result_son['asset_id'] . '成功的更新了' . $query->rowCount() . '条记录');
}
// 找父
$parent_query = $DB->prepare("
    SELECT  asset_id
    FROM    asset_list
    WHERE   asset_primary_code = :asset_primary_code
");

$parent_query->bindvalue(':asset_primary_code', $data['asset_primary_code']);

if (!$parent_query->execute()) {
    Flight::error(new Exception(errorInfo($parent_query)));
} elseif ($parent_query->rowcount() == 0) {
    Flight::json(['message' => '父编码 is empty'], 401);
}
$parent_query_line = $parent_query->fetch(PDO::FETCH_OBJ);
// 找见父ID
if (!isset($parent_query_line->asset_id)) {
    Flight::json(['message' => 'asset_primary_code is empty'], 401);
    exit();
}
// 进行父子级同步
$treeModel = new NestedSet($DB);
$move_result = $treeModel->addChild($result_son['asset_id'], $parent_query_line->asset_id);
    $result = [
        'asset_id' => $result_son['asset_id'],
        'parent_id' => $parent_query_line->asset_id,
        'details' => '父子级关系'
    ];
    update_log($result);

 

posted @ 2022-10-24 12:19  王越666  阅读(30)  评论(0编辑  收藏  举报