dever 3 years ago
parent
commit
38f13484d9

+ 9 - 0
app/goods/database/info.php

@@ -186,6 +186,15 @@ $config = array
             'control'   => 'price_type',
         ),
 
+        'sku_id'      => array
+        (
+            'type'      => 'int-11',
+            'name'      => 'sku_id',
+            'default'   => '-1',
+            'desc'      => 'sku_id',
+            'match'     => 'is_numeric',
+        ),
+
         'price'     => array
         (
             'type'      => 'decimal-10,2',

+ 8 - 2
app/goods/lib/Info.php

@@ -246,7 +246,8 @@ class Info
         $info = Dever::db($this->table_info)->find($id);
 
         if ($info) {
-            $info['sku_id'] = 0;
+            unset($info['f_price']);
+            $sku = $sku > 0 ? $sku : $info['sku_id'];
             if ($sku > 0) {
                 $where['info_id'] = $info['id'];
                 $where['id'] = $sku;
@@ -254,6 +255,7 @@ class Info
                 if ($sku) {
                     $info['sku_id'] = $sku['id'];
                     $info['price'] = $sku['price'];
+                    $info['s_price'] = $sku['s_price'];
                     //$info['num'] = $sku['num'];
                     $info['attr'] = Dever::json_decode($sku['attr']);
 
@@ -346,6 +348,10 @@ class Info
             $info = $this->info($info, $order, false, $user, $reward);
         }
 
+        if (!isset($info['goods_id'])) {
+            $info['goods_id'] = $info['id'];
+        }
+
         if ($info['pic']) {
             $info['pic'] = explode(',', $info['pic']);
         }
@@ -377,7 +383,6 @@ class Info
     public function info($info, $key = 'list_reorder', $is_sell = false, $user = array(), $reward = false)
     {
         $info['category_array'] = Dever::load('category/api')->string($info['category']);
-        
         if ($info['price_type'] == 2) {
             
             $attr = $this->setAttr($info['category'], $key);
@@ -419,6 +424,7 @@ class Info
 
             if (isset($info['price']['min']['price'])) {
                 $info['price']['value'] = $info['price']['min']['price'];
+                $info['sku_id'] = $info['price']['min']['id'];
             }
         } else {
             $price = $info['price'];

+ 15 - 3
app/goods/lib/Sku.php

@@ -121,6 +121,8 @@ class Sku
             }
 
             $min = array();
+            $cur_sku = false;
+            $sku_id = false;
             foreach ($config['key'] as $k => $v) {
                 if (isset($config['attr'][$k])) {
                     
@@ -141,14 +143,16 @@ class Sku
                     
                     foreach ($config['col'] as $k1 => $v1) {
                         $value = Dever::input($k1);
+                        $cur_sku = false;
                         if (isset($value[$k])) {
                             $data[$k1] = $value[$k];
-
                             if ($k1 == $this->main) {
                                 if (!isset($min[$k1])) {
                                     $min = $data;
+                                    $cur_sku = true;
                                 } elseif ($min[$k1] > $data[$k1]) {
                                     $min = $data;
+                                    $cur_sku = true;
                                 }
                             }
                         }
@@ -156,9 +160,15 @@ class Sku
 
                     Dever::config('base')->hook = false;
                     if (!$info) {
-                        Dever::db($sku_table)->insert($data);
+                        $temp = Dever::db($sku_table)->insert($data);
+                        if ($cur_sku) {
+                            $sku_id = $temp;
+                        }
                     } else {
                         $data['where_id'] = $info['id'];
+                        if ($cur_sku) {
+                            $sku_id = $data['where_id'];
+                        }
                         Dever::db($sku_table)->update($data);
                     }
                 }
@@ -166,11 +176,13 @@ class Sku
 
             if (!$config['other'] && $min) {
                 $update = array();
+                if ($sku_id) {
+                    $update['sku_id'] = $sku_id;
+                }
                 foreach ($config['col'] as $k => $v) {
                     if (isset($min[$k])) {
                         $update[$k] = $min[$k];
                     }
-                    
                 }
                 $update['where_id'] = $config['goods_id'];
                 Dever::db($table)->update($update);

+ 2 - 2
app/shop/database/goods.php

@@ -21,7 +21,7 @@ return array
             # 条件,另外一个表的字段 => 本表的字段
             'where' => array('id' => 'goods_id'),
             # 要填充的数据,另外一个表的字段 => 本表的字段,如果不填写就是所有的
-            'update' => array('price_type'),
+            'update' => array('price_type' => 'price_type'),
             # 不需要填充的字段 与update互斥
             //'no' => array('id', 'seller_id', 'service_id', 'chose', 'state', 'cdate')
         )
@@ -143,7 +143,7 @@ return array
         # 列表里的按钮
         'list_button' => array
         (
-            'fast_list' => array('库存设置', '"info_sku&project=goods&goods_id={goods_id}&other=shop&other_id={shop_id}&page_type=1"'),
+            'fast_list' => array('库存设置', '"info_sku&project=goods&goods_id={goods_id}&other=shop&other_id={shop_id}&page_type=1"', 'price_type <= 2'),
         ),
     ),
 

+ 73 - 31
app/shop/lib/Info.php

@@ -50,43 +50,19 @@ class Info
         return $data;
     }
 
-    # 获取店铺的推荐商品列表
-    public function getGoods($shop, $type, $type_id = false)
+    # 获取店铺的商品列表
+    public function getGoods($shop, $column)
     {
-        if ($shop) {
-            $table = 'shop/goods';
-            $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
-        } else {
-            $table = 'goods/info';
-        }
-
-        $method = 'getTop';
-        if ($type == 1) {
-            $where['hot'] = 1;
-        } elseif ($type == 2) {
-            $where['top'] = 1;
-        } elseif ($type == 3) {
-            $where['column'] = $type_id;
-            $method = 'getData';
-        }
+        $table = 'shop/goods';
+        $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
+        $where['column'] = $column;
+        $method = 'getData';
 
         $data = Dever::db($table)->$method($where);
 
         if ($data) {
             foreach ($data as $k => $v) {
-                $data[$k] = Dever::load('goods/lib/info')->getInfo($v, false);
-
-                /*
-                $price = $data[$k]['price']['value'];
-                $data[$k]['price'] = $price;
-                if (isset($data[$k]['sell_attr'])) {
-                    unset($data[$k]['sell_attr']);
-                }
-
-                if (isset($data[$k]['attr'])) {
-                    unset($data[$k]['attr']);
-                }
-                */
+                $data[$k] = $this->getGoodsInfo($where['shop_id'], $v);
             }
         }
 
@@ -135,4 +111,70 @@ class Info
 
         return $data;
     }
+
+    # 获取库存
+    public function getGoodsInfo($shop_id, $info, $sku_id = false, $attr = true)
+    {
+        $data = Dever::load('goods/lib/info')->getInfo($info, $attr);
+
+        if($data) {
+
+            $sku_id = $sku_id ? $sku_id : $data['sku_id'];
+            $data['total'] = 0;
+            if ($data['price_type'] == 3) {
+                if (isset($data['goods']) && is_array($data['goods'])) {
+                    foreach ($data['goods'] as $k => $v) {
+                        $data['goods'][$k]['total'] = $this->getTotal($shop_id, $v['id'], -1);
+
+                        if ($data['total'] > $data['goods'][$k]['total']) {
+                            $data['total'] = $data['goods'][$k]['total'];
+                        }
+                    }
+                }
+            } else {
+                $data['total'] = $this->getTotal($shop_id, $data['goods_id'], $sku_id);
+            }
+        }
+
+        return $data;
+    }
+
+    # 验证库存
+    public function checkTotal(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
+    {
+        $info = $this->getGoodsInfo($shop_id, $goods_id, $sku_id, false);
+        if (!$info) {
+            Dever::alert('商品不存在');
+        }
+        $total = $info['total'];
+
+        if ($num > $total) {
+            if ($state == 2) {
+                Dever::alert('库存不足');
+            }
+            $num = $total;
+        }
+
+        return $total;
+    }
+
+    # 获取库存
+    public function getTotal($shop_id, $goods_id, $sku_id)
+    {
+        $total = 0;
+        $w['shop_id'] = $shop_id;
+        $w['goods_id'] = $goods_id;
+        $w['sku_id'] = $sku_id ? $sku_id : -1;
+        $sku = Dever::db('shop/goods_sku')->getOne($w);
+
+        if ($sku) {
+            $total = $sku['num'];
+        }
+
+        if (!$total) {
+            $total = 0;
+        }
+
+        return $total;
+    }
 }

+ 7 - 29
app/shop/src/Buy.php

@@ -36,7 +36,7 @@ class Buy extends Core
             Dever::alert('错误的商品');
         }
 
-        $this->data['goods'] = Dever::load('goods/lib/info')->getInfo($id);
+        $this->data['goods'] = Dever::load('shop/lib/info')->getGoodsInfo($this->shop_id, $id);
 
         //$this->data['shop'] = $this->shop;
 
@@ -73,14 +73,15 @@ class Buy extends Core
 
         $info = Dever::db('shop/cart')->find($where);
 
-        
         $where['num'] = Dever::input('num');
         if (!$info) {
+            # 验证库存
+            Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
             $state = Dever::db('shop/cart')->insert($where);
         } else {
             $where['num'] += $info['num'];
             # 验证库存
-            $this->total($where['num'], $info['goods_id'], $this->shop_id, $info['sku_id'], 2);
+            Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
             $where['where_id'] = $info['id'];
             $state = Dever::db('shop/cart')->update($where);
         }
@@ -116,9 +117,9 @@ class Buy extends Core
             if ($num <= 0) {
                 $state = Dever::db('shop/cart')->delete($where);
             } else {
-                $where['num'] = $num;
+                $where['num'] = $info['num'] + $num;
                 # 验证库存
-                $this->total($where['num'], $info['goods_id'], $this->shop_id, $info['sku_id'], 2);
+                Dever::load('shop/lib/info')->checkTotal($where['num'], $info['goods_id'], $this->shop_id, $info['sku_id'], 2);
                 $state = Dever::db('shop/cart')->update($where); 
             }
         }
@@ -285,7 +286,7 @@ class Buy extends Core
             $this->data['list'][$k]['ku_state'] = 1;
 
             # 验证是否有货
-            $total = $this->total($n, $v, $this->shop_id, $s);
+            $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $this->shop_id, $s);
 
             if ($total <= 0) {
                 $this->data['list'][$k]['ku_state'] = 2;
@@ -347,27 +348,4 @@ class Buy extends Core
 
         return $pay;
     }
-
-    # 验证库存
-    private function total(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
-    {
-        $total = 0;
-        $w['goods_id'] = $goods_id;
-        $w['shop_id'] = $shop_id;
-        $w['sku_id'] = $sku_id ? $sku_id : -1;
-        $sku = Dever::db('shop/goods_sku')->getOne($w);
-
-        if ($sku) {
-            $total = $sku['num'];
-        }
-
-        if ($num > $total) {
-            if ($state == 2) {
-                Dever::alert('库存不足');
-            }
-            $num = $total;
-        }
-
-        return $total;
-    }
 }

+ 1 - 53
app/shop/src/Main.php

@@ -41,12 +41,9 @@ class Main extends Core
         $this->data['act'] = Dever::load('push/lib/data')->get('home_act', '首页活动推荐', 3);
 
         # 获取热门商品
-        //$this->data['hot'] = Dever::load('shop/lib/info')->getGoods($this->data['shop'], 1);
-        # 获取活动推荐
         $this->data['hot'] = Dever::load('push/lib/data')->get('home_hot', '首页热门商品', 6);
 
         # 获取推荐商品
-        //$this->data['top'] = Dever::load('shop/lib/info')->getGoods($this->data['shop'], 2);
         $this->data['top'] = Dever::load('push/lib/data')->get('home_top', '首页推荐商品', 6);
 
         return $this->data;
@@ -70,55 +67,6 @@ class Main extends Core
         return $this->data;
     }
 
