|
@@ -237,6 +237,138 @@ class activity_helper
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+class price_matcher
|
|
|
|
+{
|
|
|
|
+ private $mGoodsNums;
|
|
|
|
+ private $mOptions;
|
|
|
|
+ private $mPrice;
|
|
|
|
+ private $mRepeat;
|
|
|
|
+ private $mGoodsList;
|
|
|
|
+
|
|
|
|
+ private $mResult;
|
|
|
|
+
|
|
|
|
+ public function __construct($content,$goods_list)
|
|
|
|
+ {
|
|
|
|
+ $goods_nums = $content['goods_nums'];
|
|
|
|
+ $this->mOptions = $content['options'];
|
|
|
|
+ $this->mPrice = $content['price'];
|
|
|
|
+ $this->mRepeat = $content['repeat'];
|
|
|
|
+
|
|
|
|
+ $this->mGoodsList = [];
|
|
|
|
+ $goodses = [];
|
|
|
|
+ foreach ($goods_nums as $gid => $num) {
|
|
|
|
+ $this->mGoodsList[$gid] = $goods_list[$gid];
|
|
|
|
+ $goodses[] = $goods_list[$gid];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ usort($goodses,['optional_match','price_desc']);
|
|
|
|
+
|
|
|
|
+ $this->mGoodsNums = [];
|
|
|
|
+ foreach($goodses as $val) {
|
|
|
|
+ $goods_id = intval($val['goods_id']);
|
|
|
|
+ $this->mGoodsNums[$goods_id] = $goods_nums[$goods_id];
|
|
|
|
+ }
|
|
|
|
+ $this->mResult = false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function match(&$total_price_cent)
|
|
|
|
+ {
|
|
|
|
+ if($this->mRepeat) {
|
|
|
|
+ $match_goods = $this->repeat();
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ $match_goods = $this->nonrepeat();
|
|
|
|
+ }
|
|
|
|
+ if(empty($match_goods)) {
|
|
|
|
+ $total_price_cent = 0;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ $total_count = $this->goods_count($match_goods);
|
|
|
|
+ $total_price_cent = $this->mPrice * ($total_count / $this->mOptions);
|
|
|
|
+ return $match_goods;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function repeat()
|
|
|
|
+ {
|
|
|
|
+ $match_goods = [];
|
|
|
|
+
|
|
|
|
+ $goods_num = $this->mGoodsNums;
|
|
|
|
+ while ($this->goods_count($goods_num) >= $this->mOptions)
|
|
|
|
+ {
|
|
|
|
+ $left = $this->mOptions;
|
|
|
|
+
|
|
|
|
+ foreach($goods_num as $gid => &$num)
|
|
|
|
+ {
|
|
|
|
+ if($left > $num) {
|
|
|
|
+ $sel = $num;
|
|
|
|
+ } else {
|
|
|
|
+ $sel = $left;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(array_key_exists($gid,$match_goods)) {
|
|
|
|
+ $match_goods[$gid] += $sel;
|
|
|
|
+ } else {
|
|
|
|
+ $match_goods[$gid] = $sel;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $left -= $sel;
|
|
|
|
+ $num -= $sel;
|
|
|
|
+ if($num == 0) {
|
|
|
|
+ unset($goods_num[$gid]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if($left == 0) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $match_goods;
|
|
|
|
+ }
|
|
|
|
+ private function goods_count($goods_nums)
|
|
|
|
+ {
|
|
|
|
+ $count = 0;
|
|
|
|
+ foreach ($goods_nums as $gid => $num) {
|
|
|
|
+ $count += $num;
|
|
|
|
+ }
|
|
|
|
+ return $count;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function nonrepeat()
|
|
|
|
+ {
|
|
|
|
+ $match_goods = [];
|
|
|
|
+
|
|
|
|
+ $goods_num = $this->mGoodsNums;
|
|
|
|
+ while (count($goods_num) >= $this->mOptions)
|
|
|
|
+ {
|
|
|
|
+ $i = 0;
|
|
|
|
+ foreach($goods_num as $gid => &$num)
|
|
|
|
+ {
|
|
|
|
+ if($i < $this->mOptions)
|
|
|
|
+ {
|
|
|
|
+ if(array_key_exists($gid,$match_goods)) {
|
|
|
|
+ $match_goods[$gid] += 1;
|
|
|
|
+ } else {
|
|
|
|
+ $match_goods[$gid] = 1;
|
|
|
|
+ }
|
|
|
|
+ ++$i;
|
|
|
|
+ --$num;
|
|
|
|
+ if($num == 0) {
|
|
|
|
+ unset($goods_num[$gid]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $match_goods;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
class optional_match
|
|
class optional_match
|
|
{
|
|
{
|
|
private $mPregids;
|
|
private $mPregids;
|
|
@@ -247,7 +379,6 @@ class optional_match
|
|
$this->mPregids = [];
|
|
$this->mPregids = [];
|
|
$this->mAllGoods = [];
|
|
$this->mAllGoods = [];
|
|
|
|
|
|
-
|
|
|
|
foreach ($cart_list as $cart)
|
|
foreach ($cart_list as $cart)
|
|
{
|
|
{
|
|
$bl_id = intval($cart['bl_id']);
|
|
$bl_id = intval($cart['bl_id']);
|
|
@@ -275,63 +406,26 @@ class optional_match
|
|
|
|
|
|
foreach ($matchs as $item)
|
|
foreach ($matchs as $item)
|
|
{
|
|
{
|
|
- $gids = $item['gids'];
|
|
|
|
- $options = $item['options'];
|
|
|
|
- $price = $item['price'];
|
|
|
|
-
|
|
|
|
- $num = count($gids);
|
|
|
|
- $times = intval($num / $options);
|
|
|
|
- $count = $options * $times;
|
|
|
|
-
|
|
|
|
- if($num % $options == 0) {
|
|
|
|
- $match_goods = $gids;
|
|
|
|
- } else {
|
|
|
|
- $match_goods = $this->find_goods($gids,$count);
|
|
|
|
- }
|
|
|
|
- $total_price = $this->total_price_cent($match_goods);
|
|
|
|
- $discount_cent += ($total_price - $price * $times);
|
|
|
|
-
|
|
|
|
- foreach ($match_goods as $gid)
|
|
|
|
|
|
+ $matcher = new price_matcher($item,$this->mAllGoods);
|
|
|
|
+ $match_goods = $matcher->match($total_oprice_cent);
|
|
|
|
+ if(!empty($match_goods))
|
|
{
|
|
{
|
|
- if(array_key_exists($gid,$goods_nums)) {
|
|
|
|
- $goods_nums[$gid] += 1;
|
|
|
|
- } else {
|
|
|
|
- $goods_nums[$gid] = 1;
|
|
|
|
|
|
+ $total_price = $this->total_price_cent($match_goods);
|
|
|
|
+ $discount_cent += $total_price - $total_oprice_cent;
|
|
|
|
+ foreach ($match_goods as $gid => $num) {
|
|
|
|
+ $goods_nums[$gid] = $num;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return ['discount' => $discount_cent / 100, 'goods_nums' => $goods_nums];
|
|
return ['discount' => $discount_cent / 100, 'goods_nums' => $goods_nums];
|
|
}
|
|
}
|
|
- private function find_goods($gids,$count)
|
|
|
|
- {
|
|
|
|
- $goods_list = [];
|
|
|
|
- foreach ($gids as $gid) {
|
|
|
|
- $goods_list[] = $this->mAllGoods[$gid];
|
|
|
|
- }
|
|
|
|
- usort($goods_list,['optional_match','price_desc']);
|
|
|
|
-
|
|
|
|
- $match_goods = [];
|
|
|
|
- $index = 0;
|
|
|
|
- foreach ($goods_list as $goods)
|
|
|
|
- {
|
|
|
|
- if($index < $count) {
|
|
|
|
- $gid = intval($goods['goods_id']);
|
|
|
|
- $match_goods[] = $gid;
|
|
|
|
- $index++;
|
|
|
|
- } else {
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return $match_goods;
|
|
|
|
- }
|
|
|
|
|
|
|
|
- private function total_price_cent($gids)
|
|
|
|
|
|
+ private function total_price_cent($match_goods)
|
|
{
|
|
{
|
|
$total_price = 0;
|
|
$total_price = 0;
|
|
- foreach ($gids as $gid) {
|
|
|
|
- $total_price += intval($this->mAllGoods[$gid]['goods_price'] * 100 + 0.5);
|
|
|
|
|
|
+ foreach ($match_goods as $gid => $num) {
|
|
|
|
+ $total_price += intval($this->mAllGoods[$gid]['goods_price'] * 100 * $num + 0.5);
|
|
}
|
|
}
|
|
return $total_price;
|
|
return $total_price;
|
|
}
|
|
}
|