commodity_trade_zhengzhou.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package services
  2. import (
  3. "fmt"
  4. "github.com/mozillazg/go-pinyin"
  5. "hongze/hongze_data_crawler/models"
  6. "hongze/hongze_data_crawler/utils"
  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. memberShortNameArr = append(memberShortNameArr, tdText)
  134. }
  135. }
  136. if tk == 4 {
  137. if !strings.Contains(tdText, "会员简称") {
  138. memberShortNameArr = append(memberShortNameArr, tdText)
  139. }
  140. }
  141. if tk == 7 {
  142. if !strings.Contains(tdText, "会员简称") {
  143. memberShortNameArr = append(memberShortNameArr, tdText)
  144. }
  145. }
  146. if tk == 2 {
  147. if strings.Contains(tdText, "成交量") {
  148. dealSuffix = tdText
  149. } else {
  150. dealVal = tdText
  151. }
  152. }
  153. if tk == 3 {
  154. if strings.Contains(tdText, "增减量") {
  155. //dealAddCutSuffix = tdText
  156. } else {
  157. dealAddCutVal = tdText
  158. }
  159. }
  160. if tk == 5 {
  161. if strings.Contains(tdText, "持买仓量") {
  162. buySuffix = tdText
  163. } else {
  164. buyVal = tdText
  165. }
  166. }
  167. if tk == 6 {
  168. if strings.Contains(tdText, "增减量") {
  169. //buyAddCutSuffix = tdText
  170. } else {
  171. buyAddCutVal = tdText
  172. }
  173. }
  174. if tk == 8 {
  175. if strings.Contains(tdText, "持卖仓量") {
  176. sellSuffix = tdText
  177. } else {
  178. sellVal = tdText
  179. }
  180. }
  181. if tk == 9 {
  182. if strings.Contains(tdText, "增减量") {
  183. //sellAddCutSuffix = tdText
  184. } else {
  185. sellAddCutVal = tdText
  186. }
  187. }
  188. })
  189. //处理指标
  190. dealVal = strings.Replace(dealVal, ",", "", -1)
  191. dealAddCutVal = strings.Replace(dealAddCutVal, ",", "", -1)
  192. buyVal = strings.Replace(buyVal, ",", "", -1)
  193. buyAddCutVal = strings.Replace(buyAddCutVal, ",", "", -1)
  194. sellVal = strings.Replace(sellVal, ",", "", -1)
  195. sellAddCutVal = strings.Replace(sellAddCutVal, ",", "", -1)
  196. tradeDate = strings.Replace(tradeDate, "日期:", "", -1)
  197. classifyNameArr := strings.Split(classifyName, ":")
  198. if len(classifyNameArr) > 1 {
  199. classifyType = classifyNameArr[0]
  200. classifyName = classifyNameArr[1]
  201. }
  202. //fmt.Printf("memberShortNameArr:%s,len:%d\n", memberShortNameArr, len(memberShortNameArr))
  203. if len(memberShortNameArr) != 0 {
  204. if len(memberShortNameArr[0]) != 2 {
  205. if len(classifyName) > 7 {
  206. classifyName = classifyName[len(classifyName)-2:]
  207. }
  208. item := new(models.BaseFromTradeZhengzhouIndex)
  209. item.Rank, _ = strconv.Atoi(rank)
  210. item.DealShortName = memberShortNameArr[0]
  211. item.DealName = memberShortNameArr[0] + "_" + classifyName + "_" + dealSuffix
  212. item.DealCode = zIndexCodeGenerator(item.DealShortName, item.DealName, classifyName, "deal")
  213. item.DealValue, _ = strconv.Atoi(dealVal)
  214. item.DealChange, _ = strconv.Atoi(dealAddCutVal)
  215. item.BuyShortName = memberShortNameArr[1]
  216. item.BuyName = memberShortNameArr[1] + "_" + classifyName + "_" + buySuffix
  217. item.BuyCode = zIndexCodeGenerator(item.BuyShortName, item.BuyName, classifyName, "buy")
  218. item.BuyValue, _ = strconv.Atoi(buyVal)
  219. item.BuyChange, _ = strconv.Atoi(buyAddCutVal)
  220. item.SoldShortName = memberShortNameArr[2]
  221. item.SoldName = memberShortNameArr[2] + "_" + classifyName + "_" + sellSuffix
  222. item.SoldCode = zIndexCodeGenerator(item.SoldShortName, item.SoldName, classifyName, "sold")
  223. item.SoldValue, _ = strconv.Atoi(sellVal)
  224. item.SoldChange, _ = strconv.Atoi(sellAddCutVal)
  225. item.Frequency = "日度"
  226. item.ClassifyName = classifyName
  227. item.ClassifyType = classifyType
  228. item.CreateTime = time.Now()
  229. item.ModifyTime = time.Now()
  230. item.DataTime = tradeDate
  231. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  232. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  233. newID, err := models.AddBaseFromTradeZhengzhouIndex(item)
  234. if err != nil {
  235. fmt.Println("insert error:", err)
  236. }
  237. fmt.Println("insert new indexID:", newID)
  238. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  239. //更新
  240. err := models.ModifyBaseFromTradeZhengzhouIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeZhengzhouIndexId)
  241. if err != nil {
  242. fmt.Println("data update err:", err)
  243. }
  244. }
  245. } else {
  246. //合计
  247. if len(classifyName) > 7 {
  248. classifyName = classifyName[len(classifyName)-2:]
  249. }
  250. item := new(models.BaseFromTradeZhengzhouIndex)
  251. item.Rank = 999
  252. item.DealShortName = ""
  253. item.DealName = "top20" + "_" + classifyName + "_" + dealSuffix
  254. item.DealCode = zIndexCodeGenerator("top20", item.DealName, classifyName, "deal")
  255. item.DealValue, _ = strconv.Atoi(dealVal)
  256. item.DealChange, _ = strconv.Atoi(dealAddCutVal)
  257. item.BuyShortName = ""
  258. item.BuyName = "top20" + "_" + classifyName + "_" + buySuffix
  259. item.BuyCode = zIndexCodeGenerator("top20", item.BuyName, classifyName, "buy")
  260. item.BuyValue, _ = strconv.Atoi(buyVal)
  261. item.BuyChange, _ = strconv.Atoi(buyAddCutVal)
  262. item.SoldShortName = ""
  263. item.SoldName = "top20" + "_" + classifyName + "_" + sellSuffix
  264. item.SoldCode = zIndexCodeGenerator("top20", item.SoldName, classifyName, "sold")
  265. item.SoldValue, _ = strconv.Atoi(sellVal)
  266. item.SoldChange, _ = strconv.Atoi(sellAddCutVal)
  267. item.Frequency = "日度"
  268. item.ClassifyName = classifyName
  269. item.ClassifyType = classifyType
  270. item.CreateTime = time.Now()
  271. item.ModifyTime = time.Now()
  272. item.DataTime = tradeDate
  273. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  274. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  275. newID, err := models.AddBaseFromTradeZhengzhouIndex(item)
  276. if err != nil {
  277. fmt.Println("insert error:", err)
  278. }
  279. fmt.Println("insert new indexID:", newID)
  280. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  281. //更新
  282. err := models.ModifyBaseFromTradeZhengzhouIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeZhengzhouIndexId)
  283. if err != nil {
  284. fmt.Println("data update err:", err)
  285. }
  286. }
  287. }
  288. }
  289. }
  290. })
  291. }
  292. fmt.Println("end")
  293. utils.FileLog.Info("SyncRankingFromZhengzhou end:"+time.Now().Format(utils.FormatDateTime))
  294. }