UserService.class.php 20.6 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 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 16/9/18
 * Time: 14:38
 */

namespace Common\Service;

use Com\PythonExcel;
use Common\Common\Cache;
use Common\Common\Constant;
use Common\Model\UserModel;
use VcySDK\Service;
use VcySDK\Member;
use Common\Common\User;
use Common\Common\Department;
use Common\Model\AttrModel;

class UserService extends AbstractService
{

    /**
     * 查询人员状态:全部
     */
    const USER_STATUS_ALL = 0;

    /**
     * 查询人员状态:已关注
     */
    const USER_STATUS_FOLLOW = 1;

    /**
     * 查询人员状态:已禁用
     */
    const USER_STATUS_DISABLE = 2;

    /**
     * 查询人员状态:未关注
     */
    const USER_STATUS_UNFOLLOW = 4;

    /**
     * 操作人员:启用
     */
    const ENABLE_USER = 1;

    /**
     * 操作人员:禁用
     */
    const DISABLE_USER = 2;

    /**
     * 操作人员:复职
     */
    const REHAB_USER = 3;

    /**
     * 操作人员:离职
     */
    const QUIT_USER = 4;

    /**
     * (架构)人员状态:启用
     */
    const STATUS_ENABLE = 1;

    /**
     * (架构)人员状态:禁用
     */
    const STATUS_DISABLE = 0;

    /**
     * 是否递归查询部门下所有子部门人员信息:是
     */
    const DEPT_CHILDREN_FLAG = 1;

    /**
     * 一年的天数
     */
    const YEAR_OF_DAY = 365;

    /**
     * 一天的毫秒数
     */
    const DAY_OF_MILLISECOND = 86400000;

    /**
     * 用户加入方式: 管理员添加
     */
    const ADMIN_ADD_JOIN = 1;

    /**
     * 用户加入方式: 邀请加入
     */
    const USER_INVITE_JOIN = 2;

    /**
     * 邀请加入:不是通知所有人未加入的人员
     */
    const NOTICE_NOT_TO_ALL_UNFOLLOW = 1;

    /**
     * 邀请加入:通知所有人未加入的人员
     */
    const NOTICE_TO_ALL_UNFOLLOW = 2;

    // 构造方法
    public function __construct()
    {

        parent::__construct();
        $this->_attr = new AttrModel();
    }

    /**
     * 保存人员(新增、修改人员信息)
     * @author liyifei
     * @param string $uid  人员UID
     * @param array  $data 校验过的表单数据
     * @return bool|mixed
     */
    public function saveUser($uid, $data)
    {

        // 调用架构接口,创建、修改用户信息
        $memServ = new Member(Service::instance());
        if ($uid) {
            $data['memUid'] = $uid;
            unset($data['memJoinType']);
            unset($data['memJoinInviter']);
            $result = $memServ->update($data);
        } else {
            $result = $memServ->add($data);
        }

        // 清理缓存
        clear_sys_cache([
            'Common.Department',
            'Common.Job',
            'Common.Role',
            'Common.User'
        ]);

        return $result;
    }

    /**
     * 单个或批量删除用户
     * @author liyifei
     * @param array $uids 人员uid
     * @return array|bool
     */
    public function delete($uids)
    {

        $userServ = new User();
        if (count($uids) > 1) {
            $userServ->batDelete($uids);
        } else {
            $userServ->delete($uids[0]);
        }

        // 清空人员缓存
        $userServ->clearUserCache($uids);

        // 清空部门缓存(增加和删除人员,涉及到部门成员数量)
        $deptCom = new Department();
        $deptCom->clearDepCache();
    }

