<?php

namespace App\Http\Services\Backend\DataManagement;

use App\Http\Services\ConstDef\GeneralConst;
use App\Models\Web\GameGpRatio;
use App\Models\Web\Syslog;

class GameManagementService
{
    // 相關私有 model 調用器宣告
    private $gameManagementDb;
    private $syslogManagementDb;
    
    public function __construct()
    {
        date_default_timezone_set("Asia/Taipei");
        // 建構 model 調用器
        $this->gameManagementDb = new GameGpRatio();
        $this->syslogManagementDb = new Syslog();
    }
    
    public function getGames(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        $games = $this->gameManagementDb
            ->select([
                'id',
                \DB::raw("CONCAT('<a href=\"gameManagement/edit/', id, '\">', ratio, '</a>') as ratio"),
                'gp',
                'cdate',
                'mdate',
                \DB::raw("(select name from users where id=gameGpRatio.oid) as oid"),
            ]);
        // 過濾搜尋條件
        // 取總筆數
        $cnt = $games->count();
        // 排序
        $games = $games
            ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
        // 彙整
        // 分頁
        $games = $games
            ->skip($start)->take($length);
        // 實際取資料
        $games = $games
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $games;
    }
    
    public function getGameById($id)
    {
        // 取得參數
        // 調用資料庫(或者其他業務邏輯)
        $games = $this->gameManagementDb->select([
            'id',
            'ratio',
            'gp',
            'cdate',
            'mdate',
            \DB::raw("(select name from users where id=gameGpRatio.oid) as oid"),
        ])
            ->where('id', $id)
            ->first()
            ->toArray();
        
        // 整理返回值並返回
        return $games;
    }
    
    public function insertGame($ratio, $gp, $oid)
    {
        // 取得參數
        $data = [
            'ratio' => $ratio,
            'gp'    => $gp,
            'cdate' => date('Y-m-d H:i:s'),
            'mdate' => date('Y-m-d H:i:s'),
            'oid'   => $oid,
        ];
        // 調用資料庫(或者其他業務邏輯)
        $this->gameManagementDb
            ->insert($data);
        $id = \DB::getPdo()->lastInsertId();
        // syslog
        $this->syslogManagementDb
            ->insert([
                'type'    => GeneralConst::LOG_ADMIN,
                'func'    => __FUNCTION__,
                'k'       => $oid,
                'memoIn'  => json_encode(['data' => $data], JSON_UNESCAPED_UNICODE),
                'memoOut' => json_encode(['id' => $id], JSON_UNESCAPED_UNICODE),
                'cdate'   => date("Y-m-d H:i:s"),
            ]);
    
        // 整理返回值並返回
        return $id;
    }
    
    public function modifyGame($id, $ratio, $gp, $oid)
    {
        // 取得參數
        $data = [
            'ratio' => $ratio,
            'gp'    => $gp,
            'mdate' => date('Y-m-d H:i:s'),
            'oid'   => $oid,
        ];
        // 調用資料庫(或者其他業務邏輯)
        $res = $this->gameManagementDb
            ->where('id', $id)
            ->update($data);
        $rc = \DB::select("SELECT ROW_COUNT() AS rc;");
        $rc = $rc[0]->rc;
        // syslog
        $this->syslogManagementDb
            ->insert([
                'type'    => GeneralConst::LOG_ADMIN,
                'func'    => __FUNCTION__,
                'k'       => $oid,
                'memoIn'  => json_encode(['id' => $id, 'data' => $data], JSON_UNESCAPED_UNICODE),
                'memoOut' => json_encode(['rc' => $rc], JSON_UNESCAPED_UNICODE),
                'cdate'   => date("Y-m-d H:i:s"),
            ]);
    
        // 整理返回值並返回
        return $res;
    }
    
}