inspectionScopeCtrl.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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.id,
  539. inspectionType: v.id,
  540. inspectionTypeName: v.name,
  541. parent: v.parent,
  542. user: v.user?v.user.id:undefined,
  543. userName: v.user?v.user.name:undefined,
  544. group: v.group?v.group.id:undefined,
  545. groupName: v.group?v.group.groupName:undefined,
  546. }));
  547. // 一条都没选择
  548. if (arr.length === 0) {
  549. arr.push({ inspection: $scope.stateModel.id });
  550. }
  551. console.log(arr);
  552. // return;
  553. api_simple.addListData("inspectionBind", arr).then(function (response) {
  554. if (response) {
  555. if (response.status == 200) {
  556. $scope.stateModel.status = { id: 120 }; //强制扭转状态为正常
  557. var filedata = {
  558. close: "关闭",
  559. save: "暂存",
  560. submit: "下一步",
  561. inspection: $scope.stateModel,
  562. };
  563. api_bpm_data
  564. .addData("inspection", filedata)
  565. .then(function (response) {
  566. if (response) {
  567. if (response.status == 200) {
  568. SweetAlert.swal(
  569. {
  570. title: "保存成功!",
  571. type: "success",
  572. },
  573. function () {
  574. // $scope.selectedchangecategory.value = {};
  575. // $scope.copyInspection.tree = [];
  576. // $scope.changecateObj.changecate = false;
  577. // $scope.selectedInspection.isShowZtree = false;
  578. // setTimeout(()=>{
  579. // $scope.selectedInspection.isShowZtree = true;
  580. // },0)
  581. $state.go("app.inspection.inspectPlan"); //跳转巡检计划列表
  582. }
  583. );
  584. } else {
  585. SweetAlert.swal({
  586. title: "保存失败!",
  587. type: "error",
  588. });
  589. }
  590. }
  591. });
  592. } else {
  593. SweetAlert.swal({
  594. title: "保存失败!",
  595. type: "error",
  596. });
  597. }
  598. }
  599. });
  600. };
  601. $scope.add1data = {};
  602. $scope.addparent = false;
  603. // 事件分类结束
  604. // 获取组数据
  605. $scope.getGroupList = function () {
  606. var postData = {
  607. idx: 0,
  608. sum: 1000,
  609. group: { selectType: "nouser" },
  610. };
  611. api_user_data.fetchDataList("group", postData).then((res) => {
  612. $scope.groupList = res.list;
  613. });
  614. };
  615. $scope.getGroupList();
  616. // 获取巡检计划数据
  617. $scope.getGroupList = function (model) {
  618. var postData = {
  619. idx: 0,
  620. sum: 3,
  621. inspection: { model: model },
  622. };
  623. api_user_data.fetchDataList("inspection", postData).then((res) => {
  624. if (model == 1) {
  625. $scope.listDataTop = res.list; //巡检范围策略模板
  626. } else {
  627. $scope.listDataBottom = res.list; //往期巡检范围策略
  628. }
  629. });
  630. };
  631. $scope.getGroupList(1);
  632. $scope.getGroupList();
  633. // 获取人数据
  634. $scope.getUserList = function () {
  635. var postData = {
  636. idx: 0,
  637. sum: 1000,
  638. user: {
  639. name: "",
  640. selectType: "pinyin_qs",
  641. },
  642. };
  643. api_user_data.fetchDataList("user", postData).then((res) => {
  644. $scope.userList = res.list;
  645. });
  646. };
  647. $scope.getUserList();
  648. $scope.changecateObj = {
  649. changecate: false,
  650. };
  651. // 缓存的绑定数据-http
  652. $scope.cacheHttp = {
  653. value: [],
  654. };
  655. },
  656. ]);
  657. app.directive("zTree", function () {
  658. // ztree liaomingming
  659. return {
  660. restrict: "EA",
  661. scope: {
  662. selectInspectionType: "=",
  663. copyInspectionType: "=",
  664. stateModel: "=",
  665. changecate: "=",
  666. cachehttp: "=",
  667. },
  668. controller: function ($scope, $element, api_bpm_data, api_simple) {
  669. console.log($scope);
  670. getTreeInfo();
  671. function getTreeInfo() {
  672. $scope.treeInfo = [];
  673. var data = { idx: 0, sum: 1000, inspectionType: { deleteFlag: 0 } };
  674. $scope.isShowZtree = false;
  675. api_bpm_data
  676. .fetchDataList("inspectionType", data)
  677. .then(function (response) {
  678. var data = response.list;
  679. console.log(data);
  680. var objects = [];
  681. for (var i = 0; i < data.length; i++) {
  682. var object = {};
  683. object.id = data[i].id;
  684. if (angular.isDefined(data[i].user)) {
  685. object.user = data[i].user || {};
  686. }
  687. if (angular.isDefined(data[i].group)) {
  688. object.group = data[i].group || {};
  689. }
  690. if (angular.isDefined(data[i].parent)) {
  691. object.parent = data[i].parent;
  692. object.pId = data[i].parent.id;
  693. } else {
  694. object.pId = 0;
  695. }
  696. object.name = data[i].type;
  697. objects.push(object);
  698. }
  699. $scope.treeInfo = convertParentToChildList(objects);
  700. $scope.treeInfo = deleteEmputyChildren($scope.treeInfo);
  701. console.log($scope.treeInfo);
  702. // ==================start================
  703. api_simple
  704. .fetchDataList("inspectionBind", {
  705. idx: 0,
  706. sum: 1000,
  707. inspectionBind: { inspection: $scope.stateModel.id },
  708. })
  709. .then(function (response) {
  710. $.fn.zTree.destroy("z-tree");
  711. $.fn.zTree.init($($element), setting, $scope.treeInfo);
  712. $scope.isShowZtree = true;
  713. var result = response.list || [];
  714. $scope.cachehttp = angular.copy(result);
  715. result = result.map((v) => v.inspectionType);
  716. console.log(result);
  717. result.forEach((v, i) => {
  718. // 设置路径树节点选中
  719. // 1、根据id获取树的某个节点:
  720. var zTree = $.fn.zTree.getZTreeObj("z-tree");
  721. var node = zTree.getNodeByParam("id", v);
  722. // 2、设置node节点选中状态:
  723. zTree.selectNode(node);
  724. // 3、设置node节点checked选中:
  725. node.checked = true;
  726. //注:设置checked属性之后,一定要更新该节点
  727. zTree.updateNode(node);
  728. // 展开
  729. zTree.expandNode(node);
  730. setTimeout(() => {
  731. zTreeOnCheck(null, null, node);
  732. }, 0);
  733. });
  734. });
  735. // ==================end================
  736. });
  737. }
  738. function deleteEmputyChildren(data) {
  739. data.forEach((v) => {
  740. if (v.children.length) {
  741. deleteEmputyChildren(v.children);
  742. } else {
  743. delete v.children;
  744. }
  745. });
  746. return data;
  747. }
  748. function convertParentToChildList(data) {
  749. var treeMap = {};
  750. var list = [];
  751. convertListToTree(data, treeMap);
  752. angular.forEach(treeMap, function (item) {
  753. list.push(item);
  754. });
  755. return list;
  756. }
  757. function convertListToTree(data, treeMap) {
  758. var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
  759. var root = null; //Initially set our loop to null
  760. //loop over data
  761. for (var i = 0; i < data.length; i++) {
  762. var datum = data[i];
  763. //each node will have children, so let's give it a "children" poperty
  764. datum.children = [];
  765. //add an entry for this node to the map so that any future children can
  766. //lookup the parent
  767. idToNodeMap[datum.id] = datum;
  768. //Does this node have a parent?
  769. // console.log("datum="+JSON.stringify(datum))
  770. // if (typeof datum.parent === "undefined") {
  771. if (datum.pId === 0) {
  772. //Doesn't look like it, so this node is the root of the tree
  773. root = datum;
  774. treeMap[datum.id] = root;
  775. } else {
  776. //This node has a parent, so let's look it up using the id
  777. // parentNode = idToNodeMap[datum.parent.id];
  778. parentNode = idToNodeMap[datum.pId];
  779. //We don't need this property, so let's delete it.
  780. // delete datum.parent;
  781. //Let's add the current node as a child of the parent node.
  782. parentNode.children.push(datum);
  783. }
  784. }
  785. return root;
  786. }
  787. var setting = {
  788. check: {
  789. enable: true,
  790. chkStyle: "checkbox",
  791. chkboxType: { Y: "ps", N: "ps" },
  792. },
  793. callback: {
  794. onClick: onMouseDown,
  795. onCheck: zTreeOnCheck,
  796. },
  797. view: {
  798. showIcon: false,
  799. showLine: false,
  800. },
  801. };
  802. // $.fn.zTree.init($($element), setting, $scope.treeInfo);
  803. //点击菜单项
  804. function onMouseDown(event, treeId, treeNode) {
  805. console.log(event, treeId, treeNode);
  806. $scope.selectInspectionType = treeNode;
  807. }
  808. // 点击复选框
  809. function zTreeOnCheck(event, treeId, treeNode) {
  810. console.log(event, treeId, treeNode);
  811. $scope.changecate = false;
  812. var treeObj = $.fn.zTree.getZTreeObj("z-tree");
  813. var nodes_arr = treeObj.getCheckedNodes(true);
  814. var nodes = angular.copy(nodes_arr);
  815. console.log(nodes);
  816. var arr = nodes.filter((v) => v.pId === 0);
  817. arr.forEach((v) => {
  818. v.children = [];
  819. });
  820. arr.forEach((v, i) => {
  821. nodes.forEach((vv) => {
  822. if (vv.pId == v.id) {
  823. arr[i].children.push(vv);
  824. }
  825. });
  826. });
  827. if ($scope.cachehttp && $scope.cachehttp.length) {
  828. var result = angular.copy($scope.cachehttp);
  829. zTreeOnCheckCommon(result, nodes);
  830. } else {
  831. api_simple
  832. .fetchDataList("inspectionBind", {
  833. idx: 0,
  834. sum: 1000,
  835. inspectionBind: { inspection: $scope.stateModel.id },
  836. })
  837. .then(function (response) {
  838. var result = response.list || [];
  839. zTreeOnCheckCommon(result, nodes);
  840. });
  841. }
  842. }
  843. // 点击复选框-common
  844. function zTreeOnCheckCommon(result, nodes) {
  845. // 如果有值,则不覆盖
  846. if($scope.copyInspectionType.length){
  847. result = [];
  848. $scope.copyInspectionType.forEach(v=>{
  849. result.push(v);
  850. if(v.children && v.children.length){
  851. v.children.forEach(vv=>{
  852. result.push(vv);
  853. })
  854. }
  855. })
  856. }else{
  857. result = result.map((v) => ({
  858. id: v.inspectionType,
  859. name: v.inspectionTypeName,
  860. group: {id:v.group,groupName:v.groupName},
  861. user: {id:v.user,name:v.userName},
  862. parent: v.parent ? { id: v.parent.inspectionType } : undefined,
  863. pId: v.parent ? v.parent.inspectionType : 0,
  864. }));
  865. }
  866. for (var i = 0; i < nodes.length; i++) {
  867. for (var j = 0; j < result.length; j++) {
  868. if (nodes[i].id == result[j].id) {
  869. nodes[i] = angular.copy(result[j]);
  870. }
  871. }
  872. }
  873. var arr = nodes.filter((v) => v.pId === 0);
  874. arr.forEach((v) => {
  875. v.children = [];
  876. });
  877. arr.forEach((v, i) => {
  878. nodes.forEach((vv) => {
  879. if (vv.pId == v.id) {
  880. arr[i].children.push(vv);
  881. }
  882. });
  883. });
  884. console.log(arr);
  885. $scope.copyInspectionType = arr;
  886. }
  887. },
  888. };
  889. });
  890. app.directive("zTreeCopy", function () {
  891. // ztree liaomingming
  892. return {
  893. restrict: "EA",
  894. scope: {
  895. copyInspectionType: "=",
  896. changecate: "=",
  897. selectedchangecategory: "=",
  898. },
  899. link: function (scope, element, attrs) {
  900. scope.$watch("copyInspectionType", function (newVal) {
  901. console.log(newVal);
  902. var setting = {
  903. view: {
  904. showIcon: false,
  905. showLine: false,
  906. },
  907. callback: {
  908. onClick: onMouseDown,
  909. },
  910. };
  911. $.fn.zTree.destroy("z-tree-copy");
  912. var treeObj = $.fn.zTree.init($(element), setting, newVal);
  913. treeObj.expandAll(true);
  914. });
  915. //点击菜单项
  916. function onMouseDown(event, treeId, treeNode) {
  917. console.log(event, treeId, treeNode);
  918. scope.selectedchangecategory = treeNode;
  919. scope.changecate = true;
  920. console.log(scope.copyInspectionType);
  921. }
  922. },
  923. controller: function ($scope, $element, api_bpm_data) {},
  924. };
  925. });