<?php

namespace App\Http\Services\Backend\DataManagement;

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

class RoundManagementService
{
    // 相關私有 model 調用器宣告
    private $roundManagementDb;
    private $syslogManagementDb;
    
    public function __construct()
    {
        date_default_timezone_set("Asia/Taipei");
        // 建構 model 調用器
        $this->roundManagementDb = new Round();
        $this->syslogManagementDb = new Syslog();
    }
    
    public function getRounds(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        // 選欄位
        $activeStr = '';
        foreach (GeneralConst::$activeMap as $k => $v) {
            $activeStr .= ' when \'' . $k . '\' then \'' . $v . '\'';
        }
        
        $rounds = $this->roundManagementDb
            ->select([
                'id',
                \DB::raw("CONCAT('<a href=\"roundManagement/edit/', id, '\">', roundName, '</a>') as roundName"),
                'dateBegin',
                'dateFinal',
                'redeemExtra',
                \DB::raw("(case active $activeStr end) as active"),
                'cdate',
                'mdate',
                \DB::raw("(select name from users where id=rounds.oid) as oid"),
            ]);
        // 過濾搜尋條件
        // 取總筆數
        $cnt = $rounds->count();
        // 排序
        $rounds = $rounds
            ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
        // 彙整
        // 分頁
        $rounds = $rounds
            ->skip($start)->take($length);
        // 實際取資料
        $rounds = $rounds
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $rounds;
    }
    
    public function getRoundById($id)
    {
        // 取得參數
        // 調用資料庫(或者其他業務邏輯)
        $rounds = $this->roundManagementDb->select([
            'id',
            'roundName',
            'dateBegin',
            'dateFinal',
            'redeemExtra',
            'active',
            'cdate',
            'mdate',
            \DB::raw("(select name from users where id=rounds.oid) as oid"),
        ])
            ->where('id', $id)
            ->first()
            ->toArray();
        
        // 整理返回值並返回
        return $rounds;
    }
    
    public function insertRound($roundName, $dateBegin, $dateFinal, $redeemExtra, $active, $oid)
    {
        // 取得參數
        $data = [
            'roundName'   => $roundName,
            'dateBegin'   => $dateBegin,
            'dateFinal'   => $dateFinal,
            'redeemExtra' => $redeemExtra,
            'active'      => $active,
            'cdate'       => date('Y-m-d H:i:s'),
            'mdate'       => date('Y-m-d H:i:s'),
            'oid'         => $oid,
        ];
        // 調用資料庫(或者其他業務邏輯)
        $this->roundManagementDb
            ->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 modifyRound($id, $roundName, $dateBegin, $dateFinal, $redeemExtra, $active, $oid)
    {
        // 取得參數
        $data = [
            'roundName'   => $roundName,
            'dateBegin'   => $dateBegin,
            'dateFinal'   => $dateFinal,
            'redeemExtra' => $redeemExtra,
            'active'      => $active,
            'mdate'       => date('Y-m-d H:i:s'),
            'oid'         => $oid,
        ];
        // 調用資料庫(或者其他業務邏輯)
        $res = $this->roundManagementDb
            ->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;
    }
    
}