<?php
/**
 * Created by PhpStorm.
 * User: chendeben
 * Date: 2017/12/6
 * Time: 11:50
 */
namespace PhpLife\App\Api\Controller;

use PhpLife\App\Api\Package\Database\File;
use PhpLife\App\Api\Package\Database\Hash;
use PhpLife\App\Api\Controller;
use PhpLife\Library\Sphinx;

class Search extends Controller
{
    public function  main(){
        $keyword=$this->getRequest()->string("key");
        $p=$this->getRequest()->intval("page",1);
        $count=10;
        $res=Sphinx::instance()->query($keyword, ($p-1)*$count, $count);
        $ids=array_keys($res['matches']);
        $result=Hash::select(array(),array('id'=>$ids));
        foreach ($result as &$file) {
            $temp=File::getOne(array('info_hash'=>$file['info_hash']));
            if ($temp){
                $temp=json_decode($temp['file_list'],true);
                $new=array();
                foreach ($temp as $key=>$value){
                    if (strpos($value['path'], "_____")===false){
                        $new[]=$value;
                    }
                }
                $file['file_list']=$new;
            }else{
                $file['file_list']=array(array('path'=>$file['name'],'length'=>$file['length']));
            }
        }
        echo json_encode($result);
    }
}