edb_data_gl.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package data_manage
  2. import (
  3. "eta_gn/eta_task/global"
  4. "eta_gn/eta_task/utils"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func AddEdbDataGlBySql(sqlStr string) (err error) {
  11. //o := orm.NewOrm()
  12. //_, err = o.Raw(sqlStr).Exec()
  13. err = global.DEFAULT_DmSQL.Exec(sqlStr).Error
  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. //if condition != "" {
  24. // sql += condition
  25. //}
  26. //sql += ` ORDER BY PUBLISH_TIME DESC `
  27. condition += " AND IS_DELETE=0 "
  28. sql1 := ` SELECT * FROM mb_index_main_data WHERE 1=1 AND DATA_VALUE is not null `
  29. //o := orm.NewOrmUsingDB("gl")
  30. if condition != "" {
  31. sql1 += condition
  32. }
  33. sql := `select * from (` + sql1 + ` having 1 order by PUBLISH_TIME DESC ) tmp group by DATA_DATE ORDER BY PUBLISH_TIME DESC `
  34. //_, err = o.Raw(sql, pars).QueryRows(&item)
  35. err = global.DmSQL["gl"].Raw(sql, pars...).Find(&item).Error
  36. return
  37. }
  38. func GetEdbDataGlByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  39. //o := orm.NewOrm()
  40. sql := ` SELECT COUNT(1) AS count FROM edb_data_gl WHERE edb_code=? AND data_time=? `
  41. //err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  42. err = global.DEFAULT_DmSQL.Raw(sql, edbCode, startDate).Scan(&count).Error
  43. return
  44. }
  45. func ModifyEdbDataGl(edbInfoId int64, dataTime, value string) (err error) {
  46. //o := orm.NewOrm()
  47. sql := ` UPDATE edb_data_gl SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  48. //_, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  49. err = global.DEFAULT_DmSQL.Exec(sql, value, edbInfoId, dataTime).Error
  50. return
  51. }
  52. // 刷新钢联指标数据
  53. func RefreshEdbDataByGl(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  54. //o := orm.NewOrm()
  55. //tx, err := o.Begin()
  56. tx := global.DEFAULT_DmSQL.Begin()
  57. //if err != nil {
  58. // return err
  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).QueryRows(&existDataList)
  93. err = tx.Raw(sql, edbInfoId).Find(&existDataList).Error
  94. if err != nil {
  95. return err
  96. }
  97. existDataMap := make(map[string]string)
  98. for _, v := range existDataList {
  99. existDataMap[v.DataTime] = v.Value
  100. }
  101. glDataList, err := GetGlDataByTradeCode(condition, pars)
  102. addSql := ` INSERT INTO edb_data_gl(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  103. var isAdd bool
  104. addExistMap := make(map[string]string)
  105. for _, v := range glDataList {
  106. item := v
  107. if _, ok := addExistMap[v.DataTime]; !ok {
  108. if existVal, existOk := existDataMap[v.DataTime]; !existOk {
  109. eDate := item.DataTime
  110. sValue := item.InputValue
  111. if sValue != "" {
  112. dataTime, err := time.Parse(utils.FormatDate, eDate)
  113. if err != nil {
  114. return err
  115. }
  116. timestamp := dataTime.UnixNano() / 1e6
  117. timeStr := fmt.Sprintf("%d", timestamp)
  118. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  119. isAdd = true
  120. }
  121. } else {
  122. if existVal != item.InputValue {
  123. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  124. sql = fmt.Sprintf(sql, dataTableName)
  125. //_, err = tx.Raw(sql, item.InputValue, edbInfoId, v.DataTime).Exec()
  126. err = tx.Exec(sql, item.InputValue, edbInfoId, v.DataTime).Error
  127. if err != nil {
  128. return err
  129. }
  130. }
  131. }
  132. }
  133. addExistMap[v.DataTime] = v.InputValue
  134. }
  135. if isAdd {
  136. addSql = strings.TrimRight(addSql, ",")
  137. //_, err = tx.Raw(addSql).Exec()
  138. err = tx.Exec(addSql).Error
  139. if err != nil {
  140. return err
  141. }
  142. }
  143. return
  144. }