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

namespace Application\Service\DB\Item;

use Interop\Container\ContainerInterface;

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

    /**
     * Notes:获取检查点下的表单数据
     * User: lltyy
     * DateTime: 2022/9/8 9:15
     *
     * @param array $data
     *          item_id 副项目ID
     *          itemsig_id 项目中心ID
     *          checktime_id_arr  项目检查点ID数组
     * @return array
     */
    public function getFormByChecktime($data=[]){
        $item_id = isset($data['item_id']) ? $data['item_id'] : 0;//副项目ID
        $checktime_id_arr = isset($data['checktime_id_arr']) ? $data['checktime_id_arr'] : [];//检查点ID数组
        $form_id_arr = isset($data['form_id_arr']) ? $data['form_id_arr'] : [];//表单ID数组
        $key = isset($data['key']) ? $data['key'] : 0;//键值【0 检查点ID为键值 1 表单ID为键值】

        $where = [
            'is_del' => 0
        ];

        if(!empty($item_id)){
            $where['item_id'] = $item_id;
        }
        if(!empty($checktime_id_arr)){
            $where['check_id'] = $checktime_id_arr;
        }
        if(!empty($form_id_arr)){
            $where['form_id'] = $form_id_arr;
        }
        $where_arr = [
            'where'=>$where,
            'columns'=>['check_id','form_id','is_choose','type']
        ];
        $info_datas = $this->fetchAll($where_arr);

        $arr = [
            'all'=>[],//所有表单
            'select'=>[],//必做表单
            'choose'=>[],//选做表单
            'file'=>[]//研究结果文件表单
        ];

        $FormDatas = $this->LocalService()->itemForm->fetchCol('except_checktime',[
            'is_del'=>0,
            'item_id'=>$item_id,
        ]);

        if(!empty($info_datas)){

            foreach($info_datas as $info_k => $info_data){
                $check_id = intval($info_data['check_id']);
                $form_id = intval($info_data['form_id']);

                if(isset($FormDatas[$form_id]) && in_array($check_id,explode(',',$FormDatas[$form_id]))){
                    continue;
                }

                if($key == 1){
                    $arr['all'][$form_id][$check_id] = $check_id;
                    if($info_data['is_choose'] == 1){
                        $arr['select'][$form_id][$check_id] = $check_id;
                    }else{
                        $arr['choose'][$form_id][$check_id] = $check_id;
                    }
                    if($info_data['type'] == 2){
                        $arr['file'][$form_id][$check_id] = $check_id;
                    }
                }else{
                    $arr['all'][$check_id][$form_id] = $form_id;
                    if($info_data['is_choose'] == 1){
                        $arr['select'][$check_id][$form_id] = $form_id;
                    }else{
                        $arr['choose'][$check_id][$form_id] = $form_id;
                    }
                    if($info_data['type'] == 2){
                        $arr['file'][$check_id][$form_id] = $form_id;
                    }
                }
            }
        }
        return $arr;
    }
}