sci99_crawler.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/models"
  5. "eta/eta_index_lib/services/sci99"
  6. "eta/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. sic99Info, ok := IndexMap[indexName]
  105. if !ok {
  106. utils.FileLog.Info("指标异常,通过名称找不到指标")
  107. return
  108. }
  109. //获取所有指标信息
  110. allData, e := models.GetBaseFromTradeSci99DataAll(indexCode)
  111. if e != nil {
  112. err = e
  113. fmt.Println("select err:", err)
  114. utils.FileLog.Info("GetBaseFromTradeSci99IndexAll err:", err)
  115. }
  116. for _, vv := range allData {
  117. indexKey := vv.IndexCode + vv.DataTime
  118. existDataMap[indexKey] = vv
  119. }
  120. // 新增data数据
  121. addList := make([]*models.BaseFromSci99Data, 0)
  122. for _, v := range detailResponse.Data.List {
  123. if dataItem, ok := existDataMap[indexCode+v.RealDate]; ok {
  124. if dataItem.Value != v.MDataValue {
  125. // 更新
  126. fmt.Println("更新指标:", indexCode+v.RealDate)
  127. e = models.UpdateBaseFromSci99Data(dataItem.Value, indexCode, v.RealDate)
  128. if e != nil {
  129. err = e
  130. fmt.Println("Error update into database:", err)
  131. utils.FileLog.Info("Error update into database:", err)
  132. return
  133. }
  134. }
  135. } else {
  136. // 新增
  137. dataItem := models.BaseFromSci99Data{
  138. BaseFromSciIndexId: sic99Info.BaseFromSciIndexId,
  139. IndexCode: indexCode,
  140. DataTime: v.RealDate,
  141. Value: v.MDataValue,
  142. CreateTime: time.Now(),
  143. ModifyTime: time.Now(),
  144. }
  145. addList = append(addList, &dataItem)
  146. fmt.Println("新增数据:", indexCode+v.RealDate)
  147. }
  148. }
  149. if len(addList) > 0 {
  150. e = models.AddBaseFromSci99DataMulti(addList)
  151. if e != nil {
  152. err = e
  153. fmt.Println("Error inserting into database:", err)
  154. utils.FileLog.Info("Error inserting into database:", err)
  155. return
  156. }
  157. }
  158. // 更新指标最新日期
  159. {
  160. go func() {
  161. var dateItem *models.EdbInfoMaxAndMinInfo
  162. dateItem, err = sic99Info.GetMaxAndMinDateByIndexCode(indexCode)
  163. if err != nil {
  164. err = fmt.Errorf("查询指标最新日期失败 Err:%s", err)
  165. return
  166. }
  167. sic99Info.ModifyIndexMaxAndMinDate(indexCode, dateItem)
  168. }()
  169. }
  170. time.Sleep(1 * time.Second)
  171. br.Ret = 200
  172. br.Msg = "获取成功"
  173. br.Success = true
  174. }
  175. func Sci99IndexCodeGenerator(indexName, indexCodeStr, marketSampleName, model string) (indexCode string, needAdd bool) {
  176. strResult := ""
  177. indexCode, _ = IndexCodeMap[indexName]
  178. if indexCode == "" {
  179. //首字母
  180. a := pinyin.NewArgs()
  181. a.Fallback = func(r rune, a pinyin.Args) []string {
  182. return []string{string(r)}
  183. }
  184. rows := pinyin.Pinyin(indexCodeStr, a)
  185. for i := 0; i < len(rows); i++ {
  186. //strResult += rows[i][0]
  187. if len(rows[i]) != 0 {
  188. str := rows[i][0]
  189. pi := str[0:1]
  190. strResult += pi
  191. }
  192. }
  193. // 处理市场名称
  194. if province, ok := ProvinceMap[marketSampleName]; ok {
  195. strResult += province
  196. } else {
  197. a := pinyin.NewArgs()
  198. rows := pinyin.LazyPinyin(marketSampleName, a)
  199. for i := 0; i < len(rows); i++ {
  200. strResult += rows[i]
  201. }
  202. }
  203. // 去除特殊符号
  204. model = strings.Replace(model, " ", "", -1)
  205. model = strings.Replace(model, "-", "", -1)
  206. model = strings.Replace(model, "/", "", -1)
  207. model = strings.Replace(model, "#", "", -1)
  208. model = strings.Replace(model, ":", "", -1)
  209. model = strings.Replace(model, "(", "", -1)
  210. model = strings.Replace(model, ")", "", -1)
  211. // 拼接型号
  212. modelRows := pinyin.Pinyin(model, a)
  213. for i := 0; i < len(modelRows); i++ {
  214. if len(modelRows[i]) != 0 {
  215. str := modelRows[i][0]
  216. pi := str[0:1]
  217. strResult += pi
  218. }
  219. }
  220. needAdd = true
  221. indexCode = strings.Replace(strResult, " ", "", -1)
  222. IndexCodeMap[indexName] = indexCode
  223. }
  224. return
  225. }
  226. var ProvinceMap = map[string]string{
  227. "上海": "shanghai",
  228. "云南": "yunnan",
  229. "内蒙古": "innermongolia",
  230. "北京": "beijing",
  231. "台湾": "taiwan",
  232. "吉林": "jilin",
  233. "四川": "sichuan",
  234. "天津": "tianjin",
  235. "宁夏": "ningxia",
  236. "安徽": "anhui",
  237. "山东": "shandong",
  238. "山西": "shanxi",
  239. "广东": "guangdong",
  240. "广西": "guangxi",
  241. "新疆": "xinjiang",
  242. "江苏": "jiangsu",
  243. "江西": "jiangxi",
  244. "河北": "hebei",
  245. "河南": "henan",
  246. "浙江": "zhejiang",
  247. "海南": "hainan",
  248. "湖北": "hubei",
  249. "湖南": "hunan",
  250. "澳门": "macao",
  251. "甘肃": "gansu",
  252. "福建": "fujian",
  253. "西藏": "tibet",
  254. "贵州": "guizhou",
  255. "辽宁": "liaoning",
  256. "重庆": "chongqing",
  257. "陕西": "shaanxi",
  258. "青海": "qinhai",
  259. "香港": "hongkong",
  260. "黑龙江": "heilongjiang",
  261. }
  262. var requestList = []sci99.ListRequestBody{
  263. {
  264. Ppname: "天然橡胶原料",
  265. PPIDs: "13676",
  266. Navid: "593",
  267. }, {
  268. Ppname: "混合胶",
  269. PPIDs: "12973",
  270. Navid: "591",
  271. }, {
  272. Ppname: "标准胶",
  273. PPIDs: "12960",
  274. Navid: "590",
  275. }, {
  276. Ppname: "烟片胶",
  277. PPIDs: "12985",
  278. Navid: "592",
  279. }, {
  280. Ppname: "天然乳胶",
  281. PPIDs: "12947",
  282. Navid: "595",
  283. }, {
  284. Ppname: "丁苯橡胶",
  285. PPIDs: "12951",
  286. Navid: "596",
  287. }, {
  288. Ppname: "顺丁橡胶",
  289. PPIDs: "12964",
  290. Navid: "597",
  291. }, {
  292. Ppname: "SBS",
  293. PPIDs: "12948",
  294. Navid: "598",
  295. }, {
  296. Ppname: "丁腈橡胶",
  297. PPIDs: "12945",
  298. Navid: "605",
  299. },
  300. }