| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070 | <?php
namespace App\Http\Services\Web;
use Exception;
use App\Models\Web\Event;
use App\Models\Web\Item;
use App\Models\Web\Submit;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use GuzzleHttp\Client;
use App\Http\Services\ConstDef\GeneralConst;
use Illuminate\Support\Facades\DB;
use Dompdf\Dompdf;
use Dompdf\Options;
use App\Models\Web\Syslog;
use App\Models\Web\Syslogact;
use App\Models\Web\Syslogt;
use App\Models\Web\Player;
use App\Models\Web\Round;
use App\Models\Web\Good;
use App\Models\Web\Session;
use App\Models\Web\CheckinGpAlloc;
use App\Models\Web\GameGpRatio;
use App\Models\Web\Activity;
use App\Models\Web\Receipt;
use App\Models\Web\TPlayer;
use App\Models\Web\TRound;
use App\Models\Web\TGood;
use App\Models\Web\TSession;
use App\Models\Web\TCheckinGpAlloc;
use App\Models\Web\AuthLineUsers;
use App\Http\Services\Backend\DataManagement\SettingManagementService;
class Api2021Service
{
    
    private $c;
    private $eventManagementDb;
    private $itemManagementDb;
    private $submitManagementDb;
    private $syslogManagementDb;
    private $syslogactManagementDb;
    private $syslogtManagementDb;
    private $playerManagementDb;
    private $roundManagementDb;
    private $goodManagementDb;
    private $sessionManagementDb;
    private $checkinManagementDb;
    private $gameManagementDb;
    private $activityManagementDb;
    private $receiptManagementDb;
    private $tplayerManagementDb;
    private $troundManagementDb;
    private $tgoodManagementDb;
    private $tsessionManagementDb;
    private $tcheckinManagementDb;
    private $authLineUsersDb;
    private $settingManagementSv;
    private $imgC;
    private $basedir;
    
    public function __construct()
    {
        date_default_timezone_set("Asia/Taipei");
        $this->c = new Client();
        // 建構 model 調用器
        $this->eventManagementDb = new Event();
        $this->itemManagementDb = new Item();
        $this->submitManagementDb = new Submit();
        $this->syslogManagementDb = new Syslog();
        $this->syslogactManagementDb = new Syslogact();
        $this->syslogtManagementDb = new Syslogt();
        $this->playerManagementDb = new Player();
        $this->roundManagementDb = new Round();
        $this->goodManagementDb = new Good();
        $this->sessionManagementDb = new Session();
        $this->checkinManagementDb = new CheckinGpAlloc();
        $this->gameManagementDb = new GameGpRatio();
        $this->activityManagementDb = new Activity();
        $this->receiptManagementDb = new Receipt();
        $this->tplayerManagementDb = new TPlayer();
        $this->troundManagementDb = new TRound();
        $this->tsessionManagementDb = new TSession();
        $this->tgoodManagementDb = new TGood();
        $this->tcheckinManagementDb = new TCheckinGpAlloc();
        $this->authLineUsersDb = new AuthLineUsers();
        $this->settingManagementSv = new SettingManagementService();
        $this->settingManagementSv = new SettingManagementService();
        $this->basedir = preg_replace('/\/app\/.*/', '/public/', __DIR__);
        $this->imgC = new ImageAnnotatorClient([
            'credentials' => $this->basedir . 'private/google.json',
        ]);
    }
    
    // 申報系統(已廢棄)
    
    public function hashParse($hash)
    {
        $hash = \DB::select("SELECT FROM_BASE64(AES_DECRYPT(UNHEX(\"" . $hash . "\"), \"" . env('KK') . "\")) as str;");
        
        return explode(',', $hash[0]->str);
    }
    
    public function hashCheck($hash)
    {
        // 格式判斷
        if (count($hash) != 2) return false;
        // 業務邏輯判斷
        $items = $this->eventManagementDb
            ->leftJoin('items', 'events.id', '=', 'items.eid')
            ->select([
                'events.archive',
            ])
            ->where('events.id', $hash[0])
            ->where('items.id', $hash[1])
            ->where('events.date_begin', '<=', date("Y-m-d"))
            ->where('events.date_final', '>=', date("Y-m-d"))
            ->get()
            ->toArray();
        if (count($items) == 0) return false;
        
        return ($items[0]['archive'] == GeneralConst::ARCHIVE_NO) ? true : false;
    }
    
    public function getinfo($hash)
    {
        // 業務邏輯判斷
        $info = $this->eventManagementDb
            ->leftJoin('items', 'events.id', '=', 'items.eid')
            ->select([
                'events.name',
                'events.kv',
                'events.email',
                'events.tel',
                \DB::raw("items.name as item_name"),
                'items.is_ide',
                'items.id_acc',
            ])
            ->where('events.id', $hash[0])
            ->where('items.id', $hash[1])
            ->where('events.date_begin', '<=', date("Y-m-d"))
            ->where('events.date_final', '>=', date("Y-m-d"))
            ->first()
            ->toArray();
        
        return $info;
    }
    
    public function makePDF($info, $name, $identity, $add_host, $add_contact, $tel, $ida, $idb, $acc)
    {
        // 初期設定
        $options = new Options();
        $options->set('isRemoteEnabled', true);
        $options->set('chroot', '/');
        $dompdf = new Dompdf();
        $dompdf->setOptions($options);
        // 內容拼湊
        $html =
            '<!doctype html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><style> .font-zh { font-family: "msjh" } </style></head><body>' .
            '<span class="font-zh">申報資料</span><hr>' .
            '<span class="font-zh">姓名: ' . $name . '</span><br>' .
            '<span class="font-zh">身分證字號: ' . $identity . '</span><br>' .
            '<span class="font-zh">戶籍地址: ' . $add_host . '</span><br>' .
            '<span class="font-zh">通訊地址: ' . $add_contact . '</span><br>' .
            '<span class="font-zh">電話: ' . $tel . '</span><br>' .
            '<span class="font-zh"><br>' .
            '<span class="font-zh">檢附文件:</span><hr>' .
            '[is_ide]' .
            '[id_acc]' .
            '</body></html>';
        if ($info['is_ide'] == GeneralConst::PHOTO_YES) {
            $html = str_replace('[is_ide]',
                '<div class="page_break"><span class="font-zh">身分證正面</span><br>' .
                '<img src="' . env('LOCAL_PATH') . 'public/' . $ida . '" /></div>' .
                '<div class="page_break"><span class="font-zh">身分證背面</span><br>' .
                '<img src="' . env('LOCAL_PATH') . 'public/' . $idb . '" /></div>'
                , $html);
        } else {
            $html = str_replace('[is_ide]', '', $html);
        }
        if ($info['id_acc'] == GeneralConst::PHOTO_YES) {
            $html = str_replace('[id_acc]',
                '<div class="page_break"><span class="font-zh">存摺</span><br>' .
                '<img src="' . env('LOCAL_PATH') . 'public/' . $acc . '" /></div>'
                , $html);
        } else {
            $html = str_replace('[id_acc]', '', $html);
        }
        $dompdf->setPaper('A4');
        $dompdf->loadHtml($html, 'UTF-8');
        $dompdf->render();
        $output = $dompdf->output();
        // 路徑命名
        $dirname = 'material';
        $date = date("Ymd");
        if (!file_exists($dirname)) mkdir($dirname, 0777, true);
        if (!file_exists($dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'])) mkdir($dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'], 0777, true);
        if (!file_exists($dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'] . '/' . $date)) mkdir($dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'] . '/' . $date, 0777, true);
        $basepath = $dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'] . '/' . $date . '/';
        $time = md5(bcrypt(microtime()));
        file_put_contents($basepath . $time . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['ext'], $output);
        
        return $basepath . $time . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['ext'];
    }
    
