<?php
//$db = new mysqli("localhost", "mysql_user", "mysql_password", "mysql_db");
date_default_timezone_set('Asia/Seoul');
//date_default_timezone_set('Asia/Shanghai');

$db = @new mysqli('db.vpnlink.kr','u572725970_myvpn','eL6LfFCdjwkssEXCwXTVE0iAD1urZ1AF','u572725970_myvpn');
mysqli_set_charset($db,"utf8");

// $db_live = @new mysqli('localhost','u572725970_myvpn_live','eL6LfFCdjwkssEXCwXTVE0iAD1urZ1AF','u572725970_myvpn_live');
// mysqli_set_charset($db_live,"utf8");

//$db = @new mysqli('45.77.171.253','u572725970_myvpn','eL6LfFCdjwkssEXCwXTVE0iAD1urZ1AF','u572725970_myvpn');
//mysqli_set_charset($db,"utf8");

if(mysqli_connect_errno()) {
	exit("DB연결오류(Code:VLDB-E101A)");
} else {
	$AES_KEY = "cm#ds7afks.a2@3k8ioxhhJ!sd";
}


function master_log_write($tb_no,$store_id,$store_name,$member_id,$ComspecUniqId,$CpuId,$CpuName,$MainBoard,$RAM,$SSDinfo,$HDDinfo,$VideoController,$OperatingSystem,$mgrname,$start_date,$exp_date,$AnyDeskID,$TeamViewerID,$rantal_fee_type,$rental_fee,$rental_setting_fee,$rental_fee_stat,$rental_stat,$rental_pc_name,$rental_pc_memo,$LastUpdateTime,$AppVer,$jobin_datetime,$jobend_datetime,$jobstat,$jobact,$field,$value,$remark,$ip,$admin_id) {

	global $db;

	// 특수문자 입력오류 방지
	$tb_no = addslashes($tb_no);
	$store_id = addslashes($store_id);
	$store_name = addslashes($store_name);
	$member_id = addslashes($member_id);
	$ComspecUniqId = addslashes($ComspecUniqId);
	$CpuId = addslashes($CpuId);
	$CpuName = addslashes($CpuName);
	$MainBoard = addslashes($MainBoard);
	$RAM = addslashes($RAM);
	$SSDinfo = addslashes($SSDinfo);
	$HDDinfo = addslashes($HDDinfo);
	$VideoController = addslashes($VideoController);
	$OperatingSystem = addslashes($OperatingSystem);
	$mgrname = addslashes($mgrname);
	$start_date = addslashes($start_date);
	$exp_date = addslashes($exp_date);
	$AnyDeskID = addslashes($AnyDeskID);
	$TeamViewerID = addslashes($TeamViewerID);
	$rantal_fee_type = addslashes($rantal_fee_type);
	$rental_fee = addslashes($rental_fee);
	$rental_setting_fee = addslashes($rental_setting_fee);
	$rental_fee_stat = addslashes($rental_fee_stat);
	$rental_stat = addslashes($rental_stat);
	$rental_pc_name = addslashes($rental_pc_name);
	$rental_pc_memo = addslashes($rental_pc_memo);
	$LastUpdateTime = addslashes($LastUpdateTime);
	$AppVer = addslashes($AppVer);
	$jobin_datetime = addslashes($jobin_datetime);
	$jobend_datetime = addslashes($jobend_datetime);
	$jobstat = addslashes($jobstat);
	$jobact = addslashes($jobact);
	$field = addslashes($field);
	$value = addslashes($value);
	$remark = addslashes($remark);
	$ip = addslashes($ip);
	$admin_id = addslashes($admin_id);


	// 새로 삽입하려는 데이터와 동일한 값이 이미 존재하는지 확인하는 쿼리
	$checkQuery = "
	SELECT COUNT(*) as count
	FROM smart_rental_comspec_history
	WHERE
		tb_no = '$tb_no' AND
		store_id = '$store_id' AND
		field = '$field' AND
		value = '$value' AND
		ABS(TIMESTAMPDIFF(SECOND, datetime, NOW())) < 3
	";

	// 결과를 실행하여 가져오기
	$result = mysqli_query($db, $checkQuery);
	$row_duplication = mysqli_fetch_assoc($result);

	// count 값이 0이면, 동일한 데이터가 없거나 시간차가 3초 이상이므로 삽입을 수행
	if ($row_duplication['count'] == 0) {
	$query92 = "INSERT smart_rental_comspec_history SET
		tb_no = '$tb_no',
		store_id = '$store_id',
		store_name = '$store_name',
		member_id = '$member_id',
		mgrname = '$mgrname',
		start_date = '$start_date',
		exp_date = '$exp_date',
		AnyDeskID = '$AnyDeskID',
		TeamViewerID = '$TeamViewerID',
		rantal_fee_type = '$rantal_fee_type',
		rental_fee = '$rental_fee',
		rental_setting_fee = '$rental_setting_fee',
		rental_fee_stat = '$rental_fee_stat',
		rental_stat = '$rental_stat',
		rental_pc_name = '$rental_pc_name',
		rental_pc_memo = '$rental_pc_memo',
		LastUpdateTime = '$LastUpdateTime',
		AppVer = '$AppVer',
		jobin_datetime = '$jobin_datetime',
		jobend_datetime = '$jobend_datetime',
		jobstat = '$jobstat',
		jobact = '$jobact',
		field = '$field',
		value = '$value',
		remark = '$remark',
		ip = '$ip',
		admin_id = '$admin_id'
	";

	if (mysqli_query($db, $query92) !== true) {
		die("Error Code 9119A : 서버장애가 발생하였습니다. 반드시 관리자에게 신고하여 주십시오!");
	}
	} else {
		// 동일한 레코드가 3초 이내에 존재하여 삽입하지 않음
		//echo "삽입하지 않았습니다: 동일한 데이터가 3초 이내에 이미 존재합니다.";
	}



}

function get_storeid_short($storeid) {

	global $db;

	// 가맹점 단축코드 추출
	$result0 = $db->query("SELECT storeid_short FROM admin_stores WHERE storeid = '$storeid' ");
	$permissionTotal=mysqli_num_rows($result0);
	if ($permissionTotal == 0) {
		return 'NA';
	} else {
		$row0 = $result0->fetch_assoc();
		$storeid_short = $row0['storeid_short'];
		return $storeid_short;
	}

}
?>
