224 lines
9.3 KiB
JavaScript
224 lines
9.3 KiB
JavaScript
/*
|
|
** Views/Partial/LCA/BOM.cshtml is using this controller
|
|
*/
|
|
|
|
angular.module('CarbonFootprint')
|
|
.controller('PowerConsumptionController', ['$scope', '$http', '$routeParams', 'Notification', 'JoinSurveyItemAndParameter', 'MultiLanguageService', '$q','$filter',
|
|
function ($scope, $http, $routeParams, Notification, JoinService, MultiLanguageService, $q ,$filter) {
|
|
// Multi-language resource object
|
|
var resource,
|
|
inited = false,
|
|
errMsg = '工作時間加總不能超過8760';
|
|
|
|
// This property controlls modal form, will be passed to table-edit-modal directive
|
|
$scope.modalFormOption = {};
|
|
$scope.model = [];
|
|
|
|
// set $scope.baseUrl
|
|
var baseUrl = $scope.baseUrl;
|
|
if (typeof (baseUrl) === 'undefined' || baseUrl == null) {
|
|
var arr = window.location.href.split("/");
|
|
if (arr[3].indexOf('app') == 0)
|
|
baseUrl = '';
|
|
else
|
|
baseUrl = '/' + arr[3];
|
|
$scope.baseUrl = baseUrl;
|
|
}
|
|
|
|
$scope.changeSpec = function (toBeEdit) {
|
|
var workingHour = toBeEdit.WorkingHour || 0;
|
|
var minSpec = toBeEdit.MinSpec || 0;
|
|
|
|
toBeEdit.PowerUsage = toBeEdit.Spec / 1000;
|
|
toBeEdit.PowerUsagePerHour = toBeEdit.PowerUsage * workingHour;
|
|
|
|
if (minSpec > 0) {
|
|
toBeEdit.PowerLoss = (1 / toBeEdit.MinSpec - 1) / 1000;
|
|
toBeEdit.PowerLossPerYear = toBeEdit.PowerLoss * workingHour;
|
|
}
|
|
};
|
|
|
|
$scope.changeMinSpec = function (toBeEdit) {
|
|
var workingHour = toBeEdit.WorkingHour || 0;
|
|
var minSpec = toBeEdit.MinSpec || 0;
|
|
|
|
toBeEdit.PowerUsagePerHour = toBeEdit.PowerUsage * workingHour;
|
|
|
|
if (minSpec > 0) {
|
|
toBeEdit.PowerLoss = (1 / toBeEdit.MinSpec - 1) / 1000;
|
|
toBeEdit.PowerLossPerYear = toBeEdit.PowerLoss * workingHour;
|
|
}
|
|
};
|
|
|
|
$scope.changeWorkingHour = function (toBeEdit) {
|
|
toBeEdit.PowerUsagePerHour = toBeEdit.PowerUsage * toBeEdit.WorkingHour;
|
|
toBeEdit.PowerLossPerYear = toBeEdit.PowerLoss * toBeEdit.WorkingHour;
|
|
};
|
|
|
|
$scope.createProcess = function () {
|
|
$scope.powerConsumptionForm.$setPristine();
|
|
|
|
// Create an object & select it
|
|
var toBeCreated = {};
|
|
toBeCreated.ID = 0;
|
|
toBeCreated.LCAID = $routeParams.LCAID;
|
|
|
|
// just for testing
|
|
//toBeCreated.ModelType = 'WA-36N12RUGK-AAAC';
|
|
//toBeCreated.AppSystemType = '網通交換機';
|
|
//toBeCreated.Scenario = '系统工作功耗(W)';
|
|
//toBeCreated.Spec = 0.3;
|
|
//toBeCreated.MinSpec = 0.783;
|
|
//toBeCreated.PowerUsage = toBeCreated.Spec / 1000;
|
|
//toBeCreated.PowerLoss = (toBeCreated.Spec / toBeCreated.MinSpec - toBeCreated.Spec) / 1000;
|
|
//toBeCreated.WorkingHour = 7160;
|
|
//toBeCreated.PowerUsagePerHour = toBeCreated.PowerUsage * toBeCreated.WorkingHour;
|
|
//toBeCreated.PowerLossPerYear = toBeCreated.PowerLoss * toBeCreated.WorkingHour;
|
|
|
|
// For modal form
|
|
$scope.selectRow.toBeEdit = toBeCreated;
|
|
$scope.modalFormOption.show = true;
|
|
|
|
$scope.save = function () {
|
|
var isConfirm = true;
|
|
|
|
if (checkWorkingHour(toBeCreated, $scope.model.concat([toBeCreated]))) {
|
|
isConfirm = confirm(errMsg);
|
|
}
|
|
|
|
if (isConfirm) {
|
|
$http.post($scope.baseUrl+'/api/PowerConsumption/Save/' + $routeParams.LCAID + "/" + toBeCreated.ID, toBeCreated)
|
|
.success(function (data) {
|
|
$scope.model.unshift(data);
|
|
$scope.modalFormOption.show = false;
|
|
})
|
|
.error(function (error) {
|
|
console.log(error);
|
|
Notification.error({ message: error.ExceptionMessage, positionX: 'center', delay: 3000 });
|
|
});
|
|
}
|
|
};
|
|
};
|
|
|
|
$scope.editProcess = function (selected) {
|
|
var toBeEdit = $scope.selectRow.toBeEdit = angular.copy(selected);
|
|
|
|
$scope.modalFormOption.show = true;
|
|
|
|
$scope.save = function () {
|
|
var isConfirm = true;
|
|
|
|
if (checkWorkingHour(toBeEdit, $scope.model)) {
|
|
isConfirm = confirm(errMsg);
|
|
}
|
|
|
|
if (isConfirm) {
|
|
$http.post($scope.baseUrl+'/api/PowerConsumption/Save/' + toBeEdit.LCAID + "/" + toBeEdit.ID, toBeEdit)
|
|
.success(function (data) {
|
|
angular.copy(data, selected);
|
|
$scope.modalFormOption.show = false;
|
|
})
|
|
.error(function (error) {
|
|
console.log(error);
|
|
Notification.error({ message: error.ExceptionMessage, positionX: 'center', delay: 3000 });
|
|
});
|
|
}
|
|
};
|
|
};
|
|
|
|
$scope.deleteProcess = function (selected) {
|
|
var isConfirm = confirm(resource.DeleteItemMsg);
|
|
|
|
if (checkWorkingHour(selected, Enumerable.From($scope.model).Where(function (x) { return x.ID != selected.ID; }).ToArray())) {
|
|
isConfirm = confirm(errMsg);
|
|
}
|
|
|
|
if (isConfirm) {
|
|
$http.delete($scope.baseUrl+"/api/PowerConsumption/Delete/" + selected.ID)
|
|
.success(function () {
|
|
var index = $scope.model.indexOf(selected);
|
|
$scope.model.splice(index, 1);
|
|
|
|
// This operate is base on selectRow directive in TableEdit.js file
|
|
$scope.selectRow.unSelect();
|
|
});
|
|
}
|
|
};
|
|
|
|
$scope.$on('changeSelectedTab', function (event, args) {
|
|
if (!inited && args.selectedTab == 'PowerConsumption') {
|
|
inited = true;
|
|
|
|
// Get data and prepare $scope.model
|
|
getAllDataAsync($routeParams.LCAID)
|
|
.then(function (response) {
|
|
$scope.model = response.Consumptions;
|
|
}, function (error) {
|
|
console.log(error);
|
|
});
|
|
|
|
// Get multilanguage resource
|
|
MultiLanguageService.getResourceAsync()
|
|
.then(function (response) {
|
|
resource = response;
|
|
}, function (error) {
|
|
console.log(error);
|
|
});
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Get all list data from server
|
|
* @param {[Int]} : LCAID
|
|
* @return {[promise]}
|
|
*/
|
|
function getAllDataAsync(LCAID) {
|
|
var deferred = $q.defer();
|
|
$http.get($scope.baseUrl+'/api/PowerConsumption/GetByLcaId/' + LCAID)
|
|
.success(function (data) {
|
|
deferred.resolve(data);
|
|
})
|
|
.error(function () {
|
|
deferred.resolve(null);
|
|
})
|
|
return deferred.promise;
|
|
}
|
|
|
|
function checkWorkingHour(editModel, models) {
|
|
var standbyPowers = Enumerable.From(models)
|
|
.Where(function (x) {
|
|
return x.ModelType == editModel.ModelType && isStandbyPower(x);
|
|
})
|
|
.ToArray();
|
|
|
|
var sumWorkingHour = Enumerable.From(models)
|
|
.Where(function (x) {
|
|
return x.ModelType == editModel.ModelType && isNotStandbyPower(x);
|
|
})
|
|
.Sum(function (x) {
|
|
return x.ID == editModel.ID ? editModel.WorkingHour : x.WorkingHour;
|
|
});
|
|
|
|
if (standbyPowers.length == 1) {
|
|
|
|
if (isStandbyPower(editModel)) {
|
|
editModel.WorkingHour = 8760 - sumWorkingHour;
|
|
} else {
|
|
standbyPowers[0].WorkingHour = 8760 - sumWorkingHour;
|
|
}
|
|
|
|
return standbyPowers[0].WorkingHour < 0; // 自动计算为负值
|
|
} else {
|
|
return sumWorkingHour > 8760;
|
|
}
|
|
}
|
|
|
|
function isStandbyPower(model) {
|
|
return model.Scenario.indexOf('待機') >= 0 || model.Scenario.indexOf('關機') >= 0;
|
|
}
|
|
|
|
function isNotStandbyPower(model) {
|
|
return !isStandbyPower(model);
|
|
}
|
|
}]);
|