Просмотр исходного кода

asn同步测试,商品添加至缓存中

haozi 5 лет назад
Родитель
Сommit
4f83a8b315

+ 50 - 0
app/Console/Commands/SyncWmsCommoditiesInformation.php

@@ -0,0 +1,50 @@
+<?php
+
+namespace App\Console\Commands;
+
+
+use App\Services\CommodityService;
+use Illuminate\Console\Command;
+
+class SyncWmsCommoditiesInformation extends Command
+{
+
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'SyncWmsCommoditiesInformation';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '同步WMS的商品信息';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $this->SyncWmsCommoditiesInformation();
+    }
+
+    public function SyncWmsCommoditiesInformation(){
+        /** @var CommodityService $commodityService */
+        $commodityService  = app(CommodityService::class);
+    }
+}

+ 1 - 4
app/Console/Commands/WasSyncWmsAsnInformation.php

@@ -2,12 +2,9 @@
 
 namespace App\Console\Commands;
 
-use App\Services\LogService;
-use App\Services\OrderTrackingService;
+
 use App\Services\StoreService;
 use Illuminate\Console\Command;
-use Illuminate\Support\Carbon;
-use Illuminate\Support\Facades\Auth;
 
 class WasSyncWmsAsnInformation extends Command
 {

+ 8 - 0
app/Http/Controllers/TestController.php

@@ -32,6 +32,7 @@ use App\ProcessStatistic;
 use App\RejectedBill;
 use App\RejectedBillItem;
 use App\Services\CacheService;
+use App\Services\CommodityService;
 use App\Services\common\BatchUpdateService;
 use App\Services\common\DataHandlerService;
 use App\Services\FeatureService;
@@ -1162,4 +1163,11 @@ where (commodities.owner_id,commodity_barcodes.code) in (select commodities.owne
         $storeService->storeCreateByWms();
         $storeService->storeUpdateByWms();
     }
+    public function testCommodity(){
+        /**
+         * @var CommodityService $commodityService
+         */
+        $commodityService=app(CommodityService::class);
+        $commodityService->pushCommodityToCache();
+    }
 }

+ 25 - 0
app/Services/CommodityService.php

@@ -11,6 +11,7 @@ use App\Services\common\BatchUpdateService;
 use Carbon\Carbon;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Support\Facades\Cache;
 
 Class CommodityService
 {
@@ -332,4 +333,28 @@ Class CommodityService
         if ($query->count() > 0)return true;
         return false;
     }
+    public function pushCommodityToCache(){
+        $amount = 1000;
+        $sum = Commodity::query()->count('id');
+        $number = ceil($sum/$amount);
+        for ($i = 0;$i<$number; $i++){
+            $commodities = $this->getPiece(($i*$amount),$amount);
+            if (!$commodities)continue;
+            $this->pushToCache($commodities);
+        }
+    }
+    private function getPiece(int $start,int $amount){
+        $commodities= Commodity::query()->with(['owner','barcodes'])
+            ->where('id','>=',$start)
+            ->where('id','<',$amount)
+            ->get();
+        return $commodities;
+    }
+    private function pushToCache($commodities){
+        if (count($commodities)<1) return null;
+        foreach ($commodities as $commodity){
+            $key="owner_code_{$commodity['owner']['code']}_sku_{$commodity['sku']}";
+            Cache::forever($key,$commodity);
+        }
+    }
 }

+ 20 - 0
app/Services/OracleBasSkuService.php

@@ -71,4 +71,24 @@ Class OracleBasSkuService
         if(!$customer_Ids || !$sku_s){return null;}
         return OracleBasSKU::query()->whereIn('customerid',$customer_Ids)->whereIn('sku',$sku_s)->get();
     }
+    public function getWmsCreatedCommodities($startDate)
+    {
+        if (!$startDate) return null;
+        return OracleBasSKU::query()
+            ->select('customerid','sku','descr_c','alternate_sku1','alternate_sku2','alternate_sku3','skulength','skuwidth','skuhigh','cube','packid','addtime','edittime')
+            ->where('addTime', '>=', $startDate)
+            ->orderByDesc('addtime')
+            ->get();
+    }
+
+    public function getWmsUpdatedCommodities($startDate)
+    {
+        if (!$startDate) return null;
+        return OracleBasSKU::query()
+            ->select('customerid','sku','descr_c','alternate_sku1','alternate_sku2','alternate_sku3','skulength','skuwidth','skuhigh','cube','packid','addtime','edittime')
+            ->where('EditTime', '>=', $startDate)
+            ->whereColumn('EditTime', '<>', 'addTime')
+            ->orderByDesc('EditTime')
+            ->get();
+    }
 }

