send_manager.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 $mNormalGoods;
  19. private $mAdditionGoods;
  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->mNormalGoods = [];
  37. $this->mGoodsBlock = [];
  38. global $config;
  39. $fcodes_id = $config['autosend_fcodes']['fcodes_spid'];
  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->mNormalGoods);
  58. }
  59. private function add($amount,$item,$block)
  60. {
  61. global $config;
  62. $add_gids = $config['autosend_fcodes']['additional_gids'];
  63. $goods_id = intval($item['data']);
  64. if($goods_id <= 0) return;
  65. if(!empty($add_gids) && is_array($add_gids) && in_array($goods_id,$add_gids))
  66. {
  67. if(array_key_exists($amount,$this->mNormalGoods) == false) {
  68. $this->mAdditionGoods[$amount] = [];
  69. }
  70. $this->mAdditionGoods[$amount][] = $goods_id;
  71. }
  72. else
  73. {
  74. if(array_key_exists($amount,$this->mNormalGoods) == false) {
  75. $this->mNormalGoods[$amount] = [];
  76. }
  77. $this->mNormalGoods[$amount][] = $goods_id;
  78. }
  79. $this->mGoodsBlock[$goods_id] = $block;
  80. }
  81. public function fetch($amount,$pay_sn)
  82. {
  83. $normal = $this->fetch_normal($amount,$pay_sn);
  84. $addition = $this->fetch_addition($amount,$pay_sn);
  85. $result = [];
  86. if(!empty($normal)) {
  87. $result['banner'][] = $normal['banner'];
  88. $result['fcode'][] = $normal['fcode'];
  89. }
  90. if(!empty($addition)) {
  91. $result['fcode'][] = $addition['fcode'];
  92. $result['banner'][] = $addition['banner'];
  93. }
  94. return empty($result) ? false : $result;
  95. }
  96. public function fetch_addition($amount,$pay_sn)
  97. {
  98. $amount = intval($amount);
  99. if($amount <= 0) return false;
  100. $gids = [];
  101. foreach ($this->mAdditionGoods as $key => $goods_ids)
  102. {
  103. if(empty($goods_ids)) continue;
  104. if($key <= $amount)
  105. {
  106. foreach ($goods_ids as $gid) {
  107. $gids[] = $gid;
  108. }
  109. }
  110. }
  111. if(empty($gids)) {
  112. return false;
  113. }
  114. $result = $this->validate_one($gids,$pay_sn);
  115. return $result;
  116. }
  117. public function fetch_normal($amount,$pay_sn)
  118. {
  119. $amount = intval($amount);
  120. if($amount <= 0) return false;
  121. $gids = [];
  122. foreach ($this->mNormalGoods as $key => $goods_ids)
  123. {
  124. if(empty($goods_ids)) continue;
  125. if($key <= $amount)
  126. {
  127. foreach ($goods_ids as $gid) {
  128. $gids[] = $gid;
  129. }
  130. }
  131. }
  132. if(empty($gids)) {
  133. return false;
  134. }
  135. $result = $this->validate_one($gids,$pay_sn);
  136. return $result;
  137. }
  138. private function validate_one($gids,$pay_sn)
  139. {
  140. $mod_goods = Model('goods');
  141. $goods_list = $mod_goods->getGoodsOnlineList(array('goods_id' => array('in', $gids),'goods_storage' => array('gt',0),'is_fcode' => 1));
  142. if(empty($goods_list)) return false;
  143. $all_goods = [];
  144. foreach ($goods_list as $goods) {
  145. $gid = intval($goods['goods_id']);
  146. $all_goods[$gid] = $goods;
  147. }
  148. foreach ($gids as $gid)
  149. {
  150. if(array_key_exists($gid,$all_goods))
  151. {
  152. $goods = $all_goods[$gid];
  153. $commonid = $goods['goods_commonid'];
  154. $block = $this->mGoodsBlock[$gid];
  155. $batch_code = $block['items'][0]['title'];
  156. $oper = new operator($commonid,$batch_code);
  157. if($oper->grabed()) continue;
  158. $fcode = $oper->lock($pay_sn);
  159. if($fcode != false) {
  160. return ['fcode' => $fcode,'banner' => $block];
  161. }
  162. }
  163. }
  164. return false;
  165. }
  166. }