<?php

namespace App\Http\Services\Backend\DataManagement;

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

class TCheckinManagementService
{
    // 相關私有 model 調用器宣告
    private $tcheckinManagementDb;
    private $syslogtManagementDb;
    
    public function __construct()
    {
        date_default_timezone_set("Asia/Taipei");
        // 建構 model 調用器
        $this->tcheckinManagementDb = new TCheckinGpAlloc();
        $this->syslogtManagementDb = new Syslogt();
    }
    
    public function getTCheckins(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        $tcheckins = $this->tcheckinManagementDb
            ->select([
                'id',
                \DB::raw("CONCAT('<a href=\"tcheckinManagement/edit/', id, '\">', IFNULL(day_tmp, ''), '</a>', '<hr>', IFNULL(day, '')) as day"),
                \DB::raw("CONCAT(IFNULL(gp_tmp, ''), '<hr>', IFNULL(gp, '')) as gp"),
                'cdate',
                'mdate',
                \DB::raw("(select name from users where id=tcheckinGpAlloc.oid) as oid"),
            ]);
        // 過濾搜尋條件
        // 取總筆數
        $cnt = $tcheckins->count();
        // 排序
        $tcheckins = $tcheckins
            ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
        // 彙整
        // 分頁
        $tcheckins = $tcheckins
            ->skip($start)->take($length);
        // 實際取資料
        $tcheckins = $tcheckins
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $tcheckins;
    }
    
    public function getTCheckinById($id)
    {
        // 取得參數
        // 調用資料庫(或者其他業務邏輯)
        $tcheckins = $this->tcheckinManagementDb->select([
            'id',
            \DB::raw("day_tmp as day"),
            \DB::raw("gp_tmp as gp"),
            'cdate',
            'mdate',
            \DB::raw("(select name from users where id=tcheckinGpAlloc.oid) as oid"),
        ])
            ->where('id', $id)
            ->first()
            ->toArray();
        
        // 整理返回值並返回
        return $tcheckins;
    }
    
    public function insertTCheckin($day, $gp, $oid)
    {
        // 取得參數
        $data = [
            'day' . '_tmp' => $day,
            'gp' . '_tmp'  => $gp,
            'cdate'        => date('Y-m-d H:i:s'),
            'mdate'        => date('Y-m-d H:i:s'),
            'oid'          => $oid,
        ];
        // 調用資料庫(或者其他業務邏輯)
        $this->tcheckinManagementDb
            ->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 modifyTCheckin($id, $day, $gp, $oid)
    {
        // 取得參數
        $data = [
            'day' . '_tmp' => $day,
            'gp' . '_tmp'  => $gp,
            'mdate'        => date('Y-m-d H:i:s'),
            'oid'          => $oid,
        ];
        // 調用資料庫(或者其他業務邏輯)
        $res = $this->tcheckinManagementDb
            ->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;
    }
    
}