<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Services\Web\Api2021Service;
use App\Http\Services\CheckParamService;
use Illuminate\Support\Facades\Validator;
use App\Http\Services\ConstDef\GeneralConst;
use Illuminate\Support\Facades\Redis;
use App\Http\Services\Backend\DataManagement\SettingManagementService;
use Illuminate\Support\Facades\Cookie;

class Api2021Controller extends Controller
{
    
    private   $aSv;
    private   $cSv;
    protected $sisLog;
    protected $redis;
    private   $settingManagementSv;
    
    public function __construct()
    {
        $this->aSv = new Api2021Service();
        $this->cSv = new CheckParamService();
        $this->settingManagementSv = new SettingManagementService();
        $this->sisLog = [
            'type'  => GeneralConst::LOG_USER,
            'cdate' => date("Y-m-d H:i:s"),
        ];
    }
    
    public function __destruct()
    {
//        $this->aSv->syslog($this->sisLog);
    }
    
    // 申報系統(已廢棄)
    
    public function getinfo($hash)
    {
        // 業務驗證(解析HASH/是否封存/是否上架中)
        $hash = $this->aSv->hashParse($hash);
        if (!$this->aSv->hashCheck($hash)) return response()->json(["succ" => false, "err" => '代碼錯誤']);
        // 取得渲染用資訊
        $info = $this->aSv->getinfo($hash);
        
        // 返回
        return response()->json(["succ" => true, "err" => '', "info" => $info]);
    }
    
    public function saveimg($hash, Request $request)
    {
        // 參數驗證
        $messages = [
            'type.required' => 'type 必填',
            'type.regex'    => 'type 格式錯誤',
        ];
        $validate = Validator::make($request->all(), [
            'type' => ['required', 'regex:/^(' . implode('|', GeneralConst::$appendixValidateMap) . ')$/'],
        ], $messages);
        if ($validate->fails()) {
            $err = $validate->errors();
            $err_msg = "";
            foreach ($err->all() as $item) {
                $err_msg .= $item . "\n";
            }
            
            return response()->json(["succ" => false, "err" => $err_msg]);
        } else {
            $type = $request->input('type', '');
        }
        // 業務驗證(解析HASH/是否封存/是否上架中)
        $hash = $this->aSv->hashParse($hash);
        if (!$this->aSv->hashCheck($hash)) return response()->json(["succ" => false, "err" => '代碼錯誤']);
        // 路徑命名
        $dirname = 'material';
        $date = date("Ymd");
        if (!file_exists($dirname)) mkdir($dirname, 0777, true);
        if (!file_exists($dirname . '/' . GeneralConst::$appendixMap[ $type ]['web_path'])) mkdir($dirname . '/' . GeneralConst::$appendixMap[ $type ]['web_path'], 0777, true);
        if (!file_exists($dirname . '/' . GeneralConst::$appendixMap[ $type ]['web_path'] . '/' . $date)) mkdir($dirname . '/' . GeneralConst::$appendixMap[ $type ]['web_path'] . '/' . $date, 0777, true);
        $basepath = $dirname . '/' . GeneralConst::$appendixMap[ $type ]['web_path'] . '/' . $date . '/';
        // 保存圖片
        $time = md5(bcrypt(microtime()));
        $err = '';
        $url = '';
        if ($request->hasFile('img')) {
            if ($request->file('img')->isValid()) {
                if (!in_array($request->file('img')->getMimeType(), ['image/jpeg'])) $err = '只接受 jpg 圖片';
                if ($request->file('img')->getSize() > 5242880) $err = '圖片檔案太大'; // 5MB
                if ($err == '') {
                    $request->file('img')->move($basepath, $time . GeneralConst::$appendixMap[ $type ]['ext']);
                    $url = $basepath . $time . GeneralConst::$appendixMap[ $type ]['ext'];
                }
            } else {
                $err = '圖片格式有誤';
            }
        } else {
            $err = '圖片檔案太大';
        }
        if ($err != '') return response()->json(["succ" => false, "err" => $err]);
        
        // 返回
        return response()->json(["succ" => true, "err" => '', "url" => $url]);
    }
    