    /**
     * 用户详情(手机端展示信息)
     * @author liyifei
     * @param string $uid 人员ID
     * @return mixed
     */
    public function getUserInfoByUid($uid, $attrs)
    {
        // 用户信息(从缓存架构用户信息表中的数据获取用户信息,参数设为true越过缓存)
        $commUser = new User();
        $userInfo = $commUser->getByUid($uid);
        // 匹配人员属性和人员详情数据
        $list = $this->getAttrListWithUserInfo($attrs, $userInfo, ['memFace',]);

        //  邀请人名称为空时, 说明是历史数据
        if (empty($userInfo['memJoinInviter'])) {
            $joinDesc = "管理员添加";
        } else {
            $joinDesc = $userInfo['memJoinType'] == self::ADMIN_ADD_JOIN ? "管理员({$userInfo['memJoinInviter']})添加"
                : "{$userInfo['memJoinInviter']}邀请加入";
        }

        // add by zhuxun37, 加入方式特殊处理
        $list[] = array(
            'field_name' => 'memJoin',
            'attr_name' => '加入方式',
            'attr_value' => $joinDesc,
            'option' => '',
            'type' => AttrModel::AREA_CONTACT,
            'order' => 0,
            'is_allow_user_modify' => AttrModel::ATTR_NOT_ALLOWED_USER_MODIFY
        );

        return $list;
    }

    /**
     * 用户详情(管理后台)
     * @author liyifei
     * @param string $uid 人员ID
     * @param array $userInfo 人员详情
     * @return mixed
     */
    public function userInfo($uid, $userInfo = [])
    {

        // 用户信息
        if (empty($userInfo)) {
            $newUser = new User();
            $userInfo = $newUser->getByUid($uid, true);
        }

        // Edit by liyifei at 2016-11-10 18:41:36 in V1.1.0 展示所有属性,包括空值的属性
        $attrs = $this->getAttrList(true, array(), true);

        $list = [];
        foreach ($attrs as $attr) {
            // 以下属性,不在list中出现
            if (in_array($attr['field_name'], ['memUsername', 'dpName'])) {
                continue;
            }

            // 根据属性类型不同,将属性值转为与前端约定好的格式
            $attrValue = $this->formatValueByType($attr['type'], $userInfo[$attr['field_name']]);

            $list[] = [
                'field_name' => $attr['field_name'],
                'attr_name' => $attr['attr_name'],
                'attr_value' => $attrValue,
                'option' => $attr['option'],
                'type' => $attr['type'],
                'order' => $attr['order'],
            ];
        }

        //  邀请人名称为空时, 说明是历史数据
        if (empty($userInfo['memJoinInviter'])) {
            $joinDesc = "管理员添加";
        } else {
            $joinDesc = $userInfo['memJoinType'] == self::ADMIN_ADD_JOIN ? "管理员({$userInfo['memJoinInviter']})添加"
                : "{$userInfo['memJoinInviter']}邀请加入";
        }

        // add by zhuxun37, 加入方式特殊处理
        $list[] = array(
            'field_name' => 'memJoin',
            'attr_name' => '加入方式',
            'attr_value' => $joinDesc,
            'option' => '',
            'type' => AttrModel::AREA_CONTACT,
            'order' => 0
        );

        // 关注时间
        if ($userInfo['memSubscribeTime']) {
            $list[] = array(
                'field_name' => 'memSubscribeTime',
                'attr_name' => '关注时间',
                'attr_value' => $userInfo['memSubscribeTime'],
                'option' => '',
                'type' => AttrModel::AREA_PERSONAL,
                'order' => 0
            );
        }

        // 被删除时间
        if ($userInfo['memDeleted']) {
            $list[] = array(
                'field_name' => 'memDeleted',
                'attr_name' => '删除时间',
                'attr_value' => $userInfo['memDeleted'],
                'option' => '',
                'type' => AttrModel::AREA_PERSONAL,
                'order' => 0
            );
        }

        // Edit by liyifei in V1.3.0 人员详情展示"司龄"属性(当前日期-入职日期)
        /**$age = [
            'attr_name' => '司龄',
            'attr_value' => '',
        ];
        $endTime = $userInfo['leaveDate'] ? $userInfo['leaveDate'] : MILLI_TIME;
        if ($userInfo['memJoinTime'] !== null) {
            $myMilliTime = $endTime - $userInfo['memJoinTime'];
            if ($myMilliTime < self::YEAR_OF_DAY * self::DAY_OF_MILLISECOND) {
                $myDay = ceil($myMilliTime / (self::DAY_OF_MILLISECOND)) > 0 ? ceil($myMilliTime / (self::DAY_OF_MILLISECOND)) : 0;
                if ($myDay < 365) {
                    // 司龄小于365天按天显示
                    $age['attr_value'] = $myDay . '天';
                } else {
                    // 司龄等于365天时,显示为年
                    $age['attr_value'] = "1";
                }

            } else {
                // 司龄大于等于365天按年显示,小数点后保留一位,四舍五入
                $year = floor($myMilliTime / (self::YEAR_OF_DAY * self::DAY_OF_MILLISECOND));
                $age['attr_value'] = $year + round(($myMilliTime - $year * (self::YEAR_OF_DAY * self::DAY_OF_MILLISECOND)) / (self::YEAR_OF_DAY * self::DAY_OF_MILLISECOND), 1) . '年';
            }
        }

        array_push($list, $age);*/

        return $list;
    }

