edb_data_gl.go 4.2 KB

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