app.controller("inspectionScope", [ "$rootScope", "$scope", "$state", "$timeout", "$interval", "SweetAlert", "$modal", "Restangular", "api_bpm_data", "api_solution", "api_user_data", "api_wechatfile", "api_sysinfo", "api_statistic", "api_bpm", "jry_api_bpm", "api_simple", function ( $rootScope, $scope, $state, $timeout, $interval, SweetAlert, $modal, Restangular, api_bpm_data, api_solution, api_user_data, api_wechatfile, api_sysinfo, api_statistic, api_bpm, jry_api_bpm, api_simple ) { var loginUser = $rootScope.user; $scope.stateModel = JSON.parse($state.params.model); console.log($scope.stateModel); $scope.modelName = $scope.stateModel.modelName || $scope.stateModel.title; if ($scope.stateModel.model != 1) { $scope.stateModel.model = 0; } $scope.xinzeng = false; $scope.shanchu = false; $scope.bianji = false; for (var i = 0; i < loginUser.menu.length; i++) { if (loginUser.menu[i].link == "shijianshezhi_xinzeng") { $scope.xinzeng = true; } if (loginUser.menu[i].link == "shijianshezhi_shanchu") { $scope.shanchu = true; } if (loginUser.menu[i].link == "shijianshezhi_bianji") { $scope.bianji = true; } } $scope.listDataTop = []; $scope.listDataBottom = []; $scope.leftListIdx = -1; $scope.leftListName = "巡检范围"; $scope.leftListCli = function (idx, data) { $scope.leftListIdx = idx; $scope.stateModel = data; $scope.modelName = data.modelName; $scope.copyInspection.tree = []; $scope.selectedInspection.isShowZtree = false; setTimeout(() => { $scope.selectedInspection.isShowZtree = true; }, 0); }; // 事件分类开始 var loginUser = $rootScope.user; //组 api_user_data .fetchDataList("group", { idx: 0, sum: 1000 }) .then(function (data) { $scope.group = data.list; }); api_wechatfile .getDictionary({ key: "incident_priority", type: "list" }) .then(function (data) { $scope.priority = data; }); /* ----- tree----- */ var i = 0; $scope.out = []; $scope.outdata = []; $scope.subdata = {}; $scope.sub1data = {}; $scope.addcate = false; $scope.add1cate = false; $scope.changecate = false; $scope.filterTree = function (data, id) { data = angular.copy(data); var newTree = data.filter((x) => x.id !== id); newTree.forEach( (x) => x.children && (x.children = $scope.filterTree(x.children, id)) ); return newTree; }; $scope.userOrGroupType = {}; //当前节点运维组/运维人员 $scope.dataSave = []; //储存$scope.tree_data $scope.group_user = { // offlineGroup:{},//线下组 // onlineUser:{}//线上人 }; $scope.selectedInspection = { tree: {}, isShowZtree: true, }; $scope.copyInspection = { tree: [], }; $scope.timerZtree = null; $scope.timerZtreeCopy = null; // 新增 $scope.addType1 = function (type) { console.log(); // =============================================== var modalInstance = $modal.open({ templateUrl: "assets/views/system/tpl/inspectionchange.html", controller: function ($scope, scope, $modalInstance) { $scope.add1data = {}; $scope.thisParent = {}; //当前点击节点的父节点 $scope.filterTree = function (data, id) { data = angular.copy(data); var newTree = data.filter((x) => x.id !== id); newTree.forEach( (x) => x.children && (x.children = $scope.filterTree(x.children, id)) ); return newTree; }; $scope.try_async_1load = function () { $scope.my_1data = []; var data = { idx: 0, sum: 1000, inspectionType: { deleteFlag: 0 } }; api_bpm_data .fetchDataList("inspectionType", data) .then(function (response) { var data = response.list; data = data.filter((v) => !v.parent); //只显示一级 var objects = []; for (var i = 0; i < data.length; i++) { var object = {}; object.id = data[i].id; if (angular.isDefined(data[i].user)) { object.user = data[i].user || {}; } if (angular.isDefined(data[i].group)) { object.group = data[i].group || {}; } if (angular.isDefined(data[i].parent)) { object.parent = data[i].parent; } object.label = data[i].type; objects.push(object); } $scope.my_1data = convertParentToChildList(objects); $scope.tree_1data = angular.copy($scope.my_1data); // 把选中的巡检范围当作父类回显 if (scope.selectedInspection.tree.id) { // 区分新增和修改 if (type == "add") { $scope.thisParent = data.find( (v) => v.id == scope.selectedInspection.tree.id ); if ($scope.thisParent) { $scope.thisParent.label = $scope.thisParent.type; $scope.thisParent.isExpanded = true; $scope.thisParent.selected = true; $scope.addparent = true; } else { $scope.addparent = false; } } else if (type == "edit") { // 编辑 console.log($scope.my_1data, "原始数据"); $scope.tree_1data = $scope.my_1data.filter( (v) => v.id != scope.selectedInspection.tree.id ); //过滤本条 $scope.thisParent = data.find( (v) => v.id == scope.selectedInspection.tree.pId ); if ($scope.thisParent) { $scope.thisParent.label = $scope.thisParent.type; $scope.thisParent.isExpanded = true; $scope.thisParent.selected = true; $scope.addparent = true; } else { $scope.addparent = false; } $scope.add1data.type = scope.selectedInspection.tree.name; $scope.add1data.id = scope.selectedInspection.tree.id; } } else { $scope.addparent = false; } }); }; $scope.try_async_1load(); 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") { //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.title = type == "add" ? "巡检范围新增" : "巡检范围修改"; $scope.cancel = function () { $modalInstance.dismiss("cancel"); }; $scope.savercode = function (data) { if (data && data.type) { if ($scope.addparent) { data.parent = $scope.add1data.parent || $scope.thisParent; } else { data.parent = null; } scope.selectedInspection.tree = {}; $modalInstance.close(data); } else { SweetAlert.swal( { title: "新增失败!", text: "请填写必填项!", type: "error", confirmButtonColor: "#DD6B55", }, function () {} ); } }; }, resolve: { scope: function () { return $scope; }, }, }); // liaomingming modalInstance.result.then(function (selectedItem) { console.log($scope); if (selectedItem.type) { selectedItem.deleteFlag = 0; selectedItem.formUiName = "inspection_LinHu_detail"; selectedItem.formUiEdit = "inspection_LinHu_confirm"; selectedItem.processKey = "bpm_inspection"; var filedata = { inspectionType: selectedItem }; console.log(filedata); // return; api_bpm_data[type == "add" ? "addData" : "updData"]( "inspectionType", filedata ).then(function (response) { if (response) { if (response.status == 200) { $scope.selectedInspection.isShowZtree = false; setTimeout(() => { $scope.selectedInspection.isShowZtree = true; }, 0); SweetAlert.swal({ title: type == "add" ? "增加成功!" : "修改成功!", type: "success", }); } else { SweetAlert.swal({ title: type == "add" ? "增加失败!" : "修改失败!", type: "error", }); } } }); } else { SweetAlert.swal( { title: "新增失败!", text: "请填写必填项!", type: "error", confirmButtonColor: "#DD6B55", }, function () {} ); } }); }; /* -----start remove incidet category----- */ // 删除 $scope.remove1 = function () { var modalInstance = $modal.open({ templateUrl: "assets/views/delete.html", controller: function ($scope, scope, $modalInstance, api_bpm_data) { $scope.title = "巡检范围删除?"; $scope.connect = "确定要删除此巡检范围?"; $scope.ok = function () { $modalInstance.close(scope.selectedInspection.tree); }; $scope.cancel = function () { $modalInstance.dismiss("cancel"); }; }, size: "sm", resolve: { scope: function () { return $scope; }, }, }); modalInstance.result.then(function (selectedItem) { if (selectedItem) { var rmvList = []; rmvList.push(selectedItem.id); if (selectedItem.children && selectedItem.children.length > 0) { SweetAlert.swal({ title: "删除失败!", text: "请先删除选中的范围的子级", type: "error", }); } else { api_bpm_data .rmvData("inspectionType", rmvList) .then(function (response) { if (response.data) { SweetAlert.swal( { title: "删除成功!", type: "success", confirmButtonColor: "#007AFF", }, function () { $scope.selectedInspection.isShowZtree = false; setTimeout(() => { $scope.selectedInspection.isShowZtree = true; }, 0); } ); } else { SweetAlert.swal({ title: "操作异常!", text: "系统异常,请稍后重试,或者联系管理员!", type: "error", }); } }); } } }); }; /* -----end remove incidet category----- */ 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") { //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.closethis = function (formdata) { formdata.category = ""; formdata.parent.category = ""; }; /* -----start submit incidet category----- */ // 保存设置 $scope.selectedchangecategory = { value: {} }; $scope.$watch("selectedchangecategory.value", function (newVal) { console.log(newVal); if (newVal.user) { $scope.group_user["onlineUser"] = newVal.user || undefined; } else { $scope.group_user["onlineUser"] = undefined; } if (newVal.group) { $scope.group_user["offlineGroup"] = newVal.group || undefined; } else { $scope.group_user["offlineGroup"] = undefined; } }); $scope.editInspection = function (tree, id, newVal, type) { tree.forEach((v) => { if (v.id == id) { v[type] = newVal; } else if (v.children) { $scope.editInspection(v.children, id, newVal, type); } }); }; $scope.$watch("group_user.offlineGroup", function (newVal) { console.log($scope.selectedchangecategory.value); console.log($scope.copyInspection.tree); console.log(newVal, "group"); $scope.editInspection( $scope.copyInspection.tree, $scope.selectedchangecategory.value.id, newVal, "group" ); if (newVal && newVal.id) { $scope.selectedchangecategory.value.group = newVal; } else { delete $scope.selectedchangecategory.value.group; } }); $scope.$watch("group_user.onlineUser", function (newVal) { // console.log($scope.selectedchangecategory.value) // console.log($scope.copyInspection.tree) // console.log(newVal, 'user') $scope.editInspection( $scope.copyInspection.tree, $scope.selectedchangecategory.value.id, newVal, "user" ); if (newVal && newVal.id) { $scope.selectedchangecategory.value.user = newVal; } else { delete $scope.selectedchangecategory.value.user; } }); // 保存设置 liaomingming $scope.submit1change = function () { console.log($scope.copyInspection.tree); console.log($scope.stateModel); if ($scope.stateModel.model === 0) { // 不设置为模块的时候需要弹窗提示 var modalInstance = $modal.open({ templateUrl: "assets/views/system/tpl/inspectionModel.html", controller: function ($scope, scope, $modalInstance) { $scope.add1data = { model: false }; $scope.title = "提示"; $scope.cancel = function () { $modalInstance.dismiss("cancel"); }; $scope.savercode = function (data) { console.log(scope.copyInspection.tree); scope.stateModel.model = data.model ? 1 : 0; scope.saveCommon(scope); $modalInstance.close(data); }; }, resolve: { scope: function () { return $scope; }, }, }); modalInstance.result.then(function (selectedItem) {}); return; } $scope.saveCommon($scope); }; $scope.closeModel = function () { $state.go("app.inspection.inspectPlan"); //跳转巡检计划列表 }; $scope.TreeToArr = function (tree, arr) { tree.forEach((v) => { if (v.children) { arr.push(v); $scope.TreeToArr(v.children, arr); } else { arr.push(v); } }); return arr; }; $scope.saveCommon = function ($scope) { if (!$scope.modelName.trim()) { SweetAlert.swal({ title: "策略名称不能为空!", type: "error", }); return; } var arr = $scope.TreeToArr($scope.copyInspection.tree, []); $scope.stateModel.modelName = $scope.modelName; $scope.stateModel.typeIds = arr.map((v) => v.id).toString(); arr = arr.map((v) => ({ inspection: $scope.stateModel.id, inspectionType: v.id, inspectionTypeName: v.name, parent: v.parent, user: v.user?v.user.id:undefined, userName: v.user?v.user.name:undefined, group: v.group?v.group.id:undefined, groupName: v.group?v.group.groupName:undefined, })); // 一条都没选择 if (arr.length === 0) { arr.push({ inspection: $scope.stateModel.id }); } console.log(arr); // return; api_simple.addListData("inspectionBind", arr).then(function (response) { if (response) { if (response.status == 200) { $scope.stateModel.status = { id: 120 }; //强制扭转状态为正常 var filedata = { close: "关闭", save: "暂存", submit: "下一步", inspection: $scope.stateModel, }; api_bpm_data .addData("inspection", filedata) .then(function (response) { if (response) { if (response.status == 200) { SweetAlert.swal( { title: "保存成功!", type: "success", }, function () { // $scope.selectedchangecategory.value = {}; // $scope.copyInspection.tree = []; // $scope.changecateObj.changecate = false; // $scope.selectedInspection.isShowZtree = false; // setTimeout(()=>{ // $scope.selectedInspection.isShowZtree = true; // },0) $state.go("app.inspection.inspectPlan"); //跳转巡检计划列表 } ); } else { SweetAlert.swal({ title: "保存失败!", type: "error", }); } } }); } else { SweetAlert.swal({ title: "保存失败!", type: "error", }); } } }); }; $scope.add1data = {}; $scope.addparent = false; // 事件分类结束 // 获取组数据 $scope.getGroupList = function () { var postData = { idx: 0, sum: 1000, group: { selectType: "nouser" }, }; api_user_data.fetchDataList("group", postData).then((res) => { $scope.groupList = res.list; }); }; $scope.getGroupList(); // 获取巡检计划数据 $scope.getGroupList = function (model) { var postData = { idx: 0, sum: 3, inspection: { model: model }, }; api_user_data.fetchDataList("inspection", postData).then((res) => { if (model == 1) { $scope.listDataTop = res.list; //巡检范围策略模板 } else { $scope.listDataBottom = res.list; //往期巡检范围策略 } }); }; $scope.getGroupList(1); $scope.getGroupList(); // 获取人数据 $scope.getUserList = function () { var postData = { idx: 0, sum: 1000, user: { name: "", selectType: "pinyin_qs", }, }; api_user_data.fetchDataList("user", postData).then((res) => { $scope.userList = res.list; }); }; $scope.getUserList(); $scope.changecateObj = { changecate: false, }; // 缓存的绑定数据-http $scope.cacheHttp = { value: [], }; }, ]); app.directive("zTree", function () { // ztree liaomingming return { restrict: "EA", scope: { selectInspectionType: "=", copyInspectionType: "=", stateModel: "=", changecate: "=", cachehttp: "=", }, controller: function ($scope, $element, api_bpm_data, api_simple) { console.log($scope); getTreeInfo(); function getTreeInfo() { $scope.treeInfo = []; var data = { idx: 0, sum: 1000, inspectionType: { deleteFlag: 0 } }; $scope.isShowZtree = false; api_bpm_data .fetchDataList("inspectionType", data) .then(function (response) { var data = response.list; console.log(data); var objects = []; for (var i = 0; i < data.length; i++) { var object = {}; object.id = data[i].id; if (angular.isDefined(data[i].user)) { object.user = data[i].user || {}; } if (angular.isDefined(data[i].group)) { object.group = data[i].group || {}; } if (angular.isDefined(data[i].parent)) { object.parent = data[i].parent; object.pId = data[i].parent.id; } else { object.pId = 0; } object.name = data[i].type; objects.push(object); } $scope.treeInfo = convertParentToChildList(objects); $scope.treeInfo = deleteEmputyChildren($scope.treeInfo); console.log($scope.treeInfo); // ==================start================ api_simple .fetchDataList("inspectionBind", { idx: 0, sum: 1000, inspectionBind: { inspection: $scope.stateModel.id }, }) .then(function (response) { $.fn.zTree.destroy("z-tree"); $.fn.zTree.init($($element), setting, $scope.treeInfo); $scope.isShowZtree = true; var result = response.list || []; $scope.cachehttp = angular.copy(result); result = result.map((v) => v.inspectionType); console.log(result); result.forEach((v, i) => { // 设置路径树节点选中 // 1、根据id获取树的某个节点: var zTree = $.fn.zTree.getZTreeObj("z-tree"); var node = zTree.getNodeByParam("id", v); // 2、设置node节点选中状态: zTree.selectNode(node); // 3、设置node节点checked选中: node.checked = true; //注:设置checked属性之后,一定要更新该节点 zTree.updateNode(node); // 展开 zTree.expandNode(node); setTimeout(() => { zTreeOnCheck(null, null, node); }, 0); }); }); // ==================end================ }); } function deleteEmputyChildren(data) { data.forEach((v) => { if (v.children.length) { deleteEmputyChildren(v.children); } else { delete v.children; } }); return data; } function convertParentToChildList(data) { var treeMap = {}; var list = []; convertListToTree(data, treeMap); angular.forEach(treeMap, function (item) { list.push(item); }); return list; } 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") { if (datum.pId === 0) { //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]; parentNode = idToNodeMap[datum.pId]; //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; } var setting = { check: { enable: true, chkStyle: "checkbox", chkboxType: { Y: "ps", N: "ps" }, }, callback: { onClick: onMouseDown, onCheck: zTreeOnCheck, }, view: { showIcon: false, showLine: false, }, }; // $.fn.zTree.init($($element), setting, $scope.treeInfo); //点击菜单项 function onMouseDown(event, treeId, treeNode) { console.log(event, treeId, treeNode); $scope.selectInspectionType = treeNode; } // 点击复选框 function zTreeOnCheck(event, treeId, treeNode) { console.log(event, treeId, treeNode); $scope.changecate = false; var treeObj = $.fn.zTree.getZTreeObj("z-tree"); var nodes_arr = treeObj.getCheckedNodes(true); var nodes = angular.copy(nodes_arr); console.log(nodes); var arr = nodes.filter((v) => v.pId === 0); arr.forEach((v) => { v.children = []; }); arr.forEach((v, i) => { nodes.forEach((vv) => { if (vv.pId == v.id) { arr[i].children.push(vv); } }); }); if ($scope.cachehttp && $scope.cachehttp.length) { var result = angular.copy($scope.cachehttp); zTreeOnCheckCommon(result, nodes); } else { api_simple .fetchDataList("inspectionBind", { idx: 0, sum: 1000, inspectionBind: { inspection: $scope.stateModel.id }, }) .then(function (response) { var result = response.list || []; zTreeOnCheckCommon(result, nodes); }); } } // 点击复选框-common function zTreeOnCheckCommon(result, nodes) { // 如果有值,则不覆盖 if($scope.copyInspectionType.length){ result = []; $scope.copyInspectionType.forEach(v=>{ result.push(v); if(v.children && v.children.length){ v.children.forEach(vv=>{ result.push(vv); }) } }) }else{ result = result.map((v) => ({ id: v.inspectionType, name: v.inspectionTypeName, group: {id:v.group,groupName:v.groupName}, user: {id:v.user,name:v.userName}, parent: v.parent ? { id: v.parent.inspectionType } : undefined, pId: v.parent ? v.parent.inspectionType : 0, })); } for (var i = 0; i < nodes.length; i++) { for (var j = 0; j < result.length; j++) { if (nodes[i].id == result[j].id) { nodes[i] = angular.copy(result[j]); } } } var arr = nodes.filter((v) => v.pId === 0); arr.forEach((v) => { v.children = []; }); arr.forEach((v, i) => { nodes.forEach((vv) => { if (vv.pId == v.id) { arr[i].children.push(vv); } }); }); console.log(arr); $scope.copyInspectionType = arr; } }, }; }); app.directive("zTreeCopy", function () { // ztree liaomingming return { restrict: "EA", scope: { copyInspectionType: "=", changecate: "=", selectedchangecategory: "=", }, link: function (scope, element, attrs) { scope.$watch("copyInspectionType", function (newVal) { console.log(newVal); var setting = { view: { showIcon: false, showLine: false, }, callback: { onClick: onMouseDown, }, }; $.fn.zTree.destroy("z-tree-copy"); var treeObj = $.fn.zTree.init($(element), setting, newVal); treeObj.expandAll(true); }); //点击菜单项 function onMouseDown(event, treeId, treeNode) { console.log(event, treeId, treeNode); scope.selectedchangecategory = treeNode; scope.changecate = true; console.log(scope.copyInspectionType); } }, controller: function ($scope, $element, api_bpm_data) {}, }; });