+ 2 - 0
app/Services/StoreItemService.php

@@ -43,6 +43,7 @@ Class StoreItemService
 
     public function createStoreItem($asnDetails)
     {
+
         if ($asnDetails->isEmpty()) return null;
         ini_set('memory_limit','512M');
         $stores = Store::query()->whereIn('asn_code', array_unique(data_get($asnDetails, '*.asnno')))->get();
@@ -68,6 +69,7 @@ Class StoreItemService
             $storeItem = $dataHandlerService
                 ->getKeyValue(['store_asn_code' => $asnDetail->asnno, 'asn_line_code' => $asnDetail->asnlineno, 'sku' => $asnDetail->sku], $storeItem_map);
             if ($storeItem ?? false) continue;
+            //if (empty($store_asn_code_map[$asnDetail->asnno])) continue;
             $params[] = [
                 'store_id' => $store_asn_code_map[$asnDetail->asnno]['id'],
                 'asn_line_code' => (string)$asnDetail->asnlineno,

+ 4 - 4
app/Services/StoreService.php

@@ -135,7 +135,7 @@ Class StoreService
                 'owner_id' => $owner->id ?? null,
                 'stored_method' => $asnHerder->asnType ? $asnHerder->asnType->codename_c : '',
                 'status' => $asnHerder->asnStatus ? $asnHerder->asnStatus->codename_c : '',
-                'remark' => $asnHerder->notes ?? null,
+                'remark' => $asnHerder->notes ?? '1111',
                 'created_at' => $asnHerder->addtime ?? null,
                 'updated_at' => $asnHerder->edittime ?? null,
             ];
@@ -149,8 +149,8 @@ Class StoreService
         try {
             $this->insert($params);
             LogService::log(__METHOD__, __FUNCTION__, '批量创建 store success' . count($params) . json_encode($params));
-        } catch (\Exception $e) {
-            LogService::log(__METHOD__, __FUNCTION__, '批量创建 store error' . json_encode($params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
+        } catch (\Exception $e) {dd($e->getMessage());
+            LogService::log(__METHOD__, __FUNCTION__, '批量创建 store error' . json_encode($params) . '||' .json_encode($e->getMessage())  . '||' . $e->getTraceAsString());
         }
     }
 
@@ -227,7 +227,7 @@ Class StoreService
         return Store::query()->whereIn('asn_code', $asn_nos)->get();
     }
 
-    public function getAsnLastSyncAt($key, $type = 'create')
+    public function getAsnLastSyncAt($key, $type)
     {
         $last_time = ValueStore::query()->where('name', $key)->value('value');
         if ($last_time) return $last_time;

+ 14 - 0
config/sync.php

@@ -18,6 +18,20 @@ return [
             'update'=>'asn_update_',
         ]
     ],
+    'commodity_sync' => [
+        'interval' => 1,   // 时间以分为单位
+        'start_at'=> '',    // 开始同步WMS的时间  时间格式: yyyy-MM-dd HH:mm:ss
+        'created_at'=>'commodity_last_created_sync_at',
+        'updated_at'=>'commodity_last_updated_sync_at',
+        'cache_prefix'=>[
+            'create_set'=>'commodity_create_has_set',
+            'create_keys'=>'commodity_create_keys',
+            'create'=>'commodity_create_',
+            'update_set'=>'commodity_update_has_set',
+            'update_keys'=>'commodity_update_keys',
+            'update'=>'commodity_update_',
+        ]
+    ],
     'order_sync' => [
         'interval' => 1,   // 时间以分为单位
         'start_at'=> '',    // 开始同步WMS的时间  时间格式: yyyy-MM-dd HH:mm:ss 如 2020-09-05 13:15:16

+ 2 - 2
phpunit.xml

@@ -34,8 +34,8 @@
         <server name="APP_ENV" value="testing"/>
         <server name="BCRYPT_ROUNDS" value="4"/>
         <server name="CACHE_DRIVER" value="redis"/>
-        <server name="DB_CONNECTION" value="sqlite"/>
-        <server name="DB_DATABASE" value=":memory:"/>
+<!--        <server name="DB_CONNECTION" value="sqlite"/>-->
+<!--        <server name="DB_DATABASE" value=":memory:"/>-->
         <server name="MAIL_DRIVER" value="array"/>
         <server name="QUEUE_CONNECTION" value="sync"/>
         <server name="SESSION_DRIVER" value="array"/>

+ 2 - 2
tests/Services/RejectedBillService/GetLogisticNumberTest.php

@@ -50,13 +50,13 @@ class GetLogisticNumberTest extends TestCase
         $addUpdateCollect=$this->service->getUpdateCollect($this->asnHeaders);
         if ($addUpdateCollect){
             $addLogisticNumber=$this->service->getLogisticNumberReturn($addUpdateCollect);
-            $this->assertNotEmpty($addLogisticNumber);
+            $this->assertNotNull($addLogisticNumber);
         }
         if (empty($this->asnHeadersEdit))return null;
         $editUpdateCollect=$this->service->getUpdateCollect($this->asnHeadersEdit);
         if ($editUpdateCollect){
             $editLogisticNumber=$this->service->getLogisticNumberReturn($editUpdateCollect);
-            $this->assertNotEmpty($editLogisticNumber);
+            $this->assertNotNull($editLogisticNumber);
         }
     }
 }

