<?php
require_once ('config.php');
use QL\QueryList;
use QL\Ext\PhantomJs;
$url = 'https://www.hsbianma.com';
$ql = QueryList::getInstance();
$ql ->use(PhantomJs::class,'C:/soft/phantomjs-2.1.1-windows/bin/phantomjs.exe','browser');
// $ql->browser("https://www.hsbianma.com/");
// //所有分类的连接，含有空的连接
// $link = $ql->find("ul.jqtree-tree li.jqtree_common")->map(function($item){
//     return $item->find('a:eq(0)')->attr('href');
// });
 $fileName = 'all_fenlei.txt';
// foreach ($link as $l){
//     if(!empty($l)){
//         file_put_contents($fileName,$l.PHP_EOL,FILE_APPEND);
//     }
// }
// $ql->destruct();
// die();
//分类列表第一页
$allFenLei = file($fileName);
// print_r($allFenLei);
// $fenLeiList = $ql->browser('https://www.hsbianma.com/search?keywords=01');
// $pageList = $fenLeiList->find("#pagination a");
//
// $allPage = $pageList->map(function($item){
//     if(is_numeric($item->text())){
//         return $item->attr('href');
//     }
// });
// var_dump($allPage);
// die();
// die();
$allPageFile = 'all_page_list.txt';
//从297开始跑，58页
$allPageFile = 'all_page_list_chunk.txt';
// foreach ($allFenLei as $key =>$line){
//     echo $key;
//     echo PHP_EOL;
//     $catchUrl=$url.$line;
//     $catchUrl = trim($catchUrl);
//     $fenLeiList = $ql->browser($catchUrl);
//     $pageList = $fenLeiList->find("#pagination a");
//     $allPage = $pageList->map(function($item){
//         if(is_numeric($item->text()) || $item->text() == '上一页'){
//          return $item->attr('href');
//         }
//     });
//     $ql->destruct();
//    foreach ($allPage as $page){
//        if(!empty($page)) {
//            file_put_contents($allPageFile,$url.$page.PHP_EOL,FILE_APPEND);
//        }
//    }
// }

//分类海关编码总页数
$allPage = file($allPageFile);
foreach($allPage as $key=>$pageUrl){
    echo $key;
    echo PHP_EOL;
    echo $pageUrl;
    echo PHP_EOL;
    $pageUrl = trim($pageUrl);
    $fenLeiList=$ql->browser($pageUrl);
    $hsCodeLink = $fenLeiList->find(".result-grid ")->map(function($tr){
        return $tr->find("td:last")->find("a")->attr('href');
    });
    $ql->destruct();
    foreach($hsCodeLink as $link){
        $hscodeView = $ql->browser($url.$link);
        //获取详情页面
        try {
            $hscodeTax = $hscodeView->find(".cbox>table:eq(1)")->find("tr")->map(function ($item) {
                return array(
                    'tax_name' => $item->find("td:eq(0)")->text(),
                    'tax_value' => $item->find("td:eq(1)")->text()
                );
            });
            $hsCode = $hscodeView->find(".cbox>table>tbody>tr>td:eq(1)")->text();
            foreach ($hscodeTax as $taxInfo) {
                Service_HsCodeTax::add(array(
                    'hs_code' => $hsCode,
                    'tax_name' => $taxInfo['tax_name'],
                    'tax_value' => $taxInfo['tax_value']
                ));
            }
        }catch (Exception $e){
            echo $url.$link;
        }
    }


}

print_r($hsCode);
