activity_helper.php 12 KB

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