quote.go 22 KB

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