goods_sampler.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/11/27
  6. * Time: 下午2:36
  7. */
  8. namespace activity;
  9. require_once (BASE_ROOT_PATH . '/helper/message/msgutil.php');
  10. use StatesHelper;
  11. use Log;
  12. use special_manager;
  13. use algorithm;
  14. use commonid_helper;
  15. class goods_sampler
  16. {
  17. public static $stInstance;
  18. private $mMaleGoods;
  19. private $mFemaleGoods;
  20. private function __construct()
  21. {
  22. $this->mFemaleGoods = [];
  23. $this->mMaleGoods = [];
  24. }
  25. static public function instance()
  26. {
  27. if(self::$stInstance == null) {
  28. self::$stInstance = new goods_sampler();
  29. }
  30. if(StatesHelper::fetch_state('goods_sampler'))
  31. {
  32. Log::record("goods_sampler reinit data.",Log::DEBUG);
  33. self::$stInstance->init();
  34. }
  35. return self::$stInstance;
  36. }
  37. public function fetch_male($count)
  38. {
  39. return $this->fetch($this->mMaleGoods,$count);
  40. }
  41. public function fetch_female($count)
  42. {
  43. return $this->fetch($this->mFemaleGoods,$count);
  44. }
  45. private function fetch(array $datas,$count)
  46. {
  47. $total = count($datas);
  48. if($total <=0 || $total < $count) return [];
  49. $poses = [];
  50. for ($i = 0; $i < $count;)
  51. {
  52. $pos = mt_rand(0,$total - 1);
  53. if(algorithm::binary_search($poses,$pos) == false) {
  54. $cur_pos = algorithm::lower_bonud($poses,$pos);
  55. algorithm::array_insert($poses,$cur_pos,$pos);
  56. $i++;
  57. }
  58. }
  59. $result = [];
  60. foreach ($poses as $pos) {
  61. $gid = $datas[$pos];
  62. $cid = commonid_helper::instance()->common_id($gid);
  63. if($cid != false) {
  64. $result[] = $cid;
  65. }
  66. }
  67. return $result;
  68. }
  69. private function read($spid)
  70. {
  71. $spid = intval($spid);
  72. if($spid <= 0) return [];
  73. special_manager::instance()->special($spid,$goods_ids);
  74. if(empty($goods_ids)) {
  75. return [];
  76. } else {
  77. return $goods_ids;
  78. }
  79. }
  80. private function init()
  81. {
  82. global $config;
  83. $sp_male = $config['goods_sampler']['male'];
  84. $sp_female = $config['goods_sampler']['female'];
  85. $this->mMaleGoods = $this->read($sp_male);
  86. $this->mFemaleGoods = $this->read($sp_female);
  87. }
  88. }