<?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 Sigworklist extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_worklist_sigworklist';
        parent::__construct($dbAdapter, $container);
    }

    /**
     * Notes:获取指定条件下的项目中心工作清单信息，工作ID为键值
     * User: lltyy
     * DateTime: 2022/10/8 9:30
     *
     * @param $where
     * @param int $key //0 set和workset ID为键值 1 category_info和workset ID 为键值【只有 work_type = 1 的数据采用】
     * @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','worklistset_ids','workset_remark'],
            'order'=>['worklistset_id','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
     * @param int $return_type 【返回类型 0 ID值数组 1 数据】
     */
    public function getSetInfo($where,$work_type=1,$return_type=0){
        $set_info = [];
        $cond = [
            'where'=>$where,
            'columns'=>['id','category_id','info_id','workset_id','work_type','workset_value','worklistset_id','worklistset_ids','workset_remark']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach($datas as $data){
                $data_category = $data['category_id'];
                $data_info = $data['info_id'];
                if(is_array($work_type)){
                    $data_work_type = $data['work_type'];
                    $data_category_info = $data_category.'_'.$data_info;
                    if($data_work_type == 1){
                        $data_workset_id = $data['workset_id'];
                        $set_info[$data_work_type][$data_category_info][$data_workset_id] = $data_workset_id;
                    }elseif($data_work_type == 2){
                        $data_id = $data['id'];
                        $set_info[$data_work_type][$data_category_info][$data_id] = $data_id;
                    }
                }else{
                    $data_id_key = $data_category.'_'.$data_info;
                    $data_workset_id = $data['workset_id'];
                    if($work_type == 2){
                        $data_id_key .= '_'.$data_workset_id;
                    }
                    if($return_type == 1){
                        $title = $data['workset_value'];
                        if(!empty($title) || in_array($work_type,[1,2])){
                            $set_info_data = [
                                'itemsig_worklistset_id'=>$data['worklistset_id'],
                                'itemsig_worklist_id'=>$data['id'],
                                'title'=>$title,
                                'confirmation_result_text'=>'未填写'
                            ];
                            $set_info[$data_id_key][] = $set_info_data;
                        }
                    }else{
                        $set_info[] = $data_id_key;
                    }
                }
            }
        }
        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;
    }


    /**
     * Notes: 更新中心工作清单明细中的人员信息
     * User: lltyy
     * DateTime: 2022/11/21 14:49
     *
     * @param $arr
     * @return int
     */
    public function updateWorkUser($arr){
        $parent_item = $arr['parent_item'] ? $arr['parent_item'] : 0;//主项目ID
        $item_id = $arr['item_id'] ? $arr['item_id'] : 0;//副项目ID
        $itemsig_id = $arr['itemsig_id'] ? $arr['itemsig_id'] : 0;//项目中心ID
        $user_id = $arr['user_id'] ? $arr['user_id'] : 0;//工作流程人员ID
        $where = 'is_del = 0 and find_in_set("'.$user_id.'",worklistset_ids)';
        if(!empty($parent_item)){
            $item_arr = $this->LocalService()->itemInfo->fetchCol('id',[
                'is_del' => 0,
                'fid' => $parent_item,
            ]);
            if(!empty($item_arr)){
                $where['item_id'] = StringHelper::toArray($item_arr);
            }else{
                $where = [
                    'id' => 0,
                ];
            }
        }
        if(!empty($item_id)){
            $where['item_id'] = $item_id;
        }
        if(!empty($itemsig_id)){
            $where['itemsig_id'] = $itemsig_id;
        }
        $cond = [
            'where'=>$where,
            'columns'=>['id','worklistset_ids']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach($datas as $data){
                $data_user_arr = explode(',',$data['worklistset_ids']);
                $delete_key = array_search($user_id,$data_user_arr);
                unset($data_user_arr[$delete_key]);
                $data_users = !empty($data_user_arr) ? implode(',',$data_user_arr) : "";
                $this->update(['worklistset_ids'=>$data_users],['id'=>$data['id']]);
            }
        }
        return 200;
    }
}