SyncCommodityUpdatedTest.php 3.6 KB

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