Browse Source

add bonus goods random

stanley-king 7 years atrás
parent
commit
4e6ca4eac9

+ 2 - 0
centra_srv.php

@@ -9,7 +9,9 @@ require_once (BASE_ROOT_PATH . '/helper/search/event_handler.php');
 require_once (BASE_ROOT_PATH . '/helper/search/util.php');
 require_once (BASE_ROOT_PATH . '/helper/category_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/brand_helper.php');
+require_once (BASE_ROOT_PATH . '/helper/goods/commonid_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/search/searcher.php');
+require_once (BASE_ROOT_PATH . '/helper/special_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/search/area_library.php');
 require_once (BASE_ROOT_PATH . '/helper/brand_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/algorithm.php');

+ 6 - 0
helper/search/processor.php

@@ -21,6 +21,7 @@ class processor implements IProcessor
     const GetRelatedWord = 1;
     const SearchReasult  = 2;
     const MatchPrice     = 3;
+    const PromoteGoods   = 4;
 
     const ValidateArea   = 20;
 
@@ -50,6 +51,11 @@ class processor implements IProcessor
             $result = searcher::instance()->match_price($params);
             return serialize($result);
         }
+        elseif($type == self::PromoteGoods) {
+            $params = $body['params'];
+            $result = searcher::instance()->promote_goods($params);
+            return serialize($result);
+        }
         elseif ($type == self::ValidateArea) {
             $params = $body['params'];
             $area_id = $params['area_id'];

+ 68 - 0
helper/search/searcher.php

@@ -12,6 +12,8 @@ use algorithm;
 use brand_helper;
 use category_helper;
 use Log;
+use special_manager;
+use commonid_helper;
 
 function mb_str_split( $string ) {
     return preg_split('/(?<!^)(?!$)/u', $string );
@@ -303,6 +305,7 @@ class searcher
 
     private $goods_info;
     private $price_cids;
+    private $promot_cids;
 
     private static $stInstance;
 
@@ -340,6 +343,7 @@ class searcher
         $this->goods_dict = new goods_dict();
         $this->goods_info = new goods_info();
         $this->price_cids = new valtokey();
+        $this->promot_cids = [];
 
         $mod_goods = Model('goods');
         global $config;
@@ -377,6 +381,30 @@ class searcher
             $this->price_cids->add($common_id,intval($item['goods_price'] * 100 + 0.5));
         }
         $this->price_cids->finish();
+        $this->read_promotion();
+    }
+
+    private function read_promotion()
+    {
+        global $config;
+        $special = $config['bonus_match_goods'];
+        if(empty($special)) return false;
+        $special_id = intval($special['special_id']);
+        if($special_id <= 0) return false;
+
+        special_manager::instance()->special($special_id,$goods_ids);
+        if(empty($goods_ids)) return false;
+
+        $cids = [];
+        foreach ($goods_ids as $gid) {
+            $cid = commonid_helper::instance()->common_id($gid);
+            if($cid != false) {
+                $cids[] = $cid;
+            }
+        }
+        sort($cids);
+        $this->promot_cids = $cids;
+        return true;
     }
 
     private function get_name($dict, $words)
@@ -697,6 +725,46 @@ class searcher
         return $result;
     }
 
+    public function promote_goods($params)
+    {
+        $price = $params['price'];
+        $count   = intval($params['count']);
+
+        $items = $this->price_cids->findall(intval($price * 100 + 0.5));
+        sort($items);
+
+        $match_cids = $items;
+        $pro_cids = algorithm::set_intersection($match_cids,$this->promot_cids);
+        if(count($pro_cids) < $count) {
+            $cids = $this->random_goods($match_cids,$count);
+        } else {
+            $cids = $this->random_goods($pro_cids,$count);
+        }
+
+        return ['total' => 1,'cids' => $cids,'total' => count($cids)];
+    }
+
+    private function random_goods($cids,$count)
+    {
+        $length = count($cids);
+        if($length <= $count) return $cids;
+
+        $result = [];
+        for ($i = 0; $i < $count;)
+        {
+            $pos = mt_rand(0,$length - 1);
+            $val = $cids[$pos];
+
+            if(!in_array($val,$result)) {
+                $result[] = $val;
+                $i++;
+            }
+        }
+
+        return $result;
+    }
+
+
     private function get_ids($dict, $words)
     {
         $i = 0;

+ 6 - 1
helper/search/tcp_client.php

@@ -15,11 +15,11 @@ class tcp_client
     const GetRelatedWord = 1;
     const SearchReasult  = 2;
     const MatchPrice     = 3;
+    const PromoteGoods   = 4;
 
     const ValidateArea   = 20;
 
     const time_out = 3600;
-
     const body_header_len = 10;
 
     private static $stInstance;
@@ -59,6 +59,11 @@ class tcp_client
         $param = array("type" => self::MatchPrice, "params" => $params);
         return $this->request($param);
     }
+    public function promote_goods($params)
+    {
+        $param = array("type" => self::PromoteGoods, "params" => $params);
+        return $this->request($param);
+    }
 
     public function get_area($area_id)
     {

+ 25 - 0
helper/search/util.php

@@ -289,4 +289,29 @@ class valtokey
 
         return array('total' => $pos + 1,'cids' => $result);
     }
+
+    public function findall($val)
+    {
+        $pos = algorithm::upper_bound($this->mValMap,$val);
+        if($pos < 0) {
+            return false;
+        }
+        elseif ($pos >= $this->mCount) {
+            $pos = $this->mCount - 1;
+        }
+        else {
+            $data = $this->mValMap[$pos];
+            if($data > $val) {
+                $pos -= 1;
+            }
+        }
+
+
+        $result = [];
+        for ($i = $pos; $i >= 0; $i--) {
+            $result[] = $this->mKeys[$i];
+        }
+
+        return $result;
+    }
 }

+ 2 - 2
mobile/control/bonusex.php

@@ -503,8 +503,8 @@ class bonusexControl extends mobileControl
                 return false;
             }
 
-            $params = ['price' => $price,'page_no' => 1,'page_size' => 10];
-            $result = search\tcp_client::instance()->match_price($params);
+            $params = ['price' => $price,'count' => 10];
+            $result = search\tcp_client::instance()->promote_goods($params);
 
             $cids = $result['cids'];
             $model_goods = Model('goods');