RightService.class.php 16.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
<?php
/**
 * 培训权限表Service
 * @author: houyingcai
 * @email:  594609175@qq.com
 * @date :  2017-08-29 16:34:16
 * @version $Id$
 */
namespace Common\Service;

use Common\Common\Department;
use Common\Common\Job;
use Common\Common\Role;
use Common\Common\Tag;
use Common\Common\User;
use Common\Model\RightModel;

class RightService extends AbstractService
{
    // 权限类型:全公司
    const RIGHT_TYPE_ALL = 1;
    // 权限类型:部门
    const RIGHT_TYPE_DEPARTMENT = 2;
    // 权限类型:标签
    const RIGHT_TYPE_TAG = 3;
    // 权限类型:人员
    const RIGHT_TYPE_USER = 4;
    // 权限类型:职位
    const RIGHT_TYPE_JOB = 5;
    // 权限类型:角色
    const RIGHT_TYPE_ROLE = 6;

    // 是导入人员
    const IMPORT_USER = 1;
    // 非导入人员
    const NOT_IMPORT_USER = 0;

    public function __construct()
    {
        $this->_d = new RightModel();

        parent::__construct();
    }

    /**
     * 格式化数据库中的权限数据
     *
     * @author houyingcai
     *
     * @param array $rights 权限数据
     *
     * @return array
     */
    public function format_db_data($rights)
    {
        $data = [];

        // 数据分组
        foreach ($rights as $right) {
            $data[$right['obj_type']][] = $right['obj_id'];
        }

        return $data;
    }


    /**
     * 格式化用户输入的权限数据
     *
     * @author houiyngcai
     *
     * @param array $rights 权限数据
     *
     * @return array
     */
    public function format_post_data($rights)
    {
        $data = [];

        foreach ($rights as $k => $v) {
            // 全公司
            if ($k == 'is_all' && $v == self::IS_ALL) {

                $data = [self::RIGHT_TYPE_ALL => [self::IS_ALL]];
                break;
            }

            // 过滤空数组
            if (!is_array($v) || empty($v)) {

                continue;
            }

            // 数据分组
            switch ($k) {
                case 'dp_list':
                    $dp_ids = array_column($v, 'dpID');
                    $data[self::RIGHT_TYPE_DEPARTMENT] = $dp_ids;
                    break;
                case 'tag_list':
                    $tag_ids = array_column($v, 'tagID');
                    $data[self::RIGHT_TYPE_TAG] = $tag_ids;
                    break;
                case 'user_list':
                    $uids = array_column($v, 'memID');
                    $data[self::RIGHT_TYPE_USER] = $uids;
                    break;
                case 'job_list':
                    $job_ids = array_column($v, 'jobID');
                    $data[self::RIGHT_TYPE_JOB] = $job_ids;
                    break;
                case 'role_list':
                    $role_ids = array_column($v, 'roleID');
                    $data[self::RIGHT_TYPE_ROLE] = $role_ids;
                    break;
                default:

                    break;
            }
        }

        return $data;
    }

    /**
     * 比较权限数据,并返回需要新增和删除的数据
     *
     * @author houyingcai
     *
     * @param array $rights_db 数据库中的权限数据
     * @param array $rights_post 用户输入的权限数据
     *
     * @return array
     */
    public function diff_data($rights_db, $rights_post)
    {
        $keys = [
            self::RIGHT_TYPE_ALL,
            self::RIGHT_TYPE_DEPARTMENT,
            self::RIGHT_TYPE_TAG,
            self::RIGHT_TYPE_USER,
            self::RIGHT_TYPE_JOB,
            self::RIGHT_TYPE_ROLE,
        ];

        $data = [];

        // 遍历所有权限类型
        foreach ($keys as $key) {
            $rights_old = isset($rights_db[$key]) ? $rights_db[$key] : [];
            $rights_new = isset($rights_post[$key]) ? $rights_post[$key] : [];

            // 需要删除的数据
            $data[$key]['del'] = array_diff($rights_old, $rights_new);

            // 需要新增的数据
            $data[$key]['add'] = array_diff($rights_new, $rights_old);
        }

        return $data;
    }

