'use strict'; app.controller('grouplistCtrl', ["$rootScope", "$scope", "$state", "$timeout", "$interval", "SweetAlert", "$modal", "Restangular", "api_user_data", "api_configure_data", function($rootScope, $scope, $state, $timeout, $interval, SweetAlert, $modal, Restangular, api_user_data, api_configure_data) { var loginUser = $rootScope.user; $scope.xinzeng=false; $scope.shanchu=false; $scope.bianji=false; for(var i=0;i 0) { // angular.forEach(branch.dashboard, function(item) { // if (index.id == item.id) { // $scope.dashboard_tree[i].checks = true; // } // }) // } // }) // } //组树 $scope.my_tree = {}; $scope.my_user = {}; $scope.try_async_load = function() { $scope.group = {}; $scope.my_user = []; $scope.my_data = []; $scope.doing_async = true; var postData = { "idx": 0, "sum": 1000, group: {} }; if($rootScope.user.duty){ // 当前的所属责任科室 postData.group.duty = $rootScope.user.duty; }else if($rootScope.user.branch){ // 当前的所属院区 postData.group.branch = $rootScope.user.branch.id; } api_user_data.fetchDataList('group', postData).then(function(response) { $scope.doing_async = false; if (response.status == 200) { var data = response.list; var objects = []; for (var i = 0; i < data.length; i++) { var object = {}; // object.id = data[i].id; // if (data[i].parent && data[i].parent.id != 0) { // object.parent = data[i].parent.id; // } angular.extend(object, data[i]); object.label = object.branchName + '-' + (object.duty ? object.duty.dept : '') + '-' + object.groupName; // if (loginUser.id == 1) { // objects.push(object); // } else { // if (object.roletype == 0) { // objects.push(object); // } // } objects.push(object); } $scope.my_data = convertParentToChildList(objects); $scope.tree_data = angular.copy($scope.my_data); } else { SweetAlert.swal({ title: "系统错误!", text: "请刷新重试!", type: "error" }); } }); }; function convertListToTree(data, treeMap) { var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup var root = null; //Initially set our loop to null //loop over data for (var i = 0; i < data.length; i++) { var datum = data[i]; //each node will have children, so let's give it a "children" poperty datum.children = []; //add an entry for this node to the map so that any future children can //lookup the parent idToNodeMap[datum.id] = datum; //Does this node have a parent? // console.log("datum="+JSON.stringify(datum)) if (typeof datum.parent === "undefined" || datum.parent === null) { //Doesn't look like it, so this node is the root of the tree root = datum; treeMap[datum.id] = root; } else { //This node has a parent, so let's look it up using the id parentNode = idToNodeMap[datum.parent.id]; //We don't need this property, so let's delete it. // delete datum.parent; //Let's add the current node as a child of the parent node. parentNode.children.push(datum); } } return root; } function convertParentToChildList(data) { var treeMap = {}; var list = []; convertListToTree(data, treeMap); angular.forEach(treeMap, function(item) { list.push(item); }); return list; } $scope.try_async_load(); //end power //tree user $scope.try_async_powerload = function(items) { if (items) { $scope.getbranchs = items.users; } // $scope.my_power = []; var objects = []; var selected = []; for (var i = 0; i < $scope.getbranchs.length; i++) { var object = {}; angular.extend(object, $scope.getbranchs[i]); object.label = object.name; objects.push(object); } selected = angular.copy($scope.tree_user); // if (data.length > 0) { angular.forEach(selected, function(index, i) { angular.forEach(objects, function(item) { if (index.id == item.id) { selected[i].checks = true; } }) }) $scope.my_user = selected; // if ($scope.my_user.length > 0) { // $scope.doing_asyncuser = false; // } }; //tree admin $scope.my_user = []; $scope.tile = []; $scope.try_async_user = function() { var postData = { "idx": 0, "sum": 9999, user: {engineer: 1,} }; if($rootScope.user.duty){ // 当前的所属责任科室 postData.user.duty = $rootScope.user.duty; }else if($rootScope.user.branch){ // 当前的所属院区 postData.user.branch = $rootScope.user.branch; } api_user_data.fetchDataList("user", postData).then(function(response) { if (response.status == 200) { var data = response.list; var objects = []; for (var i = 0; i < data.length; i++) { var object = {}; // object.id = data[i].id; // if (data[i].parent && data[i].parent.id != 0) { // object.parent = data[i].parent.id; // } angular.extend(object, data[i]); object.label = object.name; objects.push(object); } $scope.tile = objects; $scope.my_user = convertParentToChildList(objects); $scope.tree_user = angular.copy($scope.my_user); if ($scope.my_user.length > 0) { $scope.doing_asyncuser = false; } } else { SweetAlert.swal({ title: "系统错误!", text: "请刷新重试!", type: "error" }); } }); }; $scope.try_async_user(); //点击角色 $scope.group = {}; $scope.change = true; $scope.my_tree_handler = function(branch) { $scope.group = branch; $scope.try_async_powerload(branch); $scope.change = false; } //start remove $scope.getbranchs = {}; $scope.formbranch = function(branchData) { $scope.getbranchs = branchData; } //change right $scope.changeData = function() { if(!$scope.group.id){ SweetAlert.swal({ title: "操作失败", text: "请选择需要修改的项!", type: "error", confirmButtonColor: "#DD6B55" }); return; } var users = []; $rootScope.isMask = true; angular.forEach($scope.getbranchs, function(it) { users.push({ "id": it.id }); }) // var branch = { 'menu': menu }; // angular.extend(branch, { 'id': $scope.role.id, 'role': $scope.role.role, 'rolecode': $scope.role.rolecode, 'roletype': $scope.role.roletype, 'dashboard': $scope.role.dashboard }) var fildata = { "group": { "id": $scope.group.id, "groupName": $scope.group.groupName, "users": users, "duty":$scope.group.duty, "branch": $scope.group.branch } } // api_user_data.updData('group', fildata).then(function(response) { // if (response) { // if (response.status == 200) { api_user_data.updData('group', fildata).then(function(response) { if (response && response.status == 200) { $scope.try_async_load(); SweetAlert.swal({ title: "提交成功!", type: "success", confirmButtonColor: "#007AFF" }, function() { $rootScope.isMask = false; }); } else { SweetAlert.swal({ title: "系统错误", text: "系统错误,请稍后重试!", type: "error", confirmButtonColor: "#DD6B55" }, function() { $rootScope.isMask = false; }); } }) // $state.go('app.system.form',{formKey:'system_add_menu', service:'api_configure_data', model : JSON.stringify(modelData)}); } // //change menu // $scope.choiceMenu = []; // $scope.showHandler = function(branch) { // $scope.choiceMenu = branch; // } //add role $scope.addRole = function() { var modalInstance = $modal.open({ templateUrl: 'assets/views/system/tpl/group.html', controller: function($rootScope, $scope, scope, $modalInstance, api_user_data, api_category) { $scope.disableBranch = false; $scope.disableDuty = false; if($rootScope.user.duty){ $scope.disableBranch = true; $scope.disableDuty = true; }else if($rootScope.user.branch){ $scope.disableBranch = true; $scope.disableDuty = false; } $scope.group = {}; $scope.hospitalList = []; $scope.dutyDeptList = []; // 获取院区 $scope.getHospitals = function(){ //跨科室转派 api_category.getOwnBranch({}).then(function (data) { if(data.status == 200){ $scope.hospitalList = data.data || []; if($rootScope.user.duty){ // 当前的所属责任科室 $scope.group.branch = $scope.hospitalList.find(v=>v.id == $rootScope.user.duty.branch); }else if($rootScope.user.branch){ // 当前的所属院区 $scope.group.branch = $scope.hospitalList.find(v=>v.id == $rootScope.user.branch.id); } }else{ SweetAlert.swal({ title: "系统错误!", text: "请刷新重试!", type: "error" }); } }) } $scope.getHospitals(); // 修改院区 $scope.changeBranch = function(keyword=''){ $scope.group.duty = undefined; $scope.dutyDeptList = []; api_category.getOwnDuty({branchId: $scope.group.branch.id}).then(function (data) { if(data.status == 200){ $scope.dutyDeptList = data.data || []; if($rootScope.user.duty){ // 当前的所属责任科室 $scope.group.duty = $rootScope.user.duty; }else if($rootScope.user.branch){ // 当前的所属院区 $scope.group.duty = undefined; } }else{ SweetAlert.swal({ title: "系统错误!", text: "请刷新重试!", type: "error" }); } }) } $scope.cancel = function() { $modalInstance.dismiss('cancel'); }; $scope.ok = function(group) { var close = true; for (var i = 0; i < scope.my_data.length; i++) { if (scope.my_data[i].groupName == group.groupName) { close = false; $modalInstance.dismiss('cancel'); SweetAlert.swal({ title: "修改失败!", text: "该组已存在", type: "error" }) break; } else {} } if (close) { $modalInstance.close(group); } } }, resolve: { scope: function() { return $scope; } } }); modalInstance.result.then(function(selectedItem) { if (selectedItem) { if(!selectedItem.duty || !selectedItem.branch || !selectedItem.groupName){ SweetAlert.swal({ title: "操作失败!", text: "请填写必填项!", type: "error" }) return; } var fildata = { "group": { "groupName": selectedItem.groupName,duty:selectedItem.duty } } api_user_data.addData('group', fildata).then(function(response) { if (response) { if (response.status == 200) { SweetAlert.swal({ title: "新增成功!", type: "success" }, function() { $scope.try_async_load(); }) } else { SweetAlert.swal({ title: "修改失败!", type: "error" }) } // $modalInstance.close(); } }) } }) } //change role $scope.changeRole = function() { if($scope.group.id){ var modalInstance = $modal.open({ templateUrl: 'assets/views/system/tpl/group.html', controller: function($rootScope, $scope, scope, $modalInstance, api_user_data, api_category) { $scope.disableBranch = true; $scope.disableDuty = true; $scope.group = angular.copy(scope.group); $scope.hospitalList = []; $scope.dutyDeptList = []; // 获取院区 $scope.getHospitals = function(){ //跨科室转派 api_category.getOwnBranch({}).then(function (data) { if(data.status == 200){ $scope.hospitalList = data.data || []; $scope.group.branch = $scope.hospitalList.find(v=>v.id == scope.group.branch); }else{ SweetAlert.swal({ title: "系统错误!", text: "请刷新重试!", type: "error" }); } }) } $scope.getHospitals(); // 修改院区 $scope.changeBranch = function(keyword){ if(keyword === undefined){ $scope.group.duty = undefined; $scope.dutyDeptList = []; } api_category.getOwnDuty({branchId: scope.group.branch}).then(function (data) { if(data.status == 200){ $scope.dutyDeptList = data.data || []; $scope.group.duty = scope.group.duty; }else{ SweetAlert.swal({ title: "系统错误!", text: "请刷新重试!", type: "error" }); } }) } $scope.cancel = function() { $modalInstance.dismiss('cancel'); }; $scope.ok = function(item) { $modalInstance.close(item); } }, resolve: { scope: function() { return $scope; } } }); modalInstance.result.then(function(selectedItem) { if (selectedItem) { if(!selectedItem.duty || !selectedItem.branch || !selectedItem.groupName){ SweetAlert.swal({ title: "操作失败!", text: "请填写必填项!", type: "error" }) return; } var fildata = { "group": { "id": selectedItem.id, "groupName": selectedItem.groupName, "users": selectedItem.users,duty:selectedItem.duty, "branch": selectedItem.branch.id } } api_user_data.updData('group', fildata).then(function(response) { if (response) { if (response.status == 200) { SweetAlert.swal({ title: "修改成功!", type: "success" }, function() { $scope.try_async_load(); // $scope.refreshData('expand-right', defaultFilterData); }) } else { SweetAlert.swal({ title: "修改失败!", type: "error" }) } // $modalInstance.close(); } }) } }) }else{ SweetAlert.swal({ title: "操作失败", text: "请选择需要修改的项!", type: "error", confirmButtonColor: "#DD6B55" }); } } //remove role $scope.removeRole = function() { if(!$scope.group.id){ SweetAlert.swal({ title: "操作失败", text: "请选择需要修改的项!", type: "error", confirmButtonColor: "#DD6B55" }); return; } var modalInstance = $modal.open({ templateUrl: 'assets/views/delete.html', controller: function($scope, scope, $modalInstance) { var rmvList = []; $scope.title = '工作组删除'; $scope.connect = '确定要删除此工作组?'; rmvList.push(scope.group.id); $scope.ok = function() { $modalInstance.close(rmvList); }; $scope.cancel = function() { $modalInstance.dismiss('cancel'); }; }, size: 'sm', resolve: { scope: function() { return $scope; } } }); modalInstance.result.then(function(selectedItem) { if (selectedItem) { if (selectedItem.length > 0) { api_user_data.rmvData('group', selectedItem).then(function(response) { if (response.data) { SweetAlert.swal({ title: "删除成功!", type: "success", confirmButtonColor: "#007AFF" }, function() { $scope.try_async_load(); }); } else { SweetAlert.swal({ title: "操作异常!", text: "系统异常,请稍后重试,或者联系管理员!", type: "error" }); } }) } } }) } }]);