send_manager.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 $mConfirmGoods; //确认收获后赠送的F码商品。
  19. private $mPayGoods; //支付后送的F码商品。
  20. private function __construct()
  21. {
  22. }
  23. static public function instance()
  24. {
  25. if(self::$stInstance == null) {
  26. self::$stInstance = new send_manager();
  27. }
  28. if(StatesHelper::fetch_state('fcode')) {
  29. Log::record("fcode reinit data.",Log::DEBUG);
  30. self::$stInstance->init();
  31. }
  32. return self::$stInstance;
  33. }
  34. private function init()
  35. {
  36. $this->mConfirmGoods = [];
  37. $this->mGoodsBlock = [];
  38. global $config;
  39. $fcodes_id = $config['autosend_fcodes']['fcodes_specialid'];
  40. $blocks = special_manager::instance()->special($fcodes_id,$goods_ids);
  41. foreach ($blocks as $block)
  42. {
  43. $item_type = $block['item_type'];
  44. $item_title = $block['item_title'];
  45. $amount = intval($item_title);
  46. if($item_type == 'home1' && $amount > 0)
  47. {
  48. $items = $block['items'];
  49. if(empty($items)) continue;
  50. $item = $items[0];
  51. $batch_code = $item['title'];
  52. if(!empty($batch_code)) {
  53. $this->add($amount,$item,$block);
  54. }
  55. }
  56. }
  57. krsort($this->mConfirmGoods);
  58. }
  59. private function add($amount,$item,$block)
  60. {
  61. global $config;
  62. $pay_gids = $config['autosend_fcodes']['pay_goodsids'];
  63. $goods_id = intval($item['data']);
  64. if($goods_id <= 0) return;
  65. if(!empty($pay_gids) && is_array($pay_gids) && in_array($goods_id,$pay_gids))
  66. {
  67. if(array_key_exists($amount,$this->mConfirmGoods) == false) {
  68. $this->mPayGoods[$amount] = [];
  69. }
  70. $this->mPayGoods[$amount][] = $goods_id;
  71. }
  72. else
  73. {
  74. if(array_key_exists($amount,$this->mConfirmGoods) == false) {
  75. $this->mConfirmGoods[$amount] = [];
  76. }
  77. $this->mConfirmGoods[$amount][] = $goods_id;
  78. }
  79. $this->mGoodsBlock[$goods_id] = $block;
  80. }
  81. public function fetch_pay($amount)
  82. {
  83. $amount = intval($amount);
  84. if($amount <= 0) return false;
  85. $gids = [];
  86. foreach ($this->mPayGoods as $key => $goods_ids)
  87. {
  88. if(empty($goods_ids)) continue;
  89. if($key <= $amount)
  90. {
  91. foreach ($goods_ids as $gid) {
  92. $gids[] = $gid;
  93. }
  94. }
  95. }
  96. if(empty($gids)) {
  97. return false;
  98. }
  99. $result = $this->validate_one($gids);
  100. return $result;
  101. }
  102. public function fetch_confirm($amount)
  103. {
  104. $amount = intval($amount);
  105. if($amount <= 0) return false;
  106. $gids = [];
  107. foreach ($this->mConfirmGoods as $key => $goods_ids)
  108. {
  109. if(empty($goods_ids)) continue;
  110. if($key <= $amount)
  111. {
  112. foreach ($goods_ids as $gid) {
  113. $gids[] = $gid;
  114. }
  115. }
  116. }
  117. if(empty($gids)) {
  118. return false;
  119. }
  120. $result = $this->validate_one($gids);
  121. return $result;
  122. }
  123. private function validate_one($gids)
  124. {
  125. $mod_goods = Model('goods');
  126. $goods_list = $mod_goods->getGoodsOnlineList(array('goods_id' => array('in', $gids),'goods_storage' => array('gt',0),'is_fcode' => 1));
  127. if(empty($goods_list)) return false;
  128. $all_goods = [];
  129. foreach ($goods_list as $goods) {
  130. $gid = intval($goods['goods_id']);
  131. $all_goods[$gid] = $goods;
  132. }
  133. foreach ($gids as $gid)
  134. {
  135. if(array_key_exists($gid,$all_goods))
  136. {
  137. $goods = $all_goods[$gid];
  138. $commonid = $goods['goods_commonid'];
  139. $block = $this->mGoodsBlock[$gid];
  140. $batch_code = $block['items'][0]['title'];
  141. $oper = new operator($commonid,$batch_code);
  142. if($oper->grabed()) continue;
  143. $fcode = $oper->grab();
  144. if($fcode != false) {
  145. return ['fcode' => $fcode,'banner' => $block];
  146. }
  147. }
  148. }
  149. return false;
  150. }
  151. }