summaryDetailCtrl.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. "use strict";
  2. /**
  3. * controller for User Profile Example
  4. */
  5. app.controller("summaryDetailCtrl", [
  6. "$rootScope",
  7. "$scope",
  8. "$state",
  9. "$stateParams",
  10. "$timeout",
  11. "$interval",
  12. "$modal",
  13. "SweetAlert",
  14. "i18nService",
  15. "uiGridConstants",
  16. "uiGridGroupingConstants",
  17. "Restangular",
  18. "api_bpm_schedule",
  19. "api_bpm_data",
  20. "api_wechatfile",
  21. "api_configure_data",
  22. "moment",
  23. function (
  24. $rootScope,
  25. $scope,
  26. $state,
  27. $stateParams,
  28. $timeout,
  29. $interval,
  30. $modal,
  31. SweetAlert,
  32. i18nService,
  33. uiGridConstants,
  34. uiGridGroupingConstants,
  35. Restangular,
  36. api_bpm_schedule,
  37. api_bpm_data,
  38. api_wechatfile,
  39. api_configure_data,
  40. moment
  41. ) {
  42. $scope.langs = i18nService.getAllLangs();
  43. $scope.lang = "zh-cn";
  44. i18nService.setCurrentLang($scope.lang);
  45. var loginUser = $rootScope.user;
  46. $scope.summary_hc_add = false;
  47. $scope.summary_hc_remove = false;
  48. $scope.summary_hc_edit = false;
  49. $scope.summary_gs_add = false;
  50. $scope.summary_gs_remove = false;
  51. $scope.summary_gs_edit = false;
  52. for (var i = 0; i < loginUser.menu.length; i++) {
  53. if (loginUser.menu[i].link == "summary_hc_add") {
  54. $scope.summary_hc_add = true;
  55. }
  56. if (loginUser.menu[i].link == "summary_hc_remove") {
  57. $scope.summary_hc_remove = true;
  58. }
  59. if (loginUser.menu[i].link == "summary_hc_edit") {
  60. $scope.summary_hc_edit = true;
  61. }
  62. if (loginUser.menu[i].link == "summary_gs_add") {
  63. $scope.summary_gs_add = true;
  64. }
  65. if (loginUser.menu[i].link == "summary_gs_remove") {
  66. $scope.summary_gs_remove = true;
  67. }
  68. if (loginUser.menu[i].link == "summary_gs_edit") {
  69. $scope.summary_gs_edit = true;
  70. }
  71. }
  72. // 获取信息
  73. $scope.consumableList = [];
  74. $scope.incidentDTO = {};
  75. $scope.workHourManagementList = [];
  76. $scope.totalMaintenancePrice = null;
  77. $scope.totalPriceConsumable = null;
  78. $scope.totalPriceWorkHourManagement = null;
  79. $scope.getInfo = function(){
  80. console.log($stateParams.incidentId);
  81. $rootScope.isMask = true;
  82. api_bpm_data.querySummaryDoc({"incidentId":$stateParams.incidentId}).then(
  83. function (data) {
  84. $rootScope.isMask = false;
  85. $scope.consumableList = data.consumableList || [];
  86. $scope.incidentDTO = data.incidentDTO || {};
  87. $scope.workHourManagementList = data.workHourManagementList || [];
  88. $scope.totalMaintenancePrice = data.totalMaintenancePrice;
  89. $scope.totalPriceConsumable = data.consumablePrice;
  90. $scope.totalPriceWorkHourManagement = data.workHourPrice;
  91. },
  92. function () {
  93. $rootScope.isMask = false;
  94. }
  95. );
  96. }
  97. $scope.getInfo();
  98. //返回列表
  99. $scope.goToList = function(){
  100. $state.go("app.incident.summary");
  101. }
  102. $scope.transferTime = function (time) {
  103. if(time){
  104. return moment(time).format('YYYY-MM-DD HH:mm');
  105. }else{
  106. return '';
  107. }
  108. }
  109. //查看事件
  110. $scope.goToIncident = function(){
  111. $state.go('app.incident.incidentDetail', {
  112. id: $scope.incidentDTO.id,
  113. });
  114. }
  115. // 撤销耗材
  116. $scope.removeHc = function(consumable){
  117. var modalInstance = $modal.open({
  118. templateUrl: "assets/views/delete.html",
  119. controller: function ($scope, $modalInstance) {
  120. $scope.title = "撤销";
  121. $scope.connect = "您确认要撤销此条耗材("+ consumable.consumableName + "【"+ consumable.consumablesNum +"】" +")的使用吗?";
  122. $scope.ok = function () {
  123. $modalInstance.close("start");
  124. };
  125. $scope.cancel = function () {
  126. $modalInstance.dismiss("cancel");
  127. };
  128. },
  129. size: "sm",
  130. });
  131. modalInstance.result.then(
  132. function (result) {
  133. if(result == 'start'){
  134. $rootScope.isMask = true;
  135. api_bpm_data.removeHc({
  136. summaryId: +$stateParams.id,
  137. remove: 'remove',
  138. consumableList: [
  139. {
  140. consumablesId: consumable.consumableId,
  141. consumablesNum: consumable.consumablesNum,
  142. }
  143. ]
  144. }).then(
  145. function (response) {
  146. $rootScope.isMask = false;
  147. if(response.status == 200){
  148. SweetAlert.swal({
  149. title: "撤销成功!",
  150. type: "success"
  151. }, function() {
  152. $scope.getInfo()
  153. })
  154. } else {
  155. SweetAlert.swal({
  156. title: "撤销失败!",
  157. text: response.msg || '请求数据失败!',
  158. type: "error"
  159. }, function() {
  160. $scope.getInfo()
  161. })
  162. }
  163. },
  164. function () {
  165. $rootScope.isMask = false;
  166. }
  167. );
  168. }
  169. },
  170. function () {}
  171. );
  172. }
  173. const uniqBy = (arr, predicate) => {
  174. const cb = typeof predicate === 'function' ? predicate : (o) => o[predicate];
  175. return [...arr.reduce((map, item) => {
  176. const key = (item === null || item === undefined) ?
  177. item : cb(item);
  178. map.has(key) || map.set(key, item);
  179. return map;
  180. }, new Map()).values()];
  181. };
  182. // 修改耗材
  183. $scope.updateHc = function(consumable){
  184. console.log(consumable)
  185. var modalInstance = $modal.open({
  186. templateUrl: 'assets/views/system/tpl/hcChange.html',
  187. controller: function($scope, scope, $modalInstance, api_user_data, api_bpm) {
  188. $scope.title = '耗材修改';
  189. $scope.isAdd = false;
  190. $scope.dataInfo = {
  191. num: consumable.consumablesNum,
  192. consumable: {id: consumable.consumableId, name: consumable.consumableName},
  193. }
  194. $scope.getHcList=function(keyword = ''){
  195. // 查询故障耗材列表
  196. var postData = {
  197. "idx": 0,
  198. "sum": 20,
  199. incidentCategoryConsumable: {
  200. duty: $stateParams.dutyId,
  201. category: { id: $stateParams.categoryId },
  202. }
  203. };
  204. api_user_data.fetchDataList('incidentCategoryConsumable',postData).then(function(res){
  205. if(res.status == 200){
  206. let list = res.list || [];
  207. if(list.length){
  208. let arr = list.map(v => v.consumableDTOS).flat();
  209. $scope.hcList = uniqBy(arr, 'id');
  210. }else{
  211. // 查询耗材列表
  212. var postData = {
  213. "idx": 0,
  214. "sum": 20,
  215. consumable: {
  216. keyWord: keyword,
  217. dutyDTO: { id: $stateParams.dutyId },
  218. showZero: true,
  219. }
  220. };
  221. api_user_data.fetchDataList('consumable',postData).then(function(res){
  222. if(res.status == 200){
  223. $scope.hcList = res.list || [];
  224. }else{
  225. uni.showToast({
  226. icon: 'none',
  227. title: res.msg || '请求数据失败!'
  228. });
  229. }
  230. })
  231. }
  232. }else{
  233. uni.showToast({
  234. icon: 'none',
  235. title: res.msg || '请求数据失败!'
  236. });
  237. }
  238. })
  239. }
  240. $scope.getHcList();
  241. $scope.changeNum = function(val){
  242. $scope.dataInfo.num = val?Math.abs(parseFloat(val.toFixed(2), 10)):val;
  243. }
  244. $scope.cancel = function() {
  245. $modalInstance.dismiss('cancel');
  246. };
  247. $scope.savercode = function() {
  248. if ($scope.dataInfo.num && $scope.dataInfo.consumable && $scope.dataInfo.consumable.id){
  249. $rootScope.isMask = true;
  250. let postData = {
  251. "consumableList": [
  252. {
  253. "consumablesId": $scope.dataInfo.consumable ? $scope.dataInfo.consumable.id : undefined,
  254. "consumablesNum": $scope.dataInfo.num
  255. }
  256. ],
  257. modifyNum: "edit",
  258. "summaryId": $stateParams.id,
  259. };
  260. api_bpm.addSummaryDoc(postData).then(function(response) {
  261. if (response) {
  262. if (response.status == 200) {
  263. SweetAlert.swal({
  264. title: "修改成功!",
  265. type: "success"
  266. }, function() {
  267. scope.getInfo()
  268. })
  269. } else {
  270. SweetAlert.swal({
  271. title: "修改失败!",
  272. text: response.msg || '请求数据失败!',
  273. type: "error"
  274. }, function() {
  275. scope.getInfo()
  276. })
  277. }
  278. $modalInstance.close();
  279. }else{
  280. SweetAlert.swal({
  281. title: "修改失败!",
  282. text: response.msg || '请求数据失败!',
  283. type: "error"
  284. }, function() {
  285. scope.getInfo()
  286. })
  287. }
  288. })
  289. }else{
  290. SweetAlert.swal({
  291. title: "新增失败!",
  292. text: "请填写必填项!",
  293. type: "error",
  294. confirmButtonColor: "#DD6B55"
  295. }, function () {
  296. });
  297. }
  298. }
  299. },
  300. resolve: {
  301. scope: function() {
  302. return $scope;
  303. }
  304. }
  305. });
  306. }
  307. // 新增耗材
  308. $scope.addHc = function(){
  309. var modalInstance = $modal.open({
  310. templateUrl: 'assets/views/system/tpl/hcChange.html',
  311. controller: function($scope, scope, $modalInstance, api_user_data, api_bpm) {
  312. $scope.title = '耗材新增';
  313. $scope.isAdd = true;
  314. $scope.dataInfo = {
  315. num: 1,
  316. consumable: null,
  317. }
  318. $scope.getHcList=function(keyword = ''){
  319. // 查询故障耗材列表
  320. var postData = {
  321. "idx": 0,
  322. "sum": 20,
  323. incidentCategoryConsumable: {
  324. duty: $stateParams.dutyId,
  325. category: { id: $stateParams.categoryId },
  326. }
  327. };
  328. api_user_data.fetchDataList('incidentCategoryConsumable',postData).then(function(res){
  329. if(res.status == 200){
  330. let list = res.list || [];
  331. if(list.length){
  332. let arr = list.map(v => v.consumableDTOS).flat();
  333. $scope.hcList = uniqBy(arr, 'id');
  334. }else{
  335. // 查询耗材列表
  336. var postData = {
  337. "idx": 0,
  338. "sum": 20,
  339. consumable: {
  340. keyWord: keyword,
  341. dutyDTO: { id: $stateParams.dutyId },
  342. showZero: true,
  343. }
  344. };
  345. api_user_data.fetchDataList('consumable',postData).then(function(res){
  346. if(res.status == 200){
  347. $scope.hcList = res.list || [];
  348. }else{
  349. uni.showToast({
  350. icon: 'none',
  351. title: res.msg || '请求数据失败!'
  352. });
  353. }
  354. })
  355. }
  356. }else{
  357. uni.showToast({
  358. icon: 'none',
  359. title: res.msg || '请求数据失败!'
  360. });
  361. }
  362. })
  363. }
  364. $scope.getHcList();
  365. $scope.changeNum = function(val){
  366. $scope.dataInfo.num = val?Math.abs(parseFloat(val.toFixed(2), 10)):val;
  367. }
  368. $scope.cancel = function() {
  369. $modalInstance.dismiss('cancel');
  370. };
  371. $scope.savercode = function() {
  372. if ($scope.dataInfo.num && $scope.dataInfo.consumable && $scope.dataInfo.consumable.id){
  373. $rootScope.isMask = true;
  374. let postData = {
  375. "consumableList": [
  376. {
  377. "consumablesId": $scope.dataInfo.consumable ? $scope.dataInfo.consumable.id : undefined,
  378. "consumablesNum": $scope.dataInfo.num
  379. }
  380. ],
  381. "summaryId": $stateParams.id,
  382. };
  383. api_bpm.addSummaryDoc(postData).then(function(response) {
  384. if (response) {
  385. if (response.status == 200) {
  386. SweetAlert.swal({
  387. title: "新增成功!",
  388. type: "success"
  389. }, function() {
  390. scope.getInfo()
  391. })
  392. } else {
  393. SweetAlert.swal({
  394. title: "新增失败!",
  395. text: response.msg || '请求数据失败!',
  396. type: "error"
  397. }, function() {
  398. scope.getInfo()
  399. })
  400. }
  401. $modalInstance.close();
  402. }else{
  403. SweetAlert.swal({
  404. title: "新增失败!",
  405. text: response.msg || '请求数据失败!',
  406. type: "error"
  407. }, function() {
  408. scope.getInfo()
  409. })
  410. }
  411. })
  412. }else{
  413. SweetAlert.swal({
  414. title: "新增失败!",
  415. text: "请填写必填项!",
  416. type: "error",
  417. confirmButtonColor: "#DD6B55"
  418. }, function () {
  419. });
  420. }
  421. }
  422. },
  423. resolve: {
  424. scope: function() {
  425. return $scope;
  426. }
  427. }
  428. });
  429. }
  430. // 撤销工时
  431. $scope.removeGs = function(workHourManagement){
  432. var modalInstance = $modal.open({
  433. templateUrl: "assets/views/delete.html",
  434. controller: function ($scope, $modalInstance) {
  435. $scope.title = "撤销";
  436. $scope.connect = "您确认要撤销此条工时("+ workHourManagement.workName + "【"+ workHourManagement.workHourNum2 +"】" +")的使用吗?";
  437. $scope.ok = function () {
  438. $modalInstance.close("start");
  439. };
  440. $scope.cancel = function () {
  441. $modalInstance.dismiss("cancel");
  442. };
  443. },
  444. size: "sm",
  445. });
  446. modalInstance.result.then(
  447. function (result) {
  448. if(result == 'start'){
  449. $rootScope.isMask = true;
  450. api_bpm_data.removeHc({
  451. summaryId: +$stateParams.id,
  452. remove: 'remove',
  453. workHourManagementList: [
  454. {
  455. workHourId: workHourManagement.id,
  456. workHourNum: workHourManagement.workHourNum,
  457. }
  458. ]
  459. }).then(
  460. function (response) {
  461. $rootScope.isMask = false;
  462. if(response.status == 200){
  463. SweetAlert.swal({
  464. title: "撤销成功!",
  465. type: "success"
  466. }, function() {
  467. $scope.getInfo()
  468. })
  469. } else {
  470. SweetAlert.swal({
  471. title: "撤销失败!",
  472. text: response.msg || '请求数据失败!',
  473. type: "error"
  474. }, function() {
  475. $scope.getInfo()
  476. })
  477. }
  478. },
  479. function () {
  480. $rootScope.isMask = false;
  481. }
  482. );
  483. }
  484. },
  485. function () {}
  486. );
  487. }
  488. // 修改工时
  489. $scope.updateGs = function(workHourManagement){
  490. console.log(workHourManagement)
  491. var modalInstance = $modal.open({
  492. templateUrl: 'assets/views/system/tpl/gsChange.html',
  493. controller: function($scope, scope, $modalInstance, api_user_data, api_bpm) {
  494. $scope.title = '工时修改';
  495. $scope.isAdd = false;
  496. $scope.dataInfo = {
  497. num: workHourManagement.workHourNum2,
  498. workHourManagement: [{id: workHourManagement.id, name: workHourManagement.workName}],
  499. }
  500. // --------------------
  501. $scope.select_treedata = [];
  502. $rootScope.bala1 = $scope.getGsList = function (s, fn) {
  503. if (s) {
  504. var filterKeyword = s.filterKeyword;
  505. }
  506. var postData = {
  507. idx: 0,
  508. sum: 9999,
  509. };
  510. $scope.my_data = [];
  511. $scope.doing_async = true;
  512. api_bpm_data
  513. .fetchDataList("workHourManagement", postData)
  514. .then(function (response) {
  515. if (response.status == 200) {
  516. var data = response.list;
  517. if (filterKeyword) {
  518. data.forEach((e) => {
  519. e.isExpanded = true;
  520. });
  521. var li = transform(data).children;
  522. console.log(li);
  523. fn(li);
  524. return;
  525. } else {
  526. var objects = [];
  527. for (var i = 0; i < data.length; i++) {
  528. var object = {};
  529. object.id = data[i].id;
  530. object.parent = data[i].parent;
  531. object.workName = data[i].workName;
  532. object.isExpanded = true;
  533. objects.push(object);
  534. }
  535. $scope.my_data = convertParentToChildList(objects);
  536. $scope.select_treedata = angular.copy($scope.my_data);
  537. console.log($scope.select_treedata)
  538. }
  539. if ($scope.my_data.length > 0) {
  540. $scope.doing_async = false;
  541. }
  542. selectItem(workHourManagement,$scope.select_treedata);
  543. $scope.dataInfo.workHourManagement = workHourManagement;
  544. } else {
  545. SweetAlert.swal({
  546. title: "系统错误!",
  547. text: "请刷新重试!",
  548. type: "error",
  549. });
  550. }
  551. });
  552. };
  553. $scope.getGsList();
  554. // --------------------
  555. $scope.changeNum = function(val){
  556. $scope.dataInfo.num = val?Math.abs(parseFloat(val.toFixed(2), 10)):val;
  557. }
  558. $scope.cancel = function() {
  559. $modalInstance.dismiss('cancel');
  560. };
  561. $scope.savercode = function() {
  562. if ($scope.dataInfo.num && $scope.dataInfo.workHourManagement.length){
  563. $rootScope.isMask = true;
  564. let postData = {
  565. "workHourManagementList": $scope.dataInfo.workHourManagement.map(v => ({ workHourId: v.id, workHourNum: $scope.dataInfo.num })),
  566. "summaryId": $stateParams.id,
  567. modifyNum: "edit"
  568. };
  569. api_bpm.addSummaryDoc(postData).then(function(response) {
  570. if (response) {
  571. if (response.status == 200) {
  572. SweetAlert.swal({
  573. title: "修改成功!",
  574. type: "success"
  575. }, function() {
  576. scope.getInfo()
  577. })
  578. } else {
  579. SweetAlert.swal({
  580. title: "修改失败!",
  581. text: response.msg || '请求数据失败!',
  582. type: "error"
  583. }, function() {
  584. scope.getInfo()
  585. })
  586. }
  587. $modalInstance.close();
  588. }else{
  589. SweetAlert.swal({
  590. title: "修改失败!",
  591. text: response.msg || '请求数据失败!',
  592. type: "error"
  593. }, function() {
  594. scope.getInfo()
  595. })
  596. }
  597. })
  598. }else{
  599. SweetAlert.swal({
  600. title: "新增失败!",
  601. text: "请填写必填项!",
  602. type: "error",
  603. confirmButtonColor: "#DD6B55"
  604. }, function () {
  605. });
  606. }
  607. }
  608. },
  609. resolve: {
  610. scope: function() {
  611. return $scope;
  612. }
  613. }
  614. });
  615. }
  616. // 新增工时
  617. $scope.addGs = function(){
  618. var modalInstance = $modal.open({
  619. templateUrl: 'assets/views/system/tpl/gsChange.html',
  620. controller: function($scope, scope, $modalInstance, api_user_data, api_bpm) {
  621. $scope.title = '工时新增';
  622. $scope.isAdd = true;
  623. $scope.dataInfo = {
  624. // num: 1,
  625. workHourManagement: [],
  626. }
  627. // --------------------
  628. $scope.select_treedata = [];
  629. $rootScope.bala1 = $scope.getGsList = function (s, fn) {
  630. if (s) {
  631. var filterKeyword = s.filterKeyword;
  632. }
  633. var postData = {
  634. idx: 0,
  635. sum: 9999,
  636. };
  637. $scope.my_data = [];
  638. $scope.doing_async = true;
  639. api_bpm_data
  640. .fetchDataList("workHourManagement", postData)
  641. .then(function (response) {
  642. if (response.status == 200) {
  643. var data = response.list;
  644. if (filterKeyword) {
  645. data.forEach((e) => {
  646. e.isExpanded = true;
  647. });
  648. var li = transform(data).children;
  649. console.log(li);
  650. fn(li);
  651. return;
  652. } else {
  653. var objects = [];
  654. for (var i = 0; i < data.length; i++) {
  655. var object = {};
  656. object.id = data[i].id;
  657. object.parent = data[i].parent;
  658. object.workName = data[i].workName;
  659. object.isExpanded = true;
  660. objects.push(object);
  661. }
  662. $scope.my_data = convertParentToChildList(objects);
  663. $scope.select_treedata = angular.copy($scope.my_data);
  664. console.log($scope.select_treedata)
  665. }
  666. if ($scope.my_data.length > 0) {
  667. $scope.doing_async = false;
  668. }
  669. // selectItem(selectdata.workName,$scope.select_treedata);
  670. // $scope.deptdata.workName = selectdata.workName;
  671. } else {
  672. SweetAlert.swal({
  673. title: "系统错误!",
  674. text: "请刷新重试!",
  675. type: "error",
  676. });
  677. }
  678. });
  679. };
  680. $scope.getGsList();
  681. // --------------------
  682. $scope.changeNum = function(val){
  683. $scope.dataInfo.num = val?Math.abs(parseFloat(val.toFixed(2), 10)):val;
  684. }
  685. $scope.cancel = function() {
  686. $modalInstance.dismiss('cancel');
  687. };
  688. $scope.savercode = function() {
  689. console.log($scope.dataInfo);
  690. // if ($scope.dataInfo.num && $scope.dataInfo.workHourManagement.length){
  691. if ($scope.dataInfo.workHourManagement.length){
  692. $rootScope.isMask = true;
  693. let workHourManagementList = scope.workHourManagementList || []
  694. let postData = {
  695. "workHourManagementList": $scope.dataInfo.workHourManagement.concat(workHourManagementList).map(v => ({ workHourId: v.id, workHourNum: v.workHourNum2 || 1 })),
  696. "summaryId": $stateParams.id,
  697. };
  698. api_bpm.addSummaryDoc(postData).then(function(response) {
  699. if (response) {
  700. if (response.status == 200) {
  701. SweetAlert.swal({
  702. title: "新增成功!",
  703. type: "success"
  704. }, function() {
  705. scope.getInfo()
  706. })
  707. } else {
  708. SweetAlert.swal({
  709. title: "新增失败!",
  710. text: response.msg || '请求数据失败!',
  711. type: "error"
  712. }, function() {
  713. scope.getInfo()
  714. })
  715. }
  716. $modalInstance.close();
  717. }else{
  718. SweetAlert.swal({
  719. title: "新增失败!",
  720. text: response.msg || '请求数据失败!',
  721. type: "error"
  722. }, function() {
  723. scope.getInfo()
  724. })
  725. }
  726. })
  727. }else{
  728. SweetAlert.swal({
  729. title: "新增失败!",
  730. text: "请填写必填项!",
  731. type: "error",
  732. confirmButtonColor: "#DD6B55"
  733. }, function () {
  734. });
  735. }
  736. }
  737. },
  738. resolve: {
  739. scope: function() {
  740. return $scope;
  741. }
  742. }
  743. });
  744. }
  745. function selectItem(pmodel, childrens) {
  746. if (angular.isArray(pmodel)) {
  747. angular.forEach(pmodel, function (index) {
  748. if (index && index.id) {
  749. angular.forEach(childrens, function (item) {
  750. if (item.id == index.id) {
  751. item.selected = true;
  752. }
  753. if (item && item.children) {
  754. selectItem(pmodel, item.children);
  755. }
  756. });
  757. }
  758. });
  759. } else {
  760. if (pmodel && pmodel.id) {
  761. angular.forEach(childrens, function (item, index) {
  762. if (item.id == pmodel.id) {
  763. item.selected = true;
  764. }
  765. if (item && item.children) {
  766. selectItem(pmodel, item.children);
  767. }
  768. });
  769. }
  770. }
  771. }
  772. function convertParentToChildList(data) {
  773. var treeMap = {};
  774. var list = [];
  775. convertListToTree(data, treeMap);
  776. angular.forEach(treeMap, function (item) {
  777. list.push(item);
  778. });
  779. return list;
  780. }
  781. function convertListToTree(data, treeMap) {
  782. var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
  783. var root = null; //Initially set our loop to null
  784. var parentNode = null;
  785. //loop over data
  786. for (var i = 0; i < data.length; i++) {
  787. var datum = data[i];
  788. //each node will have children, so let's give it a "children" poperty
  789. datum.children = [];
  790. //add an entry for this node to the map so that any future children can
  791. //lookup the parent
  792. idToNodeMap[datum.id] = datum;
  793. //Does this node have a parent?
  794. if (typeof datum.parent === "undefined" || datum.parent == null) {
  795. //Doesn't look like it, so this node is the root of the tree
  796. root = datum;
  797. treeMap[datum.id] = root;
  798. } else {
  799. //This node has a parent, so let's look it up using the id
  800. parentNode = idToNodeMap[datum.parent.id];
  801. //We don't need this property, so let's delete it.
  802. delete datum.parent;
  803. //Let's add the current node as a child of the parent node.
  804. parentNode.children.push(datum);
  805. }
  806. }
  807. return root;
  808. }
  809. //树形图
  810. // 将故障现象搜索结果返回的数据整理成children模式
  811. function transform(nodes) {
  812. var treeConverter = {
  813. result: null, //转化后的结果,是根节点,所有节点都是从根节点长出来的
  814. attributeName: 'id', //节点唯一标识符
  815. needFind: true, //是否查询节点在result中已经存在,为了优化效率
  816. transform: function (node) { //转化递归函数,参数:一个待插入节点
  817. if (node.parent != null) { //该节点有父节点
  818. var newNode = this.transform(node.parent); //递归进入,返回值为一个节点,用作父节点,该父节点必然存在于result中,这点由下面的算法可以控制
  819. if (this.needFind) {
  820. for (var i = 0; i < newNode.children.length; i++) { //查找要插入的node子节点是否在newNode这个父节点中存在
  821. if (newNode.children[i][this.attributeName] === node[this.attributeName]) {
  822. return newNode.children[i]; //存在的话直接返回newNode父节点内的该子节点,该子节点必然存在于result中,作为返回值它将被用作上级递归的newNode,因此newNode必然存在于result中
  823. }
  824. }
  825. }
  826. this.needFind = false; //不存在的话,关闭之后递归的循环判断,因为待插入node节点不存在于result中,故而它的子节点一定不存在于result中,不用再循环判断
  827. // delete node.parent; //删除该节点的parent属性,如果有的话
  828. node.children = []; //因为确定是要新插入的节点,没有children:[]属性,故给该节点增加children:[]属性
  829. newNode.children.push(node); //将该node节点push进newNode的子节点数组中
  830. return node; //return该新插入节点,作为递归返回值给上层,用作newNode父节点,node存在于result中故newNode存在于result中
  831. } else if (node.parent == null) { //该叶节点没有父节点,即为根节点
  832. // delete node.parent; //删除该节点的parent属性,如果有的话
  833. if (this.result == null) { //根节点不存在
  834. node.children = []; //给该节点增加children:[]属性
  835. return this.result = node; //该节点赋给result,并return根节点,作为返回值它将被用作上级递归的newNode,因此newNode必然存在于result中
  836. } else {
  837. node.children = [];
  838. // 顶级去重
  839. for (var i = 0; i < this.result.children.length; i++) {
  840. if (this.result.children[i][this.attributeName] === node[this.attributeName]) {
  841. return this.result.children[i];
  842. }
  843. }
  844. this.result.children.push(node)
  845. return node // 直接return根节点,作为返回值它将被用作上级递归的newNode,因此newNode必然存在于result中
  846. }
  847. }
  848. },
  849. getWhole: function (nodes, attributeName) { //传入整个叶子节点数组,attributeName作为节点唯一标识符属性,返回整个转化结果
  850. var _node = {};
  851. _node.children = [];
  852. this.result = _node; //重置根节点
  853. this.attributeName = attributeName == null ? 'id' : attributeName; //唯一标识符默认为“id”
  854. nodes = JSON.parse(JSON.stringify(nodes)); //复制出一个新的节点对象作为参数,保证不改变原有数据
  855. nodes.forEach(item => { //循环调用转化方法
  856. this.needFind = true; //重置开启节点是否已存在判断,保证不插入重复节点
  857. this.transform(item);
  858. })
  859. return this.result; //返回根节点
  860. }
  861. }
  862. var result = treeConverter.getWhole(nodes); //调用
  863. return result;
  864. }
  865. },
  866. ]);