sci99_crawler.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_index_lib/models"
  5. "eta_gn/eta_index_lib/services/sci99"
  6. "eta_gn/eta_index_lib/utils"
  7. "fmt"
  8. "github.com/mozillazg/go-pinyin"
  9. "strings"
  10. "time"
  11. )
  12. // 卓创资讯爬虫
  13. type Sci99Crawler struct {
  14. BaseAuthController
  15. }
  16. var IndexCodeMap = make(map[string]string)
  17. var IndexMap = make(map[string]*models.BaseFromSci99Index)
  18. var ClassifyMap = make(map[string]*models.BaseFromSci99Classify)
  19. // @Title 刷新数据
  20. // @Description 刷新数据接口
  21. // @Param request body models.AddEdbClassifyReq true "type json string"
  22. // @Success 200 {object} models.EdbClassify
  23. // @router /refresh/list [post]
  24. func (this *Sci99Crawler) RefreshSci99Classify() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. var req sci99.RefreshSci99CrawlerReq
  31. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  32. if err != nil {
  33. br.Msg = "参数解析异常!"
  34. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  35. return
  36. }
  37. allClassify, err := models.GetBaseFromSci99Classify()
  38. if err != nil {
  39. fmt.Println("select Code err:", err)
  40. utils.FileLog.Info("GetBaseFromSci99Index err:", err)
  41. return
  42. }
  43. for _, item := range allClassify {
  44. ClassifyMap[item.ClassifyName] = item
  45. }
  46. if _, ok := ClassifyMap[req.ClassifyName]; !ok {
  47. classifyItem := models.BaseFromSci99Classify{
  48. ClassifyName: req.ClassifyName,
  49. CreateTime: time.Now(),
  50. ModifyTime: time.Now(),
  51. }
  52. id, e := models.AddBaseFromSci99Classify(&classifyItem)
  53. if e != nil {
  54. err = e
  55. fmt.Println("Error inserting into database:", err)
  56. utils.FileLog.Info("Error inserting into database:", err)
  57. return
  58. }
  59. fmt.Println("新增分类:", classifyItem.ClassifyName)
  60. classifyItem.BaseFromSciClassifyId = int(id)
  61. ClassifyMap[req.ClassifyName] = &classifyItem
  62. }
  63. allCode, e := models.GetBaseFromSci99Index()
  64. if e != nil {
  65. err = e
  66. fmt.Println("select Code err:", err)
  67. utils.FileLog.Info("GetBaseFromSci99Index err:", err)
  68. return
  69. }
  70. for _, item := range allCode {
  71. IndexCodeMap[item.IndexName] = item.IndexCode
  72. IndexMap[item.IndexName] = item
  73. }
  74. v := req.ListData
  75. // 组成指标
  76. indexName := req.IndexName
  77. fmt.Println("indexName:", indexName)
  78. indexCodeStr := "卓创资讯" + v.ProductName + v.PriceType
  79. indexCode, needAdd := Sci99IndexCodeGenerator(indexName, indexCodeStr, v.MarketSampleName, v.FactorySampleName+v.Model)
  80. if needAdd {
  81. index := models.BaseFromSci99Index{
  82. IndexCode: indexCode,
  83. IndexName: indexName,
  84. ClassifyId: ClassifyMap[req.ClassifyName].BaseFromSciClassifyId,
  85. Unit: v.Unit,
  86. Frequency: "日度",
  87. Describe: v.DataItemName,
  88. CreateTime: time.Now(),
  89. ModifyTime: time.Now(),
  90. }
  91. id, e := models.AddBaseFromSci99Index(&index)
  92. if e != nil {
  93. err = e
  94. fmt.Println("Error inserting into database:", err)
  95. utils.FileLog.Info("Error inserting into database:", err)
  96. return
  97. }
  98. fmt.Println("新增指标:", index.IndexName)
  99. index.BaseFromSciIndexId = int(id)
  100. IndexMap[indexName] = &index
  101. }
  102. detailResponse := req.DetailData
  103. existDataMap := make(map[string]*models.BaseFromSci99Data)
  104. //获取所有指标信息
  105. allData, e := models.GetBaseFromTradeSci99DataAll(indexCode)
  106. if e != nil {
  107. err = e
  108. fmt.Println("select err:", err)
  109. utils.FileLog.Info("GetBaseFromTradeSci99IndexAll err:", err)
  110. }
  111. for _, vv := range allData {
  112. indexKey := vv.IndexCode + vv.DataTime
  113. existDataMap[indexKey] = vv
  114. }
  115. // 新增data数据
  116. addList := make([]*models.BaseFromSci99Data, 0)
  117. for _, v := range detailResponse.Data.List {
  118. if dataItem, ok := existDataMap[indexCode+v.RealDate]; ok {
  119. if dataItem.Value != v.MDataValue {
  120. // 更新
  121. fmt.Println("更新指标:", indexCode+v.RealDate)
  122. e = models.UpdateBaseFromSci99Data(dataItem.Value, indexCode, v.RealDate)
  123. if e != nil {
  124. err = e
  125. fmt.Println("Error update into database:", err)
  126. utils.FileLog.Info("Error update into database:", err)
  127. return
  128. }
  129. }
  130. } else {
  131. // 新增
  132. dataItem := models.BaseFromSci99Data{
  133. BaseFromSciIndexId: IndexMap[indexName].BaseFromSciIndexId,
  134. IndexCode: indexCode,
  135. DataTime: v.RealDate,
  136. Value: v.MDataValue,
  137. CreateTime: time.Now(),
  138. ModifyTime: time.Now(),
  139. }
  140. addList = append(addList, &dataItem)
  141. fmt.Println("新增数据:", indexCode+v.RealDate)
  142. }
  143. }
  144. if len(addList) > 0 {
  145. e = models.AddBaseFromSci99DataMulti(addList)
  146. if e != nil {
  147. err = e
  148. fmt.Println("Error inserting into database:", err)
  149. utils.FileLog.Info("Error inserting into database:", err)
  150. return
  151. }
  152. }
  153. time.Sleep(1 * time.Second)
  154. br.Ret = 200
  155. br.Msg = "获取成功"
  156. br.Success = true
  157. }
  158. func Sci99IndexCodeGenerator(indexName, indexCodeStr, marketSampleName, model string) (indexCode string, needAdd bool) {
  159. strResult := ""
  160. indexCode, _ = IndexCodeMap[indexName]
  161. if indexCode == "" {
  162. //首字母
  163. a := pinyin.NewArgs()
  164. a.Fallback = func(r rune, a pinyin.Args) []string {
  165. return []string{string(r)}
  166. }
  167. rows := pinyin.Pinyin(indexCodeStr, a)
  168. for i := 0; i < len(rows); i++ {
  169. //strResult += rows[i][0]
  170. if len(rows[i]) != 0 {
  171. str := rows[i][0]
  172. pi := str[0:1]
  173. strResult += pi
  174. }
  175. }
  176. // 处理市场名称
  177. if province, ok := ProvinceMap[marketSampleName]; ok {
  178. strResult += province
  179. } else {
  180. a := pinyin.NewArgs()
  181. rows := pinyin.LazyPinyin(marketSampleName, a)
  182. for i := 0; i < len(rows); i++ {
  183. strResult += rows[i]
  184. }
  185. }
  186. // 去除特殊符号
  187. model = strings.Replace(model, " ", "", -1)
  188. model = strings.Replace(model, "-", "", -1)
  189. model = strings.Replace(model, "/", "", -1)
  190. model = strings.Replace(model, "#", "", -1)
  191. model = strings.Replace(model, ":", "", -1)
  192. model = strings.Replace(model, "(", "", -1)
  193. model = strings.Replace(model, ")", "", -1)
  194. // 拼接型号
  195. modelRows := pinyin.Pinyin(model, a)
  196. for i := 0; i < len(modelRows); i++ {
  197. if len(modelRows[i]) != 0 {
  198. str := modelRows[i][0]
  199. pi := str[0:1]
  200. strResult += pi
  201. }
  202. }
  203. needAdd = true
  204. indexCode = strings.Replace(strResult, " ", "", -1)
  205. IndexCodeMap[indexName] = indexCode
  206. }
  207. return
  208. }
  209. var ProvinceMap = map[string]string{
  210. "上海": "shanghai",
  211. "云南": "yunnan",
  212. "内蒙古": "innermongolia",
  213. "北京": "beijing",
  214. "台湾": "taiwan",
  215. "吉林": "jilin",
  216. "四川": "sichuan",
  217. "天津": "tianjin",
  218. "宁夏": "ningxia",
  219. "安徽": "anhui",
  220. "山东": "shandong",
  221. "山西": "shanxi",
  222. "广东": "guangdong",
  223. "广西": "guangxi",
  224. "新疆": "xinjiang",
  225. "江苏": "jiangsu",
  226. "江西": "jiangxi",
  227. "河北": "hebei",
  228. "河南": "henan",
  229. "浙江": "zhejiang",
  230. "海南": "hainan",
  231. "湖北": "hubei",
  232. "湖南": "hunan",
  233. "澳门": "macao",
  234. "甘肃": "gansu",
  235. "福建": "fujian",
  236. "西藏": "tibet",
  237. "贵州": "guizhou",
  238. "辽宁": "liaoning",
  239. "重庆": "chongqing",
  240. "陕西": "shaanxi",
  241. "青海": "qinhai",
  242. "香港": "hongkong",
  243. "黑龙江": "heilongjiang",
  244. }
  245. var requestList = []sci99.ListRequestBody{
  246. {
  247. Ppname: "天然橡胶原料",
  248. PPIDs: "13676",
  249. Navid: "593",
  250. }, {
  251. Ppname: "混合胶",
  252. PPIDs: "12973",
  253. Navid: "591",
  254. }, {
  255. Ppname: "标准胶",
  256. PPIDs: "12960",
  257. Navid: "590",
  258. }, {
  259. Ppname: "烟片胶",
  260. PPIDs: "12985",
  261. Navid: "592",
  262. }, {
  263. Ppname: "天然乳胶",
  264. PPIDs: "12947",
  265. Navid: "595",
  266. }, {
  267. Ppname: "丁苯橡胶",
  268. PPIDs: "12951",
  269. Navid: "596",
  270. }, {
  271. Ppname: "顺丁橡胶",
  272. PPIDs: "12964",
  273. Navid: "597",
  274. }, {
  275. Ppname: "SBS",
  276. PPIDs: "12948",
  277. Navid: "598",
  278. }, {
  279. Ppname: "丁腈橡胶",
  280. PPIDs: "12945",
  281. Navid: "605",
  282. },
  283. }