activity_helper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 optional_goods_check($goods_id,&$err)
  199. {
  200. if(self::take_parted($goods_id,$type)) {
  201. $err['msg'] = '该商品已经参加其它活动';
  202. return false;
  203. }
  204. elseif(self::have_bundling($goods_id)) {
  205. $err['msg'] = '该商品已经参加套装活动';
  206. return false;
  207. }
  208. return true;
  209. }
  210. public static function optional_goods_match($gids)
  211. {
  212. $result = activity\optional_goods::instance()->match($gids);
  213. return $result;
  214. }
  215. }
  216. class price_matcher
  217. {
  218. private $mGoodsNums;
  219. private $mOptions;
  220. private $mPrice;
  221. private $mRepeat;
  222. private $mGoodsList;
  223. private $mResult;
  224. public function __construct($content,$goods_list)
  225. {
  226. $goods_nums = $content['goods_nums'];
  227. $this->mOptions = $content['options'];
  228. $this->mPrice = $content['price'];
  229. $this->mRepeat = $content['repeat'];
  230. $this->mGoodsList = [];
  231. $goodses = [];
  232. foreach ($goods_nums as $gid => $num) {
  233. $this->mGoodsList[$gid] = $goods_list[$gid];
  234. $goodses[] = $goods_list[$gid];
  235. }
  236. usort($goodses,['optional_match','price_desc']);
  237. $this->mGoodsNums = [];
  238. foreach($goodses as $val) {
  239. $goods_id = intval($val['goods_id']);
  240. $this->mGoodsNums[$goods_id] = $goods_nums[$goods_id];
  241. }
  242. $this->mResult = false;
  243. }
  244. public function match(&$total_price_cent)
  245. {
  246. if($this->mRepeat) {
  247. $match_goods = $this->repeat();
  248. }
  249. else {
  250. $match_goods = $this->nonrepeat();
  251. }
  252. if(empty($match_goods)) {
  253. $total_price_cent = 0;
  254. return false;
  255. }
  256. else {
  257. $total_count = $this->goods_count($match_goods);
  258. $total_price_cent = $this->mPrice * ($total_count / $this->mOptions);
  259. return $match_goods;
  260. }
  261. }
  262. private function repeat()
  263. {
  264. $match_goods = [];
  265. $goods_num = $this->mGoodsNums;
  266. while ($this->goods_count($goods_num) >= $this->mOptions)
  267. {
  268. $left = $this->mOptions;
  269. foreach($goods_num as $gid => &$num)
  270. {
  271. if($left > $num) {
  272. $sel = $num;
  273. } else {
  274. $sel = $left;
  275. }
  276. if(array_key_exists($gid,$match_goods)) {
  277. $match_goods[$gid] += $sel;
  278. } else {
  279. $match_goods[$gid] = $sel;
  280. }
  281. $left -= $sel;
  282. $num -= $sel;
  283. if($num == 0) {
  284. unset($goods_num[$gid]);
  285. }
  286. if($left == 0) {
  287. break;
  288. }
  289. }
  290. }
  291. return $match_goods;
  292. }
  293. private function goods_count($goods_nums)
  294. {
  295. $count = 0;
  296. foreach ($goods_nums as $gid => $num) {
  297. $count += $num;
  298. }
  299. return $count;
  300. }
  301. private function nonrepeat()
  302. {
  303. $match_goods = [];
  304. $goods_num = $this->mGoodsNums;
  305. while (count($goods_num) >= $this->mOptions)
  306. {
  307. $i = 0;
  308. foreach($goods_num as $gid => &$num)
  309. {
  310. if($i < $this->mOptions)
  311. {
  312. if(array_key_exists($gid,$match_goods)) {
  313. $match_goods[$gid] += 1;
  314. } else {
  315. $match_goods[$gid] = 1;
  316. }
  317. ++$i;
  318. --$num;
  319. if($num == 0) {
  320. unset($goods_num[$gid]);
  321. }
  322. }
  323. else {
  324. break;
  325. }
  326. }
  327. }
  328. return $match_goods;
  329. }
  330. }
  331. class optional_match
  332. {
  333. private $mPregids;
  334. private $mAllGoods;
  335. public function __construct($cart_list)
  336. {
  337. $this->mPregids = [];
  338. $this->mAllGoods = [];
  339. foreach ($cart_list as $cart)
  340. {
  341. $bl_id = intval($cart['bl_id']);
  342. $goods_id = intval($cart['goods_id']);
  343. $goods_num = intval($cart['goods_num']);
  344. if($bl_id == 0 && $goods_id > 0)
  345. {
  346. $this->mPregids[$goods_id] = $goods_num;
  347. $this->mAllGoods[$goods_id] = $cart;
  348. }
  349. }
  350. }
  351. public function match()
  352. {
  353. if(!empty($this->mPregids)) {
  354. $matchs = activity\optional_goods::instance()->match($this->mPregids);
  355. }
  356. else {
  357. return false;
  358. }
  359. $discount_cent = 0;
  360. $goods_nums = [];
  361. foreach ($matchs as $item)
  362. {
  363. $matcher = new price_matcher($item,$this->mAllGoods);
  364. $match_goods = $matcher->match($total_oprice_cent);
  365. if(!empty($match_goods))
  366. {
  367. $total_price = $this->total_price_cent($match_goods);
  368. $discount_cent += $total_price - $total_oprice_cent;
  369. foreach ($match_goods as $gid => $num) {
  370. $goods_nums[$gid] = $num;
  371. }
  372. }
  373. }
  374. return ['discount' => $discount_cent / 100, 'goods_nums' => $goods_nums];
  375. }
  376. private function total_price_cent($match_goods)
  377. {
  378. $total_price = 0;
  379. foreach ($match_goods as $gid => $num) {
  380. $total_price += intval($this->mAllGoods[$gid]['goods_price'] * 100 * $num + 0.5);
  381. }
  382. return $total_price;
  383. }
  384. public static function price_desc($left,$right)
  385. {
  386. $l_price = intval($left['goods_price'] * 100 + 0.5);
  387. $r_price = intval($right['goods_price'] * 100 + 0.5);
  388. if($l_price > $r_price) return -1;
  389. elseif($l_price < $r_price) return 1;
  390. else return 0;
  391. }
  392. }