<?php

namespace App\Console\Commands;

use App\Models\InquiryModel;
use App\Models\PatientModel;
use App\Models\PrescriptionModel;
use App\Services\ExternalService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class TestCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mohe:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '测试命令';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        print_r("执行开始...\n");
        // 使用方法
        $eHospitalApi = new ExternalService('jymd_15700159057_test', 'heU0zM9OrLrK0JehxiAXlPd3L48HHmcA', 'https://ehospital-openapi-test.sq580.com');
        // 问诊
        $response = $eHospitalApi->getMedicalRecord([
            // 根据文档填写必要的参数
            "beginTime"=>"1732982400000",
            "endTime"=>"1859212800000",
            "pageNum"=>1,
            "pageSize"=>20,
        ]);
        print_r($response);
        // Log::info('问诊信息:'.json_encode($response));
        foreach ($response['data']['list'] as $item) {
            print_r($item['visitNo']);
            
        }
        // // 西药
        // $response = $eHospitalApi->createPrescriptionWm([
        //     // 根据文档填写必要的参数
        //     "beginTime"=>"1732982400000",
        //     "endTime"=>"1859212800000",
        //     "pageNum"=>1,
        //     "pageSize"=>20,
        // ]);
        // print_r($response);
        // Log::info('处方信息（西药）:'.json_encode($response));
        // // 中药
        // $response = $eHospitalApi->createPrescriptionCm([
        //     // 根据文档填写必要的参数
        //     "beginTime"=>"1732982400000",
        //     "endTime"=>"1859212800000",
        //     "pageNum"=>1,
        //     "pageSize"=>20,
        // ]);
        // print_r($response);
        // Log::info('处方信息（中药）:'.json_encode($response));
        // 检查接口调用是否成功
        // if (isset($response['err']) && $response['err'] == 0) {
        //     echo "接口调用成功。\n";
        //     // 如果需要处理数据，则可以在这里进行
        //     if (isset($response['data'])) {
        //         print_r("数据: \n");
        //         print_r($response['data']);
        //     }
        // } else {
        //     echo "接口调用失败。\n";
        //     if (isset($response['errmsg'])) {
        //         echo "错误信息: " . $response['errmsg'] . "\n";
        //     } else {
        //         echo "未能获取详细的错误信息。\n";
        //     }
        // }
        print_r("执行结束...\n");
        // 刷新问诊问题
        // $prescriptions = PrescriptionModel::orderBy('id', 'desc')->get();
        // foreach ($prescriptions as $prescription) {
        //     $inquiry_info = $prescription->inquiry_info;
        //     foreach ($inquiry_info as &$v) {
        //         $v['question'] = InquiryModel::find($v['inquiry_id'])->question;
        //     }
        //     $prescription->inquiry_info = $inquiry_info;
        //     $prescription->save();
        // }

        // 刷新存量问诊人年龄

        // $query = PatientModel::query()->whereNotNull('id_card')->where('age', '<=', 0)->withTrashed();
        // $sql = $query->toSql();
        // var_dump($sql);
        // $patients = $query->get();
        // if ($patients->count() > 0) {
        //     foreach ($patients as $patient) {
        //         $patient->age = getAgeByIdCard($patient->id_card);
        //         $patient->save();
        //     }
        // } else {
        //     echo '没有需要更新的数据';
        // }
    }
}