    /**
     * 保存权限数据
     *
     * @author houyingcai
     *
     * @param array $conds 权限筛选条件
     * @param array $data 权限数据
     *        + int is_all 是否全公司
     *        + array dp_list 部门ID
     *        + array tag_list 标签ID
     *        + array user_list 用户ID
     *        + array job_list 职位ID
     *        + array role_list 角色ID
     * @param array $import_data 导入人员UID列表
     *
     * @return bool
     */
    public function save_data($conds, $data, $import_data)
    {
        $insert_data = [];
        // 权限范围人员
        if (!empty($data) && is_array($data)) {

            $list = $this->list_by_conds($conds);
            $rights_db = $this->format_db_data($list);
            $rights_post = $this->format_post_data($data);
            $rights = $this->diff_data($rights_db, $rights_post);

            foreach ($rights as $k => $v) {
                // 删除数据
                if (!empty($v['del'])) {

                    $del_conds = array_merge($conds, ['obj_type' => $k, 'obj_id' => $v['del']]);
                    $this->delete_by_conds($del_conds);
                }

                // 新增数据
                foreach ($v['add'] as $obj_id) {

                    $insert_data[] = array_merge($conds, [
                        'obj_type' => $k,
                        'obj_id' => $obj_id,
                        'uid_is_import' => self::NOT_IMPORT_USER, // 是否导入用户(0:否,1:是)
                    ]);
                }
            }
        }
        // 导入人员
        if (!empty($import_data) && is_array($import_data)) {

            foreach ($import_data as $val) {
                $insert_data[] = array_merge($conds, [
                    'obj_type' => self::RIGHT_TYPE_USER,
                    'obj_id' => $val,
                    'uid_is_import' => self::IMPORT_USER, // 是否导入用户(0:否,1:是)
                ]);
            }
        }

        // 批量插入新增数据
        if (!empty($insert_data)) {

            $this->insert_all($insert_data);
        }

        return true;
    }

    /**
     * 根据权限数据,获取对应的用户ID
     *
     * @author houyingcai
     *
     * @param array $rights 权限数据
     *
     * @return array
     */
    public function get_uids_by_right($rights)
    {
        $dp_ids = [];
        $uids = [];
        $conds = [];

        // 全公司
        if (isset($rights[self::RIGHT_TYPE_ALL])) {

            $userServ = &User::instance();
            $list = $userServ->listAll(['memSubscribeStatus' => self::USER_STATUS_FOLLOW]);

            return array_column($list, 'memUid');
        }

        // 部门
        if (isset($rights[self::RIGHT_TYPE_DEPARTMENT])) {

            $dp_ids = $rights[self::RIGHT_TYPE_DEPARTMENT];
        }

        // 标签
        if (isset($rights[self::RIGHT_TYPE_TAG])) {

            $tagServ = Tag::instance();
            $list = $tagServ->listAllMember(['tagIds' => $rights[self::RIGHT_TYPE_TAG]]);

            foreach ($list as $v) {
                // 标签中的部门
                if (isset($v['dpId']) && !empty($v['dpId'])) {

                    $dp_ids[] = $v['dpId'];

                    // 标签中的人员
                } elseif (isset($v['memUid']) && !empty($v['memUid'])) {

                    $uids[] = $v['memUid'];
                }
            }
        }

        // 部门ID
        if (!empty($dp_ids)) {

            $conds['dpIdList'] = $dp_ids;
            $conds['departmentChildrenFlag'] = 1;
        }

        // 职位
        if (isset($rights[self::RIGHT_TYPE_JOB])) {

            $conds['jobIdList'] = $rights[self::RIGHT_TYPE_JOB];
        }

        // 角色
        if (isset($rights[self::RIGHT_TYPE_ROLE])) {

            $conds['roleIdList'] = $rights[self::RIGHT_TYPE_ROLE];
        }

        // 根据筛选条件获取用户列表
        if (!empty($conds)) {
            $userServ = User::instance();
            $list = $userServ->listAll($conds);
            $data = array_column($list, 'memUid');
            $uids = array_merge($uids, $data);
        }

        // 人员
        if (isset($rights[self::RIGHT_TYPE_USER])) {

            $uids = array_merge($uids, $rights[self::RIGHT_TYPE_USER]);
        }

        return array_unique($uids);

    }

