Browse Source

添加缓存方法

ajun 5 years ago
parent
commit
b66397622f

+ 43 - 0
app/Services/CommodityService.php

@@ -14,6 +14,12 @@ use Illuminate\Database\Eloquent\Collection;
 
 
 Class CommodityService
 Class CommodityService
 {
 {
+    /** @var CacheService $cacheService */
+    private $cacheService;
+    function __construct(){
+        $this->cacheService=app('CacheService');
+    }
+
     public function firstOrCreate($param,$column = null):Commodity{
     public function firstOrCreate($param,$column = null):Commodity{
         if ($column) return Commodity::query()->firstOrCreate($param,$column);
         if ($column) return Commodity::query()->firstOrCreate($param,$column);
         return Commodity::query()->firstOrCreate($param);
         return Commodity::query()->firstOrCreate($param);
@@ -332,4 +338,41 @@ Class CommodityService
         if ($query->count() > 0)return true;
         if ($query->count() > 0)return true;
         return false;
         return false;
     }
     }
+
+    public function getCommoditiesByMap($map)
+    {
+        /** @var OwnerService $ownerService */
+        $ownerService = app('OwnerService');
+        $owner_codes = [];
+        foreach ($map as $item) {
+            $owner_codes[$item['owner_code']] = $item['owner_code'];
+        }
+        $owners = $ownerService->getOwnerByCodes($owner_codes);
+        $owner_map = [];
+        $owners->each(function ($owner)use(&$owner_map){
+            $owner_map[$owner['code']] = $owner;
+        });
+        $collect = collect();
+        if(count($map) == 0) return $collect;
+        $date = Carbon::now();
+        foreach ($map as $item) {
+            $collect->push($this->cacheService->getOrExecute("owner_code_{$item['owner_code']}_sku_{$item['sku']}",function()use($item,$owner_map,$date){
+                $owner = $owner_map[$item['owner_code']];
+                $commodity = Commodity::query()->where('owner_id',$owner['id'])->where('sku',$item['sku'])->first();
+                if($commodity)return $commodity;
+                $basSku = OracleBasSKU::query()->where('SKU',$item['sku'])->where('CustomerID',$item['owner_code'])->first();
+                return Commodity::query()->create([
+                    'owner_id' => $owner['id'],
+                    'sku' => $basSku['sku'],
+                    'name' =>$basSku['descr_c'],
+                    'created_at' => $date,
+                    'updated_at' => $date,
+                    'length' => $basSku['skulength'],
+                    'width' => $basSku['skuwidth'],
+                    'height' => $basSku['skuhigh'],
+                    'volumn' => $basSku['cube']]);
+            },config('cache.expirations.forever')));
+        }
+        return $collect;
+    }
 }
 }

+ 17 - 0
app/Services/LogisticService.php

@@ -88,5 +88,22 @@ Class LogisticService
         return Logistic::query()->find($id);
         return Logistic::query()->find($id);
     }
     }
 
 
+    public function getLogisticByCodes($codes)
+    {
+        $collect = collect();
+        if(count($codes) == 0) return $collect;
+        foreach ($codes as $code) {
+            $collect->push($this->cacheService->getOrExecute('getLogisticByCodes_'.$code,function()use($code){
+                $logistic = Logistic::query()->where('code',$code)->first();
+                if($logistic)return $logistic;
+                $baseCustomers = OracleBasCustomer::query()
+                    ->selectRaw('Customer_Type,CustomerID,Descr_C')
+                    ->where('Customer_Type','CA')
+                    ->where('CustomerID',$code)->first();
+                return Logistic::query()->create(['name'=>$baseCustomers['descr_c'],'code'=>$baseCustomers['customerid']]);
+            }),config('cache.expirations.forever'));
+        }
+        return $collect;
+    }
 
 
 }
 }

+ 14 - 0
app/Services/OwnerService.php

@@ -216,4 +216,18 @@ Class OwnerService
         return $builder;
         return $builder;
     }
     }
 
 
