edb_data_gl.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/rdlucklib/rdluck_tools/orm"
  5. "hongze/hongze_task/utils"
  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 AND DATA_VALUE is not null `
  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. condition += " AND IS_DELETE=0 "
  77. //获取已存在指标所有数据
  78. existDataList := make([]*EdbDataBase, 0)
  79. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_GL)
  80. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  81. sql = fmt.Sprintf(sql, dataTableName)
  82. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  83. if err != nil {
  84. return err
  85. }
  86. existDataMap := make(map[string]string)
  87. for _, v := range existDataList {
  88. existDataMap[v.DataTime] = v.Value
  89. }
  90. glDataList, err := GetGlDataByTradeCode(condition, pars)
  91. addSql := ` INSERT INTO edb_data_gl(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  92. var isAdd bool
  93. addExistMap := make(map[string]string)
  94. for _, v := range glDataList {
  95. item := v
  96. if _, ok := addExistMap[v.DataTime]; !ok {
  97. if existVal, existOk := existDataMap[v.DataTime]; !existOk {
  98. eDate := item.DataTime
  99. sValue := item.InputValue
  100. if sValue != "" {
  101. dataTime, err := time.Parse(utils.FormatDate, eDate)
  102. if err != nil {
  103. return err
  104. }
  105. timestamp := dataTime.UnixNano() / 1e6
  106. timeStr := fmt.Sprintf("%d", timestamp)
  107. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  108. isAdd = true
  109. }
  110. } else {
  111. if existVal != item.InputValue {
  112. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  113. sql = fmt.Sprintf(sql, dataTableName)
  114. _, err = o.Raw(sql, item.InputValue, edbInfoId, v.DataTime).Exec()
  115. if err != nil {
  116. return err
  117. }
  118. }
  119. }
  120. }
  121. addExistMap[v.DataTime] = v.InputValue
  122. }
  123. if isAdd {
  124. addSql = strings.TrimRight(addSql, ",")
  125. _, err = o.Raw(addSql).Exec()
  126. if err != nil {
  127. return err
  128. }
  129. }
  130. return
  131. }