<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderMedicalReport extends BaseModel
{
    use HasFactory;

    public function anomaly(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => $this->anomalyTranfer($value),
        );
    }

    public function anomalyTranfer($value)
    {
        $arr = [
            'sbp'=>"收缩压",'dbp'=>"舒张压",'fbg'=>'空腹血糖', 'urea'=>'尿素', 'ua'=>'尿酸', 'cre'=>"肌酐", 'cvsc'=>'胱抑素C', 'tc'=>'总胆固醇', 'tg'=>'甘油三脂',
            'hdl'=>'高密度脂蛋白', 'ldl'=>'低密度脂蛋白', 'tbil'=>'总胆红素', 'dbil'=>'直接胆红素', 'alt'=>'谷丙转氨酶', 'ast'=>'谷草转氨酶', 'wbc'=>'白细胞计数', 'rbc'=>'红细胞计数', 'hgb'=>'血红蛋白', 'hct'=>'红细胞比积', 'mcv'=>'平均RBC体积', 'mch'=>'平均RBC血红蛋白量',
            'mchc'=>'平均RBC血红蛋白浓度', 'rdw'=>'RBC分布宽度标准差', 'rdwcv'=>'RBC分布宽度变异系数', 'plt'=>'血小板计数', 'mpv'=>'平均血小板体积', 'pdw'=>'血小板体积分布宽带', 'plcr'=>'大血小板比率', 'pct'=>'血小板比积', 'gr'=>'中性粒细胞计数', 'gr_p'=>'中性粒细胞比值', 'ly'=>'淋巴细胞计数', 'ly_p'=>'淋巴细胞比值',
            'mono'=>'单核细胞计数', 'mono_p'=>'单核细胞比值', 'eos'=>'嗜酸粒细胞计数', 'eos_p'=>'嗜酸睡粒细胞比值', 'baso'=>'嗜碱性粒细胞计数', 'baso_p'=>'嗜碱性粒细胞比值'
        ];
        $anomaly_arr = json_decode($value)?:[];
        if (count($anomaly_arr)) {
            $data_arr = [];
            foreach ($anomaly_arr as $anomaly) {
                $data = [];
                foreach ($anomaly as $an) {
                    $data[] = $arr[$an];
                }
                $data_arr[] = $data;
            }
            $anomaly_arr = $data_arr;
        }
        return $anomaly_arr;
    }
}
