<?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;

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

    /**
     * Notes:获取指定条件下的数据，category_info 为键值，信息为值
     * User: lltyy
     * DateTime: 2022/10/19 15:58
     *
     * @param $where
     */
    public function fetchDataByCategoryAndInfo($where){
        $data = [];
        //获取项目工作清单信息
        $where_cond = [
            'where'=>$where,
            'columns'=>['id','category_id','info_id','workset_value'],
            'order'=>['id']
        ];
        $work_datas = $this->fetchAll($where_cond);
        if(!empty($work_datas)){
            foreach($work_datas as $work_data){
                $work_key = $work_data['category_id'].'_'.$work_data['info_id'];
                $data[$work_key] = $work_data;
            }
        }
        return $data;
    }

    /**
     * Notes:同步设置信息【目前只适用work_type=1的数据】
     * User: lltyy
     * DateTime: 2022/10/19 15:47
     *
     * @param $arr
     */
    public function updateSameSet($arr){
        $parent_item = $arr['parent_item'] ? $arr['parent_item'] : 0;
        $item_id = $arr['item_id'] ? $arr['item_id'] : 0;
        $itemsig_id = $arr['itemsig_id'] ? $arr['itemsig_id'] : 0;
        $work_type = $arr['work_type'] ? $arr['work_type'] : 0;
        //获取已有数据信息
        $worklistsetDatas = $this->fetchDataByCategoryAndInfo([
            'is_del' => 0,
            'itemsig_id' => $itemsig_id,
            'work_type' => $work_type,
            'item_id' => $item_id,
        ]);
        $worklistDatas = $this->LocalService()->worklistSigworklist->getDataByWork([
            'is_del' => 0,
            'itemsig_id' => $itemsig_id,
            'work_type' => $work_type,
            'item_id' => $item_id,
        ],1);

        foreach ($arr['update_same'] as $sameCategoryInfo=>$sameDatas){
            $update_worklistset = [];
            $sameCategoryInfo_arr = explode('_',$sameCategoryInfo);
            $update_same = [
                'parent_item' => $parent_item,
                'item_id' => $item_id,
                'itemsig_id' => $itemsig_id,
                'work_type' => $work_type,
                'category_id' => empty($sameCategoryInfo_arr[0]) ? 0 : $sameCategoryInfo_arr[0],
                'info_id' => empty($sameCategoryInfo_arr[1]) ? 0 : $sameCategoryInfo_arr[1]
            ];
            $new_workset_value_arr = [];
            if(isset($worklistsetDatas[$sameCategoryInfo])){
                $update_same['id'] = $worklistsetDatas[$sameCategoryInfo]['id'];
                $workset_value_arr = json_decode($worklistsetDatas[$sameCategoryInfo]['workset_value'],true);
                if(!empty($workset_value_arr)){
                    foreach($workset_value_arr as $workset_value){
                        $workset_value_code = $workset_value['work_code'];
                        if(isset($sameDatas[$workset_value_code])){
                            $sameData_workset_id = $sameDatas[$workset_value_code]['workset_id'];
                            unset($sameDatas[$workset_value_code]['workset_id']);
                            $new_workset_value_arr[] = $sameDatas[$workset_value_code];
                            $update_worklistset[$sameData_workset_id] = $sameDatas[$workset_value_code];
                            unset($sameDatas[$workset_value_code]);
                        }else{
                            $new_workset_value_arr[] = $workset_value;
                        }
                    }
                }
            }
            if(!empty($sameDatas)){
                foreach($sameDatas as $sameData){
                    $sameData_worksetid = $sameData['workset_id'];
                    unset($sameData['workset_id']);
                    $new_workset_value_arr[] = $sameData;
                    $update_worklistset[$sameData_worksetid] = $sameData;
                }
            }
            $update_same['workset_value'] = json_encode($new_workset_value_arr);
            $worklist_result = $this->save($update_same);
            if($worklist_result && !empty($update_worklistset)){
                foreach($update_worklistset as $workset_id=>$update_worklist_data){
                    $update_worklist = [
                        'parent_item' => $parent_item,
                        'item_id'=>$item_id,
                        'itemsig_id'=>$itemsig_id,
                        'work_type' => $work_type,
                        'category_id' => empty($sameCategoryInfo_arr[0]) ? 0 : $sameCategoryInfo_arr[0],
                        'info_id' => empty($sameCategoryInfo_arr[1]) ? 0 : $sameCategoryInfo_arr[1],
                        'worklistset_id' => empty($update_same['id']) ? $worklist_result : $update_same['id'],
                        'workset_id'=>$workset_id,
                        'workset_value'=>empty($update_worklist_data['work_value']) ? '' : $update_worklist_data['work_value'],
                        'worklistset_ids'=>empty($update_worklist_data['worklistset_ids']) ? '' : implode(',',$update_worklist_data['worklistset_ids']),
                        'workset_remark'=>empty($update_worklist_data['workset_remark']) ? '' : $update_worklist_data['workset_remark']
                    ];
                    if(isset($worklistDatas[$sameCategoryInfo]) && isset($worklistDatas[$sameCategoryInfo][$workset_id])){
                        $update_worklist['id'] = $worklistDatas[$sameCategoryInfo][$workset_id]['id'];
                    }
                    $this->LocalService()->worklistSigworklist->save($update_worklist);
                }
            }
        }

        return 200;
    }

    /**
     * Notes: 获取指定条件下的数据，key为键值，信息为值
     * User: lltyy
     * DateTime: 2022/10/28 11:59
     *
     * @param $where
     */
    public function fetchDataByKey($where,$columns=[],$key=''){
        $data = [];
        //获取项目工作清单信息
        $where_cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>['id']
        ];
        $work_datas = $this->fetchAll($where_cond);
        if(!empty($work_datas)){
            foreach($work_datas as $work_data){
                $work_key = $work_data[$key];
                $data[$work_key][] = $work_data;
            }
        }
        return $data;
    }

    /**
     * Notes:获取项目中心工作进度人员
     * User: lltyy
     * DateTime: 2022/11/14 8:59
     *
     * @param $item_id //项目ID
     * @param int $return_type //返回类型【0：下拉数据； 1：人员ID为键值，名称为值的数组】
     * @param int $select_type //查询类型【0：所传中心数据； 1：该项目下可查看的中心下的数据】
     * @return array
     */
    public function fetchUserData($item_id,$itemsig_id=0,$return_type=0,$select_type=0){
        $arr = [];
        $itemsig_name_arr = [];
        //获取项目中心工作进度人员
        $where = [
            'is_del' => 0,
            'work_type' => 3,
            'info_id' => 0,
            'parent_item' => $item_id,
        ];
        if(empty($itemsig_id) && $select_type == 1){
           //获取当前登录人有权限看到的中心
            $itemsig_id_arr = [];
            $user_id = $this->LocalService()->identity->getId();
            $user_id = $this->LocalService()->signatoryUser->findsignatoryUser($user_id);
            $itemsignatoryData = $this->LocalService()->itemSignatory->userRoleSignInfo($item_id,$user_id,'',0,1);
            if(!empty($itemsignatoryData)){
                foreach($itemsignatoryData as $itemsignatory){
                    $itemsignatory_id = $itemsignatory['id'];
                    $itemsig_name_arr[$itemsignatory_id] = $itemsignatory['info_name'];
                    $itemsig_id_arr[] = $itemsignatory_id;
                }
            }
            $itemsig_id = !empty($itemsig_id_arr) ? implode(',',$itemsig_id_arr) : 0;
        }
        if(!empty($itemsig_id)){
            $where['itemsig_id'] = StringHelper::toArray($itemsig_id);
        }else{
            $where = [
                'id' => 0,
            ];
        }
        $itemsig_workuser_cond = [
            'where'=>$where,
            'columns'=>['id','workset_value','itemsig_id']
        ];
        $itemsig_workusers = $this->fetchAll($itemsig_workuser_cond);
        if(!empty($itemsig_workusers)){
            foreach($itemsig_workusers as $itemsig_workuser){
                $itemsig_workuser_value = json_decode($itemsig_workuser['workset_value'],true);
                $itemsig_workuser_id = '1_'.$itemsig_workuser['id'];
                $itemsig_workuser_name = '【中心人员】'.$itemsig_workuser_value['userName'];
                if(!empty($itemsig_name_arr)){
                    $itemsig_id = $itemsig_workuser['itemsig_id'];
                    $itemsig_workuser_name = isset($itemsig_name_arr[$itemsig_id]) ? '【'.$itemsig_name_arr[$itemsig_id].'】'.$itemsig_workuser_value['userName'] : $itemsig_workuser_value['userName'];
                }
                if(isset($itemsig_workuser_value['userJob']) && $itemsig_workuser_value['userJob'] != ''){
                    $itemsig_workuser_name .= '('.$itemsig_workuser_value['userJob'].')';
                }
                if($return_type == 1){
                    $arr[$itemsig_workuser_id] = $itemsig_workuser_name;
                }else{
                    $arr[] = [
                        'value'=>$itemsig_workuser_id,
                        'label'=>$itemsig_workuser_name
                    ];
                }
            }
        }
        return $arr;
    }

    /**
     * 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';
        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','workset_value','worklistset_ids']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach($datas as $data){
                $workset_value_arr = json_decode($data['workset_value'],true);
                if(!empty($workset_value_arr)){
                    foreach($workset_value_arr as $workset_value_key=>$workset_value_info){
                        if(isset($workset_value_info['worklistset_ids']) && in_array($user_id,$workset_value_info['worklistset_ids'])){
                            $workset_value_user_arr = $workset_value_info['worklistset_ids'];
                            $workset_value_delete_key = array_search($user_id,$workset_value_user_arr);
                            unset($workset_value_user_arr[$workset_value_delete_key]);
                            $workset_value_arr[$workset_value_key]['worklistset_ids'] = array_values($workset_value_user_arr);
                        }
                    }
                }

                $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) : "";
                $update_data = [
                    'workset_value'=>json_encode($workset_value_arr),
                    'worklistset_ids'=>$data_users
                ];
                $this->update($update_data,['id'=>$data['id']]);
            }
        }

        //更新中心工作清单明细设置人员信息
        $this->LocalService()->worklistSigworklist->updateWorkUser($arr);//更新受试者工作清单中设置的人员

        //更新受试者工作清单设置人员信息
        $this->LocalService()->worklistPatientworklist->updateWorkUser($arr);

        //更新业务流程中的人员
        $this->LocalService()->businessRecord->updateWorkUser($arr);

        return 200;
    }
}