浏览代码

修改N元任选,支持商品同价多规格

stanley-king 7 年之前
父节点
当前提交
46a90d0c31
共有 1 个文件被更改,包括 89 次插入47 次删除
  1. 89 47
      helper/activity/optional_goods.php

+ 89 - 47
helper/activity/optional_goods.php

@@ -11,6 +11,7 @@ namespace activity;
 use algorithm;
 use StatesHelper;
 use Log;
+use commonid_helper;
 
 //M元买N件商品活动
 class opgroup
@@ -127,7 +128,8 @@ class optional_goods
 
     static private $stInstance = null;
 
-    private function __construct() {
+    private function __construct()
+    {
         $this->mOpgroups = [];
         $this->mAllGoods = [];
         $this->mSpecials = [];
@@ -136,34 +138,36 @@ class optional_goods
 
     static public function instance()
     {
-        if(self::$stInstance == null) {
+        if (self::$stInstance == null) {
             self::$stInstance = new optional_goods();
         }
-        if(StatesHelper::fetch_state('optional_goods')) {
-            Log::record("optional_goods reinit data.",Log::DEBUG);
+        if (StatesHelper::fetch_state('optional_goods')) {
+            Log::record("optional_goods reinit data.", Log::DEBUG);
             self::$stInstance->init();
         }
         return self::$stInstance;
     }
+
     public function layout_error()
     {
-        if(empty($this->mErrGoods)) {
+        if (empty($this->mErrGoods)) {
             return false;
-        }
-        else {
+        } else {
             return $this->mErrGoods;
         }
     }
-    public function all_goods() {
+
+    public function all_goods()
+    {
         return $this->mAllGoods;
     }
 
     public function exist_goods($gid)
     {
         $gid = intval($gid);
-        if($gid <= 0) return false;
+        if ($gid <= 0) return false;
 
-        if(algorithm::binary_search($this->mAllGoods,$gid)) {
+        if (algorithm::binary_search($this->mAllGoods, $gid)) {
             return true;
         } else {
             return false;
@@ -173,20 +177,17 @@ class optional_goods
     public function info($goods_id)
     {
         $gid = intval($goods_id);
-        if($gid <= 0) return false;
+        if ($gid <= 0) return false;
 
-        if(algorithm::binary_search($this->mAllGoods,$gid))
-        {
-            foreach ($this->mOpgroups as $group)
-            {
+        if (algorithm::binary_search($this->mAllGoods, $gid)) {
+            foreach ($this->mOpgroups as $group) {
                 $info = $group->info($gid);
-                if($info != false) {
+                if ($info != false) {
                     return $info;
                 }
             }
             return false;
-        }
-        else {
+        } else {
             return false;
         }
     }
@@ -194,19 +195,17 @@ class optional_goods
     public function match($gidnums)
     {
         $input_goods = $this->input_goods($gidnums);
-        $match_goods = algorithm::set_intersection($this->mAllGoods,$input_goods);
-        if(empty($match_goods)) return false;
+        $match_goods = algorithm::set_intersection($this->mAllGoods, $input_goods);
+        if (empty($match_goods)) return false;
 
-        $match_goods = $this->sel_goods($match_goods,$gidnums);
+        $match_goods = $this->sel_goods($match_goods, $gidnums);
 
         $result = [];
-        foreach ($this->mOpgroups as $opgroup)
-        {
-            if(empty($match_goods)) break;
+        foreach ($this->mOpgroups as $opgroup) {
+            if (empty($match_goods)) break;
 
             $part = $opgroup->match($match_goods);
-            if($part != false)
-            {
+            if ($part != false) {
                 $result[] = $part;
                 $goods_nums = $part['goods_nums'];
                 foreach ($goods_nums as $gid => $num) {
@@ -217,6 +216,7 @@ class optional_goods
 
         return $result;
     }
+
     private function sel_goods($gids, $gidnums)
     {
         $result = [];
@@ -230,7 +230,7 @@ class optional_goods
     {
         $result = [];
         foreach ($gidnums as $gid => $num) {
-            if($num > 0) {
+            if ($num > 0) {
                 $result[] = $gid;
             }
         }
@@ -242,14 +242,15 @@ class optional_goods
     {
         global $config;
         $opids = $config['optional_goods'];
-        if(empty($opids)) return true;
+        if (empty($opids)) return true;
         $this->mSpecials = array_unique($opids);
 
-        foreach($this->mSpecials as $special_id) {
+        foreach ($this->mSpecials as $special_id) {
             $this->add_special($special_id);
         }
         sort($this->mErrGoods);
     }
+
     private function add_special($special_id)
     {
         $mod_special = Model('mb_special');
@@ -257,44 +258,85 @@ class optional_goods
         $blocks = $ret['blocks'];
 
         $cur_group = null;
-        foreach ($blocks as $block)
-        {
+        foreach ($blocks as $block) {
             $type = $block['item_type'];
-            if($type == 'home1')
-            {
-                $tmp_group = $this->add_group($block,$special_id);
-                if($tmp_group != null) {
+            if ($type == 'home1') {
+                $tmp_group = $this->add_group($block, $special_id);
+                if ($tmp_group != null) {
                     $cur_group = $tmp_group;
                     $this->mOpgroups[] = $cur_group;
                 }
-            }
-            else
-            {
-                $this->add_goods($cur_group,$block);
+            } else {
+                $this->add_goods($cur_group, $block);
             }
         }
     }
-    private function add_goods($group,$block)
+
+    private function add_goods($group, $block)
     {
         $items = $block['items'];
-        if(empty($items)) return;
+        if (empty($items)) return;
 
         foreach ($items as $item)
         {
-            if(array_key_exists('type',$item) && $item['type'] == 'goods')
+            if (array_key_exists('type', $item) && $item['type'] == 'goods')
             {
                 $goods_id = intval($item['data']);
-                if($group != null && $goods_id > 0 && $this->exist_goods($goods_id) == false)
+
+                $spugids = $this->spu_goods($goods_id);
+                if($spugids == false) continue;
+
+                foreach ($spugids as $goods_id)
                 {
-                    if($group->add_goods($goods_id)) {
-                        $this->addgoods($goods_id);
+                    if ($group != null && $goods_id > 0 && $this->exist_goods($goods_id) == false) {
+                        if ($group->add_goods($goods_id)) {
+                            $this->addgoods($goods_id);
+                        }
+                    } else {
+                        $this->mErrGoods[] = $goods_id;
                     }
                 }
-                else {
-                    $this->mErrGoods[] = $goods_id;
+            }
+        }
+    }
+
+    private function spu_goods($gid)
+    {
+        $cid = commonid_helper::instance()->common_id($gid);
+        if($cid != false)
+        {
+            $gids = commonid_helper::instance()->goods_ids($cid);
+            if($gids == false) return false;
+
+            if(count($gids) == 1) {
+                return $gids;
+            }
+            else
+            {
+                $goods_map = [];
+
+                $mod_goods = Model('goods');
+                $goodses = $mod_goods->getGoodsOnlineList(array('goods_id' => array('in', $gids)));
+                foreach ($goodses as $goods) {
+                    $gid = intval($goods['goods_id']);
+                    $goods_map[$gid] = $goods['goods_price'];
                 }
+                $goods_price = $goods_map[$gid];
+
+                $result = [];
+                foreach ($goods_map as $goods_id => $price)
+                {
+                    if($price == $goods_price) {
+                        $result[] = $goods_id;
+                    }
+                }
+
+                return empty($result) ? false : $result;
             }
         }
+        else {
+            return false;
+        }
     }
 
     private function add_group($block,$special_id)