edb_data_gl.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_task/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func AddEdbDataGlBySql(sqlStr string) (err error) {
  11. o := orm.NewOrmUsingDB("data")
  12. _, err = o.Raw(sqlStr).Exec()
  13. return
  14. }
  15. type GlData struct {
  16. InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
  17. DataTime string `orm:"column(DATA_DATE)" description:"值"`
  18. }
  19. func GetGlDataByTradeCode(condition string, pars []interface{}) (item []*GlData, err error) {
  20. sql := ` SELECT * FROM mb_index_main_data WHERE 1=1 AND DATA_VALUE is not null `
  21. o := orm.NewOrmUsingDB("data")
  22. if condition != "" {
  23. sql += condition
  24. }
  25. sql += ` ORDER BY PUBLISH_TIME DESC `
  26. _, err = o.Raw(sql, pars).QueryRows(&item)
  27. return
  28. }
  29. func GetEdbDataGlByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  30. o := orm.NewOrmUsingDB("data")
  31. sql := ` SELECT COUNT(1) AS count FROM edb_data_gl WHERE edb_code=? AND data_time=? `
  32. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  33. return
  34. }
  35. func ModifyEdbDataGl(edbInfoId int64, dataTime, value string) (err error) {
  36. o := orm.NewOrmUsingDB("data")
  37. sql := ` UPDATE edb_data_gl SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  38. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  39. return
  40. }
  41. //刷新钢联指标数据
  42. func RefreshEdbDataByGl(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  43. o := orm.NewOrmUsingDB("data")
  44. tx,err:=o.Begin()
  45. if err!=nil {
  46. return err
  47. }
  48. defer func() {
  49. if err != nil {
  50. tx.Rollback()
  51. } else {
  52. tx.Commit()
  53. }
  54. }()
  55. if err != nil {
  56. return
  57. }
  58. edbInfoIdStr := strconv.Itoa(edbInfoId)
  59. //计算数据
  60. var condition string
  61. var pars []interface{}
  62. if edbCode != "" {
  63. condition += " AND INDEX_CODE=? "
  64. pars = append(pars, edbCode)
  65. }
  66. if startDate != "" {
  67. condition += " AND DATA_DATE>=? "
  68. pars = append(pars, startDate)
  69. }
  70. if endDate != "" {
  71. condition += " AND DATA_DATE<=? "
  72. pars = append(pars, endDate)
  73. }
  74. condition += " AND IS_DELETE=0 "
  75. //获取已存在指标所有数据
  76. existDataList := make([]*EdbDataBase, 0)
  77. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_GL)
  78. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  79. sql = fmt.Sprintf(sql, dataTableName)
  80. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  81. if err != nil {
  82. return err
  83. }
  84. existDataMap := make(map[string]string)
  85. for _, v := range existDataList {
  86. existDataMap[v.DataTime] = v.Value
  87. }
  88. glDataList, err := GetGlDataByTradeCode(condition, pars)
  89. addSql := ` INSERT INTO edb_data_gl(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  90. var isAdd bool
  91. addExistMap := make(map[string]string)
  92. for _, v := range glDataList {
  93. item := v
  94. if _, ok := addExistMap[v.DataTime]; !ok {
  95. if existVal, existOk := existDataMap[v.DataTime]; !existOk {
  96. eDate := item.DataTime
  97. sValue := item.InputValue
  98. if sValue != "" {
  99. dataTime, err := time.Parse(utils.FormatDate, eDate)
  100. if err != nil {
  101. return err
  102. }
  103. timestamp := dataTime.UnixNano() / 1e6
  104. timeStr := fmt.Sprintf("%d", timestamp)
  105. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  106. isAdd = true
  107. }
  108. } else {
  109. if existVal != item.InputValue {
  110. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  111. sql = fmt.Sprintf(sql, dataTableName)
  112. _, err = tx.Raw(sql, item.InputValue, edbInfoId, v.DataTime).Exec()
  113. if err != nil {
  114. return err
  115. }
  116. }
  117. }
  118. }
  119. addExistMap[v.DataTime] = v.InputValue
  120. }
  121. if isAdd {
  122. addSql = strings.TrimRight(addSql, ",")
  123. _, err = tx.Raw(addSql).Exec()
  124. if err != nil {
  125. return err
  126. }
  127. }
  128. return
  129. }