SearchData.php 24 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
<?php
namespace LibModels\Web\Product;

use Api\Yohobuy;
use Api\Sign;

class SearchData
{
    //获取list页banner
    const URI_INDEX_BANNER = '/shops/service/v1/searchbanner';
    //获取list页广告
    const URL_INDEX_AD = 'shops/api/v1/ads/getList';
    
    /**
     * 获取搜索的服务地址
     * 备注:此处是根据环境来确定使用阿里云内网还是外网的URL
     * @return string
     * @param string $type
     * @return string
     */
    public static function getUrl($type = '')
    {
        defined('APPLICATION_ENV') || define('APPLICATION_ENV', 'developer');

        switch (APPLICATION_ENV) {
            case 'production':
                if ($type == 'sort') {
                    return 'http://search.yohoops.org/yohosearch/sortgroup.json';
//                    return 'http://101.200.31.165/yohosearch/sortgroup.json';
                }
                elseif ($type == 'discount') {
                    return 'http://search.yohoops.org/yohosearch/discount.json';
//                    return 'http://101.200.31.165/yohosearch/discount.json';
                }
                elseif ($type == 'recent') {
                    return 'http://search.yohoops.org/yohosearch/recent.json';
//                    return 'http://101.200.31.165/yohosearch/recent.json';
                }
                elseif ($type == 'suggest') {
                    return 'http://search.yohoops.org/yohosearch/suggest.json';
//                    return 'http://101.200.31.165/yohosearch/suggest.json';
                }
                else if($type == 'new-shelve') {
                    return 'http://search.yohoops.org/yohosearch/new-shelve.json';
//                    return 'http://101.200.31.165/yohosearch/new-shelve.json';
                }
                elseif ($type == 'shop') {
                    return 'http://search.yohoops.org/yohosearch/shops.json';
//                    return 'http://101.200.31.165/yohosearch/shops.json';
                }
                elseif ($type == 'brand') {
                    return 'http://search.yohoops.org/yohosearch/brand/list.json';
//                    return 'http://101.200.31.165/yohosearch/brand/list.json';
                }
                return 'http://search.yohoops.org/yohosearch/search.json';
//                return 'http://101.200.31.165/yohosearch/search.json';

            case 'preview':
                if ($type == 'sort') {
                    return 'http://192.168.10.64:8080/yohosearch/sortgroup.json';
                }
                elseif ($type == 'discount') {
                    return 'http://192.168.10.64:8080/yohosearch/discount.json';
                }
                elseif ($type == 'recent') {
                    return 'http://192.168.10.64:8080/yohosearch/recent.json';
                }
                elseif ($type == 'suggest') {
                    return 'http://192.168.10.64:8080/yohosearch/suggest.json';
                }
                else if($type == 'new-shelve') {
                    return 'http://192.168.10.64:8080/yohosearch/new-shelve.json';
                }
                elseif ($type == 'shop') {
                    return 'http://192.168.10.64:8080/yohosearch/shops.json';
                }
                elseif ($type == 'brand') {
                    return 'http://192.168.10.64:8080/yohosearch/brand/list.json';
                }
                return 'http://192.168.102.216:8080/yohosearch/search.json';
            case 'testing':
                if ($type == 'sort') {
                    return 'http://testing.yohoops.org/yohosearch/sortgroup.json';
                }
                elseif ($type == 'discount') {
                    return 'http://testing.yohoops.org/yohosearch/discount.json';
                }
                elseif ($type == 'recent') {
                    return 'http://testing.yohoops.org/yohosearch/recent.json';
                }
                elseif ($type == 'suggest') {
                    return 'http://testing.yohoops.org/yohosearch/suggest.json';
                }
                else if($type == 'new-shelve') {
                    return  'http://testing.yohoops.org/yohosearch/new-shelve.json';
                }
                elseif ($type == 'shop') {
                    return 'http://testing.yohoops.org/yohosearch/shops.json';
                }
                elseif ($type == 'search') {
                    return 'http://testing.yohoops.org/yohosearch/search.json';
                }
                elseif ($type == 'brand') {
                    return 'http://testing.yohoops.org/yohosearch/brand/list.json';
                }
                return 'http://testing.yohoops.org/yohosearch/search.json';
            case 'developer':
            default:
                if ($type == 'sort') {
                    return 'http://192.168.102.216:8080/yohosearch/sortgroup.json';
                }
                elseif ($type == 'discount') {
                    return 'http://192.168.102.216:8080/yohosearch/discount.json';
                }
                elseif ($type == 'recent') {
                    return 'http://192.168.102.216:8080/yohosearch/recent.json';
                }
                elseif ($type == 'suggest') {
                    return 'http://192.168.102.216:8080/yohosearch/suggest.json';
                }
                else if($type == 'new-shelve') {
                    return  'http://192.168.102.216:8080/yohosearch/new-shelve.json';
                }
                elseif ($type == 'shop') {
                    return 'http://192.168.102.216:8080/yohosearch/shops.json';
                }
                elseif ($type == 'search') {
                    return 'http://192.168.102.216:8080/yohosearch/search.json';
                }
                elseif ($type == 'brand') {
                    return 'http://192.168.102.216:8080/yohosearch/brand/list.json';
                }
                return 'http://192.168.102.216:8080/yohosearch/search.json';
        }
    }

