send_manager.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 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('pay_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->mNormalGoods = [];
  36. $this->mGoodsBlock = [];
  37. global $config;
  38. $fcodes_id = $config['autosend_fcodes']['fcodes_spid'];
  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->mNormalGoods);
  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->mNormalGoods) == false) {
  63. $this->mNormalGoods[$amount] = [];
  64. }
  65. $this->mNormalGoods[$amount][] = $goods_id;
  66. $this->mGoodsBlock[$goods_id] = $block;
  67. }
  68. public function fetch($amount,$pay_sn)
  69. {
  70. $normal = $this->fetch_normal($amount,$pay_sn);
  71. $result = [];
  72. if(!empty($normal)) {
  73. $result['fcode'][] = $normal['fcode'];
  74. $result['banner'][] = $normal['banner'];
  75. }
  76. return empty($result) ? false : $result;
  77. }
  78. public function fetch_normal($amount,$pay_sn)
  79. {
  80. $amount = intval($amount);
  81. if($amount <= 0) return false;
  82. $gids = [];
  83. foreach ($this->mNormalGoods as $key => $goods_ids)
  84. {
  85. if(empty($goods_ids)) continue;
  86. if($key <= $amount)
  87. {
  88. foreach ($goods_ids as $gid) {
  89. $gids[] = $gid;
  90. }
  91. }
  92. }
  93. if(empty($gids)) {
  94. return false;
  95. }
  96. $result = $this->validate_one($gids,$pay_sn);
  97. return $result;
  98. }
  99. private function validate_one($gids,$pay_sn)
  100. {
  101. $mod_goods = Model('goods');
  102. $goods_list = $mod_goods->getGoodsOnlineList(array('goods_id' => array('in', $gids),'goods_storage' => array('gt',0),'is_fcode' => 1));
  103. if(empty($goods_list)) return false;
  104. $all_goods = [];
  105. foreach ($goods_list as $goods) {
  106. $gid = intval($goods['goods_id']);
  107. $all_goods[$gid] = $goods;
  108. }
  109. foreach ($gids as $gid)
  110. {
  111. if(array_key_exists($gid,$all_goods))
  112. {
  113. $goods = $all_goods[$gid];
  114. $commonid = $goods['goods_commonid'];
  115. $block = $this->mGoodsBlock[$gid];
  116. $batch_code = $block['items'][0]['title'];
  117. $oper = new operator($commonid,$batch_code);
  118. if($oper->grabed() == false)
  119. {
  120. $fcode = $oper->lock($pay_sn);
  121. if($fcode != false) {
  122. return ['fcode' => $fcode,'banner' => $block];
  123. }
  124. }
  125. }
  126. }
  127. return false;
  128. }
  129. }