<?php

namespace App\Http\Services\Backend\DataManagement;

use App\Http\Services\ConstDef\GeneralConst;
use App\Models\Web\TRound;
use App\Models\Web\Syslogt;

class TRoundManagementService
{
    // 相關私有 model 調用器宣告
    private $troundManagementDb;
    private $syslogtManagementDb;
    
    public function __construct()
    {
        date_default_timezone_set("Asia/Taipei");
        // 建構 model 調用器
        $this->troundManagementDb = new TRound();
        $this->syslogtManagementDb = new Syslogt();
    }
    
    public function getTRounds(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        // 選欄位
        $activeStr = '';
        foreach (GeneralConst::$activeMap as $k => $v) {
            $activeStr .= ' when \'' . $k . '\' then \'' . $v . '\'';
        }
        
        $trounds = $this->troundManagementDb
            ->select([
                'id',
                \DB::raw("CONCAT('<a href=\"troundManagement/edit/', id, '\">', roundName, '</a>') as roundName"),
                \DB::raw("CONCAT(IFNULL(dateBegin_tmp, ''), '<hr>', IFNULL(dateBegin, '')) as dateBegin"),
                \DB::raw("CONCAT(IFNULL(dateFinal_tmp, ''), '<hr>', IFNULL(dateFinal, '')) as dateFinal"),
                \DB::raw("CONCAT(IFNULL(redeemExtra_tmp, ''), '<hr>', IFNULL(redeemExtra, '')) as redeemExtra"),
                \DB::raw("(case active $activeStr end) as active"),
                'cdate',
                'mdate',
                \DB::raw("(select name from users where id=trounds.oid) as oid"),
            ]);
        // 過濾搜尋條件
        // 取總筆數
        $cnt = $trounds->count();
        // 排序
        $trounds = $trounds
            ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
        // 彙整
        // 分頁
        $trounds = $trounds
            ->skip($start)->take($length);
        // 實際取資料
        $trounds = $trounds
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $trounds;
    }
    
    public function getTRoundById($id)
    {
        // 取得參數
        // 調用資料庫(或者其他業務邏輯)
        $trounds = $this->troundManagementDb->select([
            'id',
            'roundName',
            \DB::raw("dateBegin_tmp as dateBegin"),
            \DB::raw("dateFinal_tmp as dateFinal"),
            \DB::raw("redeemExtra_tmp as redeemExtra"),
            'active',
            'cdate',
            'mdate',
            \DB::raw("(select name from users where id=trounds.oid) as oid"),
        ])
            ->where('id', $id)
            ->first()
            ->toArray();
        
        // 整理返回值並返回
        return $trounds;
    }
    
    public function insertTRound($roundName, $dateBegin, $dateFinal, $redeemExtra, $active, $oid)
    {
        // 取得參數
        $data = [
            'roundName'            => $roundName,
            'dateBegin' . '_tmp'   => $dateBegin,
            'dateFinal' . '_tmp'   => $dateFinal,
            'redeemExtra' . '_tmp' => $redeemExtra,
            'active'               => $active,
            'cdate'                => date('Y-m-d H:i:s'),
            'mdate'                => date('Y-m-d H:i:s'),
            'oid'                  => $oid,
        ];
        // 調用資料庫(或者其他業務邏輯)
        $this->troundManagementDb
            ->insert($data);
        $id = \DB::getPdo()->lastInsertId();
        // syslogt
        $this->syslogtManagementDb
            ->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 modifyTRound($id, $roundName, $dateBegin, $dateFinal, $redeemExtra, $active, $oid)
    {
        // 取得參數
        $data = [
            'roundName'            => $roundName,
            'dateBegin' . '_tmp'   => $dateBegin,
            'dateFinal' . '_tmp'   => $dateFinal,
            'redeemExtra' . '_tmp' => $redeemExtra,
            'active'               => $active,
            'mdate'                => date('Y-m-d H:i:s'),
            'oid'                  => $oid,
        ];
        // 調用資料庫(或者其他業務邏輯)
        $res = $this->troundManagementDb
            ->where('id', $id)
            ->update($data);
        $rc = \DB::select("SELECT ROW_COUNT() AS rc;");
        $rc = $rc[0]->rc;
        // syslogt
        $this->syslogtManagementDb
            ->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;
    }
    
}