LD 5 anni fa
parent
commit
a968a61edc

+ 2 - 3
app/Http/Controllers/TestController.php

@@ -67,6 +67,7 @@ use App\Services\OwnerService;
 use App\Services\ShopService;
 use App\Services\StoreService;
 use App\Services\WarehouseService;
+use App\StationRuleBatch;
 use App\Store;
 use App\StationTaskBatch;
 use App\StoreCheckingReceiveItem;
@@ -280,9 +281,7 @@ sql;
 
     function t1(Request $request)
     {
-        $user=City::query()->firstx();
-        $user['created_at'] = '2020-12-23';
-        $user->save();
+        var_dump(is_array(collect([1])));
     }
 
     function packageT(Request $request)

+ 23 - 11
app/Services/StationTaskBatchService.php

@@ -5,12 +5,14 @@ namespace App\Services;
 
 
 use App\Batch;
+use App\Exceptions\ErrorException;
 use App\Log;
 use App\Station;
 use App\StationTask;
 use App\StationTaskBatch;
 use App\StationTaskBatchType;
 use Exception;
+use Illuminate\Foundation\Mix;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\Cache;
 
@@ -75,22 +77,35 @@ class StationTaskBatchService
         }
         $this->batchService->updateWhereIn('id', data_get($batches_handled, '*.id'), ['status' => '处理中']);
         $this->insert($stationTaskBatches_toCreate->toArray());
+        $stationTaskBatches_toCreate=$this->getWithIds($stationTaskBatches_toCreate);
         $this->stationTaskService->registerSubTasks(
             $stationTasks_toAttach,
-            collect(
-                [$stationTaskBatches_toCreate]
-            )
+            $stationTaskBatches_toCreate->map(function($taskBatch){
+                return [$taskBatch];
+            })
         );
 
         return $stationTaskBatches_toCreate;
     }
 
+    function getWithIds($stationMissionBatches): Collection
+    {
+        $md5=is_array($stationMissionBatches)
+            ?$md5=md5(json_encode($stationMissionBatches)):null;
+
+        return Cache::remember(
+            $md5??md5(json_encode($stationMissionBatches->toArray()))
+            ,config('cache.expirations.rarelyChange')
+            ,function()use($stationMissionBatches){
+            return StationTaskBatch::query()
+                ->whereIn('status',data_get($stationMissionBatches,'*.status'))
+                ->whereIn('batch_id',data_get($stationMissionBatches,'*.batch_id'))
+                ->get();
+        });
+    }
     function insert(array $stationMissionBatches): bool
     {
-        $inserted = StationTaskBatch::query()->insert($stationMissionBatches);
-        LogService::log(__METHOD__, __FUNCTION__, json_encode($stationMissionBatches) .
-            '||' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3)));
-        return $inserted;
+        return StationTaskBatch::query()->insert($stationMissionBatches);
     }
 
     function markManyExcepted(Collection $stationTaskBatches_failed)
@@ -105,10 +120,7 @@ class StationTaskBatchService
         ($logAtFailings_andWait =
             function ($stationTaskBatches_failed) {
                 if ($stationTaskBatches_failed->isEmpty()) return;
-                LogService::log(__METHOD__, __FUNCTION__,
-                    '任务波次异常失败的:' . $stationTaskBatches_failed->toJson()
-                    . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
-                );
+                throw new ErrorException('任务波次异常失败的');
             })($stationTaskBatches_failed);
     }
 

+ 2 - 11
app/Services/StationTaskService.php