+    public function getOwnerByCodes($codes)
+    {
+        $collect = collect();
+        if(count($codes) == 0)return $collect;
+        foreach ($codes as $code) {
+            $collect->push($this->cacheService->getOrExecute("getOwnerByCodes_{$code}",function ()use($code){
+                $owner = Owner::query()->where('code',$code)->first();
+                if($owner) return $owner;
+                $basCustomer = OracleBasCustomer::query()->where('Customer_Type','OW')->where('CustomerID', $code)->first();
+                return Owner::query()->create(['name'=>$basCustomer['descr_c'],'code'=>$basCustomer['customerid']]);
+            },config('cache.expirations.forever')));
+        }
+        return $collect;
+    }
 }
 }

+ 31 - 0
app/Services/ShopService.php

@@ -9,6 +9,12 @@ use Carbon\Carbon;
 
 
 class ShopService
 class ShopService
 {
 {
+    /** @var CacheService $cacheService */
+    private $cacheService;
+    function __construct(){
+        $this->cacheService=app('CacheService');
+    }
+
     public function getSelection(array $column = ['id', 'name'])
     public function getSelection(array $column = ['id', 'name'])
     {
     {
         return Shop::query()->select($column)->get();
         return Shop::query()->select($column)->get();
@@ -86,5 +92,30 @@ class ShopService
         }
         }
     }
     }
 
 
+    public function getShopByCodeMap($map)
+    {
+        /** @var OwnerService $ownerService */
+        $ownerService = app('OwnerService');
+        $owner_codes = [];
+        foreach ($map as $item) {
+            $owner_codes[$item['owner_code']]= $item['owner_code'];
+        }
+        $owners = $ownerService->getOwnerByCodes($owner_codes);
+        $owner_map  = [];
+        $owners->each(function($owner)use(&$owner_map){
+            $owner_map[$owner['code']] = $owner;
+        });
+        $collect = collect();
+        if(count($map)==0)return $collect;
+        foreach ($map as $item) {
+            $collect->push($this->cacheService->getOrExecute("getShopByCodeMap_{$item['owner_code']}_{$item['issuepartyname']}",function()use($item,$owner_map){
+                $owner = $owner_map[$item['owner_code']] ;
+                $shop = Shop::query()->where('owner_id',$owner['id'])->where('name',$item['issuepartyname'])->first();
+                if($shop)return $shop;
+                return Shop::query()->create(['owner_id'=>$owner['id'],'name'=>$item['issuepartyname']]);
+            },config('cache.expirations.forever')));
+        }
+        return $collect;
+    }
 
 
 }
 }

+ 20 - 0
app/Services/WarehouseService.php

@@ -10,6 +10,11 @@ use Carbon\Carbon;
 
 
 Class WarehouseService
 Class WarehouseService
 {
 {
+    /** @var CacheService $cacheService */
+    private $cacheService;
+    function __construct(){
+        $this->cacheService=app('CacheService');
+    }
 
 
     public function firstOrCreate(array $params, array $values = null)
     public function firstOrCreate(array $params, array $values = null)
     {
     {
@@ -99,4 +104,19 @@ Class WarehouseService
         return Warehouse::query()->insert($fillables);
         return Warehouse::query()->insert($fillables);
     }
     }
 
 
+    public function getWareHouseByCode($codes)
+    {
+        $collect = collect();
+        if(count($codes)==0)return $collect;
+        foreach ($codes as $code) {
+            $collect->push($this->cacheService->getOrExecute("WareHouse_{$code}",function()use($code){
+                $wareHouse =  Warehouse::query()->where('code',$code)->first();
+                if($wareHouse)return $wareHouse;
+                $bas_customer = OracleBasCustomer::query()->selectRaw('Customer_Type,CustomerId,Descr_C')->where('CustomerId',$code)->where('Customer_Type','WH')->first();
+                return Warehouse::query()->create(['name'=>$bas_customer['descr_c'],'code'=>$bas_customer['customerid']]);
+            }),config('cache.expirations.forever'));
+        }
+        return $collect;
+    }
 }
 }
+