| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 | <?php
namespace App\Http\Controllers\Backend\EcManagement;
use App\Http\Services\CheckParamService;
use Illuminate\Http\Request;
use App\Http\Services\Backend\EcManagement\ProductManagementService;
use App\Http\Controllers\Controller;
use App\Http\Services\ConstDef\GeneralConst;
use Redis;
use Aws\S3\S3Client;
class ProductManagementController extends Controller
{
    
    // 相關私有服務層調用器宣告
    private $productManagementSv;
    private $checkParamSv;
    private $s3Client;
    
    public function __construct()
    {
        // 建構服務層調用器
        $this->productManagementSv = new ProductManagementService();
        $this->checkParamSv = new CheckParamService();
        $this->s3Client = new S3Client([
            'credentials' => [
                'key'    => env('AWS_S3_KEY'),
                'secret' => env('AWS_S3_SECRET'),
            ],
            'region'      => env('AWS_S3_REGION'),
            'version'     => 'latest',
        ]);
        // 時區調整
        date_default_timezone_set("Asia/Taipei");
    }
    
    /**
     * 列表頁
     *   本端點非 API,直接渲染到 view
     * @return array:          返回渲染頁
     */
    public function index()
    {
        // 渲染
        return view('admin.EcManagement.ProductManagement', [
            'can_step_0' => (Redis::get('TLW_0_MAN') || Redis::get('TLW_0_STOP')) ? false : true,
            'can_step_2' => (Redis::get('TLW_2_CSV')) ? false : true,
            'step_2_s3'  => env("AWS_S3_BUCKET"),
            'step_2_env' => env("APP_ENV"),
            'brands'     => $this->productManagementSv->getBrands(),
            'batchs'     => $this->productManagementSv->getBatchs(),
            'mstatuss'   => $this->productManagementSv->getMStatuss(),
        ]);
    }
    
    /**
     * 列表API
     *   接收搜尋參數與分頁等信息,返回JSON
     * @return string:         返回JSON
     */
    public function grid()
    {
        // 取得參數
        $param = $_GET;
        if ($param == null) exit();
        $draw = $param["draw"]; //客戶端傳來的查詢次數,無條件回傳用以核對
        $orderColumn = $param["order"][0]["column"] + 1; //前端從 0 開始送,但 mysql 從 1 開始算
        $orderDir = $param["order"][0]["dir"];
        $start = $param["start"];   // 頁碼
        $length = $param["length"]; // 一頁多大
        $searchValue = $param["search"]["value"];
        //客製化搜尋欄位
        $brand_no = $param["columns"][1]["search"]["value"];
        $batch_no = $param["columns"][2]["search"]["value"];
        $g_title = $param["columns"][3]["search"]["value"];
        $g_description = $param["columns"][4]["search"]["value"];
        $m_comment = $param["columns"][5]["search"]["value"];
        $m_status = $param["columns"][6]["search"]["value"];
        // 驗證
        if ($g_title != filter_var($g_title, FILTER_SANITIZE_SPECIAL_CHARS)) $g_title = "___CANNOT_FIND_STRING___";
        if (!$this->checkParamSv->LenMToN($g_title, 0, 50)) $g_title = "___CANNOT_FIND_STRING___";
        if ($g_description != filter_var($g_description, FILTER_SANITIZE_SPECIAL_CHARS)) $g_description = "___CANNOT_FIND_STRING___";
        if (!$this->checkParamSv->LenMToN($g_description, 0, 50)) $g_description = "___CANNOT_FIND_STRING___";
        if ($m_comment != filter_var($m_comment, FILTER_SANITIZE_SPECIAL_CHARS)) $m_comment = "___CANNOT_FIND_STRING___";
        if (!$this->checkParamSv->LenMToN($m_comment, 0, 50)) $m_comment = "___CANNOT_FIND_STRING___";
        //資料庫
        $recordsTotal = 0;
        $result = $this->productManagementSv->getProdocts(
            $recordsTotal,
            $orderColumn,
            $orderDir,
            $start,
            $length,
            $searchValue,
            $brand_no,
            $batch_no,
            $g_title,
            $g_description,
            $m_comment,
            $m_status
        );
        // 整理返回資料
        $data = array();
        for ($i = 0; $i < count($result); $i++) {
            $data[] = array(
                //一般資料
                $result[ $i ]["serno"],
                $result[ $i ]["g_id"],
                $result[ $i ]["g_title"],
                $result[ $i ]["g_description"],
                $result[ $i ]["g_image_link"],
                $result[ $i ]["g_price"],
                $result[ $i ]["g_sale_price"],
                $result[ $i ]["m_status"],
                $result[ $i ]["m_comment"],
            );
        }
        $json = array(
            "draw"            => $draw,
            "recordsTotal"    => $recordsTotal,
            "recordsFiltered" => $recordsTotal, //其實還是填入所有筆數,本次筆數可從陣列取得
            "data"            => $data,
        );
        
        // 返回
        return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
    }
    
