edb_data_baiinfo.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type BaseFromBaiinfoDataSimple struct {
  11. BaiinfoDataId int `orm:"column(baiinfo_data_id);pk"`
  12. BaseFromBaiinfoIndexId int
  13. IndexCode string
  14. DataTime string
  15. Value string
  16. }
  17. func GetEdbDataBaiinfoMaxAndMinDate(edbCode string) (min_date, max_date string, err error) {
  18. o := orm.NewOrmUsingDB("data")
  19. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_baiinfo WHERE edb_code=? `
  20. err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
  21. return
  22. }
  23. // 全部刷新有色数据
  24. func RefreshAllEdbDataByBaiinfo(edbInfoId, source, subSource int, edbCode, startDate, endDate string) (err error) {
  25. o := orm.NewOrmUsingDB("data")
  26. to, err := o.Begin()
  27. if err != nil {
  28. return
  29. }
  30. defer func() {
  31. if err != nil {
  32. _ = to.Rollback()
  33. } else {
  34. _ = to.Commit()
  35. }
  36. }()
  37. if err != nil {
  38. return
  39. }
  40. edbInfoIdStr := strconv.Itoa(edbInfoId)
  41. //获取已存在指标所有数据
  42. existDataList := make([]*EdbDataBase, 0)
  43. dataTableName := GetEdbDataTableName(source, subSource)
  44. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  45. sql = fmt.Sprintf(sql, dataTableName)
  46. _, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
  47. if err != nil {
  48. return err
  49. }
  50. existDataMap := make(map[string]string)
  51. for _, v := range existDataList {
  52. existDataMap[v.DataTime] = v.Value
  53. }
  54. baiinfoDateList := make([]*BaseFromBaiinfoDataSimple, 0)
  55. baiinfoSql := ` SELECT * FROM base_from_baiinfo_data WHERE index_code=? AND data_time>=? `
  56. _, err = to.Raw(baiinfoSql, edbCode, startDate).QueryRows(&baiinfoDateList)
  57. if err != nil {
  58. return err
  59. }
  60. addSql := ` INSERT INTO edb_data_baiinfo(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  61. var isAdd bool
  62. for _, sv := range baiinfoDateList {
  63. if existVal, ok := existDataMap[sv.DataTime]; !ok {
  64. dataTime, err := time.Parse(utils.FormatDate, sv.DataTime)
  65. if err != nil {
  66. return err
  67. }
  68. timestamp := dataTime.UnixNano() / 1e6
  69. timeStr := fmt.Sprintf("%d", timestamp)
  70. addSql += GetAddSql(edbInfoIdStr, edbCode, sv.DataTime, timeStr, sv.Value)
  71. isAdd = true
  72. } else {
  73. if existVal != sv.Value {
  74. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  75. sql = fmt.Sprintf(sql, dataTableName)
  76. _, err = to.Raw(sql, sv.Value, edbInfoId, sv.DataTime).Exec()
  77. if err != nil {
  78. return err
  79. }
  80. }
  81. }
  82. }
  83. if isAdd {
  84. addSql = strings.TrimRight(addSql, ",")
  85. _, err = to.Raw(addSql).Exec()
  86. if err != nil {
  87. return err
  88. }
  89. }
  90. return
  91. }