-    # 根据坐标,获取距离用户最近的店
-    /*
-    public function getShop()
-    {
-        $config = Dever::db('main/config')->find(1);
-        $km = $config['km'] ? $config['km'] : 5;
-
-        $lng = Dever::input('lng');
-        $lat = Dever::input('lat');
-
-        if (!$lng || !$lat) {
-            Dever::alert('请传入用户坐标');
-        }
-        $page['template'] = 'list';
-        $page['num'] = 10;
-        $sql = 'select *, round((st_distance(point(lng, lat), point('.$lng.', '.$lat.'))*111195)/1000, 2) as distance from {table} where status = 1 and state = 1 order by distance asc';
-        $data = Dever::db('shop/info')->fetchAll($sql, array(), $page);
-
-        $result = array();
-        # 5公里内没有店,状态为2
-        $result['status'] = 2;
-        $result['list'] = $data;
-        if ($data) {
-            foreach ($data as $k => $v) {
-                if ($v['distance'] <= $km) {
-                    $result['status'] = 1;
-                    if ($v['worktime']) {
-                        $time = date('Hi');
-                        $v['worktime'] = str_replace(':', '', $v['worktime']);
-                        $temp = explode('~', $v['worktime']);
-                        if ($time < $temp[0] || $time > $temp[1]) {
-                            $v['open'] = 2;
-                        }
-                    }
-                    $result['shop'] = $v;
-                    break;
-                }
-            }
-        }
-
-        if ($result['status'] == 1) {
-            $result['focus'] = Dever::db('shop/focus')->select(array('location' => 2));
-            $result['goods'] = $this->getGoods($result['shop']['id']);
-        }
-
-        return $result;
-    }
-    */
-
     # 根据店铺获取商品列表
     public function getGoods()
     {
@@ -133,7 +81,7 @@ class Main extends Core
         $data['shop'] = Dever::load('shop/lib/info')->getOne($shop_id, $lng, $lat);
         if ($column) {
             foreach ($column as $k => $v) {
-                $v['data'] = Dever::load('shop/lib/info')->getGoods($shop_id, 3, $v['id']);
+                $v['data'] = Dever::load('shop/lib/info')->getGoods($shop_id, $v['id']);
                 $data['cate'][] = $v;
             }
         }