activity.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2016/10/19
  6. * Time: 下午4:45
  7. */
  8. require_once(BASE_ROOT_PATH . '/helper/activity_helper.php');
  9. require_once(BASE_ROOT_PATH . '/helper/goods_helper.php');
  10. class act_formater
  11. {
  12. private $mActivities;
  13. private $mGoodsActContainer;
  14. public function __construct($activities)
  15. {
  16. $this->mActivities = $activities;
  17. $this->mGoodsActContainer = [];
  18. }
  19. public function format_acting()
  20. {
  21. $specials = [];
  22. $goods_ids = [];
  23. foreach ($this->mActivities as $act)
  24. {
  25. $gids =[];
  26. if(array_key_exists('xianshi_id',$act))
  27. {
  28. $block = $this->format_limit($act,$gids);
  29. if($block != false) {
  30. $specials[]= $block;
  31. self::array_copy($goods_ids,$gids);
  32. }
  33. }
  34. elseif(array_key_exists('groupbuy_id',$act))
  35. {
  36. $block = $this->format_groupbuy($act,$gids);
  37. if($block != false) {
  38. $specials[]= $block;
  39. self::array_copy($goods_ids,$gids);
  40. }
  41. }
  42. }
  43. $goods_ids = array_unique($goods_ids);
  44. if(!empty($goods_ids))
  45. {
  46. $helper = new goods_helper();
  47. $ret = $helper->get_summary($goods_ids,$related);
  48. return array('special_list' => $specials,
  49. 'summary' => $ret['summary'],
  50. 'groupbuy' => $ret['groupbuy'],
  51. 'limitime' => $ret['limitime'],
  52. 'bundling' => $ret['bundling'],
  53. 'mobile_page' => mobile_page(1));
  54. }
  55. else {
  56. return array('special_list' => null,
  57. 'summary' => null,
  58. 'groupbuy' => null,
  59. 'limitime' => null,
  60. 'bundling' => null,
  61. 'mobile_page' => mobile_page(1));
  62. }
  63. }
  64. public function format_unstart()
  65. {
  66. $specials = [];
  67. $goods_ids = [];
  68. $group_ids = [];
  69. $limit_ids = [];
  70. foreach ($this->mActivities as $act)
  71. {
  72. $gids = [];
  73. if(array_key_exists('xianshi_id',$act))
  74. {
  75. $block = $this->format_limit($act,$gids);
  76. if($block != false) {
  77. $limit_ids[] = intval($act['xianshi_id']);
  78. $specials[]= $block;
  79. self::array_copy($goods_ids,$gids);
  80. }
  81. }
  82. elseif(array_key_exists('groupbuy_id',$act))
  83. {
  84. $block = $this->format_groupbuy($act,$gids);
  85. if($block != false) {
  86. $group_ids[] = intval($act['groupbuy_id']);
  87. $specials[]= $block;
  88. self::array_copy($goods_ids,$gids);
  89. }
  90. }
  91. else {
  92. Log::record("an error activity.",Log::ERR);
  93. }
  94. }
  95. $goods_ids = array_unique($goods_ids);
  96. if(!empty($goods_ids))
  97. {
  98. $helper = new goods_helper();
  99. $ret = $helper->get_summary($goods_ids,$related);
  100. $summarys = $ret['summary'];
  101. foreach ($summarys as &$summary)
  102. {
  103. $goods_id = $summary['goods_id'];
  104. $act = $this->mGoodsActContainer[$goods_id];
  105. $summary['act_id'] = $act['act_id'];
  106. $summary['act_type'] = $act['act_type'];
  107. if($act['act_type'] == activity_helper::ACTIVITY_GROUPBUY) {
  108. $ret = activity_helper::groupbuy_price($act['act_id'],$price);
  109. } else {
  110. $ret = activity_helper::limit_price($act['act_id'],$goods_id,$price);
  111. }
  112. if($ret == true) {
  113. $summary['goods_promotion_price'] = $price;
  114. } else {
  115. Log::record("can not get : goods_promotion_price",Log::ERR);
  116. }
  117. }
  118. $groups = [];
  119. foreach ($group_ids as $group_id)
  120. {
  121. $item = activity\groupbuy::instance()->get_info($group_id);
  122. if($item != false) {
  123. $groups[] = $item;
  124. } else {
  125. Log::record("cannot get group id={$group_id}",Log::ERR);
  126. }
  127. }
  128. $limits = [];
  129. foreach ($limit_ids as $limit_id)
  130. {
  131. $item = activity\limitime::instance()->get_info($limit_id);
  132. if($item != false) {
  133. $limits[] = $item;
  134. } else {
  135. Log::record("cannot get group id={$limit_id}",Log::ERR);
  136. }
  137. }
  138. return array('special_list' => $specials,
  139. 'summary' => $summarys,
  140. 'groupbuy' => $groups,
  141. 'limitime' => $limits,
  142. 'bundling' => $ret['bundling'],
  143. 'mobile_page' => mobile_page(1));
  144. }
  145. else {
  146. return array('special_list' => null,
  147. 'summary' => null,
  148. 'groupbuy' => null,
  149. 'limitime' => null,
  150. 'bundling' => null,
  151. 'mobile_page' => mobile_page(1));
  152. }
  153. }
  154. private function format_limit($limt, &$goods_ids)
  155. {
  156. $result =[];
  157. $limt_id = intval($limt['xianshi_id']);
  158. $result['item_title'] = strval($limt_id);
  159. $result['item_type'] = 'home_limit';
  160. $goods_ids = activity_helper::limit_goods($limt_id);
  161. if(empty($goods_ids)) return false;
  162. foreach ($goods_ids as $gid) {
  163. $item['type'] = 'goods';
  164. $item['data'] = strval($gid);
  165. $item['image'] = '';
  166. $item['title'] = '';
  167. $result['items'][] = $item;
  168. $this->mGoodsActContainer[$gid] = array('act_id' => $limt_id,
  169. 'act_type' => activity_helper::ACTIVITY_LIMITTIME);
  170. }
  171. return $result;
  172. }
  173. private function format_groupbuy($group, &$goods_ids)
  174. {
  175. $result =[];
  176. $act_id = intval($group['groupbuy_id']);
  177. $result['item_title'] = strval($act_id);
  178. $result['item_type'] = 'home_group';
  179. $goods_ids = activity_helper::groupbuy_goods($act_id);
  180. if(empty($goods_ids)) return false;
  181. foreach ($goods_ids as $gid) {
  182. $item['type'] = 'goods';
  183. $item['data'] = strval($gid);
  184. $item['image'] = '';
  185. $item['title'] = '';
  186. $result['items'][] = $item;
  187. $this->mGoodsActContainer[$gid] = array('act_id' => $act_id,
  188. 'act_type' => activity_helper::ACTIVITY_GROUPBUY,
  189. 'goods_promotion_price' => $group['promotion_price']);
  190. }
  191. return $result;
  192. }
  193. private static function array_copy(&$dest,$src)
  194. {
  195. if(empty($src)) return $dest;
  196. foreach ($src as $value) {
  197. array_push($dest,$value);
  198. }
  199. return $dest;
  200. }
  201. }
  202. class limit_outer
  203. {
  204. const WAIT_START = 0;
  205. const WAIT_END = 1;
  206. private $mAct;
  207. private $mType;
  208. public function __construct($type,$act)
  209. {
  210. $this->mAct = $act;
  211. $this->mType = $type;
  212. }
  213. public function format()
  214. {
  215. if(array_key_exists('xianshi_id',$this->mAct)) {
  216. $result = $this->format_limit();
  217. }
  218. elseif(array_key_exists('groupbuy_id',$this->mAct)) {
  219. $result = $this->format_group();
  220. }
  221. else {
  222. $result = false;
  223. }
  224. return $result;
  225. }
  226. private function format_limit()
  227. {
  228. $result = [];
  229. $act = $this->mAct;
  230. $act_id = $act['xianshi_id'];
  231. $gids = activity_helper::limit_goods($act_id);
  232. $goods_ids = array($gids[0]);
  233. $helper = new goods_helper();
  234. $ret = $helper->get_summary($goods_ids,$related);
  235. if(!empty($ret['summary']))
  236. {
  237. $summary = $ret['summary'][0];
  238. if($this->mType == limit_outer::WAIT_START) {
  239. $result['act_time'] = $act['start_time'];
  240. } else {
  241. $result['act_time'] = $act['end_time'];
  242. }
  243. $result['goods_name'] = $summary['goods_mobile_name'];
  244. $result['goods_price'] = $summary['goods_price'];
  245. $result['goods_promotion_price'] = $summary['goods_promotion_price'];
  246. $result['goods_image_url'] = $summary['goods_image_url'];
  247. $result['local_time'] = time();
  248. return $result;
  249. }
  250. else {
  251. return false;
  252. }
  253. }
  254. private function format_group()
  255. {
  256. $result = [];
  257. $act = $this->mAct;
  258. $act_id = $act['groupbuy_id'];
  259. $gids = activity_helper::groupbuy_goods($act_id);
  260. $goods_ids = array($gids[0]);
  261. $helper = new goods_helper();
  262. $ret = $helper->get_summary($goods_ids,$related);
  263. if(!empty($ret['summary']))
  264. {
  265. $summary = $ret['summary'][0];
  266. if($this->mType == limit_outer::WAIT_START) {
  267. $result['act_time'] = $act['start_time'];
  268. } else {
  269. $result['act_time'] = $act['end_time'];
  270. }
  271. $result['goods_name'] = $summary['goods_mobile_name'];
  272. $result['goods_price'] = $summary['goods_price'];
  273. $result['goods_promotion_price'] = $act['promotion_price'];
  274. $result['goods_image_url'] = $summary['goods_image_url'];
  275. $result['local_time'] = time();
  276. return $result;
  277. }
  278. else {
  279. return false;
  280. }
  281. }
  282. }
  283. class activityControl extends mobileControl
  284. {
  285. public function __construct()
  286. {
  287. parent::__construct();
  288. }
  289. public function actingOp()
  290. {
  291. $actings = activity_helper::acting();
  292. $formater = new act_formater($actings);
  293. $result = $formater->format_acting();
  294. return self::outsuccess($result);
  295. }
  296. public function unstartOp()
  297. {
  298. $actings = activity_helper::unstart();
  299. $formater = new act_formater($actings);
  300. $result = $formater->format_unstart();
  301. return self::outsuccess($result);
  302. }
  303. public function limit_entraOp()
  304. {
  305. $_SESSION['client_type'] = "wap";
  306. $acts = activity_helper::acting();
  307. if(!empty($acts)) {
  308. $act = $acts[0];
  309. $outer = new limit_outer(limit_outer::WAIT_END,$act);
  310. $result = $outer->format();
  311. return self::outsuccess($result,"activity/limit_entra");
  312. }
  313. $unstarts = activity_helper::unstart();
  314. if(!empty($unstarts)) {
  315. $act = $unstarts[0];
  316. $outer = new limit_outer(limit_outer::WAIT_START,$act);
  317. $result = $outer->format();
  318. return self::outsuccess($result,"activity/limit_entra");
  319. }
  320. return self::outsuccess(null,"activity/limit_entra");
  321. }
  322. }