commodity_trade_zhengzhou.go 9.8 KB

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