|
|
@@ -0,0 +1,303 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace NewOrderCountingRecordService;
|
|
|
+
|
|
|
+use App\Order;
|
|
|
+use App\OrderCountingRecord;
|
|
|
+use App\Owner;
|
|
|
+use App\Services\NewOrderCountingRecordService;
|
|
|
+use App\User;
|
|
|
+use Carbon\Carbon;
|
|
|
+use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
+use Illuminate\Support\Arr;
|
|
|
+use Tests\TestCase;
|
|
|
+
|
|
|
+class NewOrderCountingRecordServiceTest extends TestCase
|
|
|
+{
|
|
|
+ use RefreshDatabase;
|
|
|
+
|
|
|
+ protected $newOrderCountingRecordService;
|
|
|
+
|
|
|
+ protected function setUp(): void
|
|
|
+ {
|
|
|
+ parent::setUp(); // TODO: Change the autogenerated stub
|
|
|
+ cache()->flush();
|
|
|
+ $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
|
|
|
+ $this->actingAs(factory(User::class)->create(['name' => 'yang']));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function tearDown(): void
|
|
|
+ {
|
|
|
+ cache()->flush();
|
|
|
+ OrderCountingRecord::truncate();
|
|
|
+ Order::truncate();
|
|
|
+ Owner::truncate();
|
|
|
+ parent::tearDown(); // TODO: Change the autogenerated stub
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function dateUtils_day()
|
|
|
+ {
|
|
|
+ factory(Owner::class)->times(2)->create();
|
|
|
+ $start = Carbon::now()->subDays(2)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ $dateArray = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
|
|
|
+ $this->assertEquals([
|
|
|
+ Carbon::now()->subDays(2)->toDateString() => [0 => 1, 1 => 2],
|
|
|
+ Carbon::now()->subDays(1)->toDateString() => [0 => 1, 1 => 2],
|
|
|
+ Carbon::now()->toDateString() => [0 => 1, 1 => 2],
|
|
|
+ ], $dateArray);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function orderCountingRecords_remember()
|
|
|
+ {
|
|
|
+ $start = Carbon::now()->subDays(2)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ $key = 'orderCountingRecords_' . $start . '_' . $end . '_' . $unit . '_' . auth()->id();
|
|
|
+
|
|
|
+ cache()->put($key, 'This is a test value');
|
|
|
+
|
|
|
+ $result = $this->newOrderCountingRecordService->orderCountingRecords($start, $end, $unit);
|
|
|
+ $this->assertEquals('This is a test value', $result);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function dataByDayFromCache_all()
|
|
|
+ {
|
|
|
+ $start = Carbon::now()->subDays(2)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ factory(Owner::class)->times(2)->create();
|
|
|
+ $queryCondition = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
|
|
|
+ foreach ($queryCondition as $dateStr => $ownerIds) {
|
|
|
+ foreach ($ownerIds as $ownerId) {
|
|
|
+ $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit . '_' . auth()->id();
|
|
|
+ cache()->put($key, collect()->push(factory(Owner::class)->create()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $result = $this->newOrderCountingRecordService->dataByDayFromCache($start, $end, $unit);
|
|
|
+ $this->assertCount(6, $result['dataByDayFromCache']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function dataByDayFromCache_empty()
|
|
|
+ {
|
|
|
+ $start = Carbon::now()->subDays(2)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ factory(Owner::class)->times(2)->create();
|
|
|
+ $result = $this->newOrderCountingRecordService->dataByDayFromCache($start, $end, $unit);
|
|
|
+ $this->assertEquals([
|
|
|
+ Carbon::now()->subDays(2)->toDateString() => [0 => 1, 1 => 2],
|
|
|
+ Carbon::now()->subDays(1)->toDateString() => [0 => 1, 1 => 2],
|
|
|
+ Carbon::now()->toDateString() => [0 => 1, 1 => 2],
|
|
|
+ ], $result['unQueryCondition']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function dataByDayFromMiddleTable_all()
|
|
|
+ {
|
|
|
+ $start = Carbon::now()->subDays(1)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ $owner1 = factory(Owner::class)->create();
|
|
|
+ $owner2 = factory(Owner::class)->create();
|
|
|
+ factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]);
|
|
|
+ factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner2->id]);
|
|
|
+ factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->subDays(1)->toDateString(), 'owner_id' => $owner1->id]);
|
|
|
+ factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->subDays(1)->toDateString(), 'owner_id' => $owner2->id]);
|
|
|
+ $dateArray = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
|
|
|
+ $result = $this->newOrderCountingRecordService->dataByDayFromMiddleTable($dateArray, $unit);
|
|
|
+
|
|
|
+ $this->assertCount(8, $result['dataByDayFromMiddleTable']);
|
|
|
+ $this->assertCount(0, $result['unQueryCondition'][Carbon::now()->toDateString()]);
|
|
|
+ $this->assertCount(0, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function dataByDayFromMiddleTable_empty()
|
|
|
+ {
|
|
|
+ $start = Carbon::now()->subDays(1)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ $owner1 = factory(Owner::class)->create();
|
|
|
+ $owner2 = factory(Owner::class)->create();
|
|
|
+
|
|
|
+ $dateArray = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
|
|
|
+ $result = $this->newOrderCountingRecordService->dataByDayFromMiddleTable($dateArray, $unit);
|
|
|
+
|
|
|
+ $this->assertCount(0, $result['dataByDayFromMiddleTable']);
|
|
|
+ $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->toDateString()]);
|
|
|
+ $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function dataByDayFromMiddleTable_common()
|
|
|
+ {
|
|
|
+ $start = Carbon::now()->subDays(1)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ $owner1 = factory(Owner::class)->create();
|
|
|
+ $owner2 = factory(Owner::class)->create();
|
|
|
+
|
|
|
+ $dateArray = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
|
|
|
+ factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]);
|
|
|
+
|
|
|
+ $result = $this->newOrderCountingRecordService->dataByDayFromMiddleTable($dateArray, $unit);
|
|
|
+
|
|
|
+ $this->assertCount(2, $result['dataByDayFromMiddleTable']);
|
|
|
+ $this->assertCount(1, $result['unQueryCondition'][Carbon::now()->toDateString()]);
|
|
|
+ $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function insertIntoCache()
|
|
|
+ {
|
|
|
+ $start = Carbon::now()->subDays(1)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ $owner1 = factory(Owner::class)->create();
|
|
|
+ $owner2 = factory(Owner::class)->create();
|
|
|
+
|
|
|
+ $dateArray = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
|
|
|
+ $orderCountingRecord = factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]);
|
|
|
+
|
|
|
+ $result = $this->newOrderCountingRecordService->dataByDayFromMiddleTable($dateArray, $unit);
|
|
|
+
|
|
|
+ $this->newOrderCountingRecordService->insertIntoCache($result['dataByDayFromMiddleTable'], $unit);
|
|
|
+ $key = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner1->id . '_' . $unit;
|
|
|
+ $this->assertNotNull(cache()->get($key));
|
|
|
+ $this->assertNotNull($orderCountingRecord->toArray());
|
|
|
+ $this->assertEquals($orderCountingRecord->toArray(), cache()->get($key)->toArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function dataByDayFromOrder()
|
|
|
+ {
|
|
|
+ $start = Carbon::now()->subDays(1)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ $owner1 = factory(Owner::class)->create();
|
|
|
+ $owner2 = factory(Owner::class)->create();
|
|
|
+
|
|
|
+ $unQueryCondition = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
|
|
|
+
|
|
|
+ factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
|
|
|
+ factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
|
|
|
+
|
|
|
+ factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
|
|
|
+ factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
|
|
|
+
|
|
|
+ $result = $this->newOrderCountingRecordService->dataByDayFromOrder($unQueryCondition, $unit);
|
|
|
+ $this->assertEquals(1, $result->where('date_target', Carbon::now()->toDateString())->where('owner_id', $owner1->id)->first()->amount);
|
|
|
+ $this->assertEquals(2, $result->where('date_target', Carbon::now()->toDateString())->where('owner_id', $owner2->id)->first()->amount);
|
|
|
+ $this->assertEquals(3, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->where('owner_id', $owner1->id)->first()->amount);
|
|
|
+ $this->assertEquals(4, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->where('owner_id', $owner2->id)->first()->amount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function dataByDayFromOrder_insertMiddleTable()
|
|
|
+ {
|
|
|
+ $start = Carbon::now()->subDays(1)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ $owner1 = factory(Owner::class)->create();
|
|
|
+ $owner2 = factory(Owner::class)->create();
|
|
|
+
|
|
|
+ $unQueryCondition = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
|
|
|
+
|
|
|
+ factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
|
|
|
+ factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
|
|
|
+
|
|
|
+ factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
|
|
|
+ factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
|
|
|
+
|
|
|
+ $this->newOrderCountingRecordService->dataByDayFromOrder($unQueryCondition, $unit);
|
|
|
+ //当天的数据不入中间表
|
|
|
+ $this->assertCount(0, OrderCountingRecord::query()->where('date_target', Carbon::now()->toDateString())->get());
|
|
|
+ $this->assertCount(2, OrderCountingRecord::query()->where('date_target', Carbon::now()->subDays(1)->toDateString())->get());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function dataByDayFromOrder_insertCache()
|
|
|
+ {
|
|
|
+ $start = Carbon::now()->subDays(1)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ $owner1 = factory(Owner::class)->create();
|
|
|
+ $owner2 = factory(Owner::class)->create();
|
|
|
+
|
|
|
+ $unQueryCondition = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
|
|
|
+
|
|
|
+ factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
|
|
|
+ factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
|
|
|
+
|
|
|
+ factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
|
|
|
+ factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
|
|
|
+
|
|
|
+ $this->newOrderCountingRecordService->dataByDayFromOrder($unQueryCondition, $unit);
|
|
|
+ $key1 = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner1->id . '_' . $unit;
|
|
|
+ $key2 = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner2->id . '_' . $unit;
|
|
|
+ $key3 = 'order_counting_records_' . Carbon::now()->subDays(1)->toDateString() . '_' . $owner1->id . '_' . $unit;
|
|
|
+ $key4 = 'order_counting_records_' . Carbon::now()->subDays(1)->toDateString() . '_' . $owner2->id . '_' . $unit;
|
|
|
+ $this->assertCount(1, cache()->get($key1));
|
|
|
+ $this->assertCount(1, cache()->get($key2));
|
|
|
+ $this->assertCount(1, cache()->get($key3));
|
|
|
+ $this->assertCount(1, cache()->get($key4));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function orderCountingRecordsDay()
|
|
|
+ {
|
|
|
+ $start = Carbon::now()->subDays(1)->toDateString();
|
|
|
+ $end = Carbon::now()->toDateString();
|
|
|
+ $unit = '日';
|
|
|
+ $owner1 = factory(Owner::class)->create();
|
|
|
+ $owner2 = factory(Owner::class)->create();
|
|
|
+
|
|
|
+
|
|
|
+ factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
|
|
|
+ factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
|
|
|
+
|
|
|
+ factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
|
|
|
+ factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
|
|
|
+
|
|
|
+ $result = $this->newOrderCountingRecordService->orderCountingRecordsDay($start, $end, $unit);
|
|
|
+ $this->assertEquals(3, $result->where('date_target', Carbon::now()->toDateString())->reduce(function ($carry, $item) {
|
|
|
+ return $carry + $item->amount;
|
|
|
+ }));
|
|
|
+ $this->assertEquals(7, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->reduce(function ($carry, $item) {
|
|
|
+ return $carry + $item->amount;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+}
|