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

namespace Application\Service\DB\Worklist;

use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\Operator;

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

    /**
     * Notes:获取指定条件下的项目工作清单信息，工作ID为键值
     * User: lltyy
     * DateTime: 2022/10/8 9:30
     *
     * @param $where
     * @param int $work_type //类型【1 检查项 2 药物、器械 3 人员】
     * @return array
     */
    public function getDataByWork($where,$key=0){
        $data = [];
        //获取项目工作清单信息
        $where_cond = [
            'where'=>$where,
            'columns'=>['id','worklistset_id','workset_id','workset_value','work_type','category_id','info_id']
        ];
        $work_datas = $this->fetchAll($where_cond);
        if(!empty($work_datas)){
            foreach($work_datas as $work_data){
                $workset_id = $work_data['workset_id'];
                if($key == 1){
                    $category_info = $work_data['category_id'].'_'.$work_data['info_id'];
                    $data[$category_info][$workset_id] = $work_data;
                }else{
                    $worklistset_id = $work_data['worklistset_id'];
                    $data[$worklistset_id][$workset_id] = $work_data;
                }
            }
        }
        return $data;
    }


    /**
     * Notes: 获取指定条件下有设置值的信息
     * User: lltyy
     * DateTime: 2022/10/18 18:01
     *
     * @param $where
     * @param int $work_type
     */
    public function getSetInfo($where,$work_type=1){
        $set_info = [];
        $cond = [
            'where'=>$where,
            'columns'=>['category_id','info_id','workset_id']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach($datas as $data){
                $data_category = $data['category_id'];
                $data_info = $data['info_id'];
                if($work_type == 1){
                    $set_info[] = $data_category.'_'.$data_info;
                }
                if($work_type == 2){
                    $set_info[] = $data_category.'_'.$data_info.'_'.$data['workset_id'];
                }
            }
        }
        return $set_info;
    }

    /**
     * Notes:获取信息数据
     * User: lltyy
     * DateTime: 2022/10/19 12:30
     *
     * @param $work_type
     */
    public function fetchMoreworkByWorkcode($work_type){
        $datas = [];
        //获取字典项Code值
        $workset_code_arr = $this->LocalService()->dictionaryWorkset->fetchCol('work_code', [
            'is_del' => 0,
            'work_type' => $work_type,
            'is_more' => 1,
        ]);
        $workset_arr = !empty($workset_code_arr) ? array_keys($workset_code_arr) : [];
        //获取设置的多条信息数据
        $worklist_where = [
            'is_del' => 0,
            'work_type' => $work_type,
            new Operator('info_id', Operator::OP_GT, 0),
        ];
        if(!empty($workset_arr)){
            $worklist_where['workset_id'] = StringHelper::toArray($workset_arr);
        }else{
            $worklist_where = 'id < 0';
        }
        $worklist_cond = [
            'where'=>$worklist_where,
            'columns'=>['id','info_id','workset_id','workset_value','worklistset_id'],
            'order'=>['info_id','workset_id','worklistset_id']
        ];
        $worklist_datas = $this->fetchAll($worklist_cond);
        if(!empty($worklist_datas)){
            foreach($worklist_datas as $worklist_data){
                $worklist_workset = $worklist_data['workset_id'];
                if(isset($workset_code_arr[$worklist_workset]) && !empty($workset_code_arr[$worklist_workset])){
                    $worklist_info = $worklist_data['info_id'];
                    $worklist_workset_code = $workset_code_arr[$worklist_workset];
                    $datas[$worklist_info][$worklist_workset_code][] = [
                        'id'=>$worklist_data['worklistset_id'],
                        'workset_value'=>$worklist_data['workset_value']
                    ];
                }
            }
        }
        return $datas;
    }
}