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

use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Where;

class RealroleEdc 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()->realRoleEdc->fetchAll($whereData);
                break;
            case 2:
                $data = $this->LocalService()->realRoleEdc->fetchOne($whereData);
                break;
            case 3:
                $data = $this->LocalService()->realRoleEdc->fetchCol($field,$whereData);
                break;
            default:
                $data['data'] = $this->LocalService()->realRoleEdc->fetchAll($whereData);
                $data['count'] = $this->LocalService()->realRoleEdc->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 {
            // 获取本次提交人之外的人员信息
            $roleRelationWhere = new Where();
            $roleRelationWhere->equalTo('is_del',0);
            $roleRelationWhere->equalTo('status',0);
            $roleRelationWhere->equalTo('item_id',$item_id);
            $roleRelationWhere->equalTo('item_sign_id',$itemsig_id);
            $roleRelationWhere->equalTo('role_id',$role_id);
            $roleRelationWhere->notIn('signatory_user',$signatoryUserData);

            $disabledRelationDatas = $this->LocalService()->roleSignatoryRelation->fetchCol('signatory_user', $roleRelationWhere);

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

            foreach($signatoryUserData as $signatory_user_id) {
                $relationData = [
                    'item_id' => $item_id,
                    'item_sign_id' => $itemsig_id,
                    'role_id' => $role_id,
                    'signatory_user' => $signatory_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);
            }

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


}