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

namespace Application\Service\DB\Collect;

use Interop\Container\ContainerInterface;

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


    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2023/6/2 7:50
     *
     * @param $where 条件
     * @param $columns 信息
     * @param int $return_type 返回类型【0 ID为键值 1 项目、通用字典项ID为键值 2 通用字典项ID、项目为键值】
     * @return array
     */
    public function fetchDataById($where,$columns,$return_type=0){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoData){
                if(empty($return_type)){
                    $id = $infoData['item_hospital_id'];
                    $datas[$id] = $infoData;
                }

                if($return_type == 1){
                    $key_id = $infoData['item_hospital_id'];
                    $itemsig_id = $infoData['itemsig_id'];
                    $datas[$itemsig_id][$key_id] = $infoData;
                }

                if($return_type == 2){
                    $key_id = $infoData['item_hospital_id'];
                    $itemsig_id = $infoData['itemsig_id'];
                    $datas[$key_id][$itemsig_id] = $infoData;
                }
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }

    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2023/4/25 10:16
     *
     * @param $where
     * @return array
     */
    public function fetchAllDataAndChild($where,$columns){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>['pid','info_order']
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            $parent_key_arr = [];
            $key = 0;
            foreach($infoDatas as $infoKey=>$infoData){
                $pid = $infoData['pid'];
                if(empty($pid)){
                    $datas[$key] = $infoData;
                    $datas[$key]['children'] = [];
                    $id = $infoData['id'];
                    $parent_key_arr[$id] = $key;
                    $key++;
                }else{
                    $parent_key = isset($parent_key_arr[$pid]) && !empty($parent_key_arr[$pid]) ? $parent_key_arr[$pid] : 0;
                    $datas[$parent_key]['children'][] = $infoData;
                }
                unset($infoDatas[$infoKey]);
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }


}