edb_data_gl.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. condition += " AND IS_DELETE=0 "
  27. sql1 := ` SELECT * FROM mb_index_main_data WHERE 1=1 AND DATA_VALUE is not null `
  28. o := orm.NewOrmUsingDB("gl")
  29. if condition != "" {
  30. sql1 += condition
  31. }
  32. sql := `select * from (` + sql1 + ` having 1 order by PUBLISH_TIME DESC ) tmp group by DATA_DATE ORDER BY PUBLISH_TIME DESC `
  33. _, err = o.Raw(sql, pars).QueryRows(&item)
  34. return
  35. }
  36. func GetEdbDataGlByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  37. o := orm.NewOrmUsingDB("data")
  38. sql := ` SELECT COUNT(1) AS count FROM edb_data_gl WHERE edb_code=? AND data_time=? `
  39. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  40. return
  41. }
  42. func ModifyEdbDataGl(edbInfoId int64, dataTime, value string) (err error) {
  43. o := orm.NewOrmUsingDB("data")
  44. sql := ` UPDATE edb_data_gl SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  45. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  46. return
  47. }
  48. //刷新钢联指标数据
  49. func RefreshEdbDataByGl(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  50. o := orm.NewOrmUsingDB("data")
  51. tx, err := o.Begin()
  52. if err != nil {
  53. return err
  54. }
  55. defer func() {
  56. if err != nil {
  57. tx.Rollback()
  58. } else {
  59. tx.Commit()
  60. }
  61. }()
  62. if err != nil {
  63. return
  64. }
  65. edbInfoIdStr := strconv.Itoa(edbInfoId)
  66. //计算数据
  67. var condition string
  68. var pars []interface{}
  69. if edbCode != "" {
  70. condition += " AND INDEX_CODE=? "
  71. pars = append(pars, edbCode)
  72. }
  73. if startDate != "" {
  74. condition += " AND DATA_DATE>=? "
  75. pars = append(pars, startDate)
  76. }
  77. if endDate != "" {
  78. condition += " AND DATA_DATE<=? "
  79. pars = append(pars, endDate)
  80. }
  81. condition += " AND IS_DELETE=0 "
  82. //获取已存在指标所有数据
  83. existDataList := make([]*EdbDataBase, 0)
  84. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_GL)
  85. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  86. sql = fmt.Sprintf(sql, dataTableName)
  87. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  88. if err != nil {
  89. return err
  90. }
  91. existDataMap := make(map[string]string)
  92. for _, v := range existDataList {
  93. existDataMap[v.DataTime] = v.Value
  94. }
  95. glDataList, err := GetGlDataByTradeCode(condition, pars)
  96. addSql := ` INSERT INTO edb_data_gl(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  97. var isAdd bool
  98. addExistMap := make(map[string]string)
  99. for _, v := range glDataList {
  100. item := v
  101. if _, ok := addExistMap[v.DataTime]; !ok {
  102. if existVal, existOk := existDataMap[v.DataTime]; !existOk {
  103. eDate := item.DataTime
  104. sValue := item.InputValue
  105. if sValue != "" {
  106. dataTime, err := time.Parse(utils.FormatDate, eDate)
  107. if err != nil {
  108. return err
  109. }
  110. timestamp := dataTime.UnixNano() / 1e6
  111. timeStr := fmt.Sprintf("%d", timestamp)
  112. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  113. isAdd = true
  114. }
  115. } else {
  116. if existVal != item.InputValue {
  117. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  118. sql = fmt.Sprintf(sql, dataTableName)
  119. _, err = tx.Raw(sql, item.InputValue, edbInfoId, v.DataTime).Exec()
  120. if err != nil {
  121. return err
  122. }
  123. }
  124. }
  125. }
  126. addExistMap[v.DataTime] = v.InputValue
  127. }
  128. if isAdd {
  129. addSql = strings.TrimRight(addSql, ",")
  130. _, err = tx.Raw(addSql).Exec()
  131. if err != nil {
  132. return err
  133. }
  134. }
  135. return
  136. }