activity.php 11 KB

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