<?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;
use Laminas\Db\Sql\Predicate\Like;
use Laminas\Stdlib\ArrayUtils;

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

    /**
     * Notes: 更新数据
     * User: lltyy
     * DateTime: 2023/5/24 9:48
     *
     * @param $content //更新内容
     * @param $setWordDatas //设置的白名单数据
     * @param $oldMateDatas //历史已匹配信息
     * @param $oldLockDatas //历史已锁定信息
     * @return int
     */
    public function saveMedicalData($content,&$setWordDatas,&$oldMateDatas){
        $ocrContentId = empty($content['id']) ? 0 : $content['id'];
        $save_data = [
            'id'=>$ocrContentId,
            'annex_id'=>$content['annex_id'],
            'sig_id'=>$content['sig_id'],
            'patient_id'=>$content['patient_id'],
            'medical_date'=>$content['annex_date'],
            'medical_name'=>empty($content['raw_name']) ? '' : trim($content['raw_name']),
            'mate_type'=>1
        ];
        $result = $this->LocalService()->ocrMedical->save($save_data);
        if($result){
            $ocrContentId = empty($ocrContentId) ? $result : $ocrContentId;

            //更新匹配信息
            $mate_data = [];
            if(!empty($content['mate_data'])){
                if(is_array($content['mate_data'])){
                    $mate_data = $content['mate_data'];
                }else{
                    $mate_data = explode(',',$content['mate_data']);
                }

            }
            $mate_data = array_values($mate_data);
            $save_mate = [
                'id'=>isset($oldMateDatas[$ocrContentId]) && !empty($oldMateDatas[$ocrContentId]) ? $oldMateDatas[$ocrContentId] : 0,
                'annex_id'=>$content['annex_id'],
                'sig_id'=>$content['sig_id'],
                'patient_id'=>$content['patient_id'],
                'medical_id'=>$ocrContentId,
                'medical_name'=>$save_data['medical_name'],
                'white_word'=>json_encode($setWordDatas),
                'mate_white'=>json_encode($mate_data)
            ];
            $this->LocalService()->ocrMatewhite->save($save_mate);
            unset($save_mate);

        }
        unset($save_data);
        unset($content);

        return $ocrContentId;
    }

    /**
     * Notes:获取附件下未匹配、已匹配、黑名单信息数据
     * User: lltyy
     * DateTime: 2023/5/18 8:31
     *
     * @param $where
     */
    public function fetchAnnexData($where){
        $arr = [
            'mate_id'=>[],//已匹配附件ID
            'mate_count'=>[],//附件下的已匹配数量
            'act_count'=>[],//附件下的已匹配有效数量
            'no_mate_id'=>[],//未匹配附件ID
            'no_mate_count'=>[],//附件下的未匹配数量
            'black_id'=>[],//黑名单附件ID
            'black_count'=>[],//附件下的黑名单数量
            'annex_date'=>[]//附件的采集时间
        ];
        $cond = [
            'where'=>$where,
            'columns'=>['annex_id','mate_type','is_lock','medical_date','drug_id']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach($datas as $data){
                $mate_type = $data['mate_type'];
                $annex_id = $data['annex_id'];

                //附件时间
                $arr['annex_date'][$annex_id] = isset($arr['annex_date'][$annex_id]) && $arr['annex_date'][$annex_id] != '' ? $arr['annex_date'][$annex_id] : [];
                if($arr['annex_date'][$annex_id] == '' && $data['medical_date'] != '' && !in_array($data['medical_date'],$arr['annex_date'][$annex_id])){
                    $arr['annex_date'][$annex_id][] = $data['medical_date'];
                }

                //未匹配
                if(empty($mate_type)){
                    $arr['no_mate_id'][$annex_id] = $annex_id;
                    $arr['no_mate_count'][$annex_id] = isset($arr['no_mate_count'][$annex_id]) && !empty($arr['no_mate_count'][$annex_id]) ? $arr['no_mate_count'][$annex_id] : 0;
                    $arr['no_mate_count'][$annex_id] += 1;
                }
                //已匹配
                if($mate_type == 1){
                    $arr['mate_id'][$annex_id] = $annex_id;
                    $arr['mate_count'][$annex_id] = isset($arr['mate_count'][$annex_id]) && !empty($arr['mate_count'][$annex_id]) ? $arr['mate_count'][$annex_id] : 0;
                    $arr['mate_count'][$annex_id] += 1;
                    if(!empty($data['drug_id'])){
                        $arr['act_count'][$annex_id] = isset($arr['act_count'][$annex_id]) && !empty($arr['act_count'][$annex_id]) ? $arr['act_count'][$annex_id] : 0;
                        $arr['act_count'][$annex_id] += 1;
                    }
                }
                //黑名单
                if($mate_type == 2){
                    $arr['black_id'][$annex_id] = $annex_id;
                    $arr['black_count'][$annex_id] = isset($arr['black_count'][$annex_id]) && !empty($arr['black_count'][$annex_id]) ? $arr['black_count'][$annex_id] : 0;
                    $arr['black_count'][$annex_id] += 1;
                }
            }
        }

        if(!empty($arr['black_id'])){
            foreach($arr['black_id'] as $black_annex){
                unset($arr['mate_id'][$black_annex]);//去除已匹配数据中黑名单附件
                unset($arr['no_mate_id'][$black_annex]);//去除未匹配数据中黑名单附件
            }
        }

        if(!empty($arr['no_mate_id'])){
            foreach($arr['no_mate_id'] as $no_mate_annex){
                unset($arr['mate_id'][$no_mate_annex]);//去除已匹配数据中未匹配附件
            }
        }
        $arr['no_mate_id'] = array_values($arr['no_mate_id']);
        $arr['mate_id'] = array_values($arr['mate_id']);
        $arr['black_id'] = array_values($arr['black_id']);

        return $arr;
    }

    /**
     * Notes: 删除指定识别名称数据
     * User: lltyy
     * DateTime: 2023/5/25 16:07
     *
     * @param $where//数据条件
     * @param array $delete_name_arr//要删除的识别内容数组
     * @param int $update_annex//是否更新附件信息【0 否 1 是】
     * @return int
     */
    public function deleteDataHaveName($where,array $delete_name_arr=[],$update_annex = 0){
        $count = 0;
        if(!empty($delete_name_arr)){
            $count = 1;
            //查询要删除的数据ID以及对应的附件ID
            $annex_arr = $delete_arr = [];
            $where_arr = [];
            foreach($delete_name_arr as $delete_name){
                $where_arr['or'][] = new Like('medical_name', "%{$delete_name}%");
            }
            $where[] = $where_arr;
            $cond = [
                'where'=>$where,
                'columns'=>['id','annex_id']
            ];
            $datas = $this->fetchAll($cond);
            if(!empty($datas)){
                foreach($datas as $data){
                    $data_annex = $data['annex_id'];
                    $annex_arr[$data_annex] = $data_annex;
                    $delete_arr[] = $data['id'];
                }
            }
            unset($where_arr);
            unset($where);
            unset($cond);
            unset($datas);

            //删除符合要求的识别内容
            if(!empty($delete_arr)){
                $this->upDelState([
                    'is_del' => 0,
                    'id' => StringHelper::toArray($delete_arr),
                ],1);
                $this->LocalService()->ocrMatewhite->upDelState([
                    'is_del' => 0,
                    'medical_id' => StringHelper::toArray($delete_arr),
                ],1);
                $this->LocalService()->ocrMateblack->upDelState([
                    'is_del' => 0,
                    'medical_id' => StringHelper::toArray($delete_arr),
                ],1);
                $this->LocalService()->ocrMedicallock->upDelState([
                    'is_del' => 0,
                    'medical_id' => StringHelper::toArray($delete_arr),
                ],1);
            }
            unset($delete_arr);

            //更新附件信息
            if($update_annex == 1 && !empty($annex_arr)){
                $annex_arr = array_values($annex_arr);
                $annex_count_arr = $this->fetchAnnexData([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($annex_arr),
                ]);
                $raw_annex_count_arr = $this->LocalService()->ocrRawdata->fetchAnnexData('id < 0');
                $ct_annex_count_arr = $this->LocalService()->ocrCt->fetchAnnexData('id < 0');
                $this->LocalService()->ocrOcrannex->updateAnnexCount([
                    'id' => StringHelper::toArray($annex_arr),
                ],0,0,$raw_annex_count_arr,$annex_count_arr,$ct_annex_count_arr);
            }
            unset($annex_arr);
        }

        return $count;
    }

    /**
     * Notes: 批量替换识别信息
     * User: lltyy
     * DateTime: 2023/5/25 16:07
     *
     * @param $where//数据条件
     * @param array $replace_datas//要替换的数组
     * @param int $update_ocr//是否重新匹配【0 否 1 是】
     * @return int
     */
    public function replaceDataHaveInfo($where,array $replace_datas=[],$update_ocr = 0)
    {
        $count = 0;
        if (!empty($replace_datas)) {
            $count = 1;

            //查询要替换的数据ID
            $where_arr = [];
            foreach ($replace_datas as $replace) {
                $where_arr['or'][] = new Like('medical_name', "%{$replace['replace_pre']}%");
            }
            unset($replace_arr);

            $where[] = $where_arr;
            $cond = [
                'where' => $where,
                'columns' => ['id', 'annex_id', 'medical_name']
            ];
            $update_arr = $update_annex_arr = [];
            $datas = $this->fetchAll($cond);
            if (!empty($datas)) {
                foreach ($datas as $data) {
                    $medical_id = $data['id'];
                    $medical_name = $data['medical_name'];
                    foreach ($replace_datas as $replace_info) {
                        $medical_name = str_replace($replace_info['replace_pre'], $replace_info['replace_after'], $medical_name);
                    }

                    $update_data = [];
                    if ($medical_name != $data['medical_name']) {
                        $update_data['medical_name'] = $medical_name;
                    }
                    if (!empty($update_data)) {
                        if ($update_ocr == 0) {//更新明细信息
                            $this->LocalService()->ocrMedicallock->update($update_data, [
                                'is_del' => 0,
                                'medical_id' => StringHelper::toArray($medical_id),
                            ]);
                        }

                        if ($update_ocr == 1) {//重新匹配
                            $update_arr[] = $medical_id;
                            $update_annex_arr[] = $data['annex_id'];
                            $update_data['mate_type'] = 0;
                        }
                        $this->update($update_data, 'id = ' . $medical_id);
                    }

                    unset($update_data);
                }
            }
            unset($where_arr);
            unset($cond);
            unset($datas);
            unset($replace_datas);

            //重新匹配
            if($update_ocr == 1 && !empty($update_arr)){
                //删除已匹配数据
                $this->LocalService()->ocrMatewhite->upDelState([
                    'is_del' => 0,
                    'medical_id' => StringHelper::toArray($update_arr),
                ],1);
                $this->LocalService()->ocrMateblack->upDelState([
                    'is_del' => 0,
                    'medical_id' => StringHelper::toArray($update_arr),
                ],1);

                //获取黑/白名单数据
                $wordDatas = $this->LocalService()->ocrMedicalword->wordNameData('is_del = 0');
                $white_word_arr = isset($wordDatas['white_word']) && !empty($wordDatas['white_word']) ? $wordDatas['white_word'] : [];
                $black_word_arr = isset($wordDatas['black_word']) && !empty($wordDatas['black_word']) ? $wordDatas['black_word'] : [];
                unset($wordDatas);

                //重新跑匹配
                $this->LocalService()->ocrMatewhite->mateWordData([
                    'id' => StringHelper::toArray($update_arr),
                ],0,0,$white_word_arr,$black_word_arr);

                //更新附件信息
                $medical_annex_arr = $this->fetchAnnexData([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($update_annex_arr),
                ]);
                $raw_annex_count_arr = $this->LocalService()->ocrRawdata->fetchAnnexData('id < 0');
                $ct_annex_count_arr = $this->LocalService()->ocrCt->fetchAnnexData('id < 0');
                $annex_where = [
                    'is_del' => 0,
                    'annex_type' => 2,
                    'id' => StringHelper::toArray($update_annex_arr),
                ];
                $this->LocalService()->ocrOcrannex->updateAnnexCount($annex_where,0,0,$raw_annex_count_arr,$medical_annex_arr,$ct_annex_count_arr);
            }
            unset($update_arr);
            unset($update_annex_arr);
        }
        return $count;
    }

    /**
     * Notes:匹配上黑名单的数据与黑名单下白名单名称重新匹配
     * User: lltyy
     * DateTime: 2023/5/26 14:42
     *
     * @param $where
     * @param $word_name
     */
    public function mateWordData($where,$word_name){
        $count = 0;
        $where = ArrayUtils::merge($where, [
            'mate_type' => 2,
            new Like('medical_name', "%{$word_name}%")
        ]);
        $cond = [
            'where' => $where,
            'columns' => ['id', 'annex_id']
        ];
        $update_arr = $update_annex_arr = [];
        $datas = $this->fetchAll($cond);
        if (!empty($datas)) {
            $count = 1;
            foreach ($datas as $data) {
                $update_arr[] = $data['id'];
                $update_annex_arr[] = $data['annex_id'];
            }
            $update_annex_arr = array_unique($update_annex_arr);
            $update_annex_arr = array_values($update_annex_arr);
        }
        unset($where_arr);
        unset($cond);
        unset($datas);

        //重新更新数据
        if(!empty($update_arr)){
            //删除已匹配数据
            $this->update(['mate_type'=>1],[
                'id' => StringHelper::toArray($update_arr),
            ]);
            $this->LocalService()->ocrMateblack->upDelState([
                'is_del' => 0,
                'medical_id' => StringHelper::toArray($update_arr),
            ],1);

            //更新附件信息
            $medical_annex_arr = $this->fetchAnnexData([
                'is_del' => 0,
                'annex_id' => StringHelper::toArray($update_annex_arr),
            ]);
            $raw_annex_count_arr = $this->LocalService()->ocrRawdata->fetchAnnexData('id < 0');
            $ct_annex_count_arr = $this->LocalService()->ocrCt->fetchAnnexData('id < 0');
            $annex_where = [
                'is_del' => 0,
                'annex_type' => 2,
                'id' => StringHelper::toArray($update_annex_arr),
            ];
            $this->LocalService()->ocrOcrannex->updateAnnexCount($annex_where,0,0,$raw_annex_count_arr,$medical_annex_arr,$ct_annex_count_arr);

        }
        unset($update_arr);
        unset($update_annex_arr);

        return $count;
    }


    /**
     * Notes:匹配上黑名单的数据与黑名单下白名单名称重新匹配
     * User: lltyy
     * DateTime: 2023/5/26 14:42
     *
     * @param $where
     * @param $word_name
     */
    public function updateOtherMedical(){
        $where = 'is_del = 0 and is_lock = 2 and mate_type = 1';

        //匹配为【苯磺酸氨氯地平片】 且医嘱中含有【依】字样，更新匹配药物为【依那普利】
        $medical_id0 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%依%' and drug_id = 60");
        $medical_id0 = array_values($medical_id0);
        if(!empty($medical_id0)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>62,'drug_name'=>'依那普利','mate_keyword'=>'["依那普利"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id0).')');
            $this->update(['drug_id'=>62],'is_del = 0 and id in ('.implode(',',$medical_id0).')');
        }
        echo "countPAGE {".count($medical_id0)."}：" . PHP_EOL;
        unset($medical_id0);

        //匹配为【依洛尤单抗】 且医嘱中含有【阿利】字样，更新匹配药物为【阿利西尤单抗】
        $medical_id1 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%阿利%' and drug_id = 23");
        $medical_id1 = array_values($medical_id1);
        if(!empty($medical_id1)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>24,'drug_name'=>'阿利西尤单抗','mate_keyword'=>'["阿利西尤单抗"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id1).')');
            $this->update(['drug_id'=>24],'is_del = 0 and id in ('.implode(',',$medical_id1).')');
        }
        echo "countPAGE {".count($medical_id1)."}：" . PHP_EOL;
        unset($medical_id1);

        //未匹配为【赖诺普利】 且医嘱中含有【诺普利】字样，更新匹配药物为【赖诺普利】
        $medical_id2 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%诺普利%' and drug_id != 64 and drug_id > 0");
        $medical_id2 = array_values($medical_id2);
        if(!empty($medical_id2)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>64,'drug_name'=>'赖诺普利','mate_keyword'=>'["赖诺普利"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id2).')');
            $this->update(['drug_id'=>64],'is_del = 0 and id in ('.implode(',',$medical_id2).')');
        }
        echo "countPAGE {".count($medical_id2)."}：" . PHP_EOL;
        unset($medical_id2);

        //匹配为【苯磺酸氨氯地平片】 且医嘱中含有【缬沙坦】字样，更新匹配药物为【缬沙坦氨氯地平】
        $medical_id3 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%缬沙坦%' and drug_id = 2914 and drug_id > 0");
        $medical_id3 = array_values($medical_id3);
        if(!empty($medical_id3)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>90,'drug_name'=>'缬沙坦氨氯地平','mate_keyword'=>'["缬沙坦氨氯地平"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id3).')');
            $this->update(['drug_id'=>90],'is_del = 0 and id in ('.implode(',',$medical_id3).')');
        }
        echo "countPAGE {".count($medical_id3)."}：" . PHP_EOL;
        unset($medical_id3);

        //匹配为【苯磺酸氨氯地平片】 且医嘱中含有【普利】字样，更新不匹配
        $medical_id4 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%普利%' and drug_id = 2914 and drug_id > 0");
        $medical_id4 = array_values($medical_id4);
        if(!empty($medical_id4)){
            $this->LocalService()->ocrMatedrug->update(['is_del'=>1],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id4).')');
            $this->update(['drug_id'=>0],'is_del = 0 and id in ('.implode(',',$medical_id4).')');
        }
        echo "countPAGE {".count($medical_id4)."}：" . PHP_EOL;
        unset($medical_id4);

        //匹配为【苯磺酸左氨氯地平片】 且医嘱中不含有【左】字样，更新匹配药物为【苯磺酸氨氯地平片】
        $medical_id5 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%左%' and drug_id = 3047 and drug_id > 0");
        $medical_id5 = array_values($medical_id5);
        if(!empty($medical_id5)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>2914,'drug_name'=>'苯磺酸氨氯地平片','mate_keyword'=>'["苯磺酸氨氯地平片"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id5).')');
            $this->update(['drug_id'=>2914],'is_del = 0 and id in ('.implode(',',$medical_id5).')');
        }
        echo "countPAGE {".count($medical_id5)."}：" . PHP_EOL;
        unset($medical_id5);

        //未匹配为【苯磺酸左氨氯地平片】 且医嘱中含有【苯磺酸左】字样，更新匹配药物为【苯磺酸左氨氯地平片】
        $medical_id6 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%苯磺酸左%' and drug_id != 3047 and drug_id > 0");
        $medical_id6 = array_values($medical_id6);
        if(!empty($medical_id6)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>3047,'drug_name'=>'苯磺酸左氨氯地平片','mate_keyword'=>'["苯磺酸左氨氯地平片"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id6).')');
            $this->update(['drug_id'=>3047],'is_del = 0 and id in ('.implode(',',$medical_id6).')');
        }
        echo "countPAGE {".count($medical_id6)."}：" . PHP_EOL;
        unset($medical_id6);

        //未匹配为【苯磺酸氨氯地平片】 且医嘱中含有【苯磺酸】且不含【左】字样，更新匹配药物为【苯磺酸氨氯地平片】
        $medical_id7 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%苯磺酸%' and ocr_name not like '%左%' and drug_id != 2914 and drug_id > 0");
        $medical_id7 = array_values($medical_id7);
        if(!empty($medical_id7)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>2914,'drug_name'=>'苯磺酸氨氯地平片','mate_keyword'=>'["苯磺酸氨氯地平片"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id7).')');
            $this->update(['drug_id'=>2914],'is_del = 0 and id in ('.implode(',',$medical_id7).')');
        }
        echo "countPAGE {".count($medical_id7)."}：" . PHP_EOL;
        unset($medical_id7);

        //未匹配为【辛伐他汀】 且医嘱中含有【辛伐】字样，更新匹配药物为【辛伐他汀】
        $medical_id8 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%辛伐%' and drug_id != 14 and drug_id > 0");
        $medical_id8 = array_values($medical_id8);
        if(!empty($medical_id8)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>14,'drug_name'=>'辛伐他汀','mate_keyword'=>'["辛伐他汀"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id8).')');
            $this->update(['drug_id'=>14],'is_del = 0 and id in ('.implode(',',$medical_id8).')');
        }
        echo "countPAGE {".count($medical_id8)."}：" . PHP_EOL;
        unset($medical_id8);

        //未匹配为【匹伐他汀】 且医嘱中含有【匹伐】字样，更新匹配药物为【匹伐他汀】
        $medical_id9 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%匹伐%' and drug_id != 15 and drug_id > 0");
        $medical_id9 = array_values($medical_id9);
        if(!empty($medical_id9)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>15,'drug_name'=>'匹伐他汀','mate_keyword'=>'["匹伐他汀"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id9).')');
            $this->update(['drug_id'=>15],'is_del = 0 and id in ('.implode(',',$medical_id9).')');
        }
        echo "countPAGE {".count($medical_id9)."}：" . PHP_EOL;
        unset($medical_id9);

        //未匹配为【洛伐他汀】 且医嘱中含有【洛伐】字样，更新匹配药物为【洛伐他汀】
        $medical_id10 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%洛伐%' and drug_id != 16 and drug_id > 0");
        $medical_id10 = array_values($medical_id10);
        if(!empty($medical_id10)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>16,'drug_name'=>'洛伐他汀','mate_keyword'=>'["洛伐他汀"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id10).')');
            $this->update(['drug_id'=>16],'is_del = 0 and id in ('.implode(',',$medical_id10).')');
        }
        echo "countPAGE {".count($medical_id10)."}：" . PHP_EOL;
        unset($medical_id10);

        //未匹配为【普伐他汀】 且医嘱中含有【普伐】字样，更新匹配药物为【普伐他汀】
        $medical_id11 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%普伐%' and drug_id != 17 and drug_id > 0");
        $medical_id11 = array_values($medical_id11);
        if(!empty($medical_id11)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>17,'drug_name'=>'普伐他汀','mate_keyword'=>'["普伐他汀"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id11).')');
            $this->update(['drug_id'=>17],'is_del = 0 and id in ('.implode(',',$medical_id11).')');
        }
        echo "countPAGE {".count($medical_id11)."}：" . PHP_EOL;
        unset($medical_id11);

        //未匹配为【氟伐他汀】 且医嘱中含有【氟伐】字样，更新匹配药物为【氟伐他汀】
        $medical_id12 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%氟伐%' and drug_id != 18 and drug_id > 0");
        $medical_id12 = array_values($medical_id12);
        if(!empty($medical_id12)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>18,'drug_name'=>'氟伐他汀','mate_keyword'=>'["氟伐他汀"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id12).')');
            $this->update(['drug_id'=>18],'is_del = 0 and id in ('.implode(',',$medical_id12).')');
        }
        echo "countPAGE {".count($medical_id12)."}：" . PHP_EOL;
        unset($medical_id12);

        //未匹配为【普通肝素】 且医嘱中含有【肝素】字样，更新匹配药物为【普通肝素】
        $medical_id13 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%肝素%' and drug_id != 8 and drug_id > 0");
        $medical_id13 = array_values($medical_id13);
        if(!empty($medical_id13)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>8,'drug_name'=>'普通肝素','mate_keyword'=>'["普通肝素"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id13).')');
            $this->update(['drug_id'=>8],'is_del = 0 and id in ('.implode(',',$medical_id13).')');
        }
        echo "countPAGE {".count($medical_id13)."}：" . PHP_EOL;
        unset($medical_id13);

        //未匹配为【瑞舒伐他汀】 且医嘱中含有【瑞舒】字样，更新匹配药物为【瑞舒伐他汀】
        $medical_id14 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%瑞舒%' and drug_id != 13 and drug_id > 0");
        $medical_id14 = array_values($medical_id14);
        if(!empty($medical_id14)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>13,'drug_name'=>'瑞舒伐他汀','mate_keyword'=>'["瑞舒伐他汀"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id14).')');
            $this->update(['drug_id'=>13],'is_del = 0 and id in ('.implode(',',$medical_id14).')');
        }
        echo "countPAGE {".count($medical_id14)."}：" . PHP_EOL;
        unset($medical_id14);

        //未匹配为【沙坦类+氢氯噻嗪】 且医嘱中含有【沙坦 和 氢氯】字样，更新匹配药物为【沙坦类+氢氯噻嗪】
        $medical_id15 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%沙坦%' and ocr_name like '%氢氯%' and drug_id != 89 and drug_id > 0");
        $medical_id15 = array_values($medical_id15);
        if(!empty($medical_id15)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>89,'drug_name'=>'沙坦类+氢氯噻嗪','mate_keyword'=>'["沙坦类+氢氯噻嗪"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id15).')');
            $this->update(['drug_id'=>89],'is_del = 0 and id in ('.implode(',',$medical_id15).')');
        }
        echo "countPAGE {".count($medical_id15)."}：" . PHP_EOL;
        unset($medical_id15);

        //医嘱中含有【盐酸尼卡】字样，去除匹配药物
        $medical_id16 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%盐酸尼卡%' and drug_id > 0");
        $medical_id16 = array_values($medical_id16);
        if(!empty($medical_id16)){
            $this->LocalService()->ocrMatedrug->update(['is_del'=>1],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id16).')');
            $this->update(['drug_id'=>0],'is_del = 0 and id in ('.implode(',',$medical_id16).')');
        }
        echo "countPAGE {".count($medical_id16)."}：" . PHP_EOL;
        unset($medical_id16);

        //匹配为【氯吡格雷】 且 医嘱中含有【硫 或 酸】字样，更新匹配药物为【硫酸氢氯吡格雷片】
        $medical_id17 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%硫酸%' and drug_id = 3109 and drug_id > 0");
        $medical_id17 = array_values($medical_id17);
        if(!empty($medical_id17)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>2911,'drug_name'=>'硫酸氢氯吡格雷片','mate_keyword'=>'["硫酸氢氯吡格雷片"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id17).')');
            $this->update(['drug_id'=>2911],'is_del = 0 and id in ('.implode(',',$medical_id17).')');
        }
        echo "countPAGE {".count($medical_id17)."}：" . PHP_EOL;
        unset($medical_id17);

        //匹配为【丁苯酞软胶囊】 且医嘱中含有【注射】字样，更新匹配药物为【丁苯酞注射液】
        $medical_id18 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%注射%' and drug_id = 94 and drug_id > 0");
        $medical_id18 = array_values($medical_id18);
        if(!empty($medical_id18)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>3113,'drug_name'=>'丁苯酞注射液','mate_keyword'=>'["丁苯酞注射液"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id18).')');
            $this->update(['drug_id'=>3113],'is_del = 0 and id in ('.implode(',',$medical_id18).')');
        }
        echo "countPAGE {".count($medical_id18)."}：" . PHP_EOL;
        unset($medical_id18);

        //匹配为【丁苯酞注射液】 且医嘱中含有【胶囊】字样，更新匹配药物为【丁苯酞软胶囊】
        $medical_id19 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%胶囊%' and ocr_name not like '%注射%' and drug_id = 3113 and drug_id > 0");
        $medical_id19 = array_values($medical_id19);
        if(!empty($medical_id19)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>94,'drug_name'=>'丁苯酞软胶囊','mate_keyword'=>'["丁苯酞软胶囊"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id19).')');
            $this->update(['drug_id'=>94],'is_del = 0 and id in ('.implode(',',$medical_id19).')');
        }
        echo "countPAGE {".count($medical_id19)."}：" . PHP_EOL;
        unset($medical_id19);

        //匹配为【丁苯酞注射液】 且医嘱中含有【丁苯肽针】字样，更新匹配药物为【丁苯酞注射液】
        $medical_id20 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and (ocr_name like '%丁苯肽针%' or ocr_name like '%丁苯酞针%') and drug_id != 3113 and drug_id > 0");
        $medical_id20 = array_values($medical_id20);
        if(!empty($medical_id20)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>3113,'drug_name'=>'丁苯酞注射液','mate_keyword'=>'["丁苯酞注射液"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id20).')');
            $this->update(['drug_id'=>3113],'is_del = 0 and id in ('.implode(',',$medical_id20).')');
        }
        echo "countPAGE {".count($medical_id20)."}：" . PHP_EOL;
        unset($medical_id20);

        //匹配为【二甲双胍】 且医嘱中含有【恩格】字样，更新匹配药物为【二甲双胍恩格列净】
        $medical_id21 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%恩格%' and drug_id = 26 and drug_id > 0");
        $medical_id21 = array_values($medical_id21);
        if(!empty($medical_id21)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>3116,'drug_name'=>'二甲双胍恩格列净','mate_keyword'=>'["二甲双胍恩格列净"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id21).')');
            $this->update(['drug_id'=>3116],'is_del = 0 and id in ('.implode(',',$medical_id21).')');
        }
        echo "countPAGE {".count($medical_id21)."}：" . PHP_EOL;
        unset($medical_id21);

        //匹配为【二甲双胍】 且医嘱中含有【吡格、列酮】字样，更新匹配药物为【二甲双胍吡格列酮】
        $medical_id22 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%吡格%' and ocr_name like '%列酮%' and drug_id = 26 and drug_id > 0");
        $medical_id22 = array_values($medical_id22);
        if(!empty($medical_id22)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>3118,'drug_name'=>'二甲双胍吡格列酮','mate_keyword'=>'["二甲双胍吡格列酮"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id22).')');
            $this->update(['drug_id'=>3118],'is_del = 0 and id in ('.implode(',',$medical_id22).')');
        }
        echo "countPAGE {".count($medical_id22)."}：" . PHP_EOL;
        unset($medical_id22);

        //匹配为【二甲双胍】 且医嘱中含有【西格】字样，更新匹配药物为【西格列汀二甲双胍】
        $medical_id23 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%西格%' and drug_id = 26 and drug_id > 0");
        $medical_id23 = array_values($medical_id23);
        if(!empty($medical_id23)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>3117,'drug_name'=>'西格列汀二甲双胍','mate_keyword'=>'["西格列汀二甲双胍"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id23).')');
            $this->update(['drug_id'=>3117],'is_del = 0 and id in ('.implode(',',$medical_id23).')');
        }
        echo "countPAGE {".count($medical_id23)."}：" . PHP_EOL;
        unset($medical_id23);

        //匹配为【二甲双胍】 且医嘱中含有【脲】字样，更新匹配药物为【二甲双胍格列本脲胶囊】
        $medical_id24 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%脲%' and drug_id = 26 and drug_id > 0");
        $medical_id24 = array_values($medical_id24);
        if(!empty($medical_id24)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>2982,'drug_name'=>'二甲双胍格列本脲胶囊','mate_keyword'=>'["二甲双胍格列本脲胶囊"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id24).')');
            $this->update(['drug_id'=>2982],'is_del = 0 and id in ('.implode(',',$medical_id24).')');
        }
        echo "countPAGE {".count($medical_id24)."}：" . PHP_EOL;
        unset($medical_id24);

        //匹配为【二甲双胍】 且医嘱中含有【吡嗪】字样，更新匹配药物为【二甲双胍格列吡嗪片】
        $medical_id25 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%吡嗪%' and drug_id = 26 and drug_id > 0");
        $medical_id25 = array_values($medical_id25);
        if(!empty($medical_id25)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>3099,'drug_name'=>'二甲双胍格列吡嗪片','mate_keyword'=>'["二甲双胍格列吡嗪片"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id25).')');
            $this->update(['drug_id'=>3099],'is_del = 0 and id in ('.implode(',',$medical_id25).')');
        }
        echo "countPAGE {".count($medical_id25)."}：" . PHP_EOL;
        unset($medical_id25);

        //匹配为【二甲双胍】 且医嘱中含有【维格】字样，更新匹配药物为【二甲双胍维格列汀片】
        $medical_id26 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%维格%' and drug_id = 26 and drug_id > 0");
        $medical_id26 = array_values($medical_id26);
        if(!empty($medical_id26)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>3011,'drug_name'=>'二甲双胍维格列汀片','mate_keyword'=>'["二甲双胍维格列汀片"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id26).')');
            $this->update(['drug_id'=>3011],'is_del = 0 and id in ('.implode(',',$medical_id26).')');
        }
        echo "countPAGE {".count($medical_id26)."}：" . PHP_EOL;
        unset($medical_id26);

        //匹配为【氨氯地平】 且医嘱中含有【贝那】字样，更新匹配药物为【氨氯地平贝那普利】
        $medical_id27 = $this->LocalService()->ocrMedical->fetchCol('id',$where." and ocr_name like '%贝那%' and drug_id = 55 and drug_id > 0");
        $medical_id27 = array_values($medical_id27);
        if(!empty($medical_id27)){
            $this->LocalService()->ocrMatedrug->update(['drug_id'=>91,'drug_name'=>'氨氯地平贝那普利','mate_keyword'=>'["氨氯地平贝那普利"]'],'is_del = 0 and is_mate = 1 and medical_id in ('.implode(',',$medical_id27).')');
            $this->update(['drug_id'=>91],'is_del = 0 and id in ('.implode(',',$medical_id27).')');
        }
        echo "countPAGE {".count($medical_id27)."}：" . PHP_EOL;
        unset($medical_id27);


        //更新 服用频率
        $update_times_where = $where.' and times_id = 0';
        $this->update(['times_id'=>76],$update_times_where." and ocr_name like '%隔日一次%'");
        $this->update(['times_id'=>73],$update_times_where." and (ocr_name like '%两次%' or ocr_name like '%每12小时一次%' or ocr_name like '%每十二小时一次%')");
        $this->update(['times_id'=>74],$update_times_where." and (ocr_name like '%三次%' or ocr_name like '%每8小时一次%' or ocr_name like '%每八小时一次%')");
        $this->update(['times_id'=>117],$update_times_where." and (ocr_name like '%六次%' or ocr_name like '%每4小时一次%' or ocr_name like '%每四小时一次%')");
        $this->update(['times_id'=>72],$update_times_where." and (ocr_name like '%一次%' or ocr_name like '%每晚一次%' or ocr_name like '%立即%' or ocr_name like '%睡前%')");
        $this->update(['times_id'=>75],$update_times_where." and ocr_name like '%四次%'");
        $this->update(['times_id'=>81],$update_times_where." and ocr_name like '%必要时%'");
        $this->update(['times_id'=>120],$update_times_where." and ocr_name like '%持续使用%'");
        $this->update(['times_id'=>72],$update_times_where." and (ocr_name like '%QD%' or ocr_name like '%QN%' or ocr_name like '%ST%' or ocr_name like '%HS%')");
        $this->update(['times_id'=>73],$update_times_where." and (ocr_name like '%BID%' or ocr_name like '%Q12H%')");
        $this->update(['times_id'=>74],$update_times_where." and (ocr_name like '%TID%' or ocr_name like '%Q8h%')");
        $this->update(['times_id'=>75],$update_times_where." and ocr_name like '%QID%'");
        $this->update(['times_id'=>76],$update_times_where." and ocr_name like '%QOD%'");
        $this->update(['times_id'=>81],$update_times_where." and ocr_name like '%PRN%'");
        $this->update(['times_id'=>117],$update_times_where." and ocr_name like '%Q4h%'");
        $this->update(['times_id'=>120],$update_times_where." and ocr_name like '%CX%'");
        echo "timesPAGE 1：" . PHP_EOL;
        unset($update_times_where);

        //更新出院取药标识
        $this->update(['is_dispensary'=>1],$where." and drug_id and is_dispensary = 0 and (ocr_name like '%[出院]%' or ocr_name like '%[出]%')");
        echo "dispensaryPAGE 1：" . PHP_EOL;
        unset($where);
        return 1;
    }
}