trade_futures_company.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package trade_analysis
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strings"
  6. "time"
  7. )
  8. const TradeFuturesCompanyTop20 = "TOP20"
  9. // TradeFuturesCompany 期货公司表
  10. type TradeFuturesCompany struct {
  11. TradeFuturesCompanyId int `orm:"column(trade_futures_company_id);pk"`
  12. CompanyName string `description:"标准公司名称"`
  13. ZhengzhouName string `description:"郑商所下的名称"`
  14. DalianName string `description:"大商所下的名称"`
  15. ShanghaiName string `description:"上期所下的名称"`
  16. IneName string `description:"上期能源下的名称"`
  17. GuangzhouName string `description:"广期所下的名称"`
  18. CffexName string `description:"中金所下的名称"`
  19. Sort int `description:"排序"`
  20. CreateTime time.Time `description:"创建时间"`
  21. ModifyTime time.Time `description:"修改时间"`
  22. }
  23. func (m *TradeFuturesCompany) TableName() string {
  24. return "trade_futures_company"
  25. }
  26. type TradeFuturesCompanyCols struct {
  27. PrimaryId string
  28. CompanyName string
  29. ZhengzhouName string
  30. DalianName string
  31. ShanghaiName string
  32. IneName string
  33. GuangzhouName string
  34. CffexName string
  35. Sort string
  36. CreateTime string
  37. ModifyTime string
  38. }
  39. func (m *TradeFuturesCompany) Cols() TradeFuturesCompanyCols {
  40. return TradeFuturesCompanyCols{
  41. PrimaryId: "trade_futures_company_id",
  42. CompanyName: "company_name",
  43. ZhengzhouName: "zhengzhou_name",
  44. DalianName: "dalian_name",
  45. ShanghaiName: "shanghai_name",
  46. IneName: "ine_name",
  47. GuangzhouName: "guangzhou_name",
  48. CffexName: "cffex_name",
  49. Sort: "sort",
  50. CreateTime: "create_time",
  51. ModifyTime: "modify_time",
  52. }
  53. }
  54. func (m *TradeFuturesCompany) GetItemById(id int) (item *TradeFuturesCompany, err error) {
  55. o := orm.NewOrmUsingDB("data")
  56. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  57. err = o.Raw(sql, id).QueryRow(&item)
  58. return
  59. }
  60. func (m *TradeFuturesCompany) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *TradeFuturesCompany, err error) {
  61. o := orm.NewOrmUsingDB("data")
  62. order := ``
  63. if orderRule != "" {
  64. order = ` ORDER BY ` + orderRule
  65. }
  66. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  67. err = o.Raw(sql, pars).QueryRow(&item)
  68. return
  69. }
  70. func (m *TradeFuturesCompany) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  71. o := orm.NewOrmUsingDB("data")
  72. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  73. err = o.Raw(sql, pars).QueryRow(&count)
  74. return
  75. }
  76. func (m *TradeFuturesCompany) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*TradeFuturesCompany, err error) {
  77. o := orm.NewOrmUsingDB("data")
  78. fields := strings.Join(fieldArr, ",")
  79. if len(fieldArr) == 0 {
  80. fields = `*`
  81. }
  82. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  83. if orderRule != "" {
  84. order = ` ORDER BY ` + orderRule
  85. }
  86. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  87. _, err = o.Raw(sql, pars).QueryRows(&items)
  88. return
  89. }
  90. func (m *TradeFuturesCompany) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*TradeFuturesCompany, err error) {
  91. o := orm.NewOrmUsingDB("data")
  92. fields := strings.Join(fieldArr, ",")
  93. if len(fieldArr) == 0 {
  94. fields = `*`
  95. }
  96. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  97. if orderRule != "" {
  98. order = ` ORDER BY ` + orderRule
  99. }
  100. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  101. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  102. return
  103. }
  104. // TradeFuturesCompanyItem 期货公司信息
  105. type TradeFuturesCompanyItem struct {
  106. CompanyId int `description:"期货公司ID"`
  107. CompanyName string `description:"标准公司名称"`
  108. Sort int `description:"排序"`
  109. }
  110. func (m *TradeFuturesCompany) Format2Item() (item *TradeFuturesCompanyItem) {
  111. item = new(TradeFuturesCompanyItem)
  112. item.CompanyId = m.TradeFuturesCompanyId
  113. item.CompanyName = m.CompanyName
  114. item.Sort = m.Sort
  115. return
  116. }