    public function savedata($iid, $name, $identity, $add_host, $add_contact, $tel, $ida, $idb, $acc, $pdf)
    {
        // 取得參數
        // 調用資料庫(或者其他業務邏輯)
        $this->submitManagementDb
            ->insert([
                'iid'                                                                      => $iid,
                'name'                                                                     => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $name . "'), \"" . env('KK') . "\"))"),
                'identity'                                                                 => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $identity . "'), \"" . env('KK') . "\"))"),
                'add_host'                                                                 => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $add_host . "'), \"" . env('KK') . "\"))"),
                'add_contact'                                                              => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $add_contact . "'), \"" . env('KK') . "\"))"),
                'tel'                                                                      => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $tel . "'), \"" . env('KK') . "\"))"),
                GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['sql'] => $ida,
                GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['sql']  => $idb,
                GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['sql']       => $acc,
                GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['sql']    => $pdf,
                'cdate'                                                                    => date('Y-m-d H:i:s'),
            ]);
        $id = \DB::getPdo()->lastInsertId();
        
        // 整理返回值並返回
        return $id;
    }
    
    // 小遊戲
    
    // LINE 好友判斷 (實作介接直通部分)
    public function lineFriendCheck($lineId, $from = null)
    {
        try {
            // 本次調用的參數整理
            $uid = $lineId;
            $rqid = $this->getRqId();
            // 取得 seedKey
            $seedKey = $this->getSeedKey();
            // 隨機產生多樣性資料
            $divData = $this->getDivData();
            // 產生本次調用的 sessionKey
            $sessionKey = $this->getSessionKey($seedKey, $divData);
            // 整理押碼用的資料欄內容
            $msgRaw = $this->getMsgRaw([
                'RqClient' => env('RQ_CLIENT'),
                'RqId'     => $rqid,
                'UID'      => $uid,
            ]);
            $msgHash = $this->getMsgHash($msgRaw);
            // 建立ICV
            $icv = $this->getICV();
            // 建立押碼
            $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
            $mac = $this->getMAC($macRaw);
            // 參數打包
            $param = [
                'RqClient' => env('RQ_CLIENT'),
                'RqId'     => $rqid,
                'ICV'      => $icv,
                'DivData'  => $divData,
                'MAC'      => $mac,
                'UID'      => $uid,
            ];
            // 相關中繼資料打包(LOG 用)
            $logdata = [
                'sessionKey' => $sessionKey,
                'msgRaw'     => $msgRaw,
                'msgHash'    => $msgHash,
                'macRaw'     => $macRaw,
            ];
            // 憑證參數整理
            $curlparam = [
                CURLOPT_SSLCERTTYPE    => 'PEM',
                CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
                CURLOPT_SSLKEYTYPE     => 'PEM',
                CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
                CURLOPT_SSL_VERIFYHOST => 0,
                CURLOPT_SSL_VERIFYPEER => 0,
            ];
            if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
            if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
            // 調用
            $req = [
                'curl'    => $curlparam,
                'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
                'headers' => [
                    'Content-Type' => 'application/json',
                    'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                    'Accept'       => '*/*',
                ],
                'timeout' => 15, // Response timeout
                'connect_timeout' => 10, // Connection timeout
//                'on_stats' => function (\GuzzleHttp\TransferStats $stats) use ($rqid) {
//                    if ($stats->hasResponse()) {
//                        $this->debuglog('lineFriendCheck-on_stats_ok', [
//                            'rqid' => $rqid ?? 0
//                        ], [
//                            'transferTime' => $stats->getTransferTime(),
//                            'handlerStats' => $stats->getHandlerStats(),
//                        ]);
//                    } else {
//                        $this->debuglog('lineFriendCheck-on_stats_ng', [
//                            'rqid' => $rqid ?? 0
//                        ], [
//                            'handlerErrorData' => $stats->getHandlerErrorData(),
//                        ]);
//                    }
//                }
            ];
            if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
            $requestTime = date("Y-m-d H:i:s");
            $logdata['requestTime'] = $requestTime;
            $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/contact/QueryQualification', $req);
            $res = $response->getBody();
            // 直通紀錄
            $data = [
                'type'  => GeneralConst::LOG_ESI,
                'func'  => 'QueryQualification',
                'k'     => $uid,
                'cdate' => date("Y-m-d H:i:s"),
            ];
            if ($from) $data['kk'] = $from; // 區分來源(紀錄的 log 表不同)
            $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
            $data['memoOut'] = $res;
            $this->syslog($data);
            // 返回值判斷
            $res = json_decode($res);
            $rcode = $res->RCode ?? '';
            if ($rcode != '0000') return false;
            $isfollow = $res->Result->IsFollow ?? false;
            $ismember = $res->Result->IsTWPMember ?? false;
            if (!$isfollow || !$ismember) return false;
        } catch (Exception $ex) {
            // 錯誤可能發生在 memoIn 或是 out
//            $data['memoIn'] = json_encode(['error' => 'exception', 'param' => array_merge($param, $logdata)], JSON_UNESCAPED_UNICODE);
//            $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
//            $this->syslog($data);
            $this->debuglog('lineFriendCheck', [
                'rqid' => $rqid ?? 0
            ], [
                'errCode' => $ex->getCode(),
                'requestTime' => $requestTime ?? '',
                'logTime' => date("Y-m-d H:i:s"),
                'errMsg' => $ex->getMessage(),
            ]);
            return false;
        }
        
        return true;
    }
    public function isAuthUserExists($lineUserId)
    {
        return $this->authLineUsersDb->where('lineUserId', $lineUserId)->exists();
    }
    public function addAuthUser($lineUserId)
    {
        $this->authLineUsersDb->insert([
            'lineUserId' => $lineUserId
        ]);
    }
    // LINE 取得個資(建立在好友判斷後才可以執行)
    public function lineProfile($lineId)
    {
        $response = $this->c->request('GET', env('LINE_API_BASE') . 'bot/profile/' . $lineId, [
            'headers' => [
                'Content-Type'    => 'application/json; charset=utf-8',
                'Accept'          => '*/*',
                'Authorization'   => 'Bearer ' . env('LINE_CHANNEL_TOKEN'),
                'Accept-Encoding' => 'gzip, deflate, br',
            ]
        ]);
        $profile = json_decode($response->getBody());
        
        return $profile;
    }
    
    // 新增或取得用戶資訊
    public function playerinfo($lineId)
    {
        // 取得用戶在本地的資訊
        $playerinfo = $this->playerManagementDb
            ->select(['id'])
            ->where('lineId', $lineId)
            ->get()
            ->toArray();
        // 取得用戶在 LINE 上的 profile
//        $profile = $this->lineProfile($lineId);
        if (count($playerinfo) == 0) {
            // 用戶不在本地資料庫,新增
            // 寫入資料表
            $d = [
                'lineId'    => $lineId,
                'userName'  => '',
                'userPhoto' => '',
                'gp'        => 0,
                'cdate'     => date("Y-m-d H:i:s"),
                'mdate'     => date("Y-m-d H:i:s"),
            ];
            $this->playerManagementDb
                ->insert($d);
            $id = \DB::getPdo()->lastInsertId();
            // 替用互打一次性標籤
            $this->playerTag($lineId);
        } else {
            // 用戶的 LINE 資訊可能會更新,所以這個 API 還身兼更新資訊的功能
//            $d = [
//                'userName'  => $profile->displayName,
//                'userPhoto' => $profile->pictureUrl,
//            ];
//            $this->playerManagementDb
//                ->where('lineId', $lineId)
//                ->update($d);
        }
        // 再重新查一遍
        $playerinfo = $this->playerManagementDb
            ->select([
//                'id',
'lineId',
//                'userName',
//                'userPhoto',
'gp',
//                'cdate',
//                'mdate',
            ])
            ->where('lineId', $lineId)
            ->first()
            ->toArray();
        
        return $playerinfo;
    }
    
    // 檢查是否在本地有資料
    public function isPlayer($lineId)
    {
        $playerinfo = $this->playerManagementDb->select(['id'])->where('lineId', $lineId)->get()->toArray();
        
        return (count($playerinfo) > 0);
    }
    
    // 取得回合資訊
    public function roundinfo($lineId)
    {
        // 回合資訊
        $roundinfo = $this->roundManagementDb
            ->select(['id', 'dateBegin', 'dateFinal'])
            ->where('dateFinal', '>=', date("Y-m-d"))
            ->where('dateBegin', '<=', date("Y-m-d"))
            ->where('active', '=', GeneralConst::ACTIVE_YES)
            ->get();
        if (count($roundinfo) == 0) {
            return false;
        } else {
            $roundinfo = $roundinfo->toArray();
        }
        // 簽到資訊
        $checkininfo = $this->sessionManagementDb
            ->select(['eventDate', 'currentGameGp'])
            ->where('lineId', $lineId)
            ->where('rid', $roundinfo[0]['id'])
            ->where('gid', '<>', 0) // 僅篩選簽到或遊戲
            ->orderby('id', 'asc') // 確保按照時間排
            ->get()
            ->toArray();
        // 整理
        $res = [
            'dateBegin' => $roundinfo[0]['dateBegin'],
            'dateFinal' => $roundinfo[0]['dateFinal'],
            'today'     => date("Y-m-d"),
            'checkins'  => [],
        ];
        foreach ($checkininfo as $c) {
            // 相同天可能會有重新再玩,因此會有超過1筆資料的可能,不想使用 SQL group by 耗能
            $res['checkins'][ $c['eventDate'] ] = [
                'date'    => $c['eventDate'],
                'canplay' => ($c['eventDate'] != date("Y-m-d")) ? false : ($c['currentGameGp'] < 0) ? true : false,
            ];
        }
        $res['checkins'] = array_values($res['checkins']);
        
        return $res;
    }
    
    // 取得獎項兌換資訊
    public function goodinfo($lineId)
    {
        // 獎項資訊
        $goodinfo = $this->goodManagementDb
            ->select(['id', 'lp', 'gp', 'active', 'totalQty', 'issuedQty'])
            ->get()
            ->toArray();
        // 一定有獎項所以不防呆
        // 回合資訊 (注意用於兌獎資訊的日期區間判定與玩遊戲不同)
        $roundinfo = $this->roundManagementDb
            ->select(['id'])
            ->where(\DB::raw("DATE_ADD(dateFinal, INTERVAL +redeemExtra DAY)"), '>=', date("Y-m-d"))
            ->where('dateBegin', '<=', date("Y-m-d"))
            ->where('active', '=', GeneralConst::ACTIVE_YES)
            ->get();
        // 若由 INFO 調用,則會先調用回合資訊確保來到這邊時一定拿到數值
        // 若由 REDEEM 調用,由於回合的日期限制比兌換的日期限制來的窄,不能優先於函數執行
        //  因此這邊有可能拿不到值,需要防呆
        if (count($roundinfo) == 0) {
            return false;
        } else {
            $roundinfo = $roundinfo->toArray();
        }
        // player 準備資料
        $playerinfo = $this->playerManagementDb->select(['gp'])->where('lineId', $lineId)->get();
        if (count($playerinfo) == 0) {
            return false;
        } else {
            $playerinfo = $playerinfo->toArray();
        }
        $mygp = $playerinfo[0]['gp'];
        // 兌獎資訊
        $redeeminfo = $this->sessionManagementDb
            ->select(['cid']) // 獎項 ID
            ->where('lineId', $lineId)
            ->where('rid', $roundinfo[0]['id'])
            ->where('cid', '<>', 0) // 僅篩選兌獎
            ->get()
            ->toArray();
        $redeemIds = [];
        foreach ($redeeminfo as $r) $redeemIds[] = $r['cid'];
        // 整理
        $res = [];
        foreach ($goodinfo as $g) {
            // 稽核用,如果已核發數量 >= 可發數量,就不顯示
            if ($g['issuedQty'] >= $g['totalQty'] && $g['totalQty'] > 0) {
                continue;
            }
            // 可用性限制
            $issoldout = false;
            $isredeemed = false;
            $isnomoney = false;
            $canuse = false;
            if ($g['totalQty'] - $g['issuedQty'] <= $this->settingManagementSv->getSetting()['GAME_ISSUE_BUFFER']) $issoldout = true;
            if (in_array($g['id'], $redeemIds)) $isredeemed = true;
            if ($mygp < $g['gp']) $isnomoney = true;
            if (
                ($g['totalQty'] - $g['issuedQty'] > $this->settingManagementSv->getSetting()['GAME_ISSUE_BUFFER']) // 核發數量限制還夠
                && (!in_array($g['id'], $redeemIds))   // 本回合沒有兌換過
                && ($mygp >= $g['gp'])                 // 自己的吉點足夠
            ) {
                $canuse = true;
            }
            // 最後一道防線就是新添加的 active 欄位(一般來說如果其他三個偵測有任何一個是 true 的話,canuse 就是 false ;但是當其他三個偵測都是 false 的情況如果 canuse 也是 false 就是該獎項被關閉)
            if ($g['active'] == GeneralConst::ACTIVE_NO) $canuse = false;
            $res[] = [
                'id'         => $g['id'],
                'lp'         => $g['lp'], // 獎項名
                'gp'         => $g['gp'], // 所需吉點
                'issoldout'  => $issoldout,   // 是否全數兌換完畢
                'isredeemed' => $isredeemed,  // 是否已兌換過
                'isnomoney'  => $isnomoney,    // 是否吉點還夠
                'canuse'     => $canuse,      // 綜合顯示
            ];
        }
        
        return $res;
    }
    
    // 是否今日可玩遊戲
    public function canPlay($lineId)
    {
        // 回合資訊
        $roundinfo = $this->roundManagementDb
            ->select(['id'])
            ->where('dateFinal', '>=', date("Y-m-d"))
            ->where('dateBegin', '<=', date("Y-m-d"))
            ->where('active', '=', GeneralConst::ACTIVE_YES)
            ->get();
        if (count($roundinfo) == 0) {
            return false;
        } else {
            $roundinfo = $roundinfo->toArray();
        }
        $playinfo = $this->sessionManagementDb
            ->select(['currentGameGp'])
            ->where('lineId', $lineId)
            ->where('rid', $roundinfo[0]['id'])
            ->where('gid', '<>', 0) // 僅篩選簽到或遊戲
            ->where('eventDate', date("Y-m-d"))
            ->get()
            ->toArray();
        // 可用性限制
        $canplay = true;
        foreach ($playinfo as $p) {
            // 目前只有在玩一次的狀態是 -1
            if ($p['currentGameGp'] >= 0) $canplay = false;
        }
        
        return $canplay;
    }
    
    // 玩遊戲簽到(建立在可玩的狀態下才會進來)
    public function playGameAndCheckin($lineId, $name)
    {
        // 回合資訊
        $roundinfo = $this->roundManagementDb
            ->select(['id'])
            ->where('dateFinal', '>=', date("Y-m-d"))
            ->where('dateBegin', '<=', date("Y-m-d"))
            ->where('active', '=', GeneralConst::ACTIVE_YES)
            ->get()
            ->toArray();
        // 一定有回合所以不防呆(參考本函數註解)
        // 依照比例決定返回的轉盤結果
        $gameinfo = $this->gameManagementDb
            ->select(['id', 'ratio', 'gp'])
            ->get()
            ->toArray();
        $gameArr = [];
        foreach ($gameinfo as $g) for ($i = 0; $i < $g['ratio']; $i++) $gameArr[] = ['id' => $g['id'], 'gp' => $g['gp']];
        shuffle($gameArr);
        // 依照簽到狀態決定需不需要簽到 & 決定簽到吉點
        $playinfo = $this->sessionManagementDb
            ->select(['currentCheckinGp', 'eventDate'])
            ->where('lineId', $lineId)
            ->where('rid', $roundinfo[0]['id'])
            ->where('gid', '<>', 0) // 僅篩選簽到或遊戲
            ->get()
            ->toArray();
        $cancheckin = true;
        $checkins = 0;      // 本回合不含這一次,共簽到過幾次
        foreach ($playinfo as $p) {
            // 檢驗當天是否有簽到
            if ($p['eventDate'] == date("Y-m-d")) {
                // 目前只有在玩一次的狀態是 -1
                if ($p['currentCheckinGp'] >= 0) $cancheckin = false;
            }
            // 統計本回合已簽到過幾次
            if ($p['currentCheckinGp'] > 0) {
                $checkins++;
            }
        }
        if ($lineId == $this->settingManagementSv->getSetting()['GAME_TESTING_LINE_ID']) $checkins = 0;
        $currentCheckinGp = ($cancheckin) ? $this->getGpByCheckins($checkins + 1) : 0;
        // 拼湊 session 紀錄
        $currentGp = ((($gameArr[0]['gp'] < 0) ? 0 : $gameArr[0]['gp']) + ($currentCheckinGp)); // 將遊戲吉點小於 0 的部分歸零
        $res = [
            'pid'              => $this->getPidBylineId($lineId),
            'lineId'           => $lineId,
            'userName'         => $name,
            'rid'              => $roundinfo[0]['id'],
            'gid'              => $gameArr[0]['id'],
            'cid'              => 0,
            'eventDate'        => date("Y-m-d"),
            'currentGp'        => $currentGp,
            'currentGameGp'    => $gameArr[0]['gp'],
            'currentCheckinGp' => $currentCheckinGp,
            'cdate'            => date('Y-m-d H:i:s'),
        ];
        $this->sessionManagementDb->insert($res);
        $id = \DB::getPdo()->lastInsertId();
        $res['id'] = $id;
        // 上檔玩家吉點計算
        $res['gp'] = $this->addGpToPlayer($lineId, $currentGp);
        
        return $res;
    }
    
    public function getPidBylineId($lineId)
    {
        $info = $this->playerManagementDb->select(['id'])->where('lineId', $lineId)->first()->toArray();
        
        return $info['id'];
    }
    
    public function getGpByCheckins($checkins)
    {
        $info = $this->checkinManagementDb->select(['gp'])->where('day', $checkins)->first()->toArray();
        
        return $info['gp'];
    }
    
    public function addGpToPlayer($lineId, $gp)
    {
        $info = $this->playerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
        $gp += $info['gp'];
        $this->playerManagementDb->where('lineId', $lineId)->update(['gp' => $gp]);
        
        return $gp;
    }
    
    // 兌點(建立在 goodinfo 執行過確認兌獎資格的情況)
    // 採取策略是我們資料庫先改動 > 調用直通 > 如果直通有問題我們就會加註 MEMO > 但本地的資料就不更改了,避免重複讓用戶調用
    public function redeemGood($lineId, $gid, $name)
    {
        $redeeminfo = [];
        try {
            // 回合資訊 (注意用於兌獎資訊的日期區間判定與玩遊戲不同)
            $roundinfo = $this->roundManagementDb
                ->select(['id'])
                ->where(\DB::raw("DATE_ADD(dateFinal, INTERVAL +redeemExtra DAY)"), '>=', date("Y-m-d"))
                ->where('dateBegin', '<=', date("Y-m-d"))
                ->where('active', '=', GeneralConst::ACTIVE_YES)
                ->get()
                ->toArray();
            // 來到這邊代表一定有資料
            // good 準備資料
            $goodinfo = $this->goodManagementDb->select(['issuedQty', 'gp', 'lp'])->where('id', $gid)->first()->toArray();
            $fromIssuedQty = $goodinfo['issuedQty'];
            $toIssuedQty = $fromIssuedQty + 1;
            // player 準備資料
            $playerinfo = $this->playerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
            $fromGp = $playerinfo['gp'];
            $toGp = $playerinfo['gp'] - $goodinfo['gp'];
            // 直通打點與確認 (先打點,依照返回正確與否再決定要不要後續操作本地資料)
            if ($this->settingManagementSv->getSetting()['GAME_ESI_LINEPOINT'] == 'true') {
                // 共用變數
                $uid = $lineId;
                $rqidA = $this->getRqId();
                $rqidB = $this->getRqId();
                $seedKey = $this->getSeedKey();
                $divDataA = $this->getDivData();
                $divDataB = $this->getDivData();
                //
                //
                // 1/2 打點
                //
                //
                // 產生本次調用的 sessionKey
                $sessionKey = $this->getSessionKey($seedKey, $divDataA);
                // 整理押碼用的資料欄內容
                $msgRaw = $this->getMsgRaw([
                    'RqClient' => env('RQ_CLIENT'),
                    'RqId'     => $rqidA,
                    'UID'      => $uid,
                    'Amount'   => $goodinfo['lp'],
                ]);
                $msgHash = $this->getMsgHash($msgRaw);
                // 建立ICV
                $icv = $this->getICV();
                // 建立押碼
                $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
                $mac = $this->getMAC($macRaw);
                // 參數打包
                $param = [
                    'RqClient' => env('RQ_CLIENT'),
                    'RqId'     => $rqidA,
                    'ICV'      => $icv,
                    'DivData'  => $divDataA,
                    'MAC'      => $mac,
                    'UID'      => $uid,
                    'Amount'   => $goodinfo['lp'],
                ];
                // 相關中繼資料打包(LOG 用)
                $logdata = [
                    'sessionKey' => $sessionKey,
                    'msgRaw'     => $msgRaw,
                    'msgHash'    => $msgHash,
                    'macRaw'     => $macRaw,
                ];
                // 憑證參數整理
                $curlparam = [
                    CURLOPT_SSLCERTTYPE    => 'PEM',
                    CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
                    CURLOPT_SSLKEYTYPE     => 'PEM',
                    CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ];
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
                // 直通紀錄
                $data = [
                    'type'  => GeneralConst::LOG_ESI,
                    'func'  => 'LinepointIssue',
                    'k'     => $uid,
                    'cdate' => date("Y-m-d H:i:s"),
                ];
                // 調用
                $req = [
                    'curl'    => $curlparam,
                    'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
                    'headers' => [
                        'Content-Type' => 'application/json',
                        'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                        'Accept'       => '*/*',
                    ]
                ];
                if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
                $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
                $resA = json_decode($response->getBody());
                $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
                $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
                $this->syslog($data);
                //
                //
                // 2/2 打點確認
                //
                //
                // 產生本次調用的 sessionKey
                $sessionKey = $this->getSessionKey($seedKey, $divDataB);
                // 整理押碼用的資料欄內容
                $msgRaw = $this->getMsgRaw([
                    'RqClient'  => env('RQ_CLIENT'),
                    'RqId'      => $rqidB,
                    'QueryRqid' => $rqidA, // 剛才的RQID
                    'UID'       => $uid,
                ]);
                $msgHash = $this->getMsgHash($msgRaw);
                // 建立ICV
                $icv = $this->getICV();
                // 建立押碼
                $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
                $mac = $this->getMAC($macRaw);
                // 參數打包
                $param = [
                    'RqClient'  => env('RQ_CLIENT'),
                    'RqId'      => $rqidB,
                    'ICV'       => $icv,
                    'DivData'   => $divDataB,
                    'MAC'       => $mac,
                    'QueryRqid' => $rqidA, // 剛才的RQID
                    'UID'       => $uid,
                ];
                // 相關中繼資料打包(LOG 用)
                $logdata = [
                    'sessionKey' => $sessionKey,
                    'msgRaw'     => $msgRaw,
                    'msgHash'    => $msgHash,
                    'macRaw'     => $macRaw,
                ];
                // 憑證參數整理
                $curlparam = [
                    CURLOPT_SSLCERTTYPE    => 'PEM',
                    CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
                    CURLOPT_SSLKEYTYPE     => 'PEM',
                    CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ];
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
                // 直通紀錄
                $data = [
                    'type'  => GeneralConst::LOG_ESI,
                    'func'  => 'LinepointQuery',
                    'k'     => $uid,
                    'cdate' => date("Y-m-d H:i:s"),
                ];
                // 調用
                $req = [
                    'curl'    => $curlparam,
                    'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
                    'headers' => [
                        'Content-Type' => 'application/json',
                        'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                        'Accept'       => '*/*',
                    ]
                ];
                if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
                $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/query', $req);
                $resB = json_decode($response->getBody());
                $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
                $data['memoOut'] = json_encode($resB, JSON_UNESCAPED_UNICODE);
                $this->syslog($data);
                // 打點返回成功,需要確認
                //      確認成功,對用戶返回已打點的資訊,也計入資料庫
                //      確認失敗,對用戶返回已打點的資訊,也計入資料庫(差別只在於 LOG 註記的內容不同)
                // 打點返回失敗,需要確認
                //      確認成功,對用戶返回已打點的資訊,也計入資料庫(差別也只在於 LOG 註記的內容不同)
                //      確認失敗,報錯
                if ($resA->RCode != '0000' && $resB->RCode != '0000') {
                    return false;
                }
            }
            // session 插入
            $res = [
                'pid'              => $this->getPidBylineId($lineId),
                'lineId'           => $lineId,
                'userName'         => $name,
                'rid'              => $roundinfo[0]['id'],
                'gid'              => 0,    // 注意這是轉盤的獎項 ID 不是兌獎的 ID
                'cid'              => $gid,
                'eventDate'        => date("Y-m-d"),
                'currentGp'        => -$goodinfo['gp'],
                'currentGameGp'    => 0,
                'currentCheckinGp' => 0,
                'cdate'            => date('Y-m-d H:i:s'),
            ];
            $this->sessionManagementDb->insert($res);
            // good 更新資料
            $this->goodManagementDb->where('id', $gid)->update(['issuedQty' => $toIssuedQty]);
            // player 更新資料
            $this->playerManagementDb->where('lineId', $lineId)->update(['gp' => $toGp]);
            // 整理本地紀錄資料
            $redeeminfo = [
                'player'  => [
                    'lineId' => $lineId,
                    'fromGp' => $fromGp,
                    'toGp'   => $toGp,
                ],
                'good'    => [
                    'id'            => $gid,
                    'fromIssuedQty' => $fromIssuedQty,
                    'toIssuedQty'   => $toIssuedQty,
                ],
                'session' => $res,
            ];
        } catch (Exception $ex) {
            // 錯誤可能發生在 memoIn 或是 out
            $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
            $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
            $this->syslog($data);
            
            return false;
        }
        
        return $redeeminfo;
    }
    
    public function playerTag($lineId)
    {
        try {
            // 本次調用的參數整理
            $tid = $this->settingManagementSv->getSetting()['GAME_CURRENT_TAG'];
            $uid = $lineId;
            $rqid = $this->getRqId();
            // 取得 seedKey
            $seedKey = $this->getSeedKey();
            // 隨機產生多樣性資料
            $divData = $this->getDivData();
            // 產生本次調用的 sessionKey
            $sessionKey = $this->getSessionKey($seedKey, $divData);
            // 整理押碼用的資料欄內容
            $msgRaw = $this->getMsgRaw([
                'RqClient' => env('RQ_CLIENT'),
                'RqId'     => $rqid,
                'TagInfos' => [
                    [
                        'Tagid' => $tid,
                        'UIDs'  => [$uid]
                    ],
                ],
            ]);
            $msgHash = $this->getMsgHash($msgRaw);
            // 建立ICV
            $icv = $this->getICV();
            // 建立押碼
            $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
            $mac = $this->getMAC($macRaw);
            // 參數打包
            $param = [
                'RqClient' => env('RQ_CLIENT'),
                'RqId'     => $rqid,
                'ICV'      => $icv,
                'DivData'  => $divData,
                'MAC'      => $mac,
                'TagInfos' => [
                    [
                        'Tagid' => $tid,
                        'UIDs'  => [$uid]
                    ],
                ],
            ];
            // 相關中繼資料打包(LOG 用)
            $logdata = [
                'sessionKey' => $sessionKey,
                'msgRaw'     => $msgRaw,
                'msgHash'    => $msgHash,
                'macRaw'     => $macRaw,
            ];
            // 憑證參數整理
            $curlparam = [
                CURLOPT_SSLCERTTYPE    => 'PEM',
                CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
                CURLOPT_SSLKEYTYPE     => 'PEM',
                CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
                CURLOPT_SSL_VERIFYHOST => 0,
                CURLOPT_SSL_VERIFYPEER => 0,
            ];
            if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
            if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
            // 直通紀錄
            $data = [
                'type'  => GeneralConst::LOG_ESI,
                'func'  => 'TagTagging',
                'k'     => $uid,
                'cdate' => date("Y-m-d H:i:s"),
            ];
            // 調用
            $req = [
                'curl'    => $curlparam,
                'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
                'headers' => [
                    'Content-Type' => 'application/json',
                    'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                    'Accept'       => '*/*',
                ],
                'timeout' => 15, // Response timeout
                'connect_timeout' => 10, // Connection timeout
                'on_stats' => function (\GuzzleHttp\TransferStats $stats) use ($rqid) {
                    if ($stats->hasResponse()) {
                        $this->debuglog('playerTag-on_stats_ok', [
                            'rqid' => $rqid ?? 0
                        ], [
                            'transferTime' => $stats->getTransferTime(),
                            'handlerStats' => $stats->getHandlerStats(),
                        ]);
                    } else {
                        $this->debuglog('playerTag-on_stats_ng', [
                            'rqid' => $rqid ?? 0
                        ], [
                            'handlerErrorData' => $stats->getHandlerErrorData(),
                        ]);
                    }
                }
            ];
            if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
            $requestTime = date("Y-m-d H:i:s");
            $logdata['requestTime'] = $requestTime;
            $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
            $res = $response->getBody();
            // 經由計算取得的完整 request JSON
            $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
            // 調用 API 取得返回
            $data['memoOut'] = $res;
            // 紀錄
            $this->syslog($data);
        } catch (Exception $ex) {
            // 錯誤可能發生在 memoIn 或是 out
//            $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
//            $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
//            $this->syslog($data);
            $this->debuglog('playerTag', [
                'rqid' => $rqid ?? 0
            ], [
                'errCode' => $ex->getCode(),
                'requestTime' => $requestTime ?? '',
                'logTime' => date("Y-m-d H:i:s"),
                'errMsg' => $ex->getMessage(),
            ]);
            return false;
        }
        
        return true;
    }
    
    public function syslog($sysLog)
    {
        // 此功能暫時只有小遊戲+類發票有,所以偵測 k 值存在才紀錄
        if (isset($sysLog['k'])) {
            if (!isset($sysLog['kk'])) {
                // 偵測到不存在特殊 kk 鍵的請求就進入小遊戲
                $this->syslogManagementDb->insert($sysLog);
            } else {
                // 偵測到存在特殊 kk 鍵的請求就
                if ($sysLog['kk'] == 'activity') {
                    //進入類發票(需要先移除特殊鍵)
                    unset($sysLog['kk']);
                    $this->syslogactManagementDb->insert($sysLog);
                }
                if ($sysLog['kk'] == 'tmz') {
                    //進入串門子(需要先移除特殊鍵)
                    unset($sysLog['kk']);
                    $this->syslogtManagementDb->insert($sysLog);
                }
            }
        }
        return true;
    }
    public function debuglog($func, $dataIn, $dataOut = '')
    {
        $this->syslogManagementDb->insert([
            'type' => 'debug',
            'func' => $func,
            'k' => $_SERVER['SERVER_ADDR'],
            'memoIn' => json_encode($dataIn),
            'memoOut' => substr(json_encode($dataOut),0,1024),
            'cdate' => date("Y-m-d H:i:s"),
        ]);
    }
    // 直通 API 串聯流程
    public function esiFlow()
    {
        // 本次調用的參數整理
        $tid = '87B3984D-15EC-480C-8E23-25275F2D2DF2';
        $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
        $rqid = $this->getRqId();
        // 取得 seedKey
        $seedKey = $this->getSeedKey();
        // 隨機產生多樣性資料
        $divData = $this->getDivData();
        // 產生本次調用的 sessionKey
        $sessionKey = $this->getSessionKey($seedKey, $divData);
        // 整理押碼用的資料欄內容
        $msgRaw = $this->getMsgRaw([
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqid,
            'TagInfos' => [
                [
                    'Tagid' => $tid,
                    'UIDs'  => [$uid]
                ],
            ],
        ]);
        $msgHash = $this->getMsgHash($msgRaw);
        // 建立ICV
        $icv = $this->getICV();
        // 建立押碼
        $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
        $mac = $this->getMAC($macRaw);
        // 參數打包
        $param = [
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqid,
            'ICV'      => $icv,
            'DivData'  => $divData,
            'MAC'      => $mac,
            'TagInfos' => [
                [
                    'Tagid' => $tid,
                    'UIDs'  => [$uid]
                ],
            ],
        ];
        // 相關中繼資料打包(LOG 用)
        $logdata = [
            'sessionKey' => $sessionKey,
            'msgRaw'     => $msgRaw,
            'msgHash'    => $msgHash,
            'macRaw'     => $macRaw,
        ];
        // 憑證參數整理
        $curlparam = [
            CURLOPT_SSLCERTTYPE    => 'PEM',
            CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
            CURLOPT_SSLKEYTYPE     => 'PEM',
            CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ];
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
        // 調用
        try {
            $req = [
                'curl'    => $curlparam,
                'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
                'headers' => [
                    'Content-Type' => 'application/json',
                    'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                    'Accept'       => '*/*',
                ]
            ];
            if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
            $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
            $res = $response->getBody();
        } catch (Exception $ex) {
        
        }
        // 直通紀錄
        $data = [
            'type'  => GeneralConst::LOG_ESI,
            'func'  => 'TagTagging',
            'k'     => '',
            'cdate' => date("Y-m-d H:i:s"),
        ];
        // 經由計算取得的完整 request JSON
        $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
        // 調用 API 取得返回
        $data['memoOut'] = $res;
        // 紀錄
        $this->syslog($data);
        
        return $res;
    }
    
    public function esiFlowTagUpdate()
    {
        // 本次調用的參數整理
        $tid = 'f7d27d19-b05e-4ec8-bb49-2c5b0525e095';
        $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
        $rqid = $this->getRqId();
        // 取得 seedKey
        $seedKey = $this->getSeedKey();
        // 隨機產生多樣性資料
        $divData = $this->getDivData();
        // 產生本次調用的 sessionKey
        $sessionKey = $this->getSessionKey($seedKey, $divData);
        // 整理押碼用的資料欄內容
        $msgRaw = $this->getMsgRaw([
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqid,
            'Tags'     => [
                [
                    'Tagid' => $tid,
                    'Name'  => '奧美測試用標籤' . date("YmdHis"),
                ]
            ],
        ]);
        //$msgRaw = preg_replace('/[\-]/', '', $msgRaw);
        $msgHash = $this->getMsgHash($msgRaw);
        // 建立ICV
        $icv = $this->getICV();
        // 建立押碼
        $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
        $mac = $this->getMAC($macRaw);
        // 參數打包
        $param = [
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqid,
            'ICV'      => $icv,
            'DivData'  => $divData,
            'MAC'      => $mac,
            'Tags'     => [
                [
                    'Tagid' => $tid,
                    'Name'  => '奧美測試用標籤' . date("YmdHis"),
                ]
            ],
        ];
        // 相關中繼資料打包(LOG 用)
        $logdata = [
            'sessionKey' => $sessionKey,
            'msgRaw'     => $msgRaw,
            'msgHash'    => $msgHash,
            'macRaw'     => $macRaw,
        ];
        // 憑證參數整理
        $curlparam = [
            CURLOPT_SSLCERTTYPE    => 'PEM',
            CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
            CURLOPT_SSLKEYTYPE     => 'PEM',
            CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ];
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
        // 調用
        $req = [
            'curl'    => $curlparam,
            'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-Type' => 'application/json',
                'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                'Accept'       => '*/*',
            ]
        ];
        if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
        $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Update', $req);
        $res = $response->getBody();
        // 直通紀錄
        $data = [
            'type'  => GeneralConst::LOG_ESI,
            'func'  => 'TagUpdate',
            'k'     => '',
            'cdate' => date("Y-m-d H:i:s"),
        ];
        // 經由計算取得的完整 request JSON
        $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
        // 調用 API 取得返回
        $data['memoOut'] = $res;
        // 紀錄
        $this->syslog($data);
        
        return [
            'in'  => json_decode($data['memoIn']),
            'out' => json_decode($data['memoOut']),
        ];
    }
    
    public function esiFlowTagTagging()
    {
        // 本次調用的參數整理
        $tid = 'f7d27d19-b05e-4ec8-bb49-2c5b0525e095';
        $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
        $rqid = $this->getRqId();
        // 取得 seedKey
        $seedKey = $this->getSeedKey();
        // 隨機產生多樣性資料
        $divData = $this->getDivData();
        // 產生本次調用的 sessionKey
        $sessionKey = $this->getSessionKey($seedKey, $divData);
        // 整理押碼用的資料欄內容
        $msgRaw = $this->getMsgRaw([
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqid,
            'TagInfos' => [
                [
                    'Tagid' => $tid,
                    'UIDs'  => [$uid]
                ],
            ],
        ]);
        $msgHash = $this->getMsgHash($msgRaw);
        // 建立ICV
        $icv = $this->getICV();
        // 建立押碼
        $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
        $mac = $this->getMAC($macRaw);
        // 參數打包
        $param = [
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqid,
            'ICV'      => $icv,
            'DivData'  => $divData,
            'MAC'      => $mac,
            'TagInfos' => [
                [
                    'Tagid' => $tid,
                    'UIDs'  => [$uid]
                ],
            ],
        ];
        // 相關中繼資料打包(LOG 用)
        $logdata = [
            'sessionKey' => $sessionKey,
            'msgRaw'     => $msgRaw,
            'msgHash'    => $msgHash,
            'macRaw'     => $macRaw,
        ];
        // 憑證參數整理
        $curlparam = [
            CURLOPT_SSLCERTTYPE    => 'PEM',
            CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
            CURLOPT_SSLKEYTYPE     => 'PEM',
            CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ];
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
        // 調用
        $req = [
            'curl'    => $curlparam,
            'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-Type' => 'application/json',
                'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                'Accept'       => '*/*',
            ]
        ];
        if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
        $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
        $res = $response->getBody();
        // 直通紀錄
        $data = [
            'type'  => GeneralConst::LOG_ESI,
            'func'  => 'TagTagging',
            'k'     => '',
            'cdate' => date("Y-m-d H:i:s"),
        ];
        // 經由計算取得的完整 request JSON
        $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
        // 調用 API 取得返回
        $data['memoOut'] = $res;
        // 紀錄
        $this->syslog($data);
        
        return [
            'in'  => json_decode($data['memoIn']),
            'out' => json_decode($data['memoOut']),
        ];
    }
    
    public function esiFlowTagInsert()
    {
        // 本次調用的參數整理
        $tag = '串門子-所有';
        $rqid = $this->getRqId();
        // 取得 seedKey
        $seedKey = $this->getSeedKey();
        // 隨機產生多樣性資料
        $divData = $this->getDivData();
        // 產生本次調用的 sessionKey
        $sessionKey = $this->getSessionKey($seedKey, $divData);
        // 整理押碼用的資料欄內容
        $msgRaw = $this->getMsgRaw([
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqid,
            'Tags'     => [['Name' => $tag]],
        ]);
        //$msgRaw = preg_replace('/[\-]/', '', $msgRaw);
        $msgHash = $this->getMsgHash($msgRaw);
        // 建立ICV
        $icv = $this->getICV();
        // 建立押碼
        $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
        $mac = $this->getMAC($macRaw);
        // 參數打包
        $param = [
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqid,
            'ICV'      => $icv,
            'DivData'  => $divData,
            'MAC'      => $mac,
            'Tags'     => [['Name' => $tag]],
        ];
        // 相關中繼資料打包(LOG 用)
        $logdata = [
            'sessionKey' => $sessionKey,
            'msgRaw'     => $msgRaw,
            'msgHash'    => $msgHash,
            'macRaw'     => $macRaw,
        ];
        // 憑證參數整理
        $curlparam = [
            CURLOPT_SSLCERTTYPE    => 'PEM',
            CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
            CURLOPT_SSLKEYTYPE     => 'PEM',
            CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ];
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
        // 調用
        $req = [
            'curl'    => $curlparam,
            'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-Type' => 'application/json',
                'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                'Accept'       => '*/*',
            ]
        ];
        if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
        $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Insert', $req);
        $res = $response->getBody();
        // 直通紀錄
        $data = [
            'type'  => GeneralConst::LOG_ESI,
            'func'  => 'TagInsert',
            'k'     => '',
            'cdate' => date("Y-m-d H:i:s"),
        ];
        // 經由計算取得的完整 request JSON
        $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
        // 調用 API 取得返回
        $data['memoOut'] = $res;
        // 紀錄
        $this->syslog($data);
        
        return [
            'in'  => json_decode($data['memoIn']),
            'out' => json_decode($data['memoOut']),
        ];
    }
    
    public function esiFlowLP()
    {
        // 共用變數
        $uid = "Ub02cb4e598a7d6b3802cc6fb073ce717";
        $rqidA = $this->getRqId();
        $seedKey = $this->getSeedKey();
        $divDataA = $this->getDivData();
        // 產生本次調用的 sessionKey
        $sessionKey = $this->getSessionKey($seedKey, $divDataA);
        // 整理押碼用的資料欄內容
        $msgRaw = $this->getMsgRaw([
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqidA,
            'UID'      => $uid,
            'Amount'   => 1,
        ]);
        $msgHash = $this->getMsgHash($msgRaw);
        // 建立ICV
        $icv = $this->getICV();
        // 建立押碼
        $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
        $mac = $this->getMAC($macRaw);
        // 參數打包
        $param = [
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqidA,
            'ICV'      => $icv,
            'DivData'  => $divDataA,
            'MAC'      => $mac,
            'UID'      => $uid,
            'Amount'   => 1,
        ];
        // 相關中繼資料打包(LOG 用)
        $logdata = [
            'sessionKey' => $sessionKey,
            'msgRaw'     => $msgRaw,
            'msgHash'    => $msgHash,
            'macRaw'     => $macRaw,
        ];
        // 憑證參數整理
        $curlparam = [
            CURLOPT_SSLCERTTYPE    => 'PEM',
            CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
            CURLOPT_SSLKEYTYPE     => 'PEM',
            CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ];
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
        // 直通紀錄
        $data = [
            'type'  => GeneralConst::LOG_ESI,
            'func'  => 'LinepointIssue',
            'k'     => $uid,
            'cdate' => date("Y-m-d H:i:s"),
        ];
        // 調用
        $req = [
            'curl'    => $curlparam,
            'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-Type' => 'application/json',
                'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                'Accept'       => '*/*',
            ]
        ];
        if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
        $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
        $resA = json_decode($response->getBody());
        $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
        $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
        $this->syslog($data);
        
        return [
            'in'  => json_decode($data['memoIn']),
            'out' => json_decode($data['memoOut']),
        ];
    }
    
    public function esiFlowBak()
    {
        // 本次調用的參數整理
        $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
        $rqid = $this->getRqId();
        // 取得 seedKey
        $seedKey = $this->getSeedKey();
        // 隨機產生多樣性資料
        $divData = $this->getDivData();
        // 產生本次調用的 sessionKey
        $sessionKey = $this->getSessionKey($seedKey, $divData);
        // 整理押碼用的資料欄內容
        $msgRaw = $this->getMsgRaw([
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqid,
            'UID'      => $uid,
        ]);
        $msgHash = $this->getMsgHash($msgRaw);
        // 建立ICV
        $icv = $this->getICV();
        // 建立押碼
        $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
        $mac = $this->getMAC($macRaw);
        // 參數打包
        $param = [
            'RqClient' => env('RQ_CLIENT'),
            'RqId'     => $rqid,
            'ICV'      => $icv,
            'DivData'  => $divData,
            'MAC'      => $mac,
            'UID'      => $uid,
        ];
        // 相關中繼資料打包(LOG 用)
        $logdata = [
            'sessionKey' => $sessionKey,
            'msgRaw'     => $msgRaw,
            'msgHash'    => $msgHash,
            'macRaw'     => $macRaw,
        ];
        // 憑證參數整理
        $curlparam = [
            CURLOPT_SSLCERTTYPE    => 'PEM',
            CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
            CURLOPT_SSLKEYTYPE     => 'PEM',
            CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ];
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
        if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
        // 調用
        $req = [
            'curl'    => $curlparam,
            'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-Type' => 'application/json',
                'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                'Accept'       => '*/*',
            ]
        ];
        if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
        $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/contact/QueryQualification', $req);
        $res = $response->getBody();
        // 直通紀錄
        $data = [
            'type'  => GeneralConst::LOG_ESI,
            'func'  => 'QueryQualification',
            'k'     => $uid,
            'cdate' => date("Y-m-d H:i:s"),
        ];
        // 經由計算取得的完整 request JSON
        $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
        // 調用 API 取得返回
        $data['memoOut'] = $res;
        // 紀錄
        $this->syslog($data);
        // 返回值判斷
        $res = $res . json_encode($req);
        
        return $res;
        
        $rcode = $res->RCode ?? '';
        if ($rcode != '0000') return false;
        $isfollow = $res->Result->IsFollow ?? false;
        $ismember = $res->Result->IsTWPMember ?? false;
        if (!$isfollow || !$ismember) return false;
        
        
        return true;
    }
    
    public function getSeedKey()
    {
        // 參照官方文件教學
        // HEX 的 XOR 運算
        $all = strtoupper(bin2hex(pack('H*', env('SEED_KEY_A')) ^ pack('H*', env('SEED_KEY_B'))));
        // 分為前後的 A B 兩段,而不是原先文章定義的 A B 兩個 KEY
        $a = substr($all, 0, strlen($all) / 2);
        $b = substr($all, strlen($all) / 2, strlen($all) / 2);
        
        // 最後結合的是 A-B-A 段
        return $a . $b . $a;
    }
    
    public function getSessionKey($seedKey, $divData)
    {
        // 先將 HEX 還原成 BYTE 運算
        $sessionKey = bin2hex(openssl_encrypt(hex2bin($divData), 'DES-EDE3-ECB', hex2bin($seedKey), OPENSSL_RAW_DATA));
        // 按照文件擷取前 24 BYTE (也可以不用)
        $sessionKey = substr($sessionKey, 0, 48);
        // 按照文件轉成大寫 (也可以不用)
        $sessionKey = strtoupper($sessionKey);
        
        return $sessionKey;
    }
    
    public function getRqId()
    {
        $data = $data ?? random_bytes(16);
        assert(strlen($data) == 16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
        
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }
    
    public function getDivData()
    {
        return bin2hex(random_bytes(24));
    }
    
    public function getMsgRaw($msg)
    {
        $ret = '';
        foreach ($msg as $m) {
            $ret .= (is_array($m)) ? json_encode($m, JSON_UNESCAPED_UNICODE) : preg_replace('/[^A-Za-z0-9\-]/', '', $m);
        }
        
        return $ret;
    }
    
    public function getMsgHash($msg)
    {
        // 返回可以大寫也可以不用
        return strtoupper(hash('sha256', $msg));
    }
    
    public function getICV()
    {
        return bin2hex(random_bytes(8));
    }
    
    public function getMACRaw($msgHash, $sessionKey, $icv)
    {
        // 先將 HEX 還原成 BYTE 運算 (其中 ICV 可以大寫也可以不用)
        $macRaw = bin2hex(openssl_encrypt(hex2bin($msgHash), 'DES-EDE3-CBC', hex2bin($sessionKey), OPENSSL_RAW_DATA, hex2bin($icv)));
        
        // 返回可以大寫也可以不用
        return strtoupper($macRaw);
    }
    
    public function getMAC($macRaw)
    {
        // 按照文件擷取前 32 BYTE (必要)
        $macRaw = substr($macRaw, 0, 64);
        // 可以大寫也可以不用
        $macRaw = strtoupper($macRaw);
        
        return substr($macRaw, strlen($macRaw) - 16, 8);
    }
    
    // 類發票
    
    public function setdoc($data)
    {
        // 調用
        $response = $this->imgC->annotateImage(
            $data,
            [Type::TEXT_DETECTION]
        );
        $texts = $response->getTextAnnotations();
        // 整理字串
        $ret = [];
        foreach ($texts as $text) $ret[] = $text->getDescription();
        // 銀行 APP 判別
        $account_bank = '';
        $detect = explode("\n", $ret[0]);
        foreach ($detect as $d) {
            if ($d == '出示退款碼') $account_bank = '007';
            if ($d == '登入/安控') $account_bank = '009';
            if ($d == '繼續台灣 Pay交易') $account_bank = '016';
            if ($d == '我要退款') $account_bank = '017';
            if ($d == '繼續行動支付') $account_bank = '050';
            if ($d == '完成') $account_bank = '806';
        }
        // 整理觸發字串
        $order_no = '';
        $account_no = '';
        $order_date = '';
        switch ($account_bank) {
            case '007':
                for ($i = 0; $i < count($ret); $i++) {
                    if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
                    if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
                    if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
                        $order_date = $ret[ $i ] ?? '';
                    }
                }
                break;
            case '009':
                for ($i = 0; $i < count($ret); $i++) {
                    if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
                    if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
                    if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
                        $order_date = $ret[ $i ] ?? '';
                    }
                }
                break;
            case '016':
                for ($i = 0; $i < count($ret); $i++) {
                    if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
                    if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
                    if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
                        $order_date = $ret[ $i ] ?? '';
                    }
                }
                break;
            case '017':
                for ($i = 0; $i < count($ret); $i++) {
                    if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
                    if ($ret[ $i ] == '方式') $account_no = $ret[ $i + 1 ] ?? '';
                    if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
                        $order_date = $ret[ $i ] ?? '';
                    }
                }
                break;
            case '050':
                for ($i = 0; $i < count($ret); $i++) {
                    if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
                    if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
                    // 沒有時間資訊可取得,這個是虛設
                    if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
                        $order_date = $ret[ $i ] ?? '';
                    }
                }
                break;
            case '806':
                for ($i = 0; $i < count($ret); $i++) {
                    if ($ret[ $i ] == '編號') $order_no = $ret[ $i + 1 ] ?? '';
                    if ($ret[ $i ] == '商戶') $account_no = preg_replace('/-/', "", $ret[ $i - 1 ]) ?? '';
                    if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
                        $order_date = preg_replace('/-/', "/", $ret[ $i ]) ?? '';
                    }
                }
                break;
            case '':
                for ($i = 0; $i < count($ret); $i++) {
                    if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
                    if ($ret[ $i ] == '付款' && $ret[ $i + 1 ] == '帳號') $account_bank = $ret[ $i + 2 ] ?? '';
                    if ($ret[ $i ] == '付款' && $ret[ $i + 1 ] == '帳號') $account_no = $ret[ $i + 4 ] ?? '';
                    //if ($ret[ $i ] == '時間') $order_date = $ret[ $i + 2 ] ?? '';
                    if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
                        $order_date = $ret[ $i ] ?? '';
                    }
                }
                break;
        }
        
        return [
            'order_no'     => $order_no,
            'account_bank' => $account_bank,
            'account_no'   => $account_no,
            'order_date'   => $order_date,
            //'debug'        => $ret,
        ];
    }
    
    public function activityinfo()
    {
        // 活動資訊
        $activityinfo = $this->activityManagementDb
            ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
            ->where('reqTimeFinal', '>=', date("Y-m-d"))
            ->where('reqTimeBegin', '<=', date("Y-m-d"))
            ->where('isDraw', '')
            ->get();
        if (count($activityinfo) == 0) {
            return false;
        } else {
            $activityinfo = $activityinfo->toArray();
        }
        
        return $activityinfo;
    }
    
    public function activities()
    {
        // 活動資訊
        $activities = $this->activityManagementDb
            ->select([
                'id',
                'activityName',
                'reqTimeBegin',
                'reqTimeFinal',
                'drawTime',
                'drawNumbers',
                'isDraw',
                'redeemTimeBegin',
                'redeemTimeFinal',
                'isCheckBegin',
                'checkTimeBegin',
                'checkTimeFinal',
                'getTimeBegin',
                'getTimeFinal',
                'isGetFinal',
            ])
            ->get();
        if (count($activities) == 0) {
            return false;
        } else {
            $activities = $activities->toArray();
        }
        
        return $activities;
    }
    
    public function reqreceipt($info)
    {
        // 重複登錄資料邏輯判斷
        $r = $this->receiptManagementDb
            ->select(['id'])
            ->where('lineId', $info['lineId'])
            ->where('lineName', $info['name'])
            ->where('tranDate', $info['order_date'])
            ->where('tranBank', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_bank'] . "'), \"" . env('KK') . "\"))"))
            ->where('tranAccount', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_no'] . "'), \"" . env('KK') . "\"))"))
            ->where('tranOrderNo', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['order_no'] . "'), \"" . env('KK') . "\"))"))
            ->get();
        if (count($r) == 0) return 0;
        // 實際寫入
        $this->receiptManagementDb
            ->insert([
                'aid'         => $info['aid'],
                'tranDate'    => $info['order_date'],
                'tranBank'    => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_bank'] . "'), \"" . env('KK') . "\"))"),
                'tranAccount' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_no'] . "'), \"" . env('KK') . "\"))"),
                'tranOrderNo' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['order_no'] . "'), \"" . env('KK') . "\"))"),
                'lineId'      => $info['lineId'],
                'lineName'    => $info['name'],
                'cdate'       => date('Y-m-d H:i:s'),
                'mdate'       => date('Y-m-d H:i:s'),
                'oid'         => 0, // 外部用戶使用0
            ]);
        $id = \DB::getPdo()->lastInsertId();
        
        return $id;
    }
    
    public function lstreceipt($lineId)
    {
        // 依照(有限的)不同活動逐次取得該活動的所有紀錄
        $activity = $this->activityManagementDb->select(['id', 'activityName'])->orderby('drawTime', 'desc')->get();
        if (count($activity) == 0) {
            return [];
        } else {
            $activity = $activity->toArray();
        }
        $ret = [];
        // 選欄位
        $rStatusMsg = '';
        $rStatusBtn = '';
        foreach (GeneralConst::$rStatusMap as $k => $v) {
            $rStatusMsg .= ' when \'' . $k . '\' then \'' . $v['front'] . '\'';
            $rStatusBtn .= ' when \'' . $k . '\' then \'' . $v['btn'] . '\'';
        }
        // 逐個活動取得紀錄
        foreach ($activity as $a) {
            $receipt = $this->receiptManagementDb
                ->select([
                    'id',
                    \DB::raw("tranDate as order_date"),
                    \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranBank), \"" . env('KK') . "\")) as account_bank"),
                    \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranAccount), \"" . env('KK') . "\")) as account_no"),
                    \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranOrderNo), \"" . env('KK') . "\")) as order_no"),
                    'rStatus',
                    \DB::raw("(case rStatus $rStatusMsg end) as rStatusMsg"),
                    \DB::raw("(case rStatus $rStatusBtn end) as rStatusBtn"),
                ])
                ->where('lineId', $lineId) // 自己的訂單
                ->where('aid', $a['id'])   // 某一期的訂單
                ->orderby('cdate', 'desc')
                ->get();
            if (count($receipt) != 0) {
                $receipt = $receipt->toArray();
                // 依照順序切分(356412978)
                $tmp = [];
                foreach ($receipt as $r) $tmp[ $r['rStatus'] ][] = $r;
                // 整合
                $ret[] = [
                    'activityName' => $a['activityName'],
                    'receipts'     => array_merge(
                        $tmp[ GeneralConst::RSTATUS_DRAW_DONE ] ?? [],
                        $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE ] ?? [],
                        $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_FAIL ] ?? [],
                        $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM ] ?? [],
                        $tmp[ GeneralConst::RSTATUS_INIT ] ?? [],
                        $tmp[ GeneralConst::RSTATUS_DRAW_FAIL ] ?? [],
                        $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_GET ] ?? [],
                        $tmp[ GeneralConst::RSTATUS_DRAW_DONE_EXPIRED ] ?? [],
                        $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_EXPIRED ] ?? []
                    ),
                ];
            }
        }
        
        return $ret;
    }
    
    public function redeemCheck($lineId, $rid)
    {
        // 收據資訊
        $receiptinfo = $this->receiptManagementDb
            ->select(['aid'])
            ->where('lineId', $lineId) // 找自己的
            ->where('id', $rid)        // 收據
            ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE) // 可驗證的狀態
            ->get();
        if (count($receiptinfo) == 0) {
            return false;
        } else {
            $receiptinfo = $receiptinfo->toArray();
        }
        // 活動資訊
        $activityinfo = $this->activityManagementDb
            ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
            ->where('id', $receiptinfo[0]['aid'])                    // 找該收據活動
            ->where('isDraw', 'Y')                                   // 已經開獎
            ->where('redeemTimeFinal', '>=', date("Y-m-d"))  // 且還在可登記的時間範圍內
            ->where('redeemTimeBegin', '<=', date("Y-m-d"))
            ->get();
        if (count($activityinfo) == 0) {
            return false;
        } else {
            return true;
        }
    }
    
    public function redeemExecute($lineId, $rid)
    {
        $this->receiptManagementDb
            ->where('lineId', $lineId)
            ->where('id', $rid)
            ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE)
            ->update([
                'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_REDEEM,
                'mdate'   => date('Y-m-d H:i:s'),
                'oid'     => 0, // 外部用戶使用0
            ]);
        
        return true;
    }
    
    public function getCheck($lineId, $rid)
    {
        // 收據資訊
        $receiptinfo = $this->receiptManagementDb
            ->select(['aid'])
            ->where('lineId', $lineId) // 找自己的
            ->where('id', $rid)        // 收據
            ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE) // 可領獎的狀態
            ->where('canGet', 'Y')                                          // 可領獎的狀態(輔助狀態)
            ->get();
        if (count($receiptinfo) == 0) {
            return false;
        } else {
            $receiptinfo = $receiptinfo->toArray();
        }
        // 活動資訊
        $activityinfo = $this->activityManagementDb
            ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
            ->where('id', $receiptinfo[0]['aid'])                 // 找該收據活動
            ->where('isDraw', 'Y')                                // 已經開獎
            ->where('getTimeFinal', '>=', date("Y-m-d"))  // 且還在可領獎的時間範圍內
            ->where('getTimeBegin', '<=', date("Y-m-d"))
            ->get();
        if (count($activityinfo) == 0) {
            return false;
        } else {
            return true;
        }
    }
    
    public function getExecute($lineId, $rid)
    {
        try {
            if ($this->settingManagementSv->getSetting()['RECEIPT_ESI_LINEPOINT'] == 'true') {
                // 共用變數
                $uid = $lineId;
                $rqidA = $this->getRqId();
                $rqidB = $this->getRqId();
                $seedKey = $this->getSeedKey();
                $divDataA = $this->getDivData();
                $divDataB = $this->getDivData();
                //
                //
                // 1/2 打點
                //
                //
                // 產生本次調用的 sessionKey
                $sessionKey = $this->getSessionKey($seedKey, $divDataA);
                // 整理押碼用的資料欄內容
                $msgRaw = $this->getMsgRaw([
                    'RqClient' => env('RQ_CLIENT'),
                    'RqId'     => $rqidA,
                    'UID'      => $uid,
                    'Amount'   => 1,
                ]);
                $msgHash = $this->getMsgHash($msgRaw);
                // 建立ICV
                $icv = $this->getICV();
                // 建立押碼
                $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
                $mac = $this->getMAC($macRaw);
                // 參數打包
                $param = [
                    'RqClient' => env('RQ_CLIENT'),
                    'RqId'     => $rqidA,
                    'ICV'      => $icv,
                    'DivData'  => $divDataA,
                    'MAC'      => $mac,
                    'UID'      => $uid,
                    'Amount'   => 1,
                ];
                // 相關中繼資料打包(LOG 用)
                $logdata = [
                    'sessionKey' => $sessionKey,
                    'msgRaw'     => $msgRaw,
                    'msgHash'    => $msgHash,
                    'macRaw'     => $macRaw,
                ];
                // 憑證參數整理
                $curlparam = [
                    CURLOPT_SSLCERTTYPE    => 'PEM',
                    CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
                    CURLOPT_SSLKEYTYPE     => 'PEM',
                    CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ];
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
                // 直通紀錄
                $data = [
                    'type'  => GeneralConst::LOG_ESI,
                    'func'  => 'LinepointIssue',
                    'k'     => $uid,
                    'kk'    => 'activity',
                    'cdate' => date("Y-m-d H:i:s"),
                ];
                // 調用
                $req = [
                    'curl'    => $curlparam,
                    'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
                    'headers' => [
                        'Content-Type' => 'application/json',
                        'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                        'Accept'       => '*/*',
                    ]
                ];
                if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
                $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
                $resA = json_decode($response->getBody());
                $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
                $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
                $this->syslog($data);
                //
                //
                // 2/2 打點確認
                //
                //
                // 產生本次調用的 sessionKey
                $sessionKey = $this->getSessionKey($seedKey, $divDataB);
                // 整理押碼用的資料欄內容
                $msgRaw = $this->getMsgRaw([
                    'RqClient'  => env('RQ_CLIENT'),
                    'RqId'      => $rqidB,
                    'QueryRqid' => $rqidA, // 剛才的RQID
                    'UID'       => $uid,
                ]);
                $msgHash = $this->getMsgHash($msgRaw);
                // 建立ICV
                $icv = $this->getICV();
                // 建立押碼
                $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
                $mac = $this->getMAC($macRaw);
                // 參數打包
                $param = [
                    'RqClient'  => env('RQ_CLIENT'),
                    'RqId'      => $rqidB,
                    'ICV'       => $icv,
                    'DivData'   => $divDataB,
                    'MAC'       => $mac,
                    'QueryRqid' => $rqidA, // 剛才的RQID
                    'UID'       => $uid,
                ];
                // 相關中繼資料打包(LOG 用)
                $logdata = [
                    'sessionKey' => $sessionKey,
                    'msgRaw'     => $msgRaw,
                    'msgHash'    => $msgHash,
                    'macRaw'     => $macRaw,
                ];
                // 憑證參數整理
                $curlparam = [
                    CURLOPT_SSLCERTTYPE    => 'PEM',
                    CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
                    CURLOPT_SSLKEYTYPE     => 'PEM',
                    CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ];
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
                // 直通紀錄
                $data = [
                    'type'  => GeneralConst::LOG_ESI,
                    'func'  => 'LinepointQuery',
                    'k'     => $uid,
                    'kk'    => 'activity',
                    'cdate' => date("Y-m-d H:i:s"),
                ];
                // 調用
                $req = [
                    'curl'    => $curlparam,
                    'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
                    'headers' => [
                        'Content-Type' => 'application/json',
                        'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                        'Accept'       => '*/*',
                    ]
                ];
                if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
                $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/query', $req);
                $resB = json_decode($response->getBody());
                $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
                $data['memoOut'] = json_encode($resB, JSON_UNESCAPED_UNICODE);
                $this->syslog($data);
                // 打點返回成功,需要確認
                //      確認成功,對用戶返回已打點的資訊,也計入資料庫
                //      確認失敗,對用戶返回已打點的資訊,也計入資料庫(差別只在於 LOG 註記的內容不同)
                // 打點返回失敗,需要確認
                //      確認成功,對用戶返回已打點的資訊,也計入資料庫(差別也只在於 LOG 註記的內容不同)
                //      確認失敗,報錯
                if ($resA->RCode != '0000' && $resB->RCode != '0000') {
                    return false;
                }
            }
            // 修改資料庫狀態
            $this->receiptManagementDb
                ->where('lineId', $lineId)
                ->where('id', $rid)
                ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE)
                ->update([
                    'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_GET,
                    'mdate'   => date('Y-m-d H:i:s'),
                    'oid'     => 0, // 外部用戶使用0
                ]);
        } catch (Exception $ex) {
            // 錯誤可能發生在 memoIn 或是 out
            $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
            $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
            $this->syslog($data);
            
            return false;
        }
        
        return true;
    }
    
    // 串門子
    
    public function tplayerinfo($lineId)
    {
        // 取得用戶在本地的資訊
        $tplayerinfo = $this->tplayerManagementDb
            ->select(['id'])
            ->where('lineId', $lineId)
            ->get()
            ->toArray();
        // 取得用戶在 LINE 上的 profile
//        $profile = $this->lineProfile($lineId);
        if (count($tplayerinfo) == 0) {
            // 用戶不在本地資料庫,新增
            // 寫入資料表
            $d = [
                'lineId'    => $lineId,
                'userName'  => '',
                'userPhoto' => '',
                'c1'        => 0,
                'c2'        => 0,
                'c3'        => 0,
                'c4'        => 0,
                'c5'        => 0,
                'c6'        => 0,
                'c7'        => 0,
                'c8'        => 0,
                'c9'        => 0,
                'gp'        => 0,
                'cdate'     => date("Y-m-d H:i:s"),
                'mdate'     => date("Y-m-d H:i:s"),
            ];
            $this->tplayerManagementDb
                ->insert($d);
            $id = \DB::getPdo()->lastInsertId();
            // 替用互打一次性標籤
            $this->tplayerTag($lineId);
        } else {
            // 用戶的 LINE 資訊可能會更新,所以這個 API 還身兼更新資訊的功能
//            $d = [
//                'userName'  => $profile->displayName,
//                'userPhoto' => $profile->pictureUrl,
//            ];
//            $this->tplayerManagementDb
//                ->where('lineId', $lineId)
//                ->update($d);
        }
        // 再重新查一遍
        $tplayerinfo = $this->tplayerManagementDb
            ->select([
//                'id',
'lineId',
//                'userName',
//                'userPhoto',
'gp',
'c1',
'c2',
'c3',
'c4',
'c5',
'c6',
'c7',
'c8',
'c9',
\DB::raw("FLOOR(gp / " . $this->settingManagementSv->getSetting()['TMZ_GP_DRAW'] . ") as draw"),
//                'cdate',
//                'mdate',
            ])
            ->where('lineId', $lineId)
            ->first()
            ->toArray();
        
        return $tplayerinfo;
    }
    
    public function tplayerTag($lineId)
    {
        try {
            // 本次調用的參數整理
            $tid = $this->settingManagementSv->getSetting()['TMZ_CURRENT_TAG'];
            $uid = $lineId;
            $rqid = $this->getRqId();
            // 取得 seedKey
            $seedKey = $this->getSeedKey();
            // 隨機產生多樣性資料
            $divData = $this->getDivData();
            // 產生本次調用的 sessionKey
            $sessionKey = $this->getSessionKey($seedKey, $divData);
            // 整理押碼用的資料欄內容
            $msgRaw = $this->getMsgRaw([
                'RqClient' => env('RQ_CLIENT'),
                'RqId'     => $rqid,
                'TagInfos' => [
                    [
                        'Tagid' => $tid,
                        'UIDs'  => [$uid]
                    ],
                ],
            ]);
            $msgHash = $this->getMsgHash($msgRaw);
            // 建立ICV
            $icv = $this->getICV();
            // 建立押碼
            $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
            $mac = $this->getMAC($macRaw);
            // 參數打包
            $param = [
                'RqClient' => env('RQ_CLIENT'),
                'RqId'     => $rqid,
                'ICV'      => $icv,
                'DivData'  => $divData,
                'MAC'      => $mac,
                'TagInfos' => [
                    [
                        'Tagid' => $tid,
                        'UIDs'  => [$uid]
                    ],
                ],
            ];
            // 相關中繼資料打包(LOG 用)
            $logdata = [
                'sessionKey' => $sessionKey,
                'msgRaw'     => $msgRaw,
                'msgHash'    => $msgHash,
                'macRaw'     => $macRaw,
            ];
            // 憑證參數整理
            $curlparam = [
                CURLOPT_SSLCERTTYPE    => 'PEM',
                CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
                CURLOPT_SSLKEYTYPE     => 'PEM',
                CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
                CURLOPT_SSL_VERIFYHOST => 0,
                CURLOPT_SSL_VERIFYPEER => 0,
            ];
            if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
            if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
            // 直通紀錄
            $data = [
                'type'  => GeneralConst::LOG_ESI,
                'func'  => 'TagTagging',
                'k'     => $uid,
                'kk'    => 'tmz',
                'cdate' => date("Y-m-d H:i:s"),
            ];
            // 調用
            $req = [
                'curl'    => $curlparam,
                'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
                'headers' => [
                    'Content-Type' => 'application/json',
                    'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                    'Accept'       => '*/*',
                ]
            ];
            if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
            $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
            $res = $response->getBody();
            // 經由計算取得的完整 request JSON
            $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
            // 調用 API 取得返回
            $data['memoOut'] = $res;
            // 紀錄
            $this->syslog($data);
        } catch (Exception $ex) {
            // 錯誤可能發生在 memoIn 或是 out
            $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
            $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
            $this->syslog($data);
            
            return false;
        }
        
        return true;
    }
    
    public function tcheckin($lineId, $name)
    {
        // 回合資訊
        $troundinfo = $this->troundManagementDb
            ->select(['id'])
            ->where('dateFinal', '>=', date("Y-m-d"))
            ->where('dateBegin', '<=', date("Y-m-d"))
            ->where('active', '=', GeneralConst::ACTIVE_YES)
            ->get()
            ->toArray();
        // 一定有回合所以不防呆(參考本函數註解)
        // 依照簽到狀態決定需不需要簽到 & 決定簽到吉點
        $playinfo = $this->tsessionManagementDb
            ->select(['currentCheckinGp', 'eventDate'])
            ->where('lineId', $lineId)
            ->where('rid', $troundinfo[0]['id'])
            ->where('gid', '=', -1) // 僅篩選簽到
            ->get()
            ->toArray();
        $cancheckin = true;
        $checkins = 0;      // 本回合不含這一次,共簽到過幾次
        foreach ($playinfo as $p) {
            // 檢驗當天是否有簽到
            if ($p['eventDate'] == date("Y-m-d")) {
                if ($p['currentCheckinGp'] >= 0) $cancheckin = false;
            }
            // 統計本回合已簽到過幾次
            if ($p['currentCheckinGp'] >= 0) {
                $checkins++;
            }
        }
        //if ($lineId == $this->settingManagementSv->getSetting()['GAME_TESTING_LINE_ID']) $checkins = 0;
        $currentCheckinGp = ($cancheckin) ? $this->getGpByTCheckins($checkins + 1) : 0;
        // 拼湊 tsession 紀錄
        $currentGp = $currentCheckinGp;
        $res = [
            'pid'              => $this->getTPidBylineId($lineId),
            'lineId'           => $lineId,
            'userName'         => $name,
            'rid'              => $troundinfo[0]['id'],
            'gid'              => -1, // 簽到專用
            'cid'              => 0,
            'eventDate'        => date("Y-m-d"),
            'currentGp'        => $currentGp,
            'currentGameGp'    => 0,
            'currentCheckinGp' => $currentCheckinGp,
            'cdate'            => date('Y-m-d H:i:s'),
        ];
        // 重複簽到不會有點數
        if ($currentGp > 0) {
            $this->tsessionManagementDb->insert($res);
            $id = \DB::getPdo()->lastInsertId();
            $res['id'] = $id;
            // 上檔玩家吉點計算
            $res['gp'] = $this->addGpToTPlayer($lineId, $currentGp);
        }
        
        return $res;
    }
    
    public function getGpByTCheckins($checkins)
    {
        $info = $this->tcheckinManagementDb->select(['gp'])->where('day', $checkins)->first()->toArray();
        
        return $info['gp'];
    }
    
    public function getTPidBylineId($lineId)
    {
        $info = $this->tplayerManagementDb->select(['id'])->where('lineId', $lineId)->first()->toArray();
        
        return $info['id'];
    }
    
    public function troundinfo($lineId)
    {
        // 回合資訊
        $troundinfo = $this->troundManagementDb
            ->select(['id', 'dateBegin', 'dateFinal'])
            ->where('dateFinal', '>=', date("Y-m-d"))
            ->where('dateBegin', '<=', date("Y-m-d"))
            ->where('active', '=', GeneralConst::ACTIVE_YES)
            ->get();
        if (count($troundinfo) == 0) {
            return false;
        } else {
            $troundinfo = $troundinfo->toArray();
        }
        // 簽到資訊
        $checkininfo = $this->tsessionManagementDb
            ->select(['eventDate', 'currentGameGp'])
            ->where('lineId', $lineId)
            ->where('rid', $troundinfo[0]['id'])
            ->where('gid', '=', -1) // 僅篩選簽到
            ->orderby('id', 'asc') // 確保按照時間排
            ->get()
            ->toArray();
        // 整理
        $res = [
            'dateBegin' => $troundinfo[0]['dateBegin'],
            'dateFinal' => $troundinfo[0]['dateFinal'],
            'today'     => date("Y-m-d"),
            'checkins'  => [],
        ];
        foreach ($checkininfo as $c) {
            // 相同天可能會有重新再玩,因此會有超過1筆資料的可能,不想使用 SQL group by 耗能
            $res['checkins'][ $c['eventDate'] ] = [
                'date' => $c['eventDate'],
            ];
        }
        $res['checkins'] = array_values($res['checkins']);
        
        return $res;
    }
    
    public function addGpToTPlayer($lineId, $gp)
    {
        $info = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
        $gp += $info['gp'];
        $this->tplayerManagementDb->where('lineId', $lineId)->update(['gp' => $gp]);
        
        return $gp;
    }
    
    public function isTPlayer($lineId)
    {
        $tplayerinfo = $this->tplayerManagementDb->select(['id'])->where('lineId', $lineId)->get()->toArray();
        
        return (count($tplayerinfo) > 0);
    }
    
    public function playTGame($lineId, $name, $c)
    {
        // 回合資訊
        $troundinfo = $this->troundManagementDb
            ->select(['id'])
            ->where('dateFinal', '>=', date("Y-m-d"))
            ->where('dateBegin', '<=', date("Y-m-d"))
            ->where('active', '=', GeneralConst::ACTIVE_YES)
            ->get()
            ->toArray();
        // 一定有回合所以不防呆(參考本函數註解)
        // 檢查本回合玩過沒(同時看 tsession & tplayer,順便取得上檔九宮格分布)
        $playinfo = $this->tsessionManagementDb
            ->select(['currentCheckinGp', 'eventDate'])
            ->where('lineId', $lineId)
            ->where('rid', $troundinfo[0]['id'])
            ->where('gid', '=', $c) // 僅篩選九宮格的這一格
            ->get();
        if (count($playinfo) > 0) return false; // 本周期玩過了
        $playinfo = $this->tplayerManagementDb
            ->select(['c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'])
            ->where('lineId', $lineId)
            ->first()
            ->toArray();
        $s = [];
        foreach ($playinfo as $k => $v) {
            if ('c' . $c == $k && $v != 0) return false; // 本周期玩過了
            $s[ $k ] = $v;
        }
        // 依照九宮格分布計算本次應得吉點,以及紀錄九宮格狀態變遷
        $gp = 1; // 答對本來就有1點
        $s[ 'c' . $c ] = 2; // 答對初始狀態
        switch ($c) {
            case 1:
                if ($playinfo['c2'] != 0 && $playinfo['c3'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c2'] = 3;
                    $s['c3'] = 3;
                }
                if ($playinfo['c4'] != 0 && $playinfo['c7'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c4'] = 3;
                    $s['c7'] = 3;
                }
                if ($playinfo['c5'] != 0 && $playinfo['c9'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c5'] = 3;
                    $s['c9'] = 3;
                }
                break;
            case 2:
                if ($playinfo['c1'] != 0 && $playinfo['c3'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c1'] = 3;
                    $s['c3'] = 3;
                }
                if ($playinfo['c5'] != 0 && $playinfo['c8'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c5'] = 3;
                    $s['c8'] = 3;
                }
                break;
            case 3:
                if ($playinfo['c1'] != 0 && $playinfo['c2'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c1'] = 3;
                    $s['c2'] = 3;
                }
                if ($playinfo['c6'] != 0 && $playinfo['c9'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c6'] = 3;
                    $s['c9'] = 3;
                }
                if ($playinfo['c5'] != 0 && $playinfo['c7'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c5'] = 3;
                    $s['c7'] = 3;
                }
                break;
            case 4:
                if ($playinfo['c1'] != 0 && $playinfo['c7'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c1'] = 3;
                    $s['c7'] = 3;
                }
                if ($playinfo['c5'] != 0 && $playinfo['c6'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c5'] = 3;
                    $s['c6'] = 3;
                }
                break;
            case 5:
                if ($playinfo['c4'] != 0 && $playinfo['c6'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c4'] = 3;
                    $s['c6'] = 3;
                }
                if ($playinfo['c2'] != 0 && $playinfo['c8'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c2'] = 3;
                    $s['c8'] = 3;
                }
                if ($playinfo['c1'] != 0 && $playinfo['c9'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c1'] = 3;
                    $s['c9'] = 3;
                }
                if ($playinfo['c3'] != 0 && $playinfo['c7'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c3'] = 3;
                    $s['c7'] = 3;
                }
                break;
            case 6:
                if ($playinfo['c3'] != 0 && $playinfo['c9'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c3'] = 3;
                    $s['c9'] = 3;
                }
                if ($playinfo['c4'] != 0 && $playinfo['c5'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c4'] = 3;
                    $s['c5'] = 3;
                }
                break;
            case 7:
                if ($playinfo['c8'] != 0 && $playinfo['c9'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c8'] = 3;
                    $s['c9'] = 3;
                }
                if ($playinfo['c1'] != 0 && $playinfo['c4'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c1'] = 3;
                    $s['c4'] = 3;
                }
                if ($playinfo['c3'] != 0 && $playinfo['c5'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c3'] = 3;
                    $s['c5'] = 3;
                }
                break;
            case 8:
                if ($playinfo['c2'] != 0 && $playinfo['c5'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c2'] = 3;
                    $s['c5'] = 3;
                }
                if ($playinfo['c7'] != 0 && $playinfo['c9'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c7'] = 3;
                    $s['c9'] = 3;
                }
                break;
            case 9:
                if ($playinfo['c7'] != 0 && $playinfo['c8'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c7'] = 3;
                    $s['c8'] = 3;
                }
                if ($playinfo['c3'] != 0 && $playinfo['c6'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c3'] = 3;
                    $s['c6'] = 3;
                }
                if ($playinfo['c1'] != 0 && $playinfo['c5'] != 0) {
                    $gp += 3;
                    $s[ 'c' . $c ] = 3;
                    $s['c1'] = 3;
                    $s['c5'] = 3;
                }
                break;
        }
        // 拼湊 tsession 紀錄
        $currentGp = $gp;
        $res = [
            'pid'              => $this->getTPidBylineId($lineId),
            'lineId'           => $lineId,
            'userName'         => $name,
            'rid'              => $troundinfo[0]['id'],
            'gid'              => $c,
            'cid'              => 0,
            'eventDate'        => date("Y-m-d"),
            'currentGp'        => $currentGp,
            'currentGameGp'    => $currentGp,
            'currentCheckinGp' => 0,
            'cdate'            => date('Y-m-d H:i:s'),
        ];
        $this->tsessionManagementDb->insert($res);
        $id = \DB::getPdo()->lastInsertId();
        $res['id'] = $id;
        // 上檔玩家吉點計算+九宮格布局修改
        $res['gp'] = $this->addGpAndJggToTPlayer($lineId, $currentGp, $s);
        
        return $res;
    }
    
    public function addGpAndJggToTPlayer($lineId, $gp, $jgg)
    {
        // 計算增加後的吉點
        $info = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
        $gp += $info['gp'];
        $d = ['gp' => $gp];
        $d = array_merge($d, $jgg);
        // 更新
        $this->tplayerManagementDb->where('lineId', $lineId)->update($d);
        
        return $d;
    }
    
    public function tredeeminfo($lineId)
    {
        $tplayerinfo = $this->tplayerManagementDb
            ->select(['gp'])
            ->where('lineId', $lineId)
            ->first()
            ->toArray();
        
        return ($tplayerinfo['gp'] >= $this->settingManagementSv->getSetting()['TMZ_GP_DRAW']);
    }
    
    public function redeemTGood($lineId, $name)
    {
        $redeeminfo = [];
        try {
            // 回合資訊 (注意用於兌獎資訊的日期區間判定與玩遊戲不同)
            $troundinfo = $this->troundManagementDb
                ->select(['id'])
                ->where(\DB::raw("DATE_ADD(dateFinal, INTERVAL +redeemExtra DAY)"), '>=', date("Y-m-d"))
                ->where('dateBegin', '<=', date("Y-m-d"))
                ->where('active', '=', GeneralConst::ACTIVE_YES)
                ->get()
                ->toArray();
            // 來到這邊代表一定有資料
            // 準備轉蛋資訊
            $tgoodinfo = $this->tgoodManagementDb
                ->select([
                    'id',        // 回寫用
                    'ratio',     // 抽獎製造冗餘資料用
                    'active',    //
                    'lp',        //
                    'totalQty',  //
                    'issuedQty', //
                ])
                ->where('active', 'Y')
                ->get()
                ->toArray();
            $tgoodArr = [];
            foreach ($tgoodinfo as $g) for ($i = 0; $i < $g['ratio']; $i++) $tgoodArr[] = ['id' => $g['id'], 'lp' => $g['lp'], 'totalQty' => $g['totalQty'], 'issuedQty' => $g['issuedQty'], 'active' => $g['active']];
            shuffle($tgoodArr);
            $tgoodinfo = $tgoodArr[0];
            // good 準備資料
            $fromIssuedQty = $tgoodinfo['issuedQty'];
            $toIssuedQty = $fromIssuedQty + 1;
            // player 準備資料
            $tplayerinfo = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
            $fromGp = $tplayerinfo['gp'];
            $toGp = $tplayerinfo['gp'] - $this->settingManagementSv->getSetting()['TMZ_GP_DRAW'];
            \Log::info($tgoodinfo);
            \Log::info($this->settingManagementSv->getSetting()['TMZ_ISSUE_BUFFER']);
            // 所有銘謝惠顧的可能(獎項雖然有點數但已被兌換完畢 || 獎項被設定為未啟用)
            if (
                $tgoodinfo['totalQty'] - $tgoodinfo['issuedQty'] <= $this->settingManagementSv->getSetting()['TMZ_ISSUE_BUFFER']
                || $tgoodinfo['active'] == GeneralConst::ACTIVE_NO
            ) {
                $this->debuglog('redeemTGood', [
                    'msg' => '所有銘謝惠顧的可能'
                ]);
                return false;
            }
            // 銘謝惠顧獎項還是要扣除集點,所以不能返回錯誤,只是需要先控制不要進入直通
            // 直通打點與確認 (先打點,依照返回正確與否再決定要不要後續操作本地資料)
            if ($this->settingManagementSv->getSetting()['TMZ_ESI_LINEPOINT'] == 'true' && $tgoodinfo['lp'] > 0) {
                // 共用變數
                $uid = $lineId;
                $rqidA = $this->getRqId();
                $rqidB = $this->getRqId();
                $seedKey = $this->getSeedKey();
                $divDataA = $this->getDivData();
                $divDataB = $this->getDivData();
                //
                //
                // 1/2 打點
                //
                //
                // 產生本次調用的 sessionKey
                $sessionKey = $this->getSessionKey($seedKey, $divDataA);
                // 整理押碼用的資料欄內容
                $msgRaw = $this->getMsgRaw([
                    'RqClient' => env('RQ_CLIENT'),
                    'RqId'     => $rqidA,
                    'UID'      => $uid,
                    'Amount'   => $tgoodinfo['lp'],
                ]);
                $msgHash = $this->getMsgHash($msgRaw);
                // 建立ICV
                $icv = $this->getICV();
                // 建立押碼
                $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
                $mac = $this->getMAC($macRaw);
                // 參數打包
                $param = [
                    'RqClient' => env('RQ_CLIENT'),
                    'RqId'     => $rqidA,
                    'ICV'      => $icv,
                    'DivData'  => $divDataA,
                    'MAC'      => $mac,
                    'UID'      => $uid,
                    'Amount'   => $tgoodinfo['lp'],
                ];
                // 相關中繼資料打包(LOG 用)
                $logdata = [
                    'sessionKey' => $sessionKey,
                    'msgRaw'     => $msgRaw,
                    'msgHash'    => $msgHash,
                    'macRaw'     => $macRaw,
                ];
                // 憑證參數整理
                $curlparam = [
                    CURLOPT_SSLCERTTYPE    => 'PEM',
                    CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
                    CURLOPT_SSLKEYTYPE     => 'PEM',
                    CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ];
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
                // 直通紀錄
                $data = [
                    'type'  => GeneralConst::LOG_ESI,
                    'func'  => 'LinepointIssue',
                    'k'     => $uid,
                    'kk'    => 'tmz',
                    'cdate' => date("Y-m-d H:i:s"),
                ];
                // 調用
                $req = [
                    'curl'    => $curlparam,
                    'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
                    'headers' => [
                        'Content-Type' => 'application/json',
                        'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                        'Accept'       => '*/*',
                    ]
                ];
                if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
                $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
                $resA = json_decode($response->getBody());
                $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
                $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
                $this->syslog($data);
                //
                //
                // 2/2 打點確認
                //
                //
                // 產生本次調用的 sessionKey
                $sessionKey = $this->getSessionKey($seedKey, $divDataB);
                // 整理押碼用的資料欄內容
                $msgRaw = $this->getMsgRaw([
                    'RqClient'  => env('RQ_CLIENT'),
                    'RqId'      => $rqidB,
                    'QueryRqid' => $rqidA, // 剛才的RQID
                    'UID'       => $uid,
                ]);
                $msgHash = $this->getMsgHash($msgRaw);
                // 建立ICV
                $icv = $this->getICV();
                // 建立押碼
                $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
                $mac = $this->getMAC($macRaw);
                // 參數打包
                $param = [
                    'RqClient'  => env('RQ_CLIENT'),
                    'RqId'      => $rqidB,
                    'ICV'       => $icv,
                    'DivData'   => $divDataB,
                    'MAC'       => $mac,
                    'QueryRqid' => $rqidA, // 剛才的RQID
                    'UID'       => $uid,
                ];
                // 相關中繼資料打包(LOG 用)
                $logdata = [
                    'sessionKey' => $sessionKey,
                    'msgRaw'     => $msgRaw,
                    'msgHash'    => $msgHash,
                    'macRaw'     => $macRaw,
                ];
                // 憑證參數整理
                $curlparam = [
                    CURLOPT_SSLCERTTYPE    => 'PEM',
                    CURLOPT_SSLCERT        => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
                    CURLOPT_SSLKEYTYPE     => 'PEM',
                    CURLOPT_SSLKEY         => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ];
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
                if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
                // 調用
                $req = [
                    'curl'    => $curlparam,
                    'body'    => json_encode($param, JSON_UNESCAPED_UNICODE),
                    'headers' => [
                        'Content-Type' => 'application/json',
                        'User-Agent'   => 'ogilvy-' . env('APP_ENV'),
                        'Accept'       => '*/*',
                    ]
                ];
                if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
                $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/query', $req);
                $resB = json_decode($response->getBody());
                // 直通紀錄
                $data = [
                    'type'  => GeneralConst::LOG_ESI,
                    'func'  => 'LinepointQuery',
                    'k'     => $uid,
                    'kk'    => 'tmz',
                    'cdate' => date("Y-m-d H:i:s"),
                ];
                $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
                $data['memoOut'] = json_encode($resB, JSON_UNESCAPED_UNICODE);
                $this->syslog($data);
                // 打點返回成功,需要確認
                //      確認成功,對用戶返回已打點的資訊,也計入資料庫
                //      確認失敗,對用戶返回已打點的資訊,也計入資料庫(差別只在於 LOG 註記的內容不同)
                // 打點返回失敗,需要確認
                //      確認成功,對用戶返回已打點的資訊,也計入資料庫(差別也只在於 LOG 註記的內容不同)
                //      確認失敗,報錯
                if ($resA->RCode != '0000' && $resB->RCode != '0000') {
                    $this->debuglog('redeemTGood', [
                        'msg' => '打點返回失敗,需要確認'
                    ]);
                    return false;
                }
            }
            // session 插入
            $res = [
                'pid'              => $this->getTPidBylineId($lineId),
                'lineId'           => $lineId,
                'userName'         => $name,
                'rid'              => $troundinfo[0]['id'],
                'gid'              => 0,
                'cid'              => $tgoodinfo['id'], // 獎項的 ID
                'eventDate'        => date("Y-m-d"),
                'currentGp'        => -$this->settingManagementSv->getSetting()['TMZ_GP_DRAW'],
                'currentGameGp'    => 0,
                'currentCheckinGp' => 0,
                'cdate'            => date('Y-m-d H:i:s'),
            ];
            $this->tsessionManagementDb->insert($res);
            // good 更新資料
            $this->tgoodManagementDb->where('id', $tgoodinfo['id'])->update(['issuedQty' => $toIssuedQty]);
            // tplayer 更新資料
            $this->tplayerManagementDb->where('lineId', $lineId)->update(['gp' => $toGp]);
            // 整理本地紀錄資料
            $redeeminfo = [
                'playerinfo' => [
                    'lineId' => $lineId,
                    'fromGp' => $fromGp,
                    'toGp'   => $toGp,
                ],
                'redeeminfo' => [
                    'id'            => $tgoodinfo['id'],
                    'lp'            => $tgoodinfo['lp'],
                    'fromIssuedQty' => $fromIssuedQty,
                    'toIssuedQty'   => $toIssuedQty,
                ],
                'session'    => $res,
            ];
        } catch (Exception $ex) {
            // 錯誤可能發生在 memoIn 或是 out
            $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
            $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
            $this->syslog($data);
            $this->debuglog('redeemTGood', [
                'rqidA' => $rqidA ?? 0,
                'rqidB' => $rqidB ?? 0
            ], [
                'errCode' => $ex->getCode(),
                'requestTime' => $requestTime ?? '',
                'logTime' => date("Y-m-d H:i:s"),
                'errMsg' => $ex->getMessage(),
            ]);
            
            return false;
        }
        
        return $redeeminfo;
    }
}
 |