inspectionScopeCtrl.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. if ($scope.stateModel.model == 1) {
  42. $scope.modelName = $scope.stateModel.modelName;
  43. } else {
  44. $scope.stateModel.model = 0;
  45. $scope.modelName = $scope.stateModel.title;
  46. }
  47. $scope.xinzeng = false;
  48. $scope.shanchu = false;
  49. $scope.bianji = false;
  50. for (var i = 0; i < loginUser.menu.length; i++) {
  51. if (loginUser.menu[i].link == "shijianshezhi_xinzeng") {
  52. $scope.xinzeng = true;
  53. }
  54. if (loginUser.menu[i].link == "shijianshezhi_shanchu") {
  55. $scope.shanchu = true;
  56. }
  57. if (loginUser.menu[i].link == "shijianshezhi_bianji") {
  58. $scope.bianji = true;
  59. }
  60. }
  61. $scope.listDataTop = [];
  62. $scope.listDataBottom = [];
  63. $scope.leftListIdx = -1;
  64. $scope.leftListName = "巡检范围";
  65. $scope.leftListCli = function (idx, data) {
  66. $scope.leftListIdx = idx;
  67. $scope.stateModel = data;
  68. $scope.modelName = data.modelName;
  69. $scope.copyInspection.tree = [];
  70. $scope.selectedInspection.isShowZtree = false;
  71. setTimeout(() => {
  72. $scope.selectedInspection.isShowZtree = true;
  73. }, 0);
  74. };
  75. // 事件分类开始
  76. var loginUser = $rootScope.user;
  77. //组
  78. api_user_data
  79. .fetchDataList("group", { idx: 0, sum: 1000 })
  80. .then(function (data) {
  81. $scope.group = data.list;
  82. });
  83. api_wechatfile
  84. .getDictionary({ key: "incident_priority", type: "list" })
  85. .then(function (data) {
  86. $scope.priority = data;
  87. });
  88. /* ----- tree----- */
  89. var i = 0;
  90. $scope.out = [];
  91. $scope.outdata = [];
  92. $scope.subdata = {};
  93. $scope.sub1data = {};
  94. $scope.addcate = false;
  95. $scope.add1cate = false;
  96. $scope.changecate = false;
  97. $scope.filterTree = function (data, id) {
  98. data = angular.copy(data);
  99. var newTree = data.filter((x) => x.id !== id);
  100. newTree.forEach(
  101. (x) => x.children && (x.children = $scope.filterTree(x.children, id))
  102. );
  103. return newTree;
  104. };
  105. $scope.userOrGroupType = {}; //当前节点运维组/运维人员
  106. $scope.dataSave = []; //储存$scope.tree_data
  107. $scope.group_user = {
  108. // offlineGroup:{},//线下组
  109. // onlineUser:{}//线上人
  110. };
  111. $scope.selectedInspection = {
  112. tree: {},
  113. isShowZtree: true,
  114. };
  115. $scope.copyInspection = {
  116. tree: [],
  117. };
  118. $scope.timerZtree = null;
  119. $scope.timerZtreeCopy = null;
  120. // 新增
  121. $scope.addType1 = function (type) {
  122. console.log();
  123. // ===============================================
  124. var modalInstance = $modal.open({
  125. templateUrl: "assets/views/system/tpl/inspectionchange.html",
  126. controller: function ($scope, scope, $modalInstance) {
  127. $scope.add1data = {};
  128. $scope.thisParent={};//当前点击节点的父节点
  129. $scope.filterTree = function (data, id) {
  130. data = angular.copy(data);
  131. var newTree = data.filter((x) => x.id !== id);
  132. newTree.forEach(
  133. (x) =>
  134. x.children && (x.children = $scope.filterTree(x.children, id))
  135. );
  136. return newTree;
  137. };
  138. $scope.try_async_1load = function () {
  139. $scope.my_1data = [];
  140. var data = { idx: 0, sum: 1000, inspectionType: { deleteFlag: 0 } };
  141. api_bpm_data
  142. .fetchDataList("inspectionType", data)
  143. .then(function (response) {
  144. var data = response.list;
  145. data = data.filter(v=>!v.parent);//只显示一级
  146. var objects = [];
  147. for (var i = 0; i < data.length; i++) {
  148. var object = {};
  149. object.id = data[i].id;
  150. if (angular.isDefined(data[i].user)) {
  151. object.user = data[i].user || {};
  152. }
  153. if (angular.isDefined(data[i].group)) {
  154. object.group = data[i].group || {};
  155. }
  156. if (angular.isDefined(data[i].parent)) {
  157. object.parent = data[i].parent;
  158. }
  159. object.label = data[i].type;
  160. objects.push(object);
  161. }
  162. $scope.my_1data = convertParentToChildList(objects);
  163. $scope.tree_1data = angular.copy($scope.my_1data);
  164. // 把选中的巡检范围当作父类回显
  165. if (scope.selectedInspection.tree.id) {
  166. // 区分新增和修改
  167. if (type == "add") {
  168. $scope.thisParent = data.find(
  169. (v) => v.id == scope.selectedInspection.tree.id
  170. );
  171. if ($scope.thisParent) {
  172. $scope.thisParent.label=$scope.thisParent.type;
  173. $scope.thisParent.isExpanded = true;
  174. $scope.thisParent.selected = true;
  175. $scope.addparent = true;
  176. } else {
  177. $scope.addparent = false;
  178. }
  179. } else if (type == "edit") {
  180. // 编辑
  181. console.log($scope.my_1data,'原始数据')
  182. $scope.tree_1data = $scope.my_1data.filter(v=>v.id != scope.selectedInspection.tree.id);//过滤本条
  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;
  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. $scope.closeModel = function () {
  513. $state.go("app.inspection.inspectPlan"); //跳转巡检计划列表
  514. };
  515. $scope.TreeToArr = function (tree, arr) {
  516. tree.forEach((v) => {
  517. if (v.children) {
  518. arr.push(v);
  519. $scope.TreeToArr(v.children, arr);
  520. } else {
  521. arr.push(v);
  522. }
  523. });
  524. return arr;
  525. };
  526. $scope.saveCommon = function ($scope) {
  527. if (!$scope.modelName.trim()) {
  528. SweetAlert.swal({
  529. title: "策略名称不能为空!",
  530. type: "error",
  531. });
  532. return;
  533. }
  534. var arr = $scope.TreeToArr($scope.copyInspection.tree, []);
  535. $scope.stateModel.modelName = $scope.modelName;
  536. $scope.stateModel.typeIds = arr.map((v) => v.id).toString();
  537. arr = arr.map((v) => ({
  538. inspection: $scope.stateModel,
  539. inspectionType: v.id,
  540. parent: v.parent,
  541. user: v.user,
  542. group: v.group,
  543. }));
  544. // 一条都没选择
  545. if (arr.length === 0) {
  546. arr.push({ inspection: $scope.stateModel });
  547. }
  548. console.log(arr);
  549. // return;
  550. api_simple.addListData("inspectionBind", arr).then(function (response) {
  551. if (response) {
  552. if (response.status == 200) {
  553. $scope.stateModel.status = { id: 120 }; //强制扭转状态为正常
  554. var filedata = {
  555. close: "关闭",
  556. save: "暂存",
  557. submit: "下一步",
  558. inspection: $scope.stateModel,
  559. };
  560. api_bpm_data
  561. .addData("inspection", filedata)
  562. .then(function (response) {
  563. if (response) {
  564. if (response.status == 200) {
  565. SweetAlert.swal(
  566. {
  567. title: "保存成功!",
  568. type: "success",
  569. },
  570. function () {
  571. // $scope.selectedchangecategory.value = {};
  572. // $scope.copyInspection.tree = [];
  573. // $scope.changecateObj.changecate = false;
  574. // $scope.selectedInspection.isShowZtree = false;
  575. // setTimeout(()=>{
  576. // $scope.selectedInspection.isShowZtree = true;
  577. // },0)
  578. $state.go("app.inspection.inspectPlan"); //跳转巡检计划列表
  579. }
  580. );
  581. } else {
  582. SweetAlert.swal({
  583. title: "保存失败!",
  584. type: "error",
  585. });
  586. }
  587. }
  588. });
  589. } else {
  590. SweetAlert.swal({
  591. title: "保存失败!",
  592. type: "error",
  593. });
  594. }
  595. }
  596. });
  597. };
  598. $scope.add1data = {};
  599. $scope.addparent = false;
  600. // 事件分类结束
  601. // 获取组数据
  602. $scope.getGroupList = function () {
  603. var postData = {
  604. idx: 0,
  605. sum: 1000,
  606. group: { selectType: "nouser" },
  607. };
  608. api_user_data.fetchDataList("group", postData).then((res) => {
  609. $scope.groupList = res.list;
  610. });
  611. };
  612. $scope.getGroupList();
  613. // 获取巡检计划数据
  614. $scope.getGroupList = function (model) {
  615. var postData = {
  616. idx: 0,
  617. sum: 3,
  618. inspection: { model: model },
  619. };
  620. api_user_data.fetchDataList("inspection", postData).then((res) => {
  621. if (model == 1) {
  622. $scope.listDataTop = res.list; //巡检范围策略模板
  623. } else {
  624. $scope.listDataBottom = res.list; //往期巡检范围策略
  625. }
  626. });
  627. };
  628. $scope.getGroupList(1);
  629. $scope.getGroupList();
  630. // 获取人数据
  631. $scope.getUserList = function () {
  632. var postData = {
  633. idx: 0,
  634. sum: 1000,
  635. user: {
  636. name: "",
  637. selectType: "pinyin_qs",
  638. },
  639. };
  640. api_user_data.fetchDataList("user", postData).then((res) => {
  641. $scope.userList = res.list;
  642. });
  643. };
  644. $scope.getUserList();
  645. $scope.changecateObj = {
  646. changecate: false,
  647. };
  648. },
  649. ]);
  650. app.directive("zTree", function () {
  651. // ztree liaomingming
  652. return {
  653. restrict: "EA",
  654. scope: {
  655. selectInspectionType: "=",
  656. copyInspectionType: "=",
  657. stateModel: "=",
  658. },
  659. controller: function ($scope, $element, api_bpm_data, api_simple) {
  660. console.log($scope);
  661. getTreeInfo();
  662. function getTreeInfo() {
  663. $scope.treeInfo = [];
  664. var data = { idx: 0, sum: 1000, inspectionType: { deleteFlag: 0 } };
  665. $scope.isShowZtree = false;
  666. api_bpm_data
  667. .fetchDataList("inspectionType", data)
  668. .then(function (response) {
  669. var data = response.list;
  670. console.log(data);
  671. var objects = [];
  672. for (var i = 0; i < data.length; i++) {
  673. var object = {};
  674. object.id = data[i].id;
  675. if (angular.isDefined(data[i].user)) {
  676. object.user = data[i].user || {};
  677. }
  678. if (angular.isDefined(data[i].group)) {
  679. object.group = data[i].group || {};
  680. }
  681. if (angular.isDefined(data[i].parent)) {
  682. object.parent = data[i].parent;
  683. object.pId = data[i].parent.id;
  684. } else {
  685. object.pId = 0;
  686. }
  687. object.name = data[i].type;
  688. objects.push(object);
  689. }
  690. $scope.treeInfo = convertParentToChildList(objects);
  691. $scope.treeInfo = deleteEmputyChildren($scope.treeInfo);
  692. console.log($scope.treeInfo);
  693. // ==================start================
  694. api_simple
  695. .fetchDataList("inspectionBind", {
  696. idx: 0,
  697. sum: 1000,
  698. inspectionBind: { inspection: { id: $scope.stateModel.id } },
  699. })
  700. .then(function (response) {
  701. $.fn.zTree.destroy("z-tree");
  702. $.fn.zTree.init($($element), setting, $scope.treeInfo);
  703. $scope.isShowZtree = true;
  704. var result = response.list || [];
  705. result = result.map((v) => v.inspectionType);
  706. console.log(result);
  707. result.forEach((v, i) => {
  708. // 设置路径树节点选中
  709. // 1、根据id获取树的某个节点:
  710. var zTree = $.fn.zTree.getZTreeObj("z-tree");
  711. var node = zTree.getNodeByParam("id", v);
  712. // 2、设置node节点选中状态:
  713. zTree.selectNode(node);
  714. // 3、设置node节点checked选中:
  715. node.checked = true;
  716. //注:设置checked属性之后,一定要更新该节点
  717. zTree.updateNode(node);
  718. // 展开
  719. zTree.expandNode(node);
  720. setTimeout(() => {
  721. zTreeOnCheck(null, null, node);
  722. }, 0);
  723. });
  724. });
  725. // ==================end================
  726. });
  727. }
  728. function deleteEmputyChildren(data) {
  729. data.forEach((v) => {
  730. if (v.children.length) {
  731. deleteEmputyChildren(v.children);
  732. } else {
  733. delete v.children;
  734. }
  735. });
  736. return data;
  737. }
  738. function convertParentToChildList(data) {
  739. var treeMap = {};
  740. var list = [];
  741. convertListToTree(data, treeMap);
  742. angular.forEach(treeMap, function (item) {
  743. list.push(item);
  744. });
  745. return list;
  746. }
  747. function convertListToTree(data, treeMap) {
  748. var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
  749. var root = null; //Initially set our loop to null
  750. //loop over data
  751. for (var i = 0; i < data.length; i++) {
  752. var datum = data[i];
  753. //each node will have children, so let's give it a "children" poperty
  754. datum.children = [];
  755. //add an entry for this node to the map so that any future children can
  756. //lookup the parent
  757. idToNodeMap[datum.id] = datum;
  758. //Does this node have a parent?
  759. // console.log("datum="+JSON.stringify(datum))
  760. // if (typeof datum.parent === "undefined") {
  761. if (datum.pId === 0) {
  762. //Doesn't look like it, so this node is the root of the tree
  763. root = datum;
  764. treeMap[datum.id] = root;
  765. } else {
  766. //This node has a parent, so let's look it up using the id
  767. // parentNode = idToNodeMap[datum.parent.id];
  768. parentNode = idToNodeMap[datum.pId];
  769. //We don't need this property, so let's delete it.
  770. // delete datum.parent;
  771. //Let's add the current node as a child of the parent node.
  772. parentNode.children.push(datum);
  773. }
  774. }
  775. return root;
  776. }
  777. var setting = {
  778. check: {
  779. enable: true,
  780. chkStyle: "checkbox",
  781. chkboxType: { Y: "ps", N: "ps" },
  782. },
  783. callback: {
  784. onClick: onMouseDown,
  785. onCheck: zTreeOnCheck,
  786. },
  787. view: {
  788. showIcon: false,
  789. showLine: false,
  790. },
  791. };
  792. // $.fn.zTree.init($($element), setting, $scope.treeInfo);
  793. //点击菜单项
  794. function onMouseDown(event, treeId, treeNode) {
  795. console.log(event, treeId, treeNode);
  796. $scope.selectInspectionType = treeNode;
  797. }
  798. $scope.isFirstGet = false;
  799. // 点击复选框
  800. function zTreeOnCheck(event, treeId, treeNode) {
  801. console.log(event, treeId, treeNode);
  802. var treeObj = $.fn.zTree.getZTreeObj("z-tree");
  803. var nodes_arr = treeObj.getCheckedNodes(true);
  804. var nodes = angular.copy(nodes_arr);
  805. console.log(nodes);
  806. var arr = nodes.filter((v) => v.pId === 0);
  807. arr.forEach((v) => {
  808. v.children = [];
  809. });
  810. arr.forEach((v, i) => {
  811. nodes.forEach((vv) => {
  812. if (vv.pId == v.id) {
  813. arr[i].children.push(vv);
  814. }
  815. });
  816. });
  817. if (treeNode.checked && $scope.isFirstGet) {
  818. //如果勾选上
  819. api_simple
  820. .fetchDataList("inspectionBind", {
  821. idx: 0,
  822. sum: 1000,
  823. inspectionBind: { inspection: { id: $scope.stateModel.id } },
  824. })
  825. .then(function (response) {
  826. $scope.isFirstGet = true;
  827. var result = response.list || [];
  828. result = result.map((v) => ({
  829. id: v.inspectionType,
  830. name: v.inspectionTypeName,
  831. group: v.group,
  832. user: v.user,
  833. parent: v.parent ? { id: v.parent.inspectionType } : undefined,
  834. pId: v.parent ? v.parent.inspectionType : 0,
  835. }));
  836. for (var i = 0; i < nodes.length; i++) {
  837. for (var j = 0; j < result.length; j++) {
  838. if (nodes[i].id == result[j].id) {
  839. nodes[i] = angular.copy(result[j]);
  840. }
  841. }
  842. }
  843. var arr = nodes.filter((v) => v.pId === 0);
  844. arr.forEach((v) => {
  845. v.children = [];
  846. });
  847. arr.forEach((v, i) => {
  848. nodes.forEach((vv) => {
  849. if (vv.pId == v.id) {
  850. arr[i].children.push(vv);
  851. }
  852. });
  853. });
  854. console.log(arr);
  855. $scope.copyInspectionType = arr;
  856. });
  857. } else {
  858. // 取消勾选
  859. $scope.copyInspectionType = arr;
  860. }
  861. // console.log(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
  862. }
  863. },
  864. };
  865. });
  866. app.directive("zTreeCopy", function () {
  867. // ztree liaomingming
  868. return {
  869. restrict: "EA",
  870. scope: {
  871. copyInspectionType: "=",
  872. changecate: "=",
  873. selectedchangecategory: "=",
  874. },
  875. link: function (scope, element, attrs) {
  876. scope.$watch("copyInspectionType", function (newVal) {
  877. console.log(newVal);
  878. var setting = {
  879. view: {
  880. showIcon: false,
  881. showLine: false,
  882. },
  883. callback: {
  884. onClick: onMouseDown,
  885. },
  886. };
  887. $.fn.zTree.destroy("z-tree-copy");
  888. var treeObj = $.fn.zTree.init($(element), setting, newVal);
  889. treeObj.expandAll(true);
  890. });
  891. //点击菜单项
  892. function onMouseDown(event, treeId, treeNode) {
  893. console.log(event, treeId, treeNode);
  894. scope.selectedchangecategory = treeNode;
  895. scope.changecate = true;
  896. console.log(scope.copyInspectionType);
  897. }
  898. },
  899. controller: function ($scope, $element, api_bpm_data) {},
  900. };
  901. });