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

use Interop\Container\ContainerInterface;

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

    /*
     * 将角色中用户变为字符串
     * */
    public function changeUser($item_id,$user_id = 0,$item_sign_id = 0){
        //查询角色下的角色管理用户信息
        $where = ['is_del'=>0,'status'=>0,'item_id'=>$item_id];
        if(!empty($user_id))$where['signatory_user'] = $user_id;
        if(!empty($item_sign_id))$where['item_sign_id'] = $item_sign_id;
        $whereroleSignatoryRelation['where'] = $where;
        $whereroleSignatoryRelation['columns'] = ['id', 'item_id', 'role_id', 'item_sign_id', 'signatory_user'];
        $rolesignatoryrelationDate = $this->LocalService()->roleSignatoryRelation->fetchAll($whereroleSignatoryRelation);

        //不是本合作方人员不展示
        if(!empty($rolesignatoryrelationDate)){
            foreach ($rolesignatoryrelationDate as $rolesignatoryrelationDateKey=>&$rolesignatoryrelationDateValue) {
                $signUserInfoId = 0;
                $signinfoId = $this->LocalService()->itemSignatory->getOneFieldVal('signatory_id', ['is_del'=>0,'id'=>$rolesignatoryrelationDateValue['item_sign_id']]);
                $realRoleSourceType = $this->LocalService()->realRole->getOneFieldVal('source_type',['is_del'=>0,'id'=>$rolesignatoryrelationDateValue['role_id']]);
                !empty($rolesignatoryrelationDateValue['signatory_user']) && $signUserInfoId = $this->LocalService()->signatoryUser->getOneFieldVal('info_id', ['is_del'=>0,'status'=>0,'id'=>$rolesignatoryrelationDateValue['signatory_user']]);
                if (empty($signinfoId)){
                    unset($rolesignatoryrelationDate[$rolesignatoryrelationDateKey]);
                    continue;
                }
                if (empty($realRoleSourceType) && ($signinfoId != $signUserInfoId)){
                    unset($rolesignatoryrelationDate[$rolesignatoryrelationDateKey]);
                }
            }
        }

        //处理数据  将相同的项目-中心-角色下的用户以，格式组合
        $RoleId = array_unique(array_column($rolesignatoryrelationDate, 'role_id'));
        $ItemSignId = array_unique(array_column($rolesignatoryrelationDate, 'item_sign_id'));
        $RoleData = [];
        $i = 1;
        foreach ($ItemSignId as $key=>$val) {
            foreach ($RoleId as $k=>$v) {
                $RoleData[$i]['role_id'] = $v;
                $RoleData[$i]['item_sign_id'] = $val;
                $i++;
            }
        }
        $rolesignatoryrelationDates = [];
        foreach($RoleData as $key=>$val){
            $signatory_user = "";
            foreach ($rolesignatoryrelationDate as $k=>$v) {
                if($v['role_id'] == $val['role_id']){
                    if($v['item_sign_id'] == $val['item_sign_id']){
                        $rolesignatoryrelationDates[$key]['id'] = $v['id'];
                        $rolesignatoryrelationDates[$key]['role_id'] = $v['role_id'];
                        $rolesignatoryrelationDates[$key]['item_id'] = $v['item_id'];
                        $rolesignatoryrelationDates[$key]['item_sign_id'] = $v['item_sign_id'];
                        if(!empty($v['signatory_user']))$signatory_user .= $v['signatory_user'].",";
                    }
                }
            }
            if(!empty($signatory_user)){
                $rolesignatoryrelationDates[$key]['signatory_user'] = rtrim($signatory_user,",");
            }else{
                $rolesignatoryrelationDates[$key]['signatory_user'] = "";
            }
        }
        return $rolesignatoryrelationDates;
    }

    /**
     * Notes: 获取指定人员担任的中心角色信息
     * User: lltyy
     * DateTime: 2022/11/16 11:27
     *
     * @param $item_id
     * @param int $user_id
     */
    public function fetchUserRoleBySig($item_id,$user_id){
        $arr = [];
        $cond = [
            'where'=>['is_del'=>0,'item_id'=>$item_id,'signatory_user'=>$user_id,'status'=>0],
            'columns'=>['item_sign_id','role_id']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach($datas as $data){
                $data_itemsig = $data['item_sign_id'];
                $data_role = $data['role_id'];
                $arr[$data_itemsig][$data_role] = $data_role;
            }
        }
        return $arr;
    }

    /**
     * Notes: 获取中心角色下的人员信息
     * User: lltyy
     * DateTime: 2022/11/16 11:27
     *
     * @param $item_id
     * @param int $user_id
     */
    public function fetchUserBySigRole($where){
        //这个方法不用调整，现在调用的上游没有指定对应ID
        $arr = [];
        $cond = [
            'where'=>$where,
            'columns'=>['item_sign_id','role_id','signatory_user']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach($datas as $data){
                $data_itemsig = $data['item_sign_id'];
                $data_role = $data['role_id'];
                $data_signatory_user = $data['signatory_user'];
                if(!empty($data_signatory_user)){
                    $arr[$data_itemsig][$data_role][$data_signatory_user] = $data_signatory_user;
                }
            }
        }
        return $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;
    }

}