edb_info_calculate.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models/data_manage"
  6. "eta/eta_mobile/utils"
  7. "fmt"
  8. "github.com/shopspring/decimal"
  9. "github.com/yidane/formula"
  10. "math"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. func CheckFormula(formula string) map[string]string {
  17. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN"}
  18. str := strings.ToUpper(formula)
  19. for _, v := range mathFormula {
  20. str = strings.Replace(str, v, "", -1)
  21. }
  22. str = strings.Replace(str, "(", "", -1)
  23. str = strings.Replace(str, ")", "", -1)
  24. byteMap := make(map[string]string)
  25. for i := 0; i < len(str); i++ {
  26. byteInt := str[i]
  27. if byteInt >= 65 && byteInt <= 90 {
  28. byteStr := string(byteInt)
  29. if _, ok := byteMap[byteStr]; !ok {
  30. byteMap[byteStr] = byteStr
  31. }
  32. }
  33. }
  34. return byteMap
  35. }
  36. // CheckFormula2 校验公式是否正常(比如说除法的分母不能为0之类的,实际上就是用预设的字段数据做一次计算)
  37. func CheckFormula2(edbInfoArr []*data_manage.EdbInfo, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) (ok bool, err error) {
  38. valArr := make(map[int]float64)
  39. for _, v := range edbInfoArr {
  40. valArr[v.EdbInfoId] = 100
  41. }
  42. formulaStr = strings.ToUpper(formulaStr)
  43. formulaFormStr := ReplaceFormula(edbInfoArr, valArr, formulaMap, formulaStr, edbInfoIdBytes)
  44. if formulaFormStr == "" {
  45. return
  46. }
  47. expression := formula.NewExpression(formulaFormStr)
  48. _, err = expression.Evaluate()
  49. if err != nil {
  50. } else {
  51. ok = true
  52. }
  53. return
  54. }
  55. type FormulaListItem struct {
  56. Formula string `json:"f"`
  57. Date string `json:"d"`
  58. }
  59. // HandleFormulaJson 处理计算公式json串是否异常
  60. func HandleFormulaJson(formula string, startDate string) (dateSlice []string, formulaMap map[string]string, err error) {
  61. list := make([]FormulaListItem, 0)
  62. err = json.Unmarshal([]byte(formula), &list)
  63. if err != nil {
  64. err = fmt.Errorf("公式串解析失败: json.Unmarshal Err: %v", err)
  65. return
  66. }
  67. formulaMap = make(map[string]string)
  68. dateSlice = make([]string, 0)
  69. // 日期排序
  70. for k, v := range list {
  71. if k == 0 { // 首个日期均为起始日
  72. v.Date = startDate
  73. }
  74. formulaMap[v.Date] = v.Formula
  75. dateSlice = append(dateSlice, v.Date)
  76. }
  77. sort.Slice(dateSlice, func(i, j int) bool {
  78. return dateSlice[i] > dateSlice[j]
  79. })
  80. return
  81. }
  82. // CheckFormulaJson 检测计算公式json串是否异常
  83. func CheckFormulaJson(formula string) (formulaSlice []string, err error) {
  84. list := make([]FormulaListItem, 0)
  85. err = json.Unmarshal([]byte(formula), &list)
  86. if err != nil {
  87. err = fmt.Errorf("公式串解析失败: json.Unmarshal Err: %v", err)
  88. return
  89. }
  90. formulaSlice = make([]string, 0)
  91. // 日期排序
  92. for _, v := range list {
  93. formulaSlice = append(formulaSlice, v.Formula)
  94. }
  95. return
  96. }
  97. type CalculateItems struct {
  98. EdbInfoId int
  99. DataMap map[string]float64
  100. }
  101. func Calculate(edbInfoIdArr []*data_manage.EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (err error) {
  102. defer func() {
  103. if err != nil {
  104. utils.FileLog.Info("Calculate Err:%s" + err.Error())
  105. }
  106. }()
  107. saveDataMap := make(map[string]map[int]float64)
  108. for _, v := range edbInfoIdArr {
  109. var condition string
  110. var pars []interface{}
  111. condition += " AND edb_info_id=? "
  112. pars = append(pars, v.EdbInfoId)
  113. dataList, err := data_manage.GetEdbDataListAll(condition, pars, v.Source, v.SubSource, 1)
  114. if err != nil {
  115. return err
  116. }
  117. dataMap := make(map[string]float64)
  118. for _, dv := range dataList {
  119. if val, ok := saveDataMap[dv.DataTime]; ok {
  120. if _, ok := val[v.EdbInfoId]; !ok {
  121. val[v.EdbInfoId] = dv.Value
  122. }
  123. } else {
  124. temp := make(map[int]float64)
  125. temp[v.EdbInfoId] = dv.Value
  126. saveDataMap[dv.DataTime] = temp
  127. }
  128. }
  129. item := new(CalculateItems)
  130. item.EdbInfoId = v.EdbInfoId
  131. item.DataMap = dataMap
  132. }
  133. formulaMap := CheckFormula(formulaStr)
  134. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  135. nowStr := time.Now().Format(utils.FormatDateTime)
  136. var isAdd bool
  137. for sk, sv := range saveDataMap {
  138. formulaStr = strings.ToUpper(formulaStr)
  139. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  140. if formulaStr == "" {
  141. return
  142. }
  143. if formulaFormStr != "" {
  144. expression := formula.NewExpression(formulaFormStr)
  145. calResult, err := expression.Evaluate()
  146. if err != nil {
  147. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  148. fmt.Println(err)
  149. return err
  150. }
  151. calVal, err := calResult.Float64()
  152. if err != nil {
  153. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  154. fmt.Println(err)
  155. return err
  156. }
  157. //需要存入的数据
  158. {
  159. dataTime, _ := time.Parse(utils.FormatDate, sk)
  160. timestamp := dataTime.UnixNano() / 1e6
  161. timeStr := fmt.Sprintf("%d", timestamp)
  162. addSql += "("
  163. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  164. "," + "'" + nowStr + "'" + "," + "1"
  165. addSql += "," + "'" + timeStr + "'"
  166. addSql += "),"
  167. isAdd = true
  168. }
  169. } else {
  170. fmt.Println("formulaFormStr is empty")
  171. }
  172. }
  173. if isAdd {
  174. addSql = strings.TrimRight(addSql, ",")
  175. data_manage.AddEdbDataCalculateBySql(addSql)
  176. if err != nil {
  177. fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  178. return err
  179. }
  180. }
  181. return
  182. }
  183. func ReplaceFormula(edbInfoIdArr []*data_manage.EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
  184. funMap := GetFormulaMap()
  185. for k, v := range funMap {
  186. formulaStr = strings.Replace(formulaStr, k, v, -1)
  187. }
  188. replaceCount := 0
  189. for dk, dv := range edbInfoIdArr {
  190. if dk == 0 {
  191. dKey := edbInfoIdBytes[dk]
  192. if _, ok := formulaMap[dKey]; ok { //公式中存在
  193. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  194. dvStr := fmt.Sprintf("%v", val)
  195. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  196. replaceCount++
  197. } else {
  198. fmt.Println("valArr not found:", valArr, valOk)
  199. }
  200. } else {
  201. fmt.Println("formulaMap not found:", dKey, dk)
  202. }
  203. }
  204. if dk == 1 {
  205. dKey := edbInfoIdBytes[dk]
  206. if _, ok := formulaMap[dKey]; ok { //公式中存在
  207. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  208. dvStr := fmt.Sprintf("%v", val)
  209. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  210. replaceCount++
  211. } else {
  212. fmt.Println("valArr not found:", valArr, valOk)
  213. }
  214. } else {
  215. fmt.Println("formulaMap not found:", dKey, dk)
  216. }
  217. }
  218. if dk == 2 {
  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 == 3 {
  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. if dk == 4 {
  239. dKey := edbInfoIdBytes[dk]
  240. if _, ok := formulaMap[dKey]; ok { //公式中存在
  241. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  242. dvStr := fmt.Sprintf("%v", val)
  243. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  244. replaceCount++
  245. }
  246. }
  247. }
  248. if dk == 5 {
  249. dKey := edbInfoIdBytes[dk]
  250. if _, ok := formulaMap[dKey]; ok { //公式中存在
  251. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  252. dvStr := fmt.Sprintf("%v", val)
  253. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  254. replaceCount++
  255. }
  256. }
  257. }
  258. if dk == 6 {
  259. dKey := edbInfoIdBytes[dk]
  260. if _, ok := formulaMap[dKey]; ok { //公式中存在
  261. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  262. dvStr := fmt.Sprintf("%v", val)
  263. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  264. replaceCount++
  265. }
  266. }
  267. }
  268. if dk == 7 {
  269. dKey := edbInfoIdBytes[dk]
  270. if _, ok := formulaMap[dKey]; ok { //公式中存在
  271. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  272. dvStr := fmt.Sprintf("%v", val)
  273. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  274. replaceCount++
  275. }
  276. }
  277. }
  278. if dk == 8 {
  279. dKey := edbInfoIdBytes[dk]
  280. if _, ok := formulaMap[dKey]; ok { //公式中存在
  281. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  282. dvStr := fmt.Sprintf("%v", val)
  283. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  284. replaceCount++
  285. }
  286. }
  287. }
  288. if dk == 9 {
  289. dKey := edbInfoIdBytes[dk]
  290. if _, ok := formulaMap[dKey]; ok { //公式中存在
  291. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  292. dvStr := fmt.Sprintf("%v", val)
  293. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  294. replaceCount++
  295. }
  296. }
  297. }
  298. if dk == 10 {
  299. dKey := edbInfoIdBytes[dk]
  300. if _, ok := formulaMap[dKey]; ok { //公式中存在
  301. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  302. dvStr := fmt.Sprintf("%v", val)
  303. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  304. replaceCount++
  305. }
  306. }
  307. }
  308. if dk == 11 {
  309. dKey := edbInfoIdBytes[dk]
  310. if _, ok := formulaMap[dKey]; ok { //公式中存在
  311. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  312. dvStr := fmt.Sprintf("%v", val)
  313. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  314. replaceCount++
  315. }
  316. }
  317. }
  318. if dk == 12 {
  319. dKey := edbInfoIdBytes[dk]
  320. if _, ok := formulaMap[dKey]; ok { //公式中存在
  321. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  322. dvStr := fmt.Sprintf("%v", val)
  323. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  324. replaceCount++
  325. }
  326. }
  327. }
  328. if dk == 13 {
  329. dKey := edbInfoIdBytes[dk]
  330. if _, ok := formulaMap[dKey]; ok { //公式中存在
  331. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  332. dvStr := fmt.Sprintf("%v", val)
  333. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  334. replaceCount++
  335. }
  336. }
  337. }
  338. if dk == 14 {
  339. dKey := edbInfoIdBytes[dk]
  340. if _, ok := formulaMap[dKey]; ok { //公式中存在
  341. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  342. dvStr := fmt.Sprintf("%v", val)
  343. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  344. replaceCount++
  345. }
  346. }
  347. }
  348. if dk == 15 {
  349. dKey := edbInfoIdBytes[dk]
  350. if _, ok := formulaMap[dKey]; ok { //公式中存在
  351. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  352. dvStr := fmt.Sprintf("%v", val)
  353. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  354. replaceCount++
  355. }
  356. }
  357. }
  358. if dk == 16 {
  359. dKey := edbInfoIdBytes[dk]
  360. if _, ok := formulaMap[dKey]; ok { //公式中存在
  361. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  362. dvStr := fmt.Sprintf("%v", val)
  363. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  364. replaceCount++
  365. }
  366. }
  367. }
  368. if dk == 17 {
  369. dKey := edbInfoIdBytes[dk]
  370. if _, ok := formulaMap[dKey]; ok { //公式中存在
  371. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  372. dvStr := fmt.Sprintf("%v", val)
  373. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  374. replaceCount++
  375. }
  376. }
  377. }
  378. if dk == 18 {
  379. dKey := edbInfoIdBytes[dk]
  380. if _, ok := formulaMap[dKey]; ok { //公式中存在
  381. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  382. dvStr := fmt.Sprintf("%v", val)
  383. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  384. replaceCount++
  385. }
  386. }
  387. }
  388. if dk == 19 {
  389. dKey := edbInfoIdBytes[dk]
  390. if _, ok := formulaMap[dKey]; ok { //公式中存在
  391. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  392. dvStr := fmt.Sprintf("%v", val)
  393. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  394. replaceCount++
  395. }
  396. }
  397. }
  398. if dk == 20 {
  399. dKey := edbInfoIdBytes[dk]
  400. if _, ok := formulaMap[dKey]; ok { //公式中存在
  401. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  402. dvStr := fmt.Sprintf("%v", val)
  403. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  404. replaceCount++
  405. }
  406. }
  407. }
  408. if dk == 21 {
  409. dKey := edbInfoIdBytes[dk]
  410. if _, ok := formulaMap[dKey]; ok { //公式中存在
  411. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  412. dvStr := fmt.Sprintf("%v", val)
  413. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  414. replaceCount++
  415. }
  416. }
  417. }
  418. if dk == 22 {
  419. dKey := edbInfoIdBytes[dk]
  420. if _, ok := formulaMap[dKey]; ok { //公式中存在
  421. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  422. dvStr := fmt.Sprintf("%v", val)
  423. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  424. replaceCount++
  425. }
  426. }
  427. }
  428. if dk == 23 {
  429. dKey := edbInfoIdBytes[dk]
  430. if _, ok := formulaMap[dKey]; ok { //公式中存在
  431. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  432. dvStr := fmt.Sprintf("%v", val)
  433. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  434. replaceCount++
  435. }
  436. }
  437. }
  438. if dk == 24 {
  439. dKey := edbInfoIdBytes[dk]
  440. if _, ok := formulaMap[dKey]; ok { //公式中存在
  441. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  442. dvStr := fmt.Sprintf("%v", val)
  443. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  444. replaceCount++
  445. }
  446. }
  447. }
  448. if dk == 25 {
  449. dKey := edbInfoIdBytes[dk]
  450. if _, ok := formulaMap[dKey]; ok { //公式中存在
  451. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  452. dvStr := fmt.Sprintf("%v", val)
  453. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  454. replaceCount++
  455. }
  456. }
  457. }
  458. }
  459. for k, v := range funMap {
  460. formulaStr = strings.Replace(formulaStr, v, k, -1)
  461. }
  462. if replaceCount == len(formulaMap) {
  463. return formulaStr
  464. } else {
  465. return ""
  466. }
  467. }
  468. func GetFormulaMap() map[string]string {
  469. funMap := make(map[string]string)
  470. funMap["MAX"] = "[@@]"
  471. funMap["MIN"] = "[@!]"
  472. funMap["ABS"] = "[@#]"
  473. funMap["CEIL"] = "[@$]"
  474. funMap["COS"] = "[@%]"
  475. funMap["FLOOR"] = "[@^]"
  476. funMap["MOD"] = "[@&]"
  477. funMap["POW"] = "[@*]"
  478. funMap["ROUND"] = "[@(]"
  479. return funMap
  480. }
  481. // 刷新数据
  482. func RefreshCalculate(edbInfoIdArr []*data_manage.EdbInfo, edbInfoId int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string) (err error) {
  483. defer func() {
  484. if err != nil {
  485. fmt.Println("RefreshCalculate:Err " + err.Error())
  486. utils.FileLog.Info("Calculate Err:%s" + err.Error())
  487. }
  488. }()
  489. saveDataMap := make(map[string]map[int]float64)
  490. for _, v := range edbInfoIdArr {
  491. var condition string
  492. var pars []interface{}
  493. condition += " AND edb_info_id=? "
  494. pars = append(pars, v.EdbInfoId)
  495. if startDate != "" {
  496. condition += " AND data_time>=? "
  497. pars = append(pars, startDate)
  498. }
  499. if endDate != "" {
  500. condition += " AND data_time<=? "
  501. pars = append(pars, endDate)
  502. }
  503. dataList, err := data_manage.GetEdbDataListAll(condition, pars, v.Source, v.SubSource, 1)
  504. if err != nil {
  505. return err
  506. }
  507. dataMap := make(map[string]float64)
  508. for _, dv := range dataList {
  509. if val, ok := saveDataMap[dv.DataTime]; ok {
  510. if _, ok := val[v.EdbInfoId]; !ok {
  511. val[v.EdbInfoId] = dv.Value
  512. }
  513. } else {
  514. temp := make(map[int]float64)
  515. temp[v.EdbInfoId] = dv.Value
  516. saveDataMap[dv.DataTime] = temp
  517. }
  518. }
  519. item := new(CalculateItems)
  520. item.EdbInfoId = v.EdbInfoId
  521. item.DataMap = dataMap
  522. }
  523. formulaMap := CheckFormula(formulaStr)
  524. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  525. nowStr := time.Now().Format(utils.FormatDateTime)
  526. var isAdd bool
  527. for sk, sv := range saveDataMap {
  528. formulaStr = strings.ToUpper(formulaStr)
  529. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  530. if formulaFormStr != "" {
  531. utils.FileLog.Info("formulaFormStr:%s", formulaFormStr)
  532. expression := formula.NewExpression(formulaFormStr)
  533. calResult, err := expression.Evaluate()
  534. if err != nil {
  535. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  536. fmt.Println(err)
  537. return err
  538. }
  539. calVal, err := calResult.Float64()
  540. if err != nil {
  541. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  542. fmt.Println(err)
  543. return err
  544. }
  545. count, err := data_manage.GetEdbDataCalculateByCodeAndDate(edbCode, sk)
  546. if err != nil && err.Error() != utils.ErrNoRow() {
  547. return err
  548. }
  549. if count <= 0 { //需要存入的数据
  550. dataTime, _ := time.Parse(utils.FormatDate, sk)
  551. timestamp := dataTime.UnixNano() / 1e6
  552. timeStr := fmt.Sprintf("%d", timestamp)
  553. addSql += "("
  554. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  555. "," + "'" + nowStr + "'" + "," + "1"
  556. addSql += "," + "'" + timeStr + "'"
  557. addSql += "),"
  558. isAdd = true
  559. } else {
  560. calVal = utils.FixFloat(calVal, 4)
  561. err = data_manage.ModifyEdbDataCalculate(int64(edbInfoId), sk, calVal)
  562. if err != nil {
  563. return err
  564. }
  565. }
  566. }
  567. }
  568. if isAdd {
  569. addSql = strings.TrimRight(addSql, ",")
  570. data_manage.AddEdbDataCalculateBySql(addSql)
  571. if err != nil {
  572. fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  573. return err
  574. }
  575. }
  576. return
  577. }
  578. // 处理整个数据
  579. func handleDateSaveDataMap(dateList []string, realSaveDataMap, saveDataMap map[string]map[int]float64, edbInfoIdArr []*data_manage.EdbInfo) {
  580. for _, date := range dateList {
  581. tmpDataMap := realSaveDataMap[date]
  582. for _, edbInfo := range edbInfoIdArr {
  583. tmpEdbInfoId := edbInfo.EdbInfoId // 当前指标id
  584. // 如果该日期不存在该指标数据,那么需要找寻前后日期的数据,进行填补
  585. if _, ok := tmpDataMap[tmpEdbInfoId]; !ok {
  586. //day := 0
  587. //switch edbInfo.Frequency {
  588. //case "周度":
  589. // day = 7
  590. //case "旬度":
  591. // day = 15
  592. //case "月度":
  593. // day = 30
  594. //case "季度":
  595. // day = 90
  596. //case "年度":
  597. // day = 365
  598. //}
  599. // 需求池 255 指标运算文案修改,补数据遍历区间修改(2023-3-7 09:37:23修改)
  600. day := 35
  601. handleDateDataMap(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, day)
  602. }
  603. }
  604. }
  605. }
  606. // handleDataMap 处理单个日期的数据
  607. func handleDateDataMap(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId, day int) {
  608. currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  609. // 后一天
  610. nextDateDayStr := currDate.AddDate(0, 0, 1).Format(utils.FormatDate)
  611. // 前一天
  612. preDateDayStr := currDate.AddDate(0, 0, -1).Format(utils.FormatDate)
  613. for i := 1; i <= day; i++ {
  614. // 下个日期的数据
  615. {
  616. if i >= 1 {
  617. nextDateDayStr = currDate.AddDate(0, 0, i).Format(utils.FormatDate)
  618. }
  619. if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
  620. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  621. saveDataMap[date][edbInfoId] = val
  622. return
  623. }
  624. }
  625. }
  626. // 上个日期的数据
  627. {
  628. if i >= 1 {
  629. preDateDayStr = currDate.AddDate(0, 0, -i).Format(utils.FormatDate)
  630. }
  631. if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
  632. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  633. saveDataMap[date][edbInfoId] = val
  634. return
  635. }
  636. }
  637. }
  638. }
  639. }
  640. // handleDataByLinearRegression 插值法补充数据(线性方程式)
  641. func handleDataByLinearRegression(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (err error) {
  642. if len(edbInfoDataList) < 2 {
  643. return
  644. }
  645. var startEdbInfoData *data_manage.EdbDataList
  646. for _, v := range edbInfoDataList {
  647. handleDataMap[v.DataTime] = v.Value
  648. // 第一个数据就给过滤了,给后面的试用
  649. if startEdbInfoData == nil {
  650. startEdbInfoData = v
  651. continue
  652. }
  653. // 获取两条数据之间相差的天数
  654. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  655. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  656. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  657. betweenDay := betweenHour / 24
  658. // 如果相差一天,那么过滤
  659. if betweenDay <= 1 {
  660. startEdbInfoData = v
  661. continue
  662. }
  663. // 生成线性方程式
  664. var a, b float64
  665. {
  666. coordinateData := make([]utils.Coordinate, 0)
  667. tmpCoordinate1 := utils.Coordinate{
  668. X: 1,
  669. Y: startEdbInfoData.Value,
  670. }
  671. coordinateData = append(coordinateData, tmpCoordinate1)
  672. tmpCoordinate2 := utils.Coordinate{
  673. X: float64(betweenDay) + 1,
  674. Y: v.Value,
  675. }
  676. coordinateData = append(coordinateData, tmpCoordinate2)
  677. a, b = utils.GetLinearResult(coordinateData)
  678. if math.IsNaN(a) || math.IsNaN(b) {
  679. err = errors.New("线性方程公式生成失败")
  680. return
  681. }
  682. }
  683. // 生成对应的值
  684. {
  685. for i := 1; i < betweenDay; i++ {
  686. tmpDataTime := startDataTime.AddDate(0, 0, i)
  687. aDecimal := decimal.NewFromFloat(a)
  688. xDecimal := decimal.NewFromInt(int64(i) + 1)
  689. bDecimal := decimal.NewFromFloat(b)
  690. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  691. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
  692. }
  693. }
  694. startEdbInfoData = v
  695. }
  696. return
  697. }