edb_data_calculate.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/yidane/formula"
  7. "hongze/hongze_ETA_mobile_api/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type EdbDataCalculate struct {
  13. EdbDataId int `orm:"column(edb_data_id);pk"`
  14. EdbInfoId int
  15. EdbCode string
  16. DataTime string
  17. Value float64
  18. Status int
  19. CreateTime time.Time
  20. ModifyTime time.Time
  21. DataTimestamp int64
  22. }
  23. func AddEdbDataCalculate(items []*EdbDataCalculate) (err error) {
  24. o := orm.NewOrmUsingDB("data")
  25. _, err = o.InsertMulti(1, items)
  26. return
  27. }
  28. func AddEdbDataCalculateBySql(sqlStr string) (err error) {
  29. o := orm.NewOrmUsingDB("data")
  30. _, err = o.Raw(sqlStr).Exec()
  31. return
  32. }
  33. func ModifyEdbDataCalculate(edbInfoId int64, dataTime string, value float64) (err error) {
  34. o := orm.NewOrmUsingDB("data")
  35. sql := ` UPDATE edb_data_calculate SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  36. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  37. return
  38. }
  39. func GetEdbDataCalculateByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  40. o := orm.NewOrmUsingDB("data")
  41. sql := ` SELECT COUNT(1) AS count FROM edb_data_calculate WHERE edb_code=? AND data_time=? `
  42. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  43. return
  44. }
  45. //刷新全部数据
  46. func RefreshAllCalculate(edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string) (err error) {
  47. o := orm.NewOrmUsingDB("data")
  48. to, err := o.Begin()
  49. if err != nil {
  50. return
  51. }
  52. defer func() {
  53. if err != nil {
  54. _ = to.Rollback()
  55. } else {
  56. _ = to.Commit()
  57. }
  58. }()
  59. fmt.Println(startDate, endDate)
  60. saveDataMap := make(map[string]map[int]float64)
  61. for _, v := range edbInfoIdArr {
  62. var condition string
  63. var pars []interface{}
  64. condition += " AND edb_info_id=? "
  65. pars = append(pars, v.EdbInfoId)
  66. if startDate != "" {
  67. condition += " AND data_time>=? "
  68. pars = append(pars, startDate)
  69. }
  70. if endDate != "" {
  71. condition += " AND data_time<=? "
  72. pars = append(pars, endDate)
  73. }
  74. fmt.Println("v.Source:", v.Source)
  75. dataList, err := GetEdbDataListAll(condition, pars, v.Source, 1)
  76. if err != nil {
  77. return err
  78. }
  79. dataMap := make(map[string]float64)
  80. for _, dv := range dataList {
  81. if val, ok := saveDataMap[dv.DataTime]; ok {
  82. if _, ok := val[v.EdbInfoId]; !ok {
  83. val[v.EdbInfoId] = dv.Value
  84. }
  85. } else {
  86. temp := make(map[int]float64)
  87. temp[v.EdbInfoId] = dv.Value
  88. saveDataMap[dv.DataTime] = temp
  89. }
  90. }
  91. item := new(CalculateItems)
  92. item.EdbInfoId = v.EdbInfoId
  93. item.DataMap = dataMap
  94. }
  95. formulaMap := CheckFormula(formulaStr)
  96. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  97. var isAdd bool
  98. //获取指标所有数据
  99. dataList := make([]*EdbDataBase, 0)
  100. dataTableName := GetEdbDataTableName(source)
  101. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  102. sql = fmt.Sprintf(sql, dataTableName)
  103. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  104. if err != nil {
  105. return err
  106. }
  107. dataMap := make(map[string]string)
  108. for _, v := range dataList {
  109. dataMap[v.DataTime] = v.Value
  110. }
  111. edbInfoIdStr := strconv.Itoa(edbInfoId)
  112. existDataMap := make(map[string]string)
  113. for sk, sv := range saveDataMap {
  114. fmt.Println(sk, sv)
  115. formulaStr = strings.ToUpper(formulaStr)
  116. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  117. if formulaFormStr != "" {
  118. utils.FileLog.Info("formulaFormStr:%s", formulaFormStr)
  119. expression := formula.NewExpression(formulaFormStr)
  120. calResult, err := expression.Evaluate()
  121. if err != nil {
  122. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  123. fmt.Println(err)
  124. return err
  125. }
  126. calVal, err := calResult.Float64()
  127. if err != nil {
  128. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  129. fmt.Println(err)
  130. return err
  131. }
  132. saveValue := utils.SubFloatToString(calVal, 4)
  133. if existVal, ok := dataMap[sk]; !ok {
  134. dataTime, _ := time.Parse(utils.FormatDate, sk)
  135. timestamp := dataTime.UnixNano() / 1e6
  136. timeStr := fmt.Sprintf("%d", timestamp)
  137. if _, existOk := existDataMap[sk]; !existOk {
  138. addSql += GetAddSql(edbInfoIdStr, edbCode, sk, timeStr, saveValue)
  139. isAdd = true
  140. }
  141. existDataMap[sk] = sk
  142. } else {
  143. if existVal != saveValue {
  144. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  145. sql = fmt.Sprintf(sql, dataTableName)
  146. _, err = to.Raw(sql, saveValue, edbInfoId, sk).Exec()
  147. if err != nil {
  148. return err
  149. }
  150. }
  151. }
  152. }
  153. }
  154. if isAdd {
  155. addSql = strings.TrimRight(addSql, ",")
  156. _, err = to.Raw(addSql).Exec()
  157. if err != nil {
  158. fmt.Println("RefreshAllCalculate add Err", err.Error())
  159. return
  160. }
  161. }
  162. return
  163. }