#!/usr/bin/perl 
use strict;
use DBI;#使用資料庫

#參數初始化
my @method = split("",$ARGV[0]);#使用的行車策略
my $infile = $ARGV[1];#result檔名
my @outData;#輸出檔名
$outData[0] = $ARGV[2]."PC.dat";
$outData[1] = $ARGV[2]."RG.dat";
$outData[2] = $ARGV[2]."TLP.dat";

#SQL相關參數
my $db = "EMS"; #database 名稱
my $host = "localhost"; #host name
my $user = "EMS"; #資料庫帳號
my $password = "spring67721"; #資料庫密碼

#survival rate相關參數
my $id = $ARGV[3];
my $date = $ARGV[4];
my @survival;

#連結資料庫並且取得模擬參數
sub getData{
	#連接Database 
	my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host", $user, $password) 
		or die "Can't connect to database: $DBI::errstr\n";
	#準備執行SQL指令
	my $sth = $dbh->prepare("SELECT * FROM `Survival` WHERE `ID` = '$id' AND `TIME` = '$date'");
	#執行SQL指令
	print $sth->execute(),"\n";
	#將資料存於survival
	if(@survival = $sth->fetchrow_array()){
		print "Survival Data: @survival\n";
		#結束command
		$sth->finish();
		#中斷資料庫連結
		$dbh->disconnect or warn "Disconnection error: $DBI::errstr\n";
	}
}
#設定survival Rate
sub survival{
	my @S=@_;
	if($S[1]<=$survival[2]){return $survival[12];}
	elsif($S[1]>$survival[2] && $S[1]<=$survival[3]){return $survival[13];}
	elsif($S[1]>$survival[3] && $S[1]<=$survival[4]){return $survival[14];}
	elsif($S[1]>$survival[4] && $S[1]<=$survival[5]){return $survival[15];}
	elsif($S[1]>$survival[5] && $S[1]<=$survival[6]){return $survival[16];}
	elsif($S[1]>$survival[6] && $S[1]<=$survival[7]){return $survival[17];}
	elsif($S[1]>$survival[7] && $S[1]<=$survival[8]){return $survival[18];}
	elsif($S[1]>$survival[8] && $S[1]<=$survival[9]){return $survival[19];}
	elsif($S[1]>$survival[9] && $S[1]<=$survival[10]){return $survival[20];}
	elsif($S[1]>$survival[10] && $S[1]<=$survival[11]){return $survival[21];}
	else{return 0;}
}

#取得Survival rate
&getData;

#分割欄位
my $str = `awk '{print \$1}' $infile`;
my @title = split (" ",$str);
$title[2] = "SRV";
my (@X,@A,@B,@C,@D,@E,@F,@G,@PC,@RG,@TLP);
my ($fh,$outDir);

#取出每一欄並且分割為矩陣
for (my $var = 0; $var < @method; $var++) {
		$str="";
		if ($method[$var] eq "X") {
			$str = `awk '{print \$($var+2)}' $infile`;
			@X = split (" ",$str);
            $X[0] = "None";
			$X[2] = &survival(@X);
		}elsif ($method[$var] eq "A") {
			$str = `awk '{print \$($var+2)}' $infile`;
			@A = split (" ",$str);
            $A[0] = "RG";
			$A[2] = &survival(@A);
		}elsif ($method[$var] eq "B") {
			$str = `awk '{print \$($var+2)}' $infile`;
			@B = split (" ",$str);
			$B[2] = &survival(@B);
		}elsif ($method[$var] eq "C") {
			$str = `awk '{print \$($var+2)}' $infile`;
			@C = split (" ",$str);
			$C[2] = &survival(@C);
		}elsif ($method[$var] eq "D") {
			$str = `awk '{print \$($var+2)}' $infile`;
			@D = split (" ",$str);
            $D[0] = "ALL";
			$D[2] = &survival(@D);
		}elsif ($method[$var] eq "E") {
			$str = `awk '{print \$($var+2)}' $infile`;
			@E = split (" ",$str);
            $E[0] = "PC";
			$E[2] = &survival(@E);
		}elsif ($method[$var] eq "F") {
			$str = `awk '{print \$($var+2)}' $infile`;
			@F = split (" ",$str);
            $F[0] = "TLP";
			$F[2] = &survival(@F);
		}elsif ($method[$var] eq "G") {
			$str = `awk '{print \$($var+2)}' $infile`;
			@G = split (" ",$str);
			$G[2] = &survival(@G);
		}else {
			print "Error!\n";
		}
}

#RG重新排版
if ($B[0] ne ""){
    $B[0] = "RG+PC";
}
if ($C[0] ne ""){
    $C[0] = "RG+TLP";
}
for (my $var = 0; $var < 3; $var++) {
	$RG[$var] = $title[$var]."\t".$X[$var]."\t".$A[$var]."\t".$B[$var]."\t".$C[$var]."\t".$D[$var];
}

#PC重新排版
if ($B[0] ne ""){
    $B[0] = "PC+RG";
}
if ($G[0] ne ""){
    $G[0] = "PC+TLP";
}
for (my $var = 0; $var < 3; $var++) {
	$PC[$var] = $title[$var]."\t".$X[$var]."\t".$E[$var]."\t".$B[$var]."\t".$G[$var]."\t".$D[$var];
}

#TLP重新排版
if ($C[0] ne ""){
    $C[0] = "TLP+RG";
}
if ($G[0] ne ""){
    $G[0] = "TLP+PC";
}
for (my $var = 0; $var < 3; $var++) {
	$TLP[$var] = $title[$var]."\t".$X[$var]."\t".$F[$var]."\t".$C[$var]."\t".$G[$var]."\t".$D[$var];
}

#輸出Data
open($fh,'>',$outData[0]) or die "Can't write file";
print $fh $PC[0]."\n".$PC[1]."\n".$PC[2];
close $fh;

open($fh,'>',$outData[1]) or die "Can't write file";
print $fh $RG[0]."\n".$RG[1]."\n".$RG[2];
close $fh;

open($fh,'>',$outData[2]) or die "Can't write file";
print $fh $TLP[0]."\n".$TLP[1]."\n".$TLP[2];
close $fh;
