<?php

namespace App\Http\Services\Backend\DataManagement;

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

class TAuditManagementService
{
    // 相關私有 model 調用器宣告
    private $troundManagementDb;
    private $tcheckinManagementDb;
    private $tgoodManagementDb;
    private $syslogtManagementDb;
    
    public function __construct()
    {
        date_default_timezone_set("Asia/Taipei");
        // 建構 model 調用器
        $this->troundManagementDb = new TRound();
        $this->tcheckinManagementDb = new TCheckinGpAlloc();
        $this->tgoodManagementDb = new TGood();
        $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([
                \DB::raw("(case when dateFinal_tmp=dateFinal and dateFinal_tmp=dateFinal and redeemExtra_tmp=redeemExtra then '' else CONCAT('<a href=\"tauditManagement/tround/', id, '\">通過</a>') end) as BtnTAudit"),
                'id',
                '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 getTCheckins(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        $tcheckins = $this->tcheckinManagementDb
            ->select([
                \DB::raw("(case when day_tmp=day and gp_tmp=gp then '' else CONCAT('<a href=\"tauditManagement/tcheckin/', id, '\">通過</a>') end) as BtnAudit"),
                'id',
                \DB::raw("CONCAT(IFNULL(day_tmp, ''), '<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 getTGoods(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        $tgoods = $this->tgoodManagementDb
            ->select([
                \DB::raw("(case when lp_tmp=lp and ratio_tmp=ratio and active_tmp=active and totalQty_tmp=totalQty then '' else CONCAT('<a href=\"tauditManagement/tgood/', id, '\">通過</a>') end) as BtnTAudit"),
                'id',
                \DB::raw("CONCAT(IFNULL(lp_tmp, ''), '<hr>', IFNULL(lp, '')) as lp"),
                \DB::raw("CONCAT(IFNULL(ratio_tmp, ''), '<hr>', IFNULL(ratio, '')) as ratio"),
                \DB::raw("CONCAT(IFNULL(active_tmp, ''), '<hr>', IFNULL(active, '')) as active"),
                \DB::raw("CONCAT(IFNULL(totalQty_tmp, ''), '<hr>', IFNULL(totalQty, '')) as totalQty"),
                'issuedQty',
                'cdate',
                'mdate',
                \DB::raw("(select name from users where id=tgoods.oid) as oid"),
            ]);
        // 過濾搜尋條件
        // 取總筆數
        $cnt = $tgoods->count();
        // 排序
        $tgoods = $tgoods
            ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
        // 彙整
        // 分頁
        $tgoods = $tgoods
            ->skip($start)->take($length);
        // 實際取資料
        $tgoods = $tgoods
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $tgoods;
    }
    
    public function passTRound($id, $oid)
    {
        // 寫入
        $data = [
            'dateBegin'   => \DB::raw('dateBegin' . '_tmp'),
            'dateFinal'   => \DB::raw('dateFinal' . '_tmp'),
            'redeemExtra' => \DB::raw('redeemExtra' . '_tmp'),
            '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;
    }
    
    public function passTCheckin($id, $oid)
    {
        // 寫入
        $data = [
            'day'   => \DB::raw('day' . '_tmp'),
            'gp'    => \DB::raw('gp' . '_tmp'),
            '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;
        // syslog
        $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;
    }
    
    public function passTGood($id, $oid)
    {
        // 寫入
        $data = [
            'lp'       => \DB::raw('lp' . '_tmp'),
            'ratio'    => \DB::raw('ratio' . '_tmp'),
            'active'   => \DB::raw('active' . '_tmp'),
            'totalQty' => \DB::raw('totalQty' . '_tmp'),
            'mdate'    => date('Y-m-d H:i:s'),
            'oid'      => $oid,
        ];
        $res = $this->tgoodManagementDb->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;
    }
    
}