123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/8/15
- * Time: 下午10:21
- */
- namespace activity;
- class limit_item
- {
- private $mParam;
- public function __construct($param)
- {
- $this->mParam = null;
- if(isset($param) && is_array($param))
- {
- $this->mParam = $param;
- }
- }
- public function format()
- {
- $info['xianshi_id'] = intval($this->mParam['xianshi_id']);
- $info['xianshi_name'] = $this->mParam['xianshi_name'];
- $info['xianshi_title'] = $this->mParam['xianshi_title'];
- $info['xianshi_explain'] = $this->mParam['xianshi_explain'];
- $info['start_time'] = intval($this->mParam['start_time']);
- $info['end_time'] = intval($this->mParam['end_time']);
- $info['lower_limit'] = intval($this->mParam['lower_limit']);
- $info['state'] = intval($this->mParam['state']);
- //$info['goods_list'] = $this->mParam['goods_list'];
- return $info;
- }
- }
- class limitime
- {
- const XIANSHI_STATE_NORMAL = 1;
- const XIANSHI_STATE_CLOSE = 2;
- const XIANSHI_STATE_CANCEL = 3;
- const STORE_ID = 6;
- static private $stInstance = null;
- private $limit_list;
- private $mGoodsidLimitGoods;
- private $mLimitGoods;
- private $verchecker;
- private function __construct()
- {
- $this->verchecker = new version_checker('activity_version_limit',120);
- }
- static public function instance()
- {
- if(self::$stInstance == null) {
- self::$stInstance = new limitime();
- }
- if(self::$stInstance->verchecker->need_init()) {
- self::$stInstance->init();
- }
- return self::$stInstance;
- }
- private function init()
- {
- $mod = Model('p_xianshi');
- $condition = array();
- $condition['store_id'] = self::STORE_ID;
- $condition['state'] = self::XIANSHI_STATE_NORMAL;
- $this->limit_list = array();
- $this->mGoodsidLimitGoods = array();
- $this->mLimitGoods = [];
- $list = $mod->getXianshiList($condition);
- foreach ($list as $val)
- {
- $xianshi_id = intval($val['xianshi_id']);
- $this->limit_list[$xianshi_id] = $val;
- $this->mLimitGoods[$xianshi_id] = [];
- $gids = [];
- $goods_list = Model('p_xianshi_goods')->getXianshiGoodsList(array('xianshi_id' => $xianshi_id));
- foreach ($goods_list as $xianshi_goods) {
- $goods_id = intval($xianshi_goods['goods_id']);
- $this->add_limitgoods($goods_id,$xianshi_goods);
- $gids[] = $goods_id;
- }
- $this->mLimitGoods[$xianshi_id] = $gids;
- $this->limit_list[$xianshi_id]['goods_list'] = $gids;
- }
- }
- private function add_limitgoods($goods_id,$limit_goods)
- {
- if(isset($this->mGoodsidLimitGoods[$goods_id])) {
- $this->mGoodsidLimitGoods[$goods_id][] = $limit_goods;
- } else {
- $this->mGoodsidLimitGoods[$goods_id] = [];
- $this->mGoodsidLimitGoods[$goods_id][] = $limit_goods;
- }
- }
- public function isTakepart($goods_id,&$act_id)
- {
- if(isset($this->mGoodsidLimitGoods[intval($goods_id)]))
- {
- $limits = $this->mGoodsidLimitGoods[$goods_id];
- foreach ($limits as $limit)
- {
- $start = $limit['start_time'];
- $end = $limit['end_time'];
- $cur = time();
- if($cur >= intval($start) && $cur < intval($end)) {
- $act_id = intval($limit['xianshi_id']);
- return true;
- }
- }
- return false;
- }
- else {
- return false;
- }
- }
- public function get_info($act_id)
- {
- if(isset($this->limit_list[$act_id])) {
- $limit = $this->limit_list[$act_id];
- $item = new limit_item($limit);
- return $item->format();
- }
- else {
- return false;
- }
- }
- public function promotion_price($limit_id,$goods_id,&$price)
- {
- if(isset($this->mGoodsidLimitGoods[intval($goods_id)]))
- {
- $limit_goods = $this->mGoodsidLimitGoods[$goods_id];
- foreach ($limit_goods as $goods)
- {
- $id = intval($goods['xianshi_id']);
- if($id == $limit_id) {
- $price = doubleval($goods['xianshi_price']);
- return true;
- }
- }
- return false;
- }
- else {
- return false;
- }
- }
- public function goods($limit_id)
- {
- if(isset($this->limit_list[$limit_id])) {
- return $this->mLimitGoods[$limit_id];
- } else {
- return array();
- }
- }
- public function acting()
- {
- $limits = [];
- $curtm = time();
- foreach ($this->limit_list as $limit)
- {
- $start = intval($limit['start_time']);
- $end = intval($limit['end_time']);
- if($curtm >= $start && $curtm < $end) {
- $limits[] = $limit;
- }
- }
- $result = [];
- foreach ($limits as $val) {
- $item = new limit_item($val);
- $result[] = $item->format();
- }
- return $result;
- }
- public function unstart()
- {
- $limits = [];
- $curtm = time();
- foreach ($this->limit_list as $limit)
- {
- $start = intval($limit['start_time']);
- if($curtm < $start) {
- $limits[] = $limit;
- }
- }
- $result = [];
- foreach ($limits as $val) {
- $item = new limit_item($val);
- $result[] = $item->format();
- }
- return $result;
- }
- }
|