edb_info_calculate.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package data
  2. import (
  3. "eta/eta_task/models/data_manage"
  4. "fmt"
  5. "strings"
  6. )
  7. type CalculateItems struct {
  8. EdbInfoId int
  9. DataMap map[string]float64
  10. }
  11. func CheckFormula(formula string) map[string]string {
  12. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG"}
  13. str := strings.ToUpper(formula)
  14. for _, v := range mathFormula {
  15. str = strings.Replace(str, v, "", -1)
  16. }
  17. str = strings.Replace(str, "(", "", -1)
  18. str = strings.Replace(str, ")", "", -1)
  19. byteMap := make(map[string]string)
  20. for i := 0; i < len(str); i++ {
  21. byteInt := str[i]
  22. if byteInt >= 65 && byteInt <= 90 {
  23. byteStr := string(byteInt)
  24. if _, ok := byteMap[byteStr]; !ok {
  25. byteMap[byteStr] = byteStr
  26. }
  27. }
  28. }
  29. return byteMap
  30. }
  31. func ReplaceFormula(edbInfoIdArr []*data_manage.EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
  32. funMap := GetFormulaMap()
  33. for k, v := range funMap {
  34. formulaStr = strings.Replace(formulaStr, k, v, -1)
  35. }
  36. replaceCount := 0
  37. for dk, dv := range edbInfoIdArr {
  38. if dk == 0 {
  39. dKey := edbInfoIdBytes[dk]
  40. if _, ok := formulaMap[dKey]; ok { //公式中存在
  41. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  42. dvStr := fmt.Sprintf("%v", val)
  43. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  44. replaceCount++
  45. }
  46. }
  47. }
  48. if dk == 1 {
  49. dKey := edbInfoIdBytes[dk]
  50. if _, ok := formulaMap[dKey]; ok { //公式中存在
  51. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  52. dvStr := fmt.Sprintf("%v", val)
  53. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  54. replaceCount++
  55. }
  56. }
  57. }
  58. if dk == 2 {
  59. dKey := edbInfoIdBytes[dk]
  60. if _, ok := formulaMap[dKey]; ok { //公式中存在
  61. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  62. dvStr := fmt.Sprintf("%v", val)
  63. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  64. replaceCount++
  65. }
  66. }
  67. }
  68. if dk == 3 {
  69. dKey := edbInfoIdBytes[dk]
  70. if _, ok := formulaMap[dKey]; ok { //公式中存在
  71. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  72. dvStr := fmt.Sprintf("%v", val)
  73. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  74. replaceCount++
  75. }
  76. }
  77. }
  78. if dk == 4 {
  79. dKey := edbInfoIdBytes[dk]
  80. if _, ok := formulaMap[dKey]; ok { //公式中存在
  81. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  82. dvStr := fmt.Sprintf("%v", val)
  83. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  84. replaceCount++
  85. }
  86. }
  87. }
  88. if dk == 5 {
  89. dKey := edbInfoIdBytes[dk]
  90. if _, ok := formulaMap[dKey]; ok { //公式中存在
  91. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  92. dvStr := fmt.Sprintf("%v", val)
  93. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  94. replaceCount++
  95. }
  96. }
  97. }
  98. if dk == 6 {
  99. dKey := edbInfoIdBytes[dk]
  100. if _, ok := formulaMap[dKey]; ok { //公式中存在
  101. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  102. dvStr := fmt.Sprintf("%v", val)
  103. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  104. replaceCount++
  105. }
  106. }
  107. }
  108. if dk == 7 {
  109. dKey := edbInfoIdBytes[dk]
  110. if _, ok := formulaMap[dKey]; ok { //公式中存在
  111. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  112. dvStr := fmt.Sprintf("%v", val)
  113. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  114. replaceCount++
  115. }
  116. }
  117. }
  118. if dk == 8 {
  119. dKey := edbInfoIdBytes[dk]
  120. if _, ok := formulaMap[dKey]; ok { //公式中存在
  121. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  122. dvStr := fmt.Sprintf("%v", val)
  123. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  124. replaceCount++
  125. }
  126. }
  127. }
  128. if dk == 9 {
  129. dKey := edbInfoIdBytes[dk]
  130. if _, ok := formulaMap[dKey]; ok { //公式中存在
  131. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  132. dvStr := fmt.Sprintf("%v", val)
  133. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  134. replaceCount++
  135. }
  136. }
  137. }
  138. if dk == 10 {
  139. dKey := edbInfoIdBytes[dk]
  140. if _, ok := formulaMap[dKey]; ok { //公式中存在
  141. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  142. dvStr := fmt.Sprintf("%v", val)
  143. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  144. replaceCount++
  145. }
  146. }
  147. }
  148. if dk == 11 {
  149. dKey := edbInfoIdBytes[dk]
  150. if _, ok := formulaMap[dKey]; ok { //公式中存在
  151. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  152. dvStr := fmt.Sprintf("%v", val)
  153. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  154. replaceCount++
  155. }
  156. }
  157. }
  158. if dk == 12 {
  159. dKey := edbInfoIdBytes[dk]
  160. if _, ok := formulaMap[dKey]; ok { //公式中存在
  161. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  162. dvStr := fmt.Sprintf("%v", val)
  163. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  164. replaceCount++
  165. }
  166. }
  167. }
  168. if dk == 13 {
  169. dKey := edbInfoIdBytes[dk]
  170. if _, ok := formulaMap[dKey]; ok { //公式中存在
  171. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  172. dvStr := fmt.Sprintf("%v", val)
  173. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  174. replaceCount++
  175. }
  176. }
  177. }
  178. if dk == 14 {
  179. dKey := edbInfoIdBytes[dk]
  180. if _, ok := formulaMap[dKey]; ok { //公式中存在
  181. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  182. dvStr := fmt.Sprintf("%v", val)
  183. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  184. replaceCount++
  185. }
  186. }
  187. }
  188. if dk == 15 {
  189. dKey := edbInfoIdBytes[dk]
  190. if _, ok := formulaMap[dKey]; ok { //公式中存在
  191. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  192. dvStr := fmt.Sprintf("%v", val)
  193. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  194. replaceCount++
  195. }
  196. }
  197. }
  198. if dk == 16 {
  199. dKey := edbInfoIdBytes[dk]
  200. if _, ok := formulaMap[dKey]; ok { //公式中存在
  201. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  202. dvStr := fmt.Sprintf("%v", val)
  203. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  204. replaceCount++
  205. }
  206. }
  207. }
  208. if dk == 17 {
  209. dKey := edbInfoIdBytes[dk]
  210. if _, ok := formulaMap[dKey]; ok { //公式中存在
  211. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  212. dvStr := fmt.Sprintf("%v", val)
  213. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  214. replaceCount++
  215. }
  216. }
  217. }
  218. if dk == 18 {
  219. dKey := edbInfoIdBytes[dk]
  220. if _, ok := formulaMap[dKey]; ok { //公式中存在
  221. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  222. dvStr := fmt.Sprintf("%v", val)
  223. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  224. replaceCount++
  225. }
  226. }
  227. }
  228. if dk == 19 {
  229. dKey := edbInfoIdBytes[dk]
  230. if _, ok := formulaMap[dKey]; ok { //公式中存在
  231. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  232. dvStr := fmt.Sprintf("%v", val)
  233. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  234. replaceCount++
  235. }
  236. }
  237. }
  238. }
  239. for k, v := range funMap {
  240. formulaStr = strings.Replace(formulaStr, v, k, -1)
  241. }
  242. if replaceCount == len(formulaMap) {
  243. return formulaStr
  244. } else {
  245. return ""
  246. }
  247. }
  248. func GetFormulaMap() map[string]string {
  249. funMap := make(map[string]string)
  250. funMap["MAX"] = "[@@]"
  251. funMap["MIN"] = "[@!]"
  252. funMap["ABS"] = "[@#]"
  253. funMap["CEIL"] = "[@$]"
  254. funMap["COS"] = "[@%]"
  255. funMap["FLOOR"] = "[@^]"
  256. funMap["MOD"] = "[@&]"
  257. funMap["POW"] = "[@*]"
  258. funMap["ROUND"] = "[@(]"
  259. return funMap
  260. }
  261. // 修复计算指标与基础指标的关联关系
  262. func FixEdbInfoCalculateMapping() {
  263. fmt.Println("start")
  264. data_manage.FixEdbInfoCalculateMapping()
  265. fmt.Println("end")
  266. }