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

namespace Application\Service\DB\Medical;
use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;
use Laminas\Stdlib\ArrayUtils;
use Application\Common\Com;
class Lock extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_medical_lock';
        parent::__construct($dbAdapter, $container);
    }

    /**
     * Notes: 获取指定条件下的锁定数据
     * User: lltyy
     * DateTime: 2023/5/18 8:56
     *
     * @param $where
     * @return array[]
     */
    public function fetchLockByMedical($where,$columns){
        $datas = [];
        //获取指定条件下的锁定数据
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>['ocrdata_id']
        ];
        $lockdatas = $this->fetchAll($cond);
        if(!empty($lockdatas)){
            foreach($lockdatas as &$lockdata) {
                $medical_id = $lockdata['ocrdata_id'];
                $datas[$medical_id] = $lockdata;
            }
        }
        unset($cond);
        unset($lockdatas);
        return $datas;
    }

    /**
     * Notes: 获取中心患者的药物信息
     * User: lltyy
     * DateTime: 2023/11/5 14:21
     *
     * @param array $info
     * @return array[]
     */
    public function fetchDrugDataByPatientSig(array $info=[]){
        $no_annex = isset($info['no_annex']) && !empty($info['no_annex']) ? $info['no_annex'] : 0;
        $drug_id = isset($info['drug_id']) && !empty($info['drug_id']) ? $info['drug_id'] : 0;
        $data_type = isset($info['data_type']) && !empty($info['data_type']) ? $info['data_type'] : 0;
        $sig_id = isset($info['itemsig_id']) && !empty($info['itemsig_id']) ? $info['itemsig_id'] : 0;
        $patient_id = isset($info['patient_id']) && !empty($info['patient_id']) ? $info['patient_id'] : 0;
        $drug_arr = isset($info['drug_arr']) && !empty($info['drug_arr']) ? $info['drug_arr'] : [];
        $arr = [
            'sig'=>[],
            'patient'=>[],
            'all'=>[],
            'patient_drug'=>[],
            'lock_drug'=>[]
        ];
        $where = [
            'is_del' => 0,
        ];
        if(!empty($no_annex)){
            $where['annex_id'] = $no_annex;
        }
        if(!empty($drug_id)){
            $where['drug_id'] = $drug_id;
        }
        if(!empty($drug_arr)){
            $where['drug_id'] = StringHelper::toArray($drug_arr);
        }
        if($data_type == 1){
            if(!empty($sig_id)){
                $where['itemsig_id'] = $sig_id;
            }
            if(!empty($patient_id)){
                $where['patient_id'] = $patient_id;
            }
        }
        $cond = [
            'where'=>$where,
            'columns'=>['itemsig_id','patient_id','drug_id','medical_date','end_date','day_num','unit_id','times_id','is_now_use','is_adverse','is_dispensary'],
            'order'=>['create_time desc','update_time desc']
        ];
        $lockDatas = $this->fetchAll($cond);
        unset($where);
        unset($cond);
        if(!empty($lockDatas)){
            $patientDrugDatas = $takeNameArr = [];
            $takeInfos = $this->LocalService()->ocrTake->fetchAll(['where'=> [
                'is_del' => 0,
                'take_type' => [1, 2, 3],
            ],'columns'=>['id','english','name']]);
            if(!empty($takeInfos)){
                foreach($takeInfos as $takeKey=>$takeInfo){
                    $takeId = $takeInfo['id'];
                    $takeNameArr[$takeId] = !empty($takeInfo['english']) ? $takeInfo['english'] : $takeInfo['name'];
                    unset($takeInfos[$takeKey]);
                }
            }
            unset($takeInfos);

            foreach($lockDatas as $lockKey=>$lockData){
                $lock_sig = $lockData['itemsig_id'];
                $lock_patient = $lockData['patient_id'];
                $lock_drug = $lockData['drug_id'];
                if(!isset($arr['sig'][$lock_sig][$lock_drug])){
                    $arr['sig'][$lock_sig][$lock_drug] = $lockData;
                }
                if(!isset($arr['patient'][$lock_patient][$lock_drug])){
                    $arr['patient'][$lock_patient][$lock_drug] = $lockData;
                }
                if(!isset($arr['all'][$lock_drug])){
                    $arr['all'][$lock_drug] = $lockData;
                }

                //患者药物已存信息
                $medical_date = !is_null($lockData['medical_date']) && !empty($lockData['medical_date']) ? $lockData['medical_date'] : 0;
                if(!empty($medical_date)){
                    $patientDrugDatas[$lock_patient][$lock_drug]['start_date'][] = $medical_date;
                }
                $end_date = !is_null($lockData['end_date']) && !empty($lockData['end_date']) ? $lockData['end_date'] : 0;
                if(!empty($end_date)){
                    $patientDrugDatas[$lock_patient][$lock_drug]['end_date'][] = $end_date;
                }
                $dosage_num = !is_null($lockData['day_num']) && !empty($lockData['day_num']) ? $lockData['day_num'] : 0;
                $dosage_unit = !is_null($lockData['unit_id']) && !empty($lockData['unit_id']) ? $lockData['unit_id'] : 0;
                if(!empty($dosage_num)){
                    if(!empty($dosage_unit) && isset($takeNameArr[$dosage_unit]) && !empty($takeNameArr[$dosage_unit])){
                        $patientDrugDatas[$lock_patient][$lock_drug]['day_num'][] = $dosage_num.$takeNameArr[$dosage_unit];
                    }else{
                        $patientDrugDatas[$lock_patient][$lock_drug]['day_num'][] = $dosage_num;
                    }
                }

                $times_id = !is_null($lockData['times_id']) && !empty($lockData['times_id']) ? $lockData['times_id'] : 0;
                if(!empty($times_id)){
                    $patientDrugDatas[$lock_patient][$lock_drug]['times_id'][] = $times_id;
                }
                if($lockData['is_dispensary'] == 1){
                    $patientDrugDatas[$lock_patient][$lock_drug]['is_dispensary'] = 1;
                }
                unset($lockDatas[$lockKey]);
            }

            if(!empty($patientDrugDatas)){
                foreach($patientDrugDatas as $patientId=>$lockDrugDatas){
                    foreach($lockDrugDatas as $lockDrugId=>$lockDrugData){
                        $lock_mes_arr = [];//展示信息

                        //开始日期
                        $drug_startdate_arr = isset($lockDrugData['start_date']) && !empty($lockDrugData['start_date']) ? $lockDrugData['start_date'] : [];
                        $drug_startdate_arr = array_unique($drug_startdate_arr);
                        $drug_startdate_arr = array_values($drug_startdate_arr);
                        sort($drug_startdate_arr);
                        if(!empty($drug_startdate_arr)){
                            $lock_mes_arr[] = '（'.$drug_startdate_arr[0].'）';
                        }
                        $arr['lock_drug'][$patientId][$lockDrugId]['medical_date'] = !empty($drug_startdate_arr) ? $drug_startdate_arr[0] : '';
                        unset($drug_startdate_arr);

                        //剂量
                        $drug_dosagenum_arr = isset($lockDrugData['day_num']) && !empty($lockDrugData['day_num']) ? $lockDrugData['day_num'] : [];
                        $drug_dosagenum_arr = array_unique($drug_dosagenum_arr);
                        $drug_dosagenum_arr = array_values($drug_dosagenum_arr);
                        if(!empty($drug_dosagenum_arr)){
                            $lock_mes_arr[] = '（'.implode('/',$drug_dosagenum_arr).'）';
                        }
                        $arr['lock_drug'][$patientId][$lockDrugId]['day_num'] = $drug_dosagenum_arr;
                        unset($drug_dosagenum_arr);

                        //服用频率
                        $drug_timesid_arr = isset($lockDrugData['times_id']) && !empty($lockDrugData['times_id']) ? $lockDrugData['times_id'] : [];
                        $drug_timesid_arr = array_unique($drug_timesid_arr);
                        $drug_timesid_arr = array_values($drug_timesid_arr);
                        $drug_timesid_name = [];
                        if(!empty($drug_timesid_arr)){
                            foreach($drug_timesid_arr as $drug_timesid){
                                if(isset($takeNameArr[$drug_timesid]) && !empty($takeNameArr[$drug_timesid])){
                                    $drug_timesid_name[] = $takeNameArr[$drug_timesid];
                                }
                            }
                            $lock_mes_arr[] = '（'.implode('/',$drug_timesid_name).'）';
                        }
                        $arr['lock_drug'][$patientId][$lockDrugId]['times_id'] = $drug_timesid_name;
                        unset($drug_timesid_arr);
                        unset($drug_timesid_name);

                        //结束日期
                        $drug_enddate_arr = isset($lockDrugData['end_date']) && !empty($lockDrugData['end_date']) ? $lockDrugData['end_date'] : [];
                        $drug_enddate_arr = array_unique($drug_enddate_arr);
                        $drug_enddate_arr = array_values($drug_enddate_arr);
                        rsort ($drug_enddate_arr);
                        if(!empty($drug_enddate_arr)){
                            $lock_mes_arr[] = '（'.$drug_enddate_arr[0].'）';
                        }
                        $arr['lock_drug'][$patientId][$lockDrugId]['end_date'] = !empty($drug_enddate_arr) ? $drug_enddate_arr[0] : '';
                        unset($drug_enddate_arr);

                        //取药
                        $arr['lock_drug'][$patientId][$lockDrugId]['is_dispensary'] = 0;
                        if(isset($lockDrugData['is_dispensary']) && $lockDrugData['is_dispensary'] == 1){
                            $lock_mes_arr[] = '（取药）';
                            $arr['lock_drug'][$patientId][$lockDrugId]['is_dispensary'] = 1;
                        }
                        $arr['patient_drug'][$patientId][$lockDrugId] = implode('、',$lock_mes_arr);

                        unset($lock_mes_arr);
                        unset($lockDrugDatas[$lockDrugId]);
                    }
                    unset($patientDrugDatas[$patientId]);
                }
                unset($takeNameArr);
            }
            unset($patientDrugDatas);
        }
        unset($lockDatas);
        return $arr;
    }
}