浏览代码

修改商品模型数据

Anyon 3 年之前
父节点
当前提交
ed016b1a18

+ 4 - 2
app/data/controller/api/Goods.php

@@ -3,6 +3,8 @@
 namespace app\data\controller\api;
 
 use app\data\model\ShopGoods;
+use app\data\model\ShopGoodsCate;
+use app\data\model\ShopGoodsMark;
 use app\data\service\ExpressService;
 use app\data\service\GoodsService;
 use think\admin\Controller;
@@ -22,7 +24,7 @@ class Goods extends Controller
      */
     public function getCate()
     {
-        $this->success('获取分类成功', GoodsService::instance()->getCateTree());
+        $this->success('获取分类成功', ShopGoodsCate::getTreeData());
     }
 
     /**
@@ -30,7 +32,7 @@ class Goods extends Controller
      */
     public function getMark()
     {
-        $this->success('获取标签成功', GoodsService::instance()->getMarkData());
+        $this->success('获取标签成功', ShopGoodsMark::items());
     }
 
     /**

+ 6 - 10
app/data/controller/shop/Goods.php

@@ -5,7 +5,9 @@ namespace app\data\controller\shop;
 use app\data\model\BaseUserDiscount;
 use app\data\model\BaseUserPayment;
 use app\data\model\ShopGoods;
+use app\data\model\ShopGoodsCate;
 use app\data\model\ShopGoodsItem;
+use app\data\model\ShopGoodsMark;
 use app\data\model\ShopGoodsStock;
 use app\data\service\ExpressService;
 use app\data\service\GoodsService;
@@ -21,12 +23,6 @@ use think\admin\extend\CodeExtend;
 class Goods extends Controller
 {
     /**
-     * 最大分类等级
-     * @var integer
-     */
-    protected $cateLevel;
-
-    /**
      * 商品数据管理
      * @auth true
      * @menu true
@@ -73,8 +69,8 @@ class Goods extends Controller
      */
     protected function _page_filter(array &$data)
     {
-        $this->marks = GoodsService::instance()->getMarkData();
-        $this->cates = GoodsService::instance()->getCateTree('arr2table');
+        $this->marks = ShopGoodsMark::items();
+        $this->cates = ShopGoodsCate::getLevelData();
         GoodsService::instance()->bindData($data, false);
     }
 
@@ -139,8 +135,8 @@ class Goods extends Controller
             $data['payment'] = str2arr($data['payment'] ?? '');
             $data['cateids'] = str2arr($data['cateids'] ?? '');
             // 其他表单数据
-            $this->marks = GoodsService::instance()->getMarkData();
-            $this->cates = GoodsService::instance()->getCateData();
+            $this->marks = ShopGoodsMark::items();
+            $this->cates = ShopGoodsCate::getLevelData();
             $this->trucks = ExpressService::instance()->templates();
             $this->upgrades = UserUpgradeService::instance()->levels();
             $this->payments = BaseUserPayment::mk()->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('type,code,name', 'code');

+ 13 - 0
app/data/model/ShopGoodsCate.php

@@ -35,6 +35,19 @@ class ShopGoodsCate extends Model
     }
 
     /**
+     * 获取数据树
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getTreeData(): array
+    {
+        $query = static::mk()->where(['deleted' => 0, 'status' => 1])->order('sort desc,id desc');
+        return DataExtend::arr2tree($query->withoutField('sort,status,deleted,create_at')->select()->toArray());
+    }
+
+    /**
      * 获取列表数据
      * @param bool $simple 仅子级别
      * @return array

+ 9 - 1
app/data/model/ShopGoodsMark.php

@@ -11,5 +11,13 @@ use think\admin\Model;
  */
 class ShopGoodsMark extends Model
 {
-
+    /**
+     * 获取所有标签
+     * @return array
+     */
+    public static function items(): array
+    {
+        $map = ['status' => 1];
+        return static::mk()->where($map)->order('sort desc,id desc')->column('name');
+    }
 }

+ 3 - 56
app/data/service/GoodsService.php

@@ -8,7 +8,6 @@ use app\data\model\ShopGoodsItem;
 use app\data\model\ShopGoodsMark;
 use app\data\model\ShopGoodsStock;
 use app\data\model\ShopOrder;
-use think\admin\extend\DataExtend;
 use think\admin\Service;
 
 /**
@@ -18,58 +17,6 @@ use think\admin\Service;
  */
 class GoodsService extends Service
 {
-
-    /**
-     * 获取商品标签数据
-     * @return array
-     */
-    public function getMarkData(): array
-    {
-        $map = ['status' => 1];
-        return ShopGoodsMark::mk()->where($map)->order('sort desc,id desc')->column('name');
-    }
-
-    /**
-     * 获取分类数据
-     * @param string $type 数据格式 arr2tree | arr2table
-     * @return array
-     * @throws \think\db\exception\DataNotFoundException
-     * @throws \think\db\exception\DbException
-     * @throws \think\db\exception\ModelNotFoundException
-     */
-    public function getCateTree(string $type = 'arr2tree'): array
-    {
-        $query = ShopGoodsCate::mk()->where(['deleted' => 0, 'status' => 1])->order('sort desc,id desc');
-        return DataExtend::$type($query->withoutField('sort,status,deleted,create_at')->select()->toArray());
-    }
-
-    /**
-     * 获取分类数据
-     * @param boolean $simple 简化数据
-     * @return array
-     */
-    public function getCateData(bool $simple = true): array
-    {
-        $cates = ShopGoodsCate::mk()->where(['status' => 1, 'deleted' => 0])->column('id,pid,name', 'id');
-        foreach ($cates as $cate) if (isset($cates[$cate['pid']])) $cates[$cate['id']]['parent'] =& $cates[$cate['pid']];
-        foreach ($cates as $key => $cate) {
-            $id = $cate['id'];
-            $cates[$id]['ids'][] = $cate['id'];
-            $cates[$id]['names'][] = $cate['name'];
-            while (isset($cate['parent']) && $cate = $cate['parent']) {
-                $cates[$id]['ids'][] = $cate['id'];
-                $cates[$id]['names'][] = $cate['name'];
-            }
-            $cates[$id]['ids'] = array_reverse($cates[$id]['ids']);
-            $cates[$id]['names'] = array_reverse($cates[$id]['names']);
-            if (isset($pky) && $simple && in_array($cates[$pky]['name'], $cates[$id]['names'])) {
-                unset($cates[$pky]);
-            }
-            $pky = $key;
-        }
-        return $cates;
-    }
-
     /**
      * 更新商品库存数据
      * @param string $code
@@ -122,8 +69,9 @@ class GoodsService extends Service
      */
     public function bindData(array &$data = [], bool $simple = true): array
     {
-        [$cates, $codes] = [$this->getCateData(), array_unique(array_column($data, 'code'))];
-        $marks = ShopGoodsMark::mk()->where(['status' => 1])->column('name');
+        $marks = ShopGoodsMark::items();
+        $cates = ShopGoodsCate::getLevelData();
+        $codes = array_unique(array_column($data, 'code'));
         $items = ShopGoodsItem::mk()->whereIn('goods_code', $codes)->where(['status' => 1])->select()->toArray();
         foreach ($data as &$vo) {
             [$vo['marks'], $vo['cateids'], $vo['cateinfo']] = [str2arr($vo['marks'], ',', $marks), str2arr($vo['cateids']), []];
@@ -134,5 +82,4 @@ class GoodsService extends Service
         }
         return $data;
     }
-
 }