data_calculate.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package data
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_task/models/data_manage"
  6. "hongze/hongze_task/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/yidane/formula"
  11. )
  12. //指标运算刷新数据
  13. func RefreshCalculate(edbInfoIdArr []*data_manage.EdbInfo, edbInfoId int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string) (err error) {
  14. defer func() {
  15. if err != nil {
  16. utils.FileLog.Info("Calculate Err:%s" + err.Error())
  17. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshCalculate ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  18. }
  19. }()
  20. saveDataMap := make(map[string]map[int]float64)
  21. for _, v := range edbInfoIdArr {
  22. var condition string
  23. var pars []interface{}
  24. condition += " AND edb_info_id=? "
  25. pars = append(pars, v.EdbInfoId)
  26. if startDate != "" {
  27. condition += " AND data_time>=? "
  28. pars = append(pars, startDate)
  29. }
  30. if endDate != "" {
  31. condition += " AND data_time<=? "
  32. pars = append(pars, endDate)
  33. }
  34. dataList, err := data_manage.GetEdbDataListAll(condition, pars, v.Source, 1)
  35. if err != nil {
  36. return err
  37. }
  38. dataMap := make(map[string]float64)
  39. for _, dv := range dataList {
  40. if val, ok := saveDataMap[dv.DataTime]; ok {
  41. if _, ok := val[v.EdbInfoId]; !ok {
  42. val[v.EdbInfoId] = dv.Value
  43. }
  44. } else {
  45. temp := make(map[int]float64)
  46. temp[v.EdbInfoId] = dv.Value
  47. saveDataMap[dv.DataTime] = temp
  48. }
  49. }
  50. item := new(CalculateItems)
  51. item.EdbInfoId = v.EdbInfoId
  52. item.DataMap = dataMap
  53. }
  54. formulaMap := CheckFormula(formulaStr)
  55. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  56. nowStr := time.Now().Format(utils.FormatDateTime)
  57. var isAdd bool
  58. removeDateList := make([]string, 0) //需要移除的日期
  59. for sk, sv := range saveDataMap {
  60. formulaStr = strings.ToUpper(formulaStr)
  61. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  62. //if formulaFormStr != "" && !strings.Contains(formulaFormStr, "/0") {
  63. if formulaFormStr != "" {
  64. expression := formula.NewExpression(formulaFormStr)
  65. calResult, err := expression.Evaluate()
  66. if err != nil {
  67. // 分母为0的报错
  68. if strings.Contains(err.Error(), "divide by zero") {
  69. removeDateList = append(removeDateList, sk)
  70. continue
  71. }
  72. err = errors.New("计算失败,edbCode:" + fmt.Sprint(edbCode) + ":Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  73. fmt.Println(err)
  74. return err
  75. }
  76. calVal, err := calResult.Float64()
  77. if err != nil {
  78. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  79. fmt.Println(err)
  80. return err
  81. }
  82. count, err := data_manage.GetEdbDataCalculateByCodeAndDate(edbCode, sk)
  83. if err != nil && err.Error() != utils.ErrNoRow() {
  84. return err
  85. }
  86. if count <= 0 { //需要存入的数据
  87. dataTime, _ := time.Parse(utils.FormatDate, sk)
  88. timestamp := dataTime.UnixNano() / 1e6
  89. timeStr := fmt.Sprintf("%d", timestamp)
  90. addSql += "("
  91. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  92. "," + "'" + nowStr + "'" + "," + "1"
  93. addSql += "," + "'" + timeStr + "'"
  94. addSql += "),"
  95. isAdd = true
  96. } else {
  97. calVal = utils.FixFloat(calVal, 4)
  98. err = data_manage.ModifyEdbDataCalculate(int64(edbInfoId), sk, calVal)
  99. if err != nil {
  100. return err
  101. }
  102. }
  103. }
  104. }
  105. if isAdd {
  106. addSql = strings.TrimRight(addSql, ",")
  107. err = data_manage.AddEdbDataCalculateBySql(addSql)
  108. if err != nil {
  109. fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  110. return err
  111. }
  112. }
  113. if len(removeDateList) > 0 {
  114. removeDateStr := strings.Join(removeDateList, `","`)
  115. removeDateStr = `"` + removeDateStr + `"`
  116. //如果拼接指标变更了,那么需要删除所有的指标数据
  117. sql := fmt.Sprintf(` DELETE FROM edb_data_calculate WHERE edb_info_id = ? and data_time in (%s) `, removeDateStr)
  118. err = data_manage.DeleteEdbDataCalculateBySql(sql, edbInfoId)
  119. if err != nil {
  120. err = fmt.Errorf("删除计算失败的计算指标数据失败,Err:" + err.Error())
  121. return
  122. }
  123. }
  124. return
  125. }