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

namespace Application\Service\DB\Ocr;

use Interop\Container\ContainerInterface;

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


    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2023/4/25 10:16
     *
     * @param $where
     * @param $where
     * @return array
     */
    public function fetchDataById($where,$columns){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoData){
                $id = $infoData['id'];
                $datas[$id] = $infoData;
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }


    /**
     * Notes: 获取开始日期或结束日期早于入组日期2天的数据
     * User: lltyy
     * DateTime: 2023/11/5 14:21
     * 1、开始日期与入组日期差大于15天，且与化验单日期查大于15天，无论正负
     * 2、结束日期早于开始日期
     * 3、开始日期与结束日期差大于60天
     * 4、超常规日期早于开始日期
     * 5、超常规日期与开始日期差大于30天
     * 6、超常规日期晚于开始日期 且 差大于15天
     * @param array $info
     * @return array[]
     */
    public function fetchFalseDateInfo(){
        $drug_arr = isset($info['drug_arr']) && !empty($info['drug_arr']) ? $info['drug_arr'] : 0;
        $info_type = isset($info['info_type']) && !empty($info['info_type']) ? $info['info_type'] : 0;
        $arr = [
            'lock_no'=>[],
            'lock'=>[],
            'special_no'=>[],
            'special'=>[],
            'drugid'=>[]
        ];

        $specialWhere = 'is_del = 0 and start_date is not null and start_date != "" and special_start_date is not null and special_start_date != "" and (special_start_date < start_date or (special_start_date > start_date and diff_special > 15) or is_confirm_date = 1)';
        $where = 'is_del = 0 and info_type = '.$info_type.' and ((diff_group > 15 and diff_raw > 15) or (medical_date is not null and medical_date != "" and end_date is not null and end_date != "" and medical_date > end_date) or (diff_end > 60) or is_confirm_date = 1)';
        $patient_where = 'is_del = 0 and pid > 0 and have_status <= 1';
        if(!empty($sig_id)){
            $specialWhere .= ' and sig_id = '.$sig_id;
            $where .= ' and sig_id = '.$sig_id;
            $patient_where .= ' and pid = '.$sig_id;
        }
        if(!empty($drug_arr)){
            $where .= ' and drug_id in ('.implode(',',$drug_arr).')';
            $specialWhere .= ' and drug_id in ('.implode(',',$drug_arr).')';
        }
        $patient_arr = $this->LocalService()->ocrSigpatient->fetchCol('id',$patient_where);
        $patient_arr = array_values($patient_arr);
        unset($patient_where);
        if(!empty($patient_arr)){
            $specialWhere .= ' and patient_id in ('.implode(',',$patient_arr).')';
            $where .= ' and patient_id in ('.implode(',',$patient_arr).')';
        }else{
            $where = 'id < 0';
        }
        unset($patient_arr);

        //锁定用药明细
        $cond = [
            'where'=>$where,
            'columns'=>['id','drug_id','is_confirm_date'],
            'order'=>['id']
        ];
        $lockDatas = $this->LocalService()->ocrMedicallock->fetchAll($cond);
        unset($where);
        unset($cond);
        if(!empty($lockDatas)){
            foreach($lockDatas as $lockKey=>$lockData){
                $lock_drug = $lockData['drug_id'];
                $arr['lock_no'][$lock_drug] = isset($arr['lock_no'][$lock_drug]) && !empty($arr['lock_no'][$lock_drug]) ? $arr['lock_no'][$lock_drug] : [];
                if($lockData['is_confirm_date'] != 1){
                    $arr['lock_no'][$lock_drug][] = $lockData['id'];
                }

                $arr['lock'][$lock_drug] = isset($arr['lock'][$lock_drug]) && !empty($arr['lock'][$lock_drug]) ? $arr['lock'][$lock_drug] : [];
                $arr['lock'][$lock_drug][] = $lockData['id'];

                if(!in_array($lock_drug,$arr['drugid'])){
                    $arr['drugid'][] = $lock_drug;
                }

                unset($lockDatas[$lockKey]);
            }
        }
        unset($lockDatas);

        //特殊用药整理
        $sepcialCond = [
            'where'=>$specialWhere,
            'columns'=>['id','drug_id','is_confirm_date'],
            'order'=>['id']
        ];
        $specialDatas = $this->LocalService()->ocrSpecialmedical->fetchAll($sepcialCond);
        unset($specialWhere);
        unset($sepcialCond);
        if(!empty($specialDatas)){
            foreach($specialDatas as $specialKey=>$specialData){
                $special_drug = $specialData['drug_id'];
                $arr['special_no'][$special_drug] = isset($arr['special_no'][$special_drug]) && !empty($arr['special_no'][$special_drug]) ? $arr['special_no'][$special_drug] : [];
                if($specialData['is_confirm_date'] != 1){
                    $arr['special_no'][$special_drug][] = $specialData['id'];
                }

                $arr['special'][$special_drug] = isset($arr['special'][$special_drug]) && !empty($arr['special'][$special_drug]) ? $arr['special'][$special_drug] : [];
                $arr['special'][$special_drug][] = $specialData['id'];

                if(!in_array($special_drug,$arr['drugid'])){
                    $arr['drugid'][] = $special_drug;
                }

                unset($specialDatas[$specialKey]);
            }
        }
        unset($specialDatas);

        return $arr;
    }
}