    public function savedata($hash, Request $request)
    {
        // 參數驗證
        $messages = [
            'name.required'        => '姓名必填',
            'name.min'             => '姓名字數限制為1-50',
            'name.max'             => '姓名字數限制為1-50',
            'identity.required'    => '身分證號必填',
            'identity.min'         => '身分證號格式錯誤',
            'identity.max'         => '身分證號格式錯誤',
            'add_host.required'    => '戶籍地址必填',
            'add_host.min'         => '戶籍地址字數限制為1-200',
            'add_host.max'         => '戶籍地址字數限制為1-200',
            'add_contact.required' => '通訊地址必填',
            'add_contact.min'      => '通訊地址字數限制為1-200',
            'add_contact.max'      => '通訊地址字數限制為1-200',
            'tel.required'         => '電話必填',
            'tel.min'              => '電話字數限制為1-20',
            'tel.max'              => '電話字數限制為1-20',
        ];
        $validate = Validator::make($request->all(), [
            'name'        => 'required|min:1|max:50',
            'identity'    => 'required|min:10|max:10',
            'add_host'    => 'required|min:1|max:200',
            'add_contact' => 'required|min:1|max:200',
            'tel'         => 'required|min:1|max:20',
        ], $messages);
        if ($validate->fails()) {
            $err = $validate->errors();
            $err_msg = "";
            foreach ($err->all() as $item) {
                $err_msg .= $item . "\n";
            }
            
            return response()->json(["succ" => false, "err" => $err_msg]);
        } else {
            $name = $request->input('name', '');
            $identity = $request->input('identity', '');
            $add_host = $request->input('add_host', '');
            $add_contact = $request->input('add_contact', '');
            $tel = $request->input('tel', '');
        }
        // 業務驗證(解析HASH/是否封存/是否上架中)
        $hash = $this->aSv->hashParse($hash);
        if (!$this->aSv->hashCheck($hash)) return response()->json(["succ" => false, "err" => '代碼錯誤']);
        // 業務驗證(身分證字號)
        if (!$this->cSv->isIdentity($identity)) return response()->json(["succ" => false, "err" => '身分證格式有誤']);
        // 業務驗證(是否有本獎項需要的素材)
        $info = $this->aSv->getinfo($hash);
        ${GeneralConst::APPENDIX_IDENTITY_FRONT} = '';
        ${GeneralConst::APPENDIX_IDENTITY_BACK} = '';
        ${GeneralConst::APPENDIX_PASSBOOK} = '';
        if ($info['is_ide'] == GeneralConst::PHOTO_YES) {
            ${GeneralConst::APPENDIX_IDENTITY_FRONT} = $request->input(GeneralConst::APPENDIX_IDENTITY_FRONT, '');
            ${GeneralConst::APPENDIX_IDENTITY_BACK} = $request->input(GeneralConst::APPENDIX_IDENTITY_BACK, '');
            if (!file_exists(${GeneralConst::APPENDIX_IDENTITY_FRONT})) {
                return response()->json(["succ" => false, "err" => '請重新上傳身分證正面']);
            } else {
                if (!preg_match('/^material\/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['web_path'] . '\/[0-9]{8}\/[a-zA-Z0-9]{32}' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['ext'] . '$/', ${GeneralConst::APPENDIX_IDENTITY_FRONT})) {
                    return response()->json(["succ" => false, "err" => '身分證正面路徑有問題']);
                }
            }
            if (!file_exists(${GeneralConst::APPENDIX_IDENTITY_BACK})) {
                return response()->json(["succ" => false, "err" => '請重新上傳身分證背面']);
            } else {
                if (!preg_match('/^material\/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['web_path'] . '\/[0-9]{8}\/[a-zA-Z0-9]{32}' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['ext'] . '$/', ${GeneralConst::APPENDIX_IDENTITY_BACK})) {
                    return response()->json(["succ" => false, "err" => '身分證背面路徑有問題']);
                }
            }
        }
        if ($info['id_acc'] == GeneralConst::PHOTO_YES) {
            ${GeneralConst::APPENDIX_PASSBOOK} = $request->input(GeneralConst::APPENDIX_PASSBOOK, '');
            if (!file_exists(${GeneralConst::APPENDIX_PASSBOOK})) {
                return response()->json(["succ" => false, "err" => '請重新上傳存摺']);
            } else {
                if (!preg_match('/^material\/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['web_path'] . '\/[0-9]{8}\/[a-zA-Z0-9]{32}' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['ext'] . '$/', ${GeneralConst::APPENDIX_PASSBOOK})) {
                    return response()->json(["succ" => false, "err" => '存摺路徑有問題']);
                }
            }
        }
        // 業務邏輯(製作PDF)
        $pdf_path = $this->aSv->makePDF($info, $name, $identity, $add_host, $add_contact, $tel, ${GeneralConst::APPENDIX_IDENTITY_FRONT}, ${GeneralConst::APPENDIX_IDENTITY_BACK}, ${GeneralConst::APPENDIX_PASSBOOK});
        // 業務邏輯(存入資料庫)
        $this->aSv->savedata($hash[1], $name, $identity, $add_host, $add_contact, $tel, ${GeneralConst::APPENDIX_IDENTITY_FRONT}, ${GeneralConst::APPENDIX_IDENTITY_BACK}, ${GeneralConst::APPENDIX_PASSBOOK}, $pdf_path);
        
        // 返回
        return response()->json(["succ" => true, "err" => '', "url" => $pdf_path]);
    }
    
    // 小遊戲 0518
    
    public function info($lineId)
    {
        // testonly 等待 N 秒
//        $now = time();
//        while(time() - $now < 6) {}

//        return response()->json(["succ" => true, "err" => '', "info" => []]);

        // testonly
//        $this->aSv->debuglog('server', [
//            'SERVER_ADDR' => $_SERVER['SERVER_ADDR'],
//            'lineUserId' => $lineId,
//        ]);

        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';

        // testonly
//        $this->aSv->debuglog([
//            'hashLineId' => $hashLineId,
//            'cookieGamecc' => Cookie::get('gamecc')
//        ]);

        // 業務驗證(是否加入LINE帳號的好友) [不存在 cookie 的時候就要進直通查詢，造成每天只要最多一次查詢就好]
        if (!Cookie::get('gamecc')) {
//        if (false) { // testonly
//        if (!$this->aSv->isAuthUserExists($lineId)) { // testonly 用資料庫檢查
            if (!$this->aSv->lineFriendCheck($lineId)) {
                $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
                $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);

                return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
            }
//            else { // testonly 用資料庫檢查
//                $this->aSv->addAuthUser($lineId);
//            }
        }

        // 業務邏輯(檢查有無本地資料，沒有就新增，順便取得個人資訊包含吉點)
        $playerinfo = $this->aSv->playerinfo($lineId);

        // 業務邏輯(取得當天所屬於的回合日期資訊，包含回合起訖日、本回合簽到狀態)
        // 業務邏輯(取得當回合的兌獎狀態，包含所有獎項以及可換與否)
        $roundinfo = $this->aSv->roundinfo($lineId);
        $goodinfo = $this->aSv->goodinfo($lineId);
        if (!$roundinfo && !$goodinfo) {
            // 遊戲時間已過 && 兌獎時間已過
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '活動已結束'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '活動已結束']);
        } else {
            // 要嘛都在期限內，要嘛一定至少兌獎在期限內因為他的時間範圍較大，這時候就要把回合簽到資訊清空
            if (!$roundinfo) $roundinfo = [];
        }

        // 業務邏輯(整理實際可兌換的獎項)
        foreach ($goodinfo as &$g) if ($playerinfo['gp'] < $g['gp']) $g['canuse'] = false;

        // 資料整理
        $info = [
            'playerinfo' => $playerinfo,
            'roundinfo'  => $roundinfo,
            'goodinfo'   => $goodinfo,
        ];
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['info' => $info], JSON_UNESCAPED_UNICODE);
        
