edb_data_insert_config.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package data_manage
  2. import (
  3. "errors"
  4. "eta/eta_mobile/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "time"
  8. )
  9. // EdbDataInsertConfig 指标数据插入配置表
  10. type EdbDataInsertConfig struct {
  11. EdbInfoId int `orm:"column(edb_info_id);pk" description:"指标id"`
  12. Date time.Time `description:"插入的日期"`
  13. Value string `description:"插入的值"`
  14. RealDate time.Time `description:"实际数据的值日期"`
  15. ModifyTime time.Time `description:"数据更新时间"`
  16. CreateTime time.Time `description:"数据插入的时间"`
  17. }
  18. // GetEdbDataInsertConfigByEdbId 根据指标id 获取数据插入配置详情
  19. func GetEdbDataInsertConfigByEdbId(edbInfoId int) (item *EdbDataInsertConfig, err error) {
  20. o := orm.NewOrmUsingDB("data")
  21. sql := ` SELECT * FROM edb_data_insert_config WHERE edb_info_id=? `
  22. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  23. return
  24. }
  25. // CreateEdbDataInsertConfigAndData 创建数据插入配置规则,及插入数据
  26. func CreateEdbDataInsertConfigAndData(edbInfo *EdbInfo, date time.Time, value float64) (err error, errMsg string, isSendEmail bool) {
  27. isSendEmail = true
  28. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  29. if tableName == `` {
  30. err = errors.New("找不到该指标的数据表")
  31. return
  32. }
  33. dateStr := date.Format(utils.FormatDate)
  34. timestamp := date.UnixNano() / 1e6
  35. timeStr := fmt.Sprintf("%d", timestamp)
  36. saveValue := utils.SubFloatToString(value, 30)
  37. to, err := orm.NewOrmUsingDB("data").Begin()
  38. if err != nil {
  39. return
  40. }
  41. defer func() {
  42. if err != nil {
  43. _ = to.Rollback()
  44. } else {
  45. _ = to.Commit()
  46. }
  47. }()
  48. var oldConfigDate time.Time //之前配置的日期
  49. // 添加/变更配置
  50. {
  51. var item *EdbDataInsertConfig
  52. sql := ` SELECT * FROM edb_data_insert_config WHERE edb_info_id=? `
  53. err = to.Raw(sql, edbInfo.EdbInfoId).QueryRow(&item)
  54. if err != nil && err.Error() != utils.ErrNoRow() {
  55. return
  56. }
  57. err = nil
  58. // 如果是没有配置,那么就需要添加配置
  59. if item == nil {
  60. var currLatestDate time.Time
  61. currLatestDateStr := edbInfo.LatestDate // 实际日期
  62. if currLatestDateStr != `` && currLatestDateStr != `0000-00-00` {
  63. currLatestDate, _ = time.ParseInLocation(utils.FormatDate, currLatestDateStr, time.Local)
  64. }
  65. if !currLatestDate.IsZero() && (currLatestDate.After(date) || currLatestDate.Equal(date)) {
  66. errMsg = `选择日期不能早于/等于数据最新日期`
  67. err = errors.New("选择日期不能早于/等于数据最新日期,数据最新日期:" + edbInfo.EndDate + ";填写日期:" + dateStr)
  68. isSendEmail = false
  69. return
  70. }
  71. realDate, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  72. item = &EdbDataInsertConfig{
  73. EdbInfoId: edbInfo.EdbInfoId,
  74. Date: date,
  75. Value: saveValue,
  76. RealDate: realDate,
  77. ModifyTime: time.Now(),
  78. CreateTime: time.Now(),
  79. }
  80. _, err = to.Insert(item)
  81. } else {
  82. if date.Equal(item.RealDate) || date.Before(item.RealDate) {
  83. errMsg = `选择日期不能早于/等于数据最新日期`
  84. err = errors.New("选择日期不能早于/等于数据最新日期,数据最新日期:" + edbInfo.EndDate + ";填写日期:" + dateStr)
  85. isSendEmail = false
  86. return
  87. }
  88. oldConfigDate = item.Date // 之前配置的日期
  89. item.Date = date
  90. item.Value = saveValue
  91. item.ModifyTime = time.Now()
  92. _, err = to.Update(item, "Date", "Value", "ModifyTime")
  93. }
  94. if err != nil {
  95. return
  96. }
  97. }
  98. // 更改数据
  99. {
  100. var edbDateData *EdbDataList
  101. if !oldConfigDate.IsZero() {
  102. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? AND data_time = ?`
  103. sql = fmt.Sprintf(sql, tableName)
  104. err = to.Raw(sql, edbInfo.EdbInfoId, oldConfigDate.Format(utils.FormatDate)).QueryRow(&edbDateData)
  105. if err != nil && err.Error() != utils.ErrNoRow() {
  106. return
  107. }
  108. err = nil
  109. }
  110. // 如果是没有历史数据,那么就需要增加数据
  111. if edbDateData == nil {
  112. addSql := ` INSERT INTO %s (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) VALUES ( %d, "%s", "%s", "%s", now(), now(), %s) `
  113. addSql = fmt.Sprintf(addSql, tableName, edbInfo.EdbInfoId, edbInfo.EdbCode, dateStr, saveValue, timeStr)
  114. _, err = to.Raw(addSql).Exec()
  115. } else {
  116. updateSql := `UPDATE %s SET data_time = "%s", value = "%s", modify_time= now(), data_timestamp= %s WHERE edb_data_id = %d;`
  117. updateSql = fmt.Sprintf(updateSql, tableName, dateStr, saveValue, timeStr, edbDateData.EdbDataId)
  118. _, err = to.Raw(updateSql).Exec()
  119. }
  120. if err != nil {
  121. return
  122. }
  123. }
  124. // 更改指标的数据结束日期
  125. edbInfo.EndDate = dateStr
  126. _, err = to.Update(edbInfo, "EndDate")
  127. return
  128. }