44a614baf245a2330533d0ecdc387014f6dc6df6.svn-base 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. BemsWebApplication.Point = function (params, viewInfo) {
  2. "use strict";
  3. var VIRTUAL_FACILITY_ID = 1,
  4. FACILITY_ID = 2,
  5. //FACILITY_DEPTH = 2,
  6. FACILITY_DEPTH = 3, // hcLee
  7. BID_INSERT_VIRTUAL_FACILITY = 0,
  8. BID_EDIT_VIRTUAL_FACILITY = 1,
  9. BID_GENERATE_POINT = 2,
  10. BID_DELETE_POINT = 3;
  11. //권한설정
  12. var hasnotModificationPermission = ko.observable(true);
  13. var disabledButtons = [ko.observable(), ko.observable(), ko.observable(), ko.observable()];
  14. // 2016 06 03 hcLee 추가
  15. var facilityTypeDataSource = BemsWebApplication.db.createDataSource('BemsFacilityType');
  16. var BemsMonitoringPointInfo = ko.observableArray();
  17. var BemsMonitoringPointDataSource = BemsWebApplication.db.createDataSource('BemsMonitoringPoint');
  18. var RowName;
  19. var facilityDataSource = BWA.DataUtil.createDataSource({
  20. dataSourceOptions: {
  21. select: [
  22. 'SiteId', 'FacilityCode', 'Name', 'FacilityTypeId', 'BemsFacilityType/Name', 'IsVirtualFacility'
  23. ],
  24. expand: ['BemsFacilityType'],
  25. extendOptions: {
  26. forceOriginalField: true
  27. },
  28. paginate: false, // 페이징을 하지 않음
  29. }
  30. }, 'CmFacility'),
  31. factory = BemsWebApplication.Factory.PointLocationMapping,
  32. viewModel,
  33. popup = null,
  34. virtualFacilityPopup = null,
  35. popupVisible = ko.observable(false),
  36. eq = BWA.DataUtil.constructEqualFilter,
  37. selectedTreeData = ko.observable(null),
  38. selectedTreeItem = ko.observable(null);
  39. var commandButtonOptions = [{
  40. icon: 'add',
  41. id: 'create1',
  42. title: '가상시설등록',
  43. disabled: disabledButtons[BID_INSERT_VIRTUAL_FACILITY],
  44. action: handlePopupInsertVirtualFacility
  45. }, {
  46. icon: 'edit',
  47. id: 'create2',
  48. title: '가상시설수정',
  49. disabled: disabledButtons[BID_EDIT_VIRTUAL_FACILITY],
  50. action: handlePopupUpdateVirtualFacility
  51. }, {
  52. icon: 'add',
  53. id: 'create3',
  54. title: '관제점초기생성',
  55. disabled: disabledButtons[BID_GENERATE_POINT],
  56. action: generatePoints
  57. }, {
  58. icon: 'remove',
  59. id: 'delete',
  60. title: '관제점삭제',
  61. disabled: disabledButtons[BID_DELETE_POINT],
  62. action: handleRemoveSelectedPoints
  63. }];
  64. function handleClickTreeItem(element, data) {
  65. var eq = BWA.DataUtil.constructEqualFilter;
  66. selectedTreeItem(data);
  67. var filter;
  68. var depth = data.depth;
  69. var parentData = data.parentData;
  70. data = data.data;
  71. selectedTreeData(data);
  72. //if (depth !== FACILITY_DEPTH) {
  73. if (depth == 1) {
  74. if (data.Id === FACILITY_ID) {
  75. filter = getFacilityFilter();
  76. }
  77. else {
  78. filter = getVirtualFacilityFilter();
  79. }
  80. }
  81. else if (depth == 2) {
  82. filter = [
  83. eq('SiteId', BWA.UserInfo.SiteId()),
  84. 'and',
  85. //eq('FacilityTypeId', data.FacilityCode()),
  86. eq('FacilityTypeId', data.FacilityTypeId()),
  87. ];
  88. }
  89. else {
  90. filter = [
  91. eq('SiteId', BWA.UserInfo.SiteId()),
  92. 'and',
  93. eq('FacilityCode', data.FacilityCode()),
  94. ];
  95. }
  96. viewModel.setGridViewFilter(filter);
  97. }
  98. function getVirtualFacilityFilter() {
  99. // FacilityTypeId 99 은 가상설비의 타입 아이디 임
  100. return [
  101. ['SiteId', '=', BWA.UserInfo.SiteId()],
  102. 'and',
  103. ['FacilityTypeId', '=', 99],
  104. ];
  105. }
  106. function getFacilityFilter() {
  107. return [
  108. ['SiteId', '=', BWA.UserInfo.SiteId()],
  109. 'and',
  110. ['FacilityTypeId', '<', 99]
  111. ];
  112. }
  113. function handleViewShown() {
  114. hasnotModificationPermission(!BWA.UserInfo.hasPermissionOfModification(viewInfo.viewName));
  115. if (hasnotModificationPermission()) {
  116. for (var i = 0; i < 4; i++) {
  117. commandButtonOptions[i].disabled(true);
  118. }
  119. } else {
  120. for (var i = 0; i < 4; i++) {
  121. commandButtonOptions[i].disabled(false);
  122. }
  123. }
  124. // start warning : 아래 코드는 화면이 열릴때 마다 확인 함. 성능상 이슈 발생 가능성이 있음
  125. if (facilityDataSource._items.length != 0) {
  126. var data = selectedTreeItem();
  127. if (data != null) $('#facilityTreeView').cwTreeView('reload', selectedTreeItem().parentData);
  128. }
  129. // end warning
  130. var eq = BWA.DataUtil.constructEqualFilter;
  131. $('#facilityTreeView').cwTreeView({
  132. width: '30%',
  133. height: 'auto',
  134. //maxDepth: 2,
  135. //height: '480px',
  136. onClickTreeItem: handleClickTreeItem,
  137. delegateDataSource: function (data, alterObj) {
  138. var id = data.id;
  139. var depth = data.depth;
  140. var promise = null;
  141. switch (depth) {
  142. case 0:
  143. var typeDeferred = new $.Deferred();
  144. promise = typeDeferred.promise();
  145. alterObj.IsFunction = false;
  146. typeDeferred.resolve([{
  147. Id: VIRTUAL_FACILITY_ID,
  148. Name: '가상시설'
  149. }, {
  150. Id: FACILITY_ID,
  151. Name: '일반시설'
  152. }
  153. ])
  154. break;
  155. case 1:
  156. var filter;
  157. switch (data.data.Id) {
  158. /*
  159. case VIRTUAL_FACILITY_ID:
  160. filter = getVirtualFacilityFilter();
  161. break;
  162. case FACILITY_ID:
  163. filter = getFacilityFilter();
  164. break;
  165. */
  166. case FACILITY_ID: filter = [['FacilityTypeId', '<', 99]]; break;
  167. case VIRTUAL_FACILITY_ID: filter = [['FacilityTypeId', '=', 99]]; break;
  168. }
  169. facilityTypeDataSource.filter(filter);
  170. promise = facilityTypeDataSource.load();
  171. //facilityDataSource.filter(filter);
  172. //promise = facilityDataSource.load();
  173. break;
  174. case 2:
  175. facilityDataSource.filter([
  176. eq('SiteId', BWA.UserInfo.SiteId()),
  177. 'and',
  178. eq('FacilityTypeId', data.data.FacilityTypeId())
  179. ]);
  180. promise = facilityDataSource.load();
  181. break;
  182. }
  183. return promise;
  184. }
  185. });
  186. popup.handleViewShown();
  187. virtualFacilityPopup.handleViewShown();
  188. }
  189. function handleAddPoints(points) {
  190. if (points.length === 0) return;
  191. if (selectedTreeData() === null) {
  192. utils.toast.show('선택된 빌딩/층/존이 없습니다.', 'warning');
  193. return null;
  194. }
  195. var rows = window.utils.datagrid.getItems(gridView);
  196. points = _.map(points, function (item) {
  197. var facilityCode = item.FacilityCode;
  198. var propertyId = item.PropertyId;
  199. if (_.some(rows, function (x) {
  200. return (x.FacilityCode() === facilityCode && x.PropertyId === propertyId);
  201. })) {
  202. return null;
  203. }
  204. return item;
  205. });
  206. var data = selectedTreeData();
  207. var parameters = {
  208. BuildingId: data.BuildingId()
  209. };
  210. if (_.isUndefined(data.FloorId) === false) {
  211. parameters.FloorId = data.FloorId();
  212. if (_.isUndefined(data.ZoneId) === false) {
  213. parameters.ZoneId = data.ZoneId();
  214. }
  215. else {
  216. parameters.ZoneId = null;
  217. }
  218. }
  219. else {
  220. parameters.FloorId = null;
  221. parameters.ZoneId = null;
  222. }
  223. var postData = [];
  224. var itemKeys = ['SiteId', 'FacilityCode', 'PropertyId'];
  225. var toJS = BWA.DataUtil.convertHybridViewModelToJS;
  226. // $.each(points.concat(rows), function(i, item) {
  227. $.each(points, function (i, item) {
  228. item = toJS(_.pick(item, itemKeys));
  229. item = $.extend(item, parameters);
  230. postData.push(item);
  231. });
  232. parameters.SiteId = data.SiteId();
  233. var promise = BemsWebApplication.api.post(
  234. 'BemsMonitoringPoint/AddLocation', postData, parameters
  235. );
  236. promise.done(function () {
  237. utils.toast.show($G('successDatabaseProcessMsg'));
  238. gridView.refresh();
  239. });
  240. return promise;
  241. }
  242. function handlePopupInsertVirtualFacility() {
  243. virtualFacilityPopup.show();
  244. }
  245. function handlePopupUpdateVirtualFacility() {
  246. var item = selectedTreeItem();
  247. var isExist = ko.observable(false);
  248. if (_.isNull(item)) {
  249. utils.toast.show('가상시설을 선택해 주십시오.', 'warning');
  250. return;
  251. }
  252. if (item.depth !== FACILITY_DEPTH || item.parentData.data.Id === FACILITY_ID) {
  253. utils.toast.show('가상시설만 수정이 가능합니다.', 'warning');
  254. return;
  255. }
  256. if (item != null && item.depth === FACILITY_DEPTH) {
  257. facilityDataSource.filter([
  258. eq('SiteId', BWA.UserInfo.SiteId()),
  259. 'and',
  260. eq('FacilityTypeId', item.data.FacilityTypeId())
  261. ]);
  262. facilityDataSource.load().done(function (result) {
  263. for (var i = 0; i < result.length; i++) {
  264. if (item.data.Name() == result[i].Name()) {
  265. isExist(true);
  266. }
  267. }
  268. if (_.isNull(item) || item.depth !== FACILITY_DEPTH || isExist() === false) {
  269. utils.toast.show('관제점을 등록할 시설을 선택하여 주십시오.', 'warning');
  270. }
  271. else {
  272. virtualFacilityPopup.show(BWA.DataUtil.convertViewModelToJS(selectedTreeData()), item.data.Name());
  273. }
  274. });
  275. }
  276. else {
  277. utils.toast.show('관제점을 등록할 시설을 선택하여 주십시오.', 'warning');
  278. }
  279. }
  280. function generatePoints() {
  281. var item = selectedTreeItem();
  282. if (_.isNull(item) ||
  283. (item.depth === 1 && item.data.Id !== FACILITY_ID) ||
  284. //(item.depth === 2 && item.parentData.data.Id !== FACILITY_ID) ||
  285. (item.depth === 2) ||
  286. (item.depth === 3 && item.parentData.parentData.data.Id !== FACILITY_ID)
  287. ) {
  288. utils.toast.show('모든 시설의 관제점을 재생성하려면 "일반시설"을 선택하시고, 아니면 특정 시설을 선택해 주십시오.', 'warning');
  289. return;
  290. }
  291. DevExpress.ui.dialog.confirm(
  292. '전체 혹은 선택된 시설의 관제점이 생성됩니다. 기존에 설정된 관제점 정보가 삭제됩니다. \n계속 진행하시겠습니까?',
  293. '관제점 생성'
  294. ).then(function (result) {
  295. if (result) {
  296. var data = item.data;
  297. var postData = {};
  298. switch (item.depth) {
  299. case 1: // 전체
  300. postData.SiteId = BWA.UserInfo.SiteId();
  301. postData.FacilityTypeId = null;
  302. postData.FacilityCode = null;
  303. break;
  304. case 2: // 2016 06 03 hcLee 추가
  305. postData.SiteId = BWA.UserInfo.SiteId();
  306. postData.FacilityTypeId = data.FacilityTypeId();
  307. postData.FacilityCode = null;
  308. break;
  309. case 3: // 특정 시설 // 2016 06 03 hcLee 수정 기존 2 -> 3으로 변경
  310. postData.SiteId = BWA.UserInfo.SiteId();
  311. postData.FacilityTypeId = data.FacilityTypeId();
  312. postData.FacilityCode = data.FacilityCode();
  313. break;
  314. }
  315. disabledButtons[BID_GENERATE_POINT](true);
  316. BWA.api.post('BemsMonitoringPoint/Generation', postData)
  317. .always(function () {
  318. disabledButtons[BID_GENERATE_POINT](false);
  319. }).done(function () {
  320. viewModel.refreshList();
  321. utils.toast.show('관제점 생성을 성공하였습니다.');
  322. });
  323. }
  324. });
  325. }
  326. function handleRemoveSelectedPoints() {
  327. var gridView = viewModel.gridView();
  328. var rows = gridView.getSelectedRowsData();
  329. if (_.isEmpty(rows)) {
  330. utils.toast.show('선택된 관제점이 없습니다.', 'warning');
  331. return;
  332. }
  333. // 삭제확인 메세지 박스 추가 hcLee 2016 06 03
  334. DevExpress.ui.dialog.confirm($G('deleteConfirmMsg'), "삭제").then(function (result) {
  335. if (result) {
  336. //handleConfirmDelete();
  337. var postData = [];
  338. var itemKeys = BWA.db.BemsMonitoringPoint.key();
  339. var toJS = BWA.DataUtil.convertHybridViewModelToJS;
  340. $.each(rows, function (i, item) {
  341. item = toJS(_.pick(item, itemKeys));
  342. postData.push(item);
  343. });
  344. var promise = BemsWebApplication.api.del(
  345. 'BemsMonitoringPoint', postData
  346. );
  347. promise.done(function () {
  348. utils.toast.show($G('successDatabaseProcessMsg'));
  349. gridView.clearSelection();
  350. gridView.refresh();
  351. });
  352. return promise;
  353. }
  354. });
  355. }
  356. viewModel = BWA.DataGrid.createViewWithDataGrid(params, viewInfo, 'BemsMonitoringPoint', {
  357. dataSourceOptions: factory.getDataSourceOptions(),
  358. selection: {
  359. mode: 'multiple'
  360. },
  361. columns: factory.getColumns([
  362. { dataField: 'CmFacility/Name', width: '40%' },
  363. { dataField: 'Name', width: '40%' },
  364. //{ dataField: 'ValueType', width: '20%' },
  365. { dataField: 'BemsFuelType/Name', caption: '에너지원', width: '30%', alignment: 'center' },
  366. { dataField: 'BemsServiceType/Name', caption: '용도', width: '30%', alignment: 'center' },
  367. ]),
  368. searchViewItems: [
  369. { id: 'CmFacility/Name' },
  370. { id: 'Name' }
  371. ],
  372. popupWidth: 520,
  373. insertButtonText: $G('pointInsert'),
  374. useNumberColumn: false,
  375. hasnotModificationPermission: hasnotModificationPermission, //권한설정
  376. handleViewShown: handleViewShown,
  377. handleInitializeDataModelValue: function (dataModel) {
  378. popup.handleInitializeDataModelValue(dataModel);
  379. },
  380. handleDataGridRowClick: function (id, dataGrid, clickRow, popupVisible) {
  381. popup.handleDataGridRowClick.apply(this, arguments);
  382. var data = clickRow.data;
  383. RowName = data.Name;
  384. },
  385. // 이름 중복 방지
  386. handlePopupShowing: function () {
  387. var item = selectedTreeItem();
  388. BemsMonitoringPointDataSource.filter([
  389. eq('SiteId', BWA.UserInfo.SiteId()),
  390. 'and',
  391. eq('FacilityTypeId', item.data.FacilityTypeId())
  392. ]);
  393. BemsMonitoringPointDataSource.load().done(function (result) {
  394. BemsMonitoringPointInfo(result);
  395. });
  396. },
  397. beforeInsertingDataViewModel: function (dataModel, dbModelId) {
  398. var dfd = $.Deferred();
  399. var param = {
  400. SiteId: dataModel.SiteId(),
  401. FacilityCode: dataModel.FacilityCode()
  402. };
  403. var isDuplicated = false;
  404. for (var i = 0; i < BemsMonitoringPointInfo().length; i++) {
  405. if (dataModel.Name() == BemsMonitoringPointInfo()[i].Name()) {
  406. isDuplicated = true;
  407. break;
  408. }
  409. }
  410. if (!BWA.DataUtil.isValidInputValue(dataModel.Name()))
  411. return dfd.resolve(false, '저장 혹은 수정 반드시 필수 입력 정보를 모두 입력하셔야 합니다 (앞뒤공백 허용안함)');
  412. else {
  413. if (isDuplicated)
  414. return dfd.resolve(false, '이미 동일 이름이 존재합니다.');
  415. else {
  416. $.when(BWA.api.post('BemsMonitoringPoint/GetMaxPropertyId', param))
  417. .done(function (newPid) {
  418. if (newPid > 0) {
  419. dataModel.PropertyId(newPid);
  420. dfd.resolve(true); // hcLee OK good, (true, false 또는 객체로 리턴가능함) 2015 11 19
  421. }
  422. else dfd.resolve(false, 'ProprttyId 자동 생성에 실패하였습니다!'); // hcLee OK good, (true, false 또는 객체로 리턴가능함) 2015 11 19
  423. });
  424. return dfd.promise();
  425. }
  426. }
  427. },
  428. beforeUpdateDataViewModel: function () {
  429. var isDuplicated = false;
  430. for (var i = 0; i < BemsMonitoringPointInfo().length; i++) {
  431. if (viewModel.dataModel.Name() == BemsMonitoringPointInfo()[i].Name() && RowName() != BemsMonitoringPointInfo()[i].Name()) {
  432. isDuplicated = true;
  433. break;
  434. }
  435. }
  436. if (!BWA.DataUtil.isValidInputValue(viewModel.dataModel.Name())) {
  437. utils.toast.show('저장 혹은 수정 반드시 필수 입력 정보를 모두 입력하셔야 합니다 (앞뒤공백 허용안함)', 'error');
  438. return 0;
  439. }
  440. else {
  441. if (isDuplicated) {
  442. utils.toast.show('이미 동일 이름이 존재합니다.', 'error');
  443. return 0;
  444. }
  445. else
  446. return 2;
  447. }
  448. }
  449. //if (!isNewInPopup()) return true;
  450. });
  451. function InsertButtonClickAction() {
  452. var isExist = ko.observable(false);
  453. var item = selectedTreeItem();
  454. if (item != null && item.depth === FACILITY_DEPTH) {
  455. facilityDataSource.filter([
  456. eq('SiteId', BWA.UserInfo.SiteId()),
  457. 'and',
  458. eq('FacilityTypeId', item.data.FacilityTypeId())
  459. ]);
  460. facilityDataSource.load().done(function (result) {
  461. for (var i = 0; i < result.length; i++) {
  462. if (item.data.Name() == result[i].Name()) {
  463. isExist(true);
  464. }
  465. }
  466. if (_.isNull(item) || item.depth !== FACILITY_DEPTH || isExist() === false) {
  467. utils.toast.show('관제점을 등록할 시설을 선택하여 주십시오.', 'warning');
  468. }
  469. else {
  470. viewModel.popupInsertView();
  471. }
  472. });
  473. }
  474. else {
  475. utils.toast.show('관제점을 등록할 시설을 선택하여 주십시오.', 'warning');
  476. }
  477. }
  478. viewModel.commandButtonOptions = commandButtonOptions;
  479. viewModel.handleRemoveSelectedPoints = handleRemoveSelectedPoints;
  480. popup = viewModel.popup = BWA.Popup.Point.create(viewModel, selectedTreeItem);
  481. virtualFacilityPopup = viewModel.virtualFacilityPopup = BWA.Popup.VirtualFacility.create(viewModel, selectedTreeItem);
  482. viewModel.popupInsertButtonOptions = {
  483. icon: 'add',
  484. id: 'create',
  485. title: '관제점등록',
  486. action: InsertButtonClickAction
  487. };
  488. return viewModel;
  489. };