    /**
     * 根据给定查询数据搜索数据列表 (新的)
     *
     * @param  string  $query  查询条件, 默认为null
     * @param  string  $brand  品牌,默认为null
     * @param  string  $gender 性别,默认为null,"1,3"表示男, "2,3"表示女, "1,2,3"表示全部
     * @param  integer $color  颜色id
     * @param  integer $size   尺码id
     * @param  integer $price  价格
     * @param  string  $p_d    折扣,默认为null
     * @param  string  $sort  商品所属品类,默认为null
     * @param  string  $order  排序方式,默认为按照时间倒序排列s_t_desc,
     *                        	s_t_asc表示按时间正序排列,
     *                         	s_p_asc表示按价格正序排列,
     *                          s_p_desc表示按价格倒序排列,
     *                          p_d_asc表示按折扣正序排列,
     *                          p_d_desc表示按折扣倒序排列
     * @param  integer $page   指定查询是多少页,默认为第一页
     * @param  integer $limit  指定查询多少个,默认是60个
     * @param  integer $channel表示哪个频道,1表示男生,2表示女生,3表示潮童,4表示创意生活
     * @return array           搜索到的数据
     */
    public static function searchElasticByCondition($condition, $cache = false)
    {
        // 排序数据映射表
        $orderMaps = array(
            's_t_desc' => 'shelve_time:desc',
            's_t_asc' => 'shelve_time:asc',
            's_p_asc' => 'sales_price:asc',
            's_p_desc' => 'sales_price:desc',
            'p_d_desc' => 'discount:desc',
            'p_d_asc' => 'discount:asc',
            'skn_desc' => 'product_skn:desc',
            'skn_asc' => 'product_skn:asc',
            'activities_desc' => 'activities.order_by:desc',
            'activities_asc' => 'activities.order_by:asc',
            's_n_asc' => 'sales_num:asc',
            's_n_desc' => 'sales_num:desc',
            'activities_id_desc' => 'activities.activity_id:desc',
            'activities_id_asc' => 'activities.activity_id:asc',
        );

        $param = array();
        $param['status'] = 1; // 是否上架,1表示在架,2表示不在
        $param['sales'] = 'Y'; // 只搜索销售的产品
        $param['stocknumber'] = 1; // 过滤掉已售罄的商品
        $param['attribute_not'] = '2'; //过滤掉赠品
        // $param['needFilter'] = 1; // 是否需要返回筛选条件
        if (!isset($condition['order'])) {
            $param['order'] = $orderMaps['s_t_desc'];
        } else {
            $param['order'] = $orderMaps[$condition['order']];
        }
        if (!isset($condition['page'])) {
            $param['page'] = 1;
        }

        if(isset($condition['viewNum'])) {
            $param['viewNum'] = $condition['viewNum'];
        }  else if (!isset($condition['limit'])) {
            $param['viewNum'] = 60;
        } else {
            $param['viewNum'] = $condition['limit'];
            unset($condition['limit']);
        }
        if (!empty($condition)) {
            $param += $condition;
        }

        return Yohobuy::get(self::getUrl('search'), $param, $cache);
    }
    
