123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/5/11
- * Time: 上午11:56
- */
- namespace fcode;
- require_once (BASE_ROOT_PATH . '/helper/message/msgutil.php');
- require_once (BASE_ROOT_PATH . '/helper/special_helper.php');
- use StatesHelper;
- use Log;
- use special_manager;
- class send_manager
- {
- public static $stInstance;
- private $mGoodsBlock;
- private $mNormalGoods;
- private $mAdditionGoods;
- private function __construct()
- {
- }
- static public function instance()
- {
- if(self::$stInstance == null) {
- self::$stInstance = new send_manager();
- }
- if(StatesHelper::fetch_state('fcode')) {
- Log::record("fcode reinit data.",Log::DEBUG);
- self::$stInstance->init();
- }
- return self::$stInstance;
- }
- private function init()
- {
- $this->mNormalGoods = [];
- $this->mGoodsBlock = [];
- global $config;
- $fcodes_id = $config['autosend_fcodes']['fcodes_spid'];
- $blocks = special_manager::instance()->special($fcodes_id,$goods_ids);
- foreach ($blocks as $block)
- {
- $item_type = $block['item_type'];
- $item_title = $block['item_title'];
- $amount = intval($item_title);
- if($item_type == 'home1' && $amount > 0)
- {
- $items = $block['items'];
- if(empty($items)) continue;
- $item = $items[0];
- $batch_code = $item['title'];
- if(!empty($batch_code)) {
- $this->add($amount,$item,$block);
- }
- }
- }
- krsort($this->mNormalGoods);
- }
- private function add($amount,$item,$block)
- {
- global $config;
- $add_gids = $config['autosend_fcodes']['additional_gids'];
- $goods_id = intval($item['data']);
- if($goods_id <= 0) return;
- if(!empty($add_gids) && is_array($add_gids) && in_array($goods_id,$add_gids))
- {
- if(array_key_exists($amount,$this->mNormalGoods) == false) {
- $this->mAdditionGoods[$amount] = [];
- }
- $this->mAdditionGoods[$amount][] = $goods_id;
- }
- else
- {
- if(array_key_exists($amount,$this->mNormalGoods) == false) {
- $this->mNormalGoods[$amount] = [];
- }
- $this->mNormalGoods[$amount][] = $goods_id;
- }
- $this->mGoodsBlock[$goods_id] = $block;
- }
- public function fetch($amount,$pay_sn)
- {
- $normal = $this->fetch_normal($amount,$pay_sn);
- $addition = $this->fetch_addition($amount,$pay_sn);
- $result = [];
- if(!empty($normal)) {
- $result['banner'][] = $normal['banner'];
- $result['fcode'][] = $normal['fcode'];
- }
- if(!empty($addition)) {
- $result['fcode'][] = $addition['fcode'];
- $result['banner'][] = $addition['banner'];
- }
- return empty($result) ? false : $result;
- }
- public function fetch_addition($amount,$pay_sn)
- {
- $amount = intval($amount);
- if($amount <= 0) return false;
- $gids = [];
- foreach ($this->mAdditionGoods as $key => $goods_ids)
- {
- if(empty($goods_ids)) continue;
- if($key <= $amount)
- {
- foreach ($goods_ids as $gid) {
- $gids[] = $gid;
- }
- }
- }
- if(empty($gids)) {
- return false;
- }
- $result = $this->validate_one($gids,$pay_sn);
- return $result;
- }
- public function fetch_normal($amount,$pay_sn)
- {
- $amount = intval($amount);
- if($amount <= 0) return false;
- $gids = [];
- foreach ($this->mNormalGoods as $key => $goods_ids)
- {
- if(empty($goods_ids)) continue;
- if($key <= $amount)
- {
- foreach ($goods_ids as $gid) {
- $gids[] = $gid;
- }
- }
- }
- if(empty($gids)) {
- return false;
- }
- $result = $this->validate_one($gids,$pay_sn);
- return $result;
- }
- private function validate_one($gids,$pay_sn)
- {
- $mod_goods = Model('goods');
- $goods_list = $mod_goods->getGoodsOnlineList(array('goods_id' => array('in', $gids),'goods_storage' => array('gt',0),'is_fcode' => 1));
- if(empty($goods_list)) return false;
- $all_goods = [];
- foreach ($goods_list as $goods) {
- $gid = intval($goods['goods_id']);
- $all_goods[$gid] = $goods;
- }
- foreach ($gids as $gid)
- {
- if(array_key_exists($gid,$all_goods))
- {
- $goods = $all_goods[$gid];
- $commonid = $goods['goods_commonid'];
- $block = $this->mGoodsBlock[$gid];
- $batch_code = $block['items'][0]['title'];
- $oper = new operator($commonid,$batch_code);
- if($oper->grabed()) continue;
- $fcode = $oper->lock($pay_sn);
- if($fcode != false) {
- return ['fcode' => $fcode,'banner' => $block];
- }
- }
- }
- return false;
- }
- }
|