<?php

namespace App\Http\Services\Backend\DataManagement;

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

class AuditManagementService
{
    // 相關私有 model 調用器宣告
    private $roundManagementDb;
    private $gameManagementDb;
    private $checkinManagementDb;
    private $goodManagementDb;
    private $syslogManagementDb;
    
    public function __construct()
    {
        date_default_timezone_set("Asia/Taipei");
        // 建構 model 調用器
        $this->roundManagementDb = new Round();
        $this->gameManagementDb = new GameGpRatio();
        $this->checkinManagementDb = new CheckinGpAlloc();
        $this->goodManagementDb = new Good();
        $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([
                \DB::raw("(case when dateFinal_tmp=dateFinal and dateFinal_tmp=dateFinal and redeemExtra_tmp=redeemExtra then '' else CONCAT('<a href=\"auditManagement/round/', id, '\">通過</a>') end) as BtnAudit"),
                '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=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 getGames(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        $games = $this->gameManagementDb
            ->select([
                \DB::raw("(case when ratio_tmp=ratio and gp_tmp=gp then '' else CONCAT('<a href=\"auditManagement/game/', id, '\">通過</a>') end) as BtnAudit"),
                'id',
                \DB::raw("CONCAT(IFNULL(ratio_tmp, ''), '<hr>', IFNULL(ratio, '')) as ratio"),
                \DB::raw("CONCAT(IFNULL(gp_tmp, ''), '<hr>', IFNULL(gp, '')) as 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 getCheckins(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        $checkins = $this->checkinManagementDb
            ->select([
                \DB::raw("(case when day_tmp=day and gp_tmp=gp then '' else CONCAT('<a href=\"auditManagement/checkin/', 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=checkinGpAlloc.oid) as oid"),
            ]);
        // 過濾搜尋條件
        // 取總筆數
        $cnt = $checkins->count();
        // 排序
        $checkins = $checkins
            ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
        // 彙整
        // 分頁
        $checkins = $checkins
            ->skip($start)->take($length);
        // 實際取資料
        $checkins = $checkins
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $checkins;
    }
    
    public function getGoods(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        $goods = $this->goodManagementDb
            ->select([
                \DB::raw("(case when lp_tmp=lp and gp_tmp=gp and active_tmp=active and totalQty_tmp=totalQty then '' else CONCAT('<a href=\"auditManagement/good/', id, '\">通過</a>') end) as BtnAudit"),
                'id',
                \DB::raw("CONCAT(IFNULL(lp_tmp, ''), '<hr>', IFNULL(lp, '')) as lp"),
                \DB::raw("CONCAT(IFNULL(gp_tmp, ''), '<hr>', IFNULL(gp, '')) as gp"),
                \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=goods.oid) as oid"),
            ]);
        // 過濾搜尋條件
        // 取總筆數
        $cnt = $goods->count();
        // 排序
        $goods = $goods
            ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
        // 彙整
        // 分頁
        $goods = $goods
            ->skip($start)->take($length);
        // 實際取資料
        $goods = $goods
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $goods;
    }
    
    public function passRound($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->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;
    }
    
    public function passGame($id, $oid)
    {
        // 寫入
        $data = [
            'ratio' => \DB::raw('ratio' . '_tmp'),
            'gp'    => \DB::raw('gp' . '_tmp'),
            '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;
    }
    
    public function passCheckin($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->checkinManagementDb->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;
    }
    
    public function passGood($id, $oid)
    {
        // 寫入
        $data = [
            'lp'       => \DB::raw('lp' . '_tmp'),
            'gp'       => \DB::raw('gp' . '_tmp'),
            'active'   => \DB::raw('active' . '_tmp'),
            'totalQty' => \DB::raw('totalQty' . '_tmp'),
            'mdate'    => date('Y-m-d H:i:s'),
            'oid'      => $oid,
        ];
        $res = $this->goodManagementDb->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;
    }
    
}