    /**
     * 根据分类列表获取商品信息
     *
     * @param array $param
     * @param array $sortList
     * @return array
     */
    public static function getSearchDataBySort(array $param, array $sortList)
    {
//        $data = array();
//        foreach ($sortList as $v) {
//            if(empty($v['viewNum'])){
//                continue;
//            }
//            $searchParams = array_merge($params, $v);
//            $list = self::searchElasticByCondition($searchParams, true);
//            $productList = empty($list['data']['product_list']) ? array() : $list['data']['product_list'];
//            if(count($productList) < $v['viewNum']){
//                continue;
//            }
//            $data = array_merge($data, $productList);
//        }
//        return $data;
        
        /* code review by fei.hong: 2016/01/26 优化合并misort参数, 减少调用搜索接口的次数 */      
        $data = array();
        
        //按照时间降序排的参数
        $param['order'] = 'shelve_time:desc';
        $param['status'] = 1; // 是否上架,1表示在架,2表示不在
        $param['sales'] = 'Y'; // 只搜索销售的产品
        $param['attribute_not'] = 2; //过滤掉赠品
        $param['stocknumber'] = 3; // 过滤掉即将售罄的商品
        // 获取新品的接口URL,以数组列表返回
        $urlList = self::getNewArrivalUrl($sortList, $param);
        // 并行调用搜索新品接口
        $searchd = Yohobuy::getMulti($urlList);

        // 该变量用于记录数组最后一个值的索引
        $lastIndex = 0;
        // 业务逻辑:按照指定品类排序显示
        foreach ($sortList as $key => $value) {
            $lastIndex = $value['viewNum'] - 1;
            // 业务逻辑:一个品类不满指定个数时,前台不显示
            if (!empty($searchd[$key])) {
                if(isset($searchd[$key]['product_list'][$lastIndex])) {//search
                    $data = array_merge($data, $searchd[$key]['product_list']);
                }
                else if(isset($searchd[$key][$lastIndex])) {//new-shelve
                    $data = array_merge($data, $searchd[$key]);
                }
            }
        }
        // 用完清空不使用的变量
        $searchd = array();
        
        return $data;
    }
    
    /**
     * 获取推荐新品的接口URL列表
     * 
     * @param array $sortList 分类列表
     * @param array $params 附加参数
     * @return arary
     */
    public static function getNewArrivalUrl($sortList, $params)
    {
        $urlList = array();
        $option = array();
        foreach ($sortList as $key => $value) {
            $option = $params + $value;
            $urlList[$key] = Yohobuy::httpBuildQuery(self::getUrl('new-shelve'), $option);
        }
        return $urlList;
    }

    /**
     * 根据查询条件查询品类列表
     * 
     * @param  string  $query  关键字
     * @param  bool  $needAllSort 为1时返回全部分类
     * @param  bool  needSmallSort 为1时返回小分类
     * @param  string  $brand  品牌,查多个品牌以逗号分隔
     * @param  string  $mosort 产品产品大分类ID,查多个大类以逗号分隔
     * @param  string  $misort 产品产品中分类ID,查多个中类以逗号分隔
     * @param  string  $sort 产品产品小分类ID,查多个小类以逗号分隔
     * @param  string  $gender 性别,默认为null,"1"表示男, "2"表示女, "3"表示通用
     * @param  integer $color  颜色id
     * @param  integer $size   尺码id
     * @param  integer $price  价格
     * @param  integer $style  风格ID
     * @param  integer $parameter_xxx 属性类型ID与值ID 参数名称格式:parameter_{属性类型ID}={属性值ID},如查询厚度为中“parameter_875=4801”
     * @param  integer $stocknumber 库存量
     * @param  integer $shelve_time 时间区间
     * @param  integer $specialoffer 特价类型
     * @param  bool $sales 是否销售
     * @param  bool $promotion 是否为促销品
     * @param  integer $vdt 是否为促销品
     * @param  bool $vdt 折扣类型
     * @param  integer $p_d 促销浮点值
     * @param  integer $attribute  商品属性  1正常商品 2赠品
     * @param  integer $limited 是否限量 “N”或“Y”
     * @param  integer $new  是否新品 “N”或“Y”
     * @param  integer $status 是否上架
     * @param  integer $sell_channels 销售平台
     * @param  integer $p_d_int  促销折扣 int型如7
     * @param  integer $act_temp 活动模板
     * @param  integer $act_rec    活动模板是否推荐
     * @param  integer $act_status    活动模板商品状态
     * @param  integer $attribute_not    过滤商品属性,attribute_not=2过滤掉赠品
     * @param  integer $not_*    not_字段名,过滤字段
     * @return array           搜索到的数据
     */
    public static function getClassesData($classes = array(), $cache = false)
    {
        $classes['sales'] = 'Y';  //在销售商品分类
        $classes['status'] = 1;   //上架商品分类
        $classes['stocknumber'] = 1;    //过滤掉已售罄
        return Yohobuy::get(self::getUrl('sort'),$classes, $cache);
    }

