<?php

namespace Application\Service\DB\Item;

use Interop\Container\ContainerInterface;

class ItemPatientdrugset extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        if (!$this->tableName) {
            $this->tableName = 'yikeen_item_patientdrugset';
        }

        parent::__construct($dbAdapter, $container);
    }

    /**
     * Notes: 中心和药物批次对应数组
     * @param $type 1 一对一 2 一对多
     * @return
     * @author haojinhua
     * @date 2024-05-21
     */
    public function getSigDrugbatch($where,$key,$filed,$type){
        $res = [];
        $where = empty($where) ? 'is_del = 0' : $where;
        $cond = [
            'where' => $where,
            'columns' => $filed,
            'order' => ['id'],
        ];
        $datas = $this->fetchAll($cond);

        //药物批次对应数组
        $batch_arr = $this->LocalService()->itemDrugbatch->getFiledRelation('is_del = 0','id',['id','drug_batch'],1);
        if(!empty($datas)){
            foreach ($datas as $data){
                if(in_array('drugbatch_id',$filed)){
                    $batchName = isset($batch_arr[$data['drugbatch_id']]) && !empty($batch_arr[$data['drugbatch_id']]) ? $batch_arr[$data['drugbatch_id']]['drug_batch'] : '';
                }
                $data['batch_name'] = $batchName;
                if(in_array('failure_time',$filed)){
                    $data['batch_name'] = $batchName.'('.$data['failure_time'].')';
                }
                if($type == 1){
                    $res[$data[$key]] = $data;
                }else{
                    $res[$data[$key]][] = $data;
                }
            }
        }

        return $res;
    }

}
