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

namespace Application\Service\DB\Tmp;

use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\IsNotNull;
use Laminas\Db\Sql\Predicate\Operator;

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

    /**
     * Notes: 已匹配数据和黑名单匹配
     * User: lltyy
     * DateTime: 2023/5/24 15:43
     *
     * @param $where
     * @param int $limit
     * @param $offset
     * @param $have_mate_raw
     * @param $black_word_arr
     * @return int
     */
    public function mateBlackdata(){

        //获取存在已匹配数据的内容ID
        $have_mate_raw = $this->LocalService()->tmpRawmate->fetchCol('raw_id',['is_del'=>0]);
        $have_mate_raw = array_values($have_mate_raw);
        $have_mate_raw = array_unique($have_mate_raw);
        $have_mate_raw = array_values($have_mate_raw);

        //获取指定条件下的数据
        $where = [
            'is_lock'=>0,
            'is_del'=>0,
            new Operator('raw_name',Operator::OP_NE,""),
            new IsNotNull('raw_name')
        ];
        if(!empty($have_mate_raw)){
            $where['id'] = $have_mate_raw;
        }else{
            $where[] = new Operator('id',Operator::OP_LT,0);
        }
        $rawCond = [
            'where'=>$where,
            'columns'=>['id','annex_id','itemsig_id','patient_id','raw_name'],
            'order'=>['id']
        ];
        $rawDatas = $this->LocalService()->tmpRawdata->fetchAll($rawCond);
        $count = 0;
        $annex_arr = [];
        if(!empty($rawDatas)){
            //获取指标黑名单信息
            $black_word_arr = [
                'name'=>[],
                'code'=>[]
            ];
            $blackCond = [
                'where'=>['is_del'=>0],
                'columns'=>['id','category_name','word_code','word_name'],
                'order'=>['category_name','id']
            ];
            $blackDatas = $this->LocalService()->tmpRawword->fetchAll($blackCond);
            if(!empty($blackDatas)){
                foreach($blackDatas as $blackData){
                    $black_word_arr['name'][] = $blackData['word_name'];
                    if($blackData['word_code'] != ''){
                        $black_word_arr['code'][] = $blackData['word_code'];
                    }
                }
            }

            $count = 1;
            $black_name_arr = !empty($black_word_arr['name']) ? $black_word_arr['name'] : [];
            $black_code_arr = !empty($black_word_arr['code']) ? $black_word_arr['code'] : [];
            foreach($rawDatas as $rawData){
                $raw_id = $rawData['id'];
                $raw_name = trim($rawData['raw_name']);
                $annex_id = $rawData['annex_id'];
                $annex_arr[$annex_id] = $annex_id;
                //删除原黑名单匹配数据
                $this->upDelState(['is_del'=>0,'raw_id'=>$raw_id],1);
                if((!empty($black_name_arr) || !empty($black_code_arr)) && !empty($raw_name) && !in_array($raw_name,['备注','备注：','备注:'])){
                    //切割识别名称
                    $rawName = "{$raw_name}";
                    $raw_keyword =  !empty($rawName) ? Com::cutIntoSlices($rawName) : [];
                    $raw_keyword = !empty($raw_keyword) ? array_values($raw_keyword) : [];

                    //已匹配上黑名单的名称
                    $black_word = !empty($black_name_arr)  ? array_uintersect($black_name_arr,$raw_keyword,"strcasecmp") : [];
                    $black_word = isset($black_word) && !empty($black_word) ? array_values($black_word) : [];

                    //已匹配上黑名单的项目代号
                    $black_code = !empty($black_code_arr)  ? array_uintersect($black_name_arr,$black_code_arr,"strcasecmp") : [];
                    $black_code = isset($black_code) && !empty($black_code) ? array_values($black_code) : [];

                    $black_word = !empty($black_code) ? array_merge($black_word,$black_code) : $black_word;
                    $black_word = isset($black_word) && !empty($black_word) ? array_values($black_word) : [];
                    if(!empty($black_word)){
                        //保存指标匹配上的数据与黑名单匹配数据
                        $black_data = [
                            'annex_id'=>$rawData['annex_id'],
                            'itemsig_id'=>$rawData['itemsig_id'],
                            'patient_id'=>$rawData['patient_id'],
                            'raw_id'=>$raw_id,
                            'raw_name'=>$rawData['raw_name'],
                            'black_word'=>json_encode($black_word_arr),
                            'mate_black'=>json_encode($black_word)
                        ];
                        $this->LocalService()->tmpRawblack->save($black_data);
                        unset($black_data);
                    }
                    unset($black_word);
                }
            }
        }
        unset($rawCond);
        unset($rawDatas);
        $annex_arr = array_values($annex_arr);
        if(!empty($annex_arr)){
            //获取化验单匹配数据
            $raw_annex_arr = $this->LocalService()->tmpRawdata->fetchAnnexData(['is_del'=>0,'is_lock'=>0,'annex_id'=>$annex_arr]);
        }
        return $count;
    }

    /**
     * Notes: 获取指定条件下的匹配的数据
     * User: lltyy
     * DateTime: 2023/4/25 10:16
     *
     * @param $where//数据条件
     * @param int $return_type//返回类型【0 指标下的匹配上的黑名单名称 1 指标黑名单匹配记录ID】
     * @return array
     */
    public function fetchBlackMateByRaw($where,$return_type = 0){
        $datas = [];
        if(empty($return_type)){
            $columns = ['id','raw_id','mate_black'];
        }
        if($return_type == 1){
            $columns = ['id','raw_id'];
        }
        //获取指定条件下的识别结果数据
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>['id']
        ];
        $matedatas = $this->fetchAll($cond);
        if(!empty($matedatas)){
            foreach($matedatas as &$matedata) {
                $raw_id = $matedata['raw_id'];
                if(empty($return_type)) {
                    $mate_black = !empty($matedata['mate_black']) ? json_decode($matedata['mate_black'], true) : [];
                    $datas[$raw_id] = $mate_black;
                }
                if($return_type == 1){
                    $datas[$raw_id] = $matedata['id'];
                }
            }
        }
        unset($cond);
        unset($matedatas);
        return $datas;
    }
}