    /**
     * 获取搜索提示
     * @param $param
     * @param $cache
     * @return array
     */
    public static function getSuggest($param, $cache = false)
    {
        if (empty($param['query'])) {
            return array();
        }
        if (empty($param['size'])) {
            $param['size'] = 10;
        }
        return Yohobuy::get(self::getUrl('suggest'),$param, $cache);
    }

    /**
     * 并行调接口url获取(搜索产品数据)
     * @param $condition
     * @param $type (接口类型)
     * @return string
     */
    public static function getProductUrl($condition, $type = '')
    {
        // 排序数据映射表
        $orderMaps = array(
            's_t_desc' => 'shelve_time:desc',
            's_t_asc' => 'shelve_time:asc',
            's_p_asc' => 'sales_price:asc',
            's_p_desc' => 'sales_price:desc',
            'p_d_desc' => 'discount:desc',
            'p_d_asc' => 'discount:asc',
            'skn_desc' => 'product_skn:desc',
            'skn_asc' => 'product_skn:asc',
            'activities_desc' => 'activities.order_by:desc',
            'activities_asc' => 'activities.order_by:asc',
            's_n_asc' => 'sales_num:asc',
            's_n_desc' => 'sales_num:desc',
            'activities_id_desc' => 'activities.activity_id:desc',
            'activities_id_asc' => 'activities.activity_id:asc',
            'brand_desc' => 'brand_weight:desc'
        );
        $param = array();
        $param['status'] = 1; // 是否上架,1表示在架,2表示不在
        $param['sales'] = 'Y'; // 只搜索销售的产品
        $param['stocknumber'] = 1; // 过滤掉已售罄的商品
        $param['attribute_not'] = 2; //过滤掉赠品
        if (!isset($condition['order'])) {
            $param['order'] = $orderMaps['s_t_desc'];
        } else {
            $param['order'] = isset($orderMaps[$condition['order']]) ? $orderMaps[$condition['order']] : '';
        }
        if (!isset($condition['page'])) {
            $param['page'] = 1;
        }

        if(isset($condition['viewNum'])) {
            $param['viewNum'] = $condition['viewNum'];
        }  else if (!isset($condition['limit'])) {
            $param['viewNum'] = 60;
        } else {
            $param['viewNum'] = $condition['limit'];
            unset($condition['limit']);
        }
        if (!empty($condition)) {
            $param += $condition;
        }
        return Yohobuy::httpBuildQuery(SearchData::getUrl($type), $param);
    }

    /**
     * @param $condition
     * @return string
     */
    public static function getClassesUrl($condition = array())
    {
        $condition['sales'] = 'Y';  //在销售商品分类
        $condition['status'] = 1;   //上架商品分类
        $condition['stocknumber'] = 1;    //过滤掉已售罄
        return Yohobuy::httpBuildQuery(SearchData::getUrl('sort'), $condition);
    }

    /**
     * 并行调接口url获取(获取品牌数据)
     */
    public static function getBrandUrl($customOptions = array())
    {
        // 构建必传参数
        $param = Yohobuy::param();
        $param['brand_id'] = $customOptions['brandId'];
        $param['uid'] = $customOptions['uid'];
        $param['method'] = 'app.brand.getBrandIntro';
        $param['client_secret'] = Sign::getSign($param);

        return Yohobuy::httpBuildQuery(Yohobuy::API_URL, $param);
    }
    
    /**
     * 获取品牌店铺接口地址
     * @return string
     */
    public static function getBannerData($positionId, $msort, $misort, $gender)
    {
        // 构建必传参数
        $param = Yohobuy::param();
        $param['method'] = 'web.search.banner';
        $param['position_id'] = intval($positionId);
        $param['max_sort_id'] = intval($msort);
        $param['middle_sort_id'] = intval($misort);
        $param['gender'] = $gender;
        $param['brand'] = '0';
        $param['style'] = '0';
        $param['color'] = '0';
        $param['price'] = '0';
        $param['size_id'] = '0';
        $param['client_secret'] = Sign::getSign($param);
        return Yohobuy::get(Yohobuy::API_URL, $param, 600, false, 5);
    }
    
    /**
     * 并行调接口url获取(获取折扣区间)
     * @param array $param
     * @return string
     */
    public static function getDiscountUrl($param = array())
    {
        return Yohobuy::httpBuildQuery(SearchData::getUrl('discount'), $param);
    }

    /**
     * 并行调接口url获取(获取最新上架)
     */
    public static function getRecentShelveUrl($param = array())
    {
        return Yohobuy::httpBuildQuery(SearchData::getUrl('recent'), $param);
    }