    /**
     * 根据条件查询读取人员列表(可查看通讯录范围内的人员列表)
     * @author liyifei
     * @param array $conds 搜索条件
     *                     + string $uid 人员UID
     *                     + string $dpId 部门ID
     *                     + string $keyword 关键字
     *                     + string $index 姓名首字母
     * @param int   $page  当前页码
     * @param int   $limit 每页数据总数
     * @return mixed
     */
    public function getListByConds($conds = [], $page = 1, $limit = 30)
    {

        // 验证参数
        if (empty($conds['uid'])) {
            E('_ERR_PARAMS_IS_NULL');
        }

        // 实例化
        $newUser = new User();
        $rightServ = new DeptRightService();

        // 读取登录人员所在部门的通讯录可见范围中部门列表
        $range = $rightServ->getRangByUid($conds['uid']);
        $dpList = array_column($range, 'dpId');

        // 初始化搜索条件
        $condition = [];

        // 根据部门ID搜索
        if (!empty($conds['dpId'])) {
            // 无权查看,且自己所在的部门ID集合
            $notDpIds = [];
            $myDpIds = $newUser->listDepartment(['memUid' => $conds['uid']]);
            foreach ($myDpIds as $myDpId) {
                if (!in_array($myDpId, $dpList)) {
                    $notDpIds[] = $myDpId;
                }
            }

            // 查看自己所在的部门,且无权查看本部门通讯录时,仅返回该用户信息
            if (in_array($conds['dpId'], $notDpIds)) {
                // 返回本人信息详情
                $userInfo = $newUser->getByUid($conds['uid']);
                return $userInfo;
            }

            // 搜索的部门,是否有权限查看
            if (!in_array($conds['dpId'], $dpList)) {
                E('_ERR_CANOT_FIND_USER');
            }

            // 搜索条件
            $condition['dpId'] = $conds['dpId'];
            $condition['departmentChildrenFlag'] = !isset($conds['departmentChildrenFlag']) ? self::DEPT_CHILDREN_FLAG : (int)$conds['departmentChildrenFlag'];

        } else {
            // 当可查看的部门被删除时,仅可查看本人信息
            if (empty($dpList)) {
                // 返回本人信息详情
                $userInfo = $newUser->getByUid($conds['uid']);
                return $userInfo;
            } else {
                $condition['dpIdList'] = $dpList;
            }
        }

        // 搜索关键字
        if (!empty($conds['keyword'])) {
            $condition['memUsername'] = $conds['keyword'];
        }

        // 搜索首字母
        if (!empty($conds['index'])) {
            $condition['index'] = $conds['index'];
        }

        // 排序规则
        $orderList = [
            'memIndex' => 'ASC',
        ];

        // 获取缓存用户列表信息
        return $newUser->listByConds($condition, $page, $limit, $orderList);
    }