    public function manualXml()
    {
        Redis::set('TLW_0_MAN', 'true');
        
        return $this->manualXmlStatus();
    }
    
    public function manualXmlStatus()
    {
        $json = [
            'can_step_0' => (Redis::get('TLW_0_MAN') || Redis::get('TLW_0_STOP')) ? false : true,
        ];
        
        return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
    }
    
    public function importCsv(Request $request)
    {
        // 上傳錯誤偵測(沒有包含檔案格式)
        switch ($_FILES['csv']['error']) {
            case UPLOAD_ERR_OK:
                break;
            case UPLOAD_ERR_NO_FILE:
                return json_decode(json_encode(['code' => 0, 'err' => '檔案大小錯誤'], JSON_NUMERIC_CHECK), true);
            case UPLOAD_ERR_INI_SIZE:
            case UPLOAD_ERR_FORM_SIZE:
                return json_decode(json_encode(['code' => 1, 'err' => '檔案太大'], JSON_NUMERIC_CHECK), true);
            default:
                return json_decode(json_encode(['code' => 1, 'err' => '系統錯誤'], JSON_NUMERIC_CHECK), true);
        }
        if ($_FILES['csv']['size'] > 100 * 1024 * 1024) {
            return json_decode(json_encode(['code' => 1, 'err' => '檔案最大100MB'], JSON_NUMERIC_CHECK), true);
        }
        // 路徑命名
        $path = '/tmp/tlwcsv/';
        if (!file_exists($path)) mkdir($path, 0777, true);
        chmod($path, 0777);
        $date = date('Y-m-d');
        if (!file_exists($path . 'tempImportCsv')) mkdir($path . 'tempImportCsv', 0777, true);
        chmod($path . 'tempImportCsv', 0777);
        if (!file_exists($path . 'tempImportCsv/' . $date)) mkdir($path . 'tempImportCsv/' . $date, 0777, true);
        chmod($path . 'tempImportCsv/' . $date, 0777);
        // 保存文件
        $time = time();
        move_uploaded_file($_FILES['csv']['tmp_name'], $path . 'tempImportCsv/' . $date . '/' . $time . '.csv');
        chmod($path . 'tempImportCsv/' . $date . '/' . $time . '.csv', 0777);
        // 異步處裡文件
        Redis::set('TLW_2_CSV', $path . 'tempImportCsv/' . $date . '/' . $time . '.csv' . '|' . $_POST['brand_no'] . '|' . $_POST['batch_no']);
        
        return json_decode(json_encode($_FILES, JSON_NUMERIC_CHECK), true);
    }
    
    public function importCsvStatus()
    {
        $json = [
            'can_step_2' => (Redis::get('TLW_2_CSV') || Redis::get('TLW_2_MAN')) ? false : true,
        ];
        
        return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
    }
    
