Browse Source

持有统计

afa 5 months ago
parent
commit
ca9ddaa06f

+ 7 - 28
application/admin/controller/product/Lists.php

@@ -53,17 +53,16 @@ class Lists extends Backend
         }
 
         [$where, $sort, $order, $offset, $limit] = $this->buildparams();
-        $list = $this->model->with('products')
+        $list = $this->model->with('products')->with('productarea')->with('productareastop')->with('productorder')
             ->where($where)
             ->order($sort, $order)
             ->paginate($limit);
-        $productOrder = new ProductOrder();    
+
         foreach ($list as &$item) {
-            $item['hold_num'] = $productOrder::where('product_id', $item->id)->where('status', 'in',[$productOrder::Paid, $productOrder::Transferred, $productOrder::Freeze])->count();//持有数量
-            $item['total_num']= $this->productArea::where('product_id', $item->id)->count();
-            $item['sell_num'] = $this->productArea::where('product_id', $item->id)->where('status', $this->productArea::Stop)->count();
+            $item['hold_num'] = $item->productorder? count($item->productorder): 0;//持有数量
+            $item['total_num']= $item->productarea? count($item->productarea): 0;
+            $item['sell_num'] = $item->productareastop? count($item->productareastop): 0;
         }
-
         $result = ['total' => $list->total(), 'rows' => $list->items()];
         return json($result);
     }
@@ -165,27 +164,7 @@ class Lists extends Backend
  
 
 
-    //判断是否存在相同元素
-    private function setEqualArea(int $product_id,array $areaArr, array $areaArrTxt, int $type): bool
-    {   
-        foreach ($areaArr as $key=>$row) 
-        {
-            $arr['product_id']  = $product_id;
-            $arr['province']    = $row[0]??0;
-            $arr['city']        = $row[1]??0 ; 
-            $arr['area']        = $row[2]??0;
-            $arr['county']      = $row[3]??0;
-            $arr['address']     = $areaArrTxt[$key][0];
-            if(!empty($type)){
-                if($this->productArea::where('product_id',$arr['product_id'])
-                ->where('province',$arr['province'])
-                ->where('city',$arr['city'])->where('area',$arr['area'])
-                ->where('county',$arr['county'])
-                ->count() > 0) continue;
-            }
-            $this->productArea::create($arr);
-        }
-        return true;
-    }
+   
+
 
 }

+ 3 - 3
application/admin/controller/user/User.php

@@ -63,10 +63,10 @@ class User extends Backend
 
         [$where, $sort, $order, $offset, $limit] = $this->buildparams();
         
-        $list = $this->model->with('ledgerWallet')->where($where)->order($sort, $order)->paginate($limit);
-        $productOrder = new ProductOrder();    
+        $list = $this->model->with('ledgerWallet')->with('productorder')->where($where)->order($sort, $order)->paginate($limit);
         foreach ($list as &$item) {
-            $item['hold_num'] = $productOrder::where('user_id', $item->id)->where('status', 'in',[$productOrder::Paid, $productOrder::Transferred, $productOrder::Freeze])->count();//持有数量
+            $item['hold_num'] = $item->productorder? count($item->productorder): 0;
+            //::where('user_id', $item->id)->where('status', 'in',[$productOrder::Paid, $productOrder::Transferred, $productOrder::Freeze])->count();//持有数量
         }
         $result = ['total' => $list->total(), 'rows' => $list->items()];
         return json($result);

+ 5 - 1
application/admin/model/User.php

@@ -3,7 +3,7 @@
 namespace app\admin\model;
 
 use think\Model;
-
+use app\common\model\ProductOrder;
 
 class User extends Model
 {
@@ -37,6 +37,10 @@ class User extends Model
         return $this->hasOne('\app\admin\model\LedgerWallet', 'user_id', 'id', [],'LEFT')->setEagerlyType(0);
     }
 
+    public function productorder()
+    {
+        return $this->hasMany('\app\common\model\ProductOrder', 'user_id', 'id')->where('status', 'in', [ProductOrder::Paid, ProductOrder::Transferred, ProductOrder::Freeze])->field('id,user_id');
+    }
 
     public function getStatusList()
     {

+ 16 - 2
application/common/model/ProductLists.php

@@ -48,9 +48,21 @@ class ProductLists extends Model
     }
 
     //地区
-    public function productArea()
+    public function productarea()
     {
-        return $this->hasMany('ProductArea', 'product_id', 'id');
+        return $this->hasMany('ProductArea', 'product_id', 'id')->field('id,product_id');
+    }
+
+    //已出售地区
+    public function productareastop()
+    {
+        return $this->hasMany('ProductArea', 'product_id', 'id')->where('status', 0)->field('id,product_id');
+    }
+
+    //hold_num
+    public function productorder()
+    {
+        return $this->hasMany('ProductOrder', 'product_id', 'id')->where('status', 'in', [ProductOrder::Paid, ProductOrder::Transferred, ProductOrder::Freeze])->field('id,product_id');
     }
 
     //分类
@@ -66,6 +78,8 @@ class ProductLists extends Model
     }
 
 
+
+
     //寄售
     public function producttransfer()
     {