    /**
     * 读取 Excel 文件, 为导入做准备
     * @param $result
     * @param $request
     * @param $user
     * @return bool
     */
    public function readExcel(&$result, $request, $user)
    {
        //导入默认起始行数
        $default_line = Constant::STAFF_IMPORT_START;
        // 分页和每页数
        $page = (int)$request['page'];
        $limit = (int)$request['limit'];
        $start = $default_line + $limit * ($page - 1);
        $result = array(
            'rowsReaded' => 0,
            'page' => $page,
            'limit' => $limit
        );

        // 附件Url
        $attachmentUrl = $request['attachmentUrl'];
        if (empty($attachmentUrl)) {
            E('1002:Excel文件地址为空');
            return false;
        }

        // 导入唯一标识
        $importFlag = $request['importFlag'];
        if (empty($importFlag)) {
            $importFlag = NOW_TIME . random(8);
        }
        $result['importFlag'] = $importFlag;

        // 获取附件
        $localFile = get_sitedir() . $importFlag . '.xls';
        if (!file_exists($localFile)) {
            file_put_contents($localFile, file_get_contents($attachmentUrl));
        }

        // 如果文件大小为 0
        if (0 >= filesize($localFile)) {
            E('1004:Excel文件错误或文件内容为空');
            return false;
        }

        $importDataService = new ImportDataService();
        // 读取指定行
        $data = PythonExcel::instance()->read($localFile, $start, $start + $limit);
        if (empty($data)) {
            return true;
        }

        $result['rowsReaded'] = count($data);
        // 默认表头没有失败原因一列
        $existMessage = false;
        // 最后一列索引
        $i = 0;
        // 如果是第一页, 则需要额外保存表头
        if (1 == $page) {
            // 校验表头是否有多出的'失败原因'一列,有的话需要过滤掉
            $exceHead = $data[0];
            $i = count($exceHead) - 1;
            if ($exceHead[$i] == '失败原因') {
                $existMessage = true;
                array_splice($exceHead, $i, 1);
            }

            $importDataService->insert(array(
                'ea_id' => $user['eaId'],
                'import_flag' => $importFlag,
                'data_type' => 'title',
                'data' => rjson_encode($exceHead)
            ));
            unset($data[0]);
            unset($data[1]);
        }

        // 其他数据入库
        $insertData = array();
        foreach ($data as $_data) {
            if ($existMessage) {
                array_splice($_data, $i, 1);
            }
            $insertData[] = array(
                'ea_id' => $user['eaId'],
                'import_flag' => $importFlag,
                'data_type' => 'user',
                'data' => rjson_encode($_data)
            );
        }
        $importDataService->insert_all($insertData);

        return true;
    }

    // 导出模板
    public function exportTpl($attr)
    {

        $data = [];
        $titles = $this->formatExportTitle($attr);

         // 初始化标题简介数据
         $merge_titles = Constant::STAFF_LIST_TITLES_DESC;
         $merge_titles[] = array_values($titles);
         // 初始化默认值数据
         $firstData = Constant::STAFF_LIST_DATA;

         //删除导出特定状态
         unset($firstData[count($firstData)-1]);
         unset($firstData[count($firstData)-1]);

         $merge_titles[] = $firstData;

         // 合并标题到数据中
         $data = array_merge($merge_titles, $data);

        $title_xls[] = '填写说明';

        $filename = NOW_TIME . random(8) . '.xls';
        PythonExcel::instance()->write(get_sitedir() . $filename, $title_xls, $data);

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename=userTpl.xls');
        header("Content-Transfer-Encoding:binary");
        echo file_get_contents(get_sitedir() . $filename);
        exit;
    }

    /**
     * 获取用户所在部门列表
     * @param $user
     * @return array
     */
    public function listUserDpIds($user)
    {

        $dpIds = array();
        foreach ($user['dpName'] as $_user) {
            $dpIds[] = $_user['dpId'];
        }

        return $dpIds;
    }

