edb_data_manual.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package data_manage
  2. import (
  3. "eta/eta_task/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func AddEdbDataManualBySql(sqlStr string) (err error) {
  11. o := orm.NewOrm()
  12. _, err = o.Raw(sqlStr).Exec()
  13. return
  14. }
  15. func GetEdbDataManualMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
  16. o := orm.NewOrm()
  17. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_manual WHERE edb_code=? `
  18. err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
  19. return
  20. }
  21. type ManualEdbdata struct {
  22. TradeCode string `orm:"column(TRADE_CODE);pk" description:"指标编码"`
  23. Dt string `orm:"column(DT)" description:"日期"`
  24. Close float64 `orm:"column(CLOSE)" description:"值"`
  25. ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`
  26. }
  27. func GetEdbdataManualByTradeCode(condition string, pars []interface{}) (item []*ManualEdbdata, err error) {
  28. sql := ` SELECT * FROM edbdata WHERE 1=1 `
  29. o := orm.NewOrm()
  30. if condition != "" {
  31. sql += condition
  32. }
  33. sql += ` ORDER BY DT DESC `
  34. _, err = o.Raw(sql, pars).QueryRows(&item)
  35. return
  36. }
  37. func GetEdbDataManualByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  38. o := orm.NewOrm()
  39. sql := ` SELECT COUNT(1) AS count FROM edb_data_manual WHERE edb_code=? AND data_time=? `
  40. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  41. return
  42. }
  43. func ModifyEdbDataManual(edbInfoId int64, dataTime, value string) (err error) {
  44. o := orm.NewOrm()
  45. sql := ` UPDATE edb_data_manual SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  46. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  47. return
  48. }
  49. // 刷新手工指标数据
  50. func RefreshManual(item *EdbInfoList) (err error) {
  51. //edbInfoId int, edbCode, startDate, endDate string
  52. o := orm.NewOrm()
  53. tx, err := o.Begin()
  54. if err != nil {
  55. return err
  56. }
  57. defer func() {
  58. if err != nil {
  59. tx.Rollback()
  60. } else {
  61. tx.Commit()
  62. }
  63. }()
  64. edbInfoId := item.EdbInfoId
  65. edbCode := item.EdbCode
  66. if err != nil {
  67. return
  68. }
  69. edbInfoIdStr := strconv.Itoa(edbInfoId)
  70. //计算数据
  71. var condition string
  72. var pars []interface{}
  73. if edbCode != "" {
  74. condition += " AND TRADE_CODE=? "
  75. pars = append(pars, edbCode)
  76. }
  77. //获取已存在的手工数据
  78. manualDataList, err := GetEdbdataManualByTradeCode(condition, pars)
  79. var existCondition string
  80. var existPars []interface{}
  81. if edbCode != "" {
  82. existCondition += " AND edb_code=? "
  83. existPars = append(existPars, edbCode)
  84. }
  85. //获取ETA指标库已存在的所有数据
  86. existDataList, err := GetEdbDataListAll(existCondition, existPars, utils.DATA_SOURCE_MANUAL, utils.DATA_SUB_SOURCE_EDB, 0)
  87. if err != nil {
  88. return err
  89. }
  90. existMap := make(map[string]*EdbInfoSearchData)
  91. for _, v := range existDataList {
  92. existMap[v.DataTime] = v
  93. }
  94. addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  95. var isAdd bool
  96. manualMap := make(map[string]*ManualEdbdata)
  97. for _, v := range manualDataList {
  98. item := v
  99. eDate := item.Dt
  100. sValue := utils.SubFloatToString(item.Close, 4)
  101. if findItem, ok := existMap[v.Dt]; !ok {
  102. nowStr := time.Now().Format(utils.FormatDateTime)
  103. dataTime, err := time.Parse(utils.FormatDate, eDate)
  104. if err != nil {
  105. return err
  106. }
  107. timestamp := dataTime.UnixNano() / 1e6
  108. timeStr := fmt.Sprintf("%d", timestamp)
  109. addSql += "("
  110. addSql += edbInfoIdStr + "," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" +
  111. "," + "'" + nowStr + "'" + "," + "1" + "," + "'" + timeStr + "'"
  112. addSql += "),"
  113. isAdd = true
  114. } else {
  115. if item.Close != findItem.Value {
  116. err = ModifyEdbDataManual(int64(edbInfoId), eDate, sValue)
  117. if err != nil {
  118. return err
  119. }
  120. }
  121. }
  122. manualMap[v.Dt] = v
  123. }
  124. for _, v := range existDataList {
  125. if _, ok := manualMap[v.DataTime]; !ok {
  126. go DeleteEdbDataByIdAndSource(v.EdbDataId, utils.DATA_SOURCE_MANUAL, utils.DATA_SUB_SOURCE_EDB)
  127. }
  128. }
  129. if isAdd {
  130. addSql = strings.TrimRight(addSql, ",")
  131. _, err = tx.Raw(addSql).Exec()
  132. if err != nil {
  133. return err
  134. }
  135. }
  136. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(utils.DATA_SOURCE_MANUAL, utils.DATA_SUB_SOURCE_EDB, edbCode)
  137. if err != nil {
  138. if err.Error() == utils.ErrNoRow() {
  139. err = nil
  140. } else {
  141. return err
  142. }
  143. } else {
  144. if maxAndMinItem != nil {
  145. err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  146. if err != nil {
  147. return err
  148. }
  149. }
  150. }
  151. return
  152. }