123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- <?php
- declare(strict_types=0);
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/8/30
- * Time: 下午9:14
- */
- namespace activity;
- use algorithm;
- use StatesHelper;
- use Log;
- use commonid_helper;
- //M元买N件商品活动
- class opgroup
- {
- private $mPrice;
- private $mOptions;
- private $mStartTm;
- private $mEndTm;
- private $mGoods;
- private $mRepeat;
- private $mSpecialID;
- public function __construct($price,$options,$start,$end,$repeat,$special_id)
- {
- $this->mPrice = $price;
- $this->mOptions = $options;
- $this->mStartTm = $start;
- $this->mEndTm = $end;
- $this->mRepeat = $repeat;
- $this->mSpecialID = $special_id;
- $this->mGoods = [];
- }
- public function match($goods_nums)
- {
- if($this->acting() == false) return false;
- $gids = array_keys($goods_nums);
- sort($gids);
- $match_gids = algorithm::set_intersection($this->mGoods,$gids);
- $matched = false;
- if($this->mRepeat == false)
- {
- if(count($match_gids) >= $this->mOptions) {
- $matched = true;
- }
- }
- else
- {
- $goods_num = $this->goods_num($match_gids, $goods_nums);
- if ($goods_num >= $this->mOptions) {
- $matched = true;
- }
- }
- if($matched)
- {
- $matched_goods = [];
- foreach ($match_gids as $gid) {
- $matched_goods[$gid] = $goods_nums[$gid];
- }
- return ['goods_nums' => $matched_goods,'price' => $this->mPrice,'options' => $this->mOptions,'repeat' => $this->mRepeat];
- } else {
- return false;
- }
- }
- public function info($gid)
- {
- if($this->acting() == false) return false;
- if(algorithm::binary_search($this->mGoods,$gid))
- {
- $result['special_id'] = $this->mSpecialID;
- $price = $this->mPrice / 100;
- $result['desc'] = "{$price}元任选{$this->mOptions}件";
- $result['short_desc'] = "{$price}选{$this->mOptions}";
- return $result;
- } else {
- return false;
- }
- }
- private function goods_num($gids,$gid_nums)
- {
- $num = 0;
- foreach ($gids as $gid)
- {
- $num += $gid_nums[$gid];
- }
- return $num;
- }
- public function add_goods($gid)
- {
- $gid = intval($gid);
- if($gid <= 0) return false;
- if(algorithm::binary_search($this->mGoods,$gid)) {
- return false;
- } else {
- $pos = algorithm::lower_bonud($this->mGoods,$gid);
- algorithm::array_insert($this->mGoods,$pos,$gid);
- return true;
- }
- }
- public function acting() {
- $cur_tm = time();
- return ($cur_tm >= $this->mStartTm && $cur_tm < $this->mEndTm);
- }
- public function equal($price,$options) {
- return ($this->mPrice == $price && $this->mOptions == $options);
- }
- }
- class optional_goods
- {
- private $mAllGoods;
- private $mOpgroups;
- private $mSpecials;
- private $mErrGoods;
- static private $stInstance = null;
- private function __construct()
- {
- $this->mOpgroups = [];
- $this->mAllGoods = [];
- $this->mSpecials = [];
- $this->mErrGoods = [];
- }
- static public function instance()
- {
- if (self::$stInstance == null) {
- self::$stInstance = new optional_goods();
- }
- if (StatesHelper::fetch_state('optional_goods')) {
- Log::record("optional_goods reinit data.", Log::DEBUG);
- self::$stInstance->init();
- }
- return self::$stInstance;
- }
- public function layout_error()
- {
- if (empty($this->mErrGoods)) {
- return false;
- } else {
- return $this->mErrGoods;
- }
- }
- public function all_goods()
- {
- return $this->mAllGoods;
- }
- public function exist_goods($gid)
- {
- $gid = intval($gid);
- if ($gid <= 0) return false;
- if (algorithm::binary_search($this->mAllGoods, $gid)) {
- return true;
- } else {
- return false;
- }
- }
- public function info($goods_id)
- {
- $gid = intval($goods_id);
- if ($gid <= 0) return false;
- if (algorithm::binary_search($this->mAllGoods, $gid))
- {
- foreach ($this->mOpgroups as $group)
- {
- $info = $group->info($gid);
- if($info == false) continue;
- if($group->acting() == false) {
- return false;
- } else {
- return $info;
- }
- }
- return false;
- } else {
- return false;
- }
- }
- public function match($gidnums)
- {
- $input_goods = $this->input_goods($gidnums);
- $match_goods = algorithm::set_intersection($this->mAllGoods, $input_goods);
- if (empty($match_goods)) return false;
- $match_goods = $this->sel_goods($match_goods, $gidnums);
- $result = [];
- foreach ($this->mOpgroups as $opgroup)
- {
- if (empty($match_goods)) break;
- $part = $opgroup->match($match_goods);
- if ($part != false) {
- $result[] = $part;
- $goods_nums = $part['goods_nums'];
- foreach ($goods_nums as $gid => $num) {
- unset($match_goods[$gid]);
- }
- }
- }
- return $result;
- }
- private function sel_goods($gids, $gidnums)
- {
- $result = [];
- foreach ($gids as $gid) {
- $result[$gid] = $gidnums[$gid];
- }
- return $result;
- }
- private function input_goods($gidnums)
- {
- $result = [];
- foreach ($gidnums as $gid => $num) {
- if ($num > 0) {
- $result[] = $gid;
- }
- }
- sort($result);
- return $result;
- }
- private function init()
- {
- global $config;
- $opids = $config['optional_goods'];
- if (empty($opids)) return true;
- $this->mOpgroups = [];
- $this->mAllGoods = [];
- $this->mSpecials = [];
- $this->mErrGoods = [];
- $this->mSpecials = array_unique($opids);
- foreach ($this->mSpecials as $special_id) {
- $this->add_special($special_id);
- }
- sort($this->mErrGoods);
- }
- private function add_special($special_id)
- {
- $mod_special = Model('mb_special');
- $ret = $mod_special->getMbSpecialItemUsableListByID($special_id);
- $blocks = $ret['blocks'];
- $cur_group = null;
- foreach ($blocks as $block)
- {
- $type = $block['item_type'];
- if ($type == 'home1') {
- $tmp_group = $this->add_group($block, $special_id);
- if ($tmp_group != null) {
- $cur_group = $tmp_group;
- $this->mOpgroups[] = $cur_group;
- }
- } else {
- $this->add_goods($cur_group, $block);
- }
- }
- }
- private function add_goods($group, $block)
- {
- $items = $block['items'];
- if (empty($items)) return;
- foreach ($items as $item)
- {
- if (array_key_exists('type', $item) && $item['type'] == 'goods')
- {
- $goods_id = intval($item['data']);
- $spugids = $this->spu_goods($goods_id);
- if($spugids == false) continue;
- foreach ($spugids as $goods_id)
- {
- if ($group != null && $goods_id > 0 && $this->exist_goods($goods_id) == false) {
- if ($group->add_goods($goods_id)) {
- $this->addgoods($goods_id);
- }
- } else {
- $this->mErrGoods[] = $goods_id;
- }
- }
- }
- }
- }
- private function spu_goods($gid)
- {
- $cid = commonid_helper::instance()->common_id($gid);
- if($cid != false)
- {
- $gids = commonid_helper::instance()->goods_ids($cid);
- if($gids == false) return false;
- if(count($gids) == 1) {
- return $gids;
- }
- else
- {
- $goods_map = [];
- $mod_goods = Model('goods');
- $goodses = $mod_goods->getGoodsOnlineList(array('goods_id' => array('in', $gids)));
- foreach ($goodses as $goods) {
- $gid = intval($goods['goods_id']);
- $goods_map[$gid] = $goods['goods_price'];
- }
- $goods_price = $goods_map[$gid];
- $result = [];
- foreach ($goods_map as $goods_id => $price)
- {
- if($price == $goods_price) {
- $result[] = $goods_id;
- }
- }
- return empty($result) ? false : $result;
- }
- }
- else {
- return false;
- }
- }
- private function add_group($block,$special_id)
- {
- $items = $block['items'];
- if(empty($items)) return null;
- $reserved = $items['reserved'];
- $params = preg_split("/#/",$reserved);
- if(count($params) == 5)
- {
- $price = intval($params[0] * 100 + 0.5);
- $options = intval($params[1]);
- $repeat = intval($params[2]) == 1 ? true : false;
- $startm = strtotime($params[3]);
- $hours = intval($params[4]);
- if($price > 0 && $options > 1)
- {
- $group = $this->find($price,$options);
- if($group == null)
- {
- if($startm > 0 && $hours > 0 && $startm + 3600 * $hours > time()) {
- $group = new opgroup($price,$options,$startm,$startm + 3600 * $hours,$repeat,$special_id);
- }
- }
- return $group;
- }
- else {
- return null;
- }
- }
- }
- private function find($price,$options)
- {
- foreach ($this->mOpgroups as $item)
- {
- if($item->equal($price,$options)) {
- return $item;
- }
- }
- return null;
- }
- private function addgoods($gid)
- {
- $gid = intval($gid);
- if($gid <= 0) return false;
- if(algorithm::binary_search($this->mAllGoods,$gid)) {
- return false;
- } else {
- $pos = algorithm::lower_bonud($this->mAllGoods,$gid);
- algorithm::array_insert($this->mAllGoods,$pos,$gid);
- return true;
- }
- }
- }
|