edb_data_manual.go 4.9 KB

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