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

namespace Application\Service\DB\Collect;

use Interop\Container\ContainerInterface;

class Hospital extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_collect_hospital';
        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/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;
    }


}