@@ -41,19 +41,10 @@ class StationTaskService
         $taskChildren_toInsert=collect();
         foreach ($subTaskLists as $i=>$subTaskList){
             foreach ($subTaskList as $subTask){
-                $tableName=(function()use($subTask){
-                    switch (get_class($subTask)){
-                        case StationTaskBatch::class: return 'station_task_batches';
-                        case StationTaskCommodity::class: return 'station_task_commodities';
-                        case StationTaskMaterialBox::class: return 'station_task_material_boxes';
-                        default:
-                            throw new \Exception('任务类型没有定义相应的模型类');
-                    }
-                })();
                 $paramToInsert = [
                     'station_task_id' => $tasks[$i]['id'],
-                    'station_task_table_type' => $tableName,
-                    'station_task_table_id' => $subTask['id'],
+                    'station_taskable_type' => get_class($subTask),
+                    'station_taskable_id' => $subTask['id'],
                 ];
                 $taskChild_existed=$this->stationTaskChildService->get($paramToInsert)->first();
                 if(!$taskChild_existed)

+ 1 - 1
app/StationTaskBatch.php

@@ -20,7 +20,7 @@ class StationTaskBatch extends Model
     }
     function stationTask(): MorphOne
     {
-        return $this->morphOne(StationTask::class,'station_taskable');
+        return $this->morphOne(StationTaskChild::class,'station_taskable');
     }
 
     public function batch(): HasOne

+ 1 - 1
app/StationTaskChild.php

@@ -10,5 +10,5 @@ class StationTaskChild extends Model
 {
     use LogModelChanging;
 
-    protected $fillable= ['station_task_id','station_task_table_type','station_task_table_id'];
+    protected $fillable= ['station_task_id','station_taskable_type','station_taskable_id'];
 }

+ 4 - 4
database/migrations/2021_01_06_114158_create_station_task_children2.php

@@ -16,15 +16,15 @@ class CreateStationTaskChildren2 extends Migration
         Schema::create('station_task_children', function (Blueprint $table) {
             $table->id();
             $table->bigInteger('station_task_id')->index();
-            $table->string('station_task_table_type');
-            $table->bigInteger('station_task_table_id');
-            $table->index('station_task_table_id','station_task_table_type');
+            $table->string('station_taskable_type');
+            $table->bigInteger('station_taskable_id');
+            $table->index(['station_taskable_type','station_taskable_id'],'station_taskable_t_i');
             $table->timestamps();
         });
         Schema::table('station_tasks', function (Blueprint $table) {
-            $table->dropIndex('station_taskable_type');
             $table->dropColumn('station_taskable_type');
             $table->dropColumn('station_taskable_id');
+            $table->dropIndex('station_taskable_type');
         });
     }
 

+ 16 - 11
tests/Services/StationTaskBatchService/CreateByBatchesTest.php

@@ -37,33 +37,38 @@ class CreateByBatchesTest extends TestCase
                     'status'=>'未处理',
                     'owner_id'=>$this->data['owner']['id']
                 ]);
-//        $this->data['stationType'] =
-//            factory(StationType::class)
-//                ->create();
         $this->data['stationRuleBatch'] =
             factory(StationRuleBatch::class)
                 -> create([
-                'owner_id'=>$this->data['owner']['id'],
-            ]);
+                    'owner_id'=>$this->data['owner']['id'],
+                ]);
         $this->data['stationTasks'] =
             $this->stationTaskService->create(self::AmountOfBatch);
     }
 
     public function testReturned()
     {
-        $this->data['stationTaskBatches']=$this->service->createByBatches($this->data['batches'], $this->data['stationTasks']);
+        $this->data['stationTaskBatches']
+            =$this->service->createByBatches($this->data['batches'], $this->data['stationTasks']);
         $this->assertEquals(self::AmountOfBatch, $this->data['stationTaskBatches']->count());
 
+        $this->assertEquals(
+            data_get($this->data['stationTasks'],'*.id'),
+            $this->data['stationTaskBatches']->map(function($taskBatch){
+                $taskBatch->loadMissing('stationTask');
+                return $taskBatch['stationTask']['station_task_id'];
+            })->toArray()
+        );
     }
 
 
     public function tearDown(): void
     {
-        Batch::query()->whereIn('id',data_get($this->data['batches'],'*.id')??[])->delete();
-        StationTask::query()->whereIn('id',data_get($this->data['stationTasks'],'*.id')??[])->delete();
-        StationTaskBatch::query()->whereIn('id',data_get($this->data['stationTaskBatches'],'*.id')??[])->delete();
-        StationTaskChild::query()->whereIn('station_task_id',data_get($this->data['stationTaskBatches'],'*.id')??[])->delete();
-        Owner::query()->where('id',$this->data['owner']['id']??'')->delete();
+//        Batch::query()->whereIn('id',data_get($this->data['batches'],'*.id')??[])->delete();
+//        StationTask::query()->whereIn('id',data_get($this->data['stationTasks'],'*.id')??[])->delete();
+//        StationTaskBatch::query()->whereIn('id',data_get($this->data['stationTaskBatches'],'*.id')??[])->delete();
+//        StationTaskChild::query()->whereIn('station_task_id',data_get($this->data['stationTasks'],'*.id')??[])->delete();
+//        Owner::query()->where('id',$this->data['owner']['id']??'')->delete();
         parent::tearDown();
     }
 }