    /**
     * 获取格式化后的权限数据
     *
     * @author houyingcai
     *
     * @param array $conds 权限筛选条件
     *
     * @return array
     */
    public function get_data($conds)
    {
        $list = $this->list_by_conds($conds);
        $rights_db = $this->format_db_data($list);
        $data = [
            'is_all' => self::NOT_IS_ALL,
            'dp_list' => [],
            'tag_list' => [],
            'user_list' => [],
            'job_list' => [],
            'role_list' => [],
        ];

        foreach ($rights_db as $k => $v) {
            switch ($k) {
                // 全公司
                case self::RIGHT_TYPE_ALL:
                    $data['is_all'] = self::IS_ALL;
                    break;

                // 部门
                case self::RIGHT_TYPE_DEPARTMENT:
                    $dpServ = &Department::instance();
                    $dps = $dpServ->listById($v);

                    foreach ($dps as $dp) {
                        $data['dp_list'][] = [
                            'dpID' => $dp['dpId'],
                            'dpName' => $dp['dpName'],
                        ];
                    }
                    break;

                // 标签
                case self::RIGHT_TYPE_TAG:
                    $tagServ = &Tag::instance();
                    $tags = $tagServ->listAll($v);

                    foreach ($tags as $tag) {
                        $data['tag_list'][] = [
                            'tagID' => $tag['tagId'],
                            'tagName' => $tag['tagName'],
                        ];
                    }
                    break;

                // 人员
                case self::RIGHT_TYPE_USER:
                    $userServ = &User::instance();
                    $users = $userServ->listAll(['memUids' => $v]);

                    foreach ($users as $user) {
                        $data['user_list'][] = [
                            'memID' => $user['memUid'],
                            'memUsername' => $user['memUsername'],
                            'memFace' => $user['memFace'],
                        ];
                    }
                    break;

                // 职位
                case self::RIGHT_TYPE_JOB:
                    $jobServ = &Job::instance();
                    $jobs = $jobServ->listById($v);

                    foreach ($jobs as $job) {
                        $data['job_list'][] = [
                            'jobID' => $job['jobId'],
                            'jobName' => $job['jobName'],
                        ];
                    }
                    break;

                // 角色
                case self::RIGHT_TYPE_ROLE:
                    $roleServ = &Role::instance();
                    $roles = $roleServ->listById($v);

                    foreach ($roles as $role) {
                        $data['role_list'][] = [
                            'roleID' => $role['roleId'],
                            'roleName' => $role['roleName'],
                        ];
                    }
                    break;
            }
        }

        return $data;
    }

    /**
     * 统计导入人员数量
     *
     * @author houyingai
     *
     * @param array $conds 筛选条件
     *
     * @return int
     */
    public function count_import_users($conds = [])
    {
        $count = 0;
        if (!empty($conds)) {

            $count = $this->count_by_conds($conds);
        }

        return $count;
    }

    /**
     * 获取所有导入人员UID
     *
     * @author houyingai
     *
     * @param array $conds 筛选条件
     *
     * @return int
     */
    public function list_import_users($conds = [])
    {
        $user_info_list = [];
        if (!empty($conds)) {

            $list = $this->list_by_conds($conds, null, [], 'obj_id');
        }

        $uids = array_column($list, 'obj_id');

        if (!empty($uids)) {

            $user_list = $this->get_all_user_by_cache($uids);
        }

        if (!empty($user_list)) {

            foreach ($user_list as $v) {

                $user_info_list[] = [
                    'memID' => $v['memUid'],
                    'memUsername' => $v['memUsername'],
                    'memFace' => $v['memFace']
                ];
            }
        }

        return json_encode($user_info_list);
    }

