edb_data_dl.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. type DlData struct {
  11. InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
  12. DataTime string `orm:"column(DATA_DATE)" description:"值"`
  13. }
  14. type BaseFromDlDataSimple struct {
  15. Id int `orm:"column(base_from_trade_dalian_index_id);pk"`
  16. DealCode string
  17. BuyCode string
  18. SoldCode string
  19. DataTime string
  20. DealValue string
  21. BuyValue string
  22. SoldValue string
  23. }
  24. type BaseInfoFromDl struct {
  25. DealName string
  26. BuyName string
  27. SoldName string
  28. }
  29. func GetEdbDataDlMaxOrMinDate(edbCode string) (minDate, maxDate string, err error) {
  30. o := orm.NewOrm()
  31. sql := ` SELECT MIN(data_time) AS minDate,MAX(data_time) AS maxDate FROM edb_data_dl WHERE edb_code=? `
  32. err = o.Raw(sql, edbCode).QueryRow(&minDate, &maxDate)
  33. return
  34. }
  35. // RefreshEdbDataByDl 刷新大商所指标数据
  36. func RefreshEdbDataByDl(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  37. o := orm.NewOrm()
  38. tx, err := o.Begin()
  39. if err != nil {
  40. return err
  41. }
  42. defer func() {
  43. if err != nil {
  44. tx.Rollback()
  45. } else {
  46. tx.Commit()
  47. }
  48. }()
  49. if err != nil {
  50. return
  51. }
  52. var suffix string
  53. if strings.Contains(edbCode, "deal") {
  54. suffix = "deal"
  55. } else if strings.Contains(edbCode, "buy") {
  56. suffix = "buy"
  57. } else if strings.Contains(edbCode, "sold") {
  58. suffix = "sold"
  59. }
  60. edbInfoIdStr := strconv.Itoa(edbInfoId)
  61. //计算数据
  62. var condition string
  63. var pars []interface{}
  64. if edbCode != "" {
  65. if suffix == "deal" {
  66. condition += " AND deal_code=? "
  67. } else if suffix == "buy" {
  68. condition += " AND buy_code=? "
  69. } else {
  70. condition += " AND sold_code=? "
  71. }
  72. pars = append(pars, edbCode)
  73. }
  74. if startDate != "" {
  75. condition += " AND data_time>=? "
  76. pars = append(pars, startDate)
  77. }
  78. if endDate != "" {
  79. condition += " AND data_time<=? "
  80. pars = append(pars, endDate)
  81. }
  82. glDataList, err := GetDlDataByTradeCode(condition, pars)
  83. addSql := ` INSERT INTO edb_data_dl(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  84. var isAdd bool
  85. existMap := make(map[string]string)
  86. for _, v := range glDataList {
  87. var value string
  88. if suffix == "deal" {
  89. value = v.DealValue
  90. } else if suffix == "buy" {
  91. value = v.BuyValue
  92. } else {
  93. value = v.SoldValue
  94. }
  95. item := v
  96. itemValue := value
  97. if _, ok := existMap[v.DataTime]; !ok {
  98. count, err := GetEdbDataDlByCodeAndDate(edbCode, v.DataTime)
  99. if err != nil && err.Error() != utils.ErrNoRow() {
  100. return err
  101. }
  102. if count <= 0 {
  103. eDate := item.DataTime
  104. sValue := itemValue
  105. if sValue != "" {
  106. dataTime, err := time.Parse(utils.FormatDate, eDate)
  107. if err != nil {
  108. return err
  109. }
  110. timestamp := dataTime.UnixNano() / 1e6
  111. timeStr := fmt.Sprintf("%d", timestamp)
  112. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  113. isAdd = true
  114. }
  115. } else {
  116. err = ModifyEdbDataDl(int64(edbInfoId), v.DataTime, value)
  117. if err != nil {
  118. return err
  119. }
  120. }
  121. }
  122. existMap[v.DataTime] = value
  123. }
  124. if isAdd {
  125. addSql = strings.TrimRight(addSql, ",")
  126. _, err = tx.Raw(addSql).Exec()
  127. if err != nil {
  128. return err
  129. }
  130. }
  131. return
  132. }
  133. // GetBaseInfoFromDalianByIndexCode 获取指标信息
  134. func GetBaseInfoFromDalianByIndexCode(indexCode, suffix string) (list []*BaseInfoFromDl, err error) {
  135. o := orm.NewOrm()
  136. sql := `SELECT * FROM base_from_trade_dalian_index WHERE %s_code=? `
  137. sql = fmt.Sprintf(sql, suffix)
  138. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  139. return
  140. }
  141. func GetDlDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromDlDataSimple, err error) {
  142. sql := ` SELECT * FROM base_from_trade_dalian_index WHERE 1=1 `
  143. o := orm.NewOrm()
  144. if condition != "" {
  145. sql += condition
  146. }
  147. sql += ` ORDER BY data_time DESC `
  148. _, err = o.Raw(sql, pars).QueryRows(&item)
  149. return
  150. }
  151. func AddEdbDataDlBySql(sqlStr string) (err error) {
  152. o := orm.NewOrm()
  153. _, err = o.Raw(sqlStr).Exec()
  154. return
  155. }
  156. func GetBaseFromDalianDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeDalianIndex, err error) {
  157. o := orm.NewOrm()
  158. sql := `SELECT * FROM base_from_trade_dalian_index WHERE %s_code=? `
  159. sql = fmt.Sprintf(sql, suffix)
  160. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  161. return
  162. }
  163. func GetEdbDataDlByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  164. o := orm.NewOrm()
  165. sql := ` SELECT COUNT(1) AS count FROM edb_data_dl WHERE edb_code=? AND data_time=? `
  166. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  167. return
  168. }
  169. func ModifyEdbDataDl(edbInfoId int64, dataTime, value string) (err error) {
  170. o := orm.NewOrm()
  171. sql := ` UPDATE edb_data_dl SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  172. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  173. return
  174. }