activity_helper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/8/14
  6. * Time: 下午5:27
  7. */
  8. require_once (BASE_ROOT_PATH . '/helper/activity/groupbuy.php');
  9. require_once (BASE_ROOT_PATH . '/helper/activity/limitime.php');
  10. require_once (BASE_ROOT_PATH . '/helper/activity/bundling.php');
  11. require_once (BASE_ROOT_PATH . '/helper/activity/recommend_combo.php');
  12. require_once (BASE_ROOT_PATH . '/helper/activity/full_sent.php');
  13. require_once (BASE_ROOT_PATH . '/helper/activity/recommend_goods.php');
  14. require_once (BASE_ROOT_PATH . '/helper/activity/goods_gift.php');
  15. require_once (BASE_ROOT_PATH . '/helper/activity/optional_goods.php');
  16. require_once (BASE_ROOT_PATH . '/helper/activity/goods_sampler.php');
  17. require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
  18. require_once (BASE_ROOT_PATH . '/helper/message/msgutil.php');
  19. require_once (BASE_ROOT_PATH . '/helper/activity_helper.php');
  20. function less_endtime($left,$right)
  21. {
  22. $l_end = intval($left['end_time']);
  23. $r_end = intval($right['end_time']);
  24. $l_type = intval($left['time_type']);
  25. $r_type = intval($right['time_type']);
  26. $l_actype = $left['act_type'];
  27. $r_actype = $left['act_type'];
  28. if($l_end > $r_end) {
  29. return 1;
  30. }
  31. elseif($l_end == $r_end)
  32. {
  33. if($l_type > $r_type) return 1;
  34. elseif($l_type < $r_type) return -1;
  35. else
  36. {
  37. if($l_actype > $r_actype) return 1;
  38. elseif($l_actype < $r_actype) return -1;
  39. else return 0;
  40. }
  41. }
  42. else {
  43. return -1;
  44. }
  45. }
  46. function less_startime($left,$right)
  47. {
  48. $l_start = intval($left['start_time']);
  49. $r_start = intval($right['start_time']);
  50. $l_type = intval($left['time_type']);
  51. $r_type = intval($right['time_type']);
  52. $l_actype = $left['act_type'];
  53. $r_actype = $left['act_type'];
  54. if($l_start > $r_start) {
  55. return 1;
  56. }
  57. elseif($l_start == $r_start) {
  58. if($l_type > $r_type) return 1;
  59. elseif($l_type < $r_type) return -1;
  60. else
  61. {
  62. if($l_actype > $r_actype) return 1;
  63. elseif($l_actype < $r_actype) return -1;
  64. else return 0;
  65. }
  66. }
  67. else {
  68. return -1;
  69. }
  70. }
  71. //抢,限,推,套装,三个信息会出现在商品的summary 中,其余的出现在商品的详细信息中
  72. class activity_helper
  73. {
  74. const ACTIVITY_NONE = 0;
  75. const ACTIVITY_GROUPBUY = 1; //抢
  76. const ACTIVITY_LIMITTIME = 2; //限
  77. const ACTIVITY_BUNDLING = 3; //套装
  78. public static function take_parted($goods_id,&$act_type)
  79. {
  80. if(activity\groupbuy::instance()->isTakepart($goods_id,$act_id)) { //抢购
  81. $act_type = self::ACTIVITY_GROUPBUY;
  82. return $act_id;
  83. }
  84. elseif(activity\limitime::instance()->isTakepart($goods_id,$act_id)) { //
  85. $act_type = self::ACTIVITY_LIMITTIME;
  86. return $act_id;
  87. }
  88. else {
  89. $act_type = self::ACTIVITY_NONE;
  90. return false;
  91. }
  92. }
  93. public static function have_recommend($goods_id)
  94. {
  95. if (activity\recommend_goods::instance()->isTakepart($goods_id)) {
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. }
  101. public static function have_bundling($goods_id) {
  102. return activity\bundling::instance()->have_bundling($goods_id);
  103. }
  104. public static function bundling_goods($bl_id) {
  105. return activity\bundling::instance()->bundling_goods($bl_id);
  106. }
  107. //赠品信息
  108. public static function have_gift($goods_id) {
  109. return activity\goods_gift::instance()->have_gift($goods_id);
  110. }
  111. public static function goods_gifts($goods_id) {
  112. return activity\goods_gift::instance()->gifts($goods_id);
  113. }
  114. public static function goods_giftids($goods_id) {
  115. return activity\goods_gift::instance()->goods_giftids($goods_id);
  116. }
  117. //商品组合
  118. public static function have_combo($goods_id) {
  119. return activity\recommend_combo::instance()->have_combo($goods_id);
  120. }
  121. public static function combo_goods($goods_id) {
  122. return activity\recommend_combo::instance()->combo_goods($goods_id);
  123. }
  124. //满赠活动
  125. public static function fullsent_rules() {
  126. return activity\full_sent::instance()->rules();
  127. }
  128. //包邮信息
  129. public static function free_ship() {
  130. return activity\full_sent::instance()->free_price();
  131. }
  132. //推荐商品列表
  133. public static function recomoned_goodsids()
  134. {
  135. return activity\recommend_goods::instance()->goods_ids();
  136. }
  137. public static function limit_goods($limit_id)
  138. {
  139. return activity\limitime::instance()->goods($limit_id);
  140. }
  141. public static function groupbuy_goods($group_id)
  142. {
  143. return activity\groupbuy::instance()->goods($group_id);
  144. }
  145. public static function acting()
  146. {
  147. $limits = activity\limitime::instance()->acting();
  148. $groupbuys = activity\groupbuy::instance()->acting();
  149. $values = [];
  150. foreach ($limits as $val) {
  151. $val['time_type'] = $val['xianshi_type'];
  152. $val['act_type'] = 1;
  153. $values[] = $val;
  154. }
  155. foreach ($groupbuys as $val) {
  156. $val['time_type'] = $val['groupbuy_type'];
  157. $val['act_type'] = 2;
  158. $values[] = $val;
  159. }
  160. $result = [];
  161. if(!empty($values))
  162. {
  163. uasort($values,'less_endtime');
  164. foreach ($values as $value) {
  165. $result[] = $value;
  166. }
  167. }
  168. return $result;
  169. }
  170. public static function groupbuy_price($limit_id,&$price)
  171. {
  172. return activity\groupbuy::instance()->promotion_price($limit_id,$price);
  173. }
  174. public static function limit_price($limit_id,$goods_id,&$price)
  175. {
  176. return activity\limitime::instance()->promotion_price($limit_id,$goods_id,$price);
  177. }
  178. public static function unstart()
  179. {
  180. $limits = activity\limitime::instance()->unstart();
  181. $groupbuys = activity\groupbuy::instance()->unstart();
  182. $values = [];
  183. foreach ($limits as $val) {
  184. $values[] = $val;
  185. }
  186. foreach ($groupbuys as $val) {
  187. $values[] = $val;
  188. }
  189. $result = [];
  190. if(!empty($values))
  191. {
  192. uasort($values,'less_startime');
  193. foreach ($values as $value) {
  194. $result[] = $value;
  195. }
  196. }
  197. return $result;
  198. }
  199. public static function take_part_opgoods($goods_id)
  200. {
  201. return activity\optional_goods::instance()->info($goods_id);
  202. }
  203. public static function optional_goods_check($goods_id,&$err)
  204. {
  205. if(self::take_parted($goods_id,$type)) {
  206. $err['msg'] = '该商品已经参加其它活动';
  207. return false;
  208. }
  209. elseif(self::have_bundling($goods_id)) {
  210. $err['msg'] = '该商品已经参加套装活动';
  211. return false;
  212. }
  213. return true;
  214. }
  215. public static function optional_goods_match($gids)
  216. {
  217. $result = activity\optional_goods::instance()->match($gids);
  218. return $result;
  219. }
  220. }
  221. class price_matcher
  222. {
  223. private $mGoodsNums;
  224. private $mOptions;
  225. private $mPrice;
  226. private $mRepeat;
  227. private $mGoodsList;
  228. private $mResult;
  229. public function __construct($content,$goods_list)
  230. {
  231. $goods_nums = $content['goods_nums'];
  232. $this->mOptions = $content['options'];
  233. $this->mPrice = $content['price'];
  234. $this->mRepeat = $content['repeat'];
  235. $this->mGoodsList = [];
  236. $goodses = [];
  237. foreach ($goods_nums as $gid => $num) {
  238. $this->mGoodsList[$gid] = $goods_list[$gid];
  239. $goodses[] = $goods_list[$gid];
  240. }
  241. usort($goodses,['optional_match','price_desc']);
  242. $this->mGoodsNums = [];
  243. foreach($goodses as $val) {
  244. $goods_id = intval($val['goods_id']);
  245. $this->mGoodsNums[$goods_id] = $goods_nums[$goods_id];
  246. }
  247. $this->mResult = false;
  248. }
  249. public function match(&$total_price_cent)
  250. {
  251. if($this->mRepeat) {
  252. $match_goods = $this->repeat();
  253. }
  254. else {
  255. $match_goods = $this->nonrepeat();
  256. }
  257. if(empty($match_goods)) {
  258. $total_price_cent = 0;
  259. return false;
  260. }
  261. else {
  262. $total_count = $this->goods_count($match_goods);
  263. $total_price_cent = $this->mPrice * ($total_count / $this->mOptions);
  264. return $match_goods;
  265. }
  266. }
  267. private function repeat()
  268. {
  269. $match_goods = [];
  270. $goods_num = $this->mGoodsNums;
  271. while ($this->goods_count($goods_num) >= $this->mOptions)
  272. {
  273. $left = $this->mOptions;
  274. foreach($goods_num as $gid => &$num)
  275. {
  276. if($left > $num) {
  277. $sel = $num;
  278. } else {
  279. $sel = $left;
  280. }
  281. if(array_key_exists($gid,$match_goods)) {
  282. $match_goods[$gid] += $sel;
  283. } else {
  284. $match_goods[$gid] = $sel;
  285. }
  286. $left -= $sel;
  287. $num -= $sel;
  288. if($num == 0) {
  289. unset($goods_num[$gid]);
  290. }
  291. if($left == 0) {
  292. break;
  293. }
  294. }
  295. }
  296. return $match_goods;
  297. }
  298. private function goods_count($goods_nums)
  299. {
  300. $count = 0;
  301. foreach ($goods_nums as $gid => $num) {
  302. $count += $num;
  303. }
  304. return $count;
  305. }
  306. private function nonrepeat()
  307. {
  308. $match_goods = [];
  309. $goods_num = $this->mGoodsNums;
  310. while (count($goods_num) >= $this->mOptions)
  311. {
  312. $i = 0;
  313. foreach($goods_num as $gid => &$num)
  314. {
  315. if($i < $this->mOptions)
  316. {
  317. if(array_key_exists($gid,$match_goods)) {
  318. $match_goods[$gid] += 1;
  319. } else {
  320. $match_goods[$gid] = 1;
  321. }
  322. ++$i;
  323. --$num;
  324. if($num == 0) {
  325. unset($goods_num[$gid]);
  326. }
  327. }
  328. else {
  329. break;
  330. }
  331. }
  332. }
  333. return $match_goods;
  334. }
  335. }
  336. class optional_match
  337. {
  338. private $mPregids;
  339. private $mAllGoods;
  340. public function __construct($cart_list)
  341. {
  342. $this->mPregids = [];
  343. $this->mAllGoods = [];
  344. foreach ($cart_list as $cart)
  345. {
  346. $bl_id = intval($cart['bl_id']);
  347. $goods_id = intval($cart['goods_id']);
  348. $goods_num = intval($cart['goods_num']);
  349. if($bl_id == 0 && $goods_id > 0)
  350. {
  351. $this->mPregids[$goods_id] = $goods_num;
  352. $this->mAllGoods[$goods_id] = $cart;
  353. }
  354. }
  355. }
  356. public function match()
  357. {
  358. if(!empty($this->mPregids)) {
  359. $matchs = activity\optional_goods::instance()->match($this->mPregids);
  360. if($matchs == false) return false;
  361. }
  362. else {
  363. return false;
  364. }
  365. $discount_cent = 0;
  366. $goods_nums = [];
  367. $total_cent = 0;
  368. foreach ($matchs as $item)
  369. {
  370. $matcher = new price_matcher($item,$this->mAllGoods);
  371. $match_goods = $matcher->match($total_oprice_cent);
  372. if(!empty($match_goods))
  373. {
  374. $total_price = $this->total_price_cent($match_goods);
  375. $total_cent += $total_price;
  376. $discount_cent += $total_price - $total_oprice_cent;
  377. foreach ($match_goods as $gid => $num) {
  378. $goods_nums[$gid] = $num;
  379. }
  380. }
  381. }
  382. return ['total_amount' => $total_cent / 100,'discount' => $discount_cent / 100, 'goods_nums' => $goods_nums];
  383. }
  384. private function total_price_cent($match_goods)
  385. {
  386. $total_price = 0;
  387. foreach ($match_goods as $gid => $num) {
  388. $total_price += intval($this->mAllGoods[$gid]['goods_price'] * 100 * $num + 0.5);
  389. }
  390. return $total_price;
  391. }
  392. public static function price_desc($left,$right)
  393. {
  394. $l_price = intval($left['goods_price'] * 100 + 0.5);
  395. $r_price = intval($right['goods_price'] * 100 + 0.5);
  396. if($l_price > $r_price) return -1;
  397. elseif($l_price < $r_price) return 1;
  398. else return 0;
  399. }
  400. }