commonModule.
controller('userBusinessUnitModalController', ['$scope', '$log', '$translate', 'uiGridConstants', '$location', '$timeout', '$interval', '$filter', 'SweetAlert', 'dimensionService', 'userService', 'roleService','$q',
    function ($scope, $log, $translate, uiGridConstants, $location, $timeout, $interval, $filter, SweetAlert, dimensionService, userService, roleService, $q) {

        var modalSelector="#userBuModal";

        var roleCardSelector = '#assign-user-box';

        var showWarning = function (resultMsg) {

            var errMsg = $translate.instant(resultMsg);
            SweetAlert.warning(errMsg);
        };

        var showSuccessMsg = function (resultMsg) {

            var errMsg = $translate.instant(resultMsg);
            SweetAlert.success(errMsg);
        };

        var showModal = function () {

            getSpecialUserRoleList().then(function () {
                $(modalSelector).modal("show");;
            });

            //getSpecialUserRoleList();
            //$(modalSelector).modal("show");
        };

        $scope.$watch('operateType', function (newValue, oldValue) {
            if (newValue) {
                if (newValue == constant.Operation.Add) {
                }
                else if (newValue == constant.Operation.Edit) {
                    showModal();
                }
            }
        });


        //如果添加了用户,则写入该维度下的库
        $scope.$watch('addUserIsUpdate', function (newValue, oldValue) {
            if (newValue) {
                if ($scope.selectedUserList && $scope.selectedUserList.length > 0)
                {
                    //console.log(JSON.stringify($scope.selectedUserList));
                    addUserToDimensionValue($scope.selectedUserList);
                }
            }
        });

        //为当前维度添加用户
        var addUserToDimensionValue = function (selectedUserArray) {

            var selectedArr = [];

            if (selectedUserArray && selectedUserArray.length > 0) {
                for (var i = 0; i < selectedUserArray.length; i++) {
                    //用户的角色列表
                    //var roleList = selectedUserArray[i].roleInfoList;

                    //去掉在当前维度下存在的用户,当前用户的权限可能已经改动过
                    var isUserExist = _.find($scope.userUIGridOptions.data, function (num) {
                        return num.userID == selectedUserArray[i].userID;
                    });

                    if (isUserExist) {
                        continue;
                    }
                    selectedArr.push(selectedUserArray);
                    uiGridAction.addRow(selectedUserArray[i]);
                }
            }

            if (selectedArr.length == 0) {
                showWarning('UserExists');
            }
        };


        $(modalSelector).on("hidden.bs.modal", function () {
            $scope.operateType = null;
        });

        //添加用户,弹框,call指令 add-exist-user-modal
        $scope.addUser = function () {
            var selectedKeyItem = [];
            $scope.userUIGridOptions.data.forEach(function (row) {
                selectedKeyItem.push(row.userID);
            });
            $scope.addUserOperateType = constant.Operation.Add;
            $scope.addUserIsUpdate = false;
            $scope.selectedKeyItems = selectedKeyItem;
        };

        var dataHasChanged = function () {
            $scope.operateType = null;
            $scope.isUpdate = true;
        };

        $scope.save = function ()
        {
            //比较新旧数据的变化更新数据库
            var oldData = $scope.oldUIGridData;
            var newData = uiGridAction.populateDataObject($scope.userUIGridOptions.data);

            if (angular.equals(oldData, newData)) {
                //nothing to do
              //  alert('same data');

            } else {
               // alert('something changed');
                var deleteList = [];
                var addList = [];
                var updateList = [];
                //旧数据在新数据中没有找到,则放到addList
                newData.forEach(function (row) {

                    var findResult = _.find(oldData, function (item) {
                        return item.userID == row.userID;
                    });
                    //旧数据没有找到,说明是新加的数据
                    if (!findResult) {
                        addList.push(row);

                        //添加的用户,改变了默认值,则添加到update list
                        if (!row.isAccessible || !row.hasOriginalRole || row.extraRoleList.length > 0) {
                            updateList.push(row);
                        }
                    }
                    else {
                        //如果旧数据找到了,查看是否相等,,不等的话,放到update list
                        if (!angular.equals(row, findResult)) {
                            updateList.push(row);
                        }
                    }

                });
                //新数据在旧数据中没有找到,则放到deleteList
                oldData.forEach(function (row) {

                    var findResult = _.find(newData, function (item) {
                        return item.userID == row.userID;
                    });
                    //新数据没有找到,说明是删除的数据
                    if (!findResult) {
                        deleteList.push(row);
                    }
                });

                //console.log('addList: ' + JSON.stringify(addList));
                //console.log('updateList: ' + JSON.stringify(updateList));
                //console.log('deleteList: ' + JSON.stringify(deleteList));
                service.addList(addList).then(function () {
                    service.updateList(updateList).then(function () {
                        service.deleteList(deleteList).then(function () {
                            showSuccessMsg("SaveSuccess");
                            dataHasChanged();
                        });
                    });
                });
            }
         
            $(modalSelector).modal("hide");
        };

        //数据库更新
        var service =  {
            addList: function (rowList) {
                var deferred = $q.defer();
                var selectedArr = [];
                rowList.forEach(function (row) {
                    var selectedObject = {
                        id: PWC.newGuid(),
                        dimensionValueID: $scope.dimensionValueId,
                        dimensionID: $scope.dimensionId,
                        userID: row.userID,
                        IsAccessible: true, //添加用户,默认可访问
                        hasOriginalRole: true,//添加用户,默认继承原始角色
                    };
                    selectedArr.push(selectedObject);
                });

                if (selectedArr.length > 0) {
                    //添加用户
                    userService.updateUserRoleForDimension(selectedArr).success(function (data) {
                        if (data) {
                            deferred.resolve(data);
                        }
                    });
                }
                else {
                    deferred.resolve(true);
                }
                return deferred.promise;
            },
            updateList: function (rowList) {
                var deferred = $q.defer();

                var selectedArr = [];
                rowList.forEach(function (row) {
                    //如果有附加角色
                    if (row.extraRoleList && row.extraRoleList.length > 0) {
                        row.extraRoleList.forEach(function (item) {
                            var selectedObject = {
                                dimensionValueID: $scope.dimensionValueId,
                                dimensionID: $scope.dimensionId,
                                userID: $scope.currentUserID,
                                roleID: item.id,
                                isAdd: true,
                                IsAccessible: row.isAccessible,
                                hasOriginalRole: row.hasOriginalRole || false
                            };

                            selectedArr.push(selectedObject);
                        });
                    }
                    else {
                        //如果没有附加角色
                        var selectedObject = {
                            dimensionValueID: $scope.dimensionValueId,
                            dimensionID: $scope.dimensionId,
                            userID: $scope.currentUserID,
                           // roleID: item.id,
                            isAdd: false,
                            IsAccessible: row.isAccessible,
                            hasOriginalRole: row.hasOriginalRole || false
                        };

                        selectedArr.push(selectedObject);
                    }
                });

                if (selectedArr.length > 0) {
                    //edit row
                    userService.updateUserRoleDimension(selectedArr).success(function (data) {
                        if (data) {
                            deferred.resolve(data);
                        }
                    });
                }
                else {
                    deferred.resolve(true);
                }

                return deferred.promise;
            },
            deleteList: function (rowList) {
                var deferred = $q.defer();
                var selectedArr = [];
                rowList.forEach(function (row) {
                    var selectedObject = {
                        dimensionValueID: $scope.dimensionValueId,
                        dimensionID: $scope.dimensionId,
                        userID: row.userID
                    }
                    selectedArr.push(selectedObject);
                });
                if (selectedArr.length > 0) {
                    //删除用户橘色
                    userService.deleteUserRoleDimension(selectedArr).success(function (data) {
                        if (data) {
                            deferred.resolve(data);
                        }
                    });
                }
                else {
                    deferred.resolve(true);
                }

                return deferred.promise;

            }
        };

        $scope.closeModal = function () {
            $scope.operateType = null;
        };
        //用户角色框隐藏
        $scope.closeSetRoleCard = function ()
        {
            hideRoleInfoCard();
        };


        //所有角色,全选和全不选的时候
        $scope.AllRoleChecked = function () {
            angular.forEach($scope.roleList, function (item) {
                item.isChecked = $scope.roleEntity.isAllRoleChecked;
            });
        };
        $scope.rowItemClick = function () {
            var hasCheckAll = true;
            var roleList = $scope.roleList;
            for (var i = 0; i < roleList.length; i++) {
                if (!roleList[i].isChecked) {
                    hasCheckAll = false;
                    break;
                }
            }
            $scope.roleEntity.isAllRoleChecked = hasCheckAll;

        };

        //显示角色卡片的时候,原始角色绑定,并绑定是否选中
        //var getOrignalUserRole = function (userID) {
        //    //getUserRoleListByUserID
        //    userService.getUserRoleListByUserID(userID).success(function (data) {
        //        if (data) {

        //            var subRoleList = data && data.roleInfoList || [];
        //            $scope.originalUserRoleList = subRoleList;
        //            //console.log(JSON.stringify("rolelist:" + JSON.stringify(data.RoleInfoList)));
        //        }
                

        //    });
        //};

        //所有角色列表
        var getRoleByService = function (row) {
            roleService.getRoleListByRoleTypeId(constant.serviceType.VAT).success(function (roleData) {
                if (roleData) {
                    $scope.roleList = roleData;
                }
            });
        };
        //显示角色卡片的时候,获取所有当前用户的角色列表,如果在所有角色列表中有存在,则选中
        var setRoleByService = function (row) {

            //当前用户行
            var selectedUserRole = _.find($scope.useRoleList, function (item) {
                return item.userID == row.userID;
            });
            //当前用户行的角色列表
            var roleInfoList = selectedUserRole && selectedUserRole.extraRoleList;
         
            var hasAllCheck = true;
            // if (selectedUserRole && roleInfoList && roleInfoList.length > 0) {
            $scope.roleList.forEach(function (roleItem) {

                //只有是附加维度上的角色才显示在所有角色列表
                var selectedRole = _.find(roleInfoList, function (item) {
                    return item.id == roleItem.id;
                });

                if (selectedRole) {
                    roleItem.isChecked = true;
                }
                else {
                    roleItem.isChecked = false;
                    hasAllCheck = false;
                }
            });

            $scope.roleEntity.isAllRoleChecked = hasAllCheck;
            $scope.roleEntity.isHeritable = row.hasOriginalRole;
            $scope.roleEntity.isAccessible = row.isAccessible;

        };
        

        //为事业部的值(其他维度的值)编辑用户角色
        $scope.setRoleToDimensionValue = function () {
            var selectedArr = [];
            var hasOneItem = false;
            $scope.roleList.forEach(function (item) {

                if (item.isChecked) {
                    hasOneItem = true;
                    //var selectedObject = {
                    //    //id: PWC.newGuid(),
                    //    //dimensionValueID: $scope.dimensionValueId,
                    //    //dimensionID: $scope.dimensionId,
                    //    //userID: $scope.currentUserID,
                    //    roleID: item.id
                    //    //IsAccessible: $scope.roleEntity.isAccessible,
                    //    //hasOriginalRole: $scope.roleEntity.isHeritable || false
                    //};

                    selectedArr.push(item);
               
                }
            });
            
            if (!hasOneItem && $scope.roleEntity.isAccessible && !$scope.roleEntity.isHeritable)
            {
                showWarning("SelectAtLeastOneRole");
                return false;
            };

            uiGridAction.updateRow(selectedArr);
            //userService.updateUserRoleDimension(selectedArr).success(function (data) {
            //    if (data)
            //    {
            //        showSuccessMsg("SaveSuccess");
            //        //刷新角色列表
            //        getUserRoleList();
            //        $scope.hasDataChanged = true;
            //        hideRoleInfoCard();
            //    }
            //});
            hideRoleInfoCard();
        }

        var confirmWarningWindow = function (title, text) {
            var deferred = $q.defer();
            SweetAlert.swal({
                title: title,
                text: text,
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                allowOutsideClick: false,
                confirmButtonText: $translate.instant('Confirm'),
                cancelButtonText: $translate.instant('Cancel'),
                closeOnConfirm: true,
                closeOnCancel: true
            },
              function (isConfirm) {
                  deferred.resolve(isConfirm);
              });

            return deferred.promise;
        };

        //删除用户角色
        $scope.deleteUserRole = function (row)
        {
            //var selectedObject = {
            //    dimensionValueID: $scope.dimensionValueId,
            //    dimensionID: $scope.dimensionId,
            //    userID: row.userID

            //};

            var confirmToDelete = $translate.instant('ConfirmToDelete');

            confirmWarningWindow(confirmToDelete, '').then(function (data) {
                if (data) {

                    //userService.deleteUserRoleDimension(selectedObject).success(function () {
                    //    showSuccessMsg("OrganizationStructureDeleteSuccess");
                    //    //刷新角色列表
                    //    getUserRoleList();
                    //});
                    uiGridAction.deleteRow(row.userID);
                }
            });      
        };
        //ui grid上面的增加删除修改
        var uiGridAction = {
            deleteRow: function (userID) {
                var tempData = $scope.userUIGridOptions.data;
                for (var i = 0; i < tempData.length; i++) {
                    if (tempData[i].userID == userID) {
                        tempData.splice(i, 1);
                        break;
                    }
                }
                $scope.userUIGridOptions.data = tempData;
            },
            addRow: function (row) {
                var tempData = $scope.userUIGridOptions.data;

                var hasSpecialRole = false;
                var specialRole = _.find($scope.specialUserRoleList, function (item) {
                    return item.userID == row.userID;
                });
               //默认为原始角色,设置角色来源于原始角色
                row.roleInfoList.forEach(function (item) {
                    item.roleSource == constant.roleSource.OriginalLevel;
                });

                if (specialRole) {
                    hasSpecialRole = true;
                }
                var selectedObject = {
                    id: PWC.newGuid(),
                    dimensionValueID: $scope.dimensionValueId,
                    dimensionID: $scope.dimensionId,
                    userID: row.userID,
                    userName:row.userName,
                    isAccessible: true,
                    hasOriginalRole: true,
                    extraRoleList: [],
                    roleInfoStr: row.roleInfoStr,
                    roleInfoList:  row.roleInfoList,
                    hasSpecialRole: hasSpecialRole
                };
                tempData.push(selectedObject);
                $scope.userUIGridOptions.data = tempData;
            },
            updateRow: function (extraRoleList) {

                var allShowList = angular.copy(extraRoleList);
                var tempData = $scope.userUIGridOptions.data;
                //追加原始角色
                if ($scope.roleEntity.isHeritable) {
                    $scope.originalUserRoleList.forEach(function (row) {
                        var newRow = {
                            id: row.id,
                            roleSource: constant.roleSource.OriginalLevel,
                            name: row.name
                        }
                        allShowList.push(newRow);
                    });
                }
                //附加角色
                extraRoleList.forEach(function (row) {
                    row.roleSource = constant.roleSource.DimensionLevel;
                    var newRow = {
                        id: row.id,
                        roleSource: constant.roleSource.DimensionLevel,
                        name: row.name
                    }
                    allShowList.push(newRow);
                });

                var uniqRoleList = _.uniq(allShowList, function (item) { return item.id });

                for (var i = 0; i < tempData.length; i++) {
                    var row = tempData[i];
                    if (row.userID == $scope.currentUserID) {
                        row.isAccessible = $scope.roleEntity.isAccessible;
                        row.hasOriginalRole = $scope.roleEntity.isHeritable || false;
                        row.extraRoleList = extraRoleList;
                        if (row.isAccessible) {
                            if (uniqRoleList && uniqRoleList.length > 0) {
                                row.roleInfoStr = _.map(uniqRoleList, function (x) { return x.name; }).join(constant.comma);
                            }
                            else {
                                row.roleInfoStr = $translate.instant('NoRole');
                            }
                        }
                        else {
                            row.roleInfoStr = $translate.instant('NonAccessible');
                        }

                        break;
                    }
                }
                $scope.userUIGridOptions.data = tempData;

            },//构造对象,用来比较对象是否相等
            populateDataObject: function (uiGridData) {
                var arraryList = [];

                if (uiGridData) {
                    uiGridData.forEach(function (row) {
                        var newObject = {};
                        newObject.userID = row.userID;
                        newObject.userName = row.userName;
                        newObject.isAccessible = row.isAccessible;
                        newObject.hasOriginalRole = row.hasOriginalRole;
                        newObject.extraRoleList = row.extraRoleList;
                        arraryList.push(newObject);
                    });
                }
                return arraryList;

            }
        };


        //点击显示当前用户的角色卡片
        $scope.showRoleInfoCard = function (row) {

            $scope.roleEntity = {};
            $scope.currentUserID = row.entity.userID;

            var roleCard = $(roleCardSelector);
            var rowId = $('#' + row.entity.userID);
            //原始角色
            var originalRole = _.filter(row.entity.roleInfoList, function (item) {
                return item.roleSource == constant.roleSource.OriginalLevel;
            });
            $scope.originalUserRoleList = originalRole;

            setRoleByService(row.entity);

            showOrgCard(roleCard, rowId, -60, -270);
            stopPropagation();
        };

        var stopPropagation = function (event) {
            event = event || window.event;
            if (event.stopPropagation) {
                event.stopPropagation();
            } else {
                event.cancelBubble = true;
            }
        }

        // 显示机构角色卡片
        var showOrgCard = function (popCard, row, topOffset, leftOffset) {

            popCard.css("display", "block");

            var top = row.offset().top + topOffset;
            if (top + popCard.height() > window.innerHeight) {
                top = window.innerHeight - popCard.height() - 20;
            }

            popCard.css('top', top + 'px');

            var left = row.offset().left + leftOffset;
            if (left + popCard.width() > window.innerWidth) {
                left = row.offset().left - popCard.width() - 30;
            }

            popCard.css('left', left + 'px');

            popCard.show();

            $(document).one('click', function () {
                hideRoleInfoCard();
            });
        };

        var hideRoleInfoCard = function ()
        {
            $(roleCardSelector).hide();
        };


        //初始化也的ui grid
        var initUIGrid = function ()
        {
            userUIGridInit();
            priUIGridInit();
        };


        //用户角色数据初始化
        var getUserRoleList = function () {
            var deferred = $q.defer();
            userService.getUserRoleByDimensionValueID($scope.dimensionId, $scope.dimensionValueId).success(function (data) {
                if (data) {

                   // console.log(JSON.stringify(data));

                    data.forEach(function (row) {
                        //这儿包括了原始角色和附加角色,不管有没有可继承
                        var roleInfoList = angular.copy(row.roleInfoList);
                        if (!row.hasOriginalRole) {
                            roleInfoList= _.filter(row.roleInfoList, function (item) {
                                return item.roleSource == constant.roleSource.DimensionLevel;
                            });
                        }

                        var uniqRoleList = _.uniq(roleInfoList, function (item) { return item.id });

                        //附加角色
                        row.extraRoleList = _.filter(row.roleInfoList, function (item) {
                            return item.roleSource == constant.roleSource.DimensionLevel;
                        });

                        if (row.isAccessible) {
                            if (uniqRoleList && uniqRoleList.length > 0) {
                                row.roleInfoStr = _.map(uniqRoleList, function (x) { return x.name; }).join(constant.comma);
                            }
                            else {
                                row.roleInfoStr = $translate.instant('NoRole');
                            }
                        }
                        else {
                            row.roleInfoStr = $translate.instant('NonAccessible');
                        }
                        var specialRole = _.find($scope.specialUserRoleList, function (item) {
                            return item.userID == row.userID;
                        });
                        if (specialRole) {
                            row['hasSpecialRole'] = true;
                        }
                        else {
                            row['hasSpecialRole'] = false;
                        }
                    });
                    $scope.useRoleList = data;
                    $scope.userUIGridOptions.data = data;
                    $scope.oldUIGridData = uiGridAction.populateDataObject(data);
                    deferred.resolve();
                }
            });
            return deferred.promise;
        };
        //机构特殊权限初始化
        var getSpecialUserRoleList = function () {
            var parentDimensionID = $scope.dimensionId;
            var dimensionValueID = $scope.dimensionValueId ;
            var deferred = $q.defer();

            userService.getSpecialUserRoleByDimensionValueID(parentDimensionID, dimensionValueID).success(function (data) {
                if (data) {

                    data.forEach(function (row) {

                        var uniqRoleList = _.uniq(row.roleInfoList, function (item) { return item.id });

                        if (row.isAccessible) {
                            if (uniqRoleList && uniqRoleList.length > 0) {
                                row.roleInfoStr = _.map(uniqRoleList, function (x) { return x.name; }).join(constant.comma);
                            }
                            else {
                                row.roleInfoStr = $translate.instant('NoRole');
                            }
                        }
                        else {
                            row.roleInfoStr = $translate.instant('NonAccessible');
                        }

                        row.isHighLight = false;
                    });
                    $scope.specialUserRoleList = data;
                    $scope.priUIGridOptions.data = data;
                    //初始化角色列表
                    getUserRoleList().then(function () {
                        deferred.resolve();
                    });

                }
            });

            return deferred.promise;
        };
        
        //reset hightlight to false.
        var setDefaultTheme = function () {
            if ($scope.priUIGridOptions.data && $scope.priUIGridOptions.data.length > 0) {
                $scope.priUIGridOptions.data.forEach(function (row) {
                    row.isHighLight = false;
                });
            }
        };

        //选中高亮特殊角色
        $scope.chooseRelavantRow = function (row) {
            setDefaultTheme();

            if (row.hasSpecialRole == true) {
                var matchList = _.filter($scope.priUIGridOptions.data, function (num) {
                    return num.userID == row.userID;
                });

                if (matchList && matchList.length > 0) {
                    matchList.forEach(function (row2) {
                        row2.isHighLight = !row2.isHighLight;
                    });
                }
            }
        };

        var userUIGridInit = function () {

            $scope.columns = [
                  {
                      field: 'userName',
                      name: $translate.instant('UserDesc'),
                      width: '25%',
                      cellTemplate: '<div  class="text-align-left-padding" ><span title="{{row.entity.userName }}">{{row.entity.userName | limitString:20}}</span></div>'
                  }
            ];

            if ($scope.hasEditPermission) {

                var column = {
                    field: 'role',
                    name: $translate.instant('Role'),
                    width: '40%',
                    cellTemplate: '<div class="text-align-left-padding"  id="{{row.entity.userID}}"  aria-hidden="true" ng-click="grid.appScope.showRoleInfoCard(row)"><span title="{{row.entity.roleInfoStr}}">{{row.entity.roleInfoStr | limitString:24}}</span><i class="fa fa-caret-down icon-right" ></i></div>'

                };

                $scope.columns.push(column);

                column = {
                    field: 'isAccess',
                    name: $translate.instant('SpecialAccess'),
                    width: '20%',
                    cellTemplate: '<div class="text-align-left-padding" style="margin-left:22px" ng-show="row.entity.hasSpecialRole==true" > <span><i class="fa fa-check" aria-hidden="true" ></i> </span></div>'
                };


                $scope.columns.push(column);

                column = {
                    field: 'Delete',
                    name: $translate.instant('Delete'),
                    headerCellClass: 'text-align-left',
                    cellTemplate: '<div class="text-align-left-padding" style="margin-left:5px"> \
                                         <a class="operate-btn" href="javascript:void(0)">\
                                            <i class="material-icons highlight-tag" ng-click="grid.appScope.deleteUserRole(row.entity)">&#xE872;</i></a></div>',
                    width: '15%',

                };

                $scope.columns.push(column);
            }
            else {

                var column = {
                    field: 'role',
                    name: $translate.instant('Role'),
                    width: '55%',
                    cellTemplate: '<div class="text-align-left-padding"  id="{{row.entity.userID}}"  aria-hidden="true"><span title="{{row.entity.roleInfoStr}}">{{row.entity.roleInfoStr | limitString:40}}</span></div>'

                };

                $scope.columns.push(column);

                column = {
                    field: 'isAccess',
                    name: $translate.instant('SpecialAccess'),
                    width: '20%',
                    cellTemplate: '<div class="text-align-left-padding" style="margin-left:22px" ng-show="row.entity.hasSpecialRole==true" > <span><i class="fa fa-check" aria-hidden="true" ></i> </span></div>'
                };


                $scope.columns.push(column);
            }


            $scope.userUIGridOptions = {
                data: [],
                enableRowSelection: true,
                enableSelectAll: false,
                rowHeight: 30,
                enableRowHeaderSelection: false,
                modifierKeysToMultiSelect: false,
                enableHorizontalScrollbar: uiGridConstants.scrollbars.NEVER,
                enableVerticalScrollbar: uiGridConstants.scrollbars.enableVerticalScrollbar,
                renderContainers: undefined,
                enableSorting: true,
                enableColumnMenus: false,
                enableColumnResizing: false,
                enableFiltering: false,
                enableFullRowSelection: true, //是否点击行任意位置后选中,默认为false
                multiSelect: false, // 是否可以选择多个,默认为true; 

                columnDefs: $scope.columns,

                onRegisterApi: function (gridApi) {

                    $scope.userGridApi = gridApi;

                    $scope.gridSelectCount = $scope.userGridApi.selection.getSelectedRows().length;

                    //如果有特殊权限,就高亮特殊角色的那行
                    gridApi.selection.on.rowSelectionChanged($scope, function (row) {
                        $scope.chooseRelavantRow(row.entity);
                    });

                    // call resize every 500 ms for 5 s after modal finishes opening - usually only necessary on a bootstrap modal
                    $interval(function () {
                        $scope.userGridApi.core.handleWindowResize();
                    }, 500, 60 * 60 * 8);

                }
            };
        };
            //特殊权限UI grid初始化
        var priUIGridInit = function () {
            $scope.priUIGridOptions = {
                    data:[],
                    enableRowSelection: true,
                    enableSelectAll: false,
                    rowHeight: 30,
                    enableRowHeaderSelection: false,
                    modifierKeysToMultiSelect: false,
                    enableHorizontalScrollbar: uiGridConstants.scrollbars.NEVER,
                    enableVerticalScrollbar: uiGridConstants.scrollbars.enableVerticalScrollbar,
                    renderContainers: undefined,
                    enableSorting: true,
                    enableColumnResizing: false,
                    enableColumnMenus: false,
                    enableFiltering: false,
                    enableFullRowSelection: true, //是否点击行任意位置后选中,默认为false
                    multiSelect: false, // 是否可以选择多个,默认为true; 
                    rowTemplate: '<div ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name" class="ui-grid-cell" ng-class="{\'ui-grid-row-header-cell\':col.isRowHeader,\'red-color\':row.entity.isHighLight }"  ui-grid-cell></div>',

                    columnDefs: [
                           {
                               field: 'name',
                               name: $translate.instant('UserDesc'),
                               width: '30%',
                               cellTemplate: '<div  class="text-align-left-padding" title ="{{row.entity.userName}}"><span>{{row.entity.userName}}</span></div>'
                           },
                            {
                                field: 'name',
                                name: $translate.instant('OrganizationName'),
                                width: '30%',
                                cellTemplate: '<div  class="text-align-left-padding" title ="{{row.entity.organizationName}}"><span>{{row.entity.organizationName}}</span></div>'

                            },
                             {
                                 field: 'role',
                                 name: $translate.instant('Role'),
                                 width: '40%',
                                 cellTemplate: '<div class="text-align-left-padding" style="margin-right:30px;" ><span title="{{row.entity.roleInfoStr}}">{{row.entity.roleInfoStr}}</span></div>'
                             }
                    ],

                    onRegisterApi: function (gridApi) {

                        $scope.gridApi = gridApi;

                        $scope.gridSelectCount = $scope.gridApi.selection.getSelectedRows().length;

                        // call resize every 500 ms for 5 s after modal finishes opening - usually only necessary on a bootstrap modal
                        $interval(function () {
                            $scope.gridApi.core.handleWindowResize();
                        }, 500, 60 * 60 * 8);

                    }
                };

        };

        //权限check
        var checkUserPermission = function () {

            var list = [];

            $scope.hasQueryPermission = false;
            $scope.hasEditPermission = false;

            //权限设置的权限
            var tempAcess = constant.adminPermission.infrastructure.accessSetting;
            list.push(tempAcess.queryCode);
            list.push(tempAcess.editCode);

       
            $scope.$root.checkUserPermissionList(list).success(function (data) {

                $scope.hasQueryPermission = data[tempAcess.queryCode];
                $scope.hasEditPermission = data[tempAcess.editCode];

                if ($scope.hasQueryPermission) {
                    initUIGrid();
                }
            });
        };


        ;(function initialize() {
            $log.debug('userBusinessUnitModalController.ctor()...');
           // $scope.dimensionId = 'c61a5bd6-a996-4952-9869-d053995237e5';
            //$scope.dimensionValueId = '0bae00f7-96dc-46c8-b6f5-a427616c5bcf';
            checkUserPermission();
       
            getRoleByService();
           // showModal();
            //$scope.setRoleToDimensionValue = setRoleToDimensionValue;
            
        })();
    }
]);