inspectionScopeCtrl.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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(
  183. (v) => v.id != scope.selectedInspection.tree.id
  184. ); //过滤本条
  185. $scope.thisParent = data.find(
  186. (v) => v.id == scope.selectedInspection.tree.pId
  187. );
  188. if ($scope.thisParent) {
  189. $scope.thisParent.label = $scope.thisParent.type;
  190. $scope.thisParent.isExpanded = true;
  191. $scope.thisParent.selected = true;
  192. $scope.addparent = true;
  193. } else {
  194. $scope.addparent = false;
  195. }
  196. $scope.add1data.type = scope.selectedInspection.tree.name;
  197. $scope.add1data.id = scope.selectedInspection.tree.id;
  198. }
  199. } else {
  200. $scope.addparent = false;
  201. }
  202. });
  203. };
  204. $scope.try_async_1load();
  205. function convertListToTree(data, treeMap) {
  206. var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
  207. var root = null; //Initially set our loop to null
  208. //loop over data
  209. for (var i = 0; i < data.length; i++) {
  210. var datum = data[i];
  211. //each node will have children, so let's give it a "children" poperty
  212. datum.children = [];
  213. //add an entry for this node to the map so that any future children can
  214. //lookup the parent
  215. idToNodeMap[datum.id] = datum;
  216. //Does this node have a parent?
  217. // console.log("datum="+JSON.stringify(datum))
  218. if (typeof datum.parent === "undefined") {
  219. //Doesn't look like it, so this node is the root of the tree
  220. root = datum;
  221. treeMap[datum.id] = root;
  222. } else {
  223. //This node has a parent, so let's look it up using the id
  224. parentNode = idToNodeMap[datum.parent.id];
  225. //We don't need this property, so let's delete it.
  226. // delete datum.parent;
  227. //Let's add the current node as a child of the parent node.
  228. parentNode.children.push(datum);
  229. }
  230. }
  231. return root;
  232. }
  233. function convertParentToChildList(data) {
  234. var treeMap = {};
  235. var list = [];
  236. convertListToTree(data, treeMap);
  237. angular.forEach(treeMap, function (item) {
  238. list.push(item);
  239. });
  240. return list;
  241. }
  242. // ------------------------------------------------
  243. $scope.title = type == "add" ? "巡检范围新增" : "巡检范围修改";
  244. $scope.cancel = function () {
  245. $modalInstance.dismiss("cancel");
  246. };
  247. $scope.savercode = function (data) {
  248. if (data && data.type) {
  249. if ($scope.addparent) {
  250. data.parent = $scope.add1data.parent;
  251. } else {
  252. data.parent = null;
  253. }
  254. scope.selectedInspection.tree = {};
  255. $modalInstance.close(data);
  256. } else {
  257. SweetAlert.swal(
  258. {
  259. title: "新增失败!",
  260. text: "请填写必填项!",
  261. type: "error",
  262. confirmButtonColor: "#DD6B55",
  263. },
  264. function () {}
  265. );
  266. }
  267. };
  268. },
  269. resolve: {
  270. scope: function () {
  271. return $scope;
  272. },
  273. },
  274. });
  275. // liaomingming
  276. modalInstance.result.then(function (selectedItem) {
  277. console.log($scope);
  278. if (selectedItem.type) {
  279. selectedItem.deleteFlag = 0;
  280. selectedItem.formUiName = "inspection_LinHu_detail";
  281. selectedItem.formUiEdit = "inspection_LinHu_confirm";
  282. selectedItem.processKey = "bpm_inspection";
  283. var filedata = { inspectionType: selectedItem };
  284. console.log(filedata);
  285. // return;
  286. api_bpm_data[type == "add" ? "addData" : "updData"](
  287. "inspectionType",
  288. filedata
  289. ).then(function (response) {
  290. if (response) {
  291. if (response.status == 200) {
  292. $scope.selectedInspection.isShowZtree = false;
  293. setTimeout(() => {
  294. $scope.selectedInspection.isShowZtree = true;
  295. }, 0);
  296. SweetAlert.swal({
  297. title: type == "add" ? "增加成功!" : "修改成功!",
  298. type: "success",
  299. });
  300. } else {
  301. SweetAlert.swal({
  302. title: type == "add" ? "增加失败!" : "修改失败!",
  303. type: "error",
  304. });
  305. }
  306. }
  307. });
  308. } else {
  309. SweetAlert.swal(
  310. {
  311. title: "新增失败!",
  312. text: "请填写必填项!",
  313. type: "error",
  314. confirmButtonColor: "#DD6B55",
  315. },
  316. function () {}
  317. );
  318. }
  319. });
  320. };
  321. /* -----start remove incidet category----- */
  322. // 删除
  323. $scope.remove1 = function () {
  324. var modalInstance = $modal.open({
  325. templateUrl: "assets/views/delete.html",
  326. controller: function ($scope, scope, $modalInstance, api_bpm_data) {
  327. $scope.title = "巡检范围删除?";
  328. $scope.connect = "确定要删除此巡检范围?";
  329. $scope.ok = function () {
  330. $modalInstance.close(scope.selectedInspection.tree);
  331. };
  332. $scope.cancel = function () {
  333. $modalInstance.dismiss("cancel");
  334. };
  335. },
  336. size: "sm",
  337. resolve: {
  338. scope: function () {
  339. return $scope;
  340. },
  341. },
  342. });
  343. modalInstance.result.then(function (selectedItem) {
  344. if (selectedItem) {
  345. var rmvList = [];
  346. rmvList.push(selectedItem.id);
  347. if (selectedItem.children && selectedItem.children.length > 0) {
  348. SweetAlert.swal({
  349. title: "删除失败!",
  350. text: "请先删除选中的范围的子级",
  351. type: "error",
  352. });
  353. } else {
  354. api_bpm_data
  355. .rmvData("inspectionType", rmvList)
  356. .then(function (response) {
  357. if (response.data) {
  358. SweetAlert.swal(
  359. {
  360. title: "删除成功!",
  361. type: "success",
  362. confirmButtonColor: "#007AFF",
  363. },
  364. function () {
  365. $scope.selectedInspection.isShowZtree = false;
  366. setTimeout(() => {
  367. $scope.selectedInspection.isShowZtree = true;
  368. }, 0);
  369. }
  370. );
  371. } else {
  372. SweetAlert.swal({
  373. title: "操作异常!",
  374. text: "系统异常,请稍后重试,或者联系管理员!",
  375. type: "error",
  376. });
  377. }
  378. });
  379. }
  380. }
  381. });
  382. };
  383. /* -----end remove incidet category----- */
  384. function convertListToTree(data, treeMap) {
  385. var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
  386. var root = null; //Initially set our loop to null
  387. //loop over data
  388. for (var i = 0; i < data.length; i++) {
  389. var datum = data[i];
  390. //each node will have children, so let's give it a "children" poperty
  391. datum.children = [];
  392. //add an entry for this node to the map so that any future children can
  393. //lookup the parent
  394. idToNodeMap[datum.id] = datum;
  395. //Does this node have a parent?
  396. // console.log("datum="+JSON.stringify(datum))
  397. if (typeof datum.parent === "undefined") {
  398. //Doesn't look like it, so this node is the root of the tree
  399. root = datum;
  400. treeMap[datum.id] = root;
  401. } else {
  402. //This node has a parent, so let's look it up using the id
  403. parentNode = idToNodeMap[datum.parent.id];
  404. //We don't need this property, so let's delete it.
  405. // delete datum.parent;
  406. //Let's add the current node as a child of the parent node.
  407. parentNode.children.push(datum);
  408. }
  409. }
  410. return root;
  411. }
  412. function convertParentToChildList(data) {
  413. var treeMap = {};
  414. var list = [];
  415. convertListToTree(data, treeMap);
  416. angular.forEach(treeMap, function (item) {
  417. list.push(item);
  418. });
  419. return list;
  420. }
  421. $scope.closethis = function (formdata) {
  422. formdata.category = "";
  423. formdata.parent.category = "";
  424. };
  425. /* -----start submit incidet category----- */
  426. // 保存设置
  427. $scope.selectedchangecategory = { value: {} };
  428. $scope.$watch("selectedchangecategory.value", function (newVal) {
  429. console.log(newVal);
  430. if (newVal.user) {
  431. $scope.group_user["onlineUser"] = newVal.user || undefined;
  432. } else {
  433. $scope.group_user["onlineUser"] = undefined;
  434. }
  435. if (newVal.group) {
  436. $scope.group_user["offlineGroup"] = newVal.group || undefined;
  437. } else {
  438. $scope.group_user["offlineGroup"] = undefined;
  439. }
  440. });
  441. $scope.editInspection = function (tree, id, newVal, type) {
  442. tree.forEach((v) => {
  443. if (v.id == id) {
  444. v[type] = newVal;
  445. } else if (v.children) {
  446. $scope.editInspection(v.children, id, newVal, type);
  447. }
  448. });
  449. };
  450. $scope.$watch("group_user.offlineGroup", function (newVal) {
  451. console.log($scope.selectedchangecategory.value);
  452. console.log($scope.copyInspection.tree);
  453. console.log(newVal, "group");
  454. $scope.editInspection(
  455. $scope.copyInspection.tree,
  456. $scope.selectedchangecategory.value.id,
  457. newVal,
  458. "group"
  459. );
  460. if (newVal && newVal.id) {
  461. $scope.selectedchangecategory.value.group = newVal;
  462. } else {
  463. delete $scope.selectedchangecategory.value.group;
  464. }
  465. });
  466. $scope.$watch("group_user.onlineUser", function (newVal) {
  467. // console.log($scope.selectedchangecategory.value)
  468. // console.log($scope.copyInspection.tree)
  469. // console.log(newVal, 'user')
  470. $scope.editInspection(
  471. $scope.copyInspection.tree,
  472. $scope.selectedchangecategory.value.id,
  473. newVal,
  474. "user"
  475. );
  476. if (newVal && newVal.id) {
  477. $scope.selectedchangecategory.value.user = newVal;
  478. } else {
  479. delete $scope.selectedchangecategory.value.user;
  480. }
  481. });
  482. // 保存设置 liaomingming
  483. $scope.submit1change = function () {
  484. console.log($scope.copyInspection.tree);
  485. console.log($scope.stateModel);
  486. if ($scope.stateModel.model === 0) {
  487. // 不设置为模块的时候需要弹窗提示
  488. var modalInstance = $modal.open({
  489. templateUrl: "assets/views/system/tpl/inspectionModel.html",
  490. controller: function ($scope, scope, $modalInstance) {
  491. $scope.add1data = { model: false };
  492. $scope.title = "提示";
  493. $scope.cancel = function () {
  494. $modalInstance.dismiss("cancel");
  495. };
  496. $scope.savercode = function (data) {
  497. console.log(scope.copyInspection.tree);
  498. scope.stateModel.model = data.model ? 1 : 0;
  499. scope.saveCommon(scope);
  500. $modalInstance.close(data);
  501. };
  502. },
  503. resolve: {
  504. scope: function () {
  505. return $scope;
  506. },
  507. },
  508. });
  509. modalInstance.result.then(function (selectedItem) {});
  510. return;
  511. }
  512. $scope.saveCommon($scope);
  513. };
  514. $scope.closeModel = function () {
  515. $state.go("app.inspection.inspectPlan"); //跳转巡检计划列表
  516. };
  517. $scope.TreeToArr = function (tree, arr) {
  518. tree.forEach((v) => {
  519. if (v.children) {
  520. arr.push(v);
  521. $scope.TreeToArr(v.children, arr);
  522. } else {
  523. arr.push(v);
  524. }
  525. });
  526. return arr;
  527. };
  528. $scope.saveCommon = function ($scope) {
  529. if (!$scope.modelName.trim()) {
  530. SweetAlert.swal({
  531. title: "策略名称不能为空!",
  532. type: "error",
  533. });
  534. return;
  535. }
  536. var arr = $scope.TreeToArr($scope.copyInspection.tree, []);
  537. $scope.stateModel.modelName = $scope.modelName;
  538. $scope.stateModel.typeIds = arr.map((v) => v.id).toString();
  539. arr = arr.map((v) => ({
  540. inspection: $scope.stateModel,
  541. inspectionType: v.id,
  542. parent: v.parent,
  543. user: v.user,
  544. group: v.group,
  545. }));
  546. // 一条都没选择
  547. if (arr.length === 0) {
  548. arr.push({ inspection: $scope.stateModel });
  549. }
  550. console.log(arr);
  551. // return;
  552. api_simple.addListData("inspectionBind", arr).then(function (response) {
  553. if (response) {
  554. if (response.status == 200) {
  555. $scope.stateModel.status = { id: 120 }; //强制扭转状态为正常
  556. var filedata = {
  557. close: "关闭",
  558. save: "暂存",
  559. submit: "下一步",
  560. inspection: $scope.stateModel,
  561. };
  562. api_bpm_data
  563. .addData("inspection", filedata)
  564. .then(function (response) {
  565. if (response) {
  566. if (response.status == 200) {
  567. SweetAlert.swal(
  568. {
  569. title: "保存成功!",
  570. type: "success",
  571. },
  572. function () {
  573. // $scope.selectedchangecategory.value = {};
  574. // $scope.copyInspection.tree = [];
  575. // $scope.changecateObj.changecate = false;
  576. // $scope.selectedInspection.isShowZtree = false;
  577. // setTimeout(()=>{
  578. // $scope.selectedInspection.isShowZtree = true;
  579. // },0)
  580. $state.go("app.inspection.inspectPlan"); //跳转巡检计划列表
  581. }
  582. );
  583. } else {
  584. SweetAlert.swal({
  585. title: "保存失败!",
  586. type: "error",
  587. });
  588. }
  589. }
  590. });
  591. } else {
  592. SweetAlert.swal({
  593. title: "保存失败!",
  594. type: "error",
  595. });
  596. }
  597. }
  598. });
  599. };
  600. $scope.add1data = {};
  601. $scope.addparent = false;
  602. // 事件分类结束
  603. // 获取组数据
  604. $scope.getGroupList = function () {
  605. var postData = {
  606. idx: 0,
  607. sum: 1000,
  608. group: { selectType: "nouser" },
  609. };
  610. api_user_data.fetchDataList("group", postData).then((res) => {
  611. $scope.groupList = res.list;
  612. });
  613. };
  614. $scope.getGroupList();
  615. // 获取巡检计划数据
  616. $scope.getGroupList = function (model) {
  617. var postData = {
  618. idx: 0,
  619. sum: 3,
  620. inspection: { model: model },
  621. };
  622. api_user_data.fetchDataList("inspection", postData).then((res) => {
  623. if (model == 1) {
  624. $scope.listDataTop = res.list; //巡检范围策略模板
  625. } else {
  626. $scope.listDataBottom = res.list; //往期巡检范围策略
  627. }
  628. });
  629. };
  630. $scope.getGroupList(1);
  631. $scope.getGroupList();
  632. // 获取人数据
  633. $scope.getUserList = function () {
  634. var postData = {
  635. idx: 0,
  636. sum: 1000,
  637. user: {
  638. name: "",
  639. selectType: "pinyin_qs",
  640. },
  641. };
  642. api_user_data.fetchDataList("user", postData).then((res) => {
  643. $scope.userList = res.list;
  644. });
  645. };
  646. $scope.getUserList();
  647. $scope.changecateObj = {
  648. changecate: false,
  649. };
  650. // 缓存的绑定数据-http
  651. $scope.cacheHttp = {
  652. value: [],
  653. };
  654. },
  655. ]);
  656. app.directive("zTree", function () {
  657. // ztree liaomingming
  658. return {
  659. restrict: "EA",
  660. scope: {
  661. selectInspectionType: "=",
  662. copyInspectionType: "=",
  663. stateModel: "=",
  664. changecate: "=",
  665. cachehttp: "=",
  666. },
  667. controller: function ($scope, $element, api_bpm_data, api_simple) {
  668. console.log($scope);
  669. getTreeInfo();
  670. function getTreeInfo() {
  671. $scope.treeInfo = [];
  672. var data = { idx: 0, sum: 1000, inspectionType: { deleteFlag: 0 } };
  673. $scope.isShowZtree = false;
  674. api_bpm_data
  675. .fetchDataList("inspectionType", data)
  676. .then(function (response) {
  677. var data = response.list;
  678. console.log(data);
  679. var objects = [];
  680. for (var i = 0; i < data.length; i++) {
  681. var object = {};
  682. object.id = data[i].id;
  683. if (angular.isDefined(data[i].user)) {
  684. object.user = data[i].user || {};
  685. }
  686. if (angular.isDefined(data[i].group)) {
  687. object.group = data[i].group || {};
  688. }
  689. if (angular.isDefined(data[i].parent)) {
  690. object.parent = data[i].parent;
  691. object.pId = data[i].parent.id;
  692. } else {
  693. object.pId = 0;
  694. }
  695. object.name = data[i].type;
  696. objects.push(object);
  697. }
  698. $scope.treeInfo = convertParentToChildList(objects);
  699. $scope.treeInfo = deleteEmputyChildren($scope.treeInfo);
  700. console.log($scope.treeInfo);
  701. // ==================start================
  702. api_simple
  703. .fetchDataList("inspectionBind", {
  704. idx: 0,
  705. sum: 1000,
  706. inspectionBind: { inspection: { id: $scope.stateModel.id } },
  707. })
  708. .then(function (response) {
  709. $.fn.zTree.destroy("z-tree");
  710. $.fn.zTree.init($($element), setting, $scope.treeInfo);
  711. $scope.isShowZtree = true;
  712. var result = response.list || [];
  713. $scope.cachehttp = angular.copy(result);
  714. result = result.map((v) => v.inspectionType);
  715. console.log(result);
  716. result.forEach((v, i) => {
  717. // 设置路径树节点选中
  718. // 1、根据id获取树的某个节点:
  719. var zTree = $.fn.zTree.getZTreeObj("z-tree");
  720. var node = zTree.getNodeByParam("id", v);
  721. // 2、设置node节点选中状态:
  722. zTree.selectNode(node);
  723. // 3、设置node节点checked选中:
  724. node.checked = true;
  725. //注:设置checked属性之后,一定要更新该节点
  726. zTree.updateNode(node);
  727. // 展开
  728. zTree.expandNode(node);
  729. setTimeout(() => {
  730. zTreeOnCheck(null, null, node);
  731. }, 0);
  732. });
  733. });
  734. // ==================end================
  735. });
  736. }
  737. function deleteEmputyChildren(data) {
  738. data.forEach((v) => {
  739. if (v.children.length) {
  740. deleteEmputyChildren(v.children);
  741. } else {
  742. delete v.children;
  743. }
  744. });
  745. return data;
  746. }
  747. function convertParentToChildList(data) {
  748. var treeMap = {};
  749. var list = [];
  750. convertListToTree(data, treeMap);
  751. angular.forEach(treeMap, function (item) {
  752. list.push(item);
  753. });
  754. return list;
  755. }
  756. function convertListToTree(data, treeMap) {
  757. var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
  758. var root = null; //Initially set our loop to null
  759. //loop over data
  760. for (var i = 0; i < data.length; i++) {
  761. var datum = data[i];
  762. //each node will have children, so let's give it a "children" poperty
  763. datum.children = [];
  764. //add an entry for this node to the map so that any future children can
  765. //lookup the parent
  766. idToNodeMap[datum.id] = datum;
  767. //Does this node have a parent?
  768. // console.log("datum="+JSON.stringify(datum))
  769. // if (typeof datum.parent === "undefined") {
  770. if (datum.pId === 0) {
  771. //Doesn't look like it, so this node is the root of the tree
  772. root = datum;
  773. treeMap[datum.id] = root;
  774. } else {
  775. //This node has a parent, so let's look it up using the id
  776. // parentNode = idToNodeMap[datum.parent.id];
  777. parentNode = idToNodeMap[datum.pId];
  778. //We don't need this property, so let's delete it.
  779. // delete datum.parent;
  780. //Let's add the current node as a child of the parent node.
  781. parentNode.children.push(datum);
  782. }
  783. }
  784. return root;
  785. }
  786. var setting = {
  787. check: {
  788. enable: true,
  789. chkStyle: "checkbox",
  790. chkboxType: { Y: "ps", N: "ps" },
  791. },
  792. callback: {
  793. onClick: onMouseDown,
  794. onCheck: zTreeOnCheck,
  795. },
  796. view: {
  797. showIcon: false,
  798. showLine: false,
  799. },
  800. };
  801. // $.fn.zTree.init($($element), setting, $scope.treeInfo);
  802. //点击菜单项
  803. function onMouseDown(event, treeId, treeNode) {
  804. console.log(event, treeId, treeNode);
  805. $scope.selectInspectionType = treeNode;
  806. }
  807. // 点击复选框
  808. function zTreeOnCheck(event, treeId, treeNode) {
  809. console.log(event, treeId, treeNode);
  810. $scope.changecate = false;
  811. var treeObj = $.fn.zTree.getZTreeObj("z-tree");
  812. var nodes_arr = treeObj.getCheckedNodes(true);
  813. var nodes = angular.copy(nodes_arr);
  814. console.log(nodes);
  815. var arr = nodes.filter((v) => v.pId === 0);
  816. arr.forEach((v) => {
  817. v.children = [];
  818. });
  819. arr.forEach((v, i) => {
  820. nodes.forEach((vv) => {
  821. if (vv.pId == v.id) {
  822. arr[i].children.push(vv);
  823. }
  824. });
  825. });
  826. if ($scope.cachehttp && $scope.cachehttp.length) {
  827. var result = angular.copy($scope.cachehttp);
  828. zTreeOnCheckCommon(result, nodes);
  829. } else {
  830. api_simple
  831. .fetchDataList("inspectionBind", {
  832. idx: 0,
  833. sum: 1000,
  834. inspectionBind: { inspection: { id: $scope.stateModel.id } },
  835. })
  836. .then(function (response) {
  837. var result = response.list || [];
  838. zTreeOnCheckCommon(result, nodes);
  839. });
  840. }
  841. }
  842. // 点击复选框-common
  843. function zTreeOnCheckCommon(result, nodes) {
  844. result = result.map((v) => ({
  845. id: v.inspectionType,
  846. name: v.inspectionTypeName,
  847. group: v.group,
  848. user: v.user,
  849. parent: v.parent ? { id: v.parent.inspectionType } : undefined,
  850. pId: v.parent ? v.parent.inspectionType : 0,
  851. }));
  852. for (var i = 0; i < nodes.length; i++) {
  853. for (var j = 0; j < result.length; j++) {
  854. if (nodes[i].id == result[j].id) {
  855. nodes[i] = angular.copy(result[j]);
  856. }
  857. }
  858. }
  859. var arr = nodes.filter((v) => v.pId === 0);
  860. arr.forEach((v) => {
  861. v.children = [];
  862. });
  863. arr.forEach((v, i) => {
  864. nodes.forEach((vv) => {
  865. if (vv.pId == v.id) {
  866. arr[i].children.push(vv);
  867. }
  868. });
  869. });
  870. console.log(arr);
  871. $scope.copyInspectionType = arr;
  872. }
  873. },
  874. };
  875. });
  876. app.directive("zTreeCopy", function () {
  877. // ztree liaomingming
  878. return {
  879. restrict: "EA",
  880. scope: {
  881. copyInspectionType: "=",
  882. changecate: "=",
  883. selectedchangecategory: "=",
  884. },
  885. link: function (scope, element, attrs) {
  886. scope.$watch("copyInspectionType", function (newVal) {
  887. console.log(newVal);
  888. var setting = {
  889. view: {
  890. showIcon: false,
  891. showLine: false,
  892. },
  893. callback: {
  894. onClick: onMouseDown,
  895. },
  896. };
  897. $.fn.zTree.destroy("z-tree-copy");
  898. var treeObj = $.fn.zTree.init($(element), setting, newVal);
  899. treeObj.expandAll(true);
  900. });
  901. //点击菜单项
  902. function onMouseDown(event, treeId, treeNode) {
  903. console.log(event, treeId, treeNode);
  904. scope.selectedchangecategory = treeNode;
  905. scope.changecate = true;
  906. console.log(scope.copyInspectionType);
  907. }
  908. },
  909. controller: function ($scope, $element, api_bpm_data) {},
  910. };
  911. });