    /**
     * 获取品牌店铺接口地址
     * @param $param
     * @return string
     */
    public static function getShopUrl($param)
    {
        return Yohobuy::httpBuildQuery(SearchData::getUrl('shop'), $param);
    }
    /**
     * 获取品牌列表接口地址
     * @param $param
     * @return string
     */
    public static function getBrandListUrl($param = array())
    {
        return Yohobuy::httpBuildQuery(SearchData::getUrl('brand'), $param);
    }
    
    /**
     * 获取list页广告数据
     * @param $param
     * @return string
     */
    public static function getListAd($msort, $misort, $positionId)
    {
        $param = array(
            'position_id' => $positionId,
            'max_sort_id' => $msort,
            'middle_sort_id' => $misort
        );
        
        return Yohobuy::get(Yohobuy::SERVICE_URL . self::URL_INDEX_AD, $param, 600, false, 1); // 有缓存10分钟
    }
    
    
    /**
     * 模糊搜索提供的关键词
     * 
     * @param  string $keyword 关键词
     * @return array          根据给定关键词搜索到的结果,包括数据数目count和提供的关键词keyword
     */
    public static function searchFuzzyDatas($keyword)
    {
        // 构建必传参数
        $param = Yohobuy::param();

        $param['keyword'] = $keyword;
        $param['method'] = 'app.search.fuzzy';
        $param['client_secret'] = Sign::getSign($param);

        return Yohobuy::get(Yohobuy::API_URL, $param);
    }

    /**
     * 根据给定查询数据搜索数据列表  (老的)
     * 
     * @param  string  $query  查询条件, 默认为null
     * @param  string  $brand  品牌,默认为null
     * @param  string  $gender 性别,默认为null,"1,3"表示男, "2,3"表示女, "1,2,3"表示全部
     * @param  integer $color  颜色id
     * @param  integer $size   尺码id
     * @param  integer $price  价格
     * @param  string  $p_d    折扣,默认为null
     * @param  string  $sort  商品所属品类,默认为null
     * @param  string  $order  排序方式,默认为按照时间倒序排列s_t_desc,
     *                        	s_t_asc表示按时间正序排列,
     *                         	s_p_asc表示按价格正序排列,
     *                          s_p_desc表示按价格倒序排列,
     *                          p_d_asc表示按折扣正序排列,
     *                          p_d_desc表示按折扣倒序排列
     * @param  integer $page   指定查询是多少页,默认为第一页
     * @param  integer $limit  指定查询多少个,默认是60个
     * @param  integer $channel表示哪个频道,1表示男生,2表示女生,3表示潮童,4表示创意生活
     * @return array           搜索到的数据
     */
    public static function searchByCondition($condition)
    {
        $param = Yohobuy::param();
        $param['method'] = 'app.search.li';
        if (!isset($condition['order'])) {
            $param['order'] = 's_t_desc';
        }
        if (!isset($condition['page'])) {
            $param['page'] = 1;
        }
        if (!isset($condition['limit'])) {
            $param['limit'] = 60;
        }
        if (!empty($condition)) {
            $param += $condition;
        }
        $param['client_secret'] = Sign::getSign($param);

        return Yohobuy::get(Yohobuy::API_URL, $param);
    }
    
    /**
     * search获取所有品牌list
     * @return type
     */
    public static function getBrandData()
    {
        return Yohobuy::get(self::getUrl('brand'),array(), 3600);
    }
    
    /**
     * 获取条件获取分类
     * 
     * @param array $condition [ msort: 产品大分类ID, misort:产品中分类ID, sort: 小分类ID]
     * @param boolean $isOnlyUrl
     * @return mixed
     */
    public static function getSortByCondition(array $condition, $isOnlyUrl = false) {
        $result = array();
        if(!empty($condition)) {
            if($isOnlyUrl) {
                return Yohobuy::httpBuildQuery(self::getUrl('sort'), $condition);
            }
            $result =  Yohobuy::get(self::getUrl('sort'), $condition);
        }
        return $result;
    }
    
    public static function searchAll($param = array())
    {
        return Yohobuy::get(self::getUrl(), $param);
    }
    /**
     * 根据品牌id查询店铺列表接口
     * @param type array $param
     * @return type []
     */
    public static function getQueryShopsByBrandId($brandId)
    {
        $param = Yohobuy::param();
        $param['method'] = 'app.shop.queryShopsByBrandId';
        $param['brand_id'] = $brandId;
        $param['client_secret'] = Sign::getSign($param);

        return Yohobuy::get(Yohobuy::API_URL, $param);
    }
}