        // 返回
        if (Cookie::get('gamecc')) {
            return response()->json(["succ" => true, "err" => '', "info" => $info]);
        } else {
            return response()->json(["succ" => true, "err" => '', "info" => $info])->cookie('gamecc', time(), 1440);
        }
    }
    
    public function game($lineId, Request $request)
    {
        // testonly 等待 N 秒
//        $now = time();
//        while(time() - $now < 6) {}

        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';

        // 業務驗證(是否加入LINE帳號的好友) [不存在 cookie 的時候就要進直通查詢，造成每天只要最多一次查詢就好]
//        if (false) { // testonly
        if (!Cookie::get('gamecc')) {
            if (!$this->aSv->lineFriendCheck($lineId)) {
                $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
                $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
                
                return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
            }
        }

        // 業務驗證(是否在本地有資料)
        if (!$this->aSv->isPlayer($lineId)) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請從首頁進入'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '請從首頁進入']);
        }

        // 業務驗證(是否今日已玩過遊戲與簽到)
//        if (false) { // testonly for 壓測
//        if ($lineId != 'Ud2534d6e1eed9cee2c919a993443352b') {
        if ($lineId != $this->settingManagementSv->getSetting()['GAME_TESTING_LINE_ID']) {
            if (!$this->aSv->canPlay($lineId)) {
                $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
                $this->sisLog['memoOut'] = json_encode(['msg' => '今日已玩過遊戲或者活動結束'], JSON_UNESCAPED_UNICODE);
                
                return response()->json(["succ" => false, "err" => '今日已玩過遊戲或者活動結束']);
            }
        }
        // 參數驗證
        $name = '';
        $messages = [
            'name.required' => '姓名必填',
            'name.max'      => '姓名最多100字',
        ];
        $validate = Validator::make($request->all(), [
            'name' => 'required|max:100',
        ], $messages);
        if ($validate->fails()) {
            $err = $validate->errors();
            $err_msg = "";
            foreach ($err->all() as $item) {
                $err_msg .= $item . "\n";
            }
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => $err_msg]);
        } else {
            $name = $request->input('name', '');
        }

        // 業務邏輯(玩遊戲簽到)
        $res = $this->aSv->playGameAndCheckin($lineId, $name);

        // 資料整理
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['res' => $res], JSON_UNESCAPED_UNICODE);
        
        // 返回
        if (Cookie::get('gamecc')) {
            return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "currentCheckinGp" => $res['currentCheckinGp'], "gpAll" => $res['gp']]);
        } else {
            return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "currentCheckinGp" => $res['currentCheckinGp'], "gpAll" => $res['gp']])->cookie('gamecc', time(), 1440);
        }
    }
    
    public function redeem($lineId, $gid, Request $request)
    {
        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';

        // 打點數量限制
        // 作用範圍: ALL
        $redis = new Redis();
        $redis::connect('localhost', 6379);
        $rlimitkey = $this->settingManagementSv->getSetting()['GAME_ESI_TEST_EVENT_KEY'];
        if (!$redis::exists($rlimitkey)) {
            $redis::set($rlimitkey, 1);
        } else {
            $redis::incr($rlimitkey);
        }
        if ($redis::get($rlimitkey) > $this->settingManagementSv->getSetting()['GAME_ESI_TEST_EVENT_LIMIT']) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '測試點數不能再打了'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '測試點數不能再打了']);
        }

