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

namespace Application\Service\DB\Collect;

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

class Itemdrug extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_collect_itemdrug';
        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['id'];
                    $datas[$id] = $infoData;
                }

                if($return_type == 1){
                    $keyword_id = $infoData['drug_id'];
                    $item_id = $infoData['item_id'];
                    $datas[$item_id][$keyword_id] = $infoData;
                }

                if($return_type == 2){
                    $keyword_id = $infoData['drug_id'];
                    $item_id = $infoData['item_id'];
                    $datas[$keyword_id][$item_id] = $infoData;
                }
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }

    /**
     * Notes: 获取指定药物相关的所有药物ID
     * User: lltyy
     * DateTime: 2025/9/8 17:28
     *
     */
    public function fetchDrugAndChilds(array $params){
        $item_id = isset($params['item_id']) && !empty($params['item_id']) ? $params['item_id'] : 0;//项目ID
        $drug_id = isset($params['drug_id']) && !empty($params['drug_id']) ? $params['drug_id'] : 0;//药物ID
        $arr = [];
        //获取该项目下设置的药物信息
        $item_drug_arr = [];
        $itemDrugs = $this->LocalService()->collectItemdrug->fetchAll([
            'where'=>['is_del'=>0,'item_id'=>$item_id],
            'columns'=>['drug_id','parent_drug']
        ]);
        if(!empty($itemDrugs)){
            $item_drug_arr = array_values(array_column($itemDrugs,'drug_id'));
            foreach($itemDrugs as $itemDrugKey=>$itemDrug){
                if($itemDrug['drug_id'] == $drug_id && !in_array($itemDrug['drug_id'],$arr)){
                    $arr[] = $itemDrug['drug_id'];
                }
                if(!empty($itemDrug['parent_drug']) && $itemDrug['parent_drug'] == $drug_id && !in_array($itemDrug['drug_id'],$arr)){
                    $arr[] = $itemDrug['drug_id'];
                }
                unset($itemDrugs[$itemDrugKey]);
            }
        }
        unset($itemDrugs);
        //项目未设置 则获取字典项信息
        if(empty($arr)){
            //获取该项目下对应的的该药物信息
            $drug_where = ['is_del'=>0,'id'=>$drug_id];
            if(!empty($item_drug_arr)){
                $drug_where[] = new NotIn('id',$item_drug_arr);
            }
            $drug_parent = $this->LocalService()->collectDrug->getOneFieldVal('parent_drug',$drug_where);
            $drug_parent = empty($drug_parent) ? $drug_id : $drug_parent;
            unset($drug_where);
            $all_drug_where = [
                'is_del'=>0,
                'or'=>[
                    ['id'=>$drug_parent],
                    ['parent_drug'=>$drug_parent],
                ]
            ];
            if(!empty($item_drug_arr)){
                $all_drug_where[] = new NotIn('id',$item_drug_arr);
            }
            unset($drug_parent);
            $arr = $this->LocalService()->collectDrug->fetchCol('id',$all_drug_where);
            unset($all_drug_where);
        }
        $arr = !empty($arr) ? array_values($arr) : [];

        unset($item_drug_arr);
        return $arr;
    }

}