+ 4 - 4
tests/Services/RejectedBillService/GetUpdateCollectTest.php

@@ -49,16 +49,16 @@ class GetUpdateCollectTest extends TestCase
         if (empty($this->asnHeaders))return null;
         $addUpdateCollect=$this->service->getUpdateCollect($this->asnHeaders);
         if ($addUpdateCollect){
-            $this->assertNotEmpty($addUpdateCollect);
+            $this->assertNotNull($addUpdateCollect);
         }else{
-            $this->assertEmpty($addUpdateCollect);
+            $this->assertNull($addUpdateCollect);
         }
         if (empty($this->asnHeadersEdit))return null;
         $editUpdateCollect=$this->service->getUpdateCollect($this->asnHeadersEdit);
         if ($editUpdateCollect){
-            $this->assertNotEmpty($editUpdateCollect);
+            $this->assertNotNull($editUpdateCollect);
         }else{
-            $this->assertEmpty($editUpdateCollect);
+            $this->assertNull($editUpdateCollect);
         }
     }
 }

+ 4 - 1
tests/Services/RejectedBillService/SyncLoadedStatusByAsnHeaderTest.php

@@ -40,7 +40,10 @@ class SyncLoadedStatusByAsnHeaderTest extends TestCase
         if ($updateCollect->isEmpty())return null;
         $logisticNumberReturn=$this->service->getLogisticNumberReturn($updateCollect);
         $rejectedBills=$this->service->getRejectedBills($logisticNumberReturn);
-        if ($rejectedBills->isEmpty())return null;
+        if ($rejectedBills->isEmpty()){
+            $this->assertEmpty($rejectedBills);
+            return null;
+        }
         $this->assertNull($rejectedBills);
         $this->service->syncLoadedStatusByAsnHerder($this->asnHeaders);
         $this->assertNull($logisticNumberReturn);

+ 3 - 1
tests/Services/StoreService/CreateStoreTest.php

@@ -7,11 +7,13 @@ namespace Tests\Services\StoreService;
 use App\OracleDOCASNHeader;
 use App\Services\StoreService;
 use Carbon\Carbon;
+use Illuminate\Foundation\Testing\RefreshDatabase;
 use Illuminate\Support\Facades\DB;
 use Tests\TestCase;
 
 class CreateStoreTest extends TestCase
 {
+//    use RefreshDatabase;
     /** @var StoreService $service */
     public $service;
     public $asnHeaders;
@@ -32,7 +34,7 @@ class CreateStoreTest extends TestCase
         if (!$this->asnHeaders) return null;
         $this->service->createStore($this->asnHeaders);
         $this->stores=$this->service->getByWms($this->asnHeaders);
-        $this->assertNotEmpty($this->stores);
+        $this->assertNotnull($this->stores);
         $this->assertNotNull($this->asnHeaders);
     }
     public function tearDown(): void

+ 41 - 0
tests/Services/StoreService/GetAsnSyncAtTest.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace Tests\Services\OrderService;
+
+use App\Services\OrderService;
+use App\Services\StoreService;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Tests\TestCase;
+
+class GetAsnSyncAtTest extends TestCase
+{
+    /**
+     * @var StoreService $service
+     */
+    private $service;
+    public function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        $this->service = app(StoreService::class);
+    }
+
+    /**
+     * @test
+     */
+    public function getCreatedAsnSyncAt()
+    {
+        $key = config('sync.asn_sync.created_at');
+        $data = $this->service->getAsnLastSyncAt($key,'create');
+        $this->assertNotEmpty($data);
+    }
+
+    /**
+     * @test
+     */
+    public function getUpdatedAsnSyncAt()
+    {
+        $key = config('sync.asn_sync.updated_at');
+        $data = $this->service->getAsnLastSyncAt($key,'update');
+        $this->assertNotEmpty($data);
+    }
+}