    /**
     * 获取用户提交的权限范围内的所有人UID集合
     *
     * @author houyingcai
     *
     * @param array $post_right 用户提交的权限数据
     *
     * @return array
     */
    public function list_post_right_uids($post_right)
    {
        $userServ = &User::instance();
        if ($post_right['is_all'] == self::IS_ALL) {
            
            // 获取全部人员
            $result = $userServ->listBasicByConds(['memSubscribeStatus' => self::USER_STATUS_FOLLOW], 1, 1);
            $list = $userServ->listBasicByConds(['memSubscribeStatus' => self::USER_STATUS_FOLLOW], 1, $result['total']);

            return array_column($list['list'], 'memUid');
        }

        // 处理标签
        if (!empty($post_right['tag_ids'])) {

            $tagServ = &Tag::instance();
            $list = $tagServ->listAllMember(['tagIds' => $post_right['tag_ids']]);

            foreach ($list as $v) {
                // 标签中的部门
                if (isset($v['dpId']) && !empty($v['dpId'])) {

                    $tag_dp_ids[] = $v['dpId'];

                // 标签中的人员
                } elseif (isset($v['memUid']) && !empty($v['memUid'])) {

                    $tag_uids[] = $v['memUid'];
                }
            }
        }

        // 初始化保存人员UID
        $post_right['uids'] = isset($post_right['uids']) ? $post_right['uids'] : [];
        if (isset($tag_uids)) {
            $post_right['uids'] = array_merge($post_right['uids'], $tag_uids);
        }

        // 处理部门
        $post_right['dp_ids'] = isset($post_right['dp_ids']) ? $post_right['dp_ids'] : [];
        if (isset($tag_dp_ids)) {
            $post_right['dp_ids'] = array_merge($post_right['dp_ids'], $tag_dp_ids);
        }
        if (!empty($post_right['dp_ids'])) {

            // 部门ID去重
            $post_right['dp_ids'] = array_unique($post_right['dp_ids']);
            sort($post_right['dp_ids']);

            $dpServ = &Department::instance();
            // 取子级部门ID
            $child_ids = $dpServ->list_childrens_by_cdid($post_right['dp_ids']);
            // 合并部门ID
            $dp_ids = array_unique(array_merge($post_right['dp_ids'], array_values($child_ids)));
            sort($dp_ids);

            $dp_conds = [
                'dpIdList' => $dp_ids,
                'memSubscribeStatus' => self::USER_STATUS_FOLLOW
            ];

            $dp_users = $userServ->listAll($dp_conds);
            // 选择部门下的员工id集合
            $memUids = array_column($dp_users, 'memUid');

            $post_right['uids'] = array_merge($post_right['uids'], $memUids);
        }

        // 处理岗位
        if (!empty($post_right['job_ids'])) {

            $post_right['job_ids'] = array_unique($post_right['job_ids']);
            sort($post_right['job_ids']);
            // 初始化SDK
            $job_list = $userServ->listAll(
                [
                    'jobIdList' => $post_right['job_ids'],
                    'memSubscribeStatus' => self::USER_STATUS_FOLLOW
                ]);

            // 如果岗位下存在人员
            if (!empty($job_list)) {

                $post_right['uids'] = array_merge($post_right['uids'], array_column($job_list, 'memUid'));
            }
        }

        // 处理角色
        if (!empty($post_right['role_ids'])) {

            $post_right['role_ids'] = array_unique($post_right['role_ids']);

            sort($post_right['role_ids']);

            $role_List = $userServ->listAll(
                [
                    'roleIdList' => $post_right['role_ids'],
                    'memSubscribeStatus' => self::USER_STATUS_FOLLOW
                ]);

            // 如果岗位下存在人员
            if (!empty($role_List)) {
                $post_right['uids'] = array_merge($post_right['uids'], array_column($role_List, 'memUid'));
            }
        }

        return array_unique($post_right['uids']);
    }
}