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

namespace Application\Service\DB\Listset;

use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\Operator;

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

    /**
     * Notes: 获取指定人角色工作清单数据信息
     * User: lltyy
     * DateTime: 2022/11/16 11:27
     *  all：总数【该中心下 所有工作清单】
     *  over：已完成数【该中心下 完成的工作清单】
     *  flow：审核数【该中心下 审核角色下 审核中的工作清单】
     *  operate：执行数【该中心下 执行角色下 进行中的工作清单】
     * @param $item_id
     */
    public function fetchCountBySig($item_id=0,$user_sigrole_arr=[]){
        $arr = [];
        if(!empty($user_sigrole_arr)){
            $where = [
                'is_del' => 0,
                new Operator('flow_state', Operator::OP_NE, 1)
            ];
            if(!empty($item_id)){
                $where['item_id'] = $item_id;
            }
            $cond = [
                'where'=>$where,
                'columns'=>['itemsig_id','flow_state','update_role','flow_role']
            ];
            $datas = $this->fetchAll($cond);
            if(!empty($datas)){
                foreach($datas as $data){
                    $itemsig_id = $data['itemsig_id'];
                    $arr[$itemsig_id] = [
                        'all'=>isset($arr[$itemsig_id]['all']) && !empty($arr[$itemsig_id]['all']) ? $arr[$itemsig_id]['all'] : 0,
                        'operate'=>isset($arr[$itemsig_id]['operate']) && !empty($arr[$itemsig_id]['operate']) ? $arr[$itemsig_id]['operate'] : 0,
                        'flow'=>isset($arr[$itemsig_id]['flow']) && !empty($arr[$itemsig_id]['flow']) ? $arr[$itemsig_id]['flow'] : 0,
                        'over'=>isset($arr[$itemsig_id]['over']) && !empty($arr[$itemsig_id]['over']) ? $arr[$itemsig_id]['over'] : 0,
                    ];
                    $arr[$itemsig_id]['all'] += 1;

                    $userrole_arr = isset($user_sigrole_arr[$itemsig_id]) && !empty($user_sigrole_arr[$itemsig_id]) ? $user_sigrole_arr[$itemsig_id] : [];
                    if(!empty($userrole_arr)){
                        $userrole_arr = array_values($userrole_arr);
                        $update_role_arr = !empty($data['update_role']) ? explode(',',$data['update_role']) : [];
                        $flow_role_arr = !empty($data['flow_role']) ? explode(',',$data['flow_role']) : [];
                        $update_role_arr = !empty($flow_role_arr) ? array_merge($update_role_arr,$flow_role_arr) : $update_role_arr;
                        $update_role_arr = array_unique($update_role_arr);
                        if(!empty($update_role_arr)){
                            $update_role_arr = array_values($update_role_arr);
                            $repeat_update_arr = array_intersect($userrole_arr,$update_role_arr);
                            if(!empty($repeat_update_arr)){
                                $flow_state = $data['flow_state'];
                                if($flow_state == 4){
                                    $arr[$itemsig_id]['over'] += 1;
                                }
                                if($flow_state == 3){
                                    $arr[$itemsig_id]['flow'] += 1;
                                }
                                if($flow_state == 2){
                                    $arr[$itemsig_id]['operate'] += 1;
                                }
                            }
                        }
                    }
                }
            }
        }
        return $arr;
    }

    /**
     * Notes: 获取未完成的工作清单
     * User: lltyy
     * DateTime: 2024/11/18 8:17
     *
     * @param array $info
     * @return array
     */
    public function fetchNooverSigWork(array $info = []){
        $return_type = isset($info['return_type']) && !empty($info['return_type']) ? $info['return_type'] : 0;//返回类型 0 中心项目患者
        $item_id = isset($info['item_id']) && !empty($info['item_id']) ? $info['item_id'] : 0;//副项目ID
        $itemsig_id = isset($info['itemsig_id']) && !empty($info['itemsig_id']) ? $info['itemsig_id'] : 0;//项目中心ID

        //获取患者检查点信息
        $arr = [];
        $where = [
            'is_del' => 0,
            'flow_state' => [2, 3],
        ];
        if(!empty($item_id)){
            $where['item_id'] = $item_id;
        }
        if(!empty($itemsig_id)){
            $where['itemsig_id'] = $itemsig_id;
        }
        $datas = $this->fetchAll([
            'where'=>$where,
            'columns'=>['item_id','itemsig_id','flow_state','update_role','flow_role'],
            'order'=>['id']
        ]);
        unset($where);
        if(!empty($datas)){
            foreach($datas as $key=>$data){
                $data_item = $data['item_id'];
                $data_itemsig = $data['itemsig_id'];
                $role_arr = [];
                //执行角色
                if(!empty($data['update_role']) && $data['flow_state'] == 2){
                    $role_arr = explode(',',$data['update_role']);
                }
                //审核角色
                if(!empty($data['flow_role']) && $data['flow_state'] == 3){
                    $role_arr = explode(',',$data['flow_role']);
                }
                if($return_type == 0){
                    //每个中心角色的待处理工作清单数
                    $arr['noover_work_count'][$data_itemsig][$data_item] = isset($arr['noover_work_count'][$data_itemsig][$data_item]) && !empty($arr['noover_work_count'][$data_itemsig][$data_item]) ? $arr['noover_work_count'][$data_itemsig][$data_item] : 0;
                    $arr['noover_work_count'][$data_itemsig][$data_item] += 1;

                    //需要工作清单发信的中心
                    $arr['send_mes_sig'] = isset($arr['send_mes_sig']) && !empty($arr['send_mes_sig']) ? $arr['send_mes_sig'] : [];
                    if(!in_array($data_itemsig,$arr['send_mes_sig'])){
                        $arr['send_mes_sig'][] = $data_itemsig;
                    }

                    //需要质疑发信的项目
                    $arr['send_mes_item'] = isset($arr['send_mes_item']) && !empty($arr['send_mes_item']) ? $arr['send_mes_item'] : [];
                    if(!in_array($data_item,$arr['send_mes_item'])){
                        $arr['send_mes_item'][] = $data_item;
                    }
                }

                //释放空间
                unset($data_item);
                unset($data_itemsig);
                unset($role_arr);
                unset($datas[$key]);
            }
        }

        return $arr;
    }

}