+ 1 - 1
tests/Services/StoreService/GetParamsByAsnHeaderTest.php

@@ -42,7 +42,7 @@ class GetParamsByAsnHeaderTest extends TestCase
         if (empty($this->asnHeaders))return null;
         $stores = Store::query()->whereIn('asn_code',data_get($this->asnHeaders,'*.asnno'))->get();
         $owners=$this->ownerService->getByWmsOrders($this->asnHeaders);
-        $warehouses=$this->warehouseService->getByWms($this->asnHeaders);
+        $warehouses=$this->warehouseService->getByWmsOrders($this->asnHeaders);
         $this->assertNotEmpty($this->asnHeaders);
         $this->assertNotEmpty($owners);
         $this->assertNotEmpty($warehouses);

+ 1 - 2
tests/Services/StoreService/InsertStoreTest.php

@@ -41,8 +41,7 @@ class InsertStoreTest extends TestCase
         array_push($this->params,$data2);
     }
     public  function testInsertStore(){
-        $stores=$this->service->insertStore($this->params);
-        $this->assertNotEmpty($stores);
+        $this->service->insertStore($this->params);
         $this->assertDatabaseHas('stores',$this->params[0]);
     }
      public function tearDown(): void

+ 52 - 0
tests/Services/StoreService/SetAsnSyncAtTest.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace Tests\Services\OrderService;
+
+use App\Services\OrderService;
+use App\Services\StoreService;
+use App\ValueStore;
+use Carbon\Carbon;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Illuminate\Support\Str;
+use Tests\TestCase;
+
+class SetAsnSyncAtTest extends TestCase
+{
+    /** @var StoreService $service */
+    private $service;
+    private $created_at;
+    private $original_value;
+    public function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        $this->service = app(StoreService::class);
+        $this->created_at = config('sync.asn_sync.created_at');
+    }
+
+    /**
+     * @test
+     */
+    public function testSetAsnSyncAt()
+    {
+        /** @var Carbon $time */
+
+        $data = Carbon::now();
+        $this->original_value=ValueStore::query()->where('name',$this->created_at)->value('value');
+
+        $this->service->setAsnLastSyncAt($this->created_at,$data);
+        $value=ValueStore::query()->where('name',$this->created_at)->value('value');
+        $this->assertEquals((string)$data,(string)$value);
+    }
+
+    public function tearDown(): void
+    {
+        ValueStore::query()->updateOrCreate([
+            'name' => $this->created_at,
+        ], [
+            'name' => $this->created_at,
+            'value' => $this->original_value,
+        ]);
+        parent::tearDown(); // TODO: Change the autogenerated stub
+    }
+}

+ 91 - 0
tests/Services/StoreService/SetLastRecordsByRedisTest.php

@@ -0,0 +1,91 @@
+<?php
+
+namespace Tests\Services\OrderService;
+
+use App\OracleDOCASNHeader;
+use App\Services\StoreService;
+use Illuminate\Support\Facades\Cache;
+use Tests\TestCase;
+
+class SetLastRecordsByRedisTest extends TestCase
+{
+    /** @var StoreService $service */
+    public $service;
+    public $data;
+    public $create_set;
+    public $create_keys;
+    public $create_key;
+    public $update_set;
+    public $update_keys;
+    public $update_key;
+    public $createRecord;
+    public $updateRecord;
+    public function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        $this->service = app(StoreService::class);
+        $this->create_set = config('sync.asn_sync.cache_prefix.create_set');
+        $this->create_keys = config('sync.asn_sync.cache_prefix.create_keys');
+        $this->create_key = config('sync.asn_sync.cache_prefix.create');
+        $this->update_set = config('sync.asn_sync.cache_prefix.update_set');
+        $this->update_keys = config('sync.asn_sync.cache_prefix.update_keys');
+        $this->update_key = config('sync.asn_sync.cache_prefix.update');
+    }
+    /**
+     * @test
+     */
+    public function testSetLastRecordsByRedisCreated()
+    {
+        $this->createRecord=collect();
+        $last_record=OracleDOCASNHeader::query()->orderByDesc('addtime')->first();
+        $this->createRecord->add($last_record);
+        if (!Cache::get($this->create_set)){
+            $this->assertNull(Cache::get($this->create_set));
+            $this->assertNotNull($this->createRecord);
+            return;
+        }
+        $this->service->setLastRecordsByRedis($this->create_key,$this->create_set,$this->create_keys,$this->createRecord);
+        $this->assertNotNull($this->createRecord);
+        $this->assertEquals(true,Cache::get($this->create_set));
+        $this->assertNotNull(Cache::get($this->create_keys));
+    }
+
+    /**
+     * @test
+     */
+    public function testSetLastRecordsByRedisUpdated()
+    {
+        $this->updateRecord=collect();
+        $last_record=OracleDOCASNHeader::query()->orderByDesc('edittime')->first();
+        $this->updateRecord->add($last_record);
+        if (!Cache::get($this->update_set)){
+            $this->assertNull(Cache::get($this->update_set));
+            $this->assertNotNull($this->updateRecord);
+            return;
+        }
+        $this->service->setLastRecordsByRedis($this->update_key,$this->update_set,$this->update_keys,$this->updateRecord);
+        $this->assertNotNull($this->updateRecord);
+        $this->assertEquals(true,Cache::get($this->update_set));
+        $this->assertNotNull(Cache::get($this->update_keys));
+    }
+
+    public function tearDown(): void
+    {
+        if (!Cache::get($this->create_set)) return;
+        $cacheKeys = Cache::get($this->create_keys);
+        if (!$cacheKeys) return;
+        foreach ($cacheKeys as $cacheKey) {
+            Cache::forget($cacheKey);
+        }
+        Cache::forget($this->create_keys);
+
+        if (!Cache::get($this->update_set)) return;
+        $cacheKeys = Cache::get($this->update_keys);
+        if (!$cacheKeys) return;
+        foreach ($cacheKeys as $cacheKey) {
+            Cache::forget($cacheKey);
+        }
+        Cache::forget($this->update_keys);
+        parent::tearDown(); // TODO: Change the autogenerated stub
+    }
+}