    public function importCsvHandle($info)
    {
        // 上鎖
        Redis::set('TLW_2_MAN', 'true');
        // 剖析資訊
        $info = explode('|', $info);
        $csv = $info[0];
        $brand_no = $info[1];
        $batch_no = $info[2];
        // 更新產品訊息
        $fileR = fopen($csv, "r");
        while (!feof($fileR)) {
            $line = fgets($fileR);
            $cell = str_getcsv($line, ',', '"');
            if ($cell[0] != 'Item ID') {
                $g_id = $cell[0];
                $m_comment = $cell[4] ?? '';
                $m_status = $cell[5] ?? '';
                switch ($m_status) {
                    case GeneralConst::$mStatusMap[ GeneralConst::MSTATUS_WARNING ]['BNAME']:
                        $detail = $this->productManagementSv->getProductDetail($brand_no, $batch_no, $g_id);
                        $comment = (strpos($detail['m_comment'], $m_comment . ' / ') !== false) ? $detail['m_comment'] : $detail['m_comment'] . $m_comment . ' / ';
                        if ($detail['m_status'] == GeneralConst::MSTATUS_DISAPPROVED) {
                            // 原本是錯誤,但目前讀取到警告,就依然要保持錯誤狀態
                            $this->productManagementSv->updateProduct($brand_no, $batch_no, $g_id, GeneralConst::MSTATUS_DISAPPROVED, $comment);
                        } else {
                            // 原本是正常或警告,目前讀取到警告,就要將狀態設為警告
                            $this->productManagementSv->updateProduct($brand_no, $batch_no, $g_id, GeneralConst::MSTATUS_WARNING, $comment);
                        }
                        break;
                    case GeneralConst::$mStatusMap[ GeneralConst::MSTATUS_DISAPPROVED ]['BNAME']:
                        $detail = $this->productManagementSv->getProductDetail($brand_no, $batch_no, $g_id);
                        $comment = (strpos($detail['m_comment'], $m_comment . ' / ') !== false) ? $detail['m_comment'] : $detail['m_comment'] . $m_comment . ' / ';
                        // 原本不管是甚麼,目前讀取到錯誤,就要將狀態設為錯誤
                        $this->productManagementSv->updateProduct($brand_no, $batch_no, $g_id, GeneralConst::MSTATUS_DISAPPROVED, $comment);
                        break;
                }
            }
        }
        fclose($fileR);
        // 刪除檔案(權限問題無法刪除)
        unlink($csv);
    }
    
    public function manualEndpoint()
    {
        $param = $_GET;
        Redis::set('TLW_4_MAN', $param['brand_no'] . '|' . $param['batch_no']);
        
        return $this->manualEndpointStatus();
    }
    
    public function manualEndpointStatus()
    {
        $json = [
            'can_step_4' => (Redis::get('TLW_4_MAN') || Redis::get('TLW_4_STOP')) ? false : true,
        ];
        
        return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
    }
    
    public function exportCsv(Request $request)
    {
        $title = [
            '系統號',
            '商品號',
            '品名',
            '描述',
            '網頁',
            '圖片路徑',
            '定價',
            '售價',
            '掃描結果',
            '掃描問題',
        ];
        $products = $this->productManagementSv->getExportProdocts($request);
        $this->productManagementSv->downloadExcel($title, $products, date("YmdHis") . '-REPORT-' . GeneralConst::$brandMap[ $request->brand_no ]['LB']);
    }
    
