SyncUpdateCommodityBasSkusTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Tests\Services\CommodityService;
  3. use App\Commodity;
  4. use App\OracleBasSKU;
  5. use App\Owner;
  6. use App\Services\CommodityService;
  7. use App\Services\common\DataHandlerService;
  8. use App\Services\OwnerService;
  9. use App\ValueStore;
  10. use Carbon\Carbon;
  11. use Illuminate\Support\Facades\DB;
  12. use Tests\TestCase;
  13. class SyncUpdateCommodityBasSkusTest extends TestCase
  14. {
  15. /**
  16. * @var CommodityService $service
  17. */
  18. public $service;
  19. public $bas_skus;
  20. public $updated_at;
  21. public $last_time;
  22. public $commodities;
  23. private $data=[];
  24. public function setUp(): void
  25. {
  26. parent::setUp();
  27. cache()->flush();
  28. $this->service=app(CommodityService::class);
  29. $owner = factory(Owner::class)->create();
  30. $basSKU = factory(OracleBasSKU::class)->make(['customerid'=>$owner->code,'packid'=>'STANDARD']);
  31. $this->data['owner'] = $owner;
  32. $this->data['basSKU'] = $basSKU;
  33. $this->bas_skus=collect();
  34. $this->bas_skus->add($basSKU);
  35. }
  36. /**
  37. * @test
  38. */
  39. public function testSyncCommodityUpdated()
  40. {
  41. // $this->updated_at = config('sync.commodity_sync.updated_at');
  42. // $this->last_time = ValueStore::query()->where('name',$this->updated_at)->value('value');
  43. // if (!$this->last_time) $time=Carbon::now()->subSeconds(65);
  44. // $this->bas_skus= OracleBasSKU::query()
  45. // ->select('customerid','sku','descr_c','alternate_sku1','alternate_sku2','alternate_sku3','skulength','skuwidth','skuhigh','cube','packid','addtime','edittime')
  46. // ->where('EditTime', '>=', $this->last_time??$time)
  47. // ->whereColumn('EditTime', '<>', 'addTime')
  48. // ->orderByDesc('EditTime')
  49. // ->get();
  50. $this->service->syncUpdateCommodity($this->bas_skus);
  51. $owner_sku_map = [];
  52. $sku = [];
  53. $this->bas_skus->each(function ($addBasSku) use (&$owner_sku_map, &$sku) {
  54. if (!empty($addBasSku['customerid']) && !empty($addBasSku['sku'])) {
  55. $key = "owner_code_{$addBasSku['customerid']}_sku_{$addBasSku['sku']}";
  56. $owner_sku_map[$key] = ['owner_code' => $addBasSku['customerid'], 'sku' => $addBasSku['sku']];
  57. $sku[] = $addBasSku['sku'];
  58. }
  59. });
  60. /**
  61. * @var OwnerService $ownerService
  62. */
  63. $ownerService = app(OwnerService::class);
  64. $owner_codes = (function () use ($owner_sku_map) {
  65. $owner_codes = [];
  66. if (count($owner_sku_map) == 0) return $owner_codes;
  67. foreach ($owner_sku_map as $item) {
  68. $owner_codes[$item['owner_code']] = $item['owner_code'];
  69. }
  70. return $owner_codes;
  71. })();
  72. $owner_id = (function () use ($ownerService, $owner_codes) {
  73. $owners = $ownerService->getOwnerByCodes($owner_codes);
  74. $map = [];
  75. $owners->each(function ($owner) use (&$map) {
  76. $map[] = $owner['id'];
  77. });
  78. return $map;
  79. })();
  80. $this->commodities = Commodity::query()
  81. ->whereIn('owner_id', array_unique($owner_id))
  82. ->whereIn('sku', array_unique($sku))
  83. ->groupBy('owner_id', 'sku')
  84. ->get();
  85. $this->assertNotNull($this->commodities);
  86. $this->assertEquals(count($this->bas_skus),count($this->commodities));
  87. }
  88. public function tearDown(): void
  89. {
  90. // ValueStore::query()->updateOrCreate([
  91. // 'name' => $this->updated_at,
  92. // ], [
  93. // 'name' => $this->updated_at,
  94. // 'value' => $this->last_time,
  95. // ]);
  96. $this->data['owner']->delete();
  97. $commodityIds=[];
  98. foreach ($this->commodities as $commodity){
  99. array_push($commodityIds,$commodity->id);
  100. }
  101. DB::table('commodities')->whereIn('id',$commodityIds)->delete();
  102. DB::table('commodity_barcodes')->whereIn('commodity_id',$commodityIds)->delete();
  103. cache()->flush();
  104. parent::tearDown(); // TODO: Change the autogenerated stub
  105. }
  106. }