<?php
namespace Application\Service\DB\Admin;

use Application\Service\Extension\Laminas;
use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\NotIn;
use Laminas\Db\Sql\Where;
use Laminas\Db\Sql\Predicate\Operator;
use Laminas\Stdlib\ArrayUtils;

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

    public function getAdminRealRoleData($whereData=[],$type = 0,$field=''){
        $data = [];
        switch ($type){
            case 1:
                $data = $this->LocalService()->realRole->fetchAll($whereData);
                break;
            case 2:
                $data = $this->LocalService()->realRole->fetchOne($whereData);
                break;
            case 3:
                $data = $this->LocalService()->realRole->fetchCol($field,$whereData);
                break;
            default:
                $data['data'] = $this->LocalService()->realRole->fetchAll($whereData);
                $data['count'] = $this->LocalService()->realRole->getCount($whereData['where']);
                break;
        }
        return $data ;
    }


    /** 添加中心角色和人员的关系
     * Notes:
     * User: llbjj
     * DateTime: 2022/9/6 15:22
     *
     * @param int $item_id
     * @param int $itemsig_id
     * @param int $role_id
     * @param array $signatoryUserData
     */
    public function mapToSiguser(int $item_id, int $itemsig_id, int $role_id, array &$signatoryUserData) {
        $signatoryUserData = array_filter($signatoryUserData);
        $relationWhere = [
            'is_del' => 0,
            'status' => 0,
            'item_id' => $item_id,
            'item_sign_id' => $itemsig_id,
            'role_id' => $role_id,
        ];
        $relationCount = $this->LocalService()->roleSignatoryRelation->getCount($relationWhere);
        if(!count($signatoryUserData)) {
            if ($relationCount) {
                $this->LocalService()->roleSignatoryRelation->update(['status' => 1], $relationWhere);
            }
        } else {
            // 获取本次提交人之外的人员信息
            $disabledRelationDatas = $this->LocalService()->roleSignatoryRelation->fetchCol('signatory_user', ArrayUtils::merge($relationWhere, [
                'is_del' => 0,
                new NotIn('signatory_user', $signatoryUserData),
            ]));


            // 删除本次提交之外的人员
            $this->LocalService()->roleSignatoryRelation->update(['status' => 1], ArrayUtils::merge($relationWhere,
                [
                    'is_del' => 0,
                    new NotIn('signatory_user', $signatoryUserData),
                ]
            ));

            // 获取本角色下的所有的人员（包含已删除）
            $preUserData = $this->LocalService()->roleSignatoryRelation->fetchCol('signatory_user', [
                'is_del' => 0,
                'item_id' => $item_id,
                'item_sign_id' => $itemsig_id,
                'role_id' => $role_id,
            ]);

            //查询手机号和userid对应的数组
            /*$userInfoArr = $this->LocalService()->adminUser->fetchAll([
                'where' => 'is_del = 0',
                'columns' => ['id','mobile'],
            ]);
            $userTelArr = array_column($userInfoArr,null,'mobile');

            //获取yikeen_signatory_user的id和tel对应数组
            $signatory_users = $this->LocalService()->signatoryUser->fetchCol('user_tel','is_del = 0');*/
            $signatory_admin_user_arr = $this->LocalService()->signatoryUser->fetchCol('admin_user_id','is_del = 0');
            foreach($signatoryUserData as $signatory_user_id) {
                $user_id = isset($signatory_admin_user_arr[$signatory_user_id]) && !empty($signatory_admin_user_arr[$signatory_user_id]) ? $signatory_admin_user_arr[$signatory_user_id] : 0;
                //获取到用户手机号
                /*$user_tel = isset($signatory_users[$signatory_user_id]) && !empty($signatory_users[$signatory_user_id]) ? $signatory_users[$signatory_user_id] : '';
                if(!empty($user_tel)){
                    //根据手机号查询adminuser表的用户ID
                    $user_id = isset($userTelArr[$user_tel]['id']) && !empty($userTelArr[$user_tel]['id']) ? $userTelArr[$user_tel]['id'] : 0;
                }*/

                $relationData = [
                    'item_id' => $item_id,
                    'item_sign_id' => $itemsig_id,
                    'role_id' => $role_id,
                    'signatory_user' => $signatory_user_id,
                    'admin_user_id' => $user_id,
                    'status' => 0
                ];
                if(in_array($signatory_user_id, $preUserData)) $relationData['id'] = array_keys($preUserData, $signatory_user_id)[0];

                $id = $this->LocalService()->roleSignatoryRelation->save($relationData);

                // 同步企业微信
                if($this->LocalService()->config['isOpenWechatWork']) $this->LocalService()->workUser->syncUserToWork($item_id, $itemsig_id, $signatory_user_id);
            }
            unset($signatory_users);
            unset($userTelArr);
            if(!empty($disabledRelationDatas) && $this->LocalService()->config['isOpenWechatWork']) {
                foreach ($disabledRelationDatas as $signatory_user_id) {
                    $this->LocalService()->workUser->syncUserToWork($item_id, $itemsig_id, $signatory_user_id);
                }
            }
        }
    }

    /**
     * Notes:获取角色信息，ID为键值，名称+code值为值
     * User: lltyy
     * DateTime: 2022/11/1 15:02
     *
     * @param $where
     * @return array
     */
    public function fetchNameAndCodeById($where){
        $arr = [];
        $cond = [
            'where'=>$where,
            'columns'=>['id','role_name'],
            'order'=>['sort asc']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach($datas as $data){
                $data_id = $data['id'];
                $arr[$data_id] = $data['role_name'];
            }
        }
        return $arr;
    }
    /**
     * 项目传输后给固定角色设置默认的权限
     * param $itemId 项目id 默认为大项目id
     * param $type 项目id类型：0为大项目id，1为小项目id
     */
    public function itemRealRelation($itemId,$type=0){
        //小项目id找大项目id
        !empty($type) && $itemId = $this->LocalService()->itemInfo->getOneFieldVal('fid',['is_del'=>0,'id'=>$itemId]);

        $realRolemodulerelationInfo = $this->LocalService()->realRolemodulerelationEdc->fetchOne(['where'=>['status'=>0,'item_id'=>$itemId]]);
        if(!empty($realRolemodulerelationInfo)){
            return false;
            echo '已存在角色权限，不再同步';die;
            return false;
        }

        //获取EDC菜单
        $whereMArr['where'] = [
            'is_del' => 0,
            'menu_type' => 1,
            new Operator('parent_id', Operator::OP_GT, 0)
        ];
        $whereMArr['order'] = 'menu_order';
        $menuLists = $this->LocalService()->adminMenuEdc->getAdminMenuData($whereMArr,1);

        //获取项目权限
        $relations = '';
        $realRoleData = $this->LocalService()->realRole->fetchAll(['where' =>['is_del'=>0,'item_id'=>$itemId,'code'=>['yjys','yjzl','sbz','yx','pm','cra','crc','dm']]]);
        if  (!empty($realRoleData)) {
            foreach($realRoleData as $realRoleDataK=>$realRoleDataV){
                if (!empty($menuLists)){
                    $role_code = $realRoleDataV['code'];
                    foreach ($menuLists as $menuList) {
                        $role_power_arr = !is_null($menuList['role_code_power']) && !empty($menuList['role_code_power']) ? json_decode($menuList['role_code_power'],true) : [];
                        $role_power = isset($role_power_arr[$role_code]) && !empty($role_power_arr[$role_code]) ? intval($role_power_arr[$role_code]) : 0;
                        if(!empty($role_power)){
                            $look_write = 0;
                            if($role_power == 2){
                                $look_write = 1;
                            }
                            $realRolemodulerelationArr = [
                                'role_id'=>$realRoleDataV['id'],
                                'module_id'=>$menuList['id'],
                                'item_id'=>$itemId,
                                'look_write'=>$look_write
                            ];
                            $realRolemodulerelationId = $this->LocalService()->realRolemodulerelationEdc->save($realRolemodulerelationArr);
                            $relations .= $realRolemodulerelationId.',';
                        }
                    }

                }
            }
        }
        return true;

        $count = count($arr = explode(',',$relations))-1;
        echo '成功，关系表id：'.$relations.'总数据：'.$count;die;
        return true;
        return $realRoleId;
    }

    /**
     * 项目传输后给固定角色设置默认的权限
     * param $itemId 项目id 默认为大项目id
     * param $type 项目id类型：0为大项目id，1为小项目id
     */
    public function OLDitemRealRelation($itemId,$type=0){
        $itemRealRoleName = ['研究医生','研究助理','PM','CRA','CRC','DM'];
        $itemRealRoleCode = ['yjys','yjzl','pm','cra','crc','dm'];
        //小项目id找大项目id

        !empty($type) && $itemId = $this->LocalService()->itemInfo->getOneFieldVal('fid',['is_del'=>0,'item_id'=>$itemId]);

        $realRolemodulerelationInfo = $this->LocalService()->realRolemodulerelationEdc->fetchOne(['where'=>['status'=>0,'item_id'=>$itemId]]);
        if(!empty($realRolemodulerelationInfo)){
            return false;
            echo '已存在角色权限，不再同步';die;
            return false;
        }

        $whereMArr['where'] = [
            'is_del' => 0,
            'menu_type' => 1
        ];
        $whereMArr['order'] = 'menu_order';
        $menuList = $this->LocalService()->adminMenuEdc->getAdminMenuData($whereMArr,1);
        //-1没有模块权限，0只读，1可读可写

        //1-real菜单管理-,2-研究中心管理-hospital,3-角色设置-role,4-逻辑核查管理-logical,5-进度管理-progress,6-导出CRF-crf,7-导出SAS-saas
//        $menuNameData = [
//            'yjys'=>['hospital'=>-1,'role'=>-1,'logical'=>-1,'progress'=>-1,'crf'=>-1,'saas'=>-1],
//            'yjzl'=>['hospital'=>-1,'role'=>-1,'logical'=>-1,'progress'=>-1,'crf'=>-1,'saas'=>-1],
//            'pm'=>['hospital'=>-1,'role'=>-1,'logical'=>-1,'progress'=>0,'crf'=>-1,'saas'=>-1],
//            'cra'=>['hospital'=>-1,'role'=>-1,'logical'=>1,'progress'=>1,'crf'=>-1,'saas'=>-1],
//            'crc'=>['hospital'=>-1,'role'=>-1,'logical'=>-1,'progress'=>-1,'crf'=>-1,'saas'=>-1],
//            'dm'=>['hospital'=>-1,'role'=>1,'logical'=>1,'progress'=>1,'crf'=>1,'saas'=>1,'export'=>1],
//        ];
        $menuNameData = [];
        if (!empty($menuList)){
            $menuNameDatas = array_filter(array_column($menuList,'name'));
            //yjys===yjzl==crc
            $craMenuNameData = $pmMenuNameData = $yjysMenuNameData = array_fill_keys($menuNameDatas,-1);
            $dmMenuNameData = array_fill_keys($menuNameDatas,1);
            //dm
            if (!empty($dmMenuNameData) && isset($dmMenuNameData['hospital'])){
                $dmMenuNameData['hospital'] = -1;
            }
            //pm
            if (!empty($pmMenuNameData) && isset($pmMenuNameData['progress'])){
                $pmMenuNameData['progress'] = 0;
            }
            //cra
            if (!empty($craMenuNameData)){
                if (isset($craMenuNameData['logical'])){
                    $craMenuNameData['logical'] = 1;
                }
                if (isset($craMenuNameData['progress'])){
                    $craMenuNameData['progress'] = 1;
                }
            }
            $menuNameData['yjys'] = $menuNameData['yjzl'] = $menuNameData['crc'] = $yjysMenuNameData;
            $menuNameData['dm'] = $dmMenuNameData;
            $menuNameData['pm'] = $pmMenuNameData;
            $menuNameData['cra'] = $craMenuNameData;

        }
        $relations = '';
        $realRoleData = $this->LocalService()->realRole->fetchAll(['where' =>['is_del'=>0,'item_id'=>$itemId,'code'=>['yjys','yjzl','pm','cra','crc','dm']]]);
        if  (!empty($realRoleData)) {
            foreach($realRoleData as $realRoleDataK=>$realRoleDataV){

                if (!empty($menuList)){
                    if(isset($menuNameData[$realRoleDataV['code']])){
                        $realRelationData = $menuNameData[$realRoleDataV['code']];
                        foreach ($menuList as $menuListK => $menuListV) {
                            if(isset($realRelationData[$menuListV['name']]) && $realRelationData[$menuListV['name']] >=0){
                                $realRolemodulerelationArr['look_write'] = $realRelationData[$menuListV['name']];
                                $realRolemodulerelationArr['role_id'] = $realRoleDataV['id'];
                                $realRolemodulerelationArr['module_id'] = $menuListV['id'];
                                $realRolemodulerelationArr['item_id'] = $itemId;
                                $realRolemodulerelationId = $this->LocalService()->realRolemodulerelationEdc->save($realRolemodulerelationArr);
                                $relations .= $realRolemodulerelationId.',';
                            }
                        }
                    }

                }
            }
        }
        return true;

        $count = count($arr = explode(',',$relations))-1;
        echo '成功，关系表id：'.$relations.'总数据：'.$count;die;
        return true;
        return $realRoleId;
    }

    /**
     * Notes:通过userID 查询人员对应的signatory_user_id
     * @param $userid
     * @return
     * @author haojinhua
     * @date 2024-06-06
     */
    public function getUserRole($itemfid_filterConditions = ''){
            $sig_user_arr = $user_role_arr = $siguser_role_arr = $itemsig_arr = [];
            //获取人员和角色对应数组
            //查询人员信息
            $cond = [
                'where'=>'is_del = 0',
                'columns'=>['id','realname'],
                'order'=>['id asc']
            ];
            $user_datas = $this->LocalService()->adminUser->fetchAll($cond);
            if(!empty($user_datas)){
                //查询操作人的角色 1获取用户id对应手机号
                $userPhoneArr = $this->LocalService()->adminUser->isShowMaskingField(true)->fetchCol('mobile', 'is_del = 0');
                //手机号与合作方ID数组
                $phoneSigidArr = $this->LocalService()->signatoryUser->isShowMaskingField(true)->getFiledRelation([], ['id','info_id', 'user_tel'], 'user_tel', 1);
                foreach ($user_datas as $user_data){
                    $user_id = $user_data['id'];
                    //查询用户手机号
                    $user_tel    = isset($userPhoneArr[$user_id]) && !empty($userPhoneArr[$user_id]) ? $userPhoneArr[$user_id] : '';
                    $sig_info_id = isset($phoneSigidArr[$user_tel]) && !empty($phoneSigidArr[$user_tel]) ? $phoneSigidArr[$user_tel]['info_id'] : '';
                    $sig_user_id = isset($phoneSigidArr[$user_tel]) && !empty($phoneSigidArr[$user_tel]) ? $phoneSigidArr[$user_tel]['id'] : '';
                    $sig_user_arr[$user_id] = [
                        'sig_info_id' => $sig_info_id,
                        'sig_user_id' => $sig_user_id
                    ];
                }
            }

            //获取role_id数组
            $role_cond = [
                'where' => !empty($itemfid_filterConditions) ? $itemfid_filterConditions+['status'=>0] :['status'=>0],
                'columns' => ['item_id','role_id','signatory_user','item_sign_id'],
                'order' => ['id'],
            ];
            $role_datas = $this->LocalService()->roleSignatoryRelation->fetchAll($role_cond);
            if(!empty($role_datas)){
                foreach ($role_datas as $role_data){
                    $user_role_arr[$role_data['item_id']][$role_data['signatory_user']][] = $role_data['role_id'];
                    $siguser_role_arr[$role_data['item_id']][$role_data['item_sign_id']][$role_data['signatory_user']][] = $role_data['role_id'];
                }
            }

            //根据项目ID和info_id 查询itemSig_id
            $itemsig_cond = [
                'where' => !empty($itemfid_filterConditions) ? $itemfid_filterConditions : 'id>0',
                'columns' => ['item_id','signatory_id','id'],
                'order' => ['id'],
            ];
            $itemsig_datas = $this->LocalService()->itemSignatory->fetchAll($itemsig_cond);
            if(!empty($itemsig_datas)){
                foreach ($itemsig_datas as $itemsig_data){
                     $itemsig_arr[$itemsig_data['item_id']][$itemsig_data['signatory_id']] = $itemsig_data['id'];
                }
            }

            unset($user_datas);
            unset($role_datas);
            unset($itemsig_datas);
            return[$sig_user_arr,$user_role_arr,$siguser_role_arr,$itemsig_arr];
    }

    /**
     * Notes:获取指定条件下的数据
     * User: lltyy
     * DateTime: 2024/5/28 9:50
     *
     * @param string $where 条件
     * @param string[] $columns 字段
     * @param string[] $order 排序
     * @param string $key 键值
     * @param int $is_more 是否有多笔【1 是 0 否】
     * @return array
     */
    public function fetchDataByKey($where='is_del = 0',$columns=['id'],$order=['id'],$key='id',$is_more=0){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>$order
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoData){
                $data_key = $infoData[$key];
                if($is_more == 1){
                    $datas[$data_key][] = $infoData;
                }else{
                    $datas[$data_key] = $infoData;
                }
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }
    
    /**
     * Notes: 查询角色下的人员数组
     * @param
     * @return
     * @author haojinhua
     * @date 2024-08-21
     */
    public function getRolUser(){
        $signatory_userArr = [];   //角色下的人员ID
        $role_cond = [
            'where' => 'is_del = 0 and status = 0',
            'columns' => ['item_id','role_id','signatory_user'],
            'order' => ['id'],
        ];
        $role_datas = $this->LocalService()->roleSignatoryRelation->fetchAll($role_cond);
        if(!empty($role_datas)){
            foreach ($role_datas as $role_data){
                if(!empty($role_data['signatory_user'])){
                    $signatory_userArr[$role_data['item_id']][$role_data['role_id']][] = $role_data['signatory_user'];
                }
            }
        }

        return $signatory_userArr;

    }

    public function getRole($item_id=0,$itemsig_id=0)
    {
        $platForm = Laminas::$serviceManager->identity->getPlatform();
        $userId = Laminas::$serviceManager->identity->getId();
        $role = '';
        if($platForm <> 'PC'){
            if($platForm == 'MINI_PATIENT' || $platForm == 'H5_PATIENT')  return 'Patient';
            if($item_id == 0 || $itemsig_id == 0) return '';
            //记录Real端角色
            //获取用户合作方id
            $fid = $this->LocalService()->itemInfo->getOneFieldVal('fid',['id'=>$item_id]);
            $userMobile = $this->LocalService()->adminUser->getOneFieldVal('mobile',['id'=>$userId]);
            $signUserId = $this->LocalService()->signatoryUser->getOneFieldVal('id',['user_tel'=>$userMobile]);
            if(!$signUserId) return '';
            $signRole = $this->LocalService()->roleSignatoryRelation->fetchCol('role_id',['signatory_user'=>$signUserId,'status'=>0,'is_del'=>0,'item_id'=>$fid,'item_sign_id'=>$itemsig_id]);
            if($signRole){
                $realRoleWhere = new Where();
                $realRoleWhere->equalTo('status',0);
                $realRoleWhere->in('id',$signRole);
                $roleName = $this->LocalService()->realRole->fetchCol('role_name',$realRoleWhere);
                $role = implode(',',$roleName);
            }
            return $role;
        }else{
            //记录PC端角色
            $adminRole = $this->LocalService()->userRoleRelation->fetchCol('role_id',['user_id'=>$userId,'status'=>0]);
            if(!empty($adminRole)){
                $adminRoleWhere = new Where();
                $adminRoleWhere->equalTo('status',0);
                $adminRoleWhere->in('id',$adminRole);
                $roleName = $this->LocalService()->adminRole->fetchCol('role_name',$adminRoleWhere);
                $role = implode(',',$roleName);
            }
            return $role;
        }
    }


    /**
     * Notes: 更新权限人员关系表
     * User: lltyy
     * DateTime: 2025/6/4 9:52
     *
     */
    public function scanData(){
        //获取人员信息
        $adminUserArr = [
            'id'=>[],
            'tel'=>[]
        ];
        $adminUsers = $this->LocalService()->adminUser->fetchAll([
            'where'=>'id > 0 and mobile != "" and mobile is not null',
            'columns'=>['id','mobile','is_del'],
            'order'=>['id desc']
        ]);
        if(!empty($adminUsers)){
            foreach($adminUsers as $adminUserKey=>$adminUser){
                $adminUserArr['id'][$adminUser['id']] = $adminUser;
                if($adminUser['is_del'] == 0){
                    $adminUserArr['tel'][trim($adminUser['mobile'])] = $adminUser['id'];
                }
                unset($adminUsers[$adminUserKey]);
            }
        }
        unset($adminUsers);

        //获取中心人员信息
        $sigUserArr = [
            'id'=>[],
            'tel'=>[]
        ];
        $sigUsers = $this->LocalService()->signatoryUser->fetchAll([
            'where'=>'id > 0 and user_tel != "" and user_tel is not null',
            'columns'=>['id','user_tel','is_del'],
            'order'=>['id desc']
        ]);
        if(!empty($sigUsers)){
            foreach($sigUsers as $sigUserKey=>$sigUser){
                $sigUserArr['id'][$sigUser['id']] = $sigUser;
                if($sigUser['is_del'] == 0){
                    $sigUserArr['tel'][trim($sigUser['user_tel'])] = $sigUser['id'];
                }
                unset($sigUsers[$sigUserKey]);
            }
        }
        unset($sigUsers);

        $where = 'id > 0 and (signatory_user > 0 or admin_user_id > 0)';
        $bool = true;
        $page = 0;
        $limit = 1000;
        $last_id = 0;
        while ($bool) {
            $page++;
            echo "PAGE {$page}：" . PHP_EOL;
            $last_id = $this->mateData($where.' and id > '.$last_id,$limit,0,$adminUserArr,$sigUserArr);
            if(!$last_id) $bool = false;
        }
        unset($where);
        unset($adminUserArr);
        unset($sigUserArr);
        unset($last_id);
        unset($bool);
        unset($limit);
        unset($page);
    }

    /**
     * Notes: 执行更新权限人员关系表
     * User: lltyy
     * DateTime: 2025/6/4 10:00
     *
     * @param $where
     * @param int $limit
     * @param int $offset
     * @param $adminUserDatas //人员信息
     * @param $sigUserDatas //中心人员信息
     * @return int
     */
    public function mateData($where,$limit=0,$offset=0,&$adminUserDatas,&$sigUserDatas){
        //获取指定条件下的医嘱数据
        $cond = [
            'where'=>$where,
            'columns'=>['id','signatory_user','admin_user_id'],
            'order'=>['id']
        ];
        if(!empty($limit) || !empty($offset)){
            $cond['limit'] = $limit;
            $cond['offset'] = $offset;
        }
        $datas = $this->LocalService()->roleSignatoryRelation->fetchAll($cond);
        $last_id = 0;
        if(!empty($datas)){
            foreach($datas as $dataKey=>$data){
                $last_id = $data['id'];
                $sig_user = !empty($data['signatory_user']) && isset($sigUserDatas['id'][$data['signatory_user']]) && !empty($sigUserDatas['id'][$data['signatory_user']]) ? $sigUserDatas['id'][$data['signatory_user']] : [];
                $tel = !empty($sig_user) ? trim($sig_user['user_tel']) : '';
                unset($sig_user);
                $admin_user = !empty($data['admin_user_id']) && isset($adminUserDatas['id'][$data['admin_user_id']]) && !empty($adminUserDatas['id'][$data['admin_user_id']]) ? $adminUserDatas['id'][$data['admin_user_id']] : [];
                if($tel == ''){
                    $tel = !empty($admin_user) ? trim($admin_user['mobile']) : '';
                }
                unset($admin_user);
                $new_sig_user = $new_admin_user = 0;
                if($tel != ''){
                    $new_sig_user = isset($sigUserDatas['tel'][$tel]) && !empty($sigUserDatas['tel'][$tel]) ? $sigUserDatas['tel'][$tel] : 0;
                    $new_admin_user = isset($adminUserDatas['tel'][$tel]) && !empty($adminUserDatas['tel'][$tel]) ? $adminUserDatas['tel'][$tel] : 0;
                }

                if($new_sig_user != $data['signatory_user'] || $new_admin_user != $data['admin_user_id']){
                    $this->LocalService()->roleSignatoryRelation->isSetInfo(false)->update(['signatory_user'=>$new_sig_user,'admin_user_id'=>$new_admin_user],'id = '.$data['id']);
                }
                unset($new_admin_user);
                unset($new_sig_user);
                unset($datas[$dataKey]);
            }
        }

        //释放空间
        unset($datas);
        unset($cond);

        return $last_id;

    }

}