<?php

namespace App\Http\Services\Backend\DataManagement;

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

class TGoodManagementService
{
    // 相關私有 model 調用器宣告
    private $tgoodManagementDb;
    private $syslogtManagementDb;
    
    public function __construct()
    {
        date_default_timezone_set("Asia/Taipei");
        // 建構 model 調用器
        $this->tgoodManagementDb = new TGood();
        $this->syslogtManagementDb = new Syslogt();
    }
    
    public function getTGoods(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        $tgoods = $this->tgoodManagementDb
            ->select([
                'id',
                \DB::raw("CONCAT('<a href=\"tgoodManagement/edit/', id, '\">', IFNULL(lp_tmp, ''), '</a>', '<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 getTGoodById($id)
    {
        // 取得參數
        // 調用資料庫(或者其他業務邏輯)
        $tgoods = $this->tgoodManagementDb->select([
            'id',
            \DB::raw("lp_tmp as lp"),
            \DB::raw("ratio_tmp as ratio"),
            \DB::raw("active_tmp as active"),
            \DB::raw("totalQty_tmp as totalQty"),
            'issuedQty',
            'cdate',
            'mdate',
            \DB::raw("(select name from users where id=tgoods.oid) as oid"),
        ])
            ->where('id', $id)
            ->first()
            ->toArray();
        
        // 整理返回值並返回
        return $tgoods;
    }
    
    public function insertTGood($lp, $ratio, $active, $totalQty, $oid)
    {
        // 取得參數
        $data = [
            'lp' . '_tmp'       => $lp,
            'ratio' . '_tmp'    => $ratio,
            'active' . '_tmp'   => $active,
            'totalQty' . '_tmp' => $totalQty,
            'issuedQty'         => 0,
            'cdate'             => date('Y-m-d H:i:s'),
            'mdate'             => date('Y-m-d H:i:s'),
            'oid'               => $oid,
        ];
        // 調用資料庫(或者其他業務邏輯)
        $this->tgoodManagementDb
            ->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 modifyTGood($id, $lp, $ratio, $active, $totalQty, $oid)
    {
        // 取得參數
        $data = [
            'lp' . '_tmp'       => $lp,
            'ratio' . '_tmp'    => $ratio,
            'active' . '_tmp'   => $active,
            'totalQty' . '_tmp' => $totalQty,
            '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;
    }
    
}