send_manager.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/5/11
  6. * Time: 上午11:56
  7. */
  8. namespace fcode;
  9. require_once (BASE_ROOT_PATH . '/helper/message/msgutil.php');
  10. require_once (BASE_ROOT_PATH . '/helper/special_helper.php');
  11. use StatesHelper;
  12. use Log;
  13. use special_manager;
  14. class send_manager
  15. {
  16. public static $stInstance;
  17. private $mGoodsBlock;
  18. private $mAmountGoods;
  19. private function __construct()
  20. {
  21. }
  22. static public function instance()
  23. {
  24. if(self::$stInstance == null) {
  25. self::$stInstance = new send_manager();
  26. }
  27. if(StatesHelper::fetch_state('fcode')) {
  28. Log::record("fcode reinit data.",Log::DEBUG);
  29. self::$stInstance->init();
  30. }
  31. return self::$stInstance;
  32. }
  33. private function init()
  34. {
  35. $this->mAmountGoods = [];
  36. $this->mGoodsBlock = [];
  37. global $config;
  38. $fcodes_id = $config['paysuccess_fcodes']['fcodes_id'];
  39. $blocks = special_manager::instance()->special($fcodes_id,$goods_ids);
  40. foreach ($blocks as $block)
  41. {
  42. $item_type = $block['item_type'];
  43. $item_title = $block['item_title'];
  44. $amount = intval($item_title);
  45. if($item_type == 'home1' && $amount > 0)
  46. {
  47. $items = $block['items'];
  48. if(empty($items)) continue;
  49. $item = $items[0];
  50. $batch_code = $item['title'];
  51. if(!empty($batch_code)) {
  52. $this->add($amount,$item,$block);
  53. }
  54. }
  55. }
  56. krsort($this->mAmountGoods);
  57. }
  58. private function add($amount,$item,$block)
  59. {
  60. $goods_id = intval($item['data']);
  61. if($goods_id <= 0) return;
  62. if(array_key_exists($amount,$this->mAmountGoods) == false) {
  63. $this->mAmountGoods[$amount] = [];
  64. }
  65. $this->mAmountGoods[$amount][] = $goods_id;
  66. $this->mGoodsBlock[$goods_id] = $block;
  67. }
  68. public function fetch($amount)
  69. {
  70. $amount = intval($amount);
  71. if($amount <= 0) return false;
  72. $gids = [];
  73. foreach ($this->mAmountGoods as $key => $goods_ids)
  74. {
  75. if(empty($goods_ids)) continue;
  76. if($key <= $amount)
  77. {
  78. foreach ($goods_ids as $gid) {
  79. $gids[] = $gid;
  80. }
  81. }
  82. }
  83. if(empty($gids)) {
  84. return false;
  85. }
  86. $result = $this->validate_one($gids);
  87. return $result;
  88. }
  89. private function validate_one($gids)
  90. {
  91. $mod_goods = Model('goods');
  92. $goods_list = $mod_goods->getGoodsOnlineList(array('goods_id' => array('in', $gids),'goods_storage' => array('gt',0),'is_fcode' => 1));
  93. if(empty($goods_list)) return false;
  94. $all_goods = [];
  95. foreach ($goods_list as $goods) {
  96. $gid = intval($goods['goods_id']);
  97. $all_goods[$gid] = $goods;
  98. }
  99. foreach ($gids as $gid)
  100. {
  101. if(array_key_exists($gid,$all_goods))
  102. {
  103. $goods = $all_goods[$gid];
  104. $commonid = $goods['goods_commonid'];
  105. $block = $this->mGoodsBlock[$gid];
  106. $batch_code = $block['items'][0]['title'];
  107. $oper = new operator($commonid,$batch_code);
  108. //todo 测试完成之后需要关闭掉
  109. //if($oper->grabed()) continue;
  110. $fcode = $oper->grab();
  111. if($fcode != false) {
  112. return ['fcode' => $fcode,'banner' => $block];
  113. }
  114. }
  115. }
  116. return false;
  117. }
  118. }