<?php

namespace App\Http\Services\Backend\DataManagement;

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

class GoodTmpManagementService
{
    // 相關私有 model 調用器宣告
    private $goodManagementDb;
    private $syslogManagementDb;
    
    public function __construct()
    {
        date_default_timezone_set("Asia/Taipei");
        // 建構 model 調用器
        $this->goodManagementDb = new Good();
        $this->syslogManagementDb = new Syslog();
    }
    
    public function getGoods(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        $goods = $this->goodManagementDb
            ->select([
                'id',
                \DB::raw("CONCAT('<a href=\"goodManagement/edit/', id, '\">', IFNULL(lp_tmp, ''), '</a>', '<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 getGoodById($id)
    {
        // 取得參數
        // 調用資料庫(或者其他業務邏輯)
        $goods = $this->goodManagementDb->select([
            'id',
            \DB::raw("lp_tmp as lp"),
            \DB::raw("gp_tmp as gp"),
            \DB::raw("active_tmp as active"),
            \DB::raw("totalQty_tmp as totalQty"),
            'issuedQty',
            'cdate',
            'mdate',
            \DB::raw("(select name from users where id=goods.oid) as oid"),
        ])
            ->where('id', $id)
            ->first()
            ->toArray();
        
        // 整理返回值並返回
        return $goods;
    }
    
    public function insertGood($lp, $gp, $active, $totalQty, $oid)
    {
        // 取得參數
        $data = [
            'lp' . '_tmp'       => $lp,
            'gp' . '_tmp'       => $gp,
            '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->goodManagementDb
            ->insert($data);
        $id = \DB::getPdo()->lastInsertId();
        // syslog
        $this->syslogManagementDb
            ->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 modifyGood($id, $lp, $gp, $active, $totalQty, $oid)
    {
        // 取得參數
        $data = [
            'lp' . '_tmp'       => $lp,
            'gp' . '_tmp'       => $gp,
            'active' . '_tmp'   => $active,
            'totalQty' . '_tmp' => $totalQty,
            '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;
    }
    
}