<?php

namespace App\Console\Commands;

use App\Models\InquiryModel;
use App\Models\PatientModel;
use App\Models\PrescriptionModel;
use Illuminate\Console\Command;
use App\Common\Util;

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()
    {
        // 刷新问诊问题
        // $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);
        $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 "没有需要更新的数据";
        }
    }   
}
