commodity_trade_zhengzhou.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package services
  2. import (
  3. "eta/eta_crawler/models"
  4. "eta/eta_crawler/utils"
  5. "fmt"
  6. "github.com/mozillazg/go-pinyin"
  7. "log"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/PuerkitoBio/goquery"
  12. )
  13. var zIndexCode string
  14. var zIndexCodeMap = make(map[string]string)
  15. var zActionCodeMap = make(map[string]map[string]int)
  16. func zIndexCodeGenerator(shortName, indexName, contractCode, suffix string) string {
  17. if shortName == "" || shortName == "-" {
  18. return ""
  19. }
  20. strResult := ""
  21. if shortName != "top20" {
  22. //取公司全拼
  23. a := pinyin.NewArgs()
  24. rows := pinyin.LazyPinyin(shortName, a)
  25. for i := 0; i < len(rows); i++ {
  26. strResult += rows[i]
  27. }
  28. } else {
  29. strResult = "top20"
  30. }
  31. //郑州特殊处理,当合约号有中文时只取英文代号 如 苹果AP ---> AP
  32. if len(contractCode) > 7 {
  33. contractCode = contractCode[len(contractCode)-2:]
  34. }
  35. zIndexCode, _ := zIndexCodeMap[indexName]
  36. if zIndexCode == "" {
  37. zIndexCode = strResult + contractCode + suffix
  38. zIndexCodeMap[indexName] = zIndexCode
  39. err := models.AddBaseFromTradeMapping(indexName, zIndexCode, "Z")
  40. if err != nil {
  41. fmt.Println("add Code err:", err)
  42. }
  43. }
  44. return zIndexCode
  45. }
  46. // 郑州商品交易所持单排名
  47. func SyncRankingFromZhengzhou() {
  48. utils.FileLog.Info("SyncRankingFromZhengzhou start:" + time.Now().Format(utils.FormatDateTime))
  49. n := utils.GetRandInt(10, 120)
  50. time.Sleep(time.Duration(n) * time.Second)
  51. fmt.Println("start")
  52. var err error
  53. defer func() {
  54. if err != nil {
  55. fmt.Println("Err:" + err.Error())
  56. }
  57. }()
  58. allCode, err := models.GetIndexCodeFromMapping("Z")
  59. if err != nil {
  60. fmt.Println("select Code err:", err)
  61. }
  62. for _, item := range allCode {
  63. zIndexCodeMap[item.IndexName] = item.IndexCode
  64. }
  65. for i := 2; i >= 0; i-- {
  66. var itemVerifyCode int
  67. zzUrl := "http://www.czce.com.cn/cn/DFSStaticFiles/Future/%s/%s/FutureDataHolding.htm"
  68. date := time.Now().AddDate(0, 0, -i)
  69. year := date.Year()
  70. dateStr := date.Format(utils.FormatDateUnSpace)
  71. zzUrl = fmt.Sprintf(zzUrl, strconv.Itoa(year), dateStr)
  72. fmt.Println(zzUrl)
  73. bodyStr, err := getSourceZhengZhou(zzUrl)
  74. if err != nil {
  75. fmt.Println("GetData Err:" + err.Error())
  76. return
  77. }
  78. //获取所有指标信息 某一天的
  79. allIndex, err := models.GetBaseFromTradeZhengzhouIndexAll(dateStr)
  80. if err != nil {
  81. return
  82. }
  83. existIndexMap := make(map[string]*models.BaseFromTradeZhengzhouIndex)
  84. for _, v := range allIndex {
  85. indexKey := v.DealName + v.BuyName + v.SoldName
  86. existIndexMap[indexKey] = v
  87. }
  88. utils.FileLog.Info(bodyStr)
  89. if strings.Contains(bodyStr, "出错") {
  90. continue
  91. }
  92. doc, err := goquery.NewDocumentFromReader(strings.NewReader(bodyStr))
  93. if err != nil {
  94. log.Fatal(err)
  95. }
  96. table := doc.Find("table")
  97. var classifyName, classifyType, tradeDate string
  98. var dealSuffix, buySuffix, sellSuffix string
  99. var dealVal, dealAddCutVal, buyVal, buyAddCutVal, sellVal, sellAddCutVal string
  100. var rank string
  101. table.Find("tr").Each(func(i int, tr *goquery.Selection) {
  102. var memberShortNameArr []string
  103. tds := tr.Find("td")
  104. if tds.Length() == 1 {
  105. tdText := tds.Text()
  106. utils.FileLog.Info(tdText)
  107. if tdText != "" {
  108. tdTextArr := strings.Split(tdText, "    ")
  109. for k, v := range tdTextArr {
  110. //fmt.Println(k, v)
  111. if k == 0 {
  112. classifyName = v
  113. } else {
  114. tradeDate = v
  115. }
  116. }
  117. }
  118. } else {
  119. tds.Each(func(tk int, td *goquery.Selection) {
  120. tdText := td.Text()
  121. tdText = strings.Trim(tdText, " ")
  122. if tdText == "" {
  123. return
  124. }
  125. //fmt.Println("tdText: ", tdText)
  126. if tk == 0 {
  127. if !(strings.Contains(tdText, "名次") || strings.Contains(tdText, "合计")) {
  128. rank = tdText
  129. }
  130. }
  131. if tk == 1 {
  132. if !strings.Contains(tdText, "会员简称") {
  133. tdText = strings.Replace(tdText, "(代客)", "", -1)
  134. memberShortNameArr = append(memberShortNameArr, tdText)
  135. }
  136. }
  137. if tk == 4 {
  138. if !strings.Contains(tdText, "会员简称") {
  139. tdText = strings.Replace(tdText, "(代客)", "", -1)
  140. memberShortNameArr = append(memberShortNameArr, tdText)
  141. }
  142. }
  143. if tk == 7 {
  144. if !strings.Contains(tdText, "会员简称") {
  145. tdText = strings.Replace(tdText, "(代客)", "", -1)
  146. memberShortNameArr = append(memberShortNameArr, tdText)
  147. }
  148. }
  149. if tk == 2 {
  150. if strings.Contains(tdText, "成交量") {
  151. dealSuffix = tdText
  152. } else {
  153. dealVal = tdText
  154. }
  155. }
  156. if tk == 3 {
  157. if strings.Contains(tdText, "增减量") {
  158. //dealAddCutSuffix = tdText
  159. } else {
  160. dealAddCutVal = tdText
  161. }
  162. }
  163. if tk == 5 {
  164. if strings.Contains(tdText, "持买仓量") {
  165. buySuffix = tdText
  166. } else {
  167. buyVal = tdText
  168. }
  169. }
  170. if tk == 6 {
  171. if strings.Contains(tdText, "增减量") {
  172. //buyAddCutSuffix = tdText
  173. } else {
  174. buyAddCutVal = tdText
  175. }
  176. }
  177. if tk == 8 {
  178. if strings.Contains(tdText, "持卖仓量") {
  179. sellSuffix = tdText
  180. } else {
  181. sellVal = tdText
  182. }
  183. }
  184. if tk == 9 {
  185. if strings.Contains(tdText, "增减量") {
  186. //sellAddCutSuffix = tdText
  187. } else {
  188. sellAddCutVal = tdText
  189. }
  190. }
  191. })
  192. //处理指标
  193. dealVal = strings.Replace(dealVal, ",", "", -1)
  194. dealAddCutVal = strings.Replace(dealAddCutVal, ",", "", -1)
  195. buyVal = strings.Replace(buyVal, ",", "", -1)
  196. buyAddCutVal = strings.Replace(buyAddCutVal, ",", "", -1)
  197. sellVal = strings.Replace(sellVal, ",", "", -1)
  198. sellAddCutVal = strings.Replace(sellAddCutVal, ",", "", -1)
  199. tradeDate = strings.Replace(tradeDate, "日期:", "", -1)
  200. classifyNameArr := strings.Split(classifyName, ":")
  201. if len(classifyNameArr) > 1 {
  202. classifyType = classifyNameArr[0]
  203. classifyName = classifyNameArr[1]
  204. }
  205. //fmt.Printf("memberShortNameArr:%s,len:%d\n", memberShortNameArr, len(memberShortNameArr))
  206. if len(memberShortNameArr) != 0 {
  207. if len(memberShortNameArr[0]) != 2 {
  208. if len(classifyName) > 7 {
  209. classifyName = classifyName[len(classifyName)-2:]
  210. }
  211. item := new(models.BaseFromTradeZhengzhouIndex)
  212. item.Rank, _ = strconv.Atoi(rank)
  213. item.DealShortName = memberShortNameArr[0]
  214. item.DealName = memberShortNameArr[0] + "_" + classifyName + "_" + dealSuffix
  215. item.DealCode = zIndexCodeGenerator(item.DealShortName, item.DealName, classifyName, "deal")
  216. item.DealValue, _ = strconv.Atoi(dealVal)
  217. item.DealChange, _ = strconv.Atoi(dealAddCutVal)
  218. item.BuyShortName = memberShortNameArr[1]
  219. item.BuyName = memberShortNameArr[1] + "_" + classifyName + "_" + buySuffix
  220. item.BuyCode = zIndexCodeGenerator(item.BuyShortName, item.BuyName, classifyName, "buy")
  221. item.BuyValue, _ = strconv.Atoi(buyVal)
  222. item.BuyChange, _ = strconv.Atoi(buyAddCutVal)
  223. item.SoldShortName = memberShortNameArr[2]
  224. item.SoldName = memberShortNameArr[2] + "_" + classifyName + "_" + sellSuffix
  225. item.SoldCode = zIndexCodeGenerator(item.SoldShortName, item.SoldName, classifyName, "sold")
  226. item.SoldValue, _ = strconv.Atoi(sellVal)
  227. item.SoldChange, _ = strconv.Atoi(sellAddCutVal)
  228. item.Frequency = "日度"
  229. item.ClassifyName = classifyName
  230. item.ClassifyType = classifyType
  231. item.CreateTime = time.Now()
  232. item.ModifyTime = time.Now()
  233. item.DataTime = tradeDate
  234. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  235. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  236. newID, err := models.AddBaseFromTradeZhengzhouIndex(item)
  237. if err != nil {
  238. fmt.Println("insert error:", err)
  239. }
  240. existIndexMap[item.DealName+item.BuyName+item.SoldName] = item
  241. fmt.Println("insert new indexID:", newID)
  242. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  243. //更新
  244. err := models.ModifyBaseFromTradeZhengzhouIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeZhengzhouIndexId)
  245. if err != nil {
  246. fmt.Println("data update err:", err)
  247. }
  248. }
  249. } else {
  250. //合计
  251. if len(classifyName) > 7 {
  252. classifyName = classifyName[len(classifyName)-2:]
  253. }
  254. item := new(models.BaseFromTradeZhengzhouIndex)
  255. item.Rank = 999
  256. item.DealShortName = ""
  257. item.DealName = "top20" + "_" + classifyName + "_" + dealSuffix
  258. item.DealCode = zIndexCodeGenerator("top20", item.DealName, classifyName, "deal")
  259. item.DealValue, _ = strconv.Atoi(dealVal)
  260. item.DealChange, _ = strconv.Atoi(dealAddCutVal)
  261. item.BuyShortName = ""
  262. item.BuyName = "top20" + "_" + classifyName + "_" + buySuffix
  263. item.BuyCode = zIndexCodeGenerator("top20", item.BuyName, classifyName, "buy")
  264. item.BuyValue, _ = strconv.Atoi(buyVal)
  265. item.BuyChange, _ = strconv.Atoi(buyAddCutVal)
  266. item.SoldShortName = ""
  267. item.SoldName = "top20" + "_" + classifyName + "_" + sellSuffix
  268. item.SoldCode = zIndexCodeGenerator("top20", item.SoldName, classifyName, "sold")
  269. item.SoldValue, _ = strconv.Atoi(sellVal)
  270. item.SoldChange, _ = strconv.Atoi(sellAddCutVal)
  271. item.Frequency = "日度"
  272. item.ClassifyName = classifyName
  273. item.ClassifyType = classifyType
  274. item.CreateTime = time.Now()
  275. item.ModifyTime = time.Now()
  276. item.DataTime = tradeDate
  277. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  278. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  279. newID, err := models.AddBaseFromTradeZhengzhouIndex(item)
  280. if err != nil {
  281. fmt.Println("insert error:", err)
  282. }
  283. fmt.Println("insert new indexID:", newID)
  284. existIndexMap[item.DealName+item.BuyName+item.SoldName] = item
  285. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  286. //更新
  287. err := models.ModifyBaseFromTradeZhengzhouIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeZhengzhouIndexId)
  288. if err != nil {
  289. fmt.Println("data update err:", err)
  290. }
  291. }
  292. }
  293. }
  294. }
  295. })
  296. }
  297. fmt.Println("end")
  298. utils.FileLog.Info("SyncRankingFromZhengzhou end:" + time.Now().Format(utils.FormatDateTime))
  299. }