<?php
/**
 *
 * @author：llbjj
 * @DateTime：2022/5/6 10:57
 * @Description：
 *
 */

namespace Application\Service\DB\Tmp;
use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\Operator;

class Doctoradviceformal extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_tmp_doctoradvice_formal';
        parent::__construct($dbAdapter, $container);
    }

    /**
     * Notes : 明细数据
     * @param string $patient_id
     * @param string $drug_id
     * @return false
     * Author:唐伯虎不虎
     * Date:2024/2/2 11:37
     */
    public function getlockdrugData(string $patient_id,string $drug_id,$source = 0)
    {
        if(empty($patient_id) || empty($drug_id)) return false;

        $lockWhere['where'] = ['is_del'=>0,'patient_id'=>$patient_id,'drug_id'=>$drug_id];
        $lockWhere['columns'] = ['id','medical_date','end_date','dosage_unit','dosage_way',
            'dosage_times','is_dispensary','adjust_num','dosage_num','dosage_medication'];

        $lockWhere['order'] = ['medical_date','end_date'];
        $datas = $this->fetchAll($lockWhere);



        $drug_name = $this->LocalService()->dictionaryDrug->getOneFieldVal('name',['is_del'=>0,'id'=>$drug_id]);

        $takeDatas = $this->LocalService()->dictionaryMedicaltake->fetchAll(['where' => ['is_del'=>0,'take_type'=>[2,3,4,6]], 'columns' => ['id','name','take_number']]);

        $quick_takeData = [];

        foreach($takeDatas as $k=>$v){
            $quick_takeData[$v['id']] = $v;
        }

        if($source == 1){
            $patient_name = $this->LocalService()->tmpDoctoradvicepatient->getOneFieldVal('name',['is_del'=>0,'id'=>$patient_id]);
        }

        if(!empty($datas)){
            foreach($datas as $k=>&$v){
                if($source == 1){
                    unset($datas[$k]['adjust_num'],$datas[$k]['adjust_num']);
                    $v['patient_name'] = $patient_name;
                }
                //查询药物名称
                $v['drug_name'] = $drug_name;
                //频次
                if(!empty($v['dosage_times'])){
                    $v['dosage_times'] = $quick_takeData[$v['dosage_times']]['name'];
                    if($source == 0){
                        $v['dosage_times_number'] = $quick_takeData[$v['dosage_times']]['take_number'];
                    }

                }
                //计量单位
                if(!empty($v['dosage_unit'])) $v['dosage_unit'] = $quick_takeData[$v['dosage_unit']]['name'];
                //服用方式
                if(!empty($v['dosage_way'])) $v['dosage_way'] = $quick_takeData[$v['dosage_way']]['name'];
                //服用方式
                if(!empty($v['dosage_medication'])) $v['dosage_medication'] = $quick_takeData[$v['dosage_medication']]['name'];
                if($source == 0){
                    if(empty($v['dosage_times_number'])) $v['dosage_times_number'] = 1;

                }
            }
        }
        return $datas;
    }


    public function getdormaldrugData($patient_id,$drug_id)
    {
        if(empty($patient_id) || empty($drug_id)) return false;

        $datas = $this->getlockdrugData($patient_id,$drug_id,1);

        return $datas;
    }

    /**
     * Notes : 汇总数据数据
     * @param string $patient_id
     * @param string $drug_id
     * @return false
     * Author:唐伯虎不虎
     * Date:2024/2/2 11:37
     */
    public function getgatherData($patient_id,$limit,$offset)
    {

        $lockWhere['where'] = ['is_del'=>0];
        if(!empty($patient_id)){
            $lockWhere['where']['patient_id'] = $patient_id;
        }
        $lockWhere['columns'] = ['id','medical_date','end_date','dosage_unit','dosage_way','drug_id',
            'dosage_times','is_dispensary','adjust_num','dosage_num','dosage_medication','itemsig_id','patient_id'];
        if(!empty($limit)){
            $lockWhere['limit'] = $limit;
            $lockWhere['offset'] = $offset;
        }
        if(empty($patient_id)){
            $lockWhere['order'] = ['patient_id','medical_date'];
        }else{
            $lockWhere['order'] = ['medical_date'];
        }

        $datas = $this->fetchAll($lockWhere);

        if(empty($datas)) return [];

        $drugStr = array_values(array_unique(array_column($datas,'drug_id')));

        if(empty($patient_id)){
            $drugDats = $this->LocalService()->dictionaryDrug->fetchAll(['where'=>['id'=>$drugStr],'columns'=>['id','name','english','category_id']]);

            $categoryidStr = array_values(array_unique(array_column($drugDats,'category_id')));

            $drugNames = [];
            foreach($drugDats as $k=>$v){
                $drugNames[$v['id']] = $v;
            }

            $categoryDatas = $this->LocalService()->dictionaryDrugcategory->fetchCol('category_name',['id'=>$categoryidStr]);
        }else{
            $drug_name = $this->LocalService()->dictionaryDrug->fetchCol('name',['id'=>$drugStr]);
        }


        $takeDatas = $this->LocalService()->dictionaryMedicaltake->fetchAll(['where' => ['is_del'=>0,'take_type'=>[2,3,4,6]], 'columns' => ['id','name','english','take_number']]);

        $quick_takeData = [];

        foreach($takeDatas as $k=>$v){
            $quick_takeData[$v['id']] = $v;
        }

        //查询受试者
        if(empty($patient_id)){
            $export = [];
            $itemsig_name = $this->LocalService()->tmpDoctoradvicepatient->fetchCol('name',['is_del'=>0,'pid'=>0]);

            $patient_name = $this->LocalService()->tmpDoctoradvicepatient->fetchCol('name',['is_del'=>0,new Operator('pid',Operator::OP_GT,0)]);

        }

        if(!empty($datas)){
            foreach($datas as $k=>&$v){
                if(!empty($patient_id)){
                    //查询药物名称
                    $v['name'] = $drug_name[$v['drug_id']];
                    //频次
                    if(!empty($v['dosage_times'])){
                        $v['dosage_times'] = $quick_takeData[$v['dosage_times']]['name'];
                    }
                    //计量单位
                    if(!empty($v['dosage_unit'])) $v['dosage_unit'] = $quick_takeData[$v['dosage_unit']]['name'];
                    //服用方式
                    if(!empty($v['dosage_way'])) $v['dosage_way'] = $quick_takeData[$v['dosage_way']]['name'];
                    //服用方式
                    if(!empty($v['dosage_medication'])) $v['dosage_medication'] = $quick_takeData[$v['dosage_medication']]['name'];

                    $v['is_dispensary'] = ($v['is_dispensary'] > 0) ? '是' : '否';
                    $v['is_continue'] = ($v['is_continue'] > 0) ? '是' : '否';
                }else{
                    $hearder = ['中心','受试者','药物名称','服用时间','剂量','计量单位','服用频率','用药方式','取药（出院带药）'];
                    $export[$k][] = $itemsig_name[$v['itemsig_id']];
                    $export[$k][] = $patient_name[$v['patient_id']];
                    $export[$k][] = $drugNames[$v['drug_id']]['name'].'('.$categoryDatas[$drugNames[$v['drug_id']]['category_id']].')';
                    $export[$k][] = $drugNames[$v['drug_id']]['english'];
                    $export[$k][] = $v['medical_date'];
                    $export[$k][] = $v['dosage_num'];

                    $export[$k][] = !empty($quick_takeData[$v['dosage_unit']]['english']) ? $quick_takeData[$v['dosage_unit']]['name'].'('.$quick_takeData[$v['dosage_unit']]['english'].')' : $quick_takeData[$v['dosage_unit']]['name'];

                    $export[$k][] = !empty($quick_takeData[$v['dosage_times']]['english']) ? $quick_takeData[$v['dosage_times']]['name'].'('.$quick_takeData[$v['dosage_times']]['english'].')' : $quick_takeData[$v['dosage_times']]['name'];

                    $export[$k][] = !empty($quick_takeData[$v['dosage_medication']]['english']) ? $quick_takeData[$v['dosage_medication']]['name'].'('.$quick_takeData[$v['v']]['english'].')' : $quick_takeData[$v['dosage_medication']]['name'];

                    $export[$k][] = ($v['is_dispensary'] > 0) ? '是' : '否';

                    $export[$k][] = ($v['is_continue'] > 0) ? '是' : '否';

                }

            }
        }
        if(empty($patient_id)){
            unset($datas);
            return $export;
        }else{
            return $datas;
        }

    }

    public function getgatherData_export($patient_id,$limit,$offset)
    {

        $lockWhere['where'] = ['is_del'=>0];
        if(!empty($patient_id)){
            $lockWhere['where']['patient_id'] = $patient_id;
        }
        $lockWhere['columns'] = ['id','medical_date','end_date','dosage_unit','dosage_way','drug_id',
            'dosage_times','is_dispensary','adjust_num','dosage_num','dosage_medication','itemsig_id','patient_id'];
        if(!empty($limit)){
            $lockWhere['limit'] = $limit;
            $lockWhere['offset'] = $offset;
        }
        if(empty($patient_id)){
            $lockWhere['order'] = ['patient_id','medical_date'];
        }else{
            $lockWhere['order'] = ['medical_date'];
        }

        $datas = $this->LocalService()->tmpDoctoradvicelock->fetchAll($lockWhere);

        if(empty($datas)) return [];

        $drugStr = array_values(array_unique(array_column($datas,'drug_id')));

        if(empty($patient_id)){
            $drugDats = $this->LocalService()->dictionaryDrug->fetchAll(['where'=>['id'=>$drugStr],'columns'=>['id','name','english','category_id']]);

            $categoryidStr = array_values(array_unique(array_column($drugDats,'category_id')));

            $drugNames = [];
            foreach($drugDats as $k=>$v){
                $drugNames[$v['id']] = $v;
            }

            $categoryDatas = $this->LocalService()->dictionaryDrugcategory->fetchCol('category_name',['id'=>$categoryidStr]);
        }else{
            $drug_name = $this->LocalService()->dictionaryDrug->fetchCol('name',['id'=>$drugStr]);
        }


        $takeDatas = $this->LocalService()->dictionaryMedicaltake->fetchAll(['where' => ['is_del'=>0,'take_type'=>[2,3,4,6]], 'columns' => ['id','name','english','take_number']]);

        $quick_takeData = [];

        foreach($takeDatas as $k=>$v){
            $quick_takeData[$v['id']] = $v;
        }

        //查询受试者
        if(empty($patient_id)){
            $export = [];
            $itemsig_name = $this->LocalService()->tmpDoctoradvicepatient->fetchCol('name',['is_del'=>0,'pid'=>0]);

            $patient_name = $this->LocalService()->tmpDoctoradvicepatient->fetchCol('name',['is_del'=>0,new Operator('pid',Operator::OP_GT,0)]);

        }

        if(!empty($datas)){
            foreach($datas as $k=>&$v){
                if(!empty($patient_id)){
                    //查询药物名称
                    $v['name'] = $drug_name[$v['drug_id']];
                    //频次
                    if(!empty($v['dosage_times'])){
                        $v['dosage_times'] = $quick_takeData[$v['dosage_times']]['name'];
                    }
                    //计量单位
                    if(!empty($v['dosage_unit'])) $v['dosage_unit'] = $quick_takeData[$v['dosage_unit']]['name'];
                    //服用方式
                    if(!empty($v['dosage_way'])) $v['dosage_way'] = $quick_takeData[$v['dosage_way']]['name'];
                    //服用方式
                    if(!empty($v['dosage_medication'])) $v['dosage_medication'] = $quick_takeData[$v['dosage_medication']]['name'];

                    $v['is_dispensary'] = ($v['is_dispensary'] > 0) ? '是' : '否';
                    $v['is_continue'] = ($v['is_continue'] > 0) ? '是' : '否';
                }else{
                    $hearder = ['中心','受试者','药物名称','服用时间','剂量','计量单位','服用频率','用药方式','取药（出院带药）'];
                    $export[$k][] = $itemsig_name[$v['itemsig_id']];
                    $export[$k][] = $patient_name[$v['patient_id']];
                    $export[$k][] = $drugNames[$v['drug_id']]['name'].'('.$categoryDatas[$drugNames[$v['drug_id']]['category_id']].')';
                    $export[$k][] = $drugNames[$v['drug_id']]['english'];
                    $export[$k][] = $v['medical_date'];
                    $export[$k][] = $v['dosage_num'];

                    $export[$k][] = !empty($quick_takeData[$v['dosage_unit']]['english']) ? $quick_takeData[$v['dosage_unit']]['name'].'('.$quick_takeData[$v['dosage_unit']]['english'].')' : $quick_takeData[$v['dosage_unit']]['name'];

                    $export[$k][] = !empty($quick_takeData[$v['dosage_times']]['english']) ? $quick_takeData[$v['dosage_times']]['name'].'('.$quick_takeData[$v['dosage_times']]['english'].')' : $quick_takeData[$v['dosage_times']]['name'];

                    $export[$k][] = !empty($quick_takeData[$v['dosage_medication']]['english']) ? $quick_takeData[$v['dosage_medication']]['name'].'('.$quick_takeData[$v['v']]['english'].')' : $quick_takeData[$v['dosage_medication']]['name'];

                    $export[$k][] = ($v['is_dispensary'] > 0) ? '是' : '否';

                    $export[$k][] = ($v['is_continue'] > 0) ? '是' : '否';

                }

            }
        }
        if(empty($patient_id)){
            unset($datas);
            return $export;
        }else{
            return $datas;
        }

    }
}