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

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

//返り値
//ret : 0:正常終了, -1:重複
//dupliSpgrp : 重複しているspgrpの値
$ret_array = array(
  "updRet" => true
);


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

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

try {
  global $dbutil;

  $ret = true;

  //重複チェック
  foreach($data["data"] as $detail) {
    if($detail["scls1"] != $detail["orgScls1"] || $detail["scls2"] != $detail["orgScls2"]
    || $detail["scls3"] != $detail["orgScls3"] || $detail["scls4"] != $detail["orgScls4"]) {

      $logger->info("cntAfm90_detail",$detail);
      
      $whereParams["cls"]=$data["header"]["cls"];
      $whereParams["scls1"]=$detail["scls1"];
      $whereParams["scls2"]=$detail["scls2"];
      $whereParams["scls3"]=$detail["scls3"];
      $whereParams["scls4"]=$detail["scls4"];
      $sql = file_get_contents("./sql/cntAfm90.sql");
      
      $logger->info("cntAfm90".$sql,$whereParams);
      $ret_cnt = $dbutil->select($sql, $whereParams);
      $logger->info("result_cntAfm90",$ret_cnt);

      if($ret_cnt[0]["cnt"] != '0') {
        $ret = false;
        $dupliScls1=$detail["scls1"];
        $dupliScls2=$detail["scls2"];
        $dupliScls3=$detail["scls3"];
        $dupliScls4=$detail["scls4"];
        $orgScls1=$detail["orgScls1"];
        $orgScls2=$detail["orgScls2"];
        $orgScls3=$detail["orgScls3"];
        $orgScls4=$detail["orgScls4"];
        
        break;
      }
    }
  }

  if($ret) {
    $logger->info("insupd_start");
    foreach($data["data"] as $detail) {
      if(!($stringUtil->isEmpty($detail["scls1"])) || !($stringUtil->isEmpty($detail["scls2"])) 
      || !($stringUtil->isEmpty($detail["scls3"])) || !($stringUtil->isEmpty($detail["scls4"]))) {
        if($detail["chkLineDel"] != '1') { // ins/upd
          if($detail["scls1"] != $detail["orgScls1"]
          || $detail["scls2"] != $detail["orgScls2"]
          || $detail["scls3"] != $detail["orgScls3"]
          || $detail["scls4"] != $detail["orgScls4"]
          || $detail["scls5"] != $detail["orgScls5"]
          || $detail["scls6"] != $detail["orgScls6"]
          || $detail["scls7"] != $detail["orgScls7"]
          || $detail["scls8"] != $detail["orgScls8"]
          || $detail["scls9"] != $detail["orgScls9"]
          || $detail["snum1"] != $detail["orgSnum1"]
          || $detail["snum2"] != $detail["orgSnum2"]
          || $detail["snum3"] != $detail["orgSnum3"]
          || $detail["nm"] != $detail["orgNm"]
          || $detail["nm2"] != $detail["orgNm2"]
          || $detail["nm3"] != $detail["orgNm3"]
          || $detail["nm4"] != $detail["orgNm4"]
          ) { //変更があるときだけ
            $logger->info("insupd_detail",$detail);
            insertUpdateAfm0321( $data["header"], $detail, $dbutil, $logger );
          }
        }
        else { //del
          deleteAfm0321($data["header"], $detail, $dbutil, $logger);
          $logger->info("del_detail",$detail);
        }
      }
    }
    $dbutil->commit();
  } else {
    $ret_array["dupliScls1"] = $dupliScls1;
    $ret_array["dupliScls2"] = $dupliScls2;
    $ret_array["dupliScls3"] = $dupliScls3;
    $ret_array["dupliScls4"] = $dupliScls4;
    $ret_array["orgScls1"] = $orgScls1;
    $ret_array["orgScls2"] = $orgScls2;
    $ret_array["orgScls3"] = $orgScls3;
    $ret_array["orgScls4"] = $orgScls4;
  }
  $ret_array["updRet"] = $ret;
  $response["data"] = $ret_array;

} 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("=== at990x_updata_end ===");


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

  $valueData["cls"] = $header["cls"];
  $valueData['orgScls1'] = $data['orgScls1'];
  $valueData['orgScls2'] = $data['orgScls2'];
  $valueData['orgScls3'] = $data['orgScls3'];
  $valueData['orgScls4'] = $data['orgScls4'];
  $logger->info("deleteData", $valueData);
  
  $sql = file_get_contents("./sql/delete.sql");
  $result = $dbutil->delete($sql, $valueData);

  $logger->info("deleteResult", $result);

  return $result;
}

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

  $valueData["cls"] = $header["cls"];
  $valueData["scls1"] = $data["scls1"];
  $valueData["scls2"] = $data["scls2"];
  $valueData["scls3"] = $data["scls3"];
  $valueData["scls4"] = $data["scls4"];
  $valueData["scls5"] = $data["scls5"];
  $valueData["scls6"] = $data["scls6"];
  $valueData["scls7"] = $data["scls7"];
  $valueData["scls8"] = $data["scls8"];
  $valueData["scls9"] = $data["scls9"];
  $valueData["snum1"] = $data["snum1"];
  $valueData["snum2"] = $data["snum2"];
  $valueData["snum3"] = $data["snum3"];
  $valueData["nm"] = $data["nm"];
  $valueData["nm2"] = $data["nm2"];
  $valueData["nm3"] = $data["nm3"];
  $valueData["nm4"] = $data["nm4"];

  $valueUpdate = $valueData;
  $valueUpdate['orgScls1'] = $data['orgScls1'];
  $valueUpdate['orgScls2'] = $data['orgScls2'];
  $valueUpdate['orgScls3'] = $data['orgScls3'];
  $valueUpdate['orgScls4'] = $data['orgScls4'];

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

  //まずupdate
  $sql = file_get_contents("./sql/update.sql");
  $result = $dbutil->update($sql, $valueUpdate);

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

  return $reuslt;
}

?>
