Ver código fonte

保存药检报告数据

LAPTOP-VT1IP978\suxio 2 semanas atrás
pai
commit
08d5fdca59

+ 4 - 3
app/Http/Controllers/AlihealthController.php

@@ -9,8 +9,9 @@ class AlihealthController extends Controller
 {
     public function querySealDrugReport(Request $request, AlihealthService $service)
     {
-        $beginTime = "2025-09-01 00:00:00";
-        $endTime = "2025-09-30 23:59:59";
-        $service->querySealDrugReport($beginTime, $endTime);
+        $beginTime = "2025-03-01 00:00:00";
+        $endTime = "2025-03-31 23:59:59";
+        $data = $service->querySealDrugReport($beginTime, $endTime);
+        dd($data);
     }
 }

+ 12 - 8
app/Models/DrugReportInfo.php

@@ -8,20 +8,20 @@ use Illuminate\Database\Eloquent\Model;
  * App\Models\DrugReportInfo
  *
  * @property int $id
- * @property int $drug_report_v2_id 药检报告ID
+ * @property string $drug_report_v2_id 药检报告ID
+ * @property string $drug_report_name 报告名称
+ * @property string $drug_id 药品ID
  * @property string $drug_name 药品名称
+ * @property string $prod_code 药品子类编码
  * @property string $pkg_spec 包装规格
  * @property string $prepn_spec 制剂规格
- * @property string $drug_ent_base_info_id 药品ID
- * @property string $prod_code 药品子类编码
- * @property string $pkg_ratio_list 包装比例
- * @property string $drug_report_name 报告名称
+ * @property array|null $pkg_ratio_list 包装比例
  * @property string $batch_no 批次号
- * @property string $sealed_report_url 盖章报告链接
+ * @property string $report_id 报告ID
  * @property string $report_no 报告编号
  * @property string $report_date 报告日期
- * @property string $report_id 报告ID
  * @property int $is_seal 是否盖章(1-未盖章、2-已盖章)
+ * @property string $sealed_report_url 盖章报告链接
  * @property \Illuminate\Support\Carbon $create_time 创建时间
  * @property \Illuminate\Support\Carbon $update_time 更新时间
  * @method static \Illuminate\Database\Eloquent\Builder|DrugReportInfo newModelQuery()
@@ -29,7 +29,7 @@ use Illuminate\Database\Eloquent\Model;
  * @method static \Illuminate\Database\Eloquent\Builder|DrugReportInfo query()
  * @method static \Illuminate\Database\Eloquent\Builder|DrugReportInfo whereBatchNo($value)
  * @method static \Illuminate\Database\Eloquent\Builder|DrugReportInfo whereCreateTime($value)
- * @method static \Illuminate\Database\Eloquent\Builder|DrugReportInfo whereDrugEntBaseInfoId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|DrugReportInfo whereDrugId($value)
  * @method static \Illuminate\Database\Eloquent\Builder|DrugReportInfo whereDrugName($value)
  * @method static \Illuminate\Database\Eloquent\Builder|DrugReportInfo whereDrugReportName($value)
  * @method static \Illuminate\Database\Eloquent\Builder|DrugReportInfo whereDrugReportV2Id($value)
@@ -55,4 +55,8 @@ class DrugReportInfo extends Model
     const CREATED_AT = 'create_time';
 
     const UPDATED_AT = 'update_time';
+
+    protected $casts = [
+        'pkg_ratio_list' => 'array',
+    ];
 }

+ 12 - 3
app/Services/AlihealthService.php

@@ -28,9 +28,10 @@ class AlihealthService
      * @param int $page
      * @param int $pageSize
      * @param int $isSeal
-     * @return void
+     * @return array
+     * @throws \Exception
      */
-    public function querySealDrugReport(string $beginTime, string $endTime, int $page = 1, int $pageSize = 20, int $isSeal = 0)
+    public function querySealDrugReport(string $beginTime, string $endTime, int $page = 1, int $pageSize = 20, int $isSeal = 0): array
     {
         $req = new AlibabaAlihealthSynergyYzwQuerysealdrugreportRequest;
         $req->setRefEntId($this->config['enterprise_id']);
@@ -44,7 +45,15 @@ class AlihealthService
         }
 
         $resp = $this->client->execute($req);
+        if ('SUCCESS' <> $resp->result->msg_code) {
+            throw new \Exception($resp->result->msg_info);
+        }
+
+        $data = json_decode(json_encode(
+            $resp->result->model->result_list->ocr_seal_drug_report_d_t_o
+        ), true);
+        app(DrugReportInfoService::class)->bulkInsert($data);
 
-        dd($resp);
+        return (array)$resp;
     }
 }

+ 26 - 2
app/Services/DrugReportInfoService.php

@@ -3,6 +3,7 @@
 namespace App\Services;
 
 use App\Models\DrugReportInfo;
+use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\DB;
 
 class DrugReportInfoService
@@ -22,7 +23,30 @@ class DrugReportInfoService
         });
     }*/
 
-    public function bulkInsert(array $data) {
-        DrugReportInfo::insert($data);
+    public function bulkInsert(array $data): bool
+    {
+        $data = self::correct($data);
+        return DrugReportInfo::insert($data);
+    }
+
+    public static function correct(array $data): array
+    {
+        return array_map(function ($item) {
+            return [
+                'drug_report_v2_id' => $item['drug_report_v2_id'] ?? '',
+                'drug_report_name' => $item['drug_report_name'] ?? '',
+                'drug_id' => $item['drug_ent_base_info_id'] ?? '',
+                'drug_name' => $item['drug_name'] ?? '',
+                'prod_code' => $item['prod_code'] ?? '',
+                'pkg_spec' => $item['pkg_spec'] ?? '',
+                'prepn_spec' => $item['prepn_spec'] ?? '',
+                'pkg_ratio_list' => json_encode($item['pkg_ratio_list'] ?? [], JSON_UNESCAPED_UNICODE),
+                'batch_no' => $item['batch_no'] ?? '',
+                'report_id' => $item['report_id'] ?? '',
+                'report_no' => $item['report_no'] ?? '',
+                'report_date' => $item['report_date'] ?? '',
+                'sealed_report_url' => $item['sealed_report_url'] ?? '',
+            ];
+        }, $data);
     }
 }

+ 2 - 1
composer.json

@@ -13,7 +13,8 @@
         "fruitcake/laravel-cors": "^2.0",
         "guzzlehttp/guzzle": "^7.0.1",
         "laravel/framework": "^8.12",
-        "laravel/tinker": "^2.0"
+        "laravel/tinker": "^2.0",
+        "ext-json": "*"
     },
     "require-dev": {
         "barryvdh/laravel-ide-helper": "^2.14",