init_jiayue_index.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_data_init/models"
  5. "eta/eta_data_init/utils"
  6. "fmt"
  7. "github.com/xuri/excelize/v2"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. )
  12. var (
  13. JiaYueSourceMap map[string]int
  14. JiaYueFrequencyMap map[string]string
  15. )
  16. func init() {
  17. JiaYueSourceMap = make(map[string]int)
  18. JiaYueSourceMap["webisite"] = 1001
  19. JiaYueSourceMap["website"] = 1001
  20. JiaYueSourceMap["website_gf"] = 1001
  21. JiaYueSourceMap["platts"] = 1002
  22. JiaYueSourceMap["reuter"] = 1003
  23. JiaYueSourceMap["reuter_vessel_q"] = 1003
  24. JiaYueSourceMap["路透"] = 1003
  25. JiaYueSourceMap["bloomberg"] = 1004
  26. JiaYueSourceMap["bloomberg_tmp"] = 1004
  27. JiaYueSourceMap["wind"] = 1005
  28. JiaYueSourceMap["wind_p"] = 1005
  29. JiaYueSourceMap["wind_stop"] = 1005
  30. JiaYueSourceMap["wind_tmp"] = 1005
  31. JiaYueFrequencyMap = map[string]string{
  32. "日": "日度",
  33. "周": "周度",
  34. "旬": "旬度",
  35. "半月": "旬度",
  36. "月": "月度",
  37. "季": "季度",
  38. "半年": "半年度",
  39. "年": "年度",
  40. "日度": "日度",
  41. "周度": "周度",
  42. "旬度": "旬度",
  43. "月度": "月度",
  44. "季度": "季度",
  45. "半年度": "半年度",
  46. "年度": "年度",
  47. }
  48. //fmt.Println(JiaYueSourceMap)
  49. //fmt.Println(JiaYueFrequencyMap)
  50. }
  51. // InitJiaYueIndexData 初始化基础指标数据
  52. func InitJiaYueIndexData(dataPath string) {
  53. var err error
  54. defer func() {
  55. if err != nil {
  56. fmt.Println(err.Error())
  57. utils.FileLog.Info("InitJiaYueIndexData Err: " + err.Error())
  58. }
  59. }()
  60. // 读取excel
  61. path, e := filepath.Abs(os.Args[0])
  62. if e != nil {
  63. err = fmt.Errorf("file abs err: %s", e.Error())
  64. return
  65. }
  66. dir := filepath.Dir(path)
  67. dataPath = dir + dataPath
  68. fmt.Println("dataPath:" + dataPath)
  69. //dataPath = "E:\\GoProjects\\src\\eta\\eta_data_init\\docs\\ttt.xlsx"
  70. f, e := excelize.OpenFile(dataPath)
  71. if e != nil {
  72. err = fmt.Errorf("open file err: %s", e.Error())
  73. return
  74. }
  75. defer func() {
  76. if e = f.Close(); e != nil {
  77. err = fmt.Errorf("f close err: %s", e.Error())
  78. }
  79. }()
  80. rows, e := f.GetRows("Sheet1")
  81. if e != nil {
  82. err = fmt.Errorf("f GetRows err: %s", e.Error())
  83. return
  84. }
  85. fmt.Println("rows len:", len(rows))
  86. type ExcelRows struct {
  87. ClassifyFirst string
  88. ClassifySecond string
  89. ClassifyThird string
  90. ClassifyFourth string
  91. ClassifyFifth string
  92. ClassifySixth string
  93. Id string
  94. IndexName string
  95. IndexCode string
  96. Unit string
  97. Frequency string
  98. SourceType string
  99. }
  100. totalIndex := 0
  101. repeatCodes := make(map[string]int, 0)
  102. for rk, row := range rows {
  103. if rk <= 0 {
  104. continue
  105. }
  106. var rowData ExcelRows
  107. for ck, colCell := range row {
  108. switch ck {
  109. case 0:
  110. rowData.ClassifyFirst = strings.TrimSpace(colCell)
  111. case 1:
  112. rowData.ClassifySecond = strings.TrimSpace(colCell)
  113. case 2:
  114. rowData.ClassifyThird = strings.TrimSpace(colCell)
  115. case 3:
  116. rowData.ClassifyFourth = strings.TrimSpace(colCell)
  117. case 4:
  118. rowData.ClassifyFifth = strings.TrimSpace(colCell)
  119. case 5:
  120. rowData.ClassifySixth = strings.TrimSpace(colCell)
  121. case 6:
  122. rowData.Id = strings.TrimSpace(colCell)
  123. case 7:
  124. rowData.IndexName = strings.TrimSpace(colCell)
  125. case 8:
  126. rowData.IndexCode = strings.TrimSpace(colCell)
  127. case 9:
  128. rowData.Unit = strings.TrimSpace(colCell)
  129. case 10:
  130. rowData.Frequency = strings.TrimSpace(colCell)
  131. case 11:
  132. rowData.SourceType = strings.TrimSpace(colCell)
  133. }
  134. }
  135. if rowData.ClassifyFirst == "" || rowData.IndexName == "" || rowData.Id == "" || rowData.SourceType == "" || rowData.Frequency == "" {
  136. fmt.Printf("row is empty, ClassifyFirst: %s, IndexName: %s, Id: %s, SourceType: %s, Frequency: %s\n", rowData.ClassifyFirst, rowData.IndexName, rowData.Id, rowData.SourceType, rowData.Frequency)
  137. continue
  138. }
  139. sourceId, ok := JiaYueSourceMap[rowData.SourceType]
  140. if !ok {
  141. fmt.Printf("source is undefined, source: %s\n", rowData.SourceType)
  142. continue
  143. }
  144. // 指标编码, 路透彭博wind用指标编码, 没有的就忽略
  145. edbCode := ""
  146. if sourceId == 1003 || sourceId == 1004 || sourceId == 1005 {
  147. edbCode = rowData.IndexCode
  148. } else {
  149. edbCode = rowData.Id
  150. }
  151. if edbCode == "" {
  152. fmt.Printf("edb code empty\n")
  153. continue
  154. }
  155. if rowData.Unit == "" {
  156. rowData.Unit = "无"
  157. }
  158. var firstId, secondId, thirdId, fourthId, fifthId, lastId int
  159. // 一级分类
  160. method := "classify/get_or_add"
  161. classifyFirstMap := make(map[string]interface{})
  162. classifyFirstMap["ClassifyName"] = rowData.ClassifyFirst
  163. classifyFirstMap["ParentId"] = 0
  164. classifyFirstMap["Level"] = 0
  165. classifyFirstMap["ClassifyType"] = 0
  166. result, e := PostEdbLib(classifyFirstMap, method)
  167. if e != nil {
  168. err = fmt.Errorf("ClassifyFirst PostEdbLib err: %s", e.Error())
  169. return
  170. }
  171. resp := new(models.ClassifyResp)
  172. if e = json.Unmarshal(result, &resp); e != nil {
  173. err = fmt.Errorf("ClassifyFirst json unmarshal err: %s", e.Error())
  174. return
  175. }
  176. if resp.Ret != 200 {
  177. err = fmt.Errorf("ClassifyFirst resp msg: %s; errMsg: %s", resp.Msg, resp.ErrMsg)
  178. return
  179. }
  180. firstId = resp.Data.ClassifyId
  181. lastId = firstId
  182. // 二级分类
  183. if rowData.ClassifySecond != "" {
  184. classifySecondMap := make(map[string]interface{})
  185. classifySecondMap["ClassifyName"] = rowData.ClassifySecond
  186. classifySecondMap["ParentId"] = firstId
  187. classifySecondMap["Level"] = 1
  188. classifySecondMap["ClassifyType"] = 0
  189. res2, e := PostEdbLib(classifySecondMap, method)
  190. if e != nil {
  191. err = fmt.Errorf("ClassifySecond PostEdbLib err: %s", e.Error())
  192. return
  193. }
  194. resp2 := new(models.ClassifyResp)
  195. if e = json.Unmarshal(res2, &resp2); e != nil {
  196. err = fmt.Errorf("ClassifySecond json unmarshal err: %s", e.Error())
  197. return
  198. }
  199. if resp2.Ret != 200 {
  200. err = fmt.Errorf("ClassifySecond resp msg: %s; errMsg: %s", resp2.Msg, resp2.ErrMsg)
  201. return
  202. }
  203. secondId = resp2.Data.ClassifyId
  204. lastId = secondId
  205. }
  206. // 三级分类
  207. if rowData.ClassifyThird != "" {
  208. classifyThirdMap := make(map[string]interface{})
  209. classifyThirdMap["ClassifyName"] = rowData.ClassifyThird
  210. classifyThirdMap["ParentId"] = secondId
  211. classifyThirdMap["Level"] = 2
  212. classifyThirdMap["ClassifyType"] = 0
  213. res3, e := PostEdbLib(classifyThirdMap, method)
  214. if e != nil {
  215. err = fmt.Errorf("ClassifyThird PostEdbLib err: %s", e.Error())
  216. return
  217. }
  218. resp3 := new(models.ClassifyResp)
  219. if e = json.Unmarshal(res3, &resp3); e != nil {
  220. err = fmt.Errorf("ClassifyThird json unmarshal err: %s", e.Error())
  221. return
  222. }
  223. if resp3.Ret != 200 {
  224. err = fmt.Errorf("ClassifyThird resp msg: %s; errMsg: %s", resp3.Msg, resp3.ErrMsg)
  225. return
  226. }
  227. thirdId = resp3.Data.ClassifyId
  228. lastId = thirdId
  229. }
  230. // 四级分类
  231. if rowData.ClassifyFourth != "" {
  232. classifyFourthMap := make(map[string]interface{})
  233. classifyFourthMap["ClassifyName"] = rowData.ClassifyFourth
  234. classifyFourthMap["ParentId"] = thirdId
  235. classifyFourthMap["Level"] = 3
  236. classifyFourthMap["ClassifyType"] = 0
  237. res4, e := PostEdbLib(classifyFourthMap, method)
  238. if e != nil {
  239. err = fmt.Errorf("ClassifyFourth PostEdbLib err: %s", e.Error())
  240. return
  241. }
  242. resp4 := new(models.ClassifyResp)
  243. if e = json.Unmarshal(res4, &resp4); e != nil {
  244. err = fmt.Errorf("ClassifyFourth json unmarshal err: %s", e.Error())
  245. return
  246. }
  247. if resp4.Ret != 200 {
  248. err = fmt.Errorf("ClassifyFourth resp msg: %s; errMsg: %s", resp4.Msg, resp4.ErrMsg)
  249. return
  250. }
  251. fourthId = resp4.Data.ClassifyId
  252. lastId = fourthId
  253. }
  254. // 五级分类
  255. if rowData.ClassifyFifth != "" {
  256. classifyFifthMap := make(map[string]interface{})
  257. classifyFifthMap["ClassifyName"] = rowData.ClassifyFifth
  258. classifyFifthMap["ParentId"] = fourthId
  259. classifyFifthMap["Level"] = 4
  260. classifyFifthMap["ClassifyType"] = 0
  261. res5, e := PostEdbLib(classifyFifthMap, method)
  262. if e != nil {
  263. err = fmt.Errorf("ClassifyFifth PostEdbLib err: %s", e.Error())
  264. return
  265. }
  266. resp5 := new(models.ClassifyResp)
  267. if e = json.Unmarshal(res5, &resp5); e != nil {
  268. err = fmt.Errorf("ClassifyFifth json unmarshal err: %s", e.Error())
  269. return
  270. }
  271. if resp5.Ret != 200 {
  272. err = fmt.Errorf("ClassifyFifth resp msg: %s; errMsg: %s", resp5.Msg, resp5.ErrMsg)
  273. return
  274. }
  275. fifthId = resp5.Data.ClassifyId
  276. lastId = fifthId
  277. }
  278. // 六级分类
  279. if rowData.ClassifySixth != "" {
  280. classifySixthMap := make(map[string]interface{})
  281. classifySixthMap["ClassifyName"] = rowData.ClassifySixth
  282. classifySixthMap["ParentId"] = fifthId
  283. classifySixthMap["Level"] = 5
  284. classifySixthMap["ClassifyType"] = 0
  285. res6, e := PostEdbLib(classifySixthMap, method)
  286. if e != nil {
  287. err = fmt.Errorf("ClassifySixth PostEdbLib err: %s", e.Error())
  288. return
  289. }
  290. resp6 := new(models.ClassifyResp)
  291. if e = json.Unmarshal(res6, &resp6); e != nil {
  292. err = fmt.Errorf("ClassifySixth json unmarshal err: %s", e.Error())
  293. return
  294. }
  295. if resp6.Ret != 200 {
  296. err = fmt.Errorf("ClassifySixth resp msg: %s; errMsg: %s", resp6.Msg, resp6.ErrMsg)
  297. return
  298. }
  299. lastId = resp6.Data.ClassifyId
  300. }
  301. // 新增指标
  302. method = "edb_info/add"
  303. indexMap := make(map[string]interface{})
  304. indexMap["Source"] = sourceId
  305. indexMap["EdbCode"] = edbCode
  306. indexMap["EdbName"] = rowData.IndexName
  307. indexMap["Frequency"] = JiaYueFrequencyMap[rowData.Frequency]
  308. indexMap["Unit"] = rowData.Unit
  309. indexMap["ClassifyId"] = lastId
  310. indexRes, e := PostEdbLib(indexMap, method)
  311. if e != nil {
  312. err = fmt.Errorf("edb add PostEdbLib err: %s; result: %s", e.Error(), string(indexRes))
  313. return
  314. }
  315. indexResp := new(models.EdbInfoResp)
  316. if e = json.Unmarshal(indexRes, &indexResp); e != nil {
  317. err = fmt.Errorf("edb add unmarshal err: %s; result: %s", e.Error(), string(indexRes))
  318. return
  319. }
  320. if indexResp.Ret != 200 {
  321. if strings.Contains(indexResp.Msg, "新增指标失败") {
  322. continue
  323. } else {
  324. err = fmt.Errorf("edb add resp msg: %s; errMsg: %s", indexResp.Msg, indexResp.ErrMsg)
  325. return
  326. }
  327. }
  328. fmt.Println("edb add success:" + edbCode)
  329. repeatCodes[indexResp.Data.EdbCode] += 1
  330. // 刷新指标
  331. method = "jiayue_index/refresh"
  332. refreshMap := make(map[string]interface{})
  333. refreshMap["EdbInfoId"] = indexResp.Data.EdbInfoId
  334. refreshMap["EdbCode"] = indexResp.Data.EdbCode
  335. refreshMap["Source"] = indexResp.Data.Source
  336. refreshMap["StartDate"] = "1990-01-01"
  337. refreshRes, e := PostEdbLib(refreshMap, method)
  338. if e != nil {
  339. utils.FileLog.Info("edb refresh err: %s; result: %s; IndexCode: %s", e.Error(), string(refreshRes), edbCode)
  340. }
  341. totalIndex += 1
  342. }
  343. // 打印重复编码的指标
  344. repeatCodeArr := make([]string, 0)
  345. for k, v := range repeatCodes {
  346. if v > 1 {
  347. repeatCodeArr = append(repeatCodeArr, k)
  348. }
  349. }
  350. utils.FileLog.Info(fmt.Sprintf("初始化指标总数: %d, 重复编码的指标数: %d, 重复编码: %s", totalIndex, len(repeatCodeArr), strings.Join(repeatCodeArr, ",")))
  351. }