trade_guangzhou.go 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_crawler/models"
  5. "eta/eta_crawler/services/alarm_msg"
  6. "eta/eta_crawler/utils"
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // SyncRankingFromGuangzhou 广州交易所期货-日行情
  13. func CrawlerFromGuangzhouDayQuotes(date time.Time) {
  14. var err error
  15. defer func() {
  16. if err != nil {
  17. msg := "失败提醒" + "SyncRankingFromGuangzhou ErrMsg:" + err.Error()
  18. fmt.Println("msg:", msg)
  19. utils.FileLog.Info(msg)
  20. go alarm_msg.SendAlarmMsg(msg, 3)
  21. }
  22. }()
  23. now := date //time.Now()
  24. if now.Weekday() == time.Saturday || now.Weekday() == time.Sunday {
  25. return
  26. }
  27. dateStr := now.Format(utils.FormatDateUnSpace)
  28. zzUrl := `http://www.gfex.com.cn/u/interfacesWebTiDayQuotes/loadList`
  29. zzUrl = fmt.Sprintf(zzUrl)
  30. fmt.Println(zzUrl)
  31. // params := ""
  32. params := fmt.Sprintf("trade_date=%s&trade_type=0", dateStr)
  33. headerParams := make(map[string]string)
  34. //headerParams["Cookie"] = "CT6T=312900; SF_cookie_3=68941398"
  35. headerParams["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
  36. headerParams["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
  37. body, e := utils.HttpPost(zzUrl, params, headerParams)
  38. if e != nil {
  39. err = e
  40. fmt.Println("err:", err)
  41. return
  42. }
  43. fmt.Println("body :" + string(body))
  44. utils.FileLog.Info("body:" + string(body))
  45. if strings.Contains(string(body), "很抱歉! 因系统检测到您的请求可能对网站造成威胁") {
  46. return
  47. }
  48. item := new(models.TradeGuangZhou)
  49. err = json.Unmarshal(body, &item)
  50. if err != nil {
  51. fmt.Println("json.Unmarshal err:" + err.Error())
  52. return
  53. }
  54. gzObj := new(models.BaseFromTradeGuangzhouIndex)
  55. parentId := 1
  56. classifyList, err := gzObj.GetBaseFromTradeGuangzhouClassifyAll(parentId)
  57. if err != nil {
  58. fmt.Println("GetBaseFromTradeGuangzhouClassify Err:" + err.Error())
  59. return
  60. }
  61. classifyMap := make(map[string]*models.BaseFromTradeGuangzhouClassify)
  62. for _, cv := range classifyList {
  63. if cv.ClassifyCode != "" {
  64. classifyMap[cv.ClassifyCode] = cv
  65. }
  66. }
  67. //获取所有指标
  68. indexList, err := gzObj.GetBaseFromTradeGuangzhouIndexAll()
  69. if err != nil {
  70. fmt.Println("GetBaseFromTradeGuangzhouIndexAll Err:" + err.Error())
  71. return
  72. }
  73. indexMap := make(map[string]*models.BaseFromTradeGuangzhouIndex)
  74. for _, iv := range indexList {
  75. indexMap[iv.IndexCode] = iv
  76. }
  77. dataList, err := gzObj.GetBaseFromTradeGuangzhouDataByDate(now.Format(utils.FormatDate))
  78. dataMap := make(map[string]*models.BaseFromTradeGuangzhouData)
  79. for _, dv := range dataList {
  80. dataMap[dv.IndexCode] = dv
  81. }
  82. addDataList := make([]*models.BaseFromTradeGuangzhouData, 0)
  83. updateDataList := make([]*models.BaseFromTradeGuangzhouData, 0)
  84. for _, dv := range item.Data {
  85. if dv.Variety == "总计" {
  86. continue
  87. }
  88. var classifyId int
  89. if classifyItem, ok := classifyMap[dv.VarietyOrder]; !ok {
  90. classifyItem = new(models.BaseFromTradeGuangzhouClassify)
  91. classifyItem.ClassifyCode = dv.VarietyOrder
  92. classifyItem.ClassifyName = dv.Variety
  93. classifyItem.ParentId = 1
  94. classifyItem.ModifyTime = time.Now()
  95. classifyItem.CreateTime = time.Now()
  96. classifyLastId, err := gzObj.AddBaseFromTradeGuangzhouClassify(classifyItem)
  97. if err != nil {
  98. fmt.Println("AddBaseFromTradeGuangzhouClassify err:" + err.Error())
  99. return
  100. }
  101. classifyId = int(classifyLastId)
  102. } else {
  103. classifyId = classifyItem.BaseFromTradeGuangzhouClassifyId
  104. }
  105. if dv.DelivMonth == "" && !strings.Contains(dv.Variety, "小计") {
  106. continue
  107. }
  108. if !strings.Contains(dv.Variety, "小计") {
  109. //开盘价
  110. openIndexCode := dv.VarietyOrder + dv.DelivMonth + "open"
  111. //最高价
  112. highIndexCode := dv.VarietyOrder + dv.DelivMonth + "high"
  113. //最低价
  114. lowIndexCode := dv.VarietyOrder + dv.DelivMonth + "low"
  115. //收盘价
  116. closeIndexCode := dv.VarietyOrder + dv.DelivMonth + "close"
  117. //前结算价
  118. lastClearIndexCode := dv.VarietyOrder + dv.DelivMonth + "lastClear"
  119. //结算价
  120. clearPriceIndexCode := dv.VarietyOrder + dv.DelivMonth + "clearPrice"
  121. //涨跌
  122. diffIndexCode := dv.VarietyOrder + dv.DelivMonth + "diff"
  123. //涨跌1
  124. diff1IndexCode := dv.VarietyOrder + dv.DelivMonth + "diff1"
  125. //成交量
  126. volumeIndexCode := dv.VarietyOrder + dv.DelivMonth + "volumn"
  127. //持仓量
  128. openInterestIndexCode := dv.VarietyOrder + dv.DelivMonth + "openInterest"
  129. //持仓量变化
  130. diffIChangeIndexCode := dv.VarietyOrder + dv.DelivMonth + "diffI"
  131. //成交额
  132. turnoverIndexCode := dv.VarietyOrder + dv.DelivMonth + "turnover"
  133. handleIndexCodeArr := []string{openIndexCode, highIndexCode, lowIndexCode, closeIndexCode, lastClearIndexCode, clearPriceIndexCode, diffIndexCode, diff1IndexCode, volumeIndexCode, openInterestIndexCode, diffIChangeIndexCode, turnoverIndexCode}
  134. prefxCodeArr := []string{"open", "high", "low", "close", "lastClear", "clearPrice", "diff", "diff1", "volumn", "openInterest", "diffI", "turnover"}
  135. handleIndexNameArr := []string{"开盘价", "最高价", "最低价", "收盘价", "前结算价", "结算价", "涨跌", "涨跌1", "成交量", "持仓量", "持仓量变化", "成交额"}
  136. for ck, cv := range handleIndexCodeArr {
  137. var indexId int
  138. handleIndexCode := cv
  139. if indexItem, ok := indexMap[handleIndexCode]; !ok {
  140. indexName := dv.Variety + dv.DelivMonth + handleIndexNameArr[ck]
  141. var unit string
  142. if strings.Contains(indexName, "价") || strings.Contains(indexName, "涨跌") {
  143. unit = "元/吨"
  144. } else if strings.Contains(indexName, "量") {
  145. unit = "手"
  146. } else if strings.Contains(indexName, "成交额") {
  147. unit = "万元"
  148. }
  149. gzObj = &models.BaseFromTradeGuangzhouIndex{
  150. BaseFromTradeGuangzhouClassifyId: classifyId,
  151. IndexCode: handleIndexCode,
  152. IndexName: indexName,
  153. Frequency: "日度",
  154. Unit: unit,
  155. CreateTime: time.Now(),
  156. ModifyTime: time.Now(),
  157. }
  158. lastId, err := gzObj.AddBaseFromTradeGuangzhouIndex()
  159. if err != nil {
  160. fmt.Println("AddBaseFromTradeGuangzhouIndex Err:" + err.Error())
  161. return
  162. }
  163. indexId = int(lastId)
  164. } else {
  165. indexId = indexItem.BaseFromTradeGuangzhouIndexId
  166. }
  167. if dataItem, ok := dataMap[handleIndexCode]; !ok {
  168. dataItem = new(models.BaseFromTradeGuangzhouData)
  169. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  170. dataItem.IndexCode = handleIndexCode
  171. dataItem.DataTime = dateStr
  172. switch prefxCodeArr[ck] {
  173. case "open":
  174. dataItem.Value = float64(dv.Open)
  175. case "high":
  176. dataItem.Value = float64(dv.High)
  177. case "low":
  178. dataItem.Value = float64(dv.Low)
  179. case "close":
  180. dataItem.Value = float64(dv.Close)
  181. case "lastClear":
  182. dataItem.Value = float64(dv.LastClear)
  183. case "clearPrice":
  184. dataItem.Value = float64(dv.ClearPrice)
  185. case "diff":
  186. dataItem.Value = float64(dv.Diff)
  187. case "diff1":
  188. dataItem.Value = float64(dv.Diff1)
  189. case "volumn":
  190. dataItem.Value = float64(dv.Volumn)
  191. case "openInterest":
  192. dataItem.Value = float64(dv.OpenInterest)
  193. case "diffI":
  194. dataItem.Value = float64(dv.DiffI)
  195. case "turnover":
  196. dataItem.Value = dv.Turnover
  197. }
  198. dataItem.CreateTime = time.Now()
  199. dataItem.ModifyTime = time.Now()
  200. addDataList = append(addDataList, dataItem)
  201. } else {
  202. var getValue float64
  203. switch prefxCodeArr[ck] {
  204. case "open":
  205. getValue = float64(dv.Open)
  206. case "high":
  207. getValue = float64(dv.High)
  208. case "low":
  209. getValue = float64(dv.Low)
  210. case "close":
  211. getValue = float64(dv.Close)
  212. case "lastClear":
  213. getValue = float64(dv.LastClear)
  214. case "clearPrice":
  215. getValue = float64(dv.ClearPrice)
  216. case "diff":
  217. getValue = float64(dv.Diff)
  218. case "diff1":
  219. getValue = float64(dv.Diff1)
  220. case "volumn":
  221. getValue = float64(dv.Volumn)
  222. case "openInterest":
  223. getValue = float64(dv.OpenInterest)
  224. case "diffI":
  225. getValue = float64(dv.DiffI)
  226. case "turnover":
  227. getValue = dv.Turnover
  228. }
  229. if dataItem.Value != getValue {
  230. dataItem.BaseFromTradeGuangzhouDataId = dataItem.BaseFromTradeGuangzhouDataId
  231. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  232. dataItem.IndexCode = handleIndexCode
  233. dataItem.DataTime = dateStr
  234. dataItem.Value = getValue
  235. dataItem.ModifyTime = time.Now()
  236. updateDataList = append(updateDataList, dataItem)
  237. }
  238. }
  239. }
  240. } else {
  241. verietyPy := utils.ChineseToPinyinInitials("日行情" + dv.Variety)
  242. //成交量
  243. volumeIndexCode := dv.VarietyOrder + verietyPy + "volumn"
  244. //持仓量
  245. openInterestIndexCode := dv.VarietyOrder + verietyPy + "openInterest"
  246. //持仓量变化
  247. diffIChangeIndexCode := dv.VarietyOrder + verietyPy + "diffI"
  248. //成交额
  249. turnoverIndexCode := dv.VarietyOrder + verietyPy + "turnover"
  250. handleIndexCodeArr := []string{volumeIndexCode, openInterestIndexCode, diffIChangeIndexCode, turnoverIndexCode}
  251. prefxCodeArr := []string{"volumn", "openInterest", "diffI", "turnover"}
  252. handleIndexNameArr := []string{"成交量", "持仓量", "持仓量变化", "成交额"}
  253. for ck, cv := range handleIndexCodeArr {
  254. var indexId int
  255. handleIndexCode := cv
  256. if indexItem, ok := indexMap[handleIndexCode]; !ok {
  257. indexName := "日行情" + dv.Variety + handleIndexNameArr[ck]
  258. var unit string
  259. if strings.Contains(indexName, "价") || strings.Contains(indexName, "涨跌") {
  260. unit = "元/吨"
  261. } else if strings.Contains(indexName, "量") {
  262. unit = "手"
  263. } else if strings.Contains(indexName, "成交额") {
  264. unit = "万元"
  265. }
  266. gzObj = &models.BaseFromTradeGuangzhouIndex{
  267. BaseFromTradeGuangzhouClassifyId: classifyId,
  268. IndexCode: handleIndexCode,
  269. IndexName: indexName,
  270. Frequency: "日度",
  271. Unit: unit,
  272. CreateTime: time.Now(),
  273. ModifyTime: time.Now(),
  274. }
  275. lastId, err := gzObj.AddBaseFromTradeGuangzhouIndex()
  276. if err != nil {
  277. fmt.Println("AddBaseFromTradeGuangzhouIndex Err:" + err.Error())
  278. return
  279. }
  280. indexId = int(lastId)
  281. } else {
  282. indexId = indexItem.BaseFromTradeGuangzhouIndexId
  283. }
  284. if dataItem, ok := dataMap[handleIndexCode]; !ok {
  285. dataItem = new(models.BaseFromTradeGuangzhouData)
  286. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  287. dataItem.IndexCode = handleIndexCode
  288. dataItem.DataTime = dateStr
  289. switch prefxCodeArr[ck] {
  290. case "volumn":
  291. dataItem.Value = float64(dv.Volumn)
  292. case "openInterest":
  293. dataItem.Value = float64(dv.OpenInterest)
  294. case "diffI":
  295. dataItem.Value = float64(dv.DiffI)
  296. case "turnover":
  297. dataItem.Value = dv.Turnover
  298. }
  299. dataItem.CreateTime = time.Now()
  300. dataItem.ModifyTime = time.Now()
  301. addDataList = append(addDataList, dataItem)
  302. } else {
  303. var getValue float64
  304. switch prefxCodeArr[ck] {
  305. case "volumn":
  306. getValue = float64(dv.Volumn)
  307. case "openInterest":
  308. getValue = float64(dv.OpenInterest)
  309. case "diffI":
  310. getValue = float64(dv.DiffI)
  311. case "turnover":
  312. getValue = dv.Turnover
  313. }
  314. if dataItem.Value != getValue {
  315. dataItem.BaseFromTradeGuangzhouDataId = dataItem.BaseFromTradeGuangzhouDataId
  316. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  317. dataItem.IndexCode = handleIndexCode
  318. dataItem.DataTime = dateStr
  319. dataItem.Value = getValue
  320. dataItem.ModifyTime = time.Now()
  321. updateDataList = append(updateDataList, dataItem)
  322. }
  323. }
  324. }
  325. }
  326. }
  327. if len(addDataList) > 0 {
  328. err = gzObj.AddBaseFromTradeGuangzhouData(addDataList)
  329. if err != nil {
  330. fmt.Println("AddBaseFromTradeGuangzhouData Err:" + err.Error())
  331. return
  332. }
  333. }
  334. if len(updateDataList) > 0 {
  335. for _, v := range updateDataList {
  336. updateParams := make(map[string]interface{})
  337. whereParam := make(map[string]interface{})
  338. updateParams["value"] = v.Value
  339. updateParams["modify_time"] = time.Now()
  340. whereParam["base_from_trade_guangzhou_data_id"] = v.BaseFromTradeGuangzhouDataId
  341. err = gzObj.UpdateGuangzhouData(updateParams, whereParam)
  342. if err != nil {
  343. fmt.Println("UpdateGuangzhouData Err:" + err.Error())
  344. return
  345. }
  346. }
  347. }
  348. }
  349. // SyncRankingFromGuangzhou 广州交易所期货-合约
  350. func CrawlerFromGuangzhouContract(date time.Time) {
  351. // 准备: HTTP请求
  352. now := date //time.Now()
  353. if now.Weekday() == time.Saturday || now.Weekday() == time.Sunday {
  354. return
  355. }
  356. dateStr := now.Format(utils.FormatDateUnSpace)
  357. dateFormatStr := now.Format(utils.FormatDate)
  358. gzObj := new(models.BaseFromTradeGuangzhouIndex)
  359. parentId := 2
  360. classifyList, err := gzObj.GetBaseFromTradeGuangzhouClassifyAll(parentId)
  361. if err != nil {
  362. fmt.Println("GetBaseFromTradeGuangzhouClassify Err:" + err.Error())
  363. return
  364. }
  365. contractList, err := gzObj.GetBaseFromTradeGuangzhouContract("")
  366. if err != nil {
  367. fmt.Println("GetBaseFromTradeGuangzhouContract Err:" + err.Error())
  368. return
  369. }
  370. contractMap := make(map[string]*models.BaseFromTradeGuangzhouContract)
  371. for _, v := range contractList {
  372. contractMap[v.Contract+v.TradeDate] = v
  373. }
  374. for _, cv := range classifyList {
  375. if cv.ClassifyCode != "" {
  376. rn := utils.RangeRand(1, 30)
  377. fmt.Println(cv.ClassifyCode+";sleep:", strconv.Itoa(int(rn)))
  378. time.Sleep(time.Duration(rn) * time.Second)
  379. zzUrl := `http://www.gfex.com.cn/u/interfacesWebTiMemberDealPosiQuotes/loadListContract_id`
  380. zzUrl = fmt.Sprintf(zzUrl)
  381. fmt.Println(zzUrl)
  382. params := "variety=" + cv.ClassifyCode + "&trade_date=" + dateStr
  383. headerParams := make(map[string]string)
  384. headerParams["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
  385. headerParams["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
  386. body, err := utils.HttpPost(zzUrl, params, headerParams)
  387. if err != nil {
  388. fmt.Println("err:", err)
  389. return
  390. }
  391. fmt.Println("body :" + string(body))
  392. utils.FileLog.Info("body:" + string(body))
  393. contractItem := new(models.GuangzhouContract)
  394. err = json.Unmarshal(body, &contractItem)
  395. if err != nil {
  396. fmt.Println("CrawlerFromGuangzhouContract json.Unmarshal err:", err)
  397. return
  398. }
  399. if contractItem.Code == "0" {
  400. for _, dv := range contractItem.Data {
  401. if _, ok := contractMap[dv+dateFormatStr]; !ok {
  402. contractObj := new(models.BaseFromTradeGuangzhouContract)
  403. contractObj.BaseFromTradeGuangzhouClassifyId = cv.BaseFromTradeGuangzhouClassifyId
  404. contractObj.ClassifyCode = cv.ClassifyCode
  405. contractObj.Contract = dv
  406. contractObj.TradeDate = dateFormatStr
  407. err = gzObj.AddBaseFromTradeGuangzhouContract(contractObj)
  408. if err != nil {
  409. fmt.Println("AddBaseFromTradeGuangzhouContract err:", err)
  410. return
  411. }
  412. }
  413. }
  414. } else {
  415. utils.FileLog.Info("CrawlerFromGuangzhouContract Fail:" + string(body))
  416. }
  417. }
  418. }
  419. }
  420. // CrawlerFromGuangzhouDealPosiQuotes 广州交易所期货-日成交持仓排名
  421. func CrawlerFromGuangzhouDealPosiQuotes(date time.Time) {
  422. // 准备: HTTP请求
  423. now := date //time.Now()
  424. if now.Weekday() == time.Saturday || now.Weekday() == time.Sunday {
  425. return
  426. }
  427. dateStr := now.Format(utils.FormatDateUnSpace)
  428. dateStrFormat := now.Format(utils.FormatDate)
  429. gzObj := new(models.BaseFromTradeGuangzhouIndex)
  430. //获取所有指标
  431. indexList, err := gzObj.GetBaseFromTradeGuangzhouIndexAll()
  432. if err != nil {
  433. fmt.Println("GetBaseFromTradeGuangzhouIndexAll Err:" + err.Error())
  434. return
  435. }
  436. indexMap := make(map[string]*models.BaseFromTradeGuangzhouIndex)
  437. for _, iv := range indexList {
  438. indexMap[iv.IndexCode] = iv
  439. }
  440. dataList, err := gzObj.GetBaseFromTradeGuangzhouDataByDate(dateStrFormat)
  441. dataMap := make(map[string]*models.BaseFromTradeGuangzhouData)
  442. for _, dv := range dataList {
  443. dataMap[dv.IndexCode] = dv
  444. }
  445. contractList, err := gzObj.GetBaseFromTradeGuangzhouContract(dateStrFormat)
  446. if err != nil {
  447. fmt.Println("GetBaseFromTradeGuangzhouContract Err:" + err.Error())
  448. return
  449. }
  450. if len(contractList) <= 0 {
  451. fmt.Println("contractList is 0")
  452. return
  453. }
  454. zzUrl := `http://www.gfex.com.cn/u/interfacesWebTiMemberDealPosiQuotes/loadList`
  455. pages := []int{1, 2, 3}
  456. unit := "手"
  457. for _, cv := range contractList {
  458. for _, pv := range pages {
  459. rn := utils.RangeRand(1, 30)
  460. fmt.Println(cv.Contract+";sleep:", strconv.Itoa(int(rn)))
  461. time.Sleep(time.Duration(rn) * time.Second)
  462. params := "trade_date=" + dateStr + "&trade_type=0&variety=" + cv.ClassifyCode + "&contract_id=" + cv.Contract + "&data_type=" + strconv.Itoa(pv)
  463. //params = "trade_date=20231110&trade_type=0&variety=si&contract_id=si2312&data_type=1"
  464. //params = "trade_date=20231110&trade_type=0&variety=si&contract_id=si2402&data_type=1"
  465. fmt.Println("params:", params)
  466. headerParams := make(map[string]string)
  467. headerParams["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
  468. headerParams["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
  469. body, err := utils.HttpPost(zzUrl, params, headerParams)
  470. if err != nil {
  471. fmt.Println("err:", err)
  472. return
  473. }
  474. fmt.Println("body :" + string(body))
  475. utils.FileLog.Info("body:" + string(body))
  476. dealPosiQuotes := new(models.GuangzhouDealPosiQuotes)
  477. err = json.Unmarshal(body, dealPosiQuotes)
  478. if err != nil {
  479. fmt.Println("CrawlerFromGuangzhouDealPosiQuotes json.Unmarshal Err:" + err.Error())
  480. return
  481. }
  482. if dealPosiQuotes.Code == "0" {
  483. addDataList := make([]*models.BaseFromTradeGuangzhouData, 0)
  484. updateDataList := make([]*models.BaseFromTradeGuangzhouData, 0)
  485. for _, v := range dealPosiQuotes.Data {
  486. indexCode := utils.ChineseToPinyin(v.Abbr)
  487. var indexName string
  488. switch pv {
  489. case 1:
  490. indexName = v.Abbr + "_" + cv.Contract + "_" + "成交量"
  491. indexCode = indexCode + cv.Contract + "deal"
  492. if v.ContractId == "总计" {
  493. indexCode = utils.ChineseToPinyinInitials("日成交持仓排名总计") + cv.Contract + "deal"
  494. indexName = "日成交持仓排名_" + cv.Contract + "_成交量总计"
  495. }
  496. case 2:
  497. indexName = v.Abbr + "_" + cv.Contract + "_" + "持买单量"
  498. indexCode = indexCode + cv.Contract + "buy"
  499. if v.ContractId == "总计" {
  500. indexCode = utils.ChineseToPinyinInitials("日成交持仓排名总计") + cv.Contract + "buy"
  501. indexName = "日成交持仓排名_" + cv.Contract + "_持买单量总计"
  502. }
  503. case 3:
  504. indexName = v.Abbr + "_" + cv.Contract + "_" + "持卖单量"
  505. indexCode = indexCode + cv.Contract + "sold"
  506. if v.ContractId == "总计" {
  507. indexCode = utils.ChineseToPinyinInitials("日成交持仓排名总计") + cv.Contract + "sold"
  508. indexName = "日成交持仓排名_" + cv.Contract + "_持卖单量总计"
  509. }
  510. }
  511. var indexId int
  512. if indexItem, ok := indexMap[indexCode]; !ok {
  513. gzObj = &models.BaseFromTradeGuangzhouIndex{
  514. BaseFromTradeGuangzhouClassifyId: cv.BaseFromTradeGuangzhouClassifyId,
  515. BaseFromTradeGuangzhouContractId: cv.BaseFromTradeGuangzhouContractId,
  516. IndexCode: indexCode,
  517. IndexName: indexName,
  518. Frequency: "日度",
  519. Unit: unit,
  520. CreateTime: time.Now(),
  521. ModifyTime: time.Now(),
  522. }
  523. lastId, err := gzObj.AddBaseFromTradeGuangzhouIndex()
  524. if err != nil {
  525. fmt.Println("AddBaseFromTradeGuangzhouIndex Err:" + err.Error())
  526. return
  527. }
  528. indexId = int(lastId)
  529. } else {
  530. indexId = indexItem.BaseFromTradeGuangzhouIndexId
  531. }
  532. //判断数据是否存在
  533. if dataItem, ok := dataMap[indexCode]; !ok {
  534. dataItem = new(models.BaseFromTradeGuangzhouData)
  535. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  536. dataItem.IndexCode = indexCode
  537. dataItem.DataTime = dateStr
  538. dataItem.Value = float64(v.TodayQty)
  539. dataItem.QtySub = float64(v.QtySub)
  540. dataItem.CreateTime = time.Now()
  541. dataItem.ModifyTime = time.Now()
  542. addDataList = append(addDataList, dataItem)
  543. } else {
  544. if dataItem.Value != float64(v.TodayQty) {
  545. dataItem.BaseFromTradeGuangzhouDataId = dataItem.BaseFromTradeGuangzhouDataId
  546. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  547. dataItem.IndexCode = indexCode
  548. dataItem.DataTime = dateStr
  549. dataItem.Value = float64(v.TodayQty)
  550. dataItem.QtySub = float64(v.QtySub)
  551. dataItem.ModifyTime = time.Now()
  552. updateDataList = append(updateDataList, dataItem)
  553. }
  554. }
  555. }
  556. fmt.Println("addDataList:", len(addDataList))
  557. if len(addDataList) > 0 {
  558. err = gzObj.AddBaseFromTradeGuangzhouData(addDataList)
  559. if err != nil {
  560. fmt.Println("AddBaseFromTradeGuangzhouData Err:" + err.Error())
  561. return
  562. }
  563. }
  564. if len(updateDataList) > 0 {
  565. for _, v := range updateDataList {
  566. updateParams := make(map[string]interface{})
  567. whereParam := make(map[string]interface{})
  568. updateParams["value"] = v.Value
  569. updateParams["qty_sub"] = v.QtySub
  570. updateParams["modify_time"] = time.Now()
  571. whereParam["base_from_trade_guangzhou_data_id"] = v.BaseFromTradeGuangzhouDataId
  572. err = gzObj.UpdateGuangzhouData(updateParams, whereParam)
  573. if err != nil {
  574. fmt.Println("UpdateGuangzhouData Err:" + err.Error())
  575. return
  576. }
  577. }
  578. }
  579. }
  580. }
  581. }
  582. }
  583. // CrawlerFromGuangzhouWbillWeeklyQuotes 广州交易所期货-仓单日报
  584. func CrawlerFromGuangzhouWbillWeeklyQuotes(date time.Time) {
  585. now := date //time.Now()
  586. if now.Weekday() == time.Saturday || now.Weekday() == time.Sunday {
  587. return
  588. }
  589. dateStr := now.Format(utils.FormatDateUnSpace)
  590. dateStrFormat := now.Format(utils.FormatDate)
  591. gzObj := new(models.BaseFromTradeGuangzhouIndex)
  592. parentId := 3
  593. classifyList, err := gzObj.GetBaseFromTradeGuangzhouClassifyAll(parentId)
  594. if err != nil {
  595. fmt.Println("GetBaseFromTradeGuangzhouClassify Err:" + err.Error())
  596. return
  597. }
  598. classifyMap := make(map[string]*models.BaseFromTradeGuangzhouClassify)
  599. for _, cv := range classifyList {
  600. if cv.ClassifyCode != "" {
  601. classifyMap[cv.ClassifyCode] = cv
  602. }
  603. }
  604. //获取所有指标
  605. indexList, err := gzObj.GetBaseFromTradeGuangzhouIndexAll()
  606. if err != nil {
  607. fmt.Println("GetBaseFromTradeGuangzhouIndexAll Err:" + err.Error())
  608. return
  609. }
  610. indexMap := make(map[string]*models.BaseFromTradeGuangzhouIndex)
  611. for _, iv := range indexList {
  612. indexMap[iv.IndexCode] = iv
  613. }
  614. dataList, err := gzObj.GetBaseFromTradeGuangzhouDataByDate(dateStrFormat)
  615. dataMap := make(map[string]*models.BaseFromTradeGuangzhouData)
  616. for _, dv := range dataList {
  617. dataMap[dv.IndexCode] = dv
  618. }
  619. zzUrl := `http://www.gfex.com.cn/u/interfacesWebTdWbillWeeklyQuotes/loadList`
  620. params := "gen_date=" + dateStr
  621. fmt.Println("params:", params)
  622. headerParams := make(map[string]string)
  623. headerParams["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
  624. headerParams["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
  625. body, err := utils.HttpPost(zzUrl, params, headerParams)
  626. if err != nil {
  627. fmt.Println("err:", err)
  628. return
  629. }
  630. fmt.Println("body :" + string(body))
  631. utils.FileLog.Info("body:" + string(body))
  632. wbillQty := new(models.GuangzhouWbillQty)
  633. err = json.Unmarshal(body, wbillQty)
  634. if err != nil {
  635. fmt.Println("CrawlerFromGuangzhouWbillWeeklyQuotes json.Unmarshal Err:" + err.Error())
  636. return
  637. }
  638. if wbillQty.Code == "0" {
  639. addDataList := make([]*models.BaseFromTradeGuangzhouData, 0)
  640. updateDataList := make([]*models.BaseFromTradeGuangzhouData, 0)
  641. for _, v := range wbillQty.Data {
  642. if v.Variety == "总计" {
  643. continue
  644. }
  645. if !strings.Contains(v.Variety, "小计") {
  646. indexCode := utils.ChineseToPinyinInitials(v.WhAbbr)
  647. if indexCode == "" {
  648. continue
  649. }
  650. indexCode = v.VarietyOrder + indexCode
  651. indexCode += "cdl"
  652. indexName := v.Variety + v.WhAbbr + "仓单量"
  653. var indexId int
  654. if indexItem, ok := indexMap[indexCode]; !ok {
  655. var classifyId int
  656. if classifyItem, ok := classifyMap[v.VarietyOrder]; !ok {
  657. classifyItem = new(models.BaseFromTradeGuangzhouClassify)
  658. classifyItem.ClassifyCode = v.VarietyOrder
  659. classifyItem.ClassifyName = v.Variety
  660. classifyItem.ParentId = 3
  661. classifyItem.ModifyTime = time.Now()
  662. classifyItem.CreateTime = time.Now()
  663. classifyLastId, err := gzObj.AddBaseFromTradeGuangzhouClassify(classifyItem)
  664. if err != nil {
  665. fmt.Println("AddBaseFromTradeGuangzhouClassify err:" + err.Error())
  666. return
  667. }
  668. classifyId = int(classifyLastId)
  669. } else {
  670. classifyId = classifyItem.BaseFromTradeGuangzhouClassifyId
  671. }
  672. gzObj = &models.BaseFromTradeGuangzhouIndex{
  673. BaseFromTradeGuangzhouClassifyId: classifyId,
  674. IndexCode: indexCode,
  675. IndexName: indexName,
  676. Frequency: "日度",
  677. Unit: "手",
  678. CreateTime: time.Now(),
  679. ModifyTime: time.Now(),
  680. }
  681. lastId, err := gzObj.AddBaseFromTradeGuangzhouIndex()
  682. if err != nil {
  683. fmt.Println("AddBaseFromTradeGuangzhouIndex Err:" + err.Error())
  684. return
  685. }
  686. indexId = int(lastId)
  687. } else {
  688. indexId = indexItem.BaseFromTradeGuangzhouIndexId
  689. }
  690. //判断数据是否存在
  691. if dataItem, ok := dataMap[indexCode]; !ok {
  692. dataItem = new(models.BaseFromTradeGuangzhouData)
  693. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  694. dataItem.IndexCode = indexCode
  695. dataItem.DataTime = dateStr
  696. dataItem.Value = float64(v.WbillQty)
  697. dataItem.QtySub = float64(v.RegWbillQty)
  698. dataItem.CreateTime = time.Now()
  699. dataItem.ModifyTime = time.Now()
  700. addDataList = append(addDataList, dataItem)
  701. } else {
  702. if dataItem.Value != float64(v.WbillQty) {
  703. dataItem.BaseFromTradeGuangzhouDataId = dataItem.BaseFromTradeGuangzhouDataId
  704. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  705. dataItem.IndexCode = indexCode
  706. dataItem.DataTime = dateStr
  707. dataItem.Value = float64(v.WbillQty)
  708. dataItem.QtySub = float64(v.RegWbillQty)
  709. dataItem.ModifyTime = time.Now()
  710. updateDataList = append(updateDataList, dataItem)
  711. }
  712. }
  713. } else {
  714. indexCode := utils.ChineseToPinyinInitials("仓单日报_" + v.Variety)
  715. indexCode = v.VarietyOrder + indexCode
  716. indexCode += "cdl"
  717. indexName := "仓单日报_" + v.Variety + "_仓单量"
  718. var indexId int
  719. if indexItem, ok := indexMap[indexCode]; !ok {
  720. var classifyId int
  721. if classifyItem, ok := classifyMap[v.VarietyOrder]; !ok {
  722. classifyItem = new(models.BaseFromTradeGuangzhouClassify)
  723. classifyItem.ClassifyCode = v.VarietyOrder
  724. classifyItem.ClassifyName = v.Variety
  725. classifyItem.ParentId = 3
  726. classifyItem.ModifyTime = time.Now()
  727. classifyItem.CreateTime = time.Now()
  728. classifyLastId, err := gzObj.AddBaseFromTradeGuangzhouClassify(classifyItem)
  729. if err != nil {
  730. fmt.Println("AddBaseFromTradeGuangzhouClassify err:" + err.Error())
  731. return
  732. }
  733. classifyId = int(classifyLastId)
  734. } else {
  735. classifyId = classifyItem.BaseFromTradeGuangzhouClassifyId
  736. }
  737. gzObj = &models.BaseFromTradeGuangzhouIndex{
  738. BaseFromTradeGuangzhouClassifyId: classifyId,
  739. IndexCode: indexCode,
  740. IndexName: indexName,
  741. Frequency: "日度",
  742. Unit: "手",
  743. CreateTime: time.Now(),
  744. ModifyTime: time.Now(),
  745. }
  746. lastId, err := gzObj.AddBaseFromTradeGuangzhouIndex()
  747. if err != nil {
  748. fmt.Println("AddBaseFromTradeGuangzhouIndex Err:" + err.Error())
  749. return
  750. }
  751. indexId = int(lastId)
  752. } else {
  753. indexId = indexItem.BaseFromTradeGuangzhouIndexId
  754. }
  755. //判断数据是否存在
  756. if dataItem, ok := dataMap[indexCode]; !ok {
  757. dataItem = new(models.BaseFromTradeGuangzhouData)
  758. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  759. dataItem.IndexCode = indexCode
  760. dataItem.DataTime = dateStr
  761. dataItem.Value = float64(v.WbillQty)
  762. dataItem.QtySub = float64(v.RegWbillQty)
  763. dataItem.CreateTime = time.Now()
  764. dataItem.ModifyTime = time.Now()
  765. addDataList = append(addDataList, dataItem)
  766. } else {
  767. if dataItem.Value != float64(v.WbillQty) {
  768. dataItem.BaseFromTradeGuangzhouDataId = dataItem.BaseFromTradeGuangzhouDataId
  769. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  770. dataItem.IndexCode = indexCode
  771. dataItem.DataTime = dateStr
  772. dataItem.Value = float64(v.WbillQty)
  773. dataItem.QtySub = float64(v.RegWbillQty)
  774. dataItem.ModifyTime = time.Now()
  775. updateDataList = append(updateDataList, dataItem)
  776. }
  777. }
  778. }
  779. }
  780. fmt.Println("addDataList:", len(addDataList))
  781. if len(addDataList) > 0 {
  782. err = gzObj.AddBaseFromTradeGuangzhouData(addDataList)
  783. if err != nil {
  784. fmt.Println("AddBaseFromTradeGuangzhouData Err:" + err.Error())
  785. return
  786. }
  787. }
  788. if len(updateDataList) > 0 {
  789. for _, v := range updateDataList {
  790. updateParams := make(map[string]interface{})
  791. whereParam := make(map[string]interface{})
  792. updateParams["value"] = v.Value
  793. updateParams["qty_sub"] = v.QtySub
  794. updateParams["modify_time"] = time.Now()
  795. whereParam["base_from_trade_guangzhou_data_id"] = v.BaseFromTradeGuangzhouDataId
  796. err = gzObj.UpdateGuangzhouData(updateParams, whereParam)
  797. if err != nil {
  798. fmt.Println("UpdateGuangzhouData Err:" + err.Error())
  799. return
  800. }
  801. }
  802. }
  803. }
  804. }
  805. // CrawlerFromGuangzhouCongregateDeliveryQuotes 广州交易所期货-一次性交割卖方仓单查询
  806. func CrawlerFromGuangzhouCongregateDeliveryQuotes(date time.Time) {
  807. now := date //time.Now()
  808. dateStr := now.Format(utils.FormatMonthDateUnSpace)
  809. _, monthEndDay := utils.GetMonthStartAndEnd(strconv.Itoa(now.Year()), strconv.Itoa(int(now.Month())))
  810. gzObj := new(models.BaseFromTradeGuangzhouIndex)
  811. parentId := 4
  812. classifyList, err := gzObj.GetBaseFromTradeGuangzhouClassifyAll(parentId)
  813. if err != nil {
  814. fmt.Println("GetBaseFromTradeGuangzhouClassify Err:" + err.Error())
  815. return
  816. }
  817. classifyMap := make(map[string]*models.BaseFromTradeGuangzhouClassify)
  818. for _, cv := range classifyList {
  819. if cv.ClassifyCode != "" {
  820. classifyMap[cv.ClassifyCode] = cv
  821. }
  822. }
  823. //获取所有指标
  824. indexList, err := gzObj.GetBaseFromTradeGuangzhouIndexAll()
  825. if err != nil {
  826. fmt.Println("GetBaseFromTradeGuangzhouIndexAll Err:" + err.Error())
  827. return
  828. }
  829. indexMap := make(map[string]*models.BaseFromTradeGuangzhouIndex)
  830. for _, iv := range indexList {
  831. indexMap[iv.IndexCode] = iv
  832. }
  833. dataList, err := gzObj.GetBaseFromTradeGuangzhouDataByDate(monthEndDay)
  834. dataMap := make(map[string]*models.BaseFromTradeGuangzhouData)
  835. for _, dv := range dataList {
  836. dataMap[dv.IndexCode] = dv
  837. }
  838. zzUrl := `http://www.gfex.com.cn/u/interfacesWebTcCongregateDeliveryQuotes/loadList`
  839. params := "contract_month=" + dateStr
  840. fmt.Println("params:", params)
  841. headerParams := make(map[string]string)
  842. headerParams["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
  843. headerParams["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
  844. body, err := utils.HttpPost(zzUrl, params, headerParams)
  845. if err != nil {
  846. fmt.Println("err:", err)
  847. return
  848. }
  849. fmt.Println("body :" + string(body))
  850. utils.FileLog.Info("body:" + string(body))
  851. deliveryQuotes := new(models.GuangzhouCongregateDeliveryQuotes)
  852. err = json.Unmarshal(body, deliveryQuotes)
  853. if err != nil {
  854. fmt.Println("CrawlerFromGuangzhouCongregateDeliveryQuotes json.Unmarshal Err:" + err.Error())
  855. return
  856. }
  857. if deliveryQuotes.Code == "0" {
  858. addDataList := make([]*models.BaseFromTradeGuangzhouData, 0)
  859. updateDataList := make([]*models.BaseFromTradeGuangzhouData, 0)
  860. if len(deliveryQuotes.Data) <= 0 {
  861. return
  862. }
  863. varietyId := deliveryQuotes.Data[0].VarietyId
  864. varietyName := deliveryQuotes.Data[0].VarietyName
  865. totalIndexCode := varietyId + utils.ChineseToPinyinInitials("一次性交割卖方仓单合计")
  866. totalIndexName := varietyName + "一次性交割卖方仓单合计"
  867. total := 0
  868. for _, v := range deliveryQuotes.Data {
  869. total += v.WbillNum
  870. }
  871. var indexId int
  872. if indexItem, ok := indexMap[totalIndexCode]; !ok {
  873. var classifyId int
  874. if classifyItem, ok := classifyMap[varietyId]; !ok {
  875. classifyItem = new(models.BaseFromTradeGuangzhouClassify)
  876. classifyItem.ClassifyCode = varietyId
  877. classifyItem.ClassifyName = varietyName
  878. classifyItem.ParentId = parentId
  879. classifyItem.ModifyTime = time.Now()
  880. classifyItem.CreateTime = time.Now()
  881. classifyLastId, err := gzObj.AddBaseFromTradeGuangzhouClassify(classifyItem)
  882. if err != nil {
  883. fmt.Println("AddBaseFromTradeGuangzhouClassify err:" + err.Error())
  884. return
  885. }
  886. classifyId = int(classifyLastId)
  887. } else {
  888. classifyId = classifyItem.BaseFromTradeGuangzhouClassifyId
  889. }
  890. gzObj = &models.BaseFromTradeGuangzhouIndex{
  891. BaseFromTradeGuangzhouClassifyId: classifyId,
  892. IndexCode: totalIndexCode,
  893. IndexName: totalIndexName,
  894. Frequency: "月度",
  895. Unit: "手",
  896. CreateTime: time.Now(),
  897. ModifyTime: time.Now(),
  898. }
  899. lastId, err := gzObj.AddBaseFromTradeGuangzhouIndex()
  900. if err != nil {
  901. fmt.Println("AddBaseFromTradeGuangzhouIndex Err:" + err.Error())
  902. return
  903. }
  904. indexId = int(lastId)
  905. } else {
  906. indexId = indexItem.BaseFromTradeGuangzhouIndexId
  907. }
  908. //判断数据是否存在
  909. if dataItem, ok := dataMap[totalIndexCode]; !ok {
  910. dataItem = new(models.BaseFromTradeGuangzhouData)
  911. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  912. dataItem.IndexCode = totalIndexCode
  913. dataItem.DataTime = monthEndDay
  914. dataItem.Value = float64(total)
  915. dataItem.CreateTime = time.Now()
  916. dataItem.ModifyTime = time.Now()
  917. addDataList = append(addDataList, dataItem)
  918. } else {
  919. if dataItem.Value != float64(total) {
  920. dataItem.BaseFromTradeGuangzhouDataId = dataItem.BaseFromTradeGuangzhouDataId
  921. dataItem.BaseFromTradeGuangzhouIndexId = indexId
  922. dataItem.IndexCode = totalIndexCode
  923. dataItem.DataTime = monthEndDay
  924. dataItem.Value = float64(total)
  925. dataItem.ModifyTime = time.Now()
  926. updateDataList = append(updateDataList, dataItem)
  927. }
  928. }
  929. fmt.Println("addDataList:", len(addDataList))
  930. if len(addDataList) > 0 {
  931. err = gzObj.AddBaseFromTradeGuangzhouData(addDataList)
  932. if err != nil {
  933. fmt.Println("AddBaseFromTradeGuangzhouData Err:" + err.Error())
  934. return
  935. }
  936. }
  937. if len(updateDataList) > 0 {
  938. for _, v := range updateDataList {
  939. updateParams := make(map[string]interface{})
  940. whereParam := make(map[string]interface{})
  941. updateParams["value"] = v.Value
  942. updateParams["qty_sub"] = v.QtySub
  943. updateParams["modify_time"] = time.Now()
  944. whereParam["base_from_trade_guangzhou_data_id"] = v.BaseFromTradeGuangzhouDataId
  945. err = gzObj.UpdateGuangzhouData(updateParams, whereParam)
  946. if err != nil {
  947. fmt.Println("UpdateGuangzhouData Err:" + err.Error())
  948. return
  949. }
  950. }
  951. }
  952. //处理明细
  953. addDataList = make([]*models.BaseFromTradeGuangzhouData, 0)
  954. updateDataList = make([]*models.BaseFromTradeGuangzhouData, 0)
  955. for _, v := range deliveryQuotes.Data {
  956. var subIndexId int
  957. indexCode = v.ContractId + utils.ChineseToPinyinInitials(v.WhName) + "cdl"
  958. if indexItem, ok := indexMap[indexCode]; !ok {
  959. gzObj = &models.BaseFromTradeGuangzhouIndex{
  960. BaseFromTradeGuangzhouClassifyId: indexId,
  961. IndexCode: indexCode,
  962. IndexName: v.WhName,
  963. Frequency: "月度",
  964. Unit: "手",
  965. CreateTime: time.Now(),
  966. ModifyTime: time.Now(),
  967. Value: float64(v.WbillNum),
  968. StartDate: monthEndDay,
  969. EndDate: monthEndDay,
  970. }
  971. lastId, err := gzObj.AddBaseFromTradeGuangzhouIndex()
  972. if err != nil {
  973. fmt.Println("AddBaseFromTradeGuangzhouIndex Err:" + err.Error())
  974. return
  975. }
  976. subIndexId = int(lastId)
  977. } else {
  978. subIndexId = indexItem.BaseFromTradeGuangzhouIndexId
  979. }
  980. //判断数据是否存在
  981. if dataItem, ok := dataMap[indexCode]; !ok {
  982. dataItem = new(models.BaseFromTradeGuangzhouData)
  983. dataItem.BaseFromTradeGuangzhouIndexId = subIndexId
  984. dataItem.IndexCode = indexCode
  985. dataItem.DataTime = monthEndDay
  986. dataItem.Value = float64(total)
  987. dataItem.CreateTime = time.Now()
  988. dataItem.ModifyTime = time.Now()
  989. addDataList = append(addDataList, dataItem)
  990. } else {
  991. if dataItem.Value != float64(total) {
  992. dataItem.BaseFromTradeGuangzhouDataId = dataItem.BaseFromTradeGuangzhouDataId
  993. dataItem.BaseFromTradeGuangzhouIndexId = subIndexId
  994. dataItem.IndexCode = indexCode
  995. dataItem.DataTime = monthEndDay
  996. dataItem.Value = float64(total)
  997. dataItem.ModifyTime = time.Now()
  998. updateDataList = append(updateDataList, dataItem)
  999. }
  1000. }
  1001. }
  1002. if len(addDataList) > 0 {
  1003. err = gzObj.AddBaseFromTradeGuangzhouData(addDataList)
  1004. if err != nil {
  1005. fmt.Println("AddBaseFromTradeGuangzhouData Err:" + err.Error())
  1006. return
  1007. }
  1008. }
  1009. if len(updateDataList) > 0 {
  1010. for _, v := range updateDataList {
  1011. updateParams := make(map[string]interface{})
  1012. whereParam := make(map[string]interface{})
  1013. updateParams["value"] = v.Value
  1014. updateParams["qty_sub"] = v.QtySub
  1015. updateParams["modify_time"] = time.Now()
  1016. whereParam["base_from_trade_guangzhou_data_id"] = v.BaseFromTradeGuangzhouDataId
  1017. err = gzObj.UpdateGuangzhouData(updateParams, whereParam)
  1018. if err != nil {
  1019. fmt.Println("UpdateGuangzhouData Err:" + err.Error())
  1020. return
  1021. }
  1022. }
  1023. }
  1024. }
  1025. }
  1026. // CrawlerFromGuangzhouIndexMinAndMaxDate 广州交易所期货-处理指标开始日期和结束日期
  1027. func CrawlerFromGuangzhouIndexMinAndMaxDate() {
  1028. gzObj := new(models.BaseFromTradeGuangzhouIndex)
  1029. //获取所有指标
  1030. indexList, err := gzObj.GetBaseFromTradeGuangzhouIndexAll()
  1031. if err != nil {
  1032. fmt.Println("GetBaseFromTradeGuangzhouIndexAll Err:" + err.Error())
  1033. return
  1034. }
  1035. indexMap := make(map[int]*models.BaseFromTradeGuangzhouIndex)
  1036. for _, iv := range indexList {
  1037. indexMap[iv.BaseFromTradeGuangzhouIndexId] = iv
  1038. }
  1039. //获取数据开始和结束日期
  1040. dataDateList, err := gzObj.GetBaseFromTradeGuangzhouDataMinAndMaxDate()
  1041. if err != nil {
  1042. fmt.Println("GetBaseFromTradeGuangzhouDataMinAndMaxDate Err:" + err.Error())
  1043. return
  1044. }
  1045. for _, v := range dataDateList {
  1046. if indexItem, ok := indexMap[v.BaseFromTradeGuangzhouIndexId]; ok {
  1047. updateParams := make(map[string]interface{})
  1048. whereParam := make(map[string]interface{})
  1049. if indexItem.StartDate != v.StartDate {
  1050. updateParams["start_date"] = v.StartDate
  1051. }
  1052. if indexItem.EndDate != v.EndDate {
  1053. updateParams["end_date"] = v.EndDate
  1054. updateParams["modify_time"] = time.Now()
  1055. whereParam["base_from_trade_guangzhou_index_id"] = v.BaseFromTradeGuangzhouIndexId
  1056. err = gzObj.UpdateGuangzhouIndex(updateParams, whereParam)
  1057. if err != nil {
  1058. fmt.Println("UpdateGuangzhouData Err:" + err.Error())
  1059. return
  1060. }
  1061. }
  1062. }
  1063. }
  1064. }
  1065. //http://www.gfex.com.cn/u/interfacesWebTiMemberDealPosiQuotes/loadList