base_from_trade_guangzhou.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package data_manage
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_task/global"
  5. "eta/eta_task/utils"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. type BaseFromTradeGuangzhouIndex struct {
  11. BaseFromTradeGuangzhouIndexId int `gorm:"column:base_from_trade_guangzhou_index_id;primaryKey;autoIncrement"`
  12. BaseFromTradeGuangzhouClassifyId int `description:"分类id"`
  13. BaseFromTradeGuangzhouContractId int `description:"合约id"`
  14. IndexCode string `description:"指标编码"`
  15. IndexName string `description:"指标名称"`
  16. Frequency string `description:"频率"`
  17. Unit string `description:"单位"`
  18. StartDate string `description:"开始日期"`
  19. EndDate string `description:"结束日期"`
  20. CreateTime time.Time `description:"创建日期"`
  21. ModifyTime time.Time `description:"修改日期"`
  22. Value float64 `description:"数据值"`
  23. }
  24. func (m *BaseFromTradeGuangzhouIndex) AfterFind(db *gorm.DB) (err error) {
  25. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  26. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  27. return
  28. }
  29. func AddBaseFromTradeGuangzhouIndex(item *BaseFromTradeGuangzhouIndex) (lastId int64, err error) {
  30. saveItem := *item
  31. o := global.DbMap[utils.DbNameIndex]
  32. //lastId, err = o.InsertOrUpdate(item)
  33. result := o.FirstOrCreate(item, "base_from_trade_guangzhou_index_id = ?", item.BaseFromTradeGuangzhouIndexId)
  34. if result.Error != nil {
  35. err = result.Error
  36. return
  37. }
  38. if result.RowsAffected == 0 {
  39. saveItem.BaseFromTradeGuangzhouIndexId = 0
  40. result = o.Model(item).Where("base_from_trade_guangzhou_index_id = ?", item.BaseFromTradeGuangzhouIndexId).Updates(saveItem)
  41. if result.Error != nil {
  42. err = result.Error
  43. return
  44. }
  45. }
  46. return int64(item.BaseFromTradeGuangzhouIndexId), nil
  47. }
  48. func GetBaseFromTradeGuangzhouIndexAll() (list []*BaseFromTradeGuangzhouIndex, err error) {
  49. o := global.DbMap[utils.DbNameIndex]
  50. sql := `SELECT * FROM base_from_trade_guangzhou_index `
  51. err = o.Raw(sql).Find(&list).Error
  52. return
  53. }
  54. type GuangzhouIndexResp struct {
  55. Ret int
  56. Msg string
  57. ErrMsg string
  58. ErrCode string
  59. Data []*BaseFromTradeGuangzhouIndex
  60. }
  61. type BaseFromTradeGuangzhouClassify struct {
  62. BaseFromTradeGuangzhouClassifyId int `gorm:"column:base_from_trade_guangzhou_classify_id;primaryKey;autoIncrement"`
  63. ClassifyName string `description:"分类名称"`
  64. ClassifyCode string `description:"分类编码"`
  65. ParentId int `description:"分类父级id"`
  66. ModifyTime time.Time `description:"修改时间"`
  67. CreateTime time.Time `description:"创建时间"`
  68. }
  69. type GuangzhouClassifyResp struct {
  70. Ret int
  71. Msg string
  72. ErrMsg string
  73. ErrCode string
  74. Data []*BaseFromTradeGuangzhouClassify
  75. }
  76. func GetBaseFromTradeGuangzhouClassifyAll() (list []*BaseFromTradeGuangzhouClassify, err error) {
  77. o := global.DbMap[utils.DbNameIndex]
  78. sql := `SELECT * FROM base_from_trade_guangzhou_classify `
  79. err = o.Raw(sql).Find(&list).Error
  80. return
  81. }
  82. func AddBaseFromTradeGuangzhouClassify(item *BaseFromTradeGuangzhouClassify) (lastId int64, err error) {
  83. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  84. if err != nil {
  85. return
  86. }
  87. lastId = int64(item.BaseFromTradeGuangzhouClassifyId)
  88. return
  89. }
  90. type BaseFromTradeGuangzhouContract struct {
  91. BaseFromTradeGuangzhouContractId int `gorm:"column:base_from_trade_guangzhou_contract_id;primaryKey;autoIncrement"`
  92. BaseFromTradeGuangzhouClassifyId int `description:"分类id"`
  93. ClassifyCode string `description:"分类编码"`
  94. Contract string `description:"合约编码"`
  95. TradeDate string `description:"合约日期"`
  96. }
  97. func (obj *BaseFromTradeGuangzhouIndex) AddBaseFromTradeGuangzhouContract(item *BaseFromTradeGuangzhouContract) (err error) {
  98. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  99. return
  100. }
  101. type GuangzhouContractResp struct {
  102. Ret int
  103. Msg string
  104. ErrMsg string
  105. ErrCode string
  106. Data []*BaseFromTradeGuangzhouContract
  107. }
  108. func GetBaseFromTradeGuangzhouContractAll() (list []*BaseFromTradeGuangzhouContract, err error) {
  109. o := global.DbMap[utils.DbNameIndex]
  110. sql := `SELECT * FROM base_from_trade_guangzhou_contract `
  111. err = o.Raw(sql).Find(&list).Error
  112. return
  113. }
  114. // GetBaseFromTradeGuangzhouMaxDate 获取广州期货交易所最大数据
  115. func GetBaseFromTradeGuangzhouMaxDate() (maxDate time.Time, err error) {
  116. o := global.DbMap[utils.DbNameIndex]
  117. sql := ` SELECT max(a.create_time) AS max_date FROM base_from_trade_guangzhou_data as a `
  118. var timeNull sql2.NullTime
  119. err = o.Raw(sql).Scan(&timeNull).Error
  120. if err != nil {
  121. return
  122. }
  123. if timeNull.Valid {
  124. maxDate = timeNull.Time
  125. }
  126. return
  127. }
  128. type BaseFromTradeGuangzhouData struct {
  129. BaseFromTradeGuangzhouDataId int `gorm:"column:base_from_trade_guangzhou_data_id;primaryKey;autoIncrement"`
  130. BaseFromTradeGuangzhouIndexId int `description:"指标id"`
  131. IndexCode string `description:"指标编码"`
  132. DataTime string `description:"数据日期"`
  133. Value float64 `description:"数据值"`
  134. QtySub float64 `description:"增减"`
  135. CreateTime time.Time `description:"创建日期"`
  136. ModifyTime time.Time `description:"修改日期"`
  137. }
  138. // GetAllBaseFromTradeGuangzhouDataList 获取广州期货交易所数据
  139. func GetAllBaseFromTradeGuangzhouDataList(startDate string) (list []*BaseFromTradeGuangzhouData, err error) {
  140. o := global.DbMap[utils.DbNameIndex]
  141. sql := `SELECT * FROM base_from_trade_guangzhou_data WHERE create_time>=? ORDER BY base_from_trade_guangzhou_data_id ASC `
  142. err = o.Raw(sql, startDate).Find(&list).Error
  143. return
  144. }
  145. type BaseFromTradeGuangzhouDataResp struct {
  146. Ret int
  147. Msg string
  148. ErrMsg string
  149. ErrCode string
  150. Data BaseFromTradeGuangzhouIndexAndDataResp
  151. }
  152. // BaseFromTradeGuangzhouIndexAndDataResp 分页列表响应体
  153. type BaseFromTradeGuangzhouIndexAndDataResp struct {
  154. List []*BaseFromTradeGuangzhouData
  155. Paging *paging.PagingItem `description:"分页数据"`
  156. }
  157. // MultiAddBaseFromTradeGuangzhouData 批量添加数据
  158. func MultiAddBaseFromTradeGuangzhouData(items []*BaseFromTradeGuangzhouData) (lastId int64, err error) {
  159. num := len(items)
  160. if num <= 0 {
  161. return
  162. }
  163. o := global.DbMap[utils.DbNameIndex]
  164. err = o.CreateInBatches(items, utils.MultiAddNum).Error
  165. return
  166. }