<?php

/**
 * 网前用的抓取程序，主要是单独处理抓取的单个产品
 */

defined('BASEPATH') or exit('No direct script access allowed');

class webgetbokun extends CI_Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->load->library("Bokun_lib");
        $this->load->model("tpBokun_model");
        $this->load->helper("text");
        $this->load->model("InfoBokunData_model");
    }


    public function index()
    {
    }

    /**
     * @description: 检查是否是新的bokun产品，根据infobokundata表的activityid记录
     * @param {type} 
     * @return: 
     * @Date Changed: 
     */
    function checkActivityId($activityid)
    {
        $result =  $this->tpBokun_model->checkActivityId($activityid);
        // print_r($result);
        echo $result;
        return $result;
    }

    /**
     * @description: 前端根据activityid抓取具体的产品信息，并根据传递过来pag_code，保存数据到信息平台
     * @param string $PassParam ：传递过来的参数，base64
     * @return: 
     * @Date Changed: 
     */
    function addActivity($PassParam)
    {
        $ParamData = json_decode(base64_decode($PassParam));
        //print_r($ParamData);
        $activityid = $ParamData->activityid;
        $PAG_Code = $ParamData->PAG_Code;
        $CityName = $ParamData->CityName;
        $checkCode = $ParamData->checkCode;

        if ($checkCode != "cits") {
            return "Error"; //传递的验证code错误
        } else {
            //1.根据城市名称获取对应的is_id作为 父类ID
            $is_id = $this->get_isid_byCity($CityName);

            if ($is_id == 0) {
                return;
            }

            //2.根据$activityid 抓取接口数据
            $activityDeatil = json_decode($this->bokun_lib->get_activity_detail($activityid));
            $title = $activityDeatil->title;
            $title_url = str_replace("'", "", strtolower($title));
            $title_url = str_replace(":", "", $title_url);
            $title_url = str_replace("(", " ", $title_url);
            $title_url = str_replace(")", " ", $title_url);
            $title_url = str_replace("/", " ", $title_url);
            $title_url = str_replace("\\", " ", $title_url);
            $title_url = str_replace("  ", " ", $title_url);
            $title_url = str_replace(' ', '-', $title_url);
            $CityName_url = str_replace("'", "", str_replace(' ', '-', strtolower($CityName)));

            //3.根据抓取数据进行信息表infocontent和infostructures表的添加
            $this->tpBokun_model->ic_title = $title;
            $this->tpBokun_model->ic_url_title = $title;
            $guideUrl = "/$CityName_url-tours/$title_url/";
            $this->tpBokun_model->ic_url = $guideUrl;

            //抓取数据组合成详细内容。
            $arrContent = $activityDeatil->agendaItems;
            $strContent = "";
            foreach ($arrContent as $ContentItem) {
                $strTemp = "";
                $strDay = "";
                if ($ContentItem->day != 1) {
                    $strDay = "(day " . $ContentItem->day . ")";
                }
                if ($ContentItem->title != "") {
                    $strTemp .= "<h3><i class=\"fa fa-circle\" aria-hidden=\"true\"></i>" . $ContentItem->title . $strDay . "</h3>";
                }
                if ($ContentItem->body != "") {
                    $strTemp .= $ContentItem->body;
                }
                // if ($ContentItem->excerpt != "") {
                //     $strTemp .= "<p>" . $ContentItem->excerpt . "</p>";
                // }

                if ($strTemp != "") {
                    $strContent .= "<div class=\"details-list\">" . $strTemp . "</div>";
                }
            }


            //简介
            $temp_summary = $activityDeatil->description;
            // if ($activityDeatil->excerpt != "") {
            //     $temp_summary .= "<p>" . $activityDeatil->excerpt . "</p>";
            // }

            //详细内容使用新的模板，把简介及所有行程及对应FAQ信息全部集合
            $tourstype='<div class="tours-type">
            <ul>
                <li><i class="fa fa-history" aria-hidden="true"></i>'.$activityDeatil->durationText.'</li>
                <li><i class="fa fa-calendar" aria-hidden="true"></i>Every day</li>
                <li><i class="fa fa-tags" aria-hidden="true"></i>City tour</li>
                </ul>
                <div class="clearfix"></div>
            </div>';
            //overview
            $Description ='<div class="details-box active">
            <h2>Description<i class="fa fa-angle-down" aria-hidden="true"></i></h2>
            <div class="details-content">
                <div class="details-list">
                <h3><i class="fa fa-circle" aria-hidden="true"></i>Overview</h3>'.$temp_summary.'
                </div>
            </div>
            </div>';
            //行程
            $Itinerary ='<div class="details-box ">
            <h2>Itinerary<i class="fa fa-angle-down" aria-hidden="true"></i></h2>
            <div class="details-content">'.$strContent.'</div></div>';
            //FAQ
            $tempFaq = $strFaq = $strIncluded = $strExcluded = '';
            if (isset($activityDeatil->included)){
                if ($activityDeatil->included!="" && $activityDeatil->included!=null){
                    $strFaq = '<div class="details-list">
                    <h3><i class="fa fa-circle" aria-hidden="true"></i>What\'s included?</h3>'.$activityDeatil->included.'</div>';
                    $strIncluded.=$strFaq;
                }
            }
            if (isset($activityDeatil->excluded)){
                if ($activityDeatil->excluded!="" && $activityDeatil->excluded!=null){
                    $strFaq = '<div class="details-list">
                    <h3><i class="fa fa-circle" aria-hidden="true"></i>What\'s excluded？</h3>'.$activityDeatil->excluded.'</div>';
                    $strExcluded.=$strFaq;
                }
            }
            $tempFaq = '<div class="row"><div class="col-xs-24 col-sm-12">'.$strIncluded.'</div><div class="col-xs-24 col-sm-12">'.$strExcluded.'</div><div class="clearfix"></div></div>';

            if (isset($activityDeatil->requirements)){
                if ($activityDeatil->requirements!="" && $activityDeatil->requirements!=null){
                    $strFaq = '<div class="details-list">
                    <h3><i class="fa fa-circle" aria-hidden="true"></i>What do I need to bring?</h3>'.$activityDeatil->requirements.'</div>';
                    $tempFaq.=$strFaq;
                }
            }

            if (isset($activityDeatil->attention)){
                if ($activityDeatil->attention!="" && $activityDeatil->attention!=null){
                    $strFaq = '<div class="details-list">
                    <h3><i class="fa fa-circle" aria-hidden="true"></i>What need to note？</h3>'.$activityDeatil->attention.'</div>';
                    $tempFaq.=$strFaq;
                }
            }

            if (isset($activityDeatil->noPickupMsg)){
                if ($activityDeatil->noPickupMsg!="" && $activityDeatil->noPickupMsg!=null){
                    $strFaq = '<div class="details-list">
                    <h3><i class="fa fa-circle" aria-hidden="true"></i>What if my hotel is outside the pickup area？</h3>'.$activityDeatil->noPickupMsg.'</div>';
                    $tempFaq.=$strFaq;
                }
            }

            // if (isset($activityDeatil->ticketMsg)){
            //     if ($activityDeatil->ticketMsg!="" && $activityDeatil->ticketMsg!=null){
            //         $strFaq = '<div class="details-list">
            //         <h3><i class="fa fa-circle" aria-hidden="true"></i>ticketMsg</h3>'.$activityDeatil->ticketMsg.'</div>';
            //         $tempFaq.=$strFaq;
            //     }
            // }

            //退款信息
            if (isset($activityDeatil->cancellationPolicy)){
                if (isset($activityDeatil->cancellationPolicy->penaltyRules) && is_array($activityDeatil->cancellationPolicy->penaltyRules)){
                    $cancelPolicy = "";
                    foreach ($activityDeatil->cancellationPolicy->penaltyRules as $Item_cancel) {
                        if ($Item_cancel->chargeType == "PERCENTAGE"){
                            $cancelPolicy .= "<li>We will charge a cancellation fee of ".$Item_cancel->charge."% if booking is cancelled ".$Item_cancel->cutoffHours." hours or less before event</li>";
                        }
                    }

                    if ($cancelPolicy!=""){
                        $cancelPolicy = "<ul>".$cancelPolicy."</ul>";
                    }
                    $strFaq = '<div class="details-list">
                    <h3><i class="fa fa-circle" aria-hidden="true"></i>Cancellation policy</h3>'.$cancelPolicy.'</div>';
                    $tempFaq.=$strFaq;
                }
            }

            $FAQ = '<div class="details-box">
            <h2>Know before you book<i class="fa fa-angle-down" aria-hidden="true"></i></h2>
            <div class="details-content">'.$tempFaq.'</div></div>';

            $allContent = $tourstype.$Description.$Itinerary.$FAQ;
            $this->tpBokun_model->ic_content = $allContent;
            $this->tpBokun_model->ic_summary = $temp_summary;

            //seo
            $this->tpBokun_model->ic_seo_title = character_limiter($title, 100);
            $this->tpBokun_model->ic_seo_description = $title; //iconv("GBK","UTF-8",character_limiter(strip_tags($activityDeatil->description), 200));
            $tempKeywords = $activityDeatil->keywords;
            if (is_array($tempKeywords) && count($tempKeywords) > 0) {
                $this->tpBokun_model->ic_seo_keywords = implode(",", $tempKeywords);
            }
            $this->tpBokun_model->ic_status = 1;

            $tempData = json_decode($this->tpBokun_model->addInfo($is_id));
            if (is_array($tempData)) {
                $ic_id = $tempData[0]->value->ic_id;
            } else {
                $result["status"] = "error";
                echo json_encode($result);
            }

            //4.附加属性添加（infometa表及新建的infoBokunData表）
            if ($ic_id > 0) {

                //先旧的infometa表
                $tempRequireFileds = $activityDeatil->requiredCustomerFields; //根据抓取的需求字段，判断是否需要passport
                if (is_array($tempRequireFileds)) {
                    if (in_array("passportId", $tempRequireFileds)) {
                        $this->tpBokun_model->addMeta($ic_id, "meta_trippest_type", "gg");
                    }
                }
                $this->tpBokun_model->addMeta($ic_id, "meta_product_code", $PAG_Code);  //对应翰特编码
                if (isset($activityDeatil->googlePlace->city)){
                    $this->tpBokun_model->addMeta($ic_id, "meta_facts_city", $activityDeatil->googlePlace->city); //保存一下具体城市
                }
            

                //然后保存新建的infobokunData表
                $this->InfoBokunData_model->ibd_ic_id = $ic_id;  //统一的

                $this->InfoBokunData_model->ibd_ItemName = "ActivityID";
                $this->InfoBokunData_model->ibd_Itemvalue = $activityid;
                $ibd_id = $this->InfoBokunData_model->Add();

                if ($this->InfoBokunData_model->ibd_ItemName == "ActivityID") {
                    $this->InfoBokunData_model->ibd_parentId = 0;
                } else {
                    $this->InfoBokunData_model->ibd_parentId = $ibd_id; //设置剩余的属性都是这个activityid的子类，避免每条记录都保存activityid
                }
                if (isset($activityDeatil->keyPhoto)) {
                    $this->InfoBokunData_model->ibd_ItemName = "keyPhoto";  //主要图片
                    $this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->keyPhoto->originalUrl;
                    $this->InfoBokunData_model->ibd_ItemType = "originalUrl";
                    $this->InfoBokunData_model->Add();
                    $arrKeyPhoto = $activityDeatil->keyPhoto->derived;  //主要图片派生图，大中小类型
                    if (is_array($arrKeyPhoto)) {
                        foreach ($arrKeyPhoto as $kPhotoItem) {
                            $this->InfoBokunData_model->ibd_Itemvalue = $kPhotoItem->url;
                            $this->InfoBokunData_model->ibd_ItemType = $kPhotoItem->name;
                            $this->InfoBokunData_model->Add();
                        }
                    }
                }


                $arrProductPhoto = $activityDeatil->photos; //产品主要图片
                if (is_array($arrProductPhoto)) {
                    foreach ($arrProductPhoto as $pPhotoItem) {
                        $this->InfoBokunData_model->ibd_ItemName = "ProductPhoto";
                        $this->InfoBokunData_model->ibd_Itemvalue = $pPhotoItem->originalUrl;
                        $this->InfoBokunData_model->ibd_ItemType = "originalUrl";
                        $this->InfoBokunData_model->Add();

                        $arrProductPhotoDerived = $pPhotoItem->derived; //产品主要图片的派生图，大中小类型
                        if (is_array($arrProductPhotoDerived)) {
                            foreach ($arrProductPhotoDerived as $pPhotoDerivedItem) {
                                $this->InfoBokunData_model->ibd_Itemvalue = $pPhotoDerivedItem->url;
                                $this->InfoBokunData_model->ibd_ItemType = $pPhotoDerivedItem->name;
                                $this->InfoBokunData_model->Add();
                            }
                        }
                    }
                }


                //一些FAQ类的属性，如included，requirements,这些内容已经合并到详细内容里面
                // $this->InfoBokunData_model->ibd_ItemName = "included";  //
                // $this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->included;
                // $this->InfoBokunData_model->ibd_ItemType = "";
                // $this->InfoBokunData_model->Add();

                // $this->InfoBokunData_model->ibd_ItemName = "excluded";  //
                // $this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->excluded;
                // $this->InfoBokunData_model->ibd_ItemType = "";
                // $this->InfoBokunData_model->Add();

                // $this->InfoBokunData_model->ibd_ItemName = "requirements";  //
                // $this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->requirements;
                // $this->InfoBokunData_model->ibd_ItemType = "";
                // $this->InfoBokunData_model->Add();

                // $this->InfoBokunData_model->ibd_ItemName = "attention";  //
                // $this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->attention;
                // $this->InfoBokunData_model->ibd_ItemType = "";
                // $this->InfoBokunData_model->Add();

                // if ($activityDeatil->noPickupMsg != null && $activityDeatil->noPickupMsg != "") {
                //     $this->InfoBokunData_model->ibd_ItemName = "noPickupMsg";  //
                //     $this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->noPickupMsg;
                //     $this->InfoBokunData_model->ibd_ItemType = "";
                //     $this->InfoBokunData_model->Add();
                // }

                // if ($activityDeatil->ticketMsg != null && $activityDeatil->ticketMsg != "") {
                //     $this->InfoBokunData_model->ibd_ItemName = "ticketMsg";  //
                //     $this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->ticketMsg;
                //     $this->InfoBokunData_model->ibd_ItemType = "";
                //     $this->InfoBokunData_model->Add();
                // }

                //成人，儿童，婴儿信息
                $pricingCategories = $activityDeatil->pricingCategories;
                if (is_array($pricingCategories)){
                    foreach ($pricingCategories as $priceCg){
                        $ItemName_priceCg = "";                        
                        $ticketCategory = $priceCg->ticketCategory;
                        if ( strtoupper($ticketCategory)=="ADULT"){
                            $ItemName_priceCg = "AdultMsg"; 
                        }elseif (strtoupper($ticketCategory)=="CHILD") {
                            $ItemName_priceCg = "ChildMsg"; 
                        }elseif (strtoupper($ticketCategory)=="INFANT") {
                            $ItemName_priceCg = "InfantMsg";
                        }else{
                            $ItemName_priceCg = "Other_Msg"; 
                        }

                        $ItemValue_priceCg = "age ".$priceCg->minAge.'-'.$priceCg->maxAge;

                        $this->InfoBokunData_model->ibd_ItemName =  $ItemName_priceCg;  //
                        $this->InfoBokunData_model->ibd_Itemvalue = $ItemValue_priceCg;
                        $this->InfoBokunData_model->ibd_ItemType = "";
                        $this->InfoBokunData_model->Add();  
                    }
                }

                if (isset($activityDeatil->googlePlace)){
                    if (isset($activityDeatil->googlePlace->country)){
                        $this->InfoBokunData_model->ibd_ItemName = "country";  //
                        $this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->googlePlace->country;
                        $this->InfoBokunData_model->ibd_ItemType = "";
                        $this->InfoBokunData_model->Add();
                    }

                    if (isset($activityDeatil->googlePlace->city)){
                        $this->InfoBokunData_model->ibd_ItemName = "city";  //
                        $this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->googlePlace->city;
                        $this->InfoBokunData_model->ibd_ItemType = "";
                        $this->InfoBokunData_model->Add();
                    }

                }


                $this->InfoBokunData_model->ibd_ItemName = "durationText";  //
                $this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->durationText;
                $this->InfoBokunData_model->ibd_ItemType = "";
                $this->InfoBokunData_model->Add();



                $arrAgendaItems = $activityDeatil->agendaItems; //保存产品行程里面的派生图片
                if (is_array($arrAgendaItems)) {
                    foreach ($arrAgendaItems as $agendaItem) {
                        if (isset($agendaItem->keyPhoto)) {
                            if ($agendaItem->keyPhoto != null && $agendaItem->keyPhoto != "") {
                                $this->InfoBokunData_model->ibd_ItemName = "AgendaPhoto";
                                $this->InfoBokunData_model->ibd_Itemvalue = $agendaItem->keyPhoto->originalUrl;
                                if (isset($agendaItem->keyPhoto->description)) {
                                    $this->InfoBokunData_model->ibd_ItemDescription = $agendaItem->keyPhoto->description;
                                }
                                $this->InfoBokunData_model->ibd_ItemType = "originalUrl";
                                $this->InfoBokunData_model->Add();
                            }
                        }


                        $arrAgendaItemPhotos = $agendaItem->photos;
                        if (is_array($arrAgendaItemPhotos)) {
                            foreach ($arrAgendaItemPhotos as $agendaPhotoItem) {
                                $this->InfoBokunData_model->ibd_ItemName = "AgendaPhoto";
                                $this->InfoBokunData_model->ibd_Itemvalue = $agendaPhotoItem->originalUrl;
                                $this->InfoBokunData_model->ibd_ItemType = "originalUrl";
                                if (isset($agendaPhotoItem->description)) {
                                    $this->InfoBokunData_model->ibd_ItemDescription = $agendaPhotoItem->description;
                                }

                                $this->InfoBokunData_model->Add();
                            }
                        }
                    }
                }
            } else {
                $result["status"] = "error";
                echo json_encode($result);
            }

            //5. 返回成功信息
            $result["status"] = "ok";
            $result["ic_id"] = $ic_id;
            echo json_encode($result);
        }
    }

    /**
     * @description: 根据城市名称，获取对应的is_id，判断产品放到信息平台的位置
     * @param {type} 
     * @return: int is_id :城市所在信息平台的Id
     * @Date Changed: 
     */
    function get_isid_byCity($CityName)
    {
        $cityUrl = "/" . str_replace("'", "", str_replace(' ', '-', strtolower($CityName))) . "-" . "tours/";
        $row = $this->tpBokun_model->getInfomationAll($cityUrl);
        if (isset($row)) {
            $result = $row->is_id;
        } else {
            $result = 0; //$this->tpBokun_model->is_topId;
        }
        return $result;
    }

    /**
     * @description: 为了方便，首先循环添加所有的城市目录
     * @param {type} 
     * @return: 
     * @Date Changed: 
     */
    function checkAllCity()
    {
        $str_return = "";
        $result = $this->bokun_lib->get_all_product_list();
        //print_r($result);
        $result = json_decode($result);
        if (is_array($result) && count($result) > 0) {
            //productlist是所有产品，按城市分类，然后child里面是类别分类
            foreach ($result as $pItem) {
                $cityName = $pItem->title; //城市名称，用来判断信息平台是否有该城市
                if (isset($cityName)) {
                    $infoCity = json_decode($this->doInfoCity($cityName)); //获取导入的城市名称为父类，下面的产品在信息平台里面放在这个的下面
                    if ($infoCity->type == "new") {
                        $str_return .= $infoCity->cityname . ",";
                    }
                }
            }
        }
        if ($str_return != "") {
            echo "新城市添加：" . $str_return . "\r\n";
        } else {
            echo "没有新的城市" . "\r\n";
        }
    }


    /**
     * @description: 判断信息平台是否有该城市的记录，没有则添加。/beijing-tours/
     * @param {type} 
     * @return: 返回info的is_id ,作为父类ID
     * @Date Changed: 
     */
    function doInfoCity($CityName)
    {
        $cityUrl = "/" . str_replace("'", "", str_replace(' ', '-', strtolower($CityName))) . "-" . "tours/";
        $infoDetail = $this->tpBokun_model->getInfomationAll($cityUrl);
        if (isset($infoDetail)) {
            //如果存在
            $result["Id"] = $infoDetail->is_id;
            $result["cityname"] = $infoDetail->ic_title;
            $result["type"] = "old";
        } else {
            //如果不存在就录入
            $this->tpBokun_model->ic_title = ucwords($CityName) . " Tours";
            $this->tpBokun_model->ic_url_title = ucwords($CityName) . " Tours";
            $this->tpBokun_model->ic_url = $cityUrl;
            $tempData = json_decode($this->tpBokun_model->addInfo($this->tpBokun_model->is_topId));
            if (is_array($tempData)) {
                $result["Id"]  = $tempData[0]->value->is_id;
                $result["cityname"] = $CityName;
                $result["type"] = "new";
            } else {
                $result["Id"] = 0;
                $result["cityname"] = "";
                $result["type"] = "error";
            }
        }

        return json_encode($result);
    }


    /**
     * @description: 网前只抓取信息平台的信息。由于产品的翰特编码都是yong bk_activityid的规则组成，所以分开更新是可能的。这样可以保证不需要理会本地和网前的处理速度
     * @param sring $city : 城市名称 。用来单独更新某个城市
     * @return: 
     * @Date Changed: 
     */
    function onlyUpdateInfo($city = "")
    {
        //先进行对应城市的更新
        $this->checkAllCity();

        //循环接口开始
        $result = $this->bokun_lib->get_all_product_list();
        //print_r($result);
        $result = json_decode($result);
        if (is_array($result) && count($result) > 0) {
            //productlist是所有产品，按城市分类，然后child里面是类别分类
            foreach ($result as $pItem) {
                $cityName = str_replace("'", "", $pItem->title); //城市名称，用来判断信息平台是否有该城市
                if (isset($cityName)) {
                    if ($city != "" && strtolower($city) != strtolower($cityName)) {
                        continue;  //如果设置了指定抓取城市
                    } else {

                        $list_id = $pItem->id;
                        $pchild_Detail = $this->bokun_lib->get_product_list_detail($list_id);
                        $data_pchild_Detail = json_decode($pchild_Detail); //类别详细
                        foreach ($data_pchild_Detail->items as $pcdItem) { //循环类别
                            $activityData = $pcdItem->activity; //产品详细内容
                            //解析后获取需要的参数
                            $activityId = $activityData->id; //产品的activityId，可以获取对应的产品详细内容。 
                            //先判断是否是新产品
                            $checkNew = json_decode($this->tpBokun_model->checkActivityId($activityId));
                            if ($checkNew->return == "yes") {
                               continue;
                            }
                            //提交网前接口
                            $PAGCode = "bk_" . $activityId;
                            $checkCode = "cits"; //用来防止参数被破解
                            $PassParam["activityid"] = $activityId;
                            $PassParam["PAG_Code"] = $PAGCode;
                            $PassParam["CityName"] = $cityName;
                            $PassParam["checkCode"] = $checkCode;
                            $strPass = base64_encode(json_encode($PassParam));
                            $this->addActivity($strPass);
                            echo "\r\n";
                        }
                    }
                }
            }
            echo ("抓取完成！\r\n");
        } else {
            echo "无接口数据！\r\n";
        }
    }


    /** 读取旧网站的XML信息存入信息平台 */
    
    /**
     * @description:读取XML信息存入信息平台 
     * @param {type} $category:如果设置了类别，那么只保存该类别的信息。
     * @return: 
     * @Date Changed: 
     */
    function getOldXML($category=""){
       // $xmlParent = "278001169" ; //父类ID 本地
        $xmlParent = "278013201" ; //父类 网前
        //读取XML类型
        $appPath = $_SERVER['DOCUMENT_ROOT'];
        $XmlPath = $appPath."/application/third_party/tpmanage/post0702.xml";
        $xml = simplexml_load_file($XmlPath);
       // print_r($xml);
       $Items = $xml->channel->item;
       $i=0;

       //循环保存
       foreach ($Items as $xmlItem) {
        // if ($i==0){
        //     $content = $xmlItem->children("content",true);
        //     $content2 = (string)($content->encoded);
        //     print_r($content2);
        //     return;
        // }
        $title = $xmlItem->title;
        $link = str_replace("https://www.trippest.com","",$xmlItem->link);        
        $content = (string)$xmlItem->children("content",true)->encoded;       
        $content = str_replace("https://www.trippest.com/wp-content/uploads/","/images/uploads/",$content); //替换图片显示地址

        //入信息平台
        $title = str_replace("—","-",$title);
        $this->tpBokun_model->ic_title = $title;
        $this->tpBokun_model->ic_url_title = $title;
        $this->tpBokun_model->ic_seo_title = $title;
        $this->tpBokun_model->ic_url = $link;
        $this->tpBokun_model->ic_content = $content;
        $tempData = json_decode($this->tpBokun_model->addInfo($xmlParent));

        $i=$i+1;

       // print $i.":".$content ."\r\n";
       }

       echo "导入完成" ;
        //保存
    }


    /**  这里是在cht的信息平台运行的接口 */

    /**
     * @description: CH那边的产品生成接口数据
     * @param {type} 
     * @return: 
     * @Date Changed: 
     */
    function getChtTours($strcity = "")
    {
        if ($strcity == "") {
            $strcity = "beijing,guilin,chengdu,shanghai,xian,zhangjiajie,huangshan,yunnan";
        }
        $result = array();
        //城市解析
        $arrCity = explode(",", $strcity);
        $strParam = "";
        foreach ($arrCity as $city) {
            $strParam .= "'/day-tours/" . strtolower(str_replace(" ", "-", $city)) . "/',";
        }
        $strParam = trim($strParam, ",");
        //$strParam = "'/beijing/','/shanghai/'";   //本地测试
        //传递
        $list = $this->tpBokun_model->getChtTours($strParam);
        if ($list) {

            foreach ($list as $Item) {
                $data["city"] = $Item->city;
                $data["ic_id"] = $Item->ic_id;
                $data["ic_url"] = $Item->ic_url;
                $data["ic_title"] = $Item->ic_title;
                $data["ic_url_title"] = $Item->ic_url_title;
                $data["ic_content"] = $Item->ic_content;
                $data["ic_summary"] = $Item->ic_summary;
                $data["ic_seo_title"] = $Item->ic_seo_title;
                $data["ic_seo_description"] = $Item->ic_seo_description;
                $data["ic_seo_keywords"] = $Item->ic_seo_keywords;
                $data["ic_photo"] = $Item->ic_photo;


                //meta属性
                $listmeta = $this->tpBokun_model->getChtToursMeta($Item->ic_id);
                if ($listmeta) {
                    $arrmeta = array();
                    foreach ($listmeta as $meta) {
                        $MetaData["im_key"] = $meta->im_key;
                        $MetaData["im_value"] = $meta->im_value;
                        $arrmeta[] = $MetaData;
                    }
                    if ($arrmeta) {
                        $data["meta"] = $arrmeta;
                    }
                }

                $result[] = $data;
            }
        }
        if ($result) {
            echo json_encode($result);
        } else {
            echo json_encode(array("status" => "none", "param" => $strParam));
        }
    }


    function CurlCHTTours($city = "")
    {
        $url = "https://cht.mycht.cn/info.php/thirdparty/tpmanage/webgetbokun/getChtTours/";
        if ($city != "") {
            $url = $url . strtolower(str_replace(" ", "-", $city)) . "/";
        }
        $str =  $this->tpBokun_model->getCrulData($url); //抓取cht的数据
        $chtData = json_decode($str);
        if (is_array($chtData) && count($chtData) > 0) {
            //导入开始
            foreach ($chtData as $chtItem) {
                // echo ($chtItem->city.",".$chtItem->ic_title."\n\r");
                //判断重复
                if ($this->tpBokun_model->checkChtTours($chtItem->ic_url_title)) {
                    echo "已存在:" .$chtItem->ic_url_title. "\r\n";
                    continue;
                }

                //开始导入
                //1.根据城市名称获取对应的is_id作为 父类ID
                $chtCity =  str_replace("'", "", str_replace(' ', '-', strtolower($chtItem->city)));
                $is_id = $this->get_isid_byCity($chtCity);
                if ($is_id == 0) {
                    continue;  //找不到对应城市跳过
                }

                $ic_url = $chtItem->ic_url;   //根据原来的URL替换成新的URL
                $ic_url = str_replace("/day-tours/" . $chtCity . "/", "/" . $chtCity . "-tours/", $ic_url);
                $ic_url = str_replace(".htm", "/", $ic_url);

                $this->tpBokun_model->ic_url = $ic_url;

                $this->tpBokun_model->ic_url_title = $chtItem->ic_url_title;
                $this->tpBokun_model->ic_title = $chtItem->ic_title;
                $this->tpBokun_model->ic_content = $chtItem->ic_content;
                $this->tpBokun_model->ic_summary = $chtItem->ic_summary;
                $this->tpBokun_model->ic_seo_title = $chtItem->ic_seo_title;
                $this->tpBokun_model->ic_seo_description = $chtItem->ic_seo_description;
                $this->tpBokun_model->ic_seo_keywords = $chtItem->ic_seo_keywords;
                $this->tpBokun_model->ic_photo = $chtItem->ic_photo;
                $this->tpBokun_model->ic_status = 1;

                //保存信息表
                $tempData = json_decode($this->tpBokun_model->addInfo($is_id));
                if (is_array($tempData)) {
                    $ic_id = $tempData[0]->value->ic_id;
                } else {
                    $result["status"] = "error";
                    echo json_encode($result);
                    continue;
                }

                //保存meta表
                if ($ic_id > 0) {
                    $metalist = $chtItem->meta;
                    if ($metalist) {
                        foreach ($metalist as $metaItem) {
                            $this->tpBokun_model->addMeta($ic_id, $metaItem->im_key, $metaItem->im_value);  //循环导入原来的meta
                        }
                    }
                }

                $result["status"] = "ok";
                $result["ic_id"] = $ic_id;
                echo json_encode($result);
                echo "\r\n";
            }
            echo "数据导入完成 \r\n";
        } else {
            echo "接口数据读取错误！";
        }

        //print_r(json_decode($str));
        // echo $str;
    }
}

/* End of file webgetbokun.php */