    /**
     * 获取用户的顶级部门
     * @param $user
     * @param $currentDpId
     * @return bool|mixed
     */
    public function getUserTopDpId($user, $currentDpId = null)
    {

        $myDpIds = $this->listUserDpIds($user);
        if (1 == $myDpIds) {
            return $myDpIds[0];
        }

        // 获取所有上级部门
        $parentDpIds = array();
        foreach ($myDpIds as $_dpId) {
            Department::instance()->list_parent_cdids($_dpId, $parentDpIds);
        }

        // 获取子部门
        $childIds = Department::instance()->list_childrens_by_cdid($myDpIds, true);
        $ids = array_merge($parentDpIds, $childIds);
        $departments = Department::instance()->listAll();
        // 整理上/下级关系
        $p2c = array();
        foreach ($ids as $_id) {
            $current = $departments[$_id];
            $parentId = empty($current['dpParentid']) ? 0 : $current['dpParentid'];
            if (empty($p2c[$parentId])) {
                $p2c[$parentId] = array();
            }

            $p2c[$parentId][] = $_id;
        }

        if (null == $currentDpId || !in_array($currentDpId, $childIds)) {
            $currentDpId = $this->getTopDpId($p2c, $myDpIds, 0);
        }

        return array($currentDpId, $p2c, $childIds, $ids);
    }

    /**
     * 获取用户顶级部门ID
     * @param $p2c
     * @param $myDpIds
     * @param $currentIds
     * @return mixed
     */
    public function getTopDpId($p2c, $myDpIds, $currentIds)
    {

        $currentIds = (array)$currentIds;
        $results = array();
        foreach ($currentIds as $_id) {
            if (empty($p2c[$_id])) {
                continue;
            }

            foreach ($p2c[$_id] as $_cid) {
                if (in_array($_cid, $myDpIds)) {
                    return $_cid;
                }

                $results[] = $_cid;
            }
        }

        if (empty($results)) {
            return 0;
        }

        return $this->getTopDpId($p2c, $myDpIds, $results);
    }

    /**
     * 重新整理标题
     * @param $attr
     * @return mixed
     */
    protected function formatExportTitle($attr)
    {
        $title = [];
        foreach ($attr as $item) {
            // 没有开启该字段
            if ($item['is_open_cp'] != AttrModel::ATTR_IS_OPEN_TRUE) {
                continue;
            }
            // 是否必填
            if ($item['is_required_cp'] == AttrModel::ATTR_IS_REQUIRED_TRUE) {
                $title[] = [
                    'value' => $item['attr_name'],
                    'pattern' => [
                        'pattern' => 'solid',
                        'fore_colour' => 'red'
                    ]
                ];
            } else {
                $title[] = $item['attr_name'];
            }
        }

        return $title;
    }

    /**
     * 匹配人员属性和人员详情数据
     * @param $attrs
     * @param $userInfo
     * @param $skipField
     * @return array
     */
    private function getAttrListWithUserInfo($attrs, $userInfo, $skipField)
    {
        $list = [];
        foreach ($attrs as $attr) {
            // 跳过不显示的字段
            if (in_array($attr['field_name'], $skipField)) {
                continue;
            }
            // 根据属性类型不同,将属性值转为与前端约定好的格式
            $attrValue = $this->formatValueByType($attr['type'], $userInfo[$attr['field_name']]);

            // 属性类型为单选、下拉框单选时,将属性值由单选value转为单选name显示
            if (in_array($attr['type'], [AttrModel::ATTR_TYPE_RADIO, AttrModel::ATTR_TYPE_DROPBOX])) {
                foreach ($attr['option'] as &$item) {
                    if ($item['value'] == $attrValue) {
                        // 前端需要选中标志
                        $item['checked'] = true;
                        $attrValue = $item['name'];
                    } else {
                        $item['checked'] = false;
                    }
                }
            }

            // 保留有用的字段
            $temp = array_intersect_key_reserved($attr, [
                'field_name', 'attr_name', 'option', 'postion',
                'type', 'order', 'is_allow_user_modify',
                'is_required_cp', 'is_show'], true);
            $temp['is_require'] = $temp['is_required_cp'];
            unset($temp['is_required_cp']);
            $list[] = array_merge($temp, ['attr_value' => $attrValue]);
        }

        return $list;
    }

}