+ 0 - 69
tests/Services/StoreService/SyncWmsAsnDataTest.php

@@ -1,69 +0,0 @@
-<?php
-
-
-namespace Tests\AsnSync\Services;
-
-
-use App\OracleDOCASNHeader;
-use App\Services\StoreService;
-use Illuminate\Support\Facades\DB;
-use Tests\TestCase;
-
-class SyncWmsAsnDataTest extends TestCase
-{
-    /** @var StoreService $service */
-    public $service;
-    public $asnHeaders;
-    public $asnHeadersEdit;
-    public $asnHeadersTotal;
-    public $startDate;
-    public $stores;
-    public function setUp(): void
-    {
-
-        parent::setUp(); // TODO: Change the autogenerated stub
-        $this->startDate = \Illuminate\Support\Carbon::now()->subSeconds(300);
-        $this->service = app(StoreService::class);
-        $this->asnHeaders = OracleDOCASNHeader::query()
-            ->with(['asnType', 'asnStatus', 'asnDetails' => function ($query) {
-                $query->with(['lineStatus', 'qualityStatus', 'basSku']);
-            }])
-            ->where('addTime', '>=', $this->startDate)
-            ->get();
-        $this->asnHeadersEdit = OracleDOCASNHeader::query()
-            ->with(['asnType', 'asnStatus', 'asnDetails' => function ($query) {
-                $query->with(['lineStatus', 'qualityStatus', 'basSku']);
-            }])
-            ->where('EditTime', '>=', $this->startDate)
-            ->whereColumn('EditTime', '<>', 'addTime')
-            ->get();
-    }
-
-
-    public function testSyncWmsAsnData()
-    {
-          if (!$this->asnHeaders && !$this->asnHeadersEdit) return null;
-        if (!empty($this->asnHeadersEdit)){
-            foreach ($this->asnHeadersEdit as $asnHerder)
-                $this->asnHeaders->add($asnHerder);
-        }
-        if ($this->asnHeaders) {
-            $this->service->syncWmsAsnData($this->startDate);
-            $this->stores = $this->service->getByWms($this->asnHeaders);
-            $this->assertNotEmpty($this->stores);
-        } else {
-            $this->assertNull($this->asnHeaders);
-        }
-    }
-
-    public function tearDown(): void
-    {
-        $storeIds = [];
-        foreach ($this->stores as $store) {
-            array_push($storeIds, $store->id);
-        }
-        DB::table('stores')->whereIn('asn_code', data_get($this->asnHeaders, '*.asnno'))->delete();
-        DB::table('store_items')->whereIn('store_id', $storeIds)->delete();
-        parent::tearDown(); // TODO: Change the autogenerated stub
-    }
-}

+ 1 - 1
tests/Services/StoreService/UpdateStoreTest.php

@@ -35,7 +35,7 @@ class UpdateStoreTest extends TestCase
         }else{
             $this->service->updateStore($this->asnHeaders);
             $this->stores=$this->service->getByWms($this->asnHeaders);
-            $this->assertNotEmpty($this->stores);
+            $this->assertNotNull($this->stores);
         }
     }
     public function tearDown(): void