| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 | <?php
namespace App\Http\Services\Backend\DataManagement;
use App\Http\Services\ConstDef\GeneralConst;
use App\Models\Web\TPlayer;
use App\Models\Web\Syslogt;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Illuminate\Support\Facades\DB;
class TPlayerManagementService
{
    // 相關私有 model 調用器宣告
    private $tplayerManagementDb;
    private $syslogtManagementDb;
    
    public function __construct()
    {
        // 建構 model 調用器
        $this->tplayerManagementDb = new TPlayer();
        $this->syslogtManagementDb = new Syslogt();
    }
    
    public function getTPlayers(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue,
        $lineId,
//        $userName,
        $createDateStart,
        $createDateFinal
    )
    {
        $tplayers = $this->tplayerManagementDb
            ->select([
                'id',
                'lineId',
                //                'userName',
                //                \DB::raw("CONCAT('<img src=\"', userPhoto, '\" width=\"50\" height=\"50\">') as userPhoto"),
                'gp',
                \DB::raw("CONCAT(c1, c2, c3, '<br>', c4, c5, c6, '<br>', c7, c8, c9) as jgg"),
                'cdate',
                'mdate',
            ]);
        // 過濾搜尋條件
        if ($lineId != '') $tplayers = $tplayers->where('lineId', 'like', '%' . $lineId . '%');
//        if ($userName != '') $tplayers = $tplayers->where('userName', 'like', '%' . $userName . '%');
        $tplayers = $tplayers->where('cdate', '>=', $createDateStart . ' 00:00:00');
        $tplayers = $tplayers->where('cdate', '<=', $createDateFinal . ' 23:59:59');
        // 取總筆數
        $cnt = $tplayers->count();
        // 排序
        $tplayers = $tplayers
            ->orderByRaw((int)$orderColumn . ' ' . 'ASC');
        // 彙整
        // 分頁
        $tplayers = $tplayers
            ->skip($start)->take($length);
        // 實際取資料
        $tplayers = $tplayers
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $tplayers;
    }
    
    public function getExports($param)
    {
        $tplayers = $this->tplayerManagementDb
            ->select([
                'id',
                'lineId',
                //                'userName',
                'gp',
                \DB::raw("CONCAT(c1, c2, c3, c4, c5, c6, c7, c8, c9) as jgg"),
                'cdate',
                'mdate',
            ]);
        // 過濾搜尋條件
        if ($param["lineId"] != '') $tplayers = $tplayers->where('lineId', 'like', '%' . $param["lineId"] . '%');
//        if ($param["userName"] != '') $tplayers = $tplayers->where('userName', 'like', '%' . $param["userName"] . '%');
        $tplayers = $tplayers->where('cdate', '>=', $param["createDateStart"] . ' 00:00:00');
        $tplayers = $tplayers->where('cdate', '<=', $param["createDateFinal"] . ' 23:59:59');
        // 實際取資料
        $tplayers = $tplayers
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $tplayers;
    }
    
    public function downloadExcel($titles = [], $datas = [], $fileName = 'simple')
    {
        $spreadsheet = new Spreadsheet();
        ini_set('memory_limit', '1024M');
        ini_set("max_execution_time", "600");
        $spreadsheet->getActiveSheet()
            ->fromArray(
                $titles, // The data to set
                null, // Array values with this value will not be set
                'A1' // Top left coordinate of the worksheet range where we want to set these values (default is A1)
            );
        
        $spreadsheet->getActiveSheet()
            ->fromArray(
                $datas, // The data to set
                null, // Array values with this value will not be set
                'A2' // Top left coordinate of the worksheet range where we want to set these values (default is A1)
            );
        
        // Redirect output to a client’s web browser (Xlsx)
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
        
        // If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0
        
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit;
    }
    
    public function jggclear($oid)
    {
        $this->tplayerManagementDb->where('id', '>=', 0)->update([
            'c1' => 0,
            'c2' => 0,
            'c3' => 0,
            'c4' => 0,
            'c5' => 0,
            'c6' => 0,
            'c7' => 0,
            'c8' => 0,
            'c9' => 0,
        ]);
        $rc = \DB::select("SELECT ROW_COUNT() AS rc;");
        $rc = $rc[0]->rc;
        // syslogt
        $this->syslogtManagementDb
            ->insert([
                'type'    => GeneralConst::LOG_ADMIN,
                'func'    => __FUNCTION__,
                'k'       => $oid,
                'memoIn'  => json_encode([], JSON_UNESCAPED_UNICODE),
                'memoOut' => json_encode(['rc' => $rc], JSON_UNESCAPED_UNICODE),
                'cdate'   => date("Y-m-d H:i:s"),
            ]);
        
        return true;
    }
    
    public function gpclear($oid)
    {
        $this->tplayerManagementDb->where('id', '>=', 0)->update(['gp' => 0]);
        $rc = \DB::select("SELECT ROW_COUNT() AS rc;");
        $rc = $rc[0]->rc;
        // syslog
        $this->syslogtManagementDb
            ->insert([
                'type'    => GeneralConst::LOG_ADMIN,
                'func'    => __FUNCTION__,
                'k'       => $oid,
                'memoIn'  => json_encode([], JSON_UNESCAPED_UNICODE),
                'memoOut' => json_encode(['rc' => $rc], JSON_UNESCAPED_UNICODE),
                'cdate'   => date("Y-m-d H:i:s"),
            ]);
        
        return true;
    }
    
}
 |