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

namespace Application\Service\DB\Item;

use Application\Service\DB\AbstractDb;
use Application\Service\Exceptions\InvalidArgumentException;
use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\Like;
use Laminas\Db\Sql\Predicate\Operator;
use Laminas\Stdlib\ArrayUtils;

class Signatory extends AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_item_signatory';
        parent::__construct($dbAdapter, $container);
    }

    /**
     * Notes: 获取项目下合作方所有角色人员信息
     * User: llbjj
     * DateTime: 2022/9/6 17:47
     *
     * @param int $item_id
     * @param int $itemsig_id
     * @param int $rolePrefix 角色id前缀
     */
    public function getRoleSiguser(int $item_id, int $itemsig_id, $rolePrefix = '') {
        $roleSigUserData = [];
        $relationWhere = [
            'status' => 0,
            'item_id' => $item_id,
        ];
        if($itemsig_id) $relationWhere['item_sign_id'] = $itemsig_id;

        $relationDatas = $this->LocalService()->roleSignatoryRelation->fetchAll([
            'where' => $relationWhere,
            'columns' => ['id', 'role_id', 'signatory_user', 'item_sign_id']
        ]);

        $realrole = $this->LocalService()->realRole->fetchAll([
            'where' => [
                'is_del' => 0,
                'item_id' => $item_id,
            ],
            'columns' => ['id', 'source_type']
        ]);

        $roleaarr = [];
        foreach($realrole as $k=>$v){
            $roleaarr[$v['id']] = $v['source_type'];
        }

        $signInfo = $this->LocalService()->itemSignatory->fetchCol('signatory_id',[
            'is_del' => 0,
            'item_id' => $item_id,
        ]);
        $userWhere['where'] = [
            'is_del' => 0,
            'status' => 0,
            'info_id' => $signInfo
        ];
        $userWhere['columns'] = ['user_name','info_id','id'];
        $userDatas = $this->LocalService()->signatoryUser->fetchAll($userWhere);

        $userDatasHandle = [];
        foreach($userDatas as $v){
            $userDatasHandle[$v['info_id']][$v['id']] = $v;
        }

        if(!empty($relationDatas)) {
            $sigUserData = $this->LocalService()->signatoryUser->fetchCol('user_name', 'is_del = 0  and status = 0 ');

            foreach ($relationDatas as $relationData) {

                if($roleaarr[$relationData['role_id']] == 0){
                    if(!isset($userDatasHandle[$signInfo[$relationData['item_sign_id']]][$relationData['signatory_user']])) continue;
                }

                if(isset($userDatasHandle[$signInfo[$relationData['item_sign_id']]][$relationData['signatory_user']])){

                    $roleSigUserData[$relationData['item_sign_id']][$rolePrefix.$relationData['role_id']][] = $userDatasHandle[$signInfo[$relationData['item_sign_id']]][$relationData['signatory_user']]['user_name'];
                }else{
                    if($sigUserData[$relationData['signatory_user']] == '')continue;
                    $roleSigUserData[$relationData['item_sign_id']][$rolePrefix.$relationData['role_id']][] = $sigUserData[$relationData['signatory_user']];
                }
            }
        }

        return $roleSigUserData;
    }

    /**
     * Notes: 数据保存前字段验证
     * User: llbjj
     * DateTime: 2022/9/6 13:13
     *
     * @param array $saveData
     * @return bool
     */
    public function validSave(array &$saveData)
    {
        // 验证本项目是否已存在本中心的信息
        if(!$saveData['id'] and $this->getCount([
                'is_del' => 0,
                'signatory_id' => $saveData['signatory_id'],
                'item_id' => $saveData['item_id'],
            ])) {
            $signatoryName = $this->LocalService()->signatoryInfo->getOneFieldVal('info_name', [
                'is_del' => 0,
                'id' => $saveData['signatory_id'],
            ]);
            throw new InvalidArgumentException("合作方【{$signatoryName}】已存在，请误重复添加");
        }
    }

    public function save(array $saveData)
    {
        $this->validSave($saveData);
        return parent::save($saveData); // TODO: Change the autogenerated stub
    }


    public function getRole($item_id){
        $whereRealrole['where'] = [
            'status' => 0,
            'is_del' => 0,
            'item_id' => $item_id,
        ];
        $whereRealrole['columns'] = ['id','item_id', 'role_name','source_type'];
        $whereRealrole['order'] = ['sort'];
        $realroleData = $this->LocalService()->realRole->fetchAll($whereRealrole);
        return $realroleData;
    }

    /**
     * 角色表单
     **/
    public function realroleSetArr($item_id,$signatory_id=0){
        $roleData = $this->getRole($item_id);
        //组装角色的表单验证
        $setarray = [];
        foreach($roleData as $key=>$val){
            $arraykey = 'RealRole'.$val['id'];
            $array['type'] = 'SelectUser';
            $array['label'] = $val['role_name'];
            $array['placeholder'] = "请输入".$val['role_name'];
            $array['prop'] = 'RealRole'.$val['id'];
            $array['multiple'] = true;
            $array['options'] = [];
            $array['source_type'] = $val['source_type'];
            $array['val_soure'] = function($sv, $val){
                $userNameDatas = $sv->signatoryUser->fetchCol('user_name', [
                    'id' => is_array($val) ? $val : explode(',', $val)
                ]);
                return !empty($userNameDatas) ? implode(',', $userNameDatas) : '';
            };

            foreach($array['options'] as $k=>$v){
               if($v['status'] == 1){
                   $array['options'][$k]['disabled'] = true;
               }else{
                   $array['options'][$k]['disabled'] = false;

               }
               unset($array['options'][$k]['status']);
            }
            $setarray[$arraykey] = $array;
        }
        return $setarray;
    }


    /**
     * Notes: 查询中心
     * @param int $itemsig_id 中心id
     * @param int $item_id 项目id（小项目id，有项目id时返回给数据结构用）
     * @param int $type 1为大项目id，0为小项目id
     * @param string $search 搜索医院名称
     * @return array
     */
    public function itemSigninfo($item_sign_id = 0, $item_id = 0, $type = 0, $search = '',$showType = 0) {
        //项目合作方
        if (empty($item_sign_id)) {
            if ($type) {
                $whereItemSignatory['where'] = [
                    'is_del' => 0,
                    'item_id' => $item_id,
                ];
            } else {
                //数据结构调用
                $newItemSignatoryData = [];
                $bigItemId = 0;
                $itemFid = $this->LocalService()->itemInfo->getOneFieldVal('fid', [
                    'is_del' => 0,
                    'id' => $item_id,
                ]);
                if (!empty($itemFid)) {
                    $bigItemId = $this->LocalService()->itemInfo->getOneFieldVal('id', [
                        'is_del' => 0,
                        'id' => $itemFid,
                    ]);
                }
                if (empty($bigItemId) || empty($itemFid)) {
                    return $newItemSignatoryData;
                }
                $whereItemSignatory['where'] = [
                    'is_del' => 0,
                    'item_id' => $bigItemId,
                ];
            }

        } else {
            $whereItemSignatory['where'] = [
                'is_del' => 0,
                'id' => explode(',', $item_sign_id)
            ];
        }
        $whereItemSignatory['columns'] = ['id', 'item_id', 'signatory_id','number','order','varname','is_teamleader','remark','plan_joingroup'];//, 'department_id'

        $whereItemSignatory['order'] = ['order','number'];
        $itemSignatoryData = $this->LocalService()->itemSignatory->fetchAll($whereItemSignatory);
        if (!empty($itemSignatoryData)) {
            $sign_ids = implode(',',array_filter(array_unique( array_column($itemSignatoryData, 'signatory_id'))));
            //合作方===signatoryInfo
            $whereSignatoryInfo['where'] = [
                'is_del' => 0,
                'id' => explode(',', $sign_ids),
            ];
            $whereSignatoryInfo['columns'] = ['id', 'info_name'];
            if(!empty($search)){
                $whereSignatoryInfo['where'][] = new Like('info_name', "%{$search}%");
            }
            $sign_datas = $this->LocalService()->signatoryInfo->fetchAll($whereSignatoryInfo);

            //机构==========signatoryDepartment
            $signIdArr = [];
            foreach ($itemSignatoryData as $key => $val) {

                if ($sign_datas) {
                    foreach ($sign_datas as $ks => $vs) {
                        if ($vs['id'] == $itemSignatoryData[$key]['signatory_id']) {
                            $signIdArr[] = $vs['id'];
                            if($showType == 1){
                                $itemSignatoryData[$key]['info_name'] = '【'.$val['number'].'】'.$vs['info_name'];
                            }else{
                                $itemSignatoryData[$key]['info_name'] = $vs['info_name'];
                            }
                        }

                    }
                }
            }
            if(!empty($search)){
                foreach ($itemSignatoryData as $itemSignatoryDataKey=>$itemSignatoryDataVaule) {
                    if (!in_array($itemSignatoryDataVaule['signatory_id'],$signIdArr)){
                        unset($itemSignatoryData[$itemSignatoryDataKey]);

                    }
                }
                $itemSignatoryData = array_merge($itemSignatoryData);
            }
            //数据结构调用
            if (!empty($item_id)) {
                if (empty($type)) {
                    foreach ($itemSignatoryData as $key => $val) {
                        $newItemSignatoryData[$key]['label'] = $val['info_name'];
                        $newItemSignatoryData[$key]['value'] = $val['id'];
                        $newItemSignatoryData[$key]['number'] = $val['number'];
                        $newItemSignatoryData[$key]['item_id'] = $val['item_id'];
                        $newItemSignatoryData[$key]['order'] = $val['order'];
                        $newItemSignatoryData[$key]['varname'] = $val['varname'];
                        $newItemSignatoryData[$key]['is_teamleader'] = $val['is_teamleader'];
                        $newItemSignatoryData[$key]['remark'] = $val['remark'];
                        $newItemSignatoryData[$key]['plan_joingroup'] = $val['plan_joingroup'];
                    }
                    return $newItemSignatoryData;
                }
            }
        }
        return $itemSignatoryData;
    }


    /*
     * 查询当前用户存在角色的中心
     * 小程序/real使用
     * $item_id  项目ID(主项目)
     * $user_id 用户ID(合作方联系人ID signatory_user)
     * */
    public function userRoleSignInfo($item_id,$user_id,$search='',$itemsig_id = 0,$flag = 0){
        //查询项目
        $parent_item = $this->LocalService()->itemInfo->getOneFieldVal('fid',[
            'is_del' => 0,
            'id' => $item_id,
        ]);
        if(!empty($parent_item)){
            $item_id = $parent_item;
        }
        $item_sign_id_arr = [];
        if(!empty($itemsig_id)){
            $item_sign_id_arr = explode(',',$itemsig_id);
        }else{
            $roleSignatoryRelationDatas = $this->LocalService()->roleSignatoryRelation->changeUser($item_id,$user_id);
            if(!empty($roleSignatoryRelationDatas)) {
                $item_sign_id_arr = array_unique(array_column($roleSignatoryRelationDatas, 'item_sign_id'));
            }
        }

        if(empty($item_sign_id_arr)) return [];
        //获取合作方名称
        if($search != '') $where = [
            new Like('info_name', "%{$search}%"),
        ];
        $signatoryinfoName = $this->LocalService()->signatoryInfo->fetchCol('info_name',ArrayUtils::merge([
            'is_del' => 0,
        ], $where ?? []));

        //获取中心
        $whereitemSignatory['where'] = [
            'is_del' => 0,
            'item_id' => $item_id,
            'id' => $item_sign_id_arr,
        ];
        //查询角色
        $realRoleIdArr = $this->LocalService()->realRole->fetchCol('id',[
            'is_del' => 0,
            'code' => 'yjys',
            'item_id' => $item_id,
        ]);

        $signatoryIsTeamleader = [];
        if (!empty($realRoleIdArr)){
            $whereroleSignatoryRelation['where'] = [
                'status' => 0,
                'is_del' => 0,
                'item_id' => $item_id,
                'item_sign_id' => $item_sign_id_arr,
                'signatory_user' => $user_id,
                'role_id' => $realRoleIdArr,
            ];

            $rolesignatoryrelationDate = $this->LocalService()->roleSignatoryRelation->fetchCol('item_sign_id',$whereroleSignatoryRelation['where']);

            if (!empty($rolesignatoryrelationDate)){
                //是否为组长
                $signatoryIsTeamleader = $this->LocalService()->itemSignatory->fetchCol('is_teamleader',ArrayUtils::merge($whereitemSignatory['where'], [
                    'is_teamleader' => 1,
                    ])
                );
                if (!empty($signatoryIsTeamleader)){
                    if(in_array(array_keys($signatoryIsTeamleader)[0],$rolesignatoryrelationDate) && !empty($flag)){
                        $whereitemSignatory['where'] = [
                            'is_del' => 0,
                            'item_id' => $item_id,
                        ];
                    }
                }
            }
        }

        $whereitemSignatory['columns'] = ['id','item_id','signatory_id','number','is_teamleader','plan_joingroup'];
        $whereitemSignatory['order'] = ['order','number'];
        $itemSignatoryDatas = $this->LocalService()->itemSignatory->fetchAll($whereitemSignatory);
        foreach($itemSignatoryDatas as $key=>$val){
            if(!empty($signatoryinfoName[$val['signatory_id']])){
                $itemSignatoryDatas[$key]['info_name'] = $signatoryinfoName[$val['signatory_id']];
                $itemSignatoryDatas[$key]['signatoryIds'] = 0;
                //为0默认只读，1时请读写
                $itemSignatoryDatas[$key]['signatoryLookwriteFlag'] = 1;
                if (!empty($signatoryIsTeamleader)){
                    if (in_array(1,$signatoryIsTeamleader) && !empty($flag)){
                        $itemSignatoryDatas[$key]['signatoryIds'] = implode(',', $item_sign_id_arr);
                    }
                }
            }else{
                unset($itemSignatoryDatas[$key]);
            }
        }

        return $itemSignatoryDatas;

    }
    public function getSignatoryIsTeamleaderById($data){
        if (empty($data)) return 0;
        foreach ($data as $k=>$v){
            if ($v['is_teamleader'] == 1){
                return $v['id'];
            }
        }
    }

    /**
     * Notes:获取项目中心信息
     * User: lltyy
     * DateTime: 2022/9/21 14:53
     *
     * @param $arr
     */
    public function getItemSignatoryData($arr = []){
        $flag = isset($arr['flag']) ? $arr['flag'] : 0;
        $user_id = isset($arr['user_id']) ? $arr['user_id'] : 0;
        $item_id = isset($arr['item_id']) ? $arr['item_id'] : 0;
        $search = isset($arr['search']) ? $arr['search'] : '';
        $type = isset($arr['type']) ? $arr['type'] : 0;
        $itemsig_id = isset($arr['itemsig_id']) ? $arr['itemsig_id'] : 0;
        $newItemSignInfoData = [];
        if (!empty($flag)) {
            $user_id = $this->LocalService()->signatoryUser->findsignatoryUser($user_id);
            $newItemSignInfoData = $this->userRoleSignInfo($item_id, $user_id, $search,$itemsig_id);
        }else{
            $itemSignInfoData = $this->itemSigninfo($itemsig_id, $item_id, $type,$search);
            if (empty($type)){
                if (!empty($itemSignInfoData)){
                    foreach ($itemSignInfoData as $itemSignInfoDataKey=>$itemSignInfoDataValue) {
                        $newItemSignInfoData[$itemSignInfoDataKey]['id'] = isset($itemSignInfoDataValue['value']) && !empty($itemSignInfoDataValue['value']) ? $itemSignInfoDataValue['value'] : $itemSignInfoDataValue['id'];
                        $newItemSignInfoData[$itemSignInfoDataKey]['info_name'] = isset($itemSignInfoDataValue['label']) && !empty($itemSignInfoDataValue['label']) ? $itemSignInfoDataValue['label'] : $itemSignInfoDataValue['info_name'];
                        $newItemSignInfoData[$itemSignInfoDataKey]['number'] = $itemSignInfoDataValue['number'];
                    }
                }
            }else{
                $newItemSignInfoData = $itemSignInfoData;
            }
        }
        return $newItemSignInfoData;
    }


    /**
     * Notes: 根据项目ID和中心ID 获取项目合作方ID
     * @param
     * @return
     * @author haojinhua
     * @date 2024-05-22
     */
    public function getItemSigid($where){
        $res = [];
        $cond = [
            'where' => $where,
            'columns' => ['id','item_id','signatory_id'],
            'order' => ['id'],
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach ($datas as $data){
                $item_id      = $data['item_id'];
                $signatory_id = $data['signatory_id'];
                $res[$item_id][$signatory_id] = $data['id'];
            }
        }
        return $res;
    }

    /**
     * @note 获取项目中心名称
     * @param int $mainItemId
     * @return array
     */
    public function getItemSignatoryNameList(int $mainItemId = 0): array
    {
        $signatoryName = $this->LocalService()->signatoryInfo->fetchCol('info_name', ['is_del' => 0]);

        $itemSignatoryList = $this->LocalService()->itemSignatory->fetchAll([
            'where' => ArrayUtils::merge([
                'is_del' => 0,
            ], $mainItemId ? ['main_item_id' => $mainItemId] : []),
            'columns' => ['id', 'signatory_id', 'number', 'item_id'],
            'order' => ['order', 'number'],
        ]);

        $formatList = [];
        if(!empty($itemSignatoryList)){
            foreach ($itemSignatoryList as &$itemSignatory){
                $itemSignatory['name'] = sprintf("%s【%s】", $signatoryName[$itemSignatory['signatory_id']], $itemSignatory['number']);

                $item_id = $itemSignatory['item_id'];
                $formatList[$item_id][] = array_diff_key($itemSignatory, array_flip(['signatory_id', 'number', 'item_id']));;
            }
        }

        return $mainItemId ? $formatList[$mainItemId] : $formatList;
    }

    /**
     * Notes : 通过项目中心关联表ID 获取中心名称
     * @param $id
     * @return \phpDocumentor\Reflection\Types\Mixed|string|void
     * Author:唐伯虎不虎
     * Date:2024/7/11 14:38
     */
    public function _getsignatoryName($id)
    {
        $signatory_id = $this->getOneFieldVal('signatory_id',[
            'id' => $id,
            'is_del' => 0,
        ]);

        if(empty($signatory_id)) return '';

        $signatory_name = $this->LocalService()->signatoryInfo->getOneFieldVal('info_name',[
            'id' => $signatory_id,
            'is_del' => 0,
        ]);

        if(empty($signatory_name)) return '';

        return $signatory_name;
    }

    /**
     * Notes: 获取oa中心名称
     * @param
     * @return
     * @author haojinhua
     * @date 2024-07-19
     */
    public function getoaSigData(){
        $res = [];
        $oa_sigs = $this->LocalService()->OAClient->signatoryInfoList();
        if(!empty($oa_sigs)){
            foreach ($oa_sigs as $oa_sig){
                $res[$oa_sig['value']] = $oa_sig['label'];
            }
        }

        return $res;
    }

    /**
     * Notes: 中心人员发信待处理信息数
     *
     * User: lltyy
     * DateTime: 2024/11/20 16:45
     *
     */
    public function getSendMesHandleData(array $info = []){
        $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
        $return_type = isset($info['return_type']) && !empty($info['return_type']) ? $info['return_type'] : 0;//返回类型 0 需要发提醒的人员openid  1 每个人对应的待处理明细 2 项目中心提醒数量
        $now_date = isset($info['now_date']) && !empty($info['now_date']) ? $info['now_date'] : '';//当前日期
        $send_sig = [];//需要发信的中心
        $send_item = [];//需要发信的项目
        $save_send_detail = [];//需要保存的发信明细信息
        $save_send_user = [];//需要保存的发信人员信息

        //获取各个中心 当日预约的人员 以及 已进窗口期但未预约的人员
        $makeData = $this->LocalService()->itemPatientchecktime->fetchMakeSigPatient(['now_date'=>$now_date,'item_id'=>$item_id,'itemsig_id'=>$itemsig_id]);
        $sigNowMakeData = isset($makeData['now_make']) && !empty($makeData['now_make']) ? $makeData['now_make'] : [];//当日预约人员
        $sigNoMakeData = isset($makeData['no_make']) && !empty($makeData['no_make']) ? $makeData['no_make'] : [];//已进窗口期但未预约的人员
        if(isset($makeData['send_mes_sig']) && !empty($makeData['send_mes_sig'])){
            $send_sig = !empty($send_sig) ? array_merge($send_sig,$makeData['send_mes_sig']) : $makeData['send_mes_sig'];
        }
        if(isset($makeData['send_mes_item']) && !empty($makeData['send_mes_item'])){
            $send_item = !empty($send_item) ? array_merge($send_item,$makeData['send_mes_item']) : $makeData['send_mes_item'];
        }
        unset($makeData);

        //获取各个中心下待处理的工作计划
        $workData = $this->LocalService()->listsetSiglist->fetchNooverSigWork(['item_id'=>$item_id,'itemsig_id'=>$itemsig_id]);
        $sigWorkCount = isset($workData['noover_work_count']) && !empty($workData['noover_work_count']) ? $workData['noover_work_count'] : [];
        if(isset($workData['send_mes_sig']) && !empty($workData['send_mes_sig'])){
            $send_sig = !empty($send_sig) ? array_merge($send_sig,$workData['send_mes_sig']) : $workData['send_mes_sig'];
        }
        if(isset($workData['send_mes_item']) && !empty($workData['send_mes_item'])){
            $send_item = !empty($send_item) ? array_merge($send_item,$workData['send_mes_item']) : $workData['send_mes_item'];
        }
        unset($workData);

        //获取各个中心下待处理的ESD质疑
        $esdQuestionData = $this->LocalService()->itemQuestion->fetchNoreplyQuestion(['item_id'=>$item_id,'itemsig_id'=>$itemsig_id]);
        $sigEsdQuestionCount = isset($esdQuestionData['noreplay_question_count']) && !empty($esdQuestionData['noreplay_question_count']) ? $esdQuestionData['noreplay_question_count'] : [];
        if(isset($esdQuestionData['send_mes_sig']) && !empty($esdQuestionData['send_mes_sig'])){
            $send_sig = !empty($send_sig) ? array_merge($send_sig,$esdQuestionData['send_mes_sig']) : $esdQuestionData['send_mes_sig'];
        }
        if(isset($esdQuestionData['send_mes_item']) && !empty($esdQuestionData['send_mes_item'])){
            $send_item = !empty($send_item) ? array_merge($send_item,$esdQuestionData['send_mes_item']) : $esdQuestionData['send_mes_item'];
        }
        unset($esdQuestionData);

        //获取各个中心下待处理的EDC质疑
        $edcQuestionData = $this->LocalService()->itemPatientFormLogicError->fetchNoreplyQuestion(['item_id'=>$item_id,'itemsig_id'=>$itemsig_id]);
        $sigEdcQuestionCount = isset($edcQuestionData['noreplay_question_count']) && !empty($edcQuestionData['noreplay_question_count']) ? $edcQuestionData['noreplay_question_count'] : [];
        if(isset($edcQuestionData['send_mes_sig']) && !empty($edcQuestionData['send_mes_sig'])){
            $send_sig = !empty($send_sig) ? array_merge($send_sig,$edcQuestionData['send_mes_sig']) : $edcQuestionData['send_mes_sig'];
        }
        if(isset($edcQuestionData['send_mes_item']) && !empty($edcQuestionData['send_mes_item'])){
            $send_item = !empty($send_item) ? array_merge($send_item,$edcQuestionData['send_mes_item']) : $edcQuestionData['send_mes_item'];
        }
        unset($edcQuestionData);

        //获取需要发信的项目
        $itemDatas = [];
        if(!empty($send_item)){
            $itemDatas = $this->LocalService()->itemInfo->fetchDataByKey([
                'is_del' => 0,
                'status' => 0,
                'id' => $send_item,
            ],['id','number','name','var_name'],['fid','item_order'],['id'=>0]);
            $itemDatas = isset($itemDatas['id']) && !empty($itemDatas['id']) ? $itemDatas['id'] : [];
        }
        unset($send_item);

        //获取需要发信的信息
        $send = [];
        $where = [
            'is_del' => 0,
            new Operator('signatory_id', Operator::OP_GT, 0),
        ];
        if(!empty($item_id)){
            $parent_item = $this->LocalService()->itemInfo->getOneFieldVal('fid', [
                'is_del' => 0,
                'is_mainitem' => 0,
                'id' => $item_id,
            ]);
            $where['item_id'] = $parent_item;
        }
        if(!empty($itemsig_id)){
            $where['id'] = $itemsig_id;
        }
        if($return_type != 2){
            if(!empty($send_sig)){
                $where['id'] = $send_sig;
            }else{
                $where = 'id < 0';
            }
        }
        unset($send_sig);
        $sigDatas = $this->fetchAll([
            'where'=>$where,
            'columns'=>['id','item_id','signatory_id','number'],
            'order'=>['item_id','order']
        ]);
        unset($where);
        if(!empty($sigDatas)){
            //获取中心名称
            $sigNameDatas = $this->LocalService()->signatoryInfo->fetchCol('info_name',[
                'is_del' => 0,
                'id' => array_values(array_column($sigDatas,'signatory_id')),
            ]);
            //获取中心角色人员信息以及开通发信提醒的项目中心
            $sigAdminUserArr = $isSendItemSigArr = [];
            if(in_array($return_type,[0,1,3])) {
                //获取项目下的角色信息
                $itemRoleWhere = [
                    'is_del' => 0,
                    'item_id' => array_values(array_column($sigDatas, 'item_id')),
                ];
                if(in_array($return_type,[0,1])){
                    $itemRoleWhere['code'] = ["cra","crc"];
                }
                $itemRoleArr = $this->LocalService()->realRole->fetchCol('id',$itemRoleWhere);
                unset($itemRoleWhere);
                if (!empty($itemRoleArr)) {
                    $sigUserRoleDatas = $this->LocalService()->roleSignatoryRelation->fetchAll([
                        'where' => [
                            'is_del' => 0,
                            new Operator('signatory_user', Operator::OP_GT, 0),
                            new Operator('status', Operator::OP_NE, 1),
                            'item_sign_id' => array_values(array_column($sigDatas, 'id')),
                            'role_id' => array_values($itemRoleArr),
                        ],
                        'columns' => ['item_sign_id', 'signatory_user']
                    ]);
                    if (!empty($sigUserRoleDatas)) {
                        $sigUserArr = $sigUserIdArr = [];
                        foreach ($sigUserRoleDatas as $sigUserRoleKey => $sigUserRoleData) {
                            if (!in_array($sigUserRoleData['signatory_user'], $sigUserArr[$sigUserRoleData['item_sign_id']])) {
                                $sigUserArr[$sigUserRoleData['item_sign_id']][] = $sigUserRoleData['signatory_user'];
                            }
                            if (!in_array($sigUserRoleData['signatory_user'], $sigUserIdArr)) {
                                $sigUserIdArr[] = $sigUserRoleData['signatory_user'];
                            }
                            unset($sigUserRoleDatas[$sigUserRoleKey]);
                        }
                        //获取中心人员对应的AdminUser信息
                        $sigUserAdminData = [];
                        if(!empty($sigUserIdArr)){
                            $sigUserAdminData = $this->LocalService()->signatoryUser->isShowMaskingField(true)->fetchUserAdminData(['id' => $sigUserIdArr]);
                        }
                        unset($sigUserIdArr);
                        //获取中心下的AdminUser人员信息
                        if(!empty($sigUserArr)){
                            foreach ($sigUserArr as $sigUserKey => $sigUsers) {
                                foreach ($sigUsers as $sigUserId) {
                                    if (isset($sigUserAdminData[$sigUserId]) && isset($sigUserAdminData[$sigUserId]['openid']) && !empty($sigUserAdminData[$sigUserId]['openid'])) {
                                            $sigAdminUserArr[$sigUserKey][] = [
                                                'user_id' => $sigUserAdminData[$sigUserId]['admin_user'],
                                                'openid' => $sigUserAdminData[$sigUserId]['openid']
                                            ];
                                    }
                                }
                                unset($sigUserArr[$sigUserKey]);
                            }
                        }
                        unset($sigUserArr);
                        unset($sigUserAdminData);
                    }
                    unset($sigUserRoleDatas);
                }
                unset($itemRoleArr);

                //获取开通发信提醒的项目中心
                $msgsetDatas = $this->LocalService()->projectMsgset->fetchAll([
                    'where'=> [
                        'is_del' => 0,
                        'is_send' => 1,
                        'send_type' => 0,
                        'itemsig_id' => array_values(array_column($sigDatas,'id')),
                    ],
                    'columns'=>['item_id','itemsig_id']
                ]);
                if(!empty($msgsetDatas)){
                    foreach($msgsetDatas as $msgsetKey=>$msgsetData){
                        $isSendItemSigArr[] = $msgsetData['item_id'].'_'.$msgsetData['itemsig_id'];
                        unset($msgsetDatas[$msgsetKey]);
                    }
                }
                unset($msgsetDatas);
            }

            $send_user_open_arr = [];
            foreach($sigDatas as $sigKey=>$sigData){
                $sigData_id = $sigData['id'];
                $sendItemDatas = [];//发信项目
                //获取该中心 当日预约的人员数
                if(isset($sigNowMakeData[$sigData_id]) && !empty($sigNowMakeData[$sigData_id])){
                    foreach($sigNowMakeData[$sigData_id] as $sigNowMakeItem=>$sigNowMake){
                        if(isset($itemDatas[$sigNowMakeItem]) && !empty($itemDatas[$sigNowMakeItem]) && !empty($sigNowMake)){
                            $sendItemDatas[$sigNowMakeItem]['item_id'] = $sigNowMakeItem;
                            $sendItemDatas[$sigNowMakeItem]['item_name'] = $itemDatas[$sigNowMakeItem]['number'] != '' ? '【'.$itemDatas[$sigNowMakeItem]['number'].'】' : '';
                            if($itemDatas[$sigNowMakeItem]['var_name'] != ''){
                                $sendItemDatas[$sigNowMakeItem]['item_name'] .= $itemDatas[$sigNowMakeItem]['var_name'];
                            }else{
                                $sendItemDatas[$sigNowMakeItem]['item_name'] .= $itemDatas[$sigNowMakeItem]['name'];
                            }
                            $sendItemDatas[$sigNowMakeItem]['now_make_count'] = count($sigNowMake);
                        }
                    }
                    unset($sigNowMakeData[$sigData_id]);
                }

                //获取该中心 已进窗口期但未预约的人员数
                if(isset($sigNoMakeData[$sigData_id]) && !empty($sigNoMakeData[$sigData_id])){
                    foreach($sigNoMakeData[$sigData_id] as $sigNoMakeItem=>$sigNoMake){
                        if(isset($itemDatas[$sigNoMakeItem]) && !empty($itemDatas[$sigNoMakeItem]) && !empty($sigNoMake)){
                            $sendItemDatas[$sigNoMakeItem]['item_id'] = $sigNoMakeItem;
                            $sendItemDatas[$sigNoMakeItem]['item_name'] = $itemDatas[$sigNoMakeItem]['number'] != '' ? '【'.$itemDatas[$sigNoMakeItem]['number'].'】' : '';
                            if($itemDatas[$sigNoMakeItem]['var_name'] != ''){
                                $sendItemDatas[$sigNoMakeItem]['item_name'] .= $itemDatas[$sigNoMakeItem]['var_name'];
                            }else{
                                $sendItemDatas[$sigNoMakeItem]['item_name'] .= $itemDatas[$sigNoMakeItem]['name'];
                            }
                            $sendItemDatas[$sigNoMakeItem]['no_make_count'] = count($sigNoMake);
                        }
                    }
                    unset($sigNoMakeData[$sigData_id]);
                }

                //获取该中心 待处理的工作计划数
                if(isset($sigWorkCount[$sigData_id]) && !empty($sigWorkCount[$sigData_id])){
                    foreach($sigWorkCount[$sigData_id] as $sigWorkItem=>$sigWork_Count){
                        if(isset($itemDatas[$sigWorkItem]) && !empty($itemDatas[$sigWorkItem]) && !empty($sigWork_Count)){
                            $sendItemDatas[$sigWorkItem]['item_id'] = $sigWorkItem;
                            $sendItemDatas[$sigWorkItem]['item_name'] = $itemDatas[$sigWorkItem]['number'] != '' ? '【'.$itemDatas[$sigWorkItem]['number'].'】' : '';
                            if($itemDatas[$sigWorkItem]['var_name'] != ''){
                                $sendItemDatas[$sigWorkItem]['item_name'] .= $itemDatas[$sigWorkItem]['var_name'];
                            }else{
                                $sendItemDatas[$sigWorkItem]['item_name'] .= $itemDatas[$sigWorkItem]['name'];
                            }
                            $sendItemDatas[$sigWorkItem]['work_count'] = $sigWork_Count;
                        }
                    }
                    unset($sigWorkCount[$sigData_id]);
                }

                //获取该中心 待处理的ESD质疑数
                if(isset($sigEsdQuestionCount[$sigData_id]) && !empty($sigEsdQuestionCount[$sigData_id])){
                    foreach($sigEsdQuestionCount[$sigData_id] as $sigEsdQuestionItem=>$sigEsdQuestion_Count){
                        if(isset($itemDatas[$sigEsdQuestionItem]) && !empty($itemDatas[$sigEsdQuestionItem]) && !empty($sigEsdQuestion_Count)){
                            $sendItemDatas[$sigEsdQuestionItem]['item_id'] = $sigEsdQuestionItem;
                            $sendItemDatas[$sigEsdQuestionItem]['item_name'] = $itemDatas[$sigEsdQuestionItem]['number'] != '' ? '【'.$itemDatas[$sigEsdQuestionItem]['number'].'】' : '';
                            if($itemDatas[$sigEsdQuestionItem]['var_name'] != ''){
                                $sendItemDatas[$sigEsdQuestionItem]['item_name'] .= $itemDatas[$sigEsdQuestionItem]['var_name'];
                            }else{
                                $sendItemDatas[$sigEsdQuestionItem]['item_name'] .= $itemDatas[$sigEsdQuestionItem]['name'];
                            }
                            $sendItemDatas[$sigEsdQuestionItem]['esd_question_count'] = $sigEsdQuestion_Count;
                        }
                    }
                    unset($sigEsdQuestionCount[$sigData_id]);
                }

                //获取该中心 待处理的EDC质疑数
                if(isset($sigEdcQuestionCount[$sigData_id]) && !empty($sigEdcQuestionCount[$sigData_id])){
                    foreach($sigEdcQuestionCount[$sigData_id] as $sigEdcQuestionItem=>$sigEdcQuestion_Count){
                        if(isset($itemDatas[$sigEdcQuestionItem]) && !empty($itemDatas[$sigEdcQuestionItem]) && !empty($sigEdcQuestion_Count)){
                            $sendItemDatas[$sigEdcQuestionItem]['item_id'] = $sigEdcQuestionItem;
                            $sendItemDatas[$sigEdcQuestionItem]['item_name'] = $itemDatas[$sigEdcQuestionItem]['number'] != '' ? '【'.$itemDatas[$sigEdcQuestionItem]['number'].'】' : '';
                            if($itemDatas[$sigEdcQuestionItem]['var_name'] != ''){
                                $sendItemDatas[$sigEdcQuestionItem]['item_name'] .= $itemDatas[$sigEdcQuestionItem]['var_name'];
                            }else{
                                $sendItemDatas[$sigEdcQuestionItem]['item_name'] .= $itemDatas[$sigEdcQuestionItem]['name'];
                            }
                            $sendItemDatas[$sigEdcQuestionItem]['edc_question_count'] = $sigEdcQuestion_Count;
                        }
                    }
                    unset($sigEdcQuestionCount[$sigData_id]);
                }

                unset($sigDatas[$sigKey]);

                if(!empty($sendItemDatas)){
                    $sigData_name = $sigData['number'] != '' ? '【'.$sigData['number'].'】' : '';
                    if(isset($sigNameDatas[$sigData['signatory_id']]) && $sigNameDatas[$sigData['signatory_id']] != ''){
                        $sigData_name .= $sigNameDatas[$sigData['signatory_id']];
                    }
                    foreach($sendItemDatas as $sendItemKey=>$sendItemData){
                        $send_count = 0;//总数
                        //该中心该项目当日预约人员
                        $itemNowMakeCount = isset($sendItemData['now_make_count']) && !empty($sendItemData['now_make_count']) ? $sendItemData['now_make_count'] : 0;
                        $send_count += $itemNowMakeCount;

                        //该中心该项目当日已进窗口期但未预约的人员数
                        $itemNoMakeyCount = isset($sendItemData['no_make_count']) && !empty($sendItemData['no_make_count']) ? $sendItemData['no_make_count'] : 0;
                        $send_count += $itemNoMakeyCount;

                        //该中心该项目待处理的工作计划数
                        $itemWorkCount = isset($sendItemData['work_count']) && !empty($sendItemData['work_count']) ? $sendItemData['work_count'] : 0;
                        $send_count += $itemWorkCount;

                        //该中心该项目待处理的ESD质疑数
                        $itemEsdQuestionCount = isset($sendItemData['esd_question_count']) && !empty($sendItemData['esd_question_count']) ? $sendItemData['esd_question_count'] : 0;
                        $send_count += $itemEsdQuestionCount;

                        //该中心该项目待处理的EDC质疑数
                        $itemEdcQuestionCount = isset($sendItemData['edc_question_count']) && !empty($sendItemData['edc_question_count']) ? $sendItemData['edc_question_count'] : 0;
                        $send_count += $itemEdcQuestionCount;

                        $is_itemsig_send = 0;//判断是否已开启发信提醒 [0 关闭 1 开启]
                        $is_send_key = $sendItemData['item_id'].'_'.$sigData_id;
                        if(!empty($sendItemData['item_id']) && !empty($sigData_id) && !empty($isSendItemSigArr) && in_array($is_send_key,$isSendItemSigArr)){
                            $is_itemsig_send = 1;
                        }
                        unset($is_send_key);

                        //返回数据
                        if(in_array($return_type,[0,1,3])){
                            if(isset($sigAdminUserArr[$sigData_id]) && !empty($sigAdminUserArr[$sigData_id])){
                                foreach($sigAdminUserArr[$sigData_id] as $sigUserInfo){
                                    if($return_type == 0 && $is_itemsig_send == 1){
                                        //发信的openid
                                        $send[$sigUserInfo['openid']] = isset($send[$sigUserInfo['openid']]) && !empty($send[$sigUserInfo['openid']]) ? $send[$sigUserInfo['openid']] : 0;
                                        $send[$sigUserInfo['openid']] += $send_count;

                                        //发信信息存储
                                        $save_send_detail[$sigUserInfo['user_id']][$sendItemData['item_id']][$sigData_id] = [
                                            'item_id'=>$sendItemData['item_id'],
                                            'item_name'=>$sendItemData['item_name'],
                                            'itemsig_id'=>$sigData_id,
                                            'itemsig_name'=>$sigData_name,
                                            'day_make_count'=>$itemNowMakeCount,
                                            'no_make_count'=>$itemNoMakeyCount,
                                            'work_count'=>$itemWorkCount,
                                            'esd_question_count'=>$itemEsdQuestionCount,
                                            'edc_question_count'=>$itemEdcQuestionCount
                                        ];
                                        $send_user_open_arr[$sigUserInfo['user_id']] = $sigUserInfo['openid'];
                                    }
                                    if($return_type == 3 && !in_array($sigUserInfo['user_id'],$send[$sigUserInfo['user_id']])){
                                        $send[$sigUserInfo['user_id']][$sendItemData['item_id']][$sigData_id] = [
                                            'item_id'=>$sendItemData['item_id'],
                                            'item_name'=>$sendItemData['item_name'],
                                            'itemsig_id'=>$sigData_id,
                                            'itemsig_name'=>$sigData_name,
                                            'day_make_count'=>$itemNowMakeCount,
                                            'no_make_count'=>$itemNoMakeyCount,
                                            'work_count'=>$itemWorkCount,
                                            'esd_question_count'=>$itemEsdQuestionCount,
                                            'edc_question_count'=>$itemEdcQuestionCount
                                        ];
                                    }
                                    if($return_type == 1 && $is_itemsig_send == 1 && !in_array($sigUserInfo['user_id'],$send[$sigUserInfo['user_id']])){
                                        $send[$sigUserInfo['user_id']][$sendItemData['item_id']][$sigData_id] = [
                                            'item_id'=>$sendItemData['item_id'],
                                            'item_name'=>$sendItemData['item_name'],
                                            'itemsig_id'=>$sigData_id,
                                            'itemsig_name'=>$sigData_name,
                                            'day_make_count'=>$itemNowMakeCount,
                                            'no_make_count'=>$itemNoMakeyCount,
                                            'work_count'=>$itemWorkCount,
                                            'esd_question_count'=>$itemEsdQuestionCount,
                                            'edc_question_count'=>$itemEdcQuestionCount
                                        ];
                                    }
                                }
                            }
                        }

                        //返回项目中心数量
                        if(in_array($return_type,[2]) && $is_itemsig_send == 1){
                            $send[$sendItemData['item_id']][$sigData_id] = [
                                'item_id'=>$sendItemData['item_id'],
                                'item_name'=>$sendItemData['item_name'],
                                'itemsig_id'=>$sigData_id,
                                'itemsig_name'=>$sigData_name,
                                'day_make_count'=>$itemNowMakeCount,
                                'no_make_count'=>$itemNoMakeyCount,
                                'work_count'=>$itemWorkCount,
                                'esd_question_count'=>$itemEsdQuestionCount,
                                'edc_question_count'=>$itemEdcQuestionCount
                            ];
                        }

                        //释放空间
                        unset($itemNowMakeyCount);
                        unset($itemNoMakeyCount);
                        unset($itemWorkCount);
                        unset($itemEsdQuestionCount);
                        unset($itemEdcQuestionCount);
                        unset($send_mes);
                        unset($sendItemDatas[$sendItemKey]);
                    }
                }
                unset($sendItemDatas);
                unset($sigData_id);
                unset($sigData_name);
            }
            unset($sigNameDatas);
            unset($sigAdminUserArr);
            unset($isSendItemSigArr);

            //重组每个提醒人员的提醒项目及中心
            if($return_type == 0){
                if(!empty($save_send_detail)){
                    foreach($save_send_detail as $save_send_detail_user=>$save_send_detailarr){
                        $save_send_detail_user_open = isset($send_user_open_arr[$save_send_detail_user]) && !empty($send_user_open_arr[$save_send_detail_user]) ? $send_user_open_arr[$save_send_detail_user] : '';
                        $save_send_user[$save_send_detail_user] = [
                            'info_count'=>$save_send_detail_user_open != '' && isset($send[$save_send_detail_user_open]) && !empty($send[$save_send_detail_user_open]) ? $send[$save_send_detail_user_open] :0,
                            'item_sig_info'=>[]
                        ];
                        foreach($save_send_detailarr as $save_send_detail_item=>$save_send_detailitemsigs){
                            $save_send_user[$save_send_detail_user]['item_sig_info'][$save_send_detail_item] = isset($save_send_user[$save_send_detail_user]['item_sig_info'][$save_send_detail_item]) && !empty($save_send_user[$save_send_detail_user]['item_sig_info'][$save_send_detail_item]) ? $save_send_user[$save_send_detail_user]['item_sig_info'][$save_send_detail_item] : [];
                            foreach($save_send_detailitemsigs as $save_send_detail_sig=>$save_send_detailsigs){
                                if(!in_array($save_send_detail_sig,$save_send_user[$save_send_detail_user]['item_sig_info'][$save_send_detail_item])){
                                    $save_send_user[$save_send_detail_user]['item_sig_info'][$save_send_detail_item][] = $save_send_detail_sig;
                                }
                            }
                        }
                    }
                }
            }
            unset($send_user_open_arr);
        }
        //释放空间
        unset($itemDatas);
        unset($sigDatas);
        unset($sigNowMakeData);
        unset($sigNoMakeData);
        unset($sigWorkCount);
        unset($sigEsdQuestionCount);
        unset($sigEdcQuestionCount);
        unset($send_key);

        //保存发信信息
        if($return_type == 0 && (!empty($save_send_detail) || !empty($save_send_user))){
            //保存发信人员信息
            if(!empty($save_send_user)) {
                $this->LocalService()->swTaskClient->send([
                    'svName' => 'projectMsgsend',
                    'methodName' => 'saveSendMesHandleData',
                    'params' => [
                        'now_date' => $now_date,
                        'data' => $save_send_user
                    ]
                ]);
            }

            //保存发信明细
            if(!empty($save_send_detail)){
                $this->LocalService()->swTaskClient->send([
                    'svName' => 'projectMsginfo',
                    'methodName' => 'saveSendMesHandleData',
                    'params' => [
                        'now_date'=>$now_date,
                        'data'=>$save_send_detail
                    ]
                ]);
            }
        }
        unset($save_send_user);
        unset($save_send_detail);

        return $send;
    }

    /***
     * Notes: 待处理工单消息提醒
     * User: lltyy
     * DateTime: 2024/12/16 9:10
     *
     */
    public function sendWorkStatisticMsgTip() {
        $msgList = $this->getSendMesHandleData();

        if(empty($msgList)) return;

        foreach($msgList as $openid => $msgCount) {
            $domain = 'ESD项目管理平台';
            $this->LocalService()->wechat->getApp()->template_message->send([
                'touser' => $openid,
                'template_id' => '_SSL9LHTkBWv_e_WAVdmUQ_tf0WU700G9EMWRKnuUxM',
                'url' => $this->LocalService()->config['sendUrl'] . "/#/webH5/workStatistics?date=" . time(),
                'data' => [
                    'thing17' => $domain,
                    'number29' => $msgCount,
                    'time66' => date("Y-m-d H:i:s"),
                ]
            ]);
        }
    }

    /**
     * Notes: 中心人员发信进度通知【入组分析（日进度、周进度、月进度）】
     * User: lltyy
     * DateTime: 2024/12/11 11:41
     *
     * @param array $info
     * @return array
     */
    public function getSendMesGroupData(array $info = []){
        $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
        $return_type = isset($info['return_type']) && !empty($info['return_type']) ? $info['return_type'] : 0;//返回类型 0 需要发通知的人员openid

        //获取需要发信的信息
        $send = [];
        $where = [
            'is_del' => 0,
            new Operator('signatory_id', Operator::OP_GT, 0),
        ];
        if(!empty($item_id)){
            $parent_item = $this->LocalService()->itemInfo->getOneFieldVal('fid', [
                'is_del' => 0,
                'is_mainitem' => 0,
                'id' => $item_id,
            ]);
            $where['item_id'] = $parent_item;
        }
        if(!empty($itemsig_id)){
            $where['id'] = $itemsig_id;
        }
        $sigDatas = $this->fetchAll([
            'where'=>$where,
            'columns'=>['id','item_id'],
            'order'=>['item_id','order']
        ]);
        unset($where);
        if(!empty($sigDatas)){
            //获取中心角色人员信息以及开通发信提醒的项目中心
            $sigAdminUserArr = $isSendItemSigArr = [];
            if(in_array($return_type,[0])) {
                //获取项目下的角色信息
                $itemRoleWhere = [
                    'is_del' => 0,
                    'item_id' => array_values(array_column($sigDatas, 'item_id'))
                ];
                $itemRoleArr = $this->LocalService()->realRole->fetchCol('id',$itemRoleWhere);
                unset($itemRoleWhere);
                if (!empty($itemRoleArr)) {
                    $sigUserRoleDatas = $this->LocalService()->roleSignatoryRelation->fetchAll([
                        'where' => [
                            'is_del' => 0,
                            'item_sign_id' => array_values(array_column($sigDatas, 'id')),
                            'role_id' => array_values($itemRoleArr),
                            new Operator('signatory_user', Operator::OP_GT, 0),
                            new Operator('status', Operator::OP_NE, 1),
                        ],
                        'columns' => ['item_sign_id', 'signatory_user']
                    ]);
                    if (!empty($sigUserRoleDatas)) {
                        $sigUserArr = $sigUserIdArr = [];
                        foreach ($sigUserRoleDatas as $sigUserRoleKey => $sigUserRoleData) {
                            if (!in_array($sigUserRoleData['signatory_user'], $sigUserArr[$sigUserRoleData['item_sign_id']] ?? [])) {
                                $sigUserArr[$sigUserRoleData['item_sign_id']][] = $sigUserRoleData['signatory_user'];
                            }
                            if (!in_array($sigUserRoleData['signatory_user'], $sigUserIdArr)) {
                                $sigUserIdArr[] = $sigUserRoleData['signatory_user'];
                            }
                            unset($sigUserRoleDatas[$sigUserRoleKey]);
                        }
                        //获取中心人员对应的AdminUser信息
                        $sigUserAdminData = [];
                        if(!empty($sigUserIdArr)){
                            $sigUserAdminData = $this->LocalService()->signatoryUser->fetchUserAdminData(['id' => $sigUserIdArr]);
                        }
                        unset($sigUserIdArr);
                        //获取中心下的AdminUser人员信息
                        if(!empty($sigUserArr)){
                            foreach ($sigUserArr as $sigUserKey => $sigUsers) {
                                foreach ($sigUsers as $sigUserId) {
                                    if (isset($sigUserAdminData[$sigUserId]) && isset($sigUserAdminData[$sigUserId]['openid']) && !empty($sigUserAdminData[$sigUserId]['openid'])) {
                                            $sigAdminUserArr[$sigUserKey][] = [
                                                'user_id' => $sigUserAdminData[$sigUserId]['admin_user'],
                                                'openid' => $sigUserAdminData[$sigUserId]['openid']
                                            ];
                                    }
                                }
                                unset($sigUserArr[$sigUserKey]);
                            }
                        }
                        unset($sigUserArr);
                        unset($sigUserAdminData);
                    }
                    unset($sigUserRoleDatas);
                }
                unset($itemRoleArr);

                //获取开通发信提醒的项目中心
                $msgsetDatas = $this->LocalService()->projectMsgset->fetchAll([
                    'where'=> [
                        'is_del' => 0,
                        'is_send' => 1,
                        'send_type' => 1,
                        'itemsig_id' => array_values(array_column($sigDatas,'id')),
                    ],
                    'columns'=>['item_id','itemsig_id']
                ]);
                if(!empty($msgsetDatas)){
                    foreach($msgsetDatas as $msgsetKey=>$msgsetData){
                        $isSendItemSigArr[] = $msgsetData['item_id'].'_'.$msgsetData['itemsig_id'];
                        unset($msgsetDatas[$msgsetKey]);
                    }
                }
                unset($msgsetDatas);
            }

            //获取副项目信息
            $itemDatas = $this->LocalService()->itemInfo->fetchDataByKey([
                'is_del' => 0,
                'status' => 0,
                'fid' => array_values(array_column($sigDatas,'item_id')),
            ],['id','fid'],['fid','item_order'],['fid'=>1]);
            $itemDatas = isset($itemDatas['fid']) && !empty($itemDatas['fid']) ? $itemDatas['fid'] : [];
            foreach($sigDatas as $sigKey=>$sigData){
                $sigData_id = $sigData['id'];
                //判断该中心下是否存在需要提醒的项目
                $is_itemsig_send = 0;
                if(isset($itemDatas[$sigData['item_id']]) && !empty($itemDatas[$sigData['item_id']])){
                    foreach($itemDatas[$sigData['item_id']] as $itemData){
                        $is_send_key = $itemData['id'].'_'.$sigData_id;
                        if(!empty($isSendItemSigArr) && in_array($is_send_key,$isSendItemSigArr)){
                            $is_itemsig_send = 1;
                        }
                        unset($is_send_key);
                    }
                }

                //如果存在需要提醒的项目 则记录发信人员openid
                if($is_itemsig_send == 1 && isset($sigAdminUserArr[$sigData_id]) && !empty($sigAdminUserArr[$sigData_id])){
                    foreach($sigAdminUserArr[$sigData_id] as $sigUserInfo){
                        //发信的openid
                        if($sigUserInfo['openid'] != '' && !in_array($sigUserInfo['openid'],$send)){
                            $send[] = $sigUserInfo['openid'];
                        }
                    }
                }
                unset($sigData_id);
                unset($sigDatas[$sigKey]);
            }
            unset($sigAdminUserArr);
            unset($isSendItemSigArr);

        }
        //释放空间
        unset($sigDatas);
        return $send;
    }

    /***
     * Notes: 入组进度通知提醒
     * User: lltyy
     * DateTime: 2024/12/16 9:10
     *
     */
    public function sendPatientSummaryMsgTip() {
        $msgList = $this->getSendMesGroupData();
        if(empty($msgList)) return;

        foreach($msgList as $openid) {
            $domain = '【ESD项目管理平台】项目进度分析';
            $this->LocalService()->wechat->getApp()->template_message->send([
                'touser' => $openid,
                'template_id' => 'yTdivSYvH8Ufkvxja_BrxbS_mko6c78k_n_erfBhBQ4',
                'url' => $this->LocalService()->config['sendUrl'] . "/#/webH5/patientSummaryItem?date=" . time(),
                'data' => [
                    'thing13' => $domain,
                    'time1' => date("Y-m-d H:i:s"),
                ]
            ]);
        }
    }

    public function itemSignatoryAdminUserName(int $item_id): array
    {
        $itemSignatoryUserIdList = $this->fetchCol('signatory_id', [
            'item_id' => $item_id
        ]);

        $adminUserIdList = $this->LocalService()->signatoryUser->fetchCol('admin_user_id', [
            'id' => $itemSignatoryUserIdList
        ]);

        $adminUserWhere = ['status' => 0];
        if(!empty($adminUserIdList)) {
            $adminUserWhere['or'] = [
                'id' => $adminUserIdList
            ];
        }

        return $this->LocalService()->adminUser->fetchAll([
            'where' => $adminUserWhere,
            'columns' => ['value' => 'realname', 'label' => 'realname']
        ]);
    }

}