    // XML轉換腳本
    public function xml()
    {
        // 防重鎖
        if (!Redis::get('TLW_0_XML')) {
            Redis::set('TLW_0_XML', 'true');
            Redis::expire('TLW_0_XML', 1200); // 20 分鐘
            // 產生批次號
            $batch_no = date("Y-m-d H:i:s");
            $batch_no_digit = date("YmdHis");
            // 產生刪除批次號
            $del_batch_no = date('Y-m-d', strtotime(date("Y-m-d H:i:s") . "-14 days"));
            $del_batch_no_digit = date('Ymd', strtotime(date("Y-m-d") . "-14 days"));
            // 逐一品牌處理
            foreach (GeneralConst::$brandMap as $k => $v) {
                for ($i = 1; $i <= 14; $i++) {
                }
                // 載入品牌原始 XML
                $original = simplexml_load_file($v['URL']);
                // 開始寫入檔案
                $path = '/tmp/tlw/' . env('APP_ENV') . '/' . $v['LB'] . '/';
                if (!file_exists($path)) mkdir($path, 0777, true);
                $fileW = fopen($path . $batch_no_digit . '.xml', "w");
                fwrite($fileW, '<?xml version="1.0" encoding="UTF-8"?><rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"><channel><title>特力屋</title><link>' . $v['URL'] . '</link><description></description>' . "\n");
                // 原始商品逐筆處裡(保留符號已被CDATA處理)
                // 注意可能出現相同品牌之中有相同 g_id 的現象,但由於不影響資料流程所以不處理
                $data = [];
                foreach ($original->Product as $p) {
                    // custom_label_1 為客戶系統自定義欄位,Y代表可讓GMC上架,N代表不可讓GM上架
                    if ((string)$p->custom_label_1 == GeneralConst::PUBLISH_YES) {
                        // XML
                        fwrite($fileW, '<item>' . "\n");
                        fwrite($fileW, '<g:id>' . (string)$p->SKU . '</g:id>' . "\n");
                        fwrite($fileW, '<g:title><![CDATA[' . (string)$p->Name . ']]></g:title>' . "\n");
                        fwrite($fileW, '<g:description><![CDATA[' . (string)$p->Description . ']]></g:description>' . "\n");
                        fwrite($fileW, '<g:link><![CDATA[' . (string)$p->URL . ']]></g:link>' . "\n");
                        fwrite($fileW, '<g:image_link><![CDATA[' . (string)$p->LargeImage . ']]></g:image_link>' . "\n");
                        fwrite($fileW, '<g:condition>' . (string)$p->Condition . '</g:condition>' . "\n");
                        fwrite($fileW, '<g:availability>' . (string)'in stock' . '</g:availability>' . "\n");
                        fwrite($fileW, '<g:price>' . (string)$p->Price . ' TWD' . '</g:price>' . "\n");
                        fwrite($fileW, '<g:sale_price>' . (string)$p->SalePrice . ' TWD' . '</g:sale_price>' . "\n");
                        fwrite($fileW, '<g:brand><![CDATA[' . (string)'特力屋' . ']]></g:brand>' . "\n");
                        fwrite($fileW, '<g:google_product_category>' . (string)'' . '</g:google_product_category>' . "\n");
                        fwrite($fileW, '<g:product_type><![CDATA[' . (string)$p->Category . ']]></g:product_type>' . "\n");
                        fwrite($fileW, '<g:custom_label_0><![CDATA[' . (string)$p->custom_label_0 . ']]></g:custom_label_0>' . "\n");
                        fwrite($fileW, '</item>' . "\n");
                        // 資料庫(保留符號已被框架處理)
                        $data[] = [
                            'brand_no'         => $k,
                            'batch_no'         => $batch_no,
                            'g_id'             => (string)$p->SKU,
                            'g_title'          => (string)$p->Name,
                            'g_description'    => (string)$p->Description,
                            'g_link'           => (string)$p->URL,
                            'g_image_link'     => (string)$p->LargeImage,
                            'g_price'          => (string)$p->Price . ' TWD',
                            'g_sale_price'     => (string)$p->SalePrice . ' TWD',
                            'g_product_type'   => (string)$p->Category,
                            'g_custom_label_0' => (string)$p->custom_label_0,
                        ];
                        if (count($data) >= 100) {
                            $this->productManagementSv->insertProduct($data);
                            $data = [];
                        }
                    }
                }
                fwrite($fileW, '</channel><Updated>' . $batch_no . '</Updated></rss>' . "\n");
                $this->productManagementSv->insertProduct($data);
                // 結束寫入檔案
                fclose($fileW);
                // 檔案上傳至 S3
                $fileR = fopen($path . $batch_no_digit . '.xml', "r");
                $file_name = env('APP_ENV') . '/' . $v['LB'] . '/' . $batch_no_digit . '.xml';
                $this->s3Client->putObject([
                    'ACL'         => 'public-read',
                    'Body'        => $fileR,
                    'Bucket'      => env('AWS_S3_BUCKET'),
                    'ContentType' => 'application/xml',
                    'Key'         => $file_name,
                ]);
                fclose($fileR);
                // 刪除寫入的檔案
                unlink($path . $batch_no_digit . '.xml');
                // 刪除舊資料(XML)
                $objects = $this->s3Client->listObjects([
                    'Bucket' => env('AWS_S3_BUCKET'),
                    'Prefix' => env('APP_ENV') . '/' . $v['LB'] . '/' . $del_batch_no_digit,
                ]);
                foreach ($objects['Contents'] ?? [] as $object) {
                    $tmpkey = $object['Key'] ?? null;
                    if ($tmpkey) {
                        $this->s3Client->deleteObjects([
                            'Bucket' => env('AWS_S3_BUCKET'),
                            'Delete' => ['Objects' => [['Key' => $tmpkey]]],
                        ]);
                    }
                }
            }
            // 刪除舊資料(SQL)
            $this->productManagementSv->deleteProductByBatchNo($del_batch_no);
            Redis::del('TLW_0_XML');
        }
    }
    
