searcher.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2016/10/7
  6. * Time: 下午12:44
  7. */
  8. namespace search;
  9. use algorithm;
  10. use brand_helper;
  11. use category_helper;
  12. use Log;
  13. use special_manager;
  14. use commonid_helper;
  15. class brand_dict extends indexer
  16. {
  17. private $mBrandCommon;
  18. private $mBrandHots;
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->mBrandCommon = new one_multi();
  23. $this->mBrandHots = new one_multi();
  24. $this->init();
  25. }
  26. private function init()
  27. {
  28. $brands_info = brand_helper::instance()->brandex(); //todo 确认一下
  29. $brands = $brands_info['brands'];
  30. foreach ($brands as $item)
  31. {
  32. $name = $item['brand_name'];
  33. $bid = intval($item['brand_id']);
  34. $this->parase($name,$bid);
  35. }
  36. }
  37. public function commons($brand_id)
  38. {
  39. return $this->mBrandCommon->get($brand_id);
  40. }
  41. public function hots($brand_id) {
  42. return $this->mBrandHots->get($brand_id);
  43. }
  44. public function add_brand($brand_id, $common_id, $hot_id)
  45. {
  46. $this->mBrandCommon->add($brand_id,$common_id);
  47. $this->mBrandHots->add($brand_id,$hot_id);
  48. }
  49. }
  50. class category_dict extends indexer
  51. {
  52. private $mGcHot; //一个GC对应一个HotID
  53. private $mHoTree; //HOT的父子关系
  54. private $mHotBrand;
  55. private $mHotCommon;
  56. public function __construct()
  57. {
  58. parent::__construct();
  59. $this->mGcHot = new one_one();
  60. $this->mHoTree = new parent_sub_tree();
  61. $this->mHotBrand = new one_multi();
  62. $this->mHotCommon = new one_multi();
  63. $this->init();
  64. }
  65. public function init()
  66. {
  67. $cats = category_helper::instance()->categories();
  68. foreach ($cats as $item)
  69. {
  70. $hot_id = intval($item['hot_id']);
  71. $this->mHoTree->add($hot_id,0);
  72. $name = $item['name'];
  73. $this->parase($name,$hot_id);
  74. foreach($item['subitem'] as $sub_item) {
  75. $subhot_id = intval($sub_item['hot_id']);
  76. $this->mHoTree->add($subhot_id,$hot_id);
  77. $cids = category_helper::instance()->cids($subhot_id);
  78. $this->add_gc_hot($cids,$subhot_id);
  79. $subname = $sub_item['name'];
  80. $this->parase($subname,$subhot_id);
  81. }
  82. }
  83. }
  84. public function brands($hotid)
  85. {
  86. if($this->mHoTree->is_parent($hotid) == false) {
  87. return $this->mHotBrand->get($hotid);
  88. }
  89. else
  90. {
  91. $sub_hots = $this->mHoTree->subs($hotid);
  92. $pos = 0;
  93. $brands = [];
  94. foreach ($sub_hots as $hot)
  95. {
  96. if($pos == 0) {
  97. $brands = $this->mHotBrand->get($hot);
  98. } else {
  99. $tmp = $this->mHotBrand->get($hot);
  100. $brands = algorithm::set_union($brands,$tmp);
  101. }
  102. $pos++;
  103. }
  104. return $brands;
  105. }
  106. }
  107. public function commons($hotid)
  108. {
  109. if($this->mHoTree->is_parent($hotid) == false) {
  110. return $this->mHotCommon->get($hotid);
  111. }
  112. else
  113. {
  114. $sub_hots = $this->mHoTree->subs($hotid);
  115. $pos = 0;
  116. $cids = [];
  117. foreach ($sub_hots as $hot)
  118. {
  119. if($pos == 0) {
  120. $cids = $this->mHotCommon->get($hot);
  121. } else {
  122. $tmp = $this->mHotCommon->get($hot);
  123. $cids = algorithm::set_union($cids,$tmp);
  124. }
  125. $pos++;
  126. }
  127. return $cids;
  128. }
  129. }
  130. public function add_hot($hotid,$brand,$common)
  131. {
  132. $this->mHotBrand->add($hotid,$brand);
  133. $this->mHotCommon->add($hotid,$common);
  134. }
  135. public function hotid($gc_id) {
  136. return $this->mGcHot->get($gc_id);
  137. }
  138. private function add_gc_hot($gc_ids,$hot_id)
  139. {
  140. foreach ($gc_ids as $cid) {
  141. $cid = intval($cid);
  142. $this->mGcHot->add($cid,$hot_id);
  143. }
  144. }
  145. }
  146. class goods_dict extends indexer
  147. {
  148. private $cid_brands;
  149. private $cid_hots;
  150. public function __construct()
  151. {
  152. parent::__construct();
  153. $this->cid_brands = new one_one();
  154. $this->cid_hots = new one_one();
  155. }
  156. public function add_goods($common_id,$brand,$hot) {
  157. $this->cid_brands->add($common_id,$brand);
  158. $this->cid_hots->add($common_id,$hot);
  159. }
  160. public function hots($cids)
  161. {
  162. $values = [];
  163. foreach ($cids as $cid) {
  164. $hot = $this->cid_hots->get($cid);
  165. if(algorithm::binary_search($values,$hot) == false) {
  166. $pos = algorithm::lower_bonud($values,$hot);
  167. algorithm::array_insert($values,$pos,$hot);
  168. }
  169. }
  170. return $values;
  171. }
  172. public function brands($cids)
  173. {
  174. $values = [];
  175. foreach ($cids as $cid) {
  176. $brand = $this->cid_brands->get($cid);
  177. if(algorithm::binary_search($values,$brand) == false) {
  178. $pos = algorithm::lower_bonud($values,$brand);
  179. algorithm::array_insert($values,$pos,$brand);
  180. }
  181. }
  182. return $values;
  183. }
  184. }
  185. class special_dict extends indexer
  186. {
  187. private $spid_names;
  188. public function __construct()
  189. {
  190. parent::__construct();
  191. $this->spid_names = new one_one();
  192. $this->init();
  193. }
  194. private function init()
  195. {
  196. $mod_special = Model();
  197. $i = 0;
  198. while (true)
  199. {
  200. $start = $i * 1000;
  201. $items = $mod_special->table('mb_special')
  202. ->field('*')
  203. ->where(['special_id' => ['gt', 0],'state' => 0,'reader_type' => 0])
  204. ->order('special_id asc')
  205. ->limit("{$start},1000")
  206. ->select();
  207. $i++;
  208. if (empty($items)) {
  209. break;
  210. }
  211. else
  212. {
  213. foreach ($items as $item)
  214. {
  215. $spid = intval($item['special_id']);
  216. $keywords = $item['keywords'];
  217. $title = $item['share_title'];
  218. if(!empty($keywords)) {
  219. $this->parase($keywords,$spid);
  220. $this->parase($title,$spid);
  221. $this->spid_names->add($spid,$title);
  222. }
  223. }
  224. }
  225. }
  226. }
  227. public function name($val)
  228. {
  229. return $this->spid_names->get($val);
  230. }
  231. }
  232. class goods_info
  233. {
  234. private $mGoods;
  235. public function __construct()
  236. {
  237. $this->mGoods = [];
  238. }
  239. public function add($commonid, $price, $click, $salenum)
  240. {
  241. $commonid = intval($commonid);
  242. if(!array_key_exists($commonid,$this->mGoods)) {
  243. $this->mGoods[$commonid]['price'] = intval($price * 100 + 0.5);
  244. $this->mGoods[$commonid]['click'] = intval($click);
  245. $this->mGoods[$commonid]['salenum'] = intval($salenum);
  246. }
  247. }
  248. public function goods($commonid)
  249. {
  250. return $this->mGoods[$commonid];
  251. }
  252. static public function click_asc($left,$right)
  253. {
  254. $t_l = intval($left['click']);
  255. $t_r = intval($right['click']);
  256. if($t_l > $t_r) return 1;
  257. elseif($t_l < $t_r) return -1;
  258. else return 0;
  259. }
  260. static public function click_desc($left,$right)
  261. {
  262. $t_l = intval($left['click']);
  263. $t_r = intval($right['click']);
  264. if($t_l > $t_r) return -1;
  265. elseif($t_l < $t_r) return 1;
  266. else return 0;
  267. }
  268. static public function salenum_asc($left,$right)
  269. {
  270. $t_l = intval($left['salenum']);
  271. $t_r = intval($right['salenum']);
  272. if($t_l > $t_r) return 1;
  273. elseif($t_l < $t_r) return -1;
  274. else return 0;
  275. }
  276. static public function salenum_desc($left,$right)
  277. {
  278. $t_l = intval($left['salenum']);
  279. $t_r = intval($right['salenum']);
  280. if($t_l > $t_r) return -1;
  281. elseif($t_l < $t_r) return 1;
  282. else return 0;
  283. }
  284. static public function price_asc($left,$right)
  285. {
  286. $t_l = intval($left['price']);
  287. $t_r = intval($right['price']);
  288. if($t_l > $t_r) return 1;
  289. elseif($t_l < $t_r) return -1;
  290. else return 0;
  291. }
  292. static public function price_desc($left,$right)
  293. {
  294. $t_l = intval($left['price']);
  295. $t_r = intval($right['price']);
  296. if($t_l > $t_r) return -1;
  297. elseif($t_l < $t_r) return 1;
  298. else return 0;
  299. }
  300. }
  301. class searcher
  302. {
  303. const filter = "goods_commonid,gc_id,brand_id,goods_mobile_name,goods_jingle,goods_spec,goods_click,goods_salenum,goods_price";
  304. private $name_dict;
  305. private $jingle_dict;
  306. private $spec_dict;
  307. private $special_dict;
  308. private $brand_dict;
  309. private $category_dict;
  310. private $goods_dict;
  311. private $goods_info;
  312. private $price_cids;
  313. private $promot_cids;
  314. private static $stInstance;
  315. public static function instance()
  316. {
  317. if(self::$stInstance == null) {
  318. self::$stInstance = new searcher();
  319. }
  320. return self::$stInstance;
  321. }
  322. private function get_spec($arSpec)
  323. {
  324. $specs = unserialize($arSpec);
  325. if(isset($specs) && is_array($specs))
  326. {
  327. $spec_val = '';
  328. foreach ($specs as $key => $val) {
  329. $spec_val .= " ";
  330. $spec_val .= $val;
  331. }
  332. return trim($spec_val);
  333. } else {
  334. return "";
  335. }
  336. }
  337. public function init()
  338. {
  339. $this->brand_dict = new brand_dict();
  340. $this->category_dict = new category_dict();
  341. $this->name_dict = new indexer();
  342. $this->jingle_dict = new indexer();
  343. $this->spec_dict = new indexer();
  344. $this->goods_dict = new goods_dict();
  345. $this->goods_info = new goods_info();
  346. $this->price_cids = new valtokey();
  347. $this->special_dict = new special_dict();
  348. $this->promot_cids = [];
  349. $mod_goods = Model('goods');
  350. global $config;
  351. $exbrands = $config['exclude_brands'];
  352. if(!empty($exbrands)) {
  353. $items = $mod_goods->getGoodsOnlineList(['brand_id' => ['not in',$exbrands]], self::filter, '', '', false);
  354. } else {
  355. $items = $mod_goods->getGoodsOnlineList([], self::filter, '', '', false);
  356. }
  357. $count = count($items);
  358. $index = 1;
  359. foreach ($items as $item)
  360. {
  361. Log::record("{$index} / {$count}",Log::DEBUG);
  362. $index++;
  363. $common_id = intval($item['goods_commonid']);
  364. $gc_id = intval($item['gc_id']);
  365. $brand_id = intval($item['brand_id']);
  366. $hot_id = intval($this->category_dict->hotid($gc_id));
  367. $this->name_dict->parase($item['goods_mobile_name'],$common_id);
  368. $this->jingle_dict->parase($item['goods_jingle'],$common_id);
  369. $spec = $this->get_spec($item['goods_spec']);
  370. if(!empty($spec)) {
  371. $this->spec_dict->parase($spec,$common_id);
  372. $this->goods_dict->parase($spec,$common_id);
  373. }
  374. $this->goods_dict->parase($item['goods_mobile_name'],$common_id);
  375. $this->goods_dict->parase($item['goods_jingle'],$common_id);
  376. $this->brand_dict->add_brand($brand_id,$common_id,$hot_id);
  377. $this->goods_dict->add_goods($common_id,$brand_id,$hot_id);
  378. $this->category_dict->add_hot($hot_id,$brand_id,$common_id);
  379. $this->goods_info->add($common_id,$item['goods_price'],$item['goods_click'],$item['goods_salenum']);
  380. $this->price_cids->add($common_id,intval($item['goods_price'] * 100 + 0.5));
  381. }
  382. $this->price_cids->finish();
  383. $this->read_promotion();
  384. }
  385. private function read_promotion()
  386. {
  387. global $config;
  388. $special = $config['bonus_match_goods'];
  389. if(empty($special)) return false;
  390. $special_id = intval($special['special_id']);
  391. if($special_id <= 0) return false;
  392. special_manager::instance()->special($special_id,$goods_ids);
  393. if(empty($goods_ids)) return false;
  394. $cids = [];
  395. foreach ($goods_ids as $gid) {
  396. $cid = commonid_helper::instance()->common_id($gid);
  397. if($cid != false) {
  398. $cids[] = $cid;
  399. }
  400. }
  401. sort($cids);
  402. $this->promot_cids = $cids;
  403. return true;
  404. }
  405. private function get_name($dict, $words)
  406. {
  407. if(empty($words)) {
  408. return array();
  409. }
  410. $ids = $this->get_ids($dict, $words);
  411. $result = [];
  412. foreach ($ids as $id)
  413. {
  414. $name = $dict->name($id);
  415. if($name != false) {
  416. $result[] = $name;
  417. }
  418. }
  419. return $result;
  420. }
  421. public function get_word($input)
  422. {
  423. $words = word_segment::filter($input);
  424. if(empty($words)) return false;
  425. $goods_name = $this->get_name($this->name_dict,$words);
  426. $jingle_name = $this->get_name($this->jingle_dict,$words);
  427. $spec_name = $this->get_name($this->spec_dict,$words);
  428. $brands = $this->get_name($this->brand_dict,$words);
  429. $cats = $this->get_name($this->category_dict,$words);
  430. $specials = $this->get_name($this->special_dict,$words);
  431. $result = array_merge($brands,$cats,$specials,$spec_name,$goods_name,$jingle_name);
  432. return $result;
  433. }
  434. public function keyword($keyword)
  435. {
  436. if(empty($keyword)) return false;
  437. $words = word_segment::filter($keyword);
  438. if(empty($words)) return [];
  439. $cids = $this->get_ids($this->goods_dict,$words);
  440. $brands = $this->get_ids($this->brand_dict,$words);
  441. foreach ($brands as $brand_id) {
  442. $tmp = $this->brand_dict->commons($brand_id);
  443. $cids = algorithm::set_union($cids,$tmp);
  444. }
  445. $cats = $this->get_ids($this->category_dict,$words);
  446. foreach ($cats as $hot_id) {
  447. $tmp = $this->category_dict->commons($hot_id);
  448. $cids = algorithm::set_union($cids,$tmp);
  449. }
  450. return $cids;
  451. }
  452. private function format($cids,$bids,$hotids,$spids)
  453. {
  454. if(empty($cids)) {
  455. return [];
  456. }
  457. $brands = [];
  458. if(!empty($bids))
  459. {
  460. foreach ($bids as $bid)
  461. {
  462. $title = $this->brand_dict->name($bid);
  463. if($title != false) {
  464. $item['title'] = $title;
  465. $item['data'] = $bid;
  466. $item['type'] = 'brand';
  467. $brands[] = $item;
  468. }
  469. }
  470. }
  471. $hots = [];
  472. if(!empty($hotids))
  473. {
  474. foreach ($hotids as $hot)
  475. {
  476. $title = $this->category_dict->name($hot);
  477. if($title != false) {
  478. $item['title'] = $title;
  479. $item['data'] = $hot;
  480. $item['type'] = 'category';
  481. $hots[] = $item;
  482. }
  483. }
  484. }
  485. return ['cids' => $cids,'brands' => $brands,'hots' => $hots,'spids' => $spids];
  486. }
  487. private function keyword_serarch($keyword, $hot, $brand)
  488. {
  489. $cids = $this->keyword($keyword);
  490. if($brand > 0) {
  491. $brand_name = brand_helper::instance()->name($brand);
  492. } else {
  493. $brand_name = '';
  494. }
  495. if($hot > 0) {
  496. $hot_name = category_helper::instance()->name($hot);
  497. } else {
  498. $hot_name = '';
  499. }
  500. $keywords = $keyword . $brand_name . $hot_name;
  501. $spids = $this->get_ids($this->special_dict,word_segment::filter($keywords));
  502. if(empty($cids)) {
  503. return $this->format($cids,null,null,$spids);
  504. }
  505. if($brand != 0)
  506. {
  507. $bcids = $this->brand_dict->commons($brand);
  508. $cids = algorithm::set_intersection($cids,$bcids);
  509. if(empty($cids)) {
  510. return $this->format($cids,null,null,$spids);
  511. }
  512. if($hot != 0)
  513. {
  514. $hcids = $this->category_dict->commons($hot);
  515. $cids = algorithm::set_intersection($cids,$hcids);
  516. if(empty($cids)) {
  517. return $this->format($cids,null,null,$spids);
  518. } else {
  519. return $this->format($cids,null,null,$spids);
  520. }
  521. }
  522. else {
  523. $hots = $this->goods_dict->hots($cids);
  524. return $this->format($cids,null,$hots,$spids);
  525. }
  526. }
  527. else
  528. {
  529. if($hot != 0)
  530. {
  531. $hcids = $this->category_dict->commons($hot);
  532. $cids = algorithm::set_intersection($cids, $hcids);
  533. if (empty($cids)) {
  534. return array();
  535. } else {
  536. $brands = $this->goods_dict->brands($cids);
  537. return $this->format($cids,$brands,null,$spids);
  538. }
  539. }
  540. else
  541. {
  542. $brands = $this->goods_dict->brands($cids);
  543. return $this->format($cids,$brands,null,$spids);
  544. }
  545. }
  546. }
  547. private function hb_search($hot,$brand)
  548. {
  549. if($brand > 0) {
  550. $brand_name = brand_helper::instance()->name($brand);
  551. } else {
  552. $brand_name = '';
  553. }
  554. if($hot > 0) {
  555. $hot_name = category_helper::instance()->name($hot);
  556. } else {
  557. $hot_name = '';
  558. }
  559. $keywords = $brand_name . $hot_name;
  560. $spids = $this->get_ids($this->special_dict,word_segment::filter($keywords));
  561. if($brand != 0)
  562. {
  563. $cids = $this->brand_dict->commons($brand);
  564. if(empty($cids)) return array();
  565. if($hot != 0)
  566. {
  567. $hcids = $this->category_dict->commons($hot);
  568. $cids = algorithm::set_intersection($cids,$hcids);
  569. if(empty($cids)) {
  570. return array();
  571. }
  572. else {
  573. return $this->format($cids,null,null,$spids);
  574. }
  575. } else {
  576. $hots = $this->goods_dict->hots($cids);
  577. return $this->format($cids,null,$hots,$spids);
  578. }
  579. }
  580. else
  581. {
  582. if($hot != 0)
  583. {
  584. $cids = $this->category_dict->commons($hot);
  585. if(empty($cids)) {
  586. return array();
  587. } else {
  588. $brands = $this->goods_dict->brands($cids);
  589. return $this->format($cids,$brands,null,$spids);
  590. }
  591. } else {
  592. return array();
  593. }
  594. }
  595. }
  596. public function get_result($params)
  597. {
  598. $keyword = $params['keyword'];
  599. $brand = $params['brand_id'];
  600. $hot = $params['hot_id'];
  601. if(empty($keyword) && $brand == 0 && $hot == 0) {
  602. return array();
  603. }
  604. if(isset($keyword) && !empty($keyword)) {
  605. $result = $this->keyword_serarch($keyword,$hot,$brand);
  606. } else {
  607. $result = $this->hb_search($hot,$brand);
  608. }
  609. if(!empty($result))
  610. {
  611. $order = $params['order'];
  612. $sort = $params['sort'];
  613. $page_count = 1;
  614. if(!isset($params['page_no'])) {
  615. $page_no = null;
  616. } else {
  617. $page_no = $params['page_no'];
  618. }
  619. if(!isset($params['page_size'])) {
  620. $page_size = null;
  621. } else {
  622. $page_size = intval($params['page_size']);
  623. $page_size = $page_size <= 0 ? 10 : $page_size;
  624. $page_count = intval(count($result['cids']) / $page_size) + ((count($result['cids']) % $page_size) == 0 ? 0 : 1);
  625. }
  626. $cids = $this->sort_goods($result['cids'],$order,$sort,$page_no,$page_size);
  627. $result['cids'] = $cids;
  628. $result['page_count'] = $page_count;
  629. }
  630. return $result;
  631. }
  632. private function sort_goods($cids,$order,$sort,$page_no,$page_size)
  633. {
  634. $goods = [];
  635. foreach ($cids as $cid) {
  636. $val = $this->goods_info->goods($cid);
  637. $val['cid'] = $cid;
  638. $goods[] = $val;
  639. }
  640. if($sort == 'asc')
  641. {
  642. if($order == 'click') {
  643. uasort($goods,['\search\goods_info','click_asc']);
  644. }
  645. elseif($order == 'salenum') {
  646. uasort($goods,['\search\goods_info','salenum_asc']);
  647. }
  648. elseif($order == 'price') {
  649. uasort($goods,['\search\goods_info','price_asc']);
  650. }
  651. else {
  652. Log::record("searcher sort_goods not order param");
  653. }
  654. }
  655. else
  656. {
  657. if($order == 'click') {
  658. uasort($goods,['\search\goods_info','click_desc']);
  659. }
  660. elseif($order == 'salenum') {
  661. uasort($goods,['\search\goods_info','salenum_desc']);
  662. }
  663. elseif($order == 'price') {
  664. uasort($goods,['\search\goods_info','price_desc']);
  665. }
  666. else {
  667. Log::record("searcher sort_goods not order param");
  668. }
  669. }
  670. $tmp = [];
  671. foreach ($goods as $val) {
  672. $tmp[] = $val;
  673. }
  674. $goods = $tmp;
  675. $count = count($goods);
  676. $result = [];
  677. if($page_no == null || $page_size == null)
  678. {
  679. foreach ($goods as $val) {
  680. $result[] = $val['cid'];
  681. }
  682. }
  683. else
  684. {
  685. $page_no = $page_no <= 0 ? 1 : $page_no;
  686. for ($start = ($page_no -1) * $page_size; $page_size > 0 && $start < $count; $page_size--,$start++) {
  687. $result[] = $goods[$start]['cid'];
  688. }
  689. }
  690. return $result;
  691. }
  692. public function match_price($params)
  693. {
  694. $price = $params['price'];
  695. $page_no = intval($params['page_no']);
  696. $page_size = intval($params['page_size']);
  697. if($page_no <= 0 || $page_size <= 0) {
  698. return false;
  699. }
  700. $start = ($page_no - 1) * $page_size;
  701. $result = $this->price_cids->findless(intval($price * 100 + 0.5),$start,$page_size);
  702. $result['page_count'] = intval($result['total'] / $page_size) + ($result['total'] % $page_size == 0 ? 0 : 1);
  703. return $result;
  704. }
  705. public function promote_goods($params)
  706. {
  707. $price = $params['price'];
  708. $count = intval($params['count']);
  709. $items = $this->price_cids->findall(intval($price * 100 + 0.5));
  710. sort($items);
  711. $match_cids = $items;
  712. $pro_cids = algorithm::set_intersection($match_cids,$this->promot_cids);
  713. if(count($pro_cids) < $count) {
  714. $cids = $this->random_goods($match_cids,$count);
  715. } else {
  716. $cids = $this->random_goods($pro_cids,$count);
  717. }
  718. return ['total' => 1,'cids' => $cids,'total' => count($cids)];
  719. }
  720. private function random_goods($cids,$count)
  721. {
  722. $length = count($cids);
  723. if($length <= $count) return $cids;
  724. $result = [];
  725. for ($i = 0; $i < $count;)
  726. {
  727. $pos = mt_rand(0,$length - 1);
  728. $val = $cids[$pos];
  729. if(!in_array($val,$result)) {
  730. $result[] = $val;
  731. $i++;
  732. }
  733. }
  734. return $result;
  735. }
  736. private function get_ids($dict, $words)
  737. {
  738. $i = 0;
  739. $ids = [];
  740. foreach ($words as $word)
  741. {
  742. $tmp = $dict->find($word);
  743. if ($i == 0) {
  744. $ids = $tmp;
  745. } else {
  746. $ids = algorithm::set_intersection($tmp, $ids);
  747. if (empty($ids)) break;
  748. }
  749. ++$i;
  750. }
  751. return $ids;
  752. }
  753. }