inspectionScopeCtrl.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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. $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. // 缓存的绑定数据-http
  649. $scope.cacheHttp = {
  650. value: [],
  651. };
  652. },
  653. ]);
  654. app.directive("zTree", function () {
  655. // ztree liaomingming
  656. return {
  657. restrict: "EA",
  658. scope: {
  659. selectInspectionType: "=",
  660. copyInspectionType: "=",
  661. stateModel: "=",
  662. changecate: "=",
  663. cachehttp: "=",
  664. },
  665. controller: function ($scope, $element, api_bpm_data, api_simple) {
  666. console.log($scope);
  667. getTreeInfo();
  668. function getTreeInfo() {
  669. $scope.treeInfo = [];
  670. var data = { idx: 0, sum: 1000, inspectionType: { deleteFlag: 0 } };
  671. $scope.isShowZtree = false;
  672. api_bpm_data
  673. .fetchDataList("inspectionType", data)
  674. .then(function (response) {
  675. var data = response.list;
  676. console.log(data);
  677. var objects = [];
  678. for (var i = 0; i < data.length; i++) {
  679. var object = {};
  680. object.id = data[i].id;
  681. if (angular.isDefined(data[i].user)) {
  682. object.user = data[i].user || {};
  683. }
  684. if (angular.isDefined(data[i].group)) {
  685. object.group = data[i].group || {};
  686. }
  687. if (angular.isDefined(data[i].parent)) {
  688. object.parent = data[i].parent;
  689. object.pId = data[i].parent.id;
  690. } else {
  691. object.pId = 0;
  692. }
  693. object.name = data[i].type;
  694. objects.push(object);
  695. }
  696. $scope.treeInfo = convertParentToChildList(objects);
  697. $scope.treeInfo = deleteEmputyChildren($scope.treeInfo);
  698. console.log($scope.treeInfo);
  699. // ==================start================
  700. api_simple
  701. .fetchDataList("inspectionBind", {
  702. idx: 0,
  703. sum: 1000,
  704. inspectionBind: { inspection: { id: $scope.stateModel.id } },
  705. })
  706. .then(function (response) {
  707. $.fn.zTree.destroy("z-tree");
  708. $.fn.zTree.init($($element), setting, $scope.treeInfo);
  709. $scope.isShowZtree = true;
  710. var result = response.list || [];
  711. $scope.cachehttp = angular.copy(result);
  712. result = result.map((v) => v.inspectionType);
  713. console.log(result);
  714. result.forEach((v, i) => {
  715. // 设置路径树节点选中
  716. // 1、根据id获取树的某个节点:
  717. var zTree = $.fn.zTree.getZTreeObj("z-tree");
  718. var node = zTree.getNodeByParam("id", v);
  719. // 2、设置node节点选中状态:
  720. zTree.selectNode(node);
  721. // 3、设置node节点checked选中:
  722. node.checked = true;
  723. //注:设置checked属性之后,一定要更新该节点
  724. zTree.updateNode(node);
  725. // 展开
  726. zTree.expandNode(node);
  727. setTimeout(() => {
  728. zTreeOnCheck(null, null, node);
  729. }, 0);
  730. });
  731. });
  732. // ==================end================
  733. });
  734. }
  735. function deleteEmputyChildren(data) {
  736. data.forEach((v) => {
  737. if (v.children.length) {
  738. deleteEmputyChildren(v.children);
  739. } else {
  740. delete v.children;
  741. }
  742. });
  743. return data;
  744. }
  745. function convertParentToChildList(data) {
  746. var treeMap = {};
  747. var list = [];
  748. convertListToTree(data, treeMap);
  749. angular.forEach(treeMap, function (item) {
  750. list.push(item);
  751. });
  752. return list;
  753. }
  754. function convertListToTree(data, treeMap) {
  755. var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
  756. var root = null; //Initially set our loop to null
  757. //loop over data
  758. for (var i = 0; i < data.length; i++) {
  759. var datum = data[i];
  760. //each node will have children, so let's give it a "children" poperty
  761. datum.children = [];
  762. //add an entry for this node to the map so that any future children can
  763. //lookup the parent
  764. idToNodeMap[datum.id] = datum;
  765. //Does this node have a parent?
  766. // console.log("datum="+JSON.stringify(datum))
  767. // if (typeof datum.parent === "undefined") {
  768. if (datum.pId === 0) {
  769. //Doesn't look like it, so this node is the root of the tree
  770. root = datum;
  771. treeMap[datum.id] = root;
  772. } else {
  773. //This node has a parent, so let's look it up using the id
  774. // parentNode = idToNodeMap[datum.parent.id];
  775. parentNode = idToNodeMap[datum.pId];
  776. //We don't need this property, so let's delete it.
  777. // delete datum.parent;
  778. //Let's add the current node as a child of the parent node.
  779. parentNode.children.push(datum);
  780. }
  781. }
  782. return root;
  783. }
  784. var setting = {
  785. check: {
  786. enable: true,
  787. chkStyle: "checkbox",
  788. chkboxType: { Y: "ps", N: "ps" },
  789. },
  790. callback: {
  791. onClick: onMouseDown,
  792. onCheck: zTreeOnCheck,
  793. },
  794. view: {
  795. showIcon: false,
  796. showLine: false,
  797. },
  798. };
  799. // $.fn.zTree.init($($element), setting, $scope.treeInfo);
  800. //点击菜单项
  801. function onMouseDown(event, treeId, treeNode) {
  802. console.log(event, treeId, treeNode);
  803. $scope.selectInspectionType = treeNode;
  804. }
  805. // 点击复选框
  806. function zTreeOnCheck(event, treeId, treeNode) {
  807. console.log(event, treeId, treeNode);
  808. $scope.changecate = false;
  809. var treeObj = $.fn.zTree.getZTreeObj("z-tree");
  810. var nodes_arr = treeObj.getCheckedNodes(true);
  811. var nodes = angular.copy(nodes_arr);
  812. console.log(nodes);
  813. var arr = nodes.filter((v) => v.pId === 0);
  814. arr.forEach((v) => {
  815. v.children = [];
  816. });
  817. arr.forEach((v, i) => {
  818. nodes.forEach((vv) => {
  819. if (vv.pId == v.id) {
  820. arr[i].children.push(vv);
  821. }
  822. });
  823. });
  824. if ($scope.cachehttp && $scope.cachehttp.length) {
  825. var result = angular.copy($scope.cachehttp);
  826. zTreeOnCheckCommon(result, nodes);
  827. } else {
  828. api_simple
  829. .fetchDataList("inspectionBind", {
  830. idx: 0,
  831. sum: 1000,
  832. inspectionBind: { inspection: { id: $scope.stateModel.id } },
  833. })
  834. .then(function (response) {
  835. var result = response.list || [];
  836. zTreeOnCheckCommon(result, nodes);
  837. });
  838. }
  839. }
  840. // 点击复选框-common
  841. function zTreeOnCheckCommon(result, nodes) {
  842. // 如果有值,则不覆盖
  843. if($scope.copyInspectionType.length){
  844. result = [];
  845. $scope.copyInspectionType.forEach(v=>{
  846. result.push(v);
  847. if(v.children && v.children.length){
  848. v.children.forEach(vv=>{
  849. result.push(vv);
  850. })
  851. }
  852. })
  853. }else{
  854. result = result.map((v) => ({
  855. id: v.inspectionType,
  856. name: v.inspectionTypeName,
  857. group: v.group,
  858. user: v.user,
  859. parent: v.parent ? { id: v.parent.inspectionType } : undefined,
  860. pId: v.parent ? v.parent.inspectionType : 0,
  861. }));
  862. }
  863. for (var i = 0; i < nodes.length; i++) {
  864. for (var j = 0; j < result.length; j++) {
  865. if (nodes[i].id == result[j].id) {
  866. nodes[i] = angular.copy(result[j]);
  867. }
  868. }
  869. }
  870. var arr = nodes.filter((v) => v.pId === 0);
  871. arr.forEach((v) => {
  872. v.children = [];
  873. });
  874. arr.forEach((v, i) => {
  875. nodes.forEach((vv) => {
  876. if (vv.pId == v.id) {
  877. arr[i].children.push(vv);
  878. }
  879. });
  880. });
  881. console.log(arr);
  882. $scope.copyInspectionType = arr;
  883. }
  884. },
  885. };
  886. });
  887. app.directive("zTreeCopy", function () {
  888. // ztree liaomingming
  889. return {
  890. restrict: "EA",
  891. scope: {
  892. copyInspectionType: "=",
  893. changecate: "=",
  894. selectedchangecategory: "=",
  895. },
  896. link: function (scope, element, attrs) {
  897. scope.$watch("copyInspectionType", function (newVal) {
  898. console.log(newVal);
  899. var setting = {
  900. view: {
  901. showIcon: false,
  902. showLine: false,
  903. },
  904. callback: {
  905. onClick: onMouseDown,
  906. },
  907. };
  908. $.fn.zTree.destroy("z-tree-copy");
  909. var treeObj = $.fn.zTree.init($(element), setting, newVal);
  910. treeObj.expandAll(true);
  911. });
  912. //点击菜单项
  913. function onMouseDown(event, treeId, treeNode) {
  914. console.log(event, treeId, treeNode);
  915. scope.selectedchangecategory = treeNode;
  916. scope.changecate = true;
  917. console.log(scope.copyInspectionType);
  918. }
  919. },
  920. controller: function ($scope, $element, api_bpm_data) {},
  921. };
  922. });