angular.module('system_cfg', [])
.controller('systemCfgCtrl', ['$scope', '$rootScope', 'http_service', '$compile', function($scope, $rootScope, http_service, $compile) {
_.extend($scope, {
"toggle": function(bool) {
var eval_str = '$scope';
_.each(bool.split('.'), function(key) {
eval_str += '["' + key + '"]';
});
var bool_value = eval(eval_str);
eval_str += '=' + (bool_value === '0' ? '"1"' : '"0"') + ';'
eval(eval_str);
},
"cfg_data": {
"budget": {
"budget_open": "0",
"budget_type": [false, false],
"budget_method": '0',
"budget_scale": "",
"budget_amount": ""
},
"apply": {
"apply_open": "0"
// "apply_type": [false, false],
// "apply_point": "1"
},
"expend": {
"expend_open": "0"
// "expend_over": "1",
// "expend_below": "1",
// "expend_free": "1",
// "expend_cross": "1",
// "expend_supply": "1"
}
},
"budget_ctrl_value": {
"type": "ratio"
},
"parseInt": function(v) {
return parseInt(v);
},
"check_ratio_value": function() {
if(/(^\s*\d+\.?\d*)%\s*$/.test($scope.cfg_data.budget.budget_scale)) {
$scope.ratio_val_match = true;
} else {
$scope.ratio_val_match = false;
}
},
"check_finger_value": function() {
if(/^(0|[1-9][0-9]*)$/.test($scope.cfg_data.budget.budget_amount)) {
$scope.finger_val_match = true;
} else {
$scope.finger_val_match = false;
}
},
"save_system_cfg": function() {
var cfg_data = $scope.cfg_data;
var data = [];
_.each(cfg_data, function(k, v) {
_.each(k, function(_k, _v) {
if(_v==='apply_type'){
_k = _.map(_k, function(n){
return n?'1':'0';
});
}
// if(_v==='budget_type'){
//
// _k = _.map(_k, function(n){
//
// return n?'1':'0';
// });
// }
data.push({
"scGroup": v,
"scValue": _k,
"scName": _v
});
});
});
http_service.post('syscfg/save', {
"data": data
}, function(res) {
if(res.data.errMsg) {
//alert(res.data.errMsg);
// swal({
// title: "错误",
// text: res.data.errMsg,
// type: "error",
// confirmButtonText: "确定",
// })
$scope.$parent.terro(res.data.errMsg)
} else {
//alert('系统设置保存成功。');
if(res.data.opState) {
$scope.$parent.tsuccess("系统保存设置成功!")
_.each(data, function(obj) {
switch(obj.scName) {
case "budget_open":
$rootScope.budget_open = obj.scValue;
break;
case "apply_open":
$rootScope.apply_open = obj.scValue;
break;
case "expend_open":
$rootScope.expend_open = obj.scValue;
break;
}
});
} else {
$scope.$parent.terro("系统保存失败!")
}
}
});
}
});
$scope.check_ratio_value();
$scope.check_finger_value();
$scope.show_flag = 0;
$scope.chang_show_flag = function(flag) {
$scope.show_flag = flag;
}
http_service.post('syscfg/list', {}, function(res) {
var data = res.data.data;
_.each(data, function(obj, v) {
var val_txt = obj['scValue'];
if(_.isNumber(val_txt)) {
val_txt = _.toString(val_txt);
}
$scope.cfg_data[obj['scGroup']][obj['scName']] = val_txt;
$scope.check_ratio_value();
$scope.check_finger_value();
});
});
// $http({
// url:"./static/js/formula.json",//假数据
// method:"GET",
// data:{}
// }).then(function(res){
// var formulaList = res.data.data;
// $scope.formulaList = formulaList;
// },function(err){
// //弹窗
// });
http_service.post('template/cpn/masDataEleList', {}, function(res) {
$scope.form_data_choose = res.data.data.rows;
});
http_service.post('systemMath/search/functionInfo', {}, function(res) {
$scope.fx_data = res.data.data;
// $scope.fx_data=[{
// "fx_name":"日期函数",
// "fx_value":"Date()",
// "fx_str":"Date",
// "fx_formula_str":"(1/(24*60*60*1000))*"
// }]
});
function refres_sysMath(){
http_service.post('systemMath/search/mathList', {}, function(res) {
var referenceFormula = res.data.data;
$scope.referenceFormula = referenceFormula;
});
http_service.post('systemMath/search/list', {}, function(res) {
var formulaList = res.data.data;
$scope.formulaList = formulaList;
});
}
refres_sysMath();
//系统变量编辑
$scope.add_formula = function(formula) {
if (!formula) {
formula={};
}else if(formula.child != undefined && formula.child.length != 0){
var back_data = [];
_.each(formula.child,function(cd){
back_data.push(cd);
});
back_data = _.uniqBy(back_data,'mdCode');
}
formula.resultFromat = formula.resultFromat == undefined ? 10 : formula.resultFromat;
// formula.type = formula['type'] == undefined ? 10 : formula.type;
$scope.formula = formula;
$scope.formula_content = "";
$('.el_form').find('div[name="form_el_row"]').each(function(index, el) {
var el_data = $(el).data('data');
})
var edl_arr = [];
if(formula.view) {
$scope.formula_content = formula.view;
}
// $scope.value_type = "";
//// if($scope.data.joinMath){
// if( $scope.data.joinMath && $scope.data.joinMath.length > 0){
// if($scope.data.joinMath[0].cpnMath.mView){
// $scope.formula_content = $scope.data.joinMath[0].cpnMath.mView;//显示公式回调
// }
// _.each($scope.mResultFromat,function(_result){
// if(_result.value == $scope.data.joinMath[0].cpnMath.mResultFromat){
// $scope.value_type = _result.code;
// }
// })
// _.each($scope.data.joinMath[0].cpnMath.child,function(cpn){
// if(cpn.pIndex != 0){
// edl_arr.push(cpn);
// }
// })
// }
// }
_.each($scope.form_data_choose, function(data) {
// data['edl'] = "";
// _.each(edl_arr,function(edl){
// if(data.gcMeCode == edl.pMdCode && edl.meName != undefined){
// data['edl'] = {"meCode":edl.pMeCode,"meName":edl.meName};//元素选择回调
// }
// })
});
var $pop = $('
');
var $popdiv = $compile($pop)($scope);
$('body').append($popdiv);
$pop.find('div.close, button.cancel').on('click', function() {
$(this).closest('div.bg-modal').remove();
});
//默认为公式函数
$scope.formula_type = 1;
//引用公式 选项卡
$scope.togle_formula = function(){
if($scope.formula_type == 1){
$scope.formula_type = 2;
}else{
$scope.formula_type = 1;
}
}
function insertAfterText(textDom, value) { //在光标后插入文本
var selectRange;
if(document.selection) {
// IE Support
textDom.focus();
selectRange = document.selection.createRange();
selectRange.text = value;
$scope.formula_content = selectRange.text;
textDom.focus();
} else if(textDom.selectionStart || textDom.selectionStart == '0') {
// Firefox support
var startPos = textDom.selectionStart;
var endPos = textDom.selectionEnd;
var scrollTop = textDom.scrollTop;
$scope.formula_content = $scope.formula_content.substring(0, startPos) + value + $scope.formula_content.substring(endPos, $scope.formula_content.length);
textDom.focus();
textDom.selectionStart = startPos + value.length;
textDom.selectionEnd = startPos + value.length;
textDom.scrollTop = scrollTop;
} else {
$scope.formula_content += value;
textDom.focus();
}
}
$scope.add_item = function(item) { //添加控件
var formula_str = "[" + item.masDataDefineName + "]";
insertAfterText(document.getElementById("formula_content"), formula_str);
}
$scope.add_mecode = function(item, event) {
http_service.post("base/data/element/info", {
"code": item.masDataDefineCode
}, function(res) {
_.each($scope.form_data_choose, function(_form) {
if(_form == item) {
_form['edl_data'] = res.data.data;
_form['show_edl'] = true;
} else {
_form['show_edl'] = false;
}
})
});
event.stopPropagation();
}
$scope.close_edl = function(event) {
_.each($scope.form_data_choose, function(_form) {
_form['show_edl'] = false;
});
event.stopPropagation();
event.preventDefault();
}
$scope.add_edl = function(item, data) {
insertAfterText(document.getElementById("formula_content"), '[' + item.masDataDefineName + '.' + data.meName + ']');
_.each($scope.form_data_choose, function(_form) {
if(_form == item) {
_form['edl'] = data;
}
});
}
$scope.add_fx = function(fx) {
insertAfterText(document.getElementById("formula_content"), fx.showTemplate);
}
$scope.add_rf = function(rf) {
var rf_str = "[公式:" + rf.name + "]";
insertAfterText(document.getElementById("formula_content"), rf_str);
}
function formula_save(){ // 保存公式 数据
var formula_str = $scope.formula_content;
var str = $scope.formula_content,
reg = /\[(.*?)\]/g,
code_arr = [],
x = null;
while((x = reg.exec(str)) != null) {
code_arr.push(x[1]);
}
var formula_code = [];
_.each(code_arr, function(code, i) {
var pType = 10,
item_code = code;
if(item_code) {
if(item_code.indexOf("公式:") >= 0) {
pType = 20;
item_code = item_code.replace("公式:", "");
_.each($scope.referenceFormula, function(item) {
if(item_code == item.name) {
formula_code.push({
"joinMathId": item.id,
"pType": pType,
"pIndex": (i + 1)
});
formula_str = formula_str.replace("[" + code + "]", "{" + (i + 1) + "}");
}
})
}
if(item_code.indexOf(".") >= 0) {
var arr = item_code.split('.');
_.each($scope.form_data_choose, function(item) {
if(arr[0] == item.masDataDefineName) {
_.each(item.edl_data, function(eldata) {
var arr_meName = _.split(arr[1],"(");
if(eldata.meName == arr_meName[0]) {
formula_code.push({
"mdCode": item.masDataDefineCode,
'mdName':item.masDataDefineName,
"pType": eldata.meType,
"pIndex": (i + 1),
"meCode": eldata.meCode,
"meName": eldata.meName,
"defVal":_.trim(arr_meName[1],")")
});
}
})
formula_str = formula_str.replace("[" + code + "]", "{" + (i + 1) + "}");
}
})
} else {
_.each($scope.form_data_choose, function(item) {
var item_def = _.split(item_code,"(");
if(item_def[0] == item.masDataDefineName) {
formula_code.push({
"mdCode": item.masDataDefineCode,
'mdName':item.masDataDefineName,
"pType": item.eleNameDataType,
"pIndex": (i + 1),
"meCode": item.eleCodeCode,
"defVal":_.trim(item_def[1],")")
});
formula_str = formula_str.replace("[" + code + "]", "{" + (i + 1) + "}");
}
})
}
}
});
_.each($scope.fx_data, function(fx) {
if(formula_str.indexOf(fx.fx_str) != -1) {
formula_str = formula_str.replace(fx.fx_str, fx.fx_formula_str);
}
})
// var mResultFromat;
// _.each($scope.mResultFromat,function(_result){
// if(_result.code == $scope.value_type){
// mResultFromat = _result.value;
// }
// });
// var post_data = {
// "child":formula_code,
// "mCount":formula_str,
// "mType":10,
// "mView":$scope.formula_content,
// "mResultFromat":mResultFromat
// }
// http_service.post("form/math/insert",post_data,function(res){
if($scope.formula_content == "" || $scope.formula_content == undefined) {
formula = [];
} else {
// formula = [{
// "mCount" : formula_str,
//// "mName" : "公式名称",
// "mResultFromat" :mResultFromat,
// "mType" : 10,
// "mView" : $scope.formula_content,
// "child" : formula_code
// }] ;
formula.count = formula_str;
formula.resultFromat = $scope.formula.resultFromat;
formula.view = $scope.formula_content;
formula.child = formula_code;
// formula.type = $scope.formula.type;
if (formula.id) {
http_service.post('systemMath/update', formula, function(res) {
if(res.data.opState) {
// $scope.tsuccess("系统变量["+formula.name+"]更新成功!")
refres_sysMath();
}
});
} else {
http_service.post('systemMath/insert', formula, function(res) {
if(res.data.opState) {
if(formula.name == undefined){
$scope.terro("请添加系统变量")
}else{
$scope.tsuccess("系统变量["+formula.name+"]新增成功!")
$scope.formulaList.push(formula);
refres_sysMath();
}
} else {
$scope.terro("系统变量["+formula.name+"]新增失败!")
}
});
}
}
$pop.find('div.close, button.cancel').closest('div.bg-modal').remove();
// });
}
$scope.formula_commit = function() { //保存公式
if(back_data != undefined && back_data.length > 0){
$scope.back_num = 0;
_.each(back_data,function(bd){
http_service.post("base/data/element/info", {
"code": bd.mdCode
}, function(res) {
_.each($scope.form_data_choose, function(_form) {
if(_form.masDataDefineCode == bd.mdCode) {
_form['edl_data'] = res.data.data;
}
});
$scope.back_num++;
});
})
var back_data_check = $scope.$watch(function(){
return $scope.back_num;
},function(){
if($scope.back_num != 0 && $scope.back_num == back_data.length){
formula_save();
back_data_check();// 清除$watch
}
})
}else{
formula_save();
}
}
}
var deletefo =true;
$scope.del_formula = function(formula) {
$rootScope.swalWhether({ "title": "确定删除?", 'text': "若补助标准或表单中已使用此系统变量,可能会导致异常" },
function() {
if(deletefo){
deletefo =false;
http_service.post('systemMath/delete', formula, function(res) {
if(res.data.opState) {
deletefo =true;
$rootScope.swalsuccess({ "text": "系统变量["+formula.name+"]删除成功!" },
refres_sysMath()
)
} else {
deletefo =true;
$rootScope.swalerror({ "text": "系统变量["+formula.name+"]删除失败!"})
}
});
}
});
}
$scope.refdata = function(){
http_service.post('flow/syunOrganization', {}, function(res) {
if(res.data.opState){
$rootScope.tsuccess("同步成功");
}else{
$rootScope.terro("同步组织机构失败");
}
})
}
}]);