.net 评分计算--使用配置公式进行计算
最近公司要计算活动公式,于是找了几种方式计算
第一种:使用原生.net控件 NCalc
Expression exp = new Expression(richTextBox1.Text.Trim()); exp.Parameters["需求计划安全库存满足率"] = 0.1; exp.Parameters["需求计划缺货次数"] = 10; textBox1.Text = exp.Evaluate().ToString();
if([需求计划安全库存满足率] > 0.9, 30 , if([需求计划缺货次数] < 15 && [需求计划安全库存满足率] > 0.01,15,0))+if([需求计划安全库存满足率] < 0.9, 30 , if([需求计划缺货次数] < 15 && [需求计划安全库存满足率] > 0.01,15,0))+if([需求计划安全库存满足率] >0.5, 30 , if([需求计划缺货次数] != 1 && [需求计划安全库存满足率] > 0.01,15,0))
第二种: 调用com js控件
参考 http://blog.csdn.net/return_false/article/details/7750862
注意:需要把生成平台设置为x86,否则x64无法运行...
string expression = string.Empty; SYS_PARAMETER_TABDao parameterDao = new SYS_PARAMETER_TABDao(); SYS_PARAMETER_TAB parameter = parameterDao.Retrieve("Expression"); if (!string.IsNullOrEmpty(parameter.VC_VALUE)) { expression = parameter.VC_VALUE; } expression = expression.Replace("[安全库存满足率]", (item.N_ANQUAN_QTY ?? 0).ToString()); expression = expression.Replace("[缺货次数]", (item.N_XUQIU_QTY ?? 0).ToString()); expression = expression.Replace("[4A9缺货次数]", (item.N_4A9_QTY ?? 0).ToString()); expression = expression.Replace("[4G6缺货次数]", (item.N_4G6_QTY ?? 0).ToString()); expression = expression.Replace("[调整次数]", (item.N_TIAOZHENG_QTY ?? 0).ToString()); expression = expression.Replace("[普通订单完成率]", (item.N_PUTONG_QTY ?? 0).ToString()); expression = expression.Replace("[紧急订单完成率]", (item.N_JIAJI_QTY ?? 0).ToString()); expression = expression.Replace("[订单确认情况]", (item.N_QUEREN_QTY ?? 0).ToString()); expression = expression.Replace("[公共查看情况]", (item.N_CHAKAN_QTY ?? 0).ToString()); expression = expression.Replace("[发票开据情况]", (item.N_FAPIAO_QTY ?? 0).ToString()); expression = expression.Replace("[单项否决情况]", (item.N_FOUJUE_QTY ?? 0).ToString()); ScriptControl sc = new ScriptControl(); sc.Language = "javascript"; item.N_SCORE = Math.Round(Convert.ToDecimal(sc.Eval(expression).ToString()), 2); Convert.ToInt32(sc.Eval(expression).ToString());
var result = 0;/*var [安全库存满足率] = 0;var [缺货次数] = 0;var [4A9缺货次数] = 0;var [4G6缺货次数] = 0;var [调整次数] = 0;var [普通订单完成率] = 0;var [紧急订单完成率] = 0;var [订单确认情况] = 0;var [公共查看情况] = 0;var [发票开据情况] = 0;var [单项否决情况] = 0;*/if([安全库存满足率] >= 1){result += 30;}else{if([安全库存满足率] > 0.8 && [缺货次数] <= 6){result += 20;}else{if([安全库存满足率] > 0.5 && [缺货次数] <= 10){result += 10;}else{result += 0;}}}if([调整次数] == 0){result += 15;}else{if([调整次数] <= 2){result += 10;}else{result += 0;}}if([紧急订单完成率] >= 1){result += 15;}else{if([紧急订单完成率] >= 0.8){result += 10;}else{if([紧急订单完成率] >= 0.5){result += 5;}else{result += 0;}}}result;
比较:第一种相对简单,但是功能也简单,写完的代码不容易阅读
第二种功能齐全,容易阅读,但是不支持anycpu,且不是原始C#代码