<?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\Db\Sql\Predicate\Like;

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

    /**
     * Notes:获取附件下未匹配、已匹配、黑名单信息数据
     * User: lltyy
     * DateTime: 2023/5/18 8:31
     *
     * @param $where
     */
    public function fetchAnnexData($where){
        $arr = [
            'mate_id'=>[],//已匹配附件ID
            'mate_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']
        ];
        $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($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(array $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'][] = 'medical_name like "%'.$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' => $delete_arr,
                ],1);
                $this->LocalService()->medicalMatewhite->upDelState([
                    'is_del' => 0,
                    'ocrdata_id' => StringHelper::toArray($delete_arr),
                ], 1);
                $this->LocalService()->medicalMateblack->upDelState([
                    'is_del' => 0,
                    'ocrdata_id' => StringHelper::toArray($delete_arr),
                ],1);
                $this->LocalService()->medicalLock->upDelState([
                    'is_del' => 0,
                    'ocrdata_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),
                ]);
                $this->LocalService()->ocrOcrannex->updateAnnexCount([
                    'id' => StringHelper::toArray($annex_arr),
                ],0,0,$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(array $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'][] = 'medical_name like "%' . $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()->medicalLock->update($update_data, 'is_del = 0 and ocrdata_id = ' . $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()->medicalMatewhite->upDelState([
                    'is_del' => 0,
                    'ocrdata_id' => StringHelper::toArray($update_arr),
                ],1);
                $this->LocalService()->medicalMateblack->upDelState([
                    'is_del' => 0,
                    'ocrdata_id' => StringHelper::toArray($update_arr),
                ],1);

                //获取黑/白名单数据
                $wordDatas = $this->LocalService()->dictionaryMedicalword->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()->medicalMatewhite->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),
                ]);
                $annex_where = [
                    'is_del' => 0,
                    'annex_type' => 2,
                    'id' => StringHelper::toArray($update_annex_arr)
                ];
                $this->LocalService()->ocrOcrannex->updateAnnexCount($annex_where,0,0,$medical_annex_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(array $where,$word_name){
        $count = 0;
        $where['mate_type'] = 2;
        $where[] = 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()->medicalMateblack->upDelState([
                'is_del' => 0,
                'ocrdata_id' => StringHelper::toArray($update_arr),
            ],1);

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

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

        return $count;
    }

}