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

namespace Application\Service\DB\Ocr;

use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;

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

    /**
     * Notes: 更新患者特殊药物状态
     * User: llbjj
     * DateTime: 2023/6/9 13:54
     *
     * @param array $params
     */
    public function updateSepcialStatusData(array $params) {
        $patient_id = isset($params['patient_id']) && !empty($params['patient_id']) ? $params['patient_id'] : 0;
        if(!empty($patient_id)){
            $update_data = [
                'special_status'=>0
            ];
            $drug_arr = $this->LocalService()->ocrDrug->fetchCol('id','is_del = 0 and drug_type > 0');
            $drug_arr = array_values($drug_arr);
            if(!empty($drug_arr)){
                $lock_drug_arr = $this->LocalService()->ocrMedicallock->fetchCol('drug_id',[
                    'is_del' => 0,
                    'drug_id' => StringHelper::toArray($drug_arr),
                    'patient_id' => $patient_id,
                ]);
                $lock_drug_arr = !empty($lock_drug_arr) ? array_unique($lock_drug_arr) : [];
                $lock_drug_arr = !empty($lock_drug_arr) ? array_values($lock_drug_arr) : [];
                $special_drug_arr = $this->LocalService()->ocrSpecialmedical->fetchCol('drug_id',[
                    'is_del' => 0,
                    'drug_id' => StringHelper::toArray($drug_arr),
                    'patient_id' => $patient_id,
                ]);
                $special_drug_arr = !empty($special_drug_arr) ? array_unique($special_drug_arr) : [];
                $special_drug_arr = !empty($special_drug_arr) ? array_values($special_drug_arr) : [];
                foreach($drug_arr as $drug){
                    $drug_special_status = 0;
                    if(in_array($drug,$lock_drug_arr)){
                        $drug_special_status = 1;
                        $update_data['special_status'] = 1;
                        if(in_array($drug,$special_drug_arr)){
                            $drug_special_status = 2;
                        }
                    }
                    $update_data['special_status_'.$drug] = $drug_special_status;
                }
                unset($lock_drug_arr);
                unset($special_drug_arr);
            }
            unset($drug_arr);
            $this->update($update_data,'id = '.$patient_id);
        }
    }


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

    /**
     * Notes: 获取入组时间的年-月-日
     * User: lltyy
     * DateTime: 2023/8/2 16:03
     *
     * @param string $group_date
     * @return string
     */
    public function getTrueGroupDate($group_date='',$return_type = 0){
        $group_date = is_null($group_date) || $group_date == '' ? '' : $group_date;
        $date = '';
        if($group_date != ''){
            $group_dates = !is_null($group_date) && $group_date != '' ? explode(' ',$group_date) : [];
            $group_date_arr = isset($group_dates[0]) ? explode('/',$group_dates[0]) : [];
            if(!empty($group_date_arr)){
                $date = $group_date_arr[0];
                if(isset($group_date_arr[1]) && $group_date_arr[1] != ''){
                    if(strlen($group_date_arr[1]) < 2){
                        if($return_type == 1){
                            $date .= '0'.$group_date_arr[1];
                        }else{
                            $date .= '-0'.$group_date_arr[1];
                        }
                    }else{
                        if($return_type == 1){
                            $date .= ''.$group_date_arr[1];
                        }else{
                            $date .= '-'.$group_date_arr[1];
                        }
                    }
                }
                if(isset($group_date_arr[2]) && $group_date_arr[2] != '') {
                    if (strlen($group_date_arr[2]) < 2) {
                        if($return_type == 1){
                            $date .= '0'.$group_date_arr[2];
                        }else{
                            $date .= '-0'.$group_date_arr[2];
                        }
                    } else {
                        if($return_type == 1){
                            $date .= ''.$group_date_arr[2];
                        }else{
                            $date .= '-'.$group_date_arr[2];
                        }
                    }
                }
            }
        }
        return $date;
    }

}