cqpCow 1 anno fa
parent
commit
39ed4ea922
3 ha cambiato i file con 134 aggiunte e 32 eliminazioni
  1. 118 28
      app/Jobs/AssetDeviceJob.php
  2. 14 0
      app/Model/AssetTemp.php
  3. 2 4
      app/Service/AssetService.php

+ 118 - 28
app/Jobs/AssetDeviceJob.php

@@ -3,6 +3,7 @@
 namespace App\Jobs;
 
 use App\Model\Asset;
+use App\Model\AssetTemp;
 use App\Service\InOutOptionService;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
@@ -19,6 +20,7 @@ class AssetDeviceJob implements ShouldQueue
     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
     protected $data;
+    public $timeout = 1800;
 
     /**
      * Create a new job instance
@@ -68,51 +70,139 @@ class AssetDeviceJob implements ShouldQueue
 
         //软删除
         $time = time();
-        Asset::where('del_time',0)->update(['del_time' => $time]);
+        Asset::where('del_time',0)
+            ->where('crt_time','<', strtotime(date('Y-m-d 00:00:00')))
+            ->update(['del_time' => $time]);
+
+        $this->assetTempInsert();
+        echo 'TEMP  OVER-------------------'. PHP_EOL;;
+
+        $this->assetInsert();
+        echo 'OVER-------------------';
+
+        AssetTemp::truncate();
+    }
+
+    //临时表数据插入
+    public function assetTempInsert(){
+        $data = $this->data;
 
         $batchSize = 100;
         $totalAssets = count($data['assetOas']);
         for ($i = 0; $i < $totalAssets; $i += $batchSize) {
             $batch = array_slice($data['assetOas'], $i, $batchSize);
 
+            $insert = [];
+
             // 对每个批次的数据进行处理
             foreach ($batch as $value) {
                 if(empty($value['singleCode'])) continue;
 
-                // 进行您的操作
-                Asset::updateOrCreate(
-                    ['singleCode' => $value['singleCode']], //查询条件
-                    [
-                        "assetCode" => $value['assetCode'] ?? "",
-                        "assetNo" => $value['assetNo'] ?? "",
-                        "assetType" => $value['assetType'] ?? "",
-                        "brand" => $value['brand'] ?? "",
-                        "expectedLife" => $value['expectedLife'] ?? "",
-                        "gs1" => $value['gs1'] ?? "",
-                        "isKey" => $value['isKey'] ?? "",
-                        "kind" => $value['kind'] ?? "",
-                        "located" => $value['located'] ?? "",
-                        "name" => $value['name'] ?? "",
-                        "nextCalibrationTime" => $value['nextCalibrationTime'] ?? "",
-                        "originalValue" => $value['originalValue'] ?? "",
-                        "purchaseTime" => $value['purchaseTime'] ?? "",
-                        "remark" => $value['remark'] ?? "",
-                        "singleCode" => $value['singleCode'] ?? "",
-                        "startUseDate" => $value['startUseDate'] ?? "",
-                        "type" => $value['type'] ?? "",
-                        "useDept" => $value['useDept'] ?? "",
-                        "userName" => $value['userName'] ?? "",
-                        "version" => $value['version'] ?? "",
-                        "del_time" => 0
-                    ]  //添加或者修改的数据
-                );
+                $insert[] = [
+                    "assetCode" => htmlspecialchars($value['assetCode']) ?? "",
+                    "assetNo" => $value['assetNo'] ?? "",
+                    "assetType" => $value['assetType'] ?? "",
+                    "brand" => htmlspecialchars($value['brand']) ?? "",
+                    "expectedLife" => $value['expectedLife'] ?? "",
+                    "gs1" => $value['gs1'] ?? "",
+                    "isKey" => $value['isKey'] ?? "",
+                    "kind" => $value['kind'] ?? "",
+                    "located" => $value['located'] ?? "",
+                    "name" => $value['name'] ?? "",
+                    "nextCalibrationTime" => $value['nextCalibrationTime'] ?? "",
+                    "originalValue" => $value['originalValue'] ?? "",
+                    "purchaseTime" => $value['purchaseTime'] ?? "",
+                    "remark" => $value['remark'] ?? "",
+                    "singleCode" => $value['singleCode'] ?? "",
+                    "startUseDate" => $value['startUseDate'] ?? "",
+                    "type" => $value['type'] ?? "",
+                    "useDept" => $value['useDept'] ?? "",
+                    "userName" => $value['userName'] ?? "",
+                    "version" => $value['version'] ?? "",
+                ];
             }
 
             // 在处理完每个批次后,进行一些内存清理操作
             unset($batch);
+            if(! empty($insert)) {
+                echo "insert Temp " . PHP_EOL;
+                AssetTemp::insert($insert);
+            }
         }
     }
 
+    //正式数据插入或更新
+    public function assetInsert(){
+        DB::table('asset_temp')
+            ->select('*')
+            ->orderBy('id','desc')
+            ->chunk(500,function ($data){
+                $data_array = $data->toArray();
+
+                $update = $insert = [];
+                foreach ($data_array as $value){
+                    $bool = Asset::where('singleCode',$value->singleCode)->exists();
+                    if($bool){
+                        $update[] = [
+                            "assetCode" => $value->assetCode ?? "",
+                            "assetNo" => $value->assetNo ?? "",
+                            "assetType" => $value->assetType ?? "",
+                            "brand" => $value->brand ?? "",
+                            "expectedLife" => $value->expectedLife ?? "",
+                            "gs1" => $value->gs1 ?? "",
+                            "isKey" => $value->isKey ?? "",
+                            "kind" => $value->kind ?? "",
+                            "located" => $value->located ?? "",
+                            "name" => $value->name ?? "",
+                            "nextCalibrationTime" => $value->nextCalibrationTime ?? "",
+                            "originalValue" => $value->originalValue ?? "",
+                            "purchaseTime" => $value->purchaseTime ?? "",
+                            "remark" => $value->remark ?? "",
+                            "singleCode" => $value->singleCode ?? "",
+                            "startUseDate" => $value->startUseDate ?? "",
+                            "type" => $value->type ?? "",
+                            "useDept" => $value->useDept ?? "",
+                            "userName" => $value->userName ?? "",
+                            "version" => $value->version ?? "",
+                            "del_time" => 0,
+                        ];
+                    }else{
+                        $insert[] = [
+                            "assetCode" => $value->assetCode ?? "",
+                            "assetNo" => $value->assetNo ?? "",
+                            "assetType" => $value->assetType ?? "",
+                            "brand" => $value->brand ?? "",
+                            "expectedLife" => $value->expectedLife ?? "",
+                            "gs1" => $value->gs1 ?? "",
+                            "isKey" => $value->isKey ?? "",
+                            "kind" => $value->kind ?? "",
+                            "located" => $value->located ?? "",
+                            "name" => $value->name ?? "",
+                            "nextCalibrationTime" => $value->nextCalibrationTime ?? "",
+                            "originalValue" => $value->originalValue ?? "",
+                            "purchaseTime" => $value->purchaseTime ?? "",
+                            "remark" => $value->remark ?? "",
+                            "singleCode" => $value->singleCode ?? "",
+                            "startUseDate" => $value->startUseDate ?? "",
+                            "type" => $value->type ?? "",
+                            "useDept" => $value->useDept ?? "",
+                            "userName" => $value->userName ?? "",
+                            "version" => $value->version ?? "",
+                            "del_time" => 0,
+                        ];
+                    }
+                }
+                if(! empty($update)){
+                    foreach ($update as $value){
+                        Asset::where('singleCode',$value['singleCode'])->update($value);
+                    }
+                }
+                if(! empty($insert)) Asset::insert($insert);
+
+                echo "insert OR update ing" . PHP_EOL;
+            });
+    }
+
     protected function echoMessage(OutputInterface $output)
     {
         $output->writeln($this->data);

+ 14 - 0
app/Model/AssetTemp.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class AssetTemp extends Model
+{
+    protected $guarded = [];
+    protected $table = "asset_temp"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+}

+ 2 - 4
app/Service/AssetService.php

@@ -5,10 +5,8 @@ namespace App\Service;
 
 use App\Jobs\AssetDeviceJob;
 use App\Model\Asset;
-use App\Model\InventoryOrder;
 use App\Model\InventoryOrderAsset;
 use App\Model\Settings;
-use Illuminate\Support\Facades\Redis;
 
 class AssetService extends Service
 {
@@ -128,8 +126,8 @@ class AssetService extends Service
 
         $located = $dep = [];
         foreach ($list as $value){
-            if(! in_array($value['located'], $located)) $located[] = $value['located'];
-            if(! in_array($value['useDept'], $dep)) $dep[] = $value['useDept'];
+            if(! empty($value['located']) && ! in_array($value['located'], $located)) $located[] = $value['located'];
+            if(! empty($value['useDept']) && ! in_array($value['useDept'], $dep)) $dep[] = $value['useDept'];
         }
 
         return [true,['located' => $located,'dep' => $dep]];