    // XML轉換腳本(特力屋新版)
    public function xmlG()
    {
        // 防重鎖
        if (!Redis::get('TLW_0_XML')) {
            Redis::set('TLW_0_XML', 'true');
            Redis::expire('TLW_0_XML', 1200); // 20 分鐘
            // 產生批次號
            $batch_no = date("Y-m-d H:i:s");
            $batch_no_digit = date("YmdHis");
            // 產生刪除批次號
            $del_batch_no = date('Y-m-d', strtotime(date("Y-m-d H:i:s") . "-14 days"));
            $del_batch_no_digit = date('Ymd', strtotime(date("Y-m-d") . "-14 days"));
            // 逐一品牌處理
            foreach (GeneralConst::$brandMap as $k => $v) {
                for ($i = 1; $i <= 14; $i++) {
                }
                // 載入品牌原始 XML
                $original = simplexml_load_file($v['URLG']); // [新舊版本差異]來源修改
                // 開始寫入檔案
                $path = '/tmp/tlw/' . env('APP_ENV') . '/' . $v['LB'] . '/';
                if (!file_exists($path)) mkdir($path, 0777, true);
                $fileW = fopen($path . $batch_no_digit . '.xml', "w");
                fwrite($fileW, '<?xml version="1.0" encoding="UTF-8"?><rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"><channel><title>特力屋</title><link>' . $v['URL'] . '</link><description></description>' . "\n");
                // 原始商品逐筆處裡(保留符號已被CDATA處理)
                // 注意可能出現相同品牌之中有相同 g_id 的現象,但由於不影響資料流程所以不處理
                $data = [];
                foreach ($original->channel->item as $p) { // [新舊版本差異]解析層級修改
                    // custom_label_1 為客戶系統自定義欄位,Y代表可讓GMC上架,N代表不可讓GM上架
                    if ((string)$p->custom_label_1 == GeneralConst::PUBLISH_YES) {
                        // XML
                        fwrite($fileW, '<item>' . "\n");
                        fwrite($fileW, '<g:id>' . (string)$p->children('g', true)->id . '</g:id>' . "\n");
                        fwrite($fileW, '<g:title><![CDATA[' . (string)$p->children('g', true)->title . ']]></g:title>' . "\n");
                        fwrite($fileW, '<g:description><![CDATA[' . (string)$p->children('g', true)->description . ']]></g:description>' . "\n");
                        fwrite($fileW, '<g:link><![CDATA[' . (string)$p->children('g', true)->link . ']]></g:link>' . "\n");
                        fwrite($fileW, '<g:image_link><![CDATA[' . (string)$p->children('g', true)->image_link . ']]></g:image_link>' . "\n");
                        fwrite($fileW, '<g:condition>' . (string)$p->children('g', true)->condition . '</g:condition>' . "\n");
                        fwrite($fileW, '<g:availability>' . (string)$p->children('g', true)->availability . '</g:availability>' . "\n");
                        fwrite($fileW, '<g:price>' . (string)$p->children('g', true)->price . '</g:price>' . "\n");
                        fwrite($fileW, '<g:sale_price>' . (string)$p->children('g', true)->sale_price . '</g:sale_price>' . "\n");
                        fwrite($fileW, '<g:brand><![CDATA[' . (string)$p->children('g', true)->brand . ']]></g:brand>' . "\n");
                        fwrite($fileW, '<g:google_product_category>' . (string)'' . '</g:google_product_category>' . "\n");
                        fwrite($fileW, '<g:product_type><![CDATA[' . (string)$p->children('g', true)->product_type . ']]></g:product_type>' . "\n");
                        fwrite($fileW, '<g:custom_label_0><![CDATA[' . (string)$p->custom_label_0 . ']]></g:custom_label_0>' . "\n");
                        fwrite($fileW, '</item>' . "\n");
                        // 資料庫(保留符號已被框架處理)
                        $data[] = [
                            'brand_no'         => $k,
                            'batch_no'         => $batch_no,
                            'g_id'             => (string)$p->children('g', true)->id,
                            'g_title'          => (string)$p->children('g', true)->title,
                            'g_description'    => (string)$p->children('g', true)->description,
                            'g_link'           => (string)$p->children('g', true)->link,
                            'g_image_link'     => (string)$p->children('g', true)->image_link,
                            'g_price'          => (string)$p->children('g', true)->price,
                            'g_sale_price'     => (string)$p->children('g', true)->sale_price,
                            'g_product_type'   => (string)$p->children('g', true)->product_type,
                            'g_custom_label_0' => (string)$p->custom_label_0,
                        ];
                        if (count($data) >= 100) {
                            $this->productManagementSv->insertProduct($data);
                            $data = [];
                        }
                    }
                }
                fwrite($fileW, '</channel><Updated>' . $batch_no . '</Updated></rss>' . "\n");
                $this->productManagementSv->insertProduct($data);
                // 結束寫入檔案
                fclose($fileW);
                // 檔案上傳至 S3
                $fileR = fopen($path . $batch_no_digit . '.xml', "r");
                $file_name = env('APP_ENV') . '/' . $v['LB'] . '/' . $batch_no_digit . '.xml';
                $this->s3Client->putObject([
                    'ACL'         => 'public-read',
                    'Body'        => $fileR,
                    'Bucket'      => env('AWS_S3_BUCKET'),
                    'ContentType' => 'application/xml',
                    'Key'         => $file_name,
                ]);
                fclose($fileR);
                // 刪除寫入的檔案
                unlink($path . $batch_no_digit . '.xml');
                // 刪除舊資料(XML)
                $objects = $this->s3Client->listObjects([
                    'Bucket' => env('AWS_S3_BUCKET'),
                    'Prefix' => env('APP_ENV') . '/' . $v['LB'] . '/' . $del_batch_no_digit,
                ]);
                foreach ($objects['Contents'] ?? [] as $object) {
                    $tmpkey = $object['Key'] ?? null;
                    if ($tmpkey) {
                        $this->s3Client->deleteObjects([
                            'Bucket' => env('AWS_S3_BUCKET'),
                            'Delete' => ['Objects' => [['Key' => $tmpkey]]],
                        ]);
                    }
                }
            }
            // 刪除舊資料(SQL)
            $this->productManagementSv->deleteProductByBatchNo($del_batch_no);
            Redis::del('TLW_0_XML');
        }
    }
    
