limitime.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/8/15
  6. * Time: 下午10:21
  7. */
  8. namespace activity;
  9. class limit_item
  10. {
  11. private $mParam;
  12. public function __construct($param)
  13. {
  14. $this->mParam = null;
  15. if(isset($param) && is_array($param))
  16. {
  17. $this->mParam = $param;
  18. }
  19. }
  20. public function format()
  21. {
  22. $info['xianshi_id'] = intval($this->mParam['xianshi_id']);
  23. $info['xianshi_name'] = $this->mParam['xianshi_name'];
  24. $info['xianshi_title'] = $this->mParam['xianshi_title'];
  25. $info['xianshi_explain'] = $this->mParam['xianshi_explain'];
  26. $info['start_time'] = intval($this->mParam['start_time']);
  27. $info['end_time'] = intval($this->mParam['end_time']);
  28. $info['lower_limit'] = intval($this->mParam['lower_limit']);
  29. $info['state'] = intval($this->mParam['state']);
  30. //$info['goods_list'] = $this->mParam['goods_list'];
  31. return $info;
  32. }
  33. }
  34. class limitime
  35. {
  36. const XIANSHI_STATE_NORMAL = 1;
  37. const XIANSHI_STATE_CLOSE = 2;
  38. const XIANSHI_STATE_CANCEL = 3;
  39. const STORE_ID = 6;
  40. static private $stInstance = null;
  41. private $limit_list;
  42. private $mGoodsidLimitGoods;
  43. private $mLimitGoods;
  44. private $verchecker;
  45. private function __construct()
  46. {
  47. $this->verchecker = new version_checker('activity_version_limit',120);
  48. }
  49. static public function instance()
  50. {
  51. if(self::$stInstance == null) {
  52. self::$stInstance = new limitime();
  53. }
  54. if(self::$stInstance->verchecker->need_init()) {
  55. self::$stInstance->init();
  56. }
  57. return self::$stInstance;
  58. }
  59. private function init()
  60. {
  61. $mod = Model('p_xianshi');
  62. $condition = array();
  63. $condition['store_id'] = self::STORE_ID;
  64. $condition['state'] = self::XIANSHI_STATE_NORMAL;
  65. $this->limit_list = array();
  66. $this->mGoodsidLimitGoods = array();
  67. $this->mLimitGoods = [];
  68. $list = $mod->getXianshiList($condition);
  69. foreach ($list as $val)
  70. {
  71. $xianshi_id = intval($val['xianshi_id']);
  72. $this->limit_list[$xianshi_id] = $val;
  73. $this->mLimitGoods[$xianshi_id] = [];
  74. $gids = [];
  75. $goods_list = Model('p_xianshi_goods')->getXianshiGoodsList(array('xianshi_id' => $xianshi_id));
  76. foreach ($goods_list as $xianshi_goods) {
  77. $goods_id = intval($xianshi_goods['goods_id']);
  78. $this->add_limitgoods($goods_id,$xianshi_goods);
  79. $gids[] = $goods_id;
  80. }
  81. $this->mLimitGoods[$xianshi_id] = $gids;
  82. $this->limit_list[$xianshi_id]['goods_list'] = $gids;
  83. }
  84. }
  85. private function add_limitgoods($goods_id,$limit_goods)
  86. {
  87. if(isset($this->mGoodsidLimitGoods[$goods_id])) {
  88. $this->mGoodsidLimitGoods[$goods_id][] = $limit_goods;
  89. } else {
  90. $this->mGoodsidLimitGoods[$goods_id] = [];
  91. $this->mGoodsidLimitGoods[$goods_id][] = $limit_goods;
  92. }
  93. }
  94. public function isTakepart($goods_id,&$act_id)
  95. {
  96. if(isset($this->mGoodsidLimitGoods[intval($goods_id)]))
  97. {
  98. $limits = $this->mGoodsidLimitGoods[$goods_id];
  99. foreach ($limits as $limit)
  100. {
  101. $start = $limit['start_time'];
  102. $end = $limit['end_time'];
  103. $cur = time();
  104. if($cur >= intval($start) && $cur < intval($end)) {
  105. $act_id = intval($limit['xianshi_id']);
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. else {
  112. return false;
  113. }
  114. }
  115. public function get_info($act_id)
  116. {
  117. if(isset($this->limit_list[$act_id])) {
  118. $limit = $this->limit_list[$act_id];
  119. $item = new limit_item($limit);
  120. return $item->format();
  121. }
  122. else {
  123. return false;
  124. }
  125. }
  126. public function promotion_price($limit_id,$goods_id,&$price)
  127. {
  128. if(isset($this->mGoodsidLimitGoods[intval($goods_id)]))
  129. {
  130. $limit_goods = $this->mGoodsidLimitGoods[$goods_id];
  131. foreach ($limit_goods as $goods)
  132. {
  133. $id = intval($goods['xianshi_id']);
  134. if($id == $limit_id) {
  135. $price = doubleval($goods['xianshi_price']);
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. else {
  142. return false;
  143. }
  144. }
  145. public function goods($limit_id)
  146. {
  147. if(isset($this->limit_list[$limit_id])) {
  148. return $this->mLimitGoods[$limit_id];
  149. } else {
  150. return array();
  151. }
  152. }
  153. public function acting()
  154. {
  155. $limits = [];
  156. $curtm = time();
  157. foreach ($this->limit_list as $limit)
  158. {
  159. $start = intval($limit['start_time']);
  160. $end = intval($limit['end_time']);
  161. if($curtm >= $start && $curtm < $end) {
  162. $limits[] = $limit;
  163. }
  164. }
  165. $result = [];
  166. foreach ($limits as $val) {
  167. $item = new limit_item($val);
  168. $result[] = $item->format();
  169. }
  170. return $result;
  171. }
  172. public function unstart()
  173. {
  174. $limits = [];
  175. $curtm = time();
  176. foreach ($this->limit_list as $limit)
  177. {
  178. $start = intval($limit['start_time']);
  179. if($curtm < $start) {
  180. $limits[] = $limit;
  181. }
  182. }
  183. $result = [];
  184. foreach ($limits as $val) {
  185. $item = new limit_item($val);
  186. $result[] = $item->format();
  187. }
  188. return $result;
  189. }
  190. }