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

namespace Application\Service\DB\Collect;

use Interop\Container\ContainerInterface;
use Laminas\Stdlib\ArrayUtils;

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

    /**
     * @note 获取CM判断结果（用药原因）选项信息
     * @return array
     */
    public function getCMOptions(): array
    {
        return ArrayUtils::merge($this->fetchAll([
            'where' => [ 'is_del' => 0, 'take_type' => 6 ],
            'columns' => ['id', 'name'],
            'order' => [ 'take_order' ]
        ]) ?? [], [
            [ 'id' => 1, 'name' => '属于新增AE' ],
            [ 'id' => 2, 'name' => '属于已有AE' ],
        ]);
    }

    /**
     * @note 判断CM用药原因是否是 其他
     * @param int $cmOptionVal
     * @return bool
     */
    public function isCmOptionsOther(int $cmOptionVal): bool
    {
        return $this->getOneFieldVal('id', "take_type = 6 and is_del = 0 and english = 'CMINDC4' ") == $cmOptionVal;
    }


    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2023/4/25 10:16
     *
     * @param $where
     * @param $where
     * @return array
     */
    public function fetchDataByType($where,$columns){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoData){
                $id = $infoData['take_type'];
                $datas[$id][] = $infoData;
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }
    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2023/4/25 10:16
     *
     * @param $where
     * @param $where
     * @return array
     */
    public function fetchDataByKey($where,$columns,$order=['take_order'],$key,$is_more=0){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>$order
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoData){
                $key_str = $infoData[$key];
                if($is_more == 1){
                    $datas[$key_str][] = $infoData;
                }else{
                    $datas[$key_str] = $infoData;
                }
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }

}