exchange_crawler.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/models"
  5. "fmt"
  6. "github.com/mozillazg/go-pinyin"
  7. "strings"
  8. "time"
  9. )
  10. // 交易所爬虫
  11. type ExchangeCrawler struct {
  12. BaseAuthController
  13. }
  14. var ineIndexCodeMap = make(map[string]string)
  15. // @Title 刷新数据
  16. // @Description 刷新数据接口
  17. // @Param request body models.AddEdbClassifyReq true "type json string"
  18. // @Success 200 {object} models.EdbClassify
  19. // @router /refresh/ine [post]
  20. func (this *ExchangeCrawler) RefreshIne() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. var req models.RefreshINEExchangeReq
  27. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  28. if err != nil {
  29. br.Msg = "参数解析异常!"
  30. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  31. return
  32. }
  33. allCode, err := models.GetIndexCodeFromMapping("Ine")
  34. if err != nil {
  35. fmt.Println("select Code err:", err)
  36. }
  37. for _, item := range allCode {
  38. ineIndexCodeMap[item.IndexName] = item.IndexCode
  39. }
  40. message := req.Data
  41. var position = message.OCursor
  42. var tradeDate = message.ReportDate
  43. existIndexMap := make(map[string]*models.BaseFromTradeIneIndex)
  44. //获取所有指标信息
  45. allIndex, err := models.GetBaseFromTradeIneIndexAll(req.Date)
  46. if err != nil {
  47. fmt.Println("select err:", err)
  48. }
  49. for _, v := range allIndex {
  50. indexKey := v.DealName + v.BuyName + v.SoldName
  51. existIndexMap[indexKey] = v
  52. }
  53. var itemVerifyCode int
  54. //处理指标
  55. for _, p := range position {
  56. var item = new(models.BaseFromTradeIneIndex)
  57. if p.Rank > 0 && p.Rank < 40 && p.Participantabbr1 != "" {
  58. //成交量
  59. item.Rank = p.Rank
  60. item.DealShortName = strings.Trim(p.Participantabbr1, " ")
  61. item.BuyShortName = strings.Trim(p.Participantabbr2, " ")
  62. item.SoldShortName = strings.Trim(p.Participantabbr3, " ")
  63. item.DealName = strings.Replace(fmt.Sprintf("%s", p.Participantabbr1+"_"+p.Instrumentid+"_成交量(手)"), " ", "", -1)
  64. item.BuyName = strings.Replace(fmt.Sprintf("%s", p.Participantabbr2+"_"+p.Instrumentid+"_持买单量(手)"), " ", "", -1)
  65. item.SoldName = strings.Replace(fmt.Sprintf("%s", p.Participantabbr3+"_"+p.Instrumentid+"_持卖单量(手)"), " ", "", -1)
  66. item.DealCode = IneIndexCodeGenerator(item.DealShortName, item.DealName, p.Instrumentid, "deal")
  67. item.BuyCode = IneIndexCodeGenerator(item.BuyShortName, item.BuyName, p.Instrumentid, "buy")
  68. item.SoldCode = IneIndexCodeGenerator(item.SoldShortName, item.SoldName, p.Instrumentid, "sold")
  69. item.ClassifyName = strings.Replace(p.Productname, " ", "", -1)
  70. item.ClassifyType = strings.Replace(p.Instrumentid, " ", "", -1)
  71. item.Frequency = "日度"
  72. item.CreateTime = time.Now()
  73. item.ModifyTime = time.Now()
  74. item.DataTime = tradeDate
  75. if deal, ok := p.Deal.(float64); ok {
  76. item.DealValue = int(deal)
  77. }
  78. if change1, ok := p.Change1.(float64); ok {
  79. item.DealChange = int(change1)
  80. }
  81. if buyIn, ok := p.BuyIn.(float64); ok {
  82. item.BuyValue = int(buyIn)
  83. }
  84. if change2, ok := p.Change2.(float64); ok {
  85. item.BuyChange = int(change2)
  86. }
  87. if soldOut, ok := p.SoldOut.(float64); ok {
  88. item.SoldValue = int(soldOut)
  89. }
  90. if change3, ok := p.Change3.(float64); ok {
  91. item.SoldChange = int(change3)
  92. }
  93. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  94. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  95. newID, err := models.AddBaseFromTradeIneIndex(item)
  96. if err != nil {
  97. fmt.Println("insert error:", err)
  98. }
  99. fmt.Println("insert new indexID:", newID)
  100. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  101. //更新
  102. err := models.ModifyBaseFromTradeIneIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeIneIndexId)
  103. if err != nil {
  104. fmt.Println("data update err:", err)
  105. }
  106. }
  107. } else if p.Rank == 999 {
  108. //Top 20
  109. item.Rank = p.Rank
  110. item.DealShortName = strings.Trim(p.Participantabbr1, " ")
  111. item.BuyShortName = strings.Trim(p.Participantabbr2, " ")
  112. item.SoldShortName = strings.Trim(p.Participantabbr3, " ")
  113. item.DealName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.Instrumentid+"_成交量(手)"), " ", "", -1)
  114. item.BuyName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.Instrumentid+"_持买单量(手)"), " ", "", -1)
  115. item.SoldName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.Instrumentid+"_持卖单量(手)"), " ", "", -1)
  116. item.DealCode = IneIndexCodeGenerator("top20", item.DealName, p.Instrumentid, "deal")
  117. item.BuyCode = IneIndexCodeGenerator("top20", item.BuyName, p.Instrumentid, "buy")
  118. item.SoldCode = IneIndexCodeGenerator("top20", item.SoldName, p.Instrumentid, "sold")
  119. item.ClassifyName = strings.Replace(p.Productname, " ", "", -1)
  120. item.ClassifyType = strings.Replace(p.Instrumentid, " ", "", -1)
  121. item.Frequency = "日度"
  122. item.CreateTime = time.Now()
  123. item.ModifyTime = time.Now()
  124. item.DataTime = tradeDate
  125. if deal, ok := p.Deal.(float64); ok {
  126. item.DealValue = int(deal)
  127. }
  128. if change1, ok := p.Change1.(float64); ok {
  129. item.DealChange = int(change1)
  130. }
  131. if buyIn, ok := p.BuyIn.(float64); ok {
  132. item.BuyValue = int(buyIn)
  133. }
  134. if change2, ok := p.Change2.(float64); ok {
  135. item.BuyChange = int(change2)
  136. }
  137. if soldOut, ok := p.SoldOut.(float64); ok {
  138. item.SoldValue = int(soldOut)
  139. }
  140. if change3, ok := p.Change3.(float64); ok {
  141. item.SoldChange = int(change3)
  142. }
  143. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  144. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  145. newID, err := models.AddBaseFromTradeIneIndex(item)
  146. if err != nil {
  147. fmt.Println("insert error:", err)
  148. }
  149. fmt.Println("insert new indexID:", newID)
  150. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  151. //更新
  152. err := models.ModifyBaseFromTradeIneIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeIneIndexId)
  153. if err != nil {
  154. fmt.Println("data update err:", err)
  155. }
  156. }
  157. }
  158. }
  159. br.Ret = 200
  160. br.Msg = "获取成功"
  161. br.Success = true
  162. }
  163. var indexCodeMap = make(map[string]string)
  164. // @Title 刷新数据
  165. // @Description 刷新数据接口
  166. // @Param request body models.AddEdbClassifyReq true "type json string"
  167. // @Success 200 {object} models.EdbClassify
  168. // @router /refresh/sh [post]
  169. func (this *ExchangeCrawler) RefreshSH() {
  170. br := new(models.BaseResponse).Init()
  171. defer func() {
  172. this.Data["json"] = br
  173. this.ServeJSON()
  174. }()
  175. var req models.RefreshSHExchangeReq
  176. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  177. if err != nil {
  178. br.Msg = "参数解析异常!"
  179. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  180. return
  181. }
  182. allCode, err := models.GetIndexCodeFromMapping("SH")
  183. if err != nil {
  184. fmt.Println("select Code err:", err)
  185. return
  186. }
  187. for _, item := range allCode {
  188. indexCodeMap[item.IndexName] = item.IndexCode
  189. }
  190. message := req.Data
  191. var position = message.Position
  192. var tradeDate = message.ReportDate
  193. existIndexMap := make(map[string]*models.BaseFromTradeShanghaiIndex)
  194. //获取所有指标信息
  195. allIndex, err := models.GetBaseFromTradeShangHaiIndexAll(req.Date)
  196. if err != nil {
  197. fmt.Println("select err:", err)
  198. }
  199. for _, v := range allIndex {
  200. indexKey := v.DealName + v.BuyName + v.SoldName
  201. existIndexMap[indexKey] = v
  202. }
  203. var itemVerifyCode int
  204. //处理指标
  205. for _, p := range position {
  206. var item = new(models.BaseFromTradeShanghaiIndex)
  207. if p.Rank > 0 && p.Rank < 40 && p.ParticipantName1 != "" {
  208. if strings.Replace(p.ProductName, " ", "", -1) != "20号胶" && strings.Replace(p.ProductName, " ", "", -1) != "低硫燃料油" {
  209. contractCode := strings.Replace(p.ContractCode, " ", "", -1)
  210. //成交量
  211. item.Rank = p.Rank
  212. item.DealShortName = strings.Replace(p.ParticipantName1, " ", "", -1)
  213. item.BuyShortName = strings.Replace(p.ParticipantName2, " ", "", -1)
  214. item.SoldShortName = strings.Replace(p.ParticipantName3, " ", "", -1)
  215. item.DealName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName1+"_"+p.ContractCode+"_成交量(手)"), " ", "", -1)
  216. item.BuyName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName2+"_"+p.ContractCode+"_持买单量(手)"), " ", "", -1)
  217. item.SoldName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName3+"_"+p.ContractCode+"_持卖单量(手)"), " ", "", -1)
  218. item.DealCode = ShIndexCodeGenerator(item.DealShortName, item.DealName, contractCode, "deal")
  219. item.BuyCode = ShIndexCodeGenerator(item.BuyShortName, item.BuyName, contractCode, "buy")
  220. item.SoldCode = ShIndexCodeGenerator(item.SoldShortName, item.SoldName, contractCode, "sold")
  221. item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
  222. item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
  223. item.Frequency = "日度"
  224. item.CreateTime = time.Now()
  225. item.ModifyTime = time.Now()
  226. item.DataTime = tradeDate
  227. if deal, ok := p.Deal.(float64); ok {
  228. item.DealValue = int(deal)
  229. }
  230. if change1, ok := p.Change1.(float64); ok {
  231. item.DealChange = int(change1)
  232. }
  233. if buyIn, ok := p.BuyIn.(float64); ok {
  234. item.BuyValue = int(buyIn)
  235. }
  236. if change2, ok := p.Change2.(float64); ok {
  237. item.BuyChange = int(change2)
  238. }
  239. if soldOut, ok := p.SoldOut.(float64); ok {
  240. item.SoldValue = int(soldOut)
  241. }
  242. if change3, ok := p.Change3.(float64); ok {
  243. item.SoldChange = int(change3)
  244. }
  245. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  246. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  247. newID, e := models.AddBaseFromTradeShangHaiIndex(item)
  248. if e != nil {
  249. err = e
  250. fmt.Println("insert error:", err)
  251. }
  252. fmt.Println("insert new indexID:", newID)
  253. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  254. //更新
  255. err = models.ModifyBaseFromTradeShangHaiIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeShangHaiIndexId)
  256. if err != nil {
  257. fmt.Println("data update err:", err)
  258. }
  259. }
  260. }
  261. } else if p.Rank == 999 {
  262. if strings.Replace(p.ProductName, " ", "", -1) != "20号胶" && strings.Replace(p.ProductName, " ", "", -1) != "低硫燃料油" {
  263. contractCode := strings.Replace(p.ContractCode, " ", "", -1)
  264. //Top 20
  265. item.Rank = p.Rank
  266. item.DealShortName = strings.Replace(p.ParticipantName1, " ", "", -1)
  267. item.BuyShortName = strings.Replace(p.ParticipantName2, " ", "", -1)
  268. item.SoldShortName = strings.Replace(p.ParticipantName3, " ", "", -1)
  269. item.DealName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.ContractCode+"_成交量(手)"), " ", "", -1)
  270. item.BuyName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.ContractCode+"_持买单量(手)"), " ", "", -1)
  271. item.SoldName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.ContractCode+"_持卖单量(手)"), " ", "", -1)
  272. item.DealCode = ShIndexCodeGenerator("top20", item.DealName, contractCode, "deal")
  273. item.BuyCode = ShIndexCodeGenerator("top20", item.BuyName, contractCode, "buy")
  274. item.SoldCode = ShIndexCodeGenerator("top20", item.SoldName, contractCode, "sold")
  275. item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
  276. item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
  277. item.Frequency = "日度"
  278. item.CreateTime = time.Now()
  279. item.ModifyTime = time.Now()
  280. item.DataTime = tradeDate
  281. if deal, ok := p.Deal.(float64); ok {
  282. item.DealValue = int(deal)
  283. }
  284. if change1, ok := p.Change1.(float64); ok {
  285. item.DealChange = int(change1)
  286. }
  287. if buyIn, ok := p.BuyIn.(float64); ok {
  288. item.BuyValue = int(buyIn)
  289. }
  290. if change2, ok := p.Change2.(float64); ok {
  291. item.BuyChange = int(change2)
  292. }
  293. if soldOut, ok := p.SoldOut.(float64); ok {
  294. item.SoldValue = int(soldOut)
  295. }
  296. if change3, ok := p.Change3.(float64); ok {
  297. item.SoldChange = int(change3)
  298. }
  299. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  300. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  301. newID, e := models.AddBaseFromTradeShangHaiIndex(item)
  302. if e != nil {
  303. err = e
  304. fmt.Println("insert error:", err)
  305. }
  306. fmt.Println("insert new indexID:", newID)
  307. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  308. //更新
  309. err = models.ModifyBaseFromTradeShangHaiIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeShangHaiIndexId)
  310. if err != nil {
  311. fmt.Println("data update err:", err)
  312. }
  313. }
  314. }
  315. }
  316. }
  317. br.Ret = 200
  318. br.Msg = "获取成功"
  319. br.Success = true
  320. }
  321. func ShIndexCodeGenerator(shortName, indexName, contractCode, suffix string) string {
  322. if shortName == "" {
  323. return ""
  324. }
  325. strResult := ""
  326. if shortName != "top20" {
  327. //取公司全拼
  328. a := pinyin.NewArgs()
  329. rows := pinyin.LazyPinyin(shortName, a)
  330. for i := 0; i < len(rows); i++ {
  331. strResult += rows[i]
  332. }
  333. } else {
  334. strResult = "top20"
  335. }
  336. indexCode, _ := indexCodeMap[indexName]
  337. if indexCode == "" {
  338. indexCode = strResult + contractCode + suffix
  339. indexCode = strings.Replace(indexCode, " ", "", -1)
  340. indexCodeMap[indexName] = indexCode
  341. err := models.AddBaseFromTradeMapping(indexName, indexCode, "SH")
  342. if err != nil {
  343. fmt.Println("add Code err:", err)
  344. }
  345. }
  346. return strings.Replace(indexCode, " ", "", -1)
  347. }
  348. func IneIndexCodeGenerator(shortName, indexName, Instrumentid, suffix string) string {
  349. if shortName == "" {
  350. return ""
  351. }
  352. strResult := ""
  353. if shortName != "top20" {
  354. //取公司全拼
  355. a := pinyin.NewArgs()
  356. rows := pinyin.LazyPinyin(shortName, a)
  357. for i := 0; i < len(rows); i++ {
  358. strResult += rows[i]
  359. }
  360. } else {
  361. strResult = "top20"
  362. }
  363. ineIndexCode, _ := ineIndexCodeMap[indexName]
  364. if ineIndexCode == "" {
  365. ineIndexCode = strResult + Instrumentid + suffix
  366. ineIndexCode = strings.Replace(ineIndexCode, " ", "", -1)
  367. ineIndexCodeMap[indexName] = ineIndexCode
  368. err := models.AddBaseFromTradeMapping(indexName, ineIndexCode, "INE")
  369. if err != nil {
  370. fmt.Println("add Code err:", err)
  371. }
  372. }
  373. return strings.Replace(ineIndexCode, " ", "", -1)
  374. }