//        \Log::debug($rlimitkey);
//        \Log::debug($redis::get($rlimitkey));
//        return response()->json(["succ" => true, "err" => 'okok']);

        // 業務驗證(防重)
        // 作用範圍: 以人為單位
        $rkey = $this->settingManagementSv->getSetting()['GAME_ESI_TEST_EVENT_LIMIT'] . '_' . $lineId;
        if (!$redis::setnx($rkey, 'true')) {
            $redis::expire($rkey, 15); // 15 秒防重(這樣就算業務在其他地方掛了，他也會自動失效)
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '您操作過快，請稍後再試'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '您操作過快，請稍後再試']);
        }

        // 業務驗證(是否加入LINE帳號的好友)
        if (!$this->aSv->lineFriendCheck($lineId)) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);

            return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
        }

        // 業務驗證(是否在本地有資料)
        if (!$this->aSv->isPlayer($lineId)) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請從首頁進入'], JSON_UNESCAPED_UNICODE);

            return response()->json(["succ" => false, "err" => '請從首頁進入']);
        }

        // 業務驗證(取得當回合的兌獎狀態，包含所有獎項以及可換與否，拿來比對驗證)
        $canuse = false;
        foreach ($this->aSv->goodinfo($lineId) as $g) if ($g['id'] == $gid) if ($g['canuse'] == true) $canuse = true;
        if (!$canuse) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '獎項不存在或沒有兌獎所需吉點'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '獎項不存在或沒有兌獎所需吉點']);
        }

        // 參數驗證
        $name = '';
        $messages = [
            'name.required' => '姓名必填',
            'name.max'      => '姓名最多100字',
        ];
        $validate = Validator::make($request->all(), [
            'name' => 'required|max:100',
        ], $messages);
        if ($validate->fails()) {
            $err = $validate->errors();
            $err_msg = "";
            foreach ($err->all() as $item) {
                $err_msg .= $item . "\n";
            }
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => $err_msg]);
        } else {
            $name = $request->input('name', '');
        }

        // 業務邏輯(兌點)
        $redeeminfo = $this->aSv->redeemGood($lineId, $gid, $name);
        if (!$redeeminfo) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '兌換失敗'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '兌換失敗']);
        }

        // 資料整理
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId, 'gid' => $gid], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['redeeminfo' => $redeeminfo], JSON_UNESCAPED_UNICODE);

        // 防重解鎖
        $redis::del($rkey);
        
        return response()->json(["succ" => true, "err" => '']);
    }
    
    public function test()
    {
//        if (Cookie::get('ttcc')) {
//            return response()->json(["succ" => true, "err" => '', "info" => 'I have cookie: ' . Cookie::get('ttcc')]);
//        } else {
//            return response()->json(["succ" => true, "err" => '', "info" => 'no cookie'])->cookie('ttcc', '123', 1440);
//        }
        
        return true;
//        return $this->aSv->esiFlowBak();
        //return $_SERVER['SERVER_ADDR'] ?? 'no';
    }
    
    // 類發票
    
    public function friendchk($lineId)
    {
        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['kk'] = 'activity';
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';
        // 業務驗證(是否加入LINE帳號的好友)
        if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
        }
        // 資料整理
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['res' => ''], JSON_UNESCAPED_UNICODE);
        
        // 返回
        return response()->json(["succ" => true, "err" => '']);
    }
    
    public function activities($lineId)
    {
        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['kk'] = 'activity';
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';
        // 業務驗證(是否加入LINE帳號的好友)
        if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
        }
        // 業務邏輯(取得活動時間資訊)
        $activities = $this->aSv->activities();
        // 資料整理
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['res' => $activities], JSON_UNESCAPED_UNICODE);
        
        // 返回
        return response()->json(["succ" => true, "err" => '', "data" => $activities]);
    }
    
    public function setdoc($lineId, Request $request)
    {
        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['kk'] = 'activity';
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';
        // 業務驗證(是否加入LINE帳號的好友)
//        if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
//            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
//            $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
//
//            return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
//        }
        // 參數驗證
        $messages = [
            'doc.required' => '圖片必填',
        ];
        $validate = Validator::make($request->all(), [
            'doc' => 'required',
        ], $messages);
        if ($validate->fails()) {
            $err = $validate->errors();
            $err_msg = "";
            foreach ($err->all() as $item) {
                $err_msg .= $item . "\n";
            }
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => $err_msg]);
        } else {
            $doc = $request->input('doc', '');
            $doc = explode(',', $doc);
            $doc = base64_decode($doc[1]);
        }
        // 業務邏輯(圖片辨識)
        $data = $this->aSv->setdoc($doc);
        // 資料整理
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['res' => $data], JSON_UNESCAPED_UNICODE);
        
        // 返回
        return response()->json(["succ" => true, "err" => '', "data" => $data]);
    }
    
    public function reqreceipt($lineId, Request $request)
    {
        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['kk'] = 'activity';
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';
        // 業務驗證(是否加入LINE帳號的好友)
        if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
        }
        // 參數驗證
        $messages = [
            'name.required'          => '姓名必填',
            'name.max'               => '姓名最多64字',
            'order_date.required'    => '登錄日期必填',
            'order_date.date_format' => '登錄日期格式錯誤',
            'account_bank.required'  => '銀行代碼必填',
            'account_bank.regex'     => '銀行代碼格式錯誤',
            'account_no.required'    => '銀行帳號必填',
            'account_no.regex'       => '銀行帳號格式錯誤',
            'order_no.required'      => '訂單編號必填',
            'order_no.regex'         => '訂單編號格式錯誤',
        ];
        $validate = Validator::make($request->all(), [
            'name'         => 'required|max:64',
            'order_date'   => 'required|date_format:Y-m-d',
            'account_bank' => ['required', 'regex:/^[0-9]{1,10}$/'],
            'account_no'   => ['required', 'regex:/^[0-9]{1,30}$/'],
            'order_no'     => ['required', 'regex:/^[A-Za-z0-9]{1,30}$/'],
        ], $messages);
        if ($validate->fails()) {
            $err = $validate->errors();
            $err_msg = "";
            foreach ($err->all() as $item) {
                $err_msg .= $item . "\n";
            }
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => $err_msg]);
        } else {
            $info = [
                'name'         => $request->input('name', ''),
                'order_date'   => $request->input('order_date', ''),
                'account_bank' => $request->input('account_bank', ''),
                'account_no'   => $request->input('account_no', ''),
                'order_no'     => $request->input('order_no', ''),
            ];
        }
        // 業務驗證(取得當前的登錄區間)
        $activityinfo = $this->aSv->activityinfo();
        if (!$activityinfo) {
            // 尚未到達登錄時間
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '尚未到達登錄時間'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '尚未到達登錄時間']);
        }
        // 業務驗證(收據時間也要在活動時間內)
        if ($activityinfo[0]['reqTimeBegin'] > $info['order_date'] || $activityinfo[0]['reqTimeFinal'] < $info['order_date']) {
            // 尚未到達登錄時間
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '收據日期錯誤'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '收據日期錯誤']);
        }
        // 業務邏輯(登錄)
        $info['lineId'] = $lineId;
        $info['aid'] = $activityinfo[0]['id'];
        $id = $this->aSv->reqreceipt($info);
        if ($id == 0) {
            // 完全相同的資料不能登錄
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '您已登錄過這筆資料'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '您已登錄過這筆資料']);
        }
        // 資料整理
        $this->sisLog['memoIn'] = json_encode(['info' => $info], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['res' => $id], JSON_UNESCAPED_UNICODE);
        
        // 返回
        return response()->json(["succ" => true, "err" => '']);
    }
    
    public function lstreceipt($lineId)
    {
        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['kk'] = 'activity';
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';
        // 業務驗證(是否加入LINE帳號的好友)
        if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
        }
        // 業務邏輯(取得列表)
        $data = $this->aSv->lstreceipt($lineId);
        // 資料整理
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['res' => $data], JSON_UNESCAPED_UNICODE);
        
        // 返回
        return response()->json(["succ" => true, "err" => '', "data" => $data]);
    }
    
    public function redeemreceipt($lineId, $rid, Request $request)
    {
        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['kk'] = 'activity';
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';
        // 業務驗證(是否加入LINE帳號的好友)
        if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
        }
        // 業務驗證(該紀錄狀態是否可執行本動作/該紀錄的所屬活動是否在登錄的時間範圍內)
        if (!$this->aSv->redeemCheck($lineId, (integer)$rid)) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'rid' => $rid], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '權限錯誤'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '權限錯誤']);
        }
        // 業務邏輯(更改狀態)
        $this->aSv->redeemExecute($lineId, (integer)$rid);
        // 資料整理
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['res' => ''], JSON_UNESCAPED_UNICODE);
        
        // 返回
        return response()->json(["succ" => true, "err" => '']);
    }
    
    public function getreceipt($lineId, $rid, Request $request)
    {
        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['kk'] = 'activity';
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';
        // 業務驗證(防重)
        // 作用範圍: 以人為單位
        $redis = new Redis();
        $redis::connect('localhost', 6379);
        $rkey = $this->settingManagementSv->getSetting()['RECEIPT_ESI_TEST_EVENT_KEY'] . '_' . $lineId;
        if (!$redis::setnx($rkey, 'true')) {
            $redis::expire($rkey, 15); // 15 秒防重(這樣就算業務在其他地方掛了，他也會自動失效)
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '您操作過快，請稍後再試'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '您操作過快，請稍後再試']);
        }
        // 業務驗證(是否加入LINE帳號的好友)
        if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
        }
        // 業務驗證(該紀錄狀態是否可執行本動作/該紀錄的所屬活動是否在登錄的時間範圍內)
        if (!$this->aSv->getCheck($lineId, (integer)$rid)) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'rid' => $rid], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '權限錯誤'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '權限錯誤']);
        }
        // 業務邏輯(更改狀態)
        $getinfo = $this->aSv->getExecute($lineId, (integer)$rid);
        if (!$getinfo) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '兌換失敗'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '兌換失敗']);
        }
        // 資料整理
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['res' => ''], JSON_UNESCAPED_UNICODE);
        // 防重解鎖
        $redis::del($rkey);
        
        // 返回
        return response()->json(["succ" => true, "err" => '']);
    }
    
    // 串門子
    
    public function tinfo($lineId, Request $request)
    {
        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['kk'] = 'tmz';
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';
        // 業務驗證(是否加入LINE帳號的好友) [不存在 cookie 的時候就要進直通查詢，造成每天只要最多一次查詢就好]
        if (!Cookie::get('tgamecc')) {
            if (!$this->aSv->lineFriendCheck($lineId, 'tmz')) {
                $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
                $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
                
                return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
            }
        }
        // 業務邏輯(檢查有無本地資料，沒有就新增，順便取得個人資訊包含吉點)
        $playerinfo = $this->aSv->tplayerinfo($lineId);
        // 參數驗證
        $name = '';
        $messages = [
            'name.required' => '姓名必填',
            'name.max'      => '姓名最多100字',
        ];
        $validate = Validator::make($request->all(), [
            'name' => 'required|max:100',
        ], $messages);
        if ($validate->fails()) {
            $err = $validate->errors();
            $err_msg = "";
            foreach ($err->all() as $item) {
                $err_msg .= $item . "\n";
            }
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => $err_msg]);
        } else {
            $name = $request->input('name', '');
        }
        // 業務邏輯(今日簽到)
        $this->aSv->tcheckin($lineId, $name);
        // 業務邏輯(取得當天所屬於的回合日期資訊，包含回合起訖日、本回合簽到狀態)
        $roundinfo = $this->aSv->troundinfo($lineId);
        if (!$roundinfo) {
            // 遊戲時間已過 && 兌獎時間已過
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '活動已結束'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '活動已結束']);
        } else {
            // 要嘛都在期限內，要嘛一定至少兌獎在期限內因為他的時間範圍較大，這時候就要把回合簽到資訊清空
            if (!$roundinfo) $roundinfo = [];
        }
        // 業務邏輯(取得是否可以轉蛋的資格)
        $redeeminfo = $this->aSv->tredeeminfo($lineId);
        // 資料整理
        $info = [
            'playerinfo' => $playerinfo,
            'roundinfo'  => $roundinfo,
            'redeeminfo' => $redeeminfo,
        ];
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['info' => $info], JSON_UNESCAPED_UNICODE);
        
        // 返回
        if (Cookie::get('tgamecc')) {
            return response()->json(["succ" => true, "err" => '', "info" => $info]);
        } else {
            return response()->json(["succ" => true, "err" => '', "info" => $info])->cookie('tgamecc', time(), 1440);
        }
    }
    
    public function tgame($lineId, Request $request)
    {
        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['kk'] = 'tmz';
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';
        // 業務驗證(是否加入LINE帳號的好友) [不存在 cookie 的時候就要進直通查詢，造成每天只要最多一次查詢就好]
        if (!Cookie::get('tgamecc')) {
            if (!$this->aSv->lineFriendCheck($lineId, 'tmz')) {
                $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
                $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
                
                return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
            }
        }
        // 業務驗證(是否在本地有資料)
        if (!$this->aSv->isTPlayer($lineId)) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請從首頁進入'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '請從首頁進入']);
        }
        // 參數驗證
        $name = '';
        $c = 0;
        $messages = [
            'name.required' => '姓名必填',
            'name.max'      => '姓名最多100字',
            'c.required'    => '九宮格必填',
        ];
        $validate = Validator::make($request->all(), [
            'name' => 'required|max:100',
            'c'    => 'required|numeric|min:1|max:9',
        ], $messages);
        if ($validate->fails()) {
            $err = $validate->errors();
            $err_msg = "";
            foreach ($err->all() as $item) {
                $err_msg .= $item . "\n";
            }
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => $err_msg]);
        } else {
            $name = $request->input('name', '');
            $c = $request->input('c', 0);
        }
        // 業務邏輯(玩遊戲)
        $res = $this->aSv->playTGame($lineId, $name, $c);
        if (!$res) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '已回答過囉'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '已回答過囉']);
        }
        // 資料整理
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId, 'c' => $c], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['res' => $res], JSON_UNESCAPED_UNICODE);
        
        // 返回
        if (Cookie::get('tgamecc')) {
            return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "playerinfo" => $res['gp']]);
        } else {
            return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "playerinfo" => $res['gp']])->cookie('tgamecc', time(), 1440);
        }
    }
    
    public function tredeem($lineId, Request $request)
    {
        $this->sisLog['func'] = __FUNCTION__;
        $this->sisLog['k'] = $lineId;
        $this->sisLog['kk'] = 'tmz';
        $this->sisLog['memoIn'] = '';
        $this->sisLog['memoOut'] = '';
        // 打點數量限制
        // 作用範圍: ALL
        $redis = new Redis();
        $redis::connect('localhost', 6379);
        $rlimitkey = $this->settingManagementSv->getSetting()['TMZ_ESI_TEST_EVENT_KEY'];
        if (!$redis::exists($rlimitkey)) {
            $redis::set($rlimitkey, 1);
        } else {
            $redis::incr($rlimitkey);
        }
        if ($redis::get($rlimitkey) > $this->settingManagementSv->getSetting()['TMZ_ESI_TEST_EVENT_LIMIT']) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '測試點數不能再打了'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '測試點數不能再打了']);
        }
        // 業務驗證(防重)
        // 作用範圍: 以人為單位
        $rkey = $this->settingManagementSv->getSetting()['TMZ_ESI_TEST_EVENT_LIMIT'] . '_' . $lineId;
        if (!$redis::setnx($rkey, 'true')) {
            $redis::expire($rkey, 15); // 15 秒防重(這樣就算業務在其他地方掛了，他也會自動失效)
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '您操作過快，請稍後再試'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '您操作過快，請稍後再試']);
        }
        // 業務驗證(是否加入LINE帳號的好友)
        if (!$this->aSv->lineFriendCheck($lineId, 'tmz')) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
        }
        // 業務驗證(是否在本地有資料)
        if (!$this->aSv->isTPlayer($lineId)) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '請從首頁進入'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '請從首頁進入']);
        }
        // 參數驗證
        $name = '';
        $messages = [
            'name.required' => '姓名必填',
            'name.max'      => '姓名最多100字',
        ];
        $validate = Validator::make($request->all(), [
            'name' => 'required|max:100',
        ], $messages);
        if ($validate->fails()) {
            $err = $validate->errors();
            $err_msg = "";
            foreach ($err->all() as $item) {
                $err_msg .= $item . "\n";
            }
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => $err_msg]);
        } else {
            $name = $request->input('name', '');
        }
        // 業務邏輯(可否轉蛋再次確認)
        if (!$this->aSv->tredeeminfo($lineId)) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '吉點不夠不能轉蛋'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '吉點不夠不能轉蛋']);
        }
        // 業務邏輯(轉蛋兌點)
        $redeeminfo = $this->aSv->redeemTGood($lineId, $name);
        if (!$redeeminfo) {
            $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
            $this->sisLog['memoOut'] = json_encode(['msg' => '銘謝惠顧'], JSON_UNESCAPED_UNICODE);
            
            return response()->json(["succ" => false, "err" => '銘謝惠顧']);
        }
        // 資料整理
        $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
        $this->sisLog['memoOut'] = json_encode(['redeeminfo' => $redeeminfo], JSON_UNESCAPED_UNICODE);
        unset($redeeminfo['playerinfo']['lineId']);
        unset($redeeminfo['redeeminfo']['fromIssuedQty']);
        unset($redeeminfo['redeeminfo']['toIssuedQty']);
        unset($redeeminfo['session']);
        // 防重解鎖
        $redis::del($rkey);
        
        return response()->json(["succ" => true, "err" => '', 'info' => $redeeminfo]);
    }
    
}
