<?php
//共通
require_once '../../base/common.php';
$logger = new logger(basename(__FILE__));
$logger->info("=== af908x_update_start ===");

$response["status"] = "OK";


$request_body = file_get_contents('php://input');
$data = json_decode($request_body, true);

$logger->info("data", $data);

try {
  global $dbutil;

  $detail = $data["data"][0];
  $logger->info("data", $detail );

  insertUpdateAfm08( $data["header"], $detail, $dbutil, $logger );

  $dbutil->commit();

} catch(userException $ue) {
  $retData["code"]  = $ue->getErrorCode();
  $retData["msg"]   = $ue->getErrorMessage();

  $response["status"] = "NG";
  $response["data"]   = $retData;

  $dbutil->rollback();

} catch(Exception $e) {
  $response["status"] = "NG";
  $response["data"]   = $e->getMessage();

  $dbutil->rollback();

} finally {
  $logger->info("response", $response );
  print( json_encode($response) );
}

$logger->info("=== af908x_updata_end ===");



function insertUpdateAfm08($header, $data, $dbutil, $logger) {
  global $stringUtil;

  $valueData['chgcd'] = $header['chgcd'];
  $valueData['chgnm'] = $data['chgnm'];
  $valueData['chgnms'] = $data['chgnms'];
  $valueData['chgknm'] = $data['chgknm'];
  $valueData['chgsex'] = $data['chgsex'];
  $valueData['chgcls'] = $data['chgcls'];
  $valueData['chgmem'] = $data['chgmem'];
  $valueData['chgpas'] = $data['chgpas'];
  $valueData['chgat0'] = $data['chgat0'];
  $valueData['chgat1'] = $data['chgat1'];
  $valueData['chgat2'] = $data['chgat2'];
  $valueData['chgat3'] = $data['chgat3'];
  $valueData['chgat4'] = $data['chgat4'];
  $valueData['chgat5'] = $data['chgat5'];
  $valueData['chgat6'] = $data['chgat6'];
  $valueData['chgat7'] = $data['chgat7'];
  $valueData['chgat8'] = $data['chgat8'];
  $valueData['chgat9'] = $data['chgat9'];
  $valueData['stp'] = $data['stp'];

  $valueUpdate = $valueData;
  $logger->info("valuData",$valueData);

  //まず更新
  $sql = file_get_contents("./sql/update.sql");
  $result = $dbutil->update($sql, $valueUpdate);
  $logger->info("did_update");
  $logger->info("result",$result);

  if($result == 0){ //UpdateできなかったらInsert
    $sql = file_get_contents("./sql/insert.sql");
    $result = $dbutil->insert($sql, $valueData);
    $logger->info("did_insert");
  }

  return $reuslt;
}

?>