    // 更新動態饋給
    public function endpoint()
    {
        // 防重鎖
        if (!Redis::get('TLW_4_XML')) {
            Redis::set('TLW_4_XML', 'true');
            Redis::expire('TLW_4_XML', 1200); // 20 分鐘
            // 取出資料
            $info = explode('|', Redis::get('TLW_4_MAN'));
            $brand_no = $info[0];
            $batch_no = $info[1];
            $products = $this->productManagementSv->getEndpointProdocts($brand_no, $batch_no);
            // 拼裝XML
            // 開始寫入檔案
            $path = '/tmp/tlwendpoint/' . env('APP_ENV') . '/' . GeneralConst::$brandMap[ $brand_no ]['LB'] . '/';
            if (!file_exists($path)) mkdir($path, 0777, true);
            $fileW = fopen($path . 'FINAL.xml', "w");
            fwrite($fileW, '<?xml version="1.0" encoding="UTF-8"?><rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"><channel><title>特力屋</title><link>' . GeneralConst::$brandMap[ $brand_no ]['URL'] . '</link><description></description>' . "\n");
            foreach ($products as $p) {
                fwrite($fileW, '<item>' . "\n");
                fwrite($fileW, '<g:id>' . (string)$p['g_id'] . '</g:id>' . "\n");
                fwrite($fileW, '<g:title><![CDATA[' . (string)$p['g_title'] . ']]></g:title>' . "\n");
                fwrite($fileW, '<g:description><![CDATA[' . (string)$p['g_description'] . ']]></g:description>' . "\n");
                fwrite($fileW, '<g:link><![CDATA[' . (string)$p['g_link'] . ']]></g:link>' . "\n");
                fwrite($fileW, '<g:image_link><![CDATA[' . (string)$p['g_image_link'] . ']]></g:image_link>' . "\n");
                fwrite($fileW, '<g:condition>' . (string)$p['g_condition'] . '</g:condition>' . "\n");
                fwrite($fileW, '<g:availability>' . (string)$p['g_availability'] . '</g:availability>' . "\n");
                fwrite($fileW, '<g:price>' . (string)$p['g_price'] . '</g:price>' . "\n");
                fwrite($fileW, '<g:sale_price>' . (string)$p['g_sale_price'] . '</g:sale_price>' . "\n");
                fwrite($fileW, '<g:brand><![CDATA[' . (string)$p['g_brand'] . ']]></g:brand>' . "\n");
                fwrite($fileW, '<g:google_product_category>' . (string)$p['g_google_product_category'] . '</g:google_product_category>' . "\n");
                fwrite($fileW, '<g:product_type><![CDATA[' . (string)$p['g_product_type'] . ']]></g:product_type>' . "\n");
                fwrite($fileW, '<g:custom_label_0><![CDATA[' . (string)$p['g_custom_label_0'] . ']]></g:custom_label_0>' . "\n");
                fwrite($fileW, '</item>' . "\n");
            }
            fwrite($fileW, '</channel><Updated>' . $batch_no . '</Updated></rss>' . "\n");
            // 結束寫入檔案
            fclose($fileW);
            // 上傳至S3取代
            $fileR = fopen($path . 'FINAL.xml', "r");
            $file_name = env('APP_ENV') . '/' . GeneralConst::$brandMap[ $brand_no ]['LB'] . '/FINAL.xml';
            $this->s3Client->putObject([
                'ACL'         => 'public-read',
                'Body'        => $fileR,
                'Bucket'      => env('AWS_S3_BUCKET'),
                'ContentType' => 'application/xml',
                'Key'         => $file_name,
            ]);
            fclose($fileR);
            // 刪除檔案
            unlink($path . 'FINAL.xml');
            Redis::del('TLW_4_XML');
        }
    }
    
}
 |