edb_data_gl.go 3.6 KB

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