future_good_edb_info.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package future_good
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/utils"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. type FutureGoodEdbInfo struct {
  9. FutureGoodEdbInfoId int `orm:"column(future_good_edb_info_id);pk"`
  10. FutureGoodEdbCode string `description:"期货指标code"`
  11. FutureGoodEdbName string `description:"期货指标名称"`
  12. FutureGoodEdbNameEn string `description:"期货指标英文名称"`
  13. ParentId int `description:"上级期货id"`
  14. RegionType string `description:"交易所来源,海外还是国内"`
  15. Exchange string `description:"所属交易所"`
  16. FutureGoodEdbType int `description:"指标类型,1:年月是固定的合约;2:只有M+N期的合约,未固定年月"`
  17. DateSourceId int `description:"画图时,日期来源的指标id"`
  18. Year int `description:"所属年份"`
  19. Month int `description:"所属月份"`
  20. StartDate string `description:"起始日期"`
  21. EndDate string `description:"终止日期"`
  22. MinValue float64 `description:"最小值"`
  23. MaxValue float64 `description:"最大值"`
  24. LatestValue float64 `description:"数据最新的值"`
  25. LatestDate time.Time `description:"数据最新的日期"`
  26. ServerUrl string `description:"服务器地址"`
  27. TerminalCode string `description:"终端编码,用于配置在机器上"`
  28. CreateTime time.Time
  29. ModifyTime time.Time
  30. }
  31. // GetFutureGoodEdbInfo 期货指标
  32. func GetFutureGoodEdbInfo(edbInfoId int) (item *FutureGoodEdbInfo, err error) {
  33. o := orm.NewOrm()
  34. sql := `SELECT * FROM future_good_edb_info WHERE future_good_edb_info_id = ? `
  35. sql += ` ORDER BY future_good_edb_info_id DESC `
  36. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  37. return
  38. }
  39. // GetFutureGoodEdbInfoByCode 根据指标code获取指标信息
  40. func GetFutureGoodEdbInfoByCode(edbCode string) (item *FutureGoodEdbInfo, err error) {
  41. o := orm.NewOrm()
  42. sql := `SELECT * FROM future_good_edb_info WHERE future_good_edb_code = ? limit 1 `
  43. err = o.Raw(sql, edbCode).QueryRow(&item)
  44. return
  45. }
  46. // GetFutureGoodEdbInfoList 获取指标数据列表
  47. func GetFutureGoodEdbInfoList(condition string, pars []interface{}) (list []*FutureGoodEdbInfo, err error) {
  48. o := orm.NewOrm()
  49. sql := `SELECT * FROM future_good_edb_info WHERE 1=1 `
  50. if condition != "" {
  51. sql += condition
  52. }
  53. sql += `ORDER BY future_good_edb_info_id DESC `
  54. _, err = o.Raw(sql, pars).QueryRows(&list)
  55. return
  56. }
  57. // GetAllFutureGoodEdbInfoList 获取指标数据列表
  58. func GetAllFutureGoodEdbInfoList() (list []*FutureGoodEdbInfo, err error) {
  59. o := orm.NewOrm()
  60. sql := `SELECT * FROM future_good_edb_info ORDER BY future_good_edb_info_id DESC `
  61. _, err = o.Raw(sql).QueryRows(&list)
  62. return
  63. }
  64. // AddFutureGoodEdbInfo 添加期货数据库指标
  65. func AddFutureGoodEdbInfo(item *FutureGoodEdbInfo) (err error) {
  66. o := orm.NewOrm()
  67. lastId, err := o.Insert(item)
  68. if err != nil {
  69. return
  70. }
  71. item.FutureGoodEdbInfoId = int(lastId)
  72. return
  73. }
  74. // GetChildFutureGoodEdbInfoListByParentId 根据父级ID获取下面所有的指标数据列表
  75. func GetChildFutureGoodEdbInfoListByParentId(parentId int) (list []*FutureGoodEdbInfo, err error) {
  76. o := orm.NewOrm()
  77. sql := `SELECT * FROM future_good_edb_info WHERE parent_id = ? ORDER BY future_good_edb_info_id ASC `
  78. _, err = o.Raw(sql, parentId).QueryRows(&list)
  79. return
  80. }
  81. // Update 更新指标基础信息
  82. func (FutureGoodEdbInfo *FutureGoodEdbInfo) Update(cols []string) (err error) {
  83. o := orm.NewOrm()
  84. _, err = o.Update(FutureGoodEdbInfo, cols...)
  85. return
  86. }
  87. // UnifiedModifyEdbInfoMaxAndMinInfo 统一修改指标的最大最小值
  88. func UnifiedModifyEdbInfoMaxAndMinInfo(futureGoodEdbInfo *FutureGoodEdbInfo) (err error, errMsg string) {
  89. // 修改最大最小值
  90. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(futureGoodEdbInfo.FutureGoodEdbCode)
  91. if err != nil {
  92. if err.Error() == utils.ErrNoRow() {
  93. err = nil
  94. return
  95. }
  96. errMsg = "刷新期货指标失败!"
  97. err = errors.New("获取期货指标最大最小值失败,err:" + err.Error())
  98. return
  99. }
  100. if maxAndMinItem != nil {
  101. o := orm.NewOrm()
  102. sql := ` UPDATE future_good_edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,latest_date=?,latest_value=?,modify_time=NOW() WHERE future_good_edb_info_id=? `
  103. _, err = o.Raw(sql, maxAndMinItem.MinDate, maxAndMinItem.MaxDate, maxAndMinItem.MinValue, maxAndMinItem.MaxValue, maxAndMinItem.MaxDate, maxAndMinItem.LatestValue, futureGoodEdbInfo.FutureGoodEdbInfoId).Exec()
  104. if err != nil {
  105. errMsg = "刷新指标失败!"
  106. err = errors.New("修改指标最大最小值失败,err:" + err.Error())
  107. return
  108. }
  109. }
  110. return
  111. }
  112. // FutureGoodEdbInfoMaxAndMinInfo 指标最新数据记录结构体
  113. type FutureGoodEdbInfoMaxAndMinInfo struct {
  114. MinDate string `description:"最小日期"`
  115. MaxDate string `description:"最大日期"`
  116. MinValue float64 `description:"最小值"`
  117. MaxValue float64 `description:"最大值"`
  118. LatestValue float64 `description:"最新值"`
  119. LatestDate string `description:"实际数据最新日期"`
  120. }
  121. // GetEdbInfoMaxAndMinInfo 获取指标的最新数据记录信息
  122. func GetEdbInfoMaxAndMinInfo(futureGoodEdbCode string) (item *FutureGoodEdbInfoMaxAndMinInfo, err error) {
  123. o := orm.NewOrm()
  124. sql := ``
  125. sql = ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(close) AS min_value,MAX(close) AS max_value FROM future_good_edb_data WHERE future_good_edb_code=? `
  126. err = o.Raw(sql, futureGoodEdbCode).QueryRow(&item)
  127. var latest_value float64
  128. sql = ` SELECT close AS latest_value FROM future_good_edb_data WHERE future_good_edb_code=? ORDER BY data_time DESC LIMIT 1 `
  129. err = o.Raw(sql, futureGoodEdbCode).QueryRow(&latest_value)
  130. item.LatestValue = latest_value
  131. return
  132. }