groupbuy.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/8/15
  6. * Time: 下午4:04
  7. */
  8. namespace activity;
  9. use commonid_helper;
  10. use StatesHelper;
  11. use Log;
  12. class group_item
  13. {
  14. private $mParam;
  15. public function __construct($param)
  16. {
  17. $this->mParam = null;
  18. if(isset($param) && is_array($param))
  19. {
  20. $this->mParam = $param;
  21. }
  22. }
  23. static private function img_url($value,$store_id)
  24. {
  25. return cthumb($value, 360, $store_id);
  26. }
  27. public function format()
  28. {
  29. $info['groupbuy_id'] = intval($this->mParam['groupbuy_id']);
  30. $info['groupbuy_name'] = $this->mParam['groupbuy_name'];
  31. $info['groupbuy_type'] = intval($this->mParam['groupbuy_type']);
  32. $info['remark'] = $this->mParam['remark'];
  33. $info['start_time'] = intval($this->mParam['start_time']);
  34. $info['end_time'] = intval($this->mParam['end_time']);
  35. $info['goods_id'] = intval($this->mParam['goods_id']);
  36. $info['promotion_price'] = doubleval($this->mParam['groupbuy_price']);
  37. $info['rebate'] = doubleval($this->mParam['groupbuy_rebate']);
  38. $info['quantity'] = intval($this->mParam['virtual_quantity']);
  39. $info['upper_limit'] = intval($this->mParam['upper_limit']);
  40. $info['buyer_count'] = intval($this->mParam['buyer_count']);
  41. $info['groupbuy_intro'] = $this->mParam['groupbuy_intro'];
  42. $info['groupbuy_image'] = self::img_url($this->mParam['groupbuy_image'],$this->mParam['store_id']);
  43. return $info;
  44. }
  45. }
  46. class groupbuy
  47. {
  48. const GROUPBUY_STATE_REVIEW = 10;
  49. const GROUPBUY_STATE_NORMAL = 20;
  50. const GROUPBUY_STATE_REVIEW_FAIL = 30;
  51. const GROUPBUY_STATE_CANCEL = 31;
  52. const GROUPBUY_STATE_CLOSE = 32;
  53. const STORE_ID = 6;
  54. const cache_name = 'all_groupbuy';
  55. static private $stInstance = null;
  56. private $group_list;
  57. private $mGoodsidGroupIDS;
  58. private function __construct()
  59. {
  60. }
  61. static public function instance()
  62. {
  63. if(self::$stInstance == null) {
  64. self::$stInstance = new groupbuy();
  65. }
  66. if(StatesHelper::fetch_state('groupbuy')) {
  67. Log::record("groupbuy reinit data.",Log::DEBUG);
  68. self::$stInstance->init();
  69. }
  70. return self::$stInstance;
  71. }
  72. private function init()
  73. {
  74. $data = $this->read_cache();
  75. $this->group_list = $data['list'];
  76. $this->mGoodsidGroupIDS = $data['ids'];
  77. }
  78. private function read_cache()
  79. {
  80. $prefix = 'mb_activity';
  81. $items = rcache(self::cache_name, $prefix);
  82. if (!empty($items)) {
  83. return unserialize($items['data']);
  84. }
  85. $group_list = [];
  86. $goodsidGroupIDS = [];
  87. $mod = Model('groupbuy');
  88. $condition = [];
  89. $condition['store_id'] = self::STORE_ID;
  90. $condition['state'] = array('in', array(self::GROUPBUY_STATE_CLOSE, self::GROUPBUY_STATE_NORMAL));
  91. $list = $mod->getGroupbuyList($condition,null,'state asc,groupbuy_type asc');
  92. foreach ($list as $val)
  93. {
  94. $goods_id = intval($val['goods_id']);
  95. $gid = intval($val['groupbuy_id']);
  96. $group_list[$gid] = self::filter($val);
  97. $common_id = commonid_helper::instance()->common_id($goods_id);
  98. $goods_ids = commonid_helper::instance()->goods_ids($common_id);
  99. if($goods_ids == false) continue;
  100. foreach ($goods_ids as $tmp_id) {
  101. self::add_goods_group($goodsidGroupIDS,$tmp_id,$gid);
  102. }
  103. }
  104. $data = array('list' => $group_list,'ids' => $goodsidGroupIDS);
  105. wcache(self::cache_name, array('data' => serialize($data)), $prefix);
  106. return $data;
  107. }
  108. private static function add_goods_group(&$goodsidGroupIDS,$goodsid,$gropupid)
  109. {
  110. if(isset($goodsidGroupIDS[$goodsid])) {
  111. $goodsidGroupIDS[$goodsid][] = $gropupid;
  112. } else {
  113. $goodsidGroupIDS[$goodsid] = [];
  114. $goodsidGroupIDS[$goodsid][] = $gropupid;
  115. }
  116. }
  117. static private function filter($groupbuy)
  118. {
  119. if(!empty($groupbuy)) {
  120. unset($groupbuy['class_id']);
  121. unset($groupbuy['s_class_id']);
  122. unset($groupbuy['is_vr']);
  123. unset($groupbuy['vr_city_id']);
  124. unset($groupbuy['is_vr']);
  125. unset($groupbuy['vr_area_id']);
  126. unset($groupbuy['vr_mall_id']);
  127. unset($groupbuy['vr_class_id']);
  128. unset($groupbuy['vr_s_class_id']);
  129. }
  130. return $groupbuy;
  131. }
  132. public function acting()
  133. {
  134. $groups = [];
  135. $curtm = time();
  136. foreach ($this->group_list as $group)
  137. {
  138. $start = intval($group['start_time']);
  139. $end = intval($group['end_time']);
  140. if($curtm >= $start && $curtm < $end) {
  141. $groups[] = $group;
  142. }
  143. }
  144. $result = [];
  145. foreach ($groups as $val) {
  146. $item = new group_item($val);
  147. $result[] = $item->format();
  148. }
  149. return $result;
  150. }
  151. public function unstart()
  152. {
  153. $groups = [];
  154. $curtm = time();
  155. foreach ($this->group_list as $group)
  156. {
  157. $start = intval($group['start_time']);
  158. if($curtm < $start) {
  159. $groups[] = $group;
  160. }
  161. }
  162. $result = [];
  163. foreach ($groups as $val) {
  164. $item = new group_item($val);
  165. $result[] = $item->format();
  166. }
  167. return $result;
  168. }
  169. public function goods($group_id)
  170. {
  171. if(isset($this->group_list[$group_id])) {
  172. $goods_id = intval($this->group_list[$group_id]['goods_id']);
  173. return array($goods_id);
  174. } else {
  175. return array();
  176. }
  177. }
  178. public function promotion_price($act_id,&$price)
  179. {
  180. $act_id = intval($act_id);
  181. if(isset($this->group_list[$act_id])) {
  182. $param = $this->group_list[$act_id];
  183. $price = doubleval($param['groupbuy_price']);
  184. return true;
  185. } else {
  186. return false;
  187. }
  188. }
  189. public function isTakepart($goods_id,&$act_id)
  190. {
  191. if(isset($this->mGoodsidGroupIDS[intval($goods_id)]))
  192. {
  193. $group_ids = $this->mGoodsidGroupIDS[$goods_id];
  194. foreach ($group_ids as $gid)
  195. {
  196. $start = intval($this->group_list[$gid]['start_time']);
  197. $end = intval($this->group_list[$gid]['end_time']);
  198. $cur = time();
  199. if($cur >= intval($start) && $cur < intval($end)) {
  200. $act_id = $gid;
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. else {
  207. return false;
  208. }
  209. }
  210. public function get_info($act_id)
  211. {
  212. $act_id = intval($act_id);
  213. if(isset($act_id,$this->group_list[$act_id])) {
  214. $param = $this->group_list[$act_id];
  215. $item = new group_item($param);
  216. return $item->format();
  217. } else {
  218. return false;
  219. }
  220. }
  221. }