trade_classify.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package trade_analysis
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strings"
  6. "time"
  7. )
  8. // BaseFromTradeClassify 交易所分类表
  9. type BaseFromTradeClassify struct {
  10. Id uint64 `orm:"column(id);pk"`
  11. ClassifyName string //分类名称
  12. ClassifyType string //分类名称下的类型
  13. Exchange string //交易所
  14. LatestDate time.Time //数据最近的日期
  15. CreateTime time.Time //插入时间
  16. ModifyTime time.Time //修改时间
  17. }
  18. // GetAllBaseFromTradeClassify 获取所有的交易所分类列表
  19. func GetAllBaseFromTradeClassify() (list []*BaseFromTradeClassify, err error) {
  20. sql := `SELECT * FROM base_from_trade_classify `
  21. o := orm.NewOrmUsingDB("data")
  22. _, err = o.Raw(sql).QueryRows(&list)
  23. return
  24. }
  25. func GetTradeTopLastDataTime(exchange string, classifyName, classifyType string) (item *BaseFromTradeClassify, err error) {
  26. sql := `SELECT * FROM base_from_trade_classify WHERE exchange = ? AND classify_name=? `
  27. pars := make([]interface{}, 0)
  28. pars = append(pars, exchange)
  29. if exchange == "zhengzhou" {
  30. pars = append(pars, classifyType)
  31. } else {
  32. pars = append(pars, classifyName)
  33. sql += ` AND classify_type=? `
  34. pars = append(pars, classifyType)
  35. }
  36. sql += ` ORDER BY latest_date desc`
  37. o := orm.NewOrmUsingDB("data")
  38. err = o.Raw(sql, pars...).QueryRow(&item)
  39. return
  40. }
  41. // GetClassifyTypeByClassifyName 根据分类名称获取分类类型
  42. func GetClassifyTypeByClassifyName(exchange, classifyName string) (item *TradeClassifyName, err error) {
  43. sql := `SELECT classify_name, classify_type FROM base_from_trade_classify WHERE exchange = ? AND classify_name=? `
  44. o := orm.NewOrmUsingDB("data")
  45. err = o.Raw(sql, exchange, classifyName).QueryRow(&item)
  46. return
  47. }
  48. func (m *BaseFromTradeClassify) TableName() string {
  49. return "base_from_trade_classify"
  50. }
  51. type BaseFromTradeClassifyCols struct {
  52. PrimaryId string
  53. Exchange string
  54. ClassifyName string
  55. ClassifyType string
  56. LatestDate string
  57. CreateTime string
  58. ModifyTime string
  59. }
  60. func (m *BaseFromTradeClassify) Cols() BaseFromTradeClassifyCols {
  61. return BaseFromTradeClassifyCols{
  62. PrimaryId: "id",
  63. Exchange: "exchange",
  64. ClassifyName: "classify_name",
  65. ClassifyType: "classify_type",
  66. LatestDate: "latest_date",
  67. CreateTime: "create_time",
  68. ModifyTime: "modify_time",
  69. }
  70. }
  71. func (m *BaseFromTradeClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromTradeClassify, err error) {
  72. o := orm.NewOrmUsingDB("data")
  73. fields := strings.Join(fieldArr, ",")
  74. if len(fieldArr) == 0 {
  75. fields = `*`
  76. }
  77. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  78. if orderRule != "" {
  79. order = ` ORDER BY ` + orderRule
  80. }
  81. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  82. _, err = o.Raw(sql, pars).QueryRows(&items)
  83. return
  84. }
  85. // GetClassifyItemsByCondition 获取品种信息
  86. func (m *BaseFromTradeClassify) GetClassifyItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromTradeClassify, err error) {
  87. o := orm.NewOrmUsingDB("data")
  88. fields := strings.Join(fieldArr, ",")
  89. if len(fieldArr) == 0 {
  90. fields = `*`
  91. }
  92. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  93. if orderRule != "" {
  94. order = ` ORDER BY ` + orderRule
  95. }
  96. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s GROUP BY %s %s`, fields, m.TableName(), condition, m.Cols().ClassifyName, order)
  97. _, err = o.Raw(sql, pars).QueryRows(&items)
  98. return
  99. }
  100. // BaseFromTradeClassifyItem 交易所品种信息
  101. type BaseFromTradeClassifyItem struct {
  102. ClassifyName string `description:"品种"`
  103. Exchange string `description:"交易所标识"`
  104. ExchangeName string `description:"交易所名称"`
  105. }
  106. // BaseFromTradeContractItem 交易所合约信息
  107. type BaseFromTradeContractItem struct {
  108. ClassifyName string `description:"品种"`
  109. Exchange string `description:"交易所标识"`
  110. ExchangeName string `description:"交易所名称"`
  111. ClassifyType string `description:"合约"`
  112. LatestDate string `description:"最近数据的日期"`
  113. CreateTime string `description:"创建时间"`
  114. }