瀏覽代碼

优化商品列表查询

lyh 3 年之前
父節點
當前提交
53f208eaf8

+ 0 - 2
app/admin/controller/user/User.php

@@ -14,12 +14,10 @@ use app\validate\admin\user\user\ModifyMoney;
 use app\validate\admin\user\user\ModifyScore;
 use app\validate\admin\user\user\ModifyPid;
 use app\validate\admin\user\user\PhoneSet;
-use EasyAdmin\tool\CommonTool;
 use jianyan\excel\Excel;
 use think\App;
 use think\exception\ValidateException;
 use think\facade\Cache;
-use think\facade\Db;
 use app\admin\model\dao\User as UserDao;
 
 

+ 1 - 0
app/admin/logic/BoxRecordLogic.php

@@ -32,6 +32,7 @@ class BoxRecordLogic
             ->page($page, $limit)
             ->order($sort)
             ->select();
+
         return [$count, $list];
 
     }

+ 35 - 0
app/admin/logic/ShopCategoryLogic.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace app\admin\logic;
+
+use app\admin\model\dao\ShopCategory;
+use app\admin\model\dao\ShopGoodsSpec;
+use app\admin\model\dao\ShopGoodsSpecRelation;
+use app\admin\model\dao\ShopGoodsSpecType;
+use app\admin\model\dao\ShopGoodsType;
+use app\common\model\ShopGoods;
+use think\facade\Cache;
+use think\facade\Db;
+
+class ShopCategoryLogic
+{
+
+    private static $CACHE_ID_KEY = "cache:shop_category:id:";
+
+    public static function getCateById($id)
+    {
+        $key = self::$CACHE_ID_KEY . $id;
+        if (Cache::has($key)) {
+            return Cache::get($key);
+        }
+        $name = ShopCategory::getById($id);
+        Cache::set($key, $name, 10 * 60);
+        return $name;
+    }
+
+    public static function delIdCache($id)
+    {
+        $key = self::$CACHE_ID_KEY.$id;
+        Cache::delete($key);
+    }
+}

+ 9 - 2
app/admin/logic/ShopGoodsLogic.php

@@ -7,6 +7,7 @@ use app\admin\model\dao\ShopGoodsSpecRelation;
 use app\admin\model\dao\ShopGoodsSpecType;
 use app\admin\model\dao\ShopGoodsType;
 use app\common\model\ShopGoods;
+use think\db\Query;
 use think\facade\Cache;
 use think\facade\Db;
 
@@ -41,17 +42,23 @@ class ShopGoodsLogic
     {
         $model = new ShopGoods();
         $count = $model
-            ->withJoin(['supplier', 'cate'], 'LEFT')
+            ->withJoin(['supplier', 'cate' => function (Query $query) {
+                $query->where('cate.id', '>', 0);
+            }], 'LEFT')
             ->where($where)
             ->count();
 
         $list = $model
-            ->withJoin(['supplier', 'cate'], 'LEFT')
+            ->withJoin(['supplier', 'cate' => function (Query $query) {
+                $query->where('cate.id', '>', 0);
+            }], 'LEFT')
             ->where($where)
             ->page($page, $limit)
             ->order('goods_id desc')
             ->select()->toArray();
+
         foreach ($list as $k => &$v) {
+//            $v['cate'] = ShopCategoryLogic::getCateById($v['category']);
             $v['id'] = $v['goods_id'];
 //                $v['supplier'] = Db::name('shop_supplier')->where(['id'=>$v['supplier']])->value('name');
             $v['goods_type'] = ShopGoodsTypeLogic::getNameByCache($v['goods_type']);

+ 22 - 0
app/admin/model/dao/ShopCategory.php

@@ -0,0 +1,22 @@
+<?php
+
+
+namespace app\admin\model\dao;
+
+
+use app\common\model\UserModel;
+use think\facade\Db;
+
+class ShopCategory extends BaseDao
+{
+    public static $table = "db_shop_category";
+
+    public function __construct()
+    {
+    }
+
+    public static function getById($id)
+    {
+        return Db::table(self::$table)->where(['id' => $id])->find();
+    }
+}

+ 16 - 0
app/common/model/ShopCategory.php

@@ -2,9 +2,25 @@
 
 namespace app\common\model;
 
+use app\admin\logic\ShopCategoryLogic;
 use app\common\model\TimeModel;
+use think\Model;
 
 class ShopCategory extends TimeModel
 {
 
+    public static function onAfterUpdate(Model $model)
+    {
+        ShopCategoryLogic::delIdCache($model->id);
+    }
+
+    public static function onAfterInsert(Model $model): void
+    {
+        ShopCategoryLogic::delIdCache($model->id);
+    }
+
+    public static function onAfterDelete(Model $model): void
+    {
+        ShopCategoryLogic::delIdCache($model->id);
+    }
 }