edb_data_insert_config.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package data_manage
  2. import (
  3. "errors"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "time"
  9. )
  10. // EdbDataInsertConfig 指标数据插入配置表
  11. type EdbDataInsertConfig struct {
  12. EdbInfoId int `orm:"column(edb_info_id);pk" description:"指标id"`
  13. Date time.Time `description:"插入的日期"`
  14. Value string `description:"插入的值"`
  15. RealDate time.Time `description:"实际数据的值日期"`
  16. ModifyTime time.Time `description:"数据更新时间"`
  17. CreateTime time.Time `description:"数据插入的时间"`
  18. }
  19. // GetEdbDataInsertConfigByEdbId 根据指标id 获取数据插入配置详情
  20. func GetEdbDataInsertConfigByEdbId(edbInfoId int) (item *EdbDataInsertConfig, err error) {
  21. o := orm.NewOrmUsingDB("data")
  22. sql := ` SELECT * FROM edb_data_insert_config WHERE edb_info_id=? `
  23. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  24. return
  25. }
  26. // GetEdbDataInsertConfigByEdbIdList 根据指标id列表 获取数据插入配置详情
  27. func GetEdbDataInsertConfigByEdbIdList(edbInfoIdList []int) (items []*EdbDataInsertConfig, err error) {
  28. num := len(edbInfoIdList)
  29. if num <= 0 {
  30. return
  31. }
  32. o := orm.NewOrmUsingDB("data")
  33. sql := ` SELECT * FROM edb_data_insert_config WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  34. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  35. return
  36. }
  37. // CreateEdbDataInsertConfigAndData 创建数据插入配置规则,及插入数据
  38. func CreateEdbDataInsertConfigAndData(edbInfo *EdbInfo, date time.Time, value string) (err error, errMsg string, isSendEmail bool) {
  39. isSendEmail = true
  40. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  41. if tableName == `` {
  42. err = errors.New("找不到该指标的数据表")
  43. return
  44. }
  45. dateStr := date.Format(utils.FormatDate)
  46. timestamp := date.UnixNano() / 1e6
  47. timeStr := fmt.Sprintf("%d", timestamp)
  48. var saveValue string
  49. if value != "" {
  50. floatValue, e := strconv.ParseFloat(value, 64)
  51. if e != nil {
  52. err = e
  53. fmt.Println("转换失败:", e.Error())
  54. return
  55. }
  56. saveValue = utils.SubFloatToString(floatValue, 30)
  57. }
  58. to, err := orm.NewOrmUsingDB("data").Begin()
  59. if err != nil {
  60. return
  61. }
  62. defer func() {
  63. if err != nil {
  64. _ = to.Rollback()
  65. } else {
  66. _ = to.Commit()
  67. }
  68. }()
  69. var oldConfigDate time.Time //之前配置的日期
  70. // 添加/变更配置
  71. {
  72. var item *EdbDataInsertConfig
  73. sql := ` SELECT * FROM edb_data_insert_config WHERE edb_info_id=? `
  74. err = to.Raw(sql, edbInfo.EdbInfoId).QueryRow(&item)
  75. if err != nil && err.Error() != utils.ErrNoRow() {
  76. return
  77. }
  78. err = nil
  79. // 如果是没有配置,那么就需要添加配置
  80. if item == nil {
  81. var currLatestDate time.Time
  82. currLatestDateStr := edbInfo.LatestDate // 实际日期
  83. if currLatestDateStr != `` && currLatestDateStr != `0000-00-00` {
  84. currLatestDate, _ = time.ParseInLocation(utils.FormatDate, currLatestDateStr, time.Local)
  85. }
  86. if !currLatestDate.IsZero() && (currLatestDate.After(date) || currLatestDate.Equal(date)) {
  87. errMsg = `选择日期不能早于/等于数据最新日期`
  88. err = errors.New("选择日期不能早于/等于数据最新日期,数据最新日期:" + edbInfo.EndDate + ";填写日期:" + dateStr)
  89. isSendEmail = false
  90. return
  91. }
  92. realDate, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  93. item = &EdbDataInsertConfig{
  94. EdbInfoId: edbInfo.EdbInfoId,
  95. Date: date,
  96. Value: saveValue,
  97. RealDate: realDate,
  98. ModifyTime: time.Now(),
  99. CreateTime: time.Now(),
  100. }
  101. _, err = to.Insert(item)
  102. } else {
  103. if date.Equal(item.RealDate) || date.Before(item.RealDate) {
  104. errMsg = `选择日期不能早于/等于数据最新日期`
  105. err = errors.New("选择日期不能早于/等于数据最新日期,数据最新日期:" + edbInfo.EndDate + ";填写日期:" + dateStr)
  106. isSendEmail = false
  107. return
  108. }
  109. oldConfigDate = item.Date // 之前配置的日期
  110. item.Date = date
  111. item.Value = saveValue
  112. item.ModifyTime = time.Now()
  113. _, err = to.Update(item, "Date", "Value", "ModifyTime")
  114. }
  115. if err != nil {
  116. return
  117. }
  118. }
  119. var offsetDate string
  120. // 更改数据
  121. {
  122. var edbDateData *EdbDataList
  123. if !oldConfigDate.IsZero() {
  124. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? AND data_time = ?`
  125. sql = fmt.Sprintf(sql, tableName)
  126. err = to.Raw(sql, edbInfo.EdbInfoId, oldConfigDate.Format(utils.FormatDate)).QueryRow(&edbDateData)
  127. if err != nil && err.Error() != utils.ErrNoRow() {
  128. return
  129. }
  130. err = nil
  131. }
  132. // 如果是没有历史数据,那么就需要增加数据
  133. if edbDateData == nil {
  134. 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) `
  135. addSql = fmt.Sprintf(addSql, tableName, edbInfo.EdbInfoId, edbInfo.EdbCode, dateStr, saveValue, timeStr)
  136. _, err = to.Raw(addSql).Exec()
  137. } else if value == "" {
  138. // 传空值时删除
  139. sql := `SELECT data_time FROM %s WHERE edb_info_id=? ORDER BY data_time DESC LIMIT 1 OFFSET 1`
  140. sql = fmt.Sprintf(sql, tableName)
  141. err = to.Raw(sql, edbInfo.EdbInfoId).QueryRow(&offsetDate)
  142. if err != nil && err.Error() != utils.ErrNoRow() {
  143. return
  144. }
  145. dateStr = offsetDate
  146. deleteSql := `DELETE FROM %s WHERE edb_data_id = %d;`
  147. deleteSql = fmt.Sprintf(deleteSql, tableName, edbDateData.EdbDataId)
  148. _, err = to.Raw(deleteSql).Exec()
  149. } else {
  150. updateSql := `UPDATE %s SET data_time = "%s", value = "%s", modify_time= now(), data_timestamp= %s WHERE edb_data_id = %d;`
  151. updateSql = fmt.Sprintf(updateSql, tableName, dateStr, saveValue, timeStr, edbDateData.EdbDataId)
  152. _, err = to.Raw(updateSql).Exec()
  153. }
  154. if err != nil {
  155. return
  156. }
  157. }
  158. edbInfo.EndDate = dateStr
  159. _, err = to.Update(edbInfo, "EndDate")
  160. return
  161. }