inspectionScopeCtrl.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. app.controller("inspectionScope", [
  2. "$rootScope",
  3. "$scope",
  4. "$state",
  5. "$timeout",
  6. "$interval",
  7. "SweetAlert",
  8. "$modal",
  9. "Restangular",
  10. "api_bpm_data",
  11. "api_solution",
  12. "api_user_data",
  13. "api_wechatfile",
  14. "api_sysinfo",
  15. "api_statistic",
  16. "api_bpm",
  17. "jry_api_bpm",
  18. "api_simple",
  19. function (
  20. $rootScope,
  21. $scope,
  22. $state,
  23. $timeout,
  24. $interval,
  25. SweetAlert,
  26. $modal,
  27. Restangular,
  28. api_bpm_data,
  29. api_solution,
  30. api_user_data,
  31. api_wechatfile,
  32. api_sysinfo,
  33. api_statistic,
  34. api_bpm,
  35. jry_api_bpm,
  36. api_simple
  37. ) {
  38. var loginUser = $rootScope.user;
  39. $scope.stateModel = JSON.parse($state.params.model);
  40. console.log($scope.stateModel);
  41. $scope.modelName = $scope.stateModel.modelName || $scope.stateModel.title;
  42. if ($scope.stateModel.model != 1) {
  43. $scope.stateModel.model = 0;
  44. }
  45. $scope.xinzeng = false;
  46. $scope.shanchu = false;
  47. $scope.bianji = false;
  48. for (var i = 0; i < loginUser.menu.length; i++) {
  49. if (loginUser.menu[i].link == "shijianshezhi_xinzeng") {
  50. $scope.xinzeng = true;
  51. }
  52. if (loginUser.menu[i].link == "shijianshezhi_shanchu") {
  53. $scope.shanchu = true;
  54. }
  55. if (loginUser.menu[i].link == "shijianshezhi_bianji") {
  56. $scope.bianji = true;
  57. }
  58. }
  59. $scope.listDataTop = [];
  60. $scope.listDataBottom = [];
  61. $scope.leftListIdx = -1;
  62. $scope.leftListName = "巡检范围";
  63. $scope.leftListCli = function (idx, data) {
  64. $scope.leftListIdx = idx;
  65. $scope.stateModel = data;
  66. $scope.modelName = data.modelName;
  67. $scope.copyInspection.tree = [];
  68. $scope.selectedInspection.isShowZtree = false;
  69. setTimeout(() => {
  70. $scope.selectedInspection.isShowZtree = true;
  71. }, 0);
  72. };
  73. // 事件分类开始
  74. var loginUser = $rootScope.user;
  75. //组
  76. api_user_data
  77. .fetchDataList("group", { idx: 0, sum: 1000 })
  78. .then(function (data) {
  79. $scope.group = data.list;
  80. });
  81. api_wechatfile
  82. .getDictionary({ key: "incident_priority", type: "list" })
  83. .then(function (data) {
  84. $scope.priority = data;
  85. });
  86. /* ----- tree----- */
  87. var i = 0;
  88. $scope.out = [];
  89. $scope.outdata = [];
  90. $scope.subdata = {};
  91. $scope.sub1data = {};
  92. $scope.addcate = false;
  93. $scope.add1cate = false;
  94. $scope.changecate = false;
  95. $scope.filterTree = function (data, id) {
  96. data = angular.copy(data);
  97. var newTree = data.filter((x) => x.id !== id);
  98. newTree.forEach(
  99. (x) => x.children && (x.children = $scope.filterTree(x.children, id))
  100. );
  101. return newTree;
  102. };
  103. $scope.userOrGroupType = {}; //当前节点运维组/运维人员
  104. $scope.dataSave = []; //储存$scope.tree_data
  105. $scope.group_user = {
  106. // offlineGroup:{},//线下组
  107. // onlineUser:{}//线上人
  108. };
  109. $scope.selectedInspection = {
  110. tree: {},
  111. isShowZtree: true,
  112. };
  113. $scope.copyInspection = {
  114. tree: [],
  115. };
  116. $scope.timerZtree = null;
  117. $scope.timerZtreeCopy = null;
  118. // 新增
  119. $scope.addType1 = function (type) {
  120. console.log();
  121. // ===============================================
  122. var modalInstance = $modal.open({
  123. templateUrl: "assets/views/system/tpl/inspectionchange.html",
  124. controller: function ($scope, scope, $modalInstance) {
  125. $scope.add1data = {};
  126. $scope.thisParent = {}; //当前点击节点的父节点
  127. $scope.filterTree = function (data, id) {
  128. data = angular.copy(data);
  129. var newTree = data.filter((x) => x.id !== id);
  130. newTree.forEach(
  131. (x) =>
  132. x.children && (x.children = $scope.filterTree(x.children, id))
  133. );
  134. return newTree;
  135. };
  136. $scope.try_async_1load = function () {
  137. $scope.my_1data = [];
  138. var data = { idx: 0, sum: 1000, inspectionType: { deleteFlag: 0 } };
  139. api_bpm_data
  140. .fetchDataList("inspectionType", data)
  141. .then(function (response) {
  142. var data = response.list;
  143. data = data.filter((v) => !v.parent); //只显示一级
  144. var objects = [];
  145. for (var i = 0; i < data.length; i++) {
  146. var object = {};
  147. object.id = data[i].id;
  148. if (angular.isDefined(data[i].user)) {
  149. object.user = data[i].user || {};
  150. }
  151. if (angular.isDefined(data[i].group)) {
  152. object.group = data[i].group || {};
  153. }
  154. if (angular.isDefined(data[i].parent)) {
  155. object.parent = data[i].parent;
  156. }
  157. object.label = data[i].type;
  158. objects.push(object);
  159. }
  160. $scope.my_1data = convertParentToChildList(objects);
  161. $scope.tree_1data = angular.copy($scope.my_1data);
  162. // 把选中的巡检范围当作父类回显
  163. if (scope.selectedInspection.tree.id) {
  164. // 区分新增和修改
  165. if (type == "add") {
  166. $scope.thisParent = data.find(
  167. (v) => v.id == scope.selectedInspection.tree.id
  168. );
  169. if ($scope.thisParent) {
  170. $scope.thisParent.label = $scope.thisParent.type;
  171. $scope.thisParent.isExpanded = true;
  172. $scope.thisParent.selected = true;
  173. $scope.addparent = true;
  174. } else {
  175. $scope.addparent = false;
  176. }
  177. } else if (type == "edit") {
  178. // 编辑
  179. console.log($scope.my_1data, "原始数据");
  180. $scope.tree_1data = $scope.my_1data.filter(
  181. (v) => v.id != scope.selectedInspection.tree.id
  182. ); //过滤本条
  183. $scope.thisParent = data.find(
  184. (v) => v.id == scope.selectedInspection.tree.pId
  185. );
  186. if ($scope.thisParent) {
  187. $scope.thisParent.label = $scope.thisParent.type;
  188. $scope.thisParent.isExpanded = true;
  189. $scope.thisParent.selected = true;
  190. $scope.addparent = true;
  191. } else {
  192. $scope.addparent = false;
  193. }
  194. $scope.add1data.type = scope.selectedInspection.tree.name;
  195. $scope.add1data.id = scope.selectedInspection.tree.id;
  196. }
  197. } else {
  198. $scope.addparent = false;
  199. }
  200. });
  201. };
  202. $scope.try_async_1load();
  203. function convertListToTree(data, treeMap) {
  204. var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
  205. var root = null; //Initially set our loop to null
  206. //loop over data
  207. for (var i = 0; i < data.length; i++) {
  208. var datum = data[i];
  209. //each node will have children, so let's give it a "children" poperty
  210. datum.children = [];
  211. //add an entry for this node to the map so that any future children can
  212. //lookup the parent
  213. idToNodeMap[datum.id] = datum;
  214. //Does this node have a parent?
  215. // console.log("datum="+JSON.stringify(datum))
  216. if (typeof datum.parent === "undefined") {
  217. //Doesn't look like it, so this node is the root of the tree
  218. root = datum;
  219. treeMap[datum.id] = root;
  220. } else {
  221. //This node has a parent, so let's look it up using the id
  222. parentNode = idToNodeMap[datum.parent.id];
  223. //We don't need this property, so let's delete it.
  224. // delete datum.parent;
  225. //Let's add the current node as a child of the parent node.
  226. parentNode.children.push(datum);
  227. }
  228. }
  229. return root;
  230. }
  231. function convertParentToChildList(data) {
  232. var treeMap = {};
  233. var list = [];
  234. convertListToTree(data, treeMap);
  235. angular.forEach(treeMap, function (item) {
  236. list.push(item);
  237. });
  238. return list;
  239. }
  240. // ------------------------------------------------
  241. $scope.title = type == "add" ? "巡检范围新增" : "巡检范围修改";
  242. $scope.cancel = function () {
  243. $modalInstance.dismiss("cancel");
  244. };
  245. $scope.savercode = function (data) {
  246. if (data && data.type) {
  247. if ($scope.addparent) {
  248. data.parent = $scope.add1data.parent || $scope.thisParent;
  249. } else {
  250. data.parent = null;
  251. }
  252. scope.selectedInspection.tree = {};
  253. $modalInstance.close(data);
  254. } else {
  255. SweetAlert.swal(
  256. {
  257. title: "新增失败!",
  258. text: "请填写必填项!",
  259. type: "error",
  260. confirmButtonColor: "#DD6B55",
  261. },
  262. function () {}
  263. );
  264. }
  265. };
  266. },
  267. resolve: {
  268. scope: function () {
  269. return $scope;
  270. },
  271. },
  272. });
  273. // liaomingming
  274. modalInstance.result.then(function (selectedItem) {
  275. console.log($scope);
  276. if (selectedItem.type) {
  277. selectedItem.deleteFlag = 0;
  278. selectedItem.formUiName = "inspection_LinHu_detail";
  279. selectedItem.formUiEdit = "inspection_LinHu_confirm";
  280. selectedItem.processKey = "bpm_inspection";
  281. var filedata = { inspectionType: selectedItem };
  282. console.log(filedata);
  283. // return;
  284. api_bpm_data[type == "add" ? "addData" : "updData"](
  285. "inspectionType",
  286. filedata
  287. ).then(function (response) {
  288. if (response) {
  289. if (response.status == 200) {
  290. $scope.selectedInspection.isShowZtree = false;
  291. setTimeout(() => {
  292. $scope.selectedInspection.isShowZtree = true;
  293. }, 0);
  294. SweetAlert.swal({
  295. title: type == "add" ? "增加成功!" : "修改成功!",
  296. type: "success",
  297. });
  298. } else {
  299. SweetAlert.swal({
  300. title: type == "add" ? "增加失败!" : "修改失败!",
  301. type: "error",
  302. });
  303. }
  304. }
  305. });
  306. } else {
  307. SweetAlert.swal(
  308. {
  309. title: "新增失败!",
  310. text: "请填写必填项!",
  311. type: "error",
  312. confirmButtonColor: "#DD6B55",
  313. },
  314. function () {}
  315. );
  316. }
  317. });
  318. };
  319. /* -----start remove incidet category----- */
  320. // 删除
  321. $scope.remove1 = function () {
  322. var modalInstance = $modal.open({
  323. templateUrl: "assets/views/delete.html",
  324. controller: function ($scope, scope, $modalInstance, api_bpm_data) {
  325. $scope.title = "巡检范围删除?";
  326. $scope.connect = "确定要删除此巡检范围?";
  327. $scope.ok = function () {
  328. $modalInstance.close(scope.selectedInspection.tree);
  329. };
  330. $scope.cancel = function () {
  331. $modalInstance.dismiss("cancel");
  332. };
  333. },
  334. size: "sm",
  335. resolve: {
  336. scope: function () {
  337. return $scope;
  338. },
  339. },
  340. });
  341. modalInstance.result.then(function (selectedItem) {
  342. if (selectedItem) {
  343. var rmvList = [];
  344. rmvList.push(selectedItem.id);
  345. if (selectedItem.children && selectedItem.children.length > 0) {
  346. SweetAlert.swal({
  347. title: "删除失败!",
  348. text: "请先删除选中的范围的子级",
  349. type: "error",
  350. });
  351. } else {
  352. api_bpm_data
  353. .rmvData("inspectionType", rmvList)
  354. .then(function (response) {
  355. if (response.data) {
  356. SweetAlert.swal(
  357. {
  358. title: "删除成功!",
  359. type: "success",
  360. confirmButtonColor: "#007AFF",
  361. },
  362. function () {
  363. $scope.selectedInspection.isShowZtree = false;
  364. setTimeout(() => {
  365. $scope.selectedInspection.isShowZtree = true;
  366. }, 0);
  367. }
  368. );
  369. } else {
  370. SweetAlert.swal({
  371. title: "操作异常!",
  372. text: "系统异常,请稍后重试,或者联系管理员!",
  373. type: "error",
  374. });
  375. }
  376. });
  377. }
  378. }
  379. });
  380. };
  381. /* -----end remove incidet category----- */
  382. function convertListToTree(data, treeMap) {
  383. var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
  384. var root = null; //Initially set our loop to null
  385. //loop over data
  386. for (var i = 0; i < data.length; i++) {
  387. var datum = data[i];
  388. //each node will have children, so let's give it a "children" poperty
  389. datum.children = [];
  390. //add an entry for this node to the map so that any future children can
  391. //lookup the parent
  392. idToNodeMap[datum.id] = datum;
  393. //Does this node have a parent?
  394. // console.log("datum="+JSON.stringify(datum))
  395. if (typeof datum.parent === "undefined") {
  396. //Doesn't look like it, so this node is the root of the tree
  397. root = datum;
  398. treeMap[datum.id] = root;
  399. } else {
  400. //This node has a parent, so let's look it up using the id
  401. parentNode = idToNodeMap[datum.parent.id];
  402. //We don't need this property, so let's delete it.
  403. // delete datum.parent;
  404. //Let's add the current node as a child of the parent node.
  405. parentNode.children.push(datum);
  406. }
  407. }
  408. return root;
  409. }
  410. function convertParentToChildList(data) {
  411. var treeMap = {};
  412. var list = [];
  413. convertListToTree(data, treeMap);
  414. angular.forEach(treeMap, function (item) {
  415. list.push(item);
  416. });
  417. return list;
  418. }
  419. $scope.closethis = function (formdata) {
  420. formdata.category = "";
  421. formdata.parent.category = "";
  422. };
  423. /* -----start submit incidet category----- */
  424. // 保存设置
  425. $scope.selectedchangecategory = { value: {} };
  426. $scope.$watch("selectedchangecategory.value", function (newVal) {
  427. console.log(newVal);
  428. if (newVal.user) {
  429. $scope.group_user["onlineUser"] = newVal.user || undefined;
  430. } else {
  431. $scope.group_user["onlineUser"] = undefined;
  432. }
  433. if (newVal.group) {
  434. $scope.group_user["offlineGroup"] = newVal.group || undefined;
  435. } else {
  436. $scope.group_user["offlineGroup"] = undefined;
  437. }
  438. });
  439. $scope.editInspection = function (tree, id, newVal, type) {
  440. tree.forEach((v) => {
  441. if (v.id == id) {
  442. v[type] = newVal;
  443. } else if (v.children) {
  444. $scope.editInspection(v.children, id, newVal, type);
  445. }
  446. });
  447. };
  448. $scope.$watch("group_user.offlineGroup", function (newVal) {
  449. console.log($scope.selectedchangecategory.value);
  450. console.log($scope.copyInspection.tree);
  451. console.log(newVal, "group");
  452. $scope.editInspection(
  453. $scope.copyInspection.tree,
  454. $scope.selectedchangecategory.value.id,
  455. newVal,
  456. "group"
  457. );
  458. if (newVal && newVal.id) {
  459. $scope.selectedchangecategory.value.group = newVal;
  460. } else {
  461. delete $scope.selectedchangecategory.value.group;
  462. }
  463. });
  464. $scope.$watch("group_user.onlineUser", function (newVal) {
  465. // console.log($scope.selectedchangecategory.value)
  466. // console.log($scope.copyInspection.tree)
  467. // console.log(newVal, 'user')
  468. $scope.editInspection(
  469. $scope.copyInspection.tree,
  470. $scope.selectedchangecategory.value.id,
  471. newVal,
  472. "user"
  473. );
  474. if (newVal && newVal.id) {
  475. $scope.selectedchangecategory.value.user = newVal;
  476. } else {
  477. delete $scope.selectedchangecategory.value.user;
  478. }
  479. });
  480. // 保存设置 liaomingming
  481. $scope.submit1change = function () {
  482. console.log($scope.copyInspection.tree);
  483. console.log($scope.stateModel);
  484. if ($scope.stateModel.model === 0) {
  485. // 不设置为模块的时候需要弹窗提示
  486. var modalInstance = $modal.open({
  487. templateUrl: "assets/views/system/tpl/inspectionModel.html",
  488. controller: function ($scope, scope, $modalInstance) {
  489. $scope.add1data = { model: false };
  490. $scope.title = "提示";
  491. $scope.cancel = function () {
  492. $modalInstance.dismiss("cancel");
  493. };
  494. $scope.savercode = function (data) {
  495. console.log(scope.copyInspection.tree);
  496. scope.stateModel.model = data.model ? 1 : 0;
  497. scope.saveCommon(scope);
  498. $modalInstance.close(data);
  499. };
  500. },
  501. resolve: {
  502. scope: function () {
  503. return $scope;
  504. },
  505. },
  506. });
  507. modalInstance.result.then(function (selectedItem) {});
  508. return;
  509. }
  510. $scope.saveCommon($scope);
  511. };
  512. // 暂存
  513. $scope.submit1change_save = function (flag) {
  514. $rootScope.isMask = true;
  515. var arr = $scope.TreeToArr($scope.copyInspection.tree, []);
  516. $scope.stateModel.modelName = $scope.modelName;
  517. $scope.stateModel.typeIds = arr.map((v) => v.id).toString();
  518. arr = arr.map((v) => ({
  519. inspection: $scope.stateModel.id,
  520. inspectionType: v.id,
  521. inspectionTypeName: v.name,
  522. parent: v.parent,
  523. user: v.user?v.user.id:undefined,
  524. userName: v.user?v.user.name:undefined,
  525. group: v.group?v.group.id:undefined,
  526. groupName: v.group?v.group.groupName:undefined,
  527. }));
  528. // 一条都没选择
  529. if (arr.length === 0) {
  530. arr.push({ inspection: $scope.stateModel.id });
  531. }
  532. console.log(arr);
  533. // return;
  534. api_simple.addListData("inspectionBind", arr).then(function (response) {
  535. $rootScope.isMask = false;
  536. if (response) {
  537. if (response.status == 200) {
  538. SweetAlert.swal(
  539. {
  540. title: "暂存成功!",
  541. type: "success",
  542. },
  543. function () {
  544. if(flag){
  545. window.history.back();
  546. }else{
  547. $scope.selectedchangecategory.value = {};
  548. $scope.copyInspection.tree = [];
  549. $scope.changecateObj.changecate = false;
  550. $scope.selectedInspection.isShowZtree = false;
  551. setTimeout(()=>{
  552. $scope.selectedInspection.isShowZtree = true;
  553. },0)
  554. }
  555. }
  556. );
  557. } else {
  558. SweetAlert.swal({
  559. title: "保存失败!",
  560. type: "error",
  561. });
  562. }
  563. }
  564. });
  565. };
  566. // 上一步
  567. $scope.submit1change_prev = function(){
  568. SweetAlert.swal(
  569. {
  570. title: "您是否暂存当前设置!",
  571. type: "warning",
  572. showCancelButton: true,
  573. confirmButtonColor: "#DD6B55",
  574. confirmButtonText: "是",
  575. cancelButtonText: "否",
  576. },
  577. function (isConfirm) {
  578. if (isConfirm) {
  579. $scope.submit1change_save(true);
  580. }
  581. }
  582. );
  583. }
  584. $scope.closeModel = function () {
  585. $state.go("app.inspection.inspectPlan"); //跳转巡检计划列表
  586. };
  587. $scope.TreeToArr = function (tree, arr) {
  588. tree.forEach((v) => {
  589. if (v.children) {
  590. arr.push(v);
  591. $scope.TreeToArr(v.children, arr);
  592. } else {
  593. arr.push(v);
  594. }
  595. });
  596. return arr;
  597. };
  598. $scope.saveCommon = function ($scope) {
  599. if (!$scope.modelName.trim()) {
  600. SweetAlert.swal({
  601. title: "策略名称不能为空!",
  602. type: "error",
  603. });
  604. return;
  605. }
  606. $rootScope.isMask = true;
  607. var arr = $scope.TreeToArr($scope.copyInspection.tree, []);
  608. $scope.stateModel.modelName = $scope.modelName;
  609. $scope.stateModel.typeIds = arr.map((v) => v.id).toString();
  610. arr = arr.map((v) => ({
  611. inspection: $scope.stateModel.id,
  612. inspectionType: v.id,
  613. inspectionTypeName: v.name,
  614. parent: v.parent,
  615. user: v.user?v.user.id:undefined,
  616. userName: v.user?v.user.name:undefined,
  617. group: v.group?v.group.id:undefined,
  618. groupName: v.group?v.group.groupName:undefined,
  619. }));
  620. // 一条都没选择
  621. if (arr.length === 0) {
  622. arr.push({ inspection: $scope.stateModel.id });
  623. }
  624. console.log(arr);
  625. // return;
  626. api_simple.addListData("inspectionBind", arr).then(function (response) {
  627. $rootScope.isMask = false;
  628. if (response) {
  629. if (response.status == 200) {
  630. $scope.stateModel.status = { id: 120 }; //强制扭转状态为正常
  631. var filedata = {
  632. close: "关闭",
  633. save: "暂存",
  634. submit: "下一步",
  635. inspection: $scope.stateModel,
  636. };
  637. api_bpm_data
  638. .addData("inspection", filedata)
  639. .then(function (response) {
  640. if (response) {
  641. if (response.status == 200) {
  642. SweetAlert.swal(
  643. {
  644. title: "保存成功!",
  645. type: "success",
  646. },
  647. function () {
  648. // $scope.selectedchangecategory.value = {};
  649. // $scope.copyInspection.tree = [];
  650. // $scope.changecateObj.changecate = false;
  651. // $scope.selectedInspection.isShowZtree = false;
  652. // setTimeout(()=>{
  653. // $scope.selectedInspection.isShowZtree = true;
  654. // },0)
  655. $state.go("app.inspection.inspectPlan"); //跳转巡检计划列表
  656. }
  657. );
  658. } else {
  659. SweetAlert.swal({
  660. title: "保存失败!",
  661. type: "error",
  662. });
  663. }
  664. }
  665. });
  666. } else {
  667. SweetAlert.swal({
  668. title: "保存失败!",
  669. type: "error",
  670. });
  671. }
  672. }
  673. });
  674. };
  675. $scope.add1data = {};
  676. $scope.addparent = false;
  677. // 事件分类结束
  678. // 获取组数据
  679. $scope.getGroupList = function () {
  680. var postData = {
  681. idx: 0,
  682. sum: 1000,
  683. group: { selectType: "nouser" },
  684. };
  685. api_user_data.fetchDataList("group", postData).then((res) => {
  686. $scope.groupList = res.list;
  687. });
  688. };
  689. $scope.getGroupList();
  690. // 获取巡检计划数据
  691. $scope.getGroupList = function (model) {
  692. var postData = {
  693. idx: 0,
  694. sum: 3,
  695. inspection: { model: model },
  696. };
  697. api_user_data.fetchDataList("inspection", postData).then((res) => {
  698. if (model == 1) {
  699. $scope.listDataTop = res.list; //巡检范围策略模板
  700. } else {
  701. $scope.listDataBottom = res.list; //往期巡检范围策略
  702. }
  703. });
  704. };
  705. $scope.getGroupList(1);
  706. $scope.getGroupList();
  707. // 获取人数据
  708. $scope.getUserList = function () {
  709. var postData = {
  710. idx: 0,
  711. sum: 1000,
  712. user: {
  713. name: "",
  714. selectType: "pinyin_qs",
  715. },
  716. };
  717. api_user_data.fetchDataList("user", postData).then((res) => {
  718. $scope.userList = res.list;
  719. });
  720. };
  721. $scope.getUserList();
  722. $scope.changecateObj = {
  723. changecate: false,
  724. };
  725. // 缓存的绑定数据-http
  726. $scope.cacheHttp = {
  727. value: [],
  728. };
  729. },
  730. ]);
  731. app.directive("zTree", function () {
  732. // ztree liaomingming
  733. return {
  734. restrict: "EA",
  735. scope: {
  736. selectInspectionType: "=",
  737. copyInspectionType: "=",
  738. stateModel: "=",
  739. changecate: "=",
  740. cachehttp: "=",
  741. },
  742. controller: function ($scope, $element, api_bpm_data, api_simple) {
  743. console.log($scope);
  744. getTreeInfo();
  745. function getTreeInfo() {
  746. $scope.treeInfo = [];
  747. var data = { idx: 0, sum: 1000, inspectionType: { deleteFlag: 0 } };
  748. $scope.isShowZtree = false;
  749. api_bpm_data
  750. .fetchDataList("inspectionType", data)
  751. .then(function (response) {
  752. var data = response.list;
  753. console.log(data);
  754. var objects = [];
  755. for (var i = 0; i < data.length; i++) {
  756. var object = {};
  757. object.id = data[i].id;
  758. if (angular.isDefined(data[i].user)) {
  759. object.user = data[i].user || {};
  760. }
  761. if (angular.isDefined(data[i].group)) {
  762. object.group = data[i].group || {};
  763. }
  764. if (angular.isDefined(data[i].parent)) {
  765. object.parent = data[i].parent;
  766. object.pId = data[i].parent.id;
  767. } else {
  768. object.pId = 0;
  769. }
  770. object.name = data[i].type;
  771. objects.push(object);
  772. }
  773. $scope.treeInfo = convertParentToChildList(objects);
  774. $scope.treeInfo = deleteEmputyChildren($scope.treeInfo);
  775. console.log($scope.treeInfo);
  776. // ==================start================
  777. api_simple
  778. .fetchDataList("inspectionBind", {
  779. idx: 0,
  780. sum: 1000,
  781. inspectionBind: { inspection: $scope.stateModel.id },
  782. })
  783. .then(function (response) {
  784. $.fn.zTree.destroy("z-tree");
  785. $.fn.zTree.init($($element), setting, $scope.treeInfo);
  786. $scope.isShowZtree = true;
  787. var result = response.list || [];
  788. $scope.cachehttp = angular.copy(result);
  789. result = result.map((v) => v.inspectionType);
  790. console.log(result);
  791. result.forEach((v, i) => {
  792. // 设置路径树节点选中
  793. // 1、根据id获取树的某个节点:
  794. var zTree = $.fn.zTree.getZTreeObj("z-tree");
  795. var node = zTree.getNodeByParam("id", v);
  796. // 2、设置node节点选中状态:
  797. zTree.selectNode(node);
  798. // 3、设置node节点checked选中:
  799. node.checked = true;
  800. //注:设置checked属性之后,一定要更新该节点
  801. zTree.updateNode(node);
  802. console.log($scope.copyInspectionType)
  803. // 展开
  804. // zTree.expandNode(node);
  805. // setTimeout(() => {
  806. // zTreeOnCheck(null, null, node);
  807. // }, 0);
  808. });
  809. //展示选中的数据
  810. var arr = [];
  811. $scope.cachehttp.forEach(v=>{
  812. arr.push({
  813. id: v.inspectionType,
  814. name: v.inspectionTypeName,
  815. group: v.group?{id:v.group,groupName:v.groupName}:undefined,
  816. user: v.user?{id:v.user,name:v.userName}:undefined,
  817. parent: v.parent ? { id: v.parent.inspectionType } : undefined,
  818. pId: v.parent ? v.parent.inspectionType : 0,
  819. });
  820. })
  821. $scope.copyInspectionType = tranListToTreeData(arr);
  822. });
  823. // ==================end================
  824. });
  825. }
  826. function deleteEmputyChildren(data) {
  827. data.forEach((v) => {
  828. if (v.children.length) {
  829. deleteEmputyChildren(v.children);
  830. } else {
  831. delete v.children;
  832. }
  833. });
  834. return data;
  835. }
  836. function convertParentToChildList(data) {
  837. var treeMap = {};
  838. var list = [];
  839. convertListToTree(data, treeMap);
  840. angular.forEach(treeMap, function (item) {
  841. list.push(item);
  842. });
  843. return list;
  844. }
  845. function convertListToTree(data, treeMap) {
  846. var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
  847. var root = null; //Initially set our loop to null
  848. //loop over data
  849. for (var i = 0; i < data.length; i++) {
  850. var datum = data[i];
  851. //each node will have children, so let's give it a "children" poperty
  852. datum.children = [];
  853. //add an entry for this node to the map so that any future children can
  854. //lookup the parent
  855. idToNodeMap[datum.id] = datum;
  856. //Does this node have a parent?
  857. // console.log("datum="+JSON.stringify(datum))
  858. // if (typeof datum.parent === "undefined") {
  859. if (datum.pId === 0) {
  860. //Doesn't look like it, so this node is the root of the tree
  861. root = datum;
  862. treeMap[datum.id] = root;
  863. } else {
  864. //This node has a parent, so let's look it up using the id
  865. // parentNode = idToNodeMap[datum.parent.id];
  866. parentNode = idToNodeMap[datum.pId];
  867. //We don't need this property, so let's delete it.
  868. // delete datum.parent;
  869. //Let's add the current node as a child of the parent node.
  870. parentNode.children.push(datum);
  871. }
  872. }
  873. return root;
  874. }
  875. var setting = {
  876. check: {
  877. enable: true,
  878. chkStyle: "checkbox",
  879. chkboxType: { Y: "ps", N: "ps" },
  880. },
  881. callback: {
  882. onClick: onMouseDown,
  883. onCheck: zTreeOnCheck,
  884. },
  885. view: {
  886. showIcon: false,
  887. showLine: false,
  888. },
  889. };
  890. // $.fn.zTree.init($($element), setting, $scope.treeInfo);
  891. //点击菜单项
  892. function onMouseDown(event, treeId, treeNode) {
  893. console.log(event, treeId, treeNode);
  894. $scope.selectInspectionType = treeNode;
  895. }
  896. // 点击复选框
  897. function zTreeOnCheck(event, treeId, treeNode) {
  898. console.log(event, treeId, treeNode);
  899. $scope.changecate = false;
  900. var treeObj = $.fn.zTree.getZTreeObj("z-tree");
  901. var nodes_arr = treeObj.getCheckedNodes(true);
  902. var nodes = angular.copy(nodes_arr);
  903. console.log(nodes);
  904. if ($scope.cachehttp && $scope.cachehttp.length) {
  905. var result = angular.copy($scope.cachehttp);
  906. zTreeOnCheckCommon(result, nodes);
  907. } else {
  908. api_simple
  909. .fetchDataList("inspectionBind", {
  910. idx: 0,
  911. sum: 1000,
  912. inspectionBind: { inspection: $scope.stateModel.id },
  913. })
  914. .then(function (response) {
  915. var result = response.list || [];
  916. zTreeOnCheckCommon(result, nodes);
  917. });
  918. }
  919. }
  920. function tranListToTreeData(nodes){
  921. var arr = nodes.filter((v) => v.pId === 0);
  922. arr.forEach((v) => {
  923. v.children = [];
  924. });
  925. arr.forEach((v, i) => {
  926. nodes.forEach((vv) => {
  927. if (vv.pId == v.id) {
  928. arr[i].children.push(vv);
  929. }
  930. });
  931. });
  932. return arr;
  933. }
  934. // 点击复选框-common
  935. function zTreeOnCheckCommon(result, nodes) {
  936. // 如果有值,则不覆盖
  937. if($scope.copyInspectionType.length){
  938. result = [];
  939. $scope.copyInspectionType.forEach(v=>{
  940. result.push(v);
  941. if(v.children && v.children.length){
  942. v.children.forEach(vv=>{
  943. result.push(vv);
  944. })
  945. }
  946. })
  947. }else{
  948. result = result.map((v) => ({
  949. id: v.inspectionType,
  950. name: v.inspectionTypeName,
  951. group: v.group?{id:v.group,groupName:v.groupName}:undefined,
  952. user: v.user?{id:v.user,name:v.userName}:undefined,
  953. parent: v.parent ? { id: v.parent.inspectionType } : undefined,
  954. pId: v.parent ? v.parent.inspectionType : 0,
  955. }));
  956. }
  957. for (var i = 0; i < nodes.length; i++) {
  958. for (var j = 0; j < result.length; j++) {
  959. if (nodes[i].id == result[j].id) {
  960. nodes[i] = angular.copy(result[j]);
  961. }
  962. }
  963. }
  964. $scope.copyInspectionType = tranListToTreeData(nodes);
  965. }
  966. },
  967. };
  968. });
  969. app.directive("zTreeCopy", function () {
  970. // ztree liaomingming
  971. return {
  972. restrict: "EA",
  973. scope: {
  974. copyInspectionType: "=",
  975. changecate: "=",
  976. selectedchangecategory: "=",
  977. },
  978. link: function (scope, element, attrs) {
  979. scope.$watch("copyInspectionType", function (newVal) {
  980. console.log(newVal);
  981. var setting = {
  982. view: {
  983. showIcon: false,
  984. showLine: false,
  985. },
  986. callback: {
  987. onClick: onMouseDown,
  988. },
  989. };
  990. $.fn.zTree.destroy("z-tree-copy");
  991. var treeObj = $.fn.zTree.init($(element), setting, newVal);
  992. treeObj.expandAll(true);
  993. });
  994. //点击菜单项
  995. function onMouseDown(event, treeId, treeNode) {
  996. console.log(event, treeId, treeNode);
  997. scope.selectedchangecategory = treeNode;
  998. scope.changecate = true;
  999. console.log(scope.copyInspectionType);
  1000. }
  1001. },
  1002. controller: function ($scope, $element, api_bpm_data) {},
  1003. };
  1004. });