quote.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "mtp2_if/config"
  7. "mtp2_if/db"
  8. "mtp2_if/logger"
  9. "mtp2_if/rediscli"
  10. "mtp2_if/utils"
  11. "strings"
  12. "time"
  13. "github.com/go-viper/mapstructure/v2"
  14. "gopkg.in/mgo.v2/bson"
  15. )
  16. // CycleType 周期类型
  17. type CycleType int
  18. const (
  19. // CycleTypeSecond 周期类型 - 秒
  20. CycleTypeSecond CycleType = 0
  21. // CycleTypeMinutes1 周期类型 - 1分钟
  22. CycleTypeMinutes1 CycleType = 1
  23. // CycleTypeMinutes5 周期类型 - 5分钟
  24. CycleTypeMinutes5 CycleType = 2
  25. // CycleTypeMinutes30 周期类型 - 30分钟
  26. CycleTypeMinutes30 CycleType = 3
  27. // CycleTypeMinutes60 周期类型 - 60分钟
  28. CycleTypeMinutes60 CycleType = 4
  29. // CycleTypeMinutes120 周期类型 - 2小时
  30. CycleTypeMinutes120 CycleType = 120
  31. // CycleTypeMinutes240 周期类型 - 4小时
  32. CycleTypeMinutes240 CycleType = 240
  33. // CycleTypeMinutesDay 周期类型 - 日线
  34. CycleTypeMinutesDay CycleType = 11
  35. // CycleTypeWeek 周期类型 - 周线
  36. CycleTypeWeek CycleType = 12
  37. // CycleTypeMonth 周期类型 - 月线
  38. CycleTypeMonth CycleType = 13
  39. // CycleTypeYear 周期类型 - 年线
  40. CycleTypeYear CycleType = 14
  41. // CycleTypeTik 周期类型 - Tik
  42. CycleTypeTik CycleType = 10
  43. )
  44. // CycleData MongoDB中历史数据模型
  45. type CycleData struct {
  46. ID bson.ObjectId `json:"-" bson:"_id"` // id
  47. GC string `bson:"GC"` // 商品代码
  48. ST int `json:"-" bson:"ST"` // 时间戳
  49. SST string `bson:"SST"` // 时间文本
  50. Open int `bson:"Open"` // 开盘价
  51. High int `bson:"High"` // 最高价
  52. Low int `bson:"Low"` // 最低价
  53. Close int `bson:"Close"` // 收盘价
  54. TV int `bson:"TV"` // 总量
  55. TT int `bson:"TT"` // 总金额
  56. HV int `bson:"HV"` // 持仓量
  57. SP int `bson:"SP"` // 结算价,日线周期(包括)以上才有
  58. FI bool `json:"FI"` // 是否补充数据
  59. }
  60. // TikData MongoDB中Tik数据模型
  61. type TikData struct {
  62. ID bson.ObjectId `json:"-" bson:"_id"` // id
  63. GC string `bson:"GC"` // 商品代码
  64. TD int `bson:"TD"` // 交易日时间戳
  65. AT int `bson:"AT"` // 行情时间戳
  66. SAT string `bson:"SAT"` // 行情时间文本
  67. PE int `bson:"PE"` // 现价
  68. Vol int `bson:"Vol"` // 现量
  69. TT int `bson:"TT"` // 现金额
  70. Bid int `bson:"Bid"` // 买价
  71. BV int `bson:"BV"` // 买量
  72. Ask int `bson:"Ask"` // 卖价
  73. AV int `bson:"AV"` // 卖量
  74. HV int `bson:"HV"` // 持仓量
  75. HI int `bson:"HI"` // 单笔持仓
  76. TDR int `bson:"TDR"` // 交易方向
  77. TK int `bson:"TK"` // 交易类型
  78. OId int `bson:"OId"` // 行情序号
  79. }
  80. // Quoteday 行情盘面
  81. type Quoteday struct {
  82. Id int64 `mapstructure:"" xorm:"pk autoincr BIGINT(20)"`
  83. Exchangedate int64 `mapstructure:"ExchangeDate" xorm:"not null BIGINT(20)"` // 交易日
  84. Goodscode string `mapstructure:"GoodsCode" xorm:"not null unique CHAR(10)"` // 商品代码
  85. Exchangecode int `mapstructure:"ExchangeCode" xorm:"INT(11)"` // 交易所代码
  86. Preclose int64 `mapstructure:"Preclose" xorm:"default 0 BIGINT(20)"` // 昨收
  87. Opentime int64 `mapstructure:"Opentime" xorm:"BIGINT(20)"` // 开盘时间
  88. Opened int64 `mapstructure:"Opened" xorm:"not null default 0 BIGINT(20)"` // 开盘价
  89. Highest int64 `mapstructure:"Highest" xorm:"not null default 0 BIGINT(20)"` // 最高价
  90. Lowest int64 `mapstructure:"Lowest" xorm:"not null default 0 BIGINT(20)"` // 最低价
  91. Lasttime string `mapstructure:"Lasttime" xorm:"VARCHAR(20)"` // 行情时间(只有现价变化行情时间才变化)
  92. Utclasttime int64 `mapstructure:"Utclasttime" xorm:"not null BIGINT(20)"` // utc的行情时间
  93. Last int64 `mapstructure:"Last" xorm:"not null BIGINT(20)"` // 最新价
  94. Lastvolume int64 `mapstructure:"Lastvolume" xorm:"default 0 BIGINT(20)"` // 最新成交量
  95. Lastturnover int64 `mapstructure:"Lastturnover" xorm:"default 0 BIGINT(20)"` // 最新成交金额
  96. Totalbidvolume int64 `mapstructure:"Totalbidvolume" xorm:"default 0 BIGINT(20)"` // 外盘
  97. Totalaskvolume int64 `mapstructure:"Totalaskvolume" xorm:"default 0 BIGINT(20)"` // 内盘
  98. Totalvolume int64 `mapstructure:"Totalvolume" xorm:"default 0 BIGINT(20)"` // 总量
  99. Totalturnover int64 `mapstructure:"Totalturnover" xorm:"default 0 BIGINT(20)"` // 总金额
  100. Bid int64 `mapstructure:"Bid" xorm:"default 0 BIGINT(20)"` // 买1
  101. Bid2 int64 `mapstructure:"Bid2" xorm:"default 0 BIGINT(20)"` // 买2
  102. Bid3 int64 `mapstructure:"Bid3" xorm:"default 0 BIGINT(20)"` // 买3
  103. Bid4 int64 `mapstructure:"Bid4" xorm:"default 0 BIGINT(20)"` // 买4
  104. Bid5 int64 `mapstructure:"Bid5" xorm:"default 0 BIGINT(20)"` // 买5
  105. Bidvolume int64 `mapstructure:"Bidvolume" xorm:"default 0 BIGINT(20)"` // 买量1
  106. Bidvolume2 int64 `mapstructure:"Bidvolume2" xorm:"default 0 BIGINT(20)"` // 买量2
  107. Bidvolume3 int64 `mapstructure:"Bidvolume3" xorm:"default 0 BIGINT(20)"` // 买量3
  108. Bidvolume4 int64 `mapstructure:"Bidvolume4" xorm:"default 0 BIGINT(20)"` // 买量4
  109. Bidvolume5 int64 `mapstructure:"Bidvolume5" xorm:"default 0 BIGINT(20)"` // 买量5
  110. Ask int64 `mapstructure:"Ask" xorm:"default 0 BIGINT(20)"` // 卖1
  111. Ask2 int64 `mapstructure:"Ask2" xorm:"default 0 BIGINT(20)"` // 卖2
  112. Ask3 int64 `mapstructure:"Ask3" xorm:"default 0 BIGINT(20)"` // 卖3
  113. Ask4 int64 `mapstructure:"Ask4" xorm:"default 0 BIGINT(20)"` // 卖4
  114. Ask5 int64 `mapstructure:"Ask5" xorm:"default 0 BIGINT(20)"` // 卖5
  115. Askvolume int64 `mapstructure:"Askvolume" xorm:"default 0 BIGINT(20)"` // 卖量1
  116. Askvolume2 int64 `mapstructure:"Askvolume2" xorm:"default 0 BIGINT(20)"` // 卖量2
  117. Askvolume3 int64 `mapstructure:"Askvolume3" xorm:"default 0 BIGINT(20)"` // 卖量3
  118. Askvolume4 int64 `mapstructure:"Askvolume4" xorm:"default 0 BIGINT(20)"` // 卖量4
  119. Askvolume5 int64 `mapstructure:"Askvolume5" xorm:"default 0 BIGINT(20)"` // 卖量5
  120. Presettle int64 `mapstructure:"Presettle" xorm:"default 0 BIGINT(20)"` // 昨结价
  121. Settle int64 `mapstructure:"Settle" xorm:"default 0 BIGINT(20)"` // 结算价
  122. Preholdvolume int64 `mapstructure:"Preholdvolume" xorm:"default 0 BIGINT(20)"` // 昨持仓
  123. Holdvolume int64 `mapstructure:"Holdvolume" xorm:"default 0 BIGINT(20)"` // 持仓
  124. Averageprice int64 `mapstructure:"Averageprice" xorm:"default 0 BIGINT(20)"` // 均价
  125. Orderid int64 `mapstructure:"Orderid" xorm:"default 0 BIGINT(20)"` // 序号
  126. Limitup int64 `mapstructure:"Limitup" xorm:"default 0 BIGINT(20)"` // 涨停价
  127. Limitdown int64 `mapstructure:"Limitdown" xorm:"default 0 BIGINT(20)"` // 跌停价
  128. Inventory int64 `mapstructure:"Inventory" xorm:"default 0 BIGINT(20)"` // 库存
  129. Holdincrement int64 `mapstructure:"Holdincrement" xorm:"default 0 BIGINT(20)"` // 单笔持仓
  130. Iscleared int `mapstructure:"Iscleared" xorm:"INT(11)"` // 是否清盘标志
  131. Issettled int `mapstructure:"Issettled" xorm:"INT(11)"` // 是否结算标志
  132. Bidqueueinfo string `mapstructure:"Bidqueueinfo" xorm:"VARCHAR(2000)"` // 大利市买港股用
  133. Askqueueinfo string `mapstructure:"Askqueueinfo" xorm:"VARCHAR(2000)"` // 大利市卖港股用
  134. Bidorderid int64 `mapstructure:"Bidorderid" xorm:"BIGINT(20)"` // 买单号1
  135. Bidorderid2 int64 `mapstructure:"Bidorderid2" xorm:"BIGINT(20)"` // 买单号2
  136. Bidorderid3 int64 `mapstructure:"Bidorderid3" xorm:"BIGINT(20)"` // 买单号3
  137. Bidorderid4 int64 `mapstructure:"Bidorderid4" xorm:"BIGINT(20)"` // 买单号4
  138. Bidorderid5 int64 `mapstructure:"Bidorderid5" xorm:"BIGINT(20)"` // 买单号5
  139. Askorderid int64 `mapstructure:"Askorderid" xorm:"BIGINT(20)"` // 卖单号1
  140. Askorderid2 int64 `mapstructure:"Askorderid2" xorm:"BIGINT(20)"` // 卖单号2
  141. Askorderid3 int64 `mapstructure:"Askorderid3" xorm:"BIGINT(20)"` // 卖单号3
  142. Askorderid4 int64 `mapstructure:"Askorderid4" xorm:"BIGINT(20)"` // 卖单号4
  143. Askorderid5 int64 `mapstructure:"Askorderid5" xorm:"BIGINT(20)"` // 卖单号5
  144. Lastlot int64 `mapstructure:"Lastlot" xorm:"BIGINT(20)"` // 最新成交手数
  145. Totallot int64 `mapstructure:"Totallot" xorm:"BIGINT(20)"` // 总手数
  146. Strikeprice int64 `mapstructure:"Strikeprice" xorm:"BIGINT(20)"` // 发行价
  147. Cleartime int64 `mapstructure:"Cleartime" xorm:"BIGINT(20)"` // 清盘时间
  148. Calloptionpremiums int64 `mapstructure:"Calloptionpremiums" xorm:"default 0 BIGINT(20)"` // 认购期权1
  149. Calloptionpremiums2 int64 `mapstructure:"Calloptionpremiums2" xorm:"default 0 BIGINT(20)"` // 认购期权2
  150. Calloptionpremiums3 int64 `mapstructure:"Calloptionpremiums3" xorm:"default 0 BIGINT(20)"` // 认购期权3
  151. Calloptionpremiums4 int64 `mapstructure:"Calloptionpremiums4" xorm:"default 0 BIGINT(20)"` // 认购期权4
  152. Calloptionpremiums5 int64 `mapstructure:"Calloptionpremiums5" xorm:"default 0 BIGINT(20)"` // 认购期权5
  153. Putoptionpremiums int64 `mapstructure:"Putoptionpremiums" xorm:"default 0 BIGINT(20)"` // 认沽期权1
  154. Putoptionpremiums2 int64 `mapstructure:"Putoptionpremiums2" xorm:"default 0 BIGINT(20)"` // 认沽期权2
  155. Putoptionpremiums3 int64 `mapstructure:"Putoptionpremiums3" xorm:"default 0 BIGINT(20)"` // 认沽期权3
  156. Putoptionpremiums4 int64 `mapstructure:"Putoptionpremiums4" xorm:"default 0 BIGINT(20)"` // 认沽期权4
  157. Putoptionpremiums5 int64 `mapstructure:"Putoptionpremiums5" xorm:"default 0 BIGINT(20)"` // 认沽期权5
  158. Nontotalvolume int64 `mapstructure:"Nontotalvolume" xorm:"default 0 BIGINT(20)"` // 非交易总量
  159. Nontotalholdervolume int64 `mapstructure:"Nontotalholdervolume" xorm:"default 0 BIGINT(20)"` // 非交易持仓量
  160. Nontotalturnover int64 `mapstructure:"Nontotalturnover" xorm:"default 0 BIGINT(20)"` // 非交易总金额
  161. Nontotallot int64 `mapstructure:"Nontotallot" xorm:"default 0 BIGINT(20)"` // 非交易总手数
  162. Publictradetype string `mapstructure:"Publictradetype" xorm:"VARCHAR(2)"` // 公共交易标志类型 港股专用
  163. Iep int64 `mapstructure:"Iep" xorm:"default 0 BIGINT(20)"` // 平衡价 港股专用
  164. Iev int64 `mapstructure:"Iev" xorm:"default 0 BIGINT(20)"` // 平衡量 港股专用
  165. Grepmarketprice int64 `mapstructure:"Grepmarketprice" xorm:"default 0 BIGINT(20)"` // 暗盘价 港股专用
  166. Bid6 int64 `mapstructure:"Bid6" xorm:"default 0 BIGINT(20)"` // 买6
  167. Bid7 int64 `mapstructure:"Bid7" xorm:"default 0 BIGINT(20)"` // 买7
  168. Bid8 int64 `mapstructure:"Bid8" xorm:"default 0 BIGINT(20)"` // 买8
  169. Bid9 int64 `mapstructure:"Bid9" xorm:"default 0 BIGINT(20)"` // 买9
  170. Bid10 int64 `mapstructure:"Bid10" xorm:"default 0 BIGINT(20)"` // 买10
  171. Bidvolume6 int64 `mapstructure:"Bidvolume6" xorm:"default 0 BIGINT(20)"` // 买量6
  172. Bidvolume7 int64 `mapstructure:"Bidvolume7" xorm:"default 0 BIGINT(20)"` // 买量7
  173. Bidvolume8 int64 `mapstructure:"Bidvolume8" xorm:"default 0 BIGINT(20)"` // 买量8
  174. Bidvolume9 int64 `mapstructure:"Bidvolume9" xorm:"default 0 BIGINT(20)"` // 买量9
  175. Bidvolume10 int64 `mapstructure:"Bidvolume10" xorm:"default 0 BIGINT(20)"` // 买量10
  176. Ask6 int64 `mapstructure:"Ask6" xorm:"default 0 BIGINT(20)"` // 卖6
  177. Ask7 int64 `mapstructure:"Ask7" xorm:"default 0 BIGINT(20)"` // 卖7
  178. Ask8 int64 `mapstructure:"Ask8" xorm:"default 0 BIGINT(20)"` // 卖8
  179. Ask9 int64 `mapstructure:"Ask9" xorm:"default 0 BIGINT(20)"` // 卖9
  180. Ask10 int64 `mapstructure:"Ask10" xorm:"default 0 BIGINT(20)"` // 卖10
  181. Askvolume6 int64 `mapstructure:"Askvolume6" xorm:"default 0 BIGINT(20)"` // 卖量6
  182. Askvolume7 int64 `mapstructure:"Askvolume7" xorm:"default 0 BIGINT(20)"` // 卖量7
  183. Askvolume8 int64 `mapstructure:"Askvolume8" xorm:"default 0 BIGINT(20)"` // 卖量8
  184. Askvolume9 int64 `mapstructure:"Askvolume9" xorm:"default 0 BIGINT(20)"` // 卖量9
  185. Askvolume10 int64 `mapstructure:"Askvolume10" xorm:"default 0 BIGINT(20)"` // 卖量10
  186. Bidordervolume int64 `mapstructure:"Bidordervolume" xorm:"default 0 BIGINT(20)"` // 买单量1
  187. Bidordervolume2 int64 `mapstructure:"Bidordervolume2" xorm:"default 0 BIGINT(20)"` // 买单量2
  188. Bidordervolume3 int64 `mapstructure:"Bidordervolume3" xorm:"default 0 BIGINT(20)"` // 买单量3
  189. Bidordervolume4 int64 `mapstructure:"Bidordervolume4" xorm:"default 0 BIGINT(20)"` // 买单量4
  190. Bidordervolume5 int64 `mapstructure:"Bidordervolume5" xorm:"default 0 BIGINT(20)"` // 买单量5
  191. Bidordervolume6 int64 `mapstructure:"Bidordervolume6" xorm:"default 0 BIGINT(20)"` // 买单量6
  192. Bidordervolume7 int64 `mapstructure:"Bidordervolume7" xorm:"default 0 BIGINT(20)"` // 买单量7
  193. Bidordervolume8 int64 `mapstructure:"Bidordervolume8" xorm:"default 0 BIGINT(20)"` // 买单量8
  194. Bidordervolume9 int64 `mapstructure:"Bidordervolume9" xorm:"default 0 BIGINT(20)"` // 买单量9
  195. Bidordervolume10 int64 `mapstructure:"Bidordervolume10" xorm:"default 0 BIGINT(20)"` // 买单量10
  196. Askordervolume int64 `mapstructure:"Askordervolume" xorm:"default 0 BIGINT(20)"` // 卖单量1
  197. Askordervolume2 int64 `mapstructure:"Askordervolume2" xorm:"default 0 BIGINT(20)"` // 卖单量2
  198. Askordervolume3 int64 `mapstructure:"Askordervolume3" xorm:"default 0 BIGINT(20)"` // 卖单量3
  199. Askordervolume4 int64 `mapstructure:"Askordervolume4" xorm:"default 0 BIGINT(20)"` // 卖单量4
  200. Askordervolume5 int64 `mapstructure:"Askordervolume5" xorm:"default 0 BIGINT(20)"` // 卖单量5
  201. Askordervolume6 int64 `mapstructure:"Askordervolume6" xorm:"default 0 BIGINT(20)"` // 卖单量6
  202. Askordervolume7 int64 `mapstructure:"Askordervolume7" xorm:"default 0 BIGINT(20)"` // 卖单量7
  203. Askordervolume8 int64 `mapstructure:"Askordervolume8" xorm:"default 0 BIGINT(20)"` // 卖单量8
  204. Askordervolume9 int64 `mapstructure:"Askordervolume9" xorm:"default 0 BIGINT(20)"` // 卖单量9
  205. Askordervolume10 int64 `mapstructure:"Askordervolume10" xorm:"default 0 BIGINT(20)"` // 卖单量10
  206. }
  207. // TableName is Quoteday
  208. func (*Quoteday) TableName() string {
  209. return "quoteday"
  210. }
  211. // GetHistoryCycleDatas 获取历史数据
  212. // 参数 cycleType CycleType 周期类型
  213. // 参数 goodsCode string 商品代码
  214. // 参数 startTime time.Time 开始时间(闭区间)
  215. // 参数 endTime time.Time 结束时间(闭区间)
  216. // 参数 count int 条数
  217. // 参数 isAscForST bool 是否按时间顺序排序,默认为时间倒序排序
  218. // 返回值 []CycleData 历史数据
  219. // 返回值 error 错误
  220. func GetHistoryCycleDatas(cycleType CycleType, goodsCode string, startTime, endTime *time.Time, count int, isAscForST bool) ([]CycleData, error) {
  221. db := db.GetMongoDB()
  222. // 获取目标Collection
  223. collection := "mincycle"
  224. switch cycleType {
  225. case CycleTypeTik:
  226. collection = "quotetik"
  227. case CycleTypeMinutes1:
  228. collection = "mincycle"
  229. case CycleTypeMinutes5:
  230. collection = "min5cycle"
  231. case CycleTypeMinutes30:
  232. collection = "min30cycle"
  233. case CycleTypeMinutes60:
  234. collection = "min60cycle"
  235. case CycleTypeMinutes240:
  236. collection = "min240cycle"
  237. case CycleTypeMinutesDay:
  238. collection = "daycycle"
  239. case CycleTypeWeek:
  240. collection = "weekcycle"
  241. case CycleTypeMonth:
  242. collection = "monthcycle"
  243. case CycleTypeYear:
  244. collection = "yearcycle"
  245. default:
  246. return nil, errors.New("不支持的周期类型")
  247. }
  248. c := db.C(collection)
  249. // 按时间排序
  250. sort := "-ST"
  251. if isAscForST {
  252. sort = "ST"
  253. }
  254. // 查询数据
  255. var cycleDatas []CycleData
  256. m := bson.M{"GC": goodsCode}
  257. if startTime != nil && endTime == nil {
  258. m["ST"] = bson.M{"$gte": startTime.Unix()}
  259. } else if startTime == nil && endTime != nil {
  260. m["ST"] = bson.M{"$lte": endTime.Unix()}
  261. } else if startTime != nil && endTime != nil {
  262. m["ST"] = bson.M{"$gte": startTime.Unix(), "$lte": endTime.Unix()}
  263. }
  264. query := c.Find(m)
  265. if count > 0 {
  266. query = query.Limit(count)
  267. }
  268. if err := query.Sort(sort).All(&cycleDatas); err != nil {
  269. return nil, err
  270. }
  271. return cycleDatas, nil
  272. }
  273. // GetHistoryTikDatas 获取历史数据
  274. // 参数 goodsCode string 商品代码
  275. // 参数 startTime time.Time 开始时间(闭区间)
  276. // 参数 endTime time.Time 结束时间(闭区间)
  277. // 参数 count int 条数
  278. // 参数 isAscForST bool 是否按时间顺序排序,默认为时间倒序排序
  279. // 返回值 []TikData Tik数据
  280. // 返回值 error 错误
  281. func GetHistoryTikDatas(goodsCode string, startTime, endTime *time.Time, count int, isAscForST bool) ([]TikData, error) {
  282. db := db.GetMongoDB()
  283. // 获取目标Collection
  284. collection := "quotetik"
  285. c := db.C(collection)
  286. // 按时间排序
  287. sort := "-_id"
  288. if isAscForST {
  289. sort = "id"
  290. }
  291. // 查询数据
  292. var tikDatas []TikData
  293. m := bson.M{"GC": goodsCode}
  294. if startTime != nil && endTime == nil {
  295. m["AT"] = bson.M{"$gte": startTime.Unix()}
  296. } else if startTime == nil && endTime != nil {
  297. m["AT"] = bson.M{"$lte": endTime.Unix()}
  298. } else if startTime != nil && endTime != nil {
  299. m["AT"] = bson.M{"$gte": startTime.Unix(), "$lte": endTime.Unix()}
  300. }
  301. query := c.Find(m)
  302. if count > 0 {
  303. query = query.Limit(count)
  304. }
  305. if err := query.Sort(sort).All(&tikDatas); err != nil {
  306. return nil, err
  307. }
  308. return tikDatas, nil
  309. }
  310. // GetQuoteDays 获取目标商品的盘面数据
  311. // 参数 goodsCodes string 商品代码字串,以“,”分隔
  312. // 返回 []Quoteday 盘面数据
  313. // 返回 error error
  314. func GetQuoteDays(goodsCodes string) ([]Quoteday, error) {
  315. if config.SerCfg.MySQLCfg.Enabled == 0 {
  316. // 使用 Redis 盘面
  317. return QueryRedisQuoteDays(goodsCodes)
  318. }
  319. engine := db.GetMySQLEngine()
  320. datas := make([]Quoteday, 0)
  321. if len(goodsCodes) > 0 {
  322. // 指定商品代码查询
  323. // 处理商品代码, 让其可作为sql的in条件内容。 例如, 原字符串 ru2201,CU2406, => 处理成 'ru2201','CU2406'
  324. goodsCodes = strings.Trim(goodsCodes, ",")
  325. sCode := strings.Split(goodsCodes, ",")
  326. for i := range sCode {
  327. // 如果没有单引号, 则加上单引号
  328. if !strings.Contains(sCode[i], "'") {
  329. sCode[i] = "'" + sCode[i] + "'"
  330. }
  331. }
  332. Instr := strings.Join(sCode, ",")
  333. if err := engine.Where(fmt.Sprintf("goodscode in (%s)", Instr)).Find(&datas); err != nil {
  334. return nil, err
  335. }
  336. } else {
  337. // 不指定则查所有
  338. if err := engine.Find(&datas); err != nil {
  339. return nil, err
  340. }
  341. }
  342. return datas, nil
  343. }
  344. // SetRedisQuoteDays 将盘面数据同步到Redis
  345. func SetRedisQuoteDays() (err error) {
  346. engine := db.GetMySQLEngine()
  347. datas := make([]Quoteday, 0)
  348. if err = engine.Find(&datas); err != nil {
  349. return
  350. }
  351. b, err := json.Marshal(datas)
  352. if err != nil {
  353. return
  354. }
  355. redisCli := rediscli.GetRedisClient()
  356. err = redisCli.Set("Tourist:QuoteDay", b, 0).Err()
  357. return
  358. }
  359. // QueryRedisQuoteDays 从Redis读取盘面数据
  360. func QueryRedisQuoteDays(goodsCodes string) (quoteDays []Quoteday, err error) {
  361. exchangeCode := "250"
  362. quoteDays = make([]Quoteday, 0)
  363. redisCli := rediscli.GetRedisClient()
  364. for _, goodsCode := range strings.Split(goodsCodes, ",") {
  365. var m map[string]string
  366. m, err = redisCli.HGetAll(fmt.Sprintf("QuoteDay:%s_%s", exchangeCode, goodsCode)).Result()
  367. if err != nil {
  368. return
  369. }
  370. if len(m) == 0 {
  371. return
  372. }
  373. var quoteDay Quoteday
  374. config := &mapstructure.DecoderConfig{
  375. WeaklyTypedInput: true, // 启用弱类型转换,允许字符串自动转换为数字
  376. Result: &quoteDay,
  377. }
  378. decoder, err := mapstructure.NewDecoder(config)
  379. if err != nil {
  380. return quoteDays, err
  381. }
  382. err = decoder.Decode(m)
  383. if err != nil {
  384. return quoteDays, err
  385. }
  386. quoteDays = append(quoteDays, quoteDay)
  387. }
  388. return
  389. }
  390. // GetRedisQuoteDays 从Redis读取盘面数据(游客)
  391. func GetRedisQuoteDays(goodsCodes []string) (quoteDays []Quoteday, err error) {
  392. redisCli := rediscli.GetRedisClient()
  393. b, err := redisCli.Get("Tourist:QuoteDay").Bytes()
  394. if err != nil {
  395. return
  396. }
  397. var datas []Quoteday
  398. if err = json.Unmarshal(b, &datas); err != nil {
  399. return
  400. }
  401. quoteDays = make([]Quoteday, 0)
  402. for _, item := range datas {
  403. if utils.InArray(len(goodsCodes), func(i int) bool { return goodsCodes[i] == item.Goodscode }) {
  404. quoteDays = append(quoteDays, item)
  405. }
  406. }
  407. return
  408. }
  409. var Done chan bool
  410. func StartSyncRedisQuoteDay() {
  411. ticker := time.NewTicker(2 * time.Second)
  412. Done = make(chan bool)
  413. go func() {
  414. for {
  415. select {
  416. case <-Done:
  417. ticker.Stop()
  418. return
  419. case <-ticker.C:
  420. // 在这里执行定时任务的代码
  421. if err := SetRedisQuoteDays(); err != nil {
  422. logger.GetLogger().Errorf("Sync Redis failed: %s", err.Error())
  423. }
  424. }
  425. }
  426. }()
  427. }