16 changed files with 400 additions and 21 deletions
@ -0,0 +1,262 @@ |
|||
package matsformula |
|||
|
|||
import ( |
|||
"appPlatform/overall/publicmethod" |
|||
"fmt" |
|||
"math" |
|||
"strconv" |
|||
"strings" |
|||
|
|||
"github.com/dengsgo/math-engine/engine" |
|||
"github.com/gin-gonic/gin" |
|||
) |
|||
|
|||
/* |
|||
* |
|||
@ 作者: 秦东 |
|||
@ 时间: 2024-01-02 08:53:21 |
|||
@ 功能: 解析数学公式 |
|||
@ 参数 |
|||
|
|||
# |
|||
|
|||
@ 返回值 |
|||
|
|||
# |
|||
|
|||
@ 方法原型 |
|||
|
|||
# |
|||
*/ |
|||
func (a *ApiMethod) AnalyticalMathematicalFormulas(c *gin.Context) { |
|||
var requestsData Formula |
|||
err := c.ShouldBindJSON(&requestsData) |
|||
if err != nil { |
|||
publicmethod.Result(1, err, c) |
|||
return |
|||
} |
|||
mathsStr := requestsData.Val |
|||
for _, v := range requestsData.FormJson { |
|||
zhi := strconv.FormatFloat(v.Val, 'f', -1, 64) |
|||
mathsStr = strings.Replace(mathsStr, v.Keys, zhi, 1) |
|||
} |
|||
|
|||
sendData := publicmethod.MapOut[string]() |
|||
|
|||
sendData["mathsStr"] = mathsStr |
|||
|
|||
runMaths, err := AnalyticCalculation(mathsStr, 0, 2) |
|||
|
|||
sendData["runMaths"] = runMaths |
|||
sendData["err"] = err |
|||
|
|||
publicmethod.Result(0, sendData, c) |
|||
} |
|||
|
|||
/* |
|||
* |
|||
@ 作者: 秦东 |
|||
@ 时间: 2024-01-08 10:46:15 |
|||
@ 功能: 解析数学公式 |
|||
@ 参数 |
|||
|
|||
#mathsFormula 组合后的公式 |
|||
#takingMethod 结果保留形式 0:原始数据; 1:四舍五入;2:向上取整;3:向下取整;4:舍去 |
|||
#digit 保留位数 |
|||
|
|||
@ 返回值 |
|||
|
|||
# |
|||
|
|||
@ 方法原型 |
|||
|
|||
# |
|||
*/ |
|||
func AnalyticCalculation(mathsFormula string, takingMethod, digit int) (runMaths float64, err error) { |
|||
//自定义一个Sum计算
|
|||
engine.RegFunction("sum", -1, func(expr ...engine.ExprAST) float64 { |
|||
var sum float64 |
|||
for _, v := range expr { |
|||
sum = sum + engine.ExprASTResult(v) |
|||
} |
|||
return sum |
|||
}) |
|||
engine.RegFunction("average", -1, func(expr ...engine.ExprAST) float64 { |
|||
var sum float64 |
|||
num := 0 |
|||
for _, v := range expr { |
|||
sum = sum + engine.ExprASTResult(v) |
|||
num++ |
|||
} |
|||
if num > 0 { |
|||
sum = sum / float64(num) |
|||
} |
|||
return sum |
|||
}) |
|||
//解析、执行数学公式字符串
|
|||
runMaths, err = engine.ParseAndExec(mathsFormula) |
|||
switch takingMethod { |
|||
case 1: |
|||
// fmt.Printf("1:%v--1-->%v---->%v\n", takingMethod, digit, runMaths)
|
|||
runMaths = publicmethod.DecimalEs(runMaths, digit) |
|||
// fmt.Printf("1:%v---->%v---->%v\n", takingMethod, digit, runMaths)
|
|||
case 2: |
|||
runMaths = math.Ceil(runMaths) |
|||
// fmt.Printf("2:%v---->%v---->%v\n", takingMethod, digit, runMaths)
|
|||
case 3: |
|||
runMaths = math.Floor(runMaths) |
|||
// fmt.Printf("3:%v---->%v---->%v\n", takingMethod, digit, runMaths)
|
|||
case 4: |
|||
runMaths = float64(int64(runMaths)) |
|||
// fmt.Printf("4:%v---->%v---->%v\n", takingMethod, digit, runMaths)
|
|||
default: |
|||
// fmt.Printf("5:%v---->%v---->%v\n", takingMethod, digit, runMaths)
|
|||
return |
|||
} |
|||
return |
|||
} |
|||
|
|||
/* |
|||
* |
|||
@ 作者: 秦东 |
|||
@ 时间: 2024-01-09 14:48:06 |
|||
@ 功能: 数学计算 |
|||
@ 参数 |
|||
|
|||
# |
|||
|
|||
@ 返回值 |
|||
|
|||
# |
|||
|
|||
@ 方法原型 |
|||
|
|||
# |
|||
*/ |
|||
func (a *ApiMethod) MathematicalCalculations(c *gin.Context) { |
|||
var requestsData ReceiveMathsFornula |
|||
err := c.ShouldBindJSON(&requestsData) |
|||
if err != nil { |
|||
publicmethod.Result(1, err, c) |
|||
return |
|||
} |
|||
zhi := make(map[string]float64) |
|||
var formulaList []FormJsonInfor |
|||
for i, v := range requestsData.MathsFornula { |
|||
var formulaCont FormJsonInfor |
|||
formulaCont.Keys = i |
|||
formulaCont.Val = v.MathsFormula |
|||
formulaList = append(formulaList, formulaCont) |
|||
if i != requestsData.FieldKey { |
|||
zhiVal := FieldToValue(v.MathsFormula, requestsData.KeyVal, v.TakingMethod, v.Digit) |
|||
zhi[i] = zhiVal |
|||
} |
|||
} |
|||
// publicmethod.Result(10000, zhi, c)
|
|||
sendData := publicmethod.MapOut[string]() |
|||
sendData["formulaList"] = formulaList |
|||
sendData["zhi"] = zhi |
|||
sendData["requestsData"] = requestsData |
|||
publicmethod.Result(0, zhi, c) |
|||
} |
|||
|
|||
/* |
|||
* |
|||
@ 作者: 秦东 |
|||
@ 时间: 2024-01-09 15:52:41 |
|||
@ 功能: 解析计算数值 |
|||
@ 参数 |
|||
|
|||
#formula 计算公式 |
|||
#formKeyVal 表单字段 |
|||
#takingMethod 结果保留形式 1:四舍五入;2:向上取整;3:向下取整;4:舍去 |
|||
#digit 保留位数 |
|||
|
|||
@ 返回值 |
|||
|
|||
# |
|||
|
|||
@ 方法原型 |
|||
|
|||
# |
|||
*/ |
|||
func FieldToValue(formula string, formKeyVal map[string]interface{}, takingMethod, digit int) (runMaths float64) { |
|||
for i, v := range formKeyVal { |
|||
if val, isOk := v.(string); isOk { |
|||
if val == "" { |
|||
val = "0" |
|||
} |
|||
formula = strings.Replace(formula, i, val, -1) |
|||
} |
|||
if val, isOk := v.(int); isOk { |
|||
valStr := strconv.Itoa(val) |
|||
formula = strings.Replace(formula, i, valStr, -1) |
|||
} |
|||
if val, isOk := v.(int64); isOk { |
|||
valStr := strconv.FormatInt(val, 10) |
|||
formula = strings.Replace(formula, i, valStr, -1) |
|||
} |
|||
if val, isOk := v.(float32); isOk { |
|||
valStr := strconv.FormatFloat(float64(val), 'f', -1, 64) |
|||
formula = strings.Replace(formula, i, valStr, -1) |
|||
} |
|||
if val, isOk := v.(float64); isOk { |
|||
valStr := strconv.FormatFloat(val, 'f', -1, 64) |
|||
formula = strings.Replace(formula, i, valStr, -1) |
|||
} |
|||
} |
|||
formula = strings.Replace(formula, ",", ",", -1) |
|||
smaillMathsStr := strings.ToLower(formula) |
|||
fmt.Printf("smaillMathsStr===>%v\n", smaillMathsStr) |
|||
runMaths, err := AnalyticCalculation(smaillMathsStr, takingMethod, digit) |
|||
if err != nil { |
|||
runMaths = 0 |
|||
return |
|||
} |
|||
|
|||
return |
|||
} |
|||
|
|||
/* |
|||
* |
|||
@ 作者: 秦东 |
|||
@ 时间: 2024-01-09 15:00:33 |
|||
@ 功能: 数学公式字段转化数值 |
|||
@ 参数 |
|||
|
|||
# |
|||
|
|||
@ 返回值 |
|||
|
|||
# |
|||
|
|||
@ 方法原型 |
|||
|
|||
# |
|||
*/ |
|||
func FieldToValues(formulaList []FormJsonInfor, key string, val string, takingMethod, digit int) map[string]float64 { |
|||
var fieldVal map[string]float64 |
|||
for _, v := range formulaList { |
|||
mathsStr := strings.Replace(v.Val, key, val, -1) |
|||
mathsStr = strings.Replace(mathsStr, ",", ",", -1) |
|||
smaillMathsStr := strings.ToLower(mathsStr) |
|||
fmt.Printf("smaillMathsStr===>%v\n", smaillMathsStr) |
|||
runMaths, err := AnalyticCalculation(smaillMathsStr, takingMethod, digit) |
|||
fmt.Printf("runMaths===>%v\n err===>%v\n", runMaths, err) |
|||
// if err == nil {
|
|||
// switch takingMethod {
|
|||
// case 2:
|
|||
// fieldVal[v.Keys] = math.Ceil(runMaths)
|
|||
// case 3:
|
|||
// fieldVal[v.Keys] = math.Floor(runMaths)
|
|||
// case 4:
|
|||
// fieldVal[v.Keys] = math.Floor(runMaths)
|
|||
// default:
|
|||
// fieldVal[v.Keys] = math.Round(publicmethod.DecimalEs(runMaths, digit))
|
|||
// }
|
|||
// } else {
|
|||
// fieldVal[v.Keys] = 0
|
|||
// }
|
|||
} |
|||
return fieldVal |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
package matsformula |
|||
|
|||
import ( |
|||
"appPlatform/overall/publicmethod" |
|||
|
|||
"github.com/gin-gonic/gin" |
|||
) |
|||
|
|||
type ApiMethod struct{} |
|||
|
|||
func (a *ApiMethod) Index(c *gin.Context) { |
|||
outputCont := publicmethod.MapOut[string]() |
|||
outputCont["index"] = "数学函数处理内容列表入口" |
|||
publicmethod.Result(0, outputCont, c) |
|||
} |
|||
|
|||
// 接收公式
|
|||
type Formula struct { |
|||
Val string `json:"val"` |
|||
FormJson []FormJsonCont `json:"formJson"` |
|||
} |
|||
type FormJsonCont struct { |
|||
Keys string `json:"key"` |
|||
Val float64 `json:"val"` |
|||
} |
|||
type FormJsonInfor struct { |
|||
Keys string `json:"key"` |
|||
Val string `json:"val"` |
|||
} |
|||
|
|||
// 接收数学公式及字段表单
|
|||
type ReceiveMathsFornula struct { |
|||
FieldKey string `json:"fieldKey"` |
|||
MathsFornula map[string]MathsValCont `json:"mathsFornula"` |
|||
KeyVal map[string]interface{} `json:"keyVal"` |
|||
} |
|||
|
|||
// 数值配置参数
|
|||
type MathsValCont struct { |
|||
MathsFormula string `json:"mathsFormula"` |
|||
TakingMethod int `json:"takingMethod"` |
|||
Digit int `json:"digit"` |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
package mathsrouter |
|||
|
|||
import ( |
|||
"appPlatform/api/version1" |
|||
|
|||
"github.com/gin-gonic/gin" |
|||
) |
|||
|
|||
// 数学公式接口路由
|
|||
func (a *ApiRouter) RouterGroup(router *gin.RouterGroup) { |
|||
apiRouter := router.Group("maths") |
|||
var mathsClassRouter = version1.AppApiEntry.MathsApi |
|||
{ |
|||
apiRouter.GET("", mathsClassRouter.Index) //入口
|
|||
apiRouter.POST("", mathsClassRouter.Index) //入口
|
|||
apiRouter.POST("am_formulas", mathsClassRouter.AnalyticalMathematicalFormulas) //解析数学公式
|
|||
apiRouter.POST("mathematicalCalculations", mathsClassRouter.MathematicalCalculations) //数学计算
|
|||
} |
|||
} |
|||
@ -0,0 +1,4 @@ |
|||
package mathsrouter |
|||
|
|||
//数学公式路由设定
|
|||
type ApiRouter struct{} |
|||
Loading…
Reference in new issue