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

namespace Application\Service\DB\Ocr;

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

class Ocrannexnew extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_ocr_annex_new';
        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:
     * User: lltyy
     * DateTime: 2023/8/17 9:48
     *
     * @param array $info
     * @return array
     */
    public function fetchAnnexNum(array $info = []){
        $select_type = isset($info['select_type']) && !empty($info['select_type']) ? $info['select_type'] : 0;//查询类型【0 所有数据 1 第一批数据 2 第二批数据】
        $return_type = isset($info['return_type']) && !empty($info['return_type']) ? $info['return_type'] : 0;//中心化验单情况
        $sig_id = isset($info['sig_id']) && !empty($info['sig_id']) ? $info['sig_id'] : 0;//中心ID

        $arr = [];

        //获取中心住院及自费单据数量
        if ($return_type == 2) {
            //获取附件信息
            $where = [
                'is_del' => 0,
                'annex_type' => 8,
            ];
            //获取未删除的患者
            $patient_where = [
                'is_del' => 0,
                new Operator('pid', Operator::OP_GT, 0),
                new Operator('have_status', Operator::OP_LTE, 1),
            ];
            if(!empty($sig_id)){
                $where['sig_id'] = $sig_id;
                $patient_where['pid'] = $sig_id;
            }
            if ($select_type == 1) {
                $patient_where[] = new Operator('id', Operator::OP_LT, 18017);
            }
            if ($select_type == 2) {
                $patient_where[] = new Operator('id', Operator::OP_GTE, 18017);
            }
            $patient_where[] = new Operator('csae_status', Operator::OP_GT, 0);
            $have_patient_arr = $this->LocalService()->ocrSigpatient->fetchCol('id', $patient_where);
            $have_patient_arr = array_values($have_patient_arr);
            if (!empty($have_patient_arr)) {
                $where['patient_id'] = $have_patient_arr;
            } else {
                $where = 'id < 0';
            }
            $cond = [
                'where' => $where,
                'columns' => ['patient_id', 'id']
            ];
            $annexs = $this->fetchAll($cond);
            unset($where);
            unset($cond);
            if (!empty($annexs)) {
                foreach ($annexs as $annexKey => $annex) {
                    $annex_patient = $annex['patient_id'];
                    $arr[$annex_patient] = isset($arr[$annex_patient]) && !empty($arr[$annex_patient]) ? $arr[$annex_patient] : 0;
                    $arr[$annex_patient] += 1;
                    unset($annexs[$annexKey]);
                }
            }
            unset($annexs);
        }


        return $arr;
    }
}