init_base_index.go 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_data_init/models"
  6. "eta/eta_data_init/utils"
  7. "fmt"
  8. "github.com/xuri/excelize/v2"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. )
  13. // InitBaseIndexData 初始化基础指标数据
  14. func InitBaseIndexData(dataPath string) {
  15. var err error
  16. defer func() {
  17. if err != nil {
  18. fmt.Println("InitBaseIndexData Err:" + err.Error())
  19. utils.FileLog.Info("InitJiaYueIndexData Err: " + err.Error())
  20. }
  21. }()
  22. //读取excel
  23. path, err := filepath.Abs(os.Args[0])
  24. if err != nil {
  25. fmt.Println(err)
  26. }
  27. dir := filepath.Dir(path)
  28. fmt.Println("dir:" + dir)
  29. //dataPath := dir + "/docs/东吴ETA同花顺指标20230925.xlsx"
  30. dataPath = dir + dataPath
  31. fmt.Println("dataPath:" + dataPath)
  32. f, err := excelize.OpenFile(dataPath)
  33. if err != nil {
  34. fmt.Println(err)
  35. return
  36. }
  37. defer func() {
  38. // Close the spreadsheet.
  39. if err := f.Close(); err != nil {
  40. fmt.Println(err)
  41. }
  42. }()
  43. rows, err := f.GetRows("Sheet1")
  44. if err != nil {
  45. fmt.Println(err)
  46. return
  47. }
  48. fmt.Println("rows len:", len(rows))
  49. // 获取创建人信息
  50. mobileMap := make(map[string]*models.Admin)
  51. if utils.MYSQL_URL_ETA != "" {
  52. admins, e := models.GetSysAdminList(``, make([]interface{}, 0))
  53. if e != nil {
  54. err = fmt.Errorf("GetSysAdminList err: %s", e.Error())
  55. return
  56. }
  57. for _, v := range admins {
  58. if v.Mobile == "" {
  59. continue
  60. }
  61. mobileMap[v.Mobile] = v
  62. }
  63. }
  64. for rk, row := range rows {
  65. if rk > 0 {
  66. var classifyFirst, classifySecond, classifyThree, classifyFourth, classifyFifth, classifySixth, indexCode, indexName, frequency, unit, source, mobile, terminalCode string
  67. for ck, colCell := range row {
  68. colCell = strings.TrimSpace(colCell)
  69. switch ck {
  70. case 0:
  71. classifyFirst = colCell
  72. case 1:
  73. classifySecond = colCell
  74. case 2:
  75. classifyThree = colCell
  76. case 3:
  77. classifyFourth = colCell
  78. case 4:
  79. classifyFifth = colCell
  80. case 5:
  81. classifySixth = colCell
  82. case 6:
  83. indexCode = colCell
  84. case 7:
  85. indexName = colCell
  86. case 8:
  87. frequency = colCell
  88. case 9:
  89. unit = colCell
  90. case 10:
  91. source = colCell
  92. case 11:
  93. mobile = colCell
  94. case 12:
  95. terminalCode = colCell
  96. }
  97. }
  98. if classifyFirst != "" &&
  99. indexCode != "" &&
  100. indexName != "" &&
  101. unit != "" &&
  102. frequency != "" &&
  103. source != "" {
  104. var firstId, secondId, thirdId, fourthId, fifthId, lastId int
  105. method := "classify/get_or_add"
  106. classifyFirstMap := make(map[string]interface{})
  107. classifyFirstMap["ClassifyName"] = classifyFirst
  108. classifyFirstMap["ParentId"] = 0
  109. classifyFirstMap["Level"] = 0
  110. classifyFirstMap["ClassifyType"] = 0
  111. result, e := PostEdbLib(classifyFirstMap, method)
  112. if e != nil {
  113. err = fmt.Errorf("ClassifyFirst PostEdbLib err: %s", e.Error())
  114. return
  115. }
  116. resp := new(models.ClassifyResp)
  117. if e = json.Unmarshal(result, &resp); e != nil {
  118. err = fmt.Errorf("ClassifyFirst json unmarshal err: %s", e.Error())
  119. return
  120. }
  121. if resp.Ret != 200 {
  122. err = fmt.Errorf("ClassifyFirst resp msg: %s; errMsg: %s", resp.Msg, resp.ErrMsg)
  123. return
  124. }
  125. firstId = resp.Data.ClassifyId
  126. lastId = firstId
  127. // 二级分类
  128. if classifySecond != "" {
  129. classifySecondMap := make(map[string]interface{})
  130. classifySecondMap["ClassifyName"] = classifySecond
  131. classifySecondMap["ParentId"] = firstId
  132. classifySecondMap["Level"] = 1
  133. classifySecondMap["ClassifyType"] = 0
  134. res2, e := PostEdbLib(classifySecondMap, method)
  135. if e != nil {
  136. err = fmt.Errorf("ClassifySecond PostEdbLib err: %s", e.Error())
  137. return
  138. }
  139. resp2 := new(models.ClassifyResp)
  140. if e = json.Unmarshal(res2, &resp2); e != nil {
  141. err = fmt.Errorf("ClassifySecond json unmarshal err: %s", e.Error())
  142. return
  143. }
  144. if resp2.Ret != 200 {
  145. err = fmt.Errorf("ClassifySecond resp msg: %s; errMsg: %s", resp2.Msg, resp2.ErrMsg)
  146. return
  147. }
  148. secondId = resp2.Data.ClassifyId
  149. lastId = secondId
  150. }
  151. // 三级分类
  152. if classifyThree != "" {
  153. classifyThreeMap := make(map[string]interface{})
  154. classifyThreeMap["ClassifyName"] = classifyThree
  155. classifyThreeMap["ParentId"] = secondId
  156. classifyThreeMap["Level"] = 2
  157. classifyThreeMap["ClassifyType"] = 0
  158. res3, e := PostEdbLib(classifyThreeMap, method)
  159. if e != nil {
  160. err = fmt.Errorf("ClassifyThird PostEdbLib err: %s", e.Error())
  161. return
  162. }
  163. resp3 := new(models.ClassifyResp)
  164. if e = json.Unmarshal(res3, &resp3); e != nil {
  165. err = fmt.Errorf("ClassifyThird json unmarshal err: %s", e.Error())
  166. return
  167. }
  168. if resp3.Ret != 200 {
  169. err = fmt.Errorf("ClassifyThird resp msg: %s; errMsg: %s", resp3.Msg, resp3.ErrMsg)
  170. return
  171. }
  172. thirdId = resp3.Data.ClassifyId
  173. lastId = thirdId
  174. }
  175. // 四级分类
  176. if classifyFourth != "" {
  177. classifyFourthMap := make(map[string]interface{})
  178. classifyFourthMap["ClassifyName"] = classifyFourth
  179. classifyFourthMap["ParentId"] = thirdId
  180. classifyFourthMap["Level"] = 3
  181. classifyFourthMap["ClassifyType"] = 0
  182. res4, e := PostEdbLib(classifyFourthMap, method)
  183. if e != nil {
  184. err = fmt.Errorf("ClassifyFourth PostEdbLib err: %s", e.Error())
  185. return
  186. }
  187. resp4 := new(models.ClassifyResp)
  188. if e = json.Unmarshal(res4, &resp4); e != nil {
  189. err = fmt.Errorf("ClassifyFourth json unmarshal err: %s", e.Error())
  190. return
  191. }
  192. if resp4.Ret != 200 {
  193. err = fmt.Errorf("ClassifyFourth resp msg: %s; errMsg: %s", resp4.Msg, resp4.ErrMsg)
  194. return
  195. }
  196. fourthId = resp4.Data.ClassifyId
  197. lastId = fourthId
  198. }
  199. // 五级分类
  200. if classifyFifth != "" {
  201. classifyFifthMap := make(map[string]interface{})
  202. classifyFifthMap["ClassifyName"] = classifyFifth
  203. classifyFifthMap["ParentId"] = fourthId
  204. classifyFifthMap["Level"] = 4
  205. classifyFifthMap["ClassifyType"] = 0
  206. res5, e := PostEdbLib(classifyFifthMap, method)
  207. if e != nil {
  208. err = fmt.Errorf("ClassifyFifth PostEdbLib err: %s", e.Error())
  209. return
  210. }
  211. resp5 := new(models.ClassifyResp)
  212. if e = json.Unmarshal(res5, &resp5); e != nil {
  213. err = fmt.Errorf("ClassifyFifth json unmarshal err: %s", e.Error())
  214. return
  215. }
  216. if resp5.Ret != 200 {
  217. err = fmt.Errorf("ClassifyFifth resp msg: %s; errMsg: %s", resp5.Msg, resp5.ErrMsg)
  218. return
  219. }
  220. fifthId = resp5.Data.ClassifyId
  221. lastId = fifthId
  222. }
  223. // 六级分类
  224. if classifySixth != "" {
  225. classifySixthMap := make(map[string]interface{})
  226. classifySixthMap["ClassifyName"] = classifySixth
  227. classifySixthMap["ParentId"] = fifthId
  228. classifySixthMap["Level"] = 5
  229. classifySixthMap["ClassifyType"] = 0
  230. res6, e := PostEdbLib(classifySixthMap, method)
  231. if e != nil {
  232. err = fmt.Errorf("ClassifySixth PostEdbLib err: %s", e.Error())
  233. return
  234. }
  235. resp6 := new(models.ClassifyResp)
  236. if e = json.Unmarshal(res6, &resp6); e != nil {
  237. err = fmt.Errorf("ClassifySixth json unmarshal err: %s", e.Error())
  238. return
  239. }
  240. if resp6.Ret != 200 {
  241. err = fmt.Errorf("ClassifySixth resp msg: %s; errMsg: %s", resp6.Msg, resp6.ErrMsg)
  242. return
  243. }
  244. lastId = resp6.Data.ClassifyId
  245. }
  246. method = "edb_info/add"
  247. sourceId, ok := IndexSourceMap[source]
  248. if !ok {
  249. fmt.Println("source is not defined")
  250. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
  251. continue
  252. }
  253. indexMap := make(map[string]interface{})
  254. indexMap["Source"] = sourceId
  255. indexMap["EdbCode"] = indexCode
  256. indexMap["EdbName"] = indexName
  257. indexMap["Frequency"] = frequency
  258. indexMap["Unit"] = unit
  259. indexMap["ClassifyId"] = lastId
  260. indexMap["TerminalCode"] = terminalCode
  261. admin := mobileMap[mobile]
  262. if admin != nil {
  263. indexMap["AdminId"] = admin.AdminId
  264. indexMap["AdminName"] = admin.RealName
  265. }
  266. result, err = PostEdbLib(indexMap, method)
  267. if err != nil {
  268. utils.FileLog.Info("初始化指标失败:" + err.Error() + " result:" + string(result))
  269. return
  270. }
  271. indexResp := new(models.EdbInfoResp)
  272. err = json.Unmarshal(result, &indexResp)
  273. if err != nil {
  274. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  275. return
  276. }
  277. if indexResp.Ret != 200 {
  278. if strings.Contains(indexResp.Msg, "新增指标失败") {
  279. continue
  280. } else {
  281. fmt.Println("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  282. utils.FileLog.Info("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  283. return
  284. }
  285. }
  286. fmt.Println("add index success:" + indexCode)
  287. //刷新指标
  288. {
  289. switch source {
  290. case "wind", "万得":
  291. method = "wind/refresh"
  292. case "ths", "同花顺":
  293. method = "ths/refresh"
  294. case "彭博":
  295. method = "pb/refresh"
  296. case "路透":
  297. method = "lt/refresh"
  298. case "":
  299. }
  300. if method != `` {
  301. refreshMap := make(map[string]interface{})
  302. refreshMap["EdbInfoId"] = indexResp.Data.EdbInfoId
  303. refreshMap["EdbCode"] = indexCode
  304. refreshMap["StartDate"] = "1990-01-01"
  305. PostEdbLib(refreshMap, method)
  306. }
  307. }
  308. } else {
  309. fmt.Println("data is empty")
  310. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
  311. }
  312. }
  313. }
  314. }
  315. // 初始化基础指标数据-钢联
  316. func InitBaseIndexDataFromMysteel(filePath string) {
  317. var err error
  318. defer func() {
  319. if err != nil {
  320. fmt.Println("InitBaseIndexDataFromMysteel Err:" + err.Error())
  321. utils.FileLog.Info("InitBaseIndexDataFromMysteel Err:" + err.Error())
  322. }
  323. }()
  324. //读取excel
  325. path, err := filepath.Abs(os.Args[0])
  326. if err != nil {
  327. fmt.Println(err)
  328. }
  329. dir := filepath.Dir(path)
  330. fmt.Println("dir:" + dir)
  331. dataPath := dir + filePath
  332. fmt.Println("dataPath:" + dataPath)
  333. f, err := excelize.OpenFile(dataPath)
  334. if err != nil {
  335. fmt.Println(err)
  336. return
  337. }
  338. defer func() {
  339. // Close the spreadsheet.
  340. if err := f.Close(); err != nil {
  341. fmt.Println(err)
  342. }
  343. }()
  344. rows, err := f.GetRows("Sheet1")
  345. if err != nil {
  346. fmt.Println(err)
  347. return
  348. }
  349. fmt.Println("rows len:", len(rows))
  350. // 获取创建人信息
  351. mobileMap := make(map[string]*models.Admin)
  352. if utils.MYSQL_URL_ETA != "" {
  353. admins, e := models.GetSysAdminList(``, make([]interface{}, 0))
  354. if e != nil {
  355. err = fmt.Errorf("GetSysAdminList err: %s", e.Error())
  356. return
  357. }
  358. for _, v := range admins {
  359. if v.Mobile == "" {
  360. continue
  361. }
  362. mobileMap[v.Mobile] = v
  363. }
  364. }
  365. for rk, row := range rows {
  366. if rk > 0 {
  367. var classifyFirst, classifySecond, classifyThree, classifyFourth, classifyFifth, classifySixth, indexCode, indexName, frequency, unit, source, mobile string
  368. for ck, colCell := range row {
  369. colCell = strings.TrimSpace(colCell)
  370. switch ck {
  371. case 0:
  372. classifyFirst = colCell
  373. case 1:
  374. classifySecond = colCell
  375. case 2:
  376. classifyThree = colCell
  377. case 3:
  378. classifyFourth = colCell
  379. case 4:
  380. classifyFifth = colCell
  381. case 5:
  382. classifySixth = colCell
  383. case 6:
  384. indexCode = colCell
  385. case 7:
  386. indexName = colCell
  387. case 8:
  388. frequency = colCell
  389. case 9:
  390. unit = colCell
  391. case 10:
  392. source = colCell
  393. case 11:
  394. mobile = colCell
  395. }
  396. }
  397. if classifyFirst != "" &&
  398. indexCode != "" &&
  399. indexName != "" &&
  400. unit != "" &&
  401. frequency != "" &&
  402. source != "" {
  403. //判断指标是否存在
  404. method := "mysteel_chemical/index_detail"
  405. indexSearchMap := make(map[string]interface{})
  406. indexSearchMap["IndexCode"] = indexCode
  407. indexResult, e := PostEdbLib(indexSearchMap, method)
  408. if e != nil {
  409. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  410. return
  411. }
  412. mysteelIndexResp := new(models.MysteelIndexResp)
  413. e = json.Unmarshal(indexResult, &mysteelIndexResp)
  414. if e != nil {
  415. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  416. return
  417. }
  418. if mysteelIndexResp.Ret != 200 {
  419. fmt.Println("判断指标是否存在失败,Err:" + mysteelIndexResp.ErrMsg)
  420. //utils.FileLog.Info("判断指标是否存在失败:" + err.Error())
  421. //return
  422. continue
  423. }
  424. if mysteelIndexResp.Data.BaseFromMysteelChemicalIndexId <= 0 {
  425. fmt.Println("指标:" + indexCode + ";不存在")
  426. continue
  427. }
  428. var firstId, secondId, thirdId, fourthId, fifthId, lastId int
  429. method = "classify/get_or_add"
  430. classifyFirstMap := make(map[string]interface{})
  431. classifyFirstMap["ClassifyName"] = classifyFirst
  432. classifyFirstMap["ParentId"] = 0
  433. classifyFirstMap["Level"] = 0
  434. classifyFirstMap["ClassifyType"] = 0
  435. result, e := PostEdbLib(classifyFirstMap, method)
  436. if e != nil {
  437. err = fmt.Errorf("ClassifyFirst PostEdbLib err: %s", e.Error())
  438. return
  439. }
  440. resp := new(models.ClassifyResp)
  441. if e = json.Unmarshal(result, &resp); e != nil {
  442. err = fmt.Errorf("ClassifyFirst json unmarshal err: %s", e.Error())
  443. return
  444. }
  445. if resp.Ret != 200 {
  446. err = fmt.Errorf("ClassifyFirst resp msg: %s; errMsg: %s", resp.Msg, resp.ErrMsg)
  447. return
  448. }
  449. firstId = resp.Data.ClassifyId
  450. lastId = firstId
  451. // 二级分类
  452. if classifySecond != "" {
  453. classifySecondMap := make(map[string]interface{})
  454. classifySecondMap["ClassifyName"] = classifySecond
  455. classifySecondMap["ParentId"] = firstId
  456. classifySecondMap["Level"] = 1
  457. classifySecondMap["ClassifyType"] = 0
  458. res2, e := PostEdbLib(classifySecondMap, method)
  459. if e != nil {
  460. err = fmt.Errorf("ClassifySecond PostEdbLib err: %s", e.Error())
  461. return
  462. }
  463. resp2 := new(models.ClassifyResp)
  464. if e = json.Unmarshal(res2, &resp2); e != nil {
  465. err = fmt.Errorf("ClassifySecond json unmarshal err: %s", e.Error())
  466. return
  467. }
  468. if resp2.Ret != 200 {
  469. err = fmt.Errorf("ClassifySecond resp msg: %s; errMsg: %s", resp2.Msg, resp2.ErrMsg)
  470. return
  471. }
  472. secondId = resp2.Data.ClassifyId
  473. lastId = secondId
  474. }
  475. // 三级分类
  476. if classifyThree != "" {
  477. classifyThreeMap := make(map[string]interface{})
  478. classifyThreeMap["ClassifyName"] = classifyThree
  479. classifyThreeMap["ParentId"] = secondId
  480. classifyThreeMap["Level"] = 2
  481. classifyThreeMap["ClassifyType"] = 0
  482. res3, e := PostEdbLib(classifyThreeMap, method)
  483. if e != nil {
  484. err = fmt.Errorf("ClassifyThird PostEdbLib err: %s", e.Error())
  485. return
  486. }
  487. resp3 := new(models.ClassifyResp)
  488. if e = json.Unmarshal(res3, &resp3); e != nil {
  489. err = fmt.Errorf("ClassifyThird json unmarshal err: %s", e.Error())
  490. return
  491. }
  492. if resp3.Ret != 200 {
  493. err = fmt.Errorf("ClassifyThird resp msg: %s; errMsg: %s", resp3.Msg, resp3.ErrMsg)
  494. return
  495. }
  496. thirdId = resp3.Data.ClassifyId
  497. lastId = thirdId
  498. }
  499. // 四级分类
  500. if classifyFourth != "" {
  501. classifyFourthMap := make(map[string]interface{})
  502. classifyFourthMap["ClassifyName"] = classifyFourth
  503. classifyFourthMap["ParentId"] = thirdId
  504. classifyFourthMap["Level"] = 3
  505. classifyFourthMap["ClassifyType"] = 0
  506. res4, e := PostEdbLib(classifyFourthMap, method)
  507. if e != nil {
  508. err = fmt.Errorf("ClassifyFourth PostEdbLib err: %s", e.Error())
  509. return
  510. }
  511. resp4 := new(models.ClassifyResp)
  512. if e = json.Unmarshal(res4, &resp4); e != nil {
  513. err = fmt.Errorf("ClassifyFourth json unmarshal err: %s", e.Error())
  514. return
  515. }
  516. if resp4.Ret != 200 {
  517. err = fmt.Errorf("ClassifyFourth resp msg: %s; errMsg: %s", resp4.Msg, resp4.ErrMsg)
  518. return
  519. }
  520. fourthId = resp4.Data.ClassifyId
  521. lastId = fourthId
  522. }
  523. // 五级分类
  524. if classifyFifth != "" {
  525. classifyFifthMap := make(map[string]interface{})
  526. classifyFifthMap["ClassifyName"] = classifyFifth
  527. classifyFifthMap["ParentId"] = fourthId
  528. classifyFifthMap["Level"] = 4
  529. classifyFifthMap["ClassifyType"] = 0
  530. res5, e := PostEdbLib(classifyFifthMap, method)
  531. if e != nil {
  532. err = fmt.Errorf("ClassifyFifth PostEdbLib err: %s", e.Error())
  533. return
  534. }
  535. resp5 := new(models.ClassifyResp)
  536. if e = json.Unmarshal(res5, &resp5); e != nil {
  537. err = fmt.Errorf("ClassifyFifth json unmarshal err: %s", e.Error())
  538. return
  539. }
  540. if resp5.Ret != 200 {
  541. err = fmt.Errorf("ClassifyFifth resp msg: %s; errMsg: %s", resp5.Msg, resp5.ErrMsg)
  542. return
  543. }
  544. fifthId = resp5.Data.ClassifyId
  545. lastId = fifthId
  546. }
  547. // 六级分类
  548. if classifySixth != "" {
  549. classifySixthMap := make(map[string]interface{})
  550. classifySixthMap["ClassifyName"] = classifySixth
  551. classifySixthMap["ParentId"] = fifthId
  552. classifySixthMap["Level"] = 5
  553. classifySixthMap["ClassifyType"] = 0
  554. res6, e := PostEdbLib(classifySixthMap, method)
  555. if e != nil {
  556. err = fmt.Errorf("ClassifySixth PostEdbLib err: %s", e.Error())
  557. return
  558. }
  559. resp6 := new(models.ClassifyResp)
  560. if e = json.Unmarshal(res6, &resp6); e != nil {
  561. err = fmt.Errorf("ClassifySixth json unmarshal err: %s", e.Error())
  562. return
  563. }
  564. if resp6.Ret != 200 {
  565. err = fmt.Errorf("ClassifySixth resp msg: %s; errMsg: %s", resp6.Msg, resp6.ErrMsg)
  566. return
  567. }
  568. lastId = resp6.Data.ClassifyId
  569. }
  570. method = "edb_info/add"
  571. indexMap := make(map[string]interface{})
  572. indexMap["Source"] = IndexSourceMap[source]
  573. indexMap["EdbCode"] = indexCode
  574. indexMap["EdbName"] = indexName
  575. indexMap["Frequency"] = frequency
  576. indexMap["Unit"] = unit
  577. indexMap["ClassifyId"] = lastId
  578. admin := mobileMap[mobile]
  579. if admin != nil {
  580. indexMap["AdminId"] = admin.AdminId
  581. indexMap["AdminName"] = admin.RealName
  582. }
  583. result, err = PostEdbLib(indexMap, method)
  584. if err != nil {
  585. utils.FileLog.Info("初始化指标失败:" + err.Error() + " result:" + string(result))
  586. return
  587. }
  588. indexResp := new(models.EdbInfoResp)
  589. err = json.Unmarshal(result, &indexResp)
  590. if err != nil {
  591. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  592. return
  593. }
  594. if indexResp.Ret != 200 {
  595. if strings.Contains(indexResp.Msg, "新增指标失败") {
  596. continue
  597. } else {
  598. fmt.Println("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  599. utils.FileLog.Info("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  600. return
  601. }
  602. }
  603. fmt.Println("add index success:" + indexCode)
  604. //刷新指标
  605. method = "mysteel_chemical/refresh"
  606. refreshMap := make(map[string]interface{})
  607. refreshMap["EdbInfoId"] = indexResp.Data.EdbInfoId
  608. refreshMap["EdbCode"] = indexCode
  609. refreshMap["StartDate"] = "1990-01-01"
  610. PostEdbLib(refreshMap, method)
  611. } else {
  612. fmt.Println("data is empty")
  613. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
  614. }
  615. }
  616. }
  617. }
  618. // 初始化基础指标数据-有色
  619. func InitBaseIndexDataFromDataSourceSmm(filePath string) {
  620. var err error
  621. defer func() {
  622. if err != nil {
  623. fmt.Println("InitBaseIndexDataFromDataSourceSmm Err:" + err.Error())
  624. utils.FileLog.Info("InitBaseIndexDataFromDataSourceSmm Err:" + err.Error())
  625. }
  626. }()
  627. //读取excel
  628. path, err := filepath.Abs(os.Args[0])
  629. if err != nil {
  630. fmt.Println(err)
  631. }
  632. dir := filepath.Dir(path)
  633. fmt.Println("dir:" + dir)
  634. dataPath := dir + filePath
  635. fmt.Println("dataPath:" + dataPath)
  636. f, err := excelize.OpenFile(dataPath)
  637. if err != nil {
  638. fmt.Println(err)
  639. return
  640. }
  641. defer func() {
  642. // Close the spreadsheet.
  643. if err := f.Close(); err != nil {
  644. fmt.Println(err)
  645. }
  646. }()
  647. rows, err := f.GetRows("Sheet1")
  648. if err != nil {
  649. fmt.Println(err)
  650. return
  651. }
  652. fmt.Println("rows len:", len(rows))
  653. // 获取创建人信息
  654. mobileMap := make(map[string]*models.Admin)
  655. if utils.MYSQL_URL_ETA != "" {
  656. admins, e := models.GetSysAdminList(``, make([]interface{}, 0))
  657. if e != nil {
  658. err = fmt.Errorf("GetSysAdminList err: %s", e.Error())
  659. return
  660. }
  661. for _, v := range admins {
  662. if v.Mobile == "" {
  663. continue
  664. }
  665. mobileMap[v.Mobile] = v
  666. }
  667. }
  668. for rk, row := range rows {
  669. if rk > 0 {
  670. var classifyFirst, classifySecond, classifyThree, classifyFourth, classifyFifth, classifySixth, indexCode, indexName, frequency, unit, source, mobile string
  671. for ck, colCell := range row {
  672. colCell = strings.TrimSpace(colCell)
  673. switch ck {
  674. case 0:
  675. classifyFirst = colCell
  676. case 1:
  677. classifySecond = colCell
  678. case 2:
  679. classifyThree = colCell
  680. case 3:
  681. classifyFourth = colCell
  682. case 4:
  683. classifyFifth = colCell
  684. case 5:
  685. classifySixth = colCell
  686. case 6:
  687. indexCode = colCell
  688. case 7:
  689. indexName = colCell
  690. case 8:
  691. frequency = colCell
  692. case 9:
  693. unit = colCell
  694. case 10:
  695. source = colCell
  696. case 11:
  697. mobile = colCell
  698. }
  699. }
  700. if classifyFirst != "" &&
  701. indexCode != "" &&
  702. indexName != "" &&
  703. unit != "" &&
  704. frequency != "" &&
  705. source != "" {
  706. //判断指标是否存在
  707. method := "smm/index_detail/from_data_source"
  708. indexSearchMap := make(map[string]interface{})
  709. indexSearchMap["IndexCode"] = indexCode
  710. indexResult, e := PostEdbLib(indexSearchMap, method)
  711. if e != nil {
  712. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  713. return
  714. }
  715. smmIndexResp := new(models.BaseFromSmmIndexResp)
  716. e = json.Unmarshal(indexResult, &smmIndexResp)
  717. if e != nil {
  718. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  719. return
  720. }
  721. if smmIndexResp.Ret != 200 {
  722. fmt.Println("判断指标是否存在失败,Err:" + smmIndexResp.ErrMsg)
  723. //utils.FileLog.Info("判断指标是否存在失败:" + err.Error())
  724. //return
  725. continue
  726. }
  727. if smmIndexResp.Data.BaseFromSmmIndexId <= 0 {
  728. fmt.Println("指标:" + indexCode + ";不存在")
  729. continue
  730. }
  731. var firstId, secondId, thirdId, fourthId, fifthId, lastId int
  732. method = "classify/get_or_add"
  733. classifyFirstMap := make(map[string]interface{})
  734. classifyFirstMap["ClassifyName"] = classifyFirst
  735. classifyFirstMap["ParentId"] = 0
  736. classifyFirstMap["Level"] = 0
  737. classifyFirstMap["ClassifyType"] = 0
  738. result, e := PostEdbLib(classifyFirstMap, method)
  739. if e != nil {
  740. err = fmt.Errorf("ClassifyFirst PostEdbLib err: %s", e.Error())
  741. return
  742. }
  743. resp := new(models.ClassifyResp)
  744. if e = json.Unmarshal(result, &resp); e != nil {
  745. err = fmt.Errorf("ClassifyFirst json unmarshal err: %s", e.Error())
  746. return
  747. }
  748. if resp.Ret != 200 {
  749. err = fmt.Errorf("ClassifyFirst resp msg: %s; errMsg: %s", resp.Msg, resp.ErrMsg)
  750. return
  751. }
  752. firstId = resp.Data.ClassifyId
  753. lastId = firstId
  754. // 二级分类
  755. if classifySecond != "" {
  756. classifySecondMap := make(map[string]interface{})
  757. classifySecondMap["ClassifyName"] = classifySecond
  758. classifySecondMap["ParentId"] = firstId
  759. classifySecondMap["Level"] = 1
  760. classifySecondMap["ClassifyType"] = 0
  761. res2, e := PostEdbLib(classifySecondMap, method)
  762. if e != nil {
  763. err = fmt.Errorf("ClassifySecond PostEdbLib err: %s", e.Error())
  764. return
  765. }
  766. resp2 := new(models.ClassifyResp)
  767. if e = json.Unmarshal(res2, &resp2); e != nil {
  768. err = fmt.Errorf("ClassifySecond json unmarshal err: %s", e.Error())
  769. return
  770. }
  771. if resp2.Ret != 200 {
  772. err = fmt.Errorf("ClassifySecond resp msg: %s; errMsg: %s", resp2.Msg, resp2.ErrMsg)
  773. return
  774. }
  775. secondId = resp2.Data.ClassifyId
  776. lastId = secondId
  777. }
  778. // 三级分类
  779. if classifyThree != "" {
  780. classifyThreeMap := make(map[string]interface{})
  781. classifyThreeMap["ClassifyName"] = classifyThree
  782. classifyThreeMap["ParentId"] = secondId
  783. classifyThreeMap["Level"] = 2
  784. classifyThreeMap["ClassifyType"] = 0
  785. res3, e := PostEdbLib(classifyThreeMap, method)
  786. if e != nil {
  787. err = fmt.Errorf("ClassifyThird PostEdbLib err: %s", e.Error())
  788. return
  789. }
  790. resp3 := new(models.ClassifyResp)
  791. if e = json.Unmarshal(res3, &resp3); e != nil {
  792. err = fmt.Errorf("ClassifyThird json unmarshal err: %s", e.Error())
  793. return
  794. }
  795. if resp3.Ret != 200 {
  796. err = fmt.Errorf("ClassifyThird resp msg: %s; errMsg: %s", resp3.Msg, resp3.ErrMsg)
  797. return
  798. }
  799. thirdId = resp3.Data.ClassifyId
  800. lastId = thirdId
  801. }
  802. // 四级分类
  803. if classifyFourth != "" {
  804. classifyFourthMap := make(map[string]interface{})
  805. classifyFourthMap["ClassifyName"] = classifyFourth
  806. classifyFourthMap["ParentId"] = thirdId
  807. classifyFourthMap["Level"] = 3
  808. classifyFourthMap["ClassifyType"] = 0
  809. res4, e := PostEdbLib(classifyFourthMap, method)
  810. if e != nil {
  811. err = fmt.Errorf("ClassifyFourth PostEdbLib err: %s", e.Error())
  812. return
  813. }
  814. resp4 := new(models.ClassifyResp)
  815. if e = json.Unmarshal(res4, &resp4); e != nil {
  816. err = fmt.Errorf("ClassifyFourth json unmarshal err: %s", e.Error())
  817. return
  818. }
  819. if resp4.Ret != 200 {
  820. err = fmt.Errorf("ClassifyFourth resp msg: %s; errMsg: %s", resp4.Msg, resp4.ErrMsg)
  821. return
  822. }
  823. fourthId = resp4.Data.ClassifyId
  824. lastId = fourthId
  825. }
  826. // 五级分类
  827. if classifyFifth != "" {
  828. classifyFifthMap := make(map[string]interface{})
  829. classifyFifthMap["ClassifyName"] = classifyFifth
  830. classifyFifthMap["ParentId"] = fourthId
  831. classifyFifthMap["Level"] = 4
  832. classifyFifthMap["ClassifyType"] = 0
  833. res5, e := PostEdbLib(classifyFifthMap, method)
  834. if e != nil {
  835. err = fmt.Errorf("ClassifyFifth PostEdbLib err: %s", e.Error())
  836. return
  837. }
  838. resp5 := new(models.ClassifyResp)
  839. if e = json.Unmarshal(res5, &resp5); e != nil {
  840. err = fmt.Errorf("ClassifyFifth json unmarshal err: %s", e.Error())
  841. return
  842. }
  843. if resp5.Ret != 200 {
  844. err = fmt.Errorf("ClassifyFifth resp msg: %s; errMsg: %s", resp5.Msg, resp5.ErrMsg)
  845. return
  846. }
  847. fifthId = resp5.Data.ClassifyId
  848. lastId = fifthId
  849. }
  850. // 六级分类
  851. if classifySixth != "" {
  852. classifySixthMap := make(map[string]interface{})
  853. classifySixthMap["ClassifyName"] = classifySixth
  854. classifySixthMap["ParentId"] = fifthId
  855. classifySixthMap["Level"] = 5
  856. classifySixthMap["ClassifyType"] = 0
  857. res6, e := PostEdbLib(classifySixthMap, method)
  858. if e != nil {
  859. err = fmt.Errorf("ClassifySixth PostEdbLib err: %s", e.Error())
  860. return
  861. }
  862. resp6 := new(models.ClassifyResp)
  863. if e = json.Unmarshal(res6, &resp6); e != nil {
  864. err = fmt.Errorf("ClassifySixth json unmarshal err: %s", e.Error())
  865. return
  866. }
  867. if resp6.Ret != 200 {
  868. err = fmt.Errorf("ClassifySixth resp msg: %s; errMsg: %s", resp6.Msg, resp6.ErrMsg)
  869. return
  870. }
  871. lastId = resp6.Data.ClassifyId
  872. }
  873. method = "edb_info/add"
  874. indexMap := make(map[string]interface{})
  875. indexMap["Source"] = IndexSourceMap[source]
  876. indexMap["EdbCode"] = indexCode
  877. indexMap["EdbName"] = indexName
  878. indexMap["Frequency"] = frequency
  879. indexMap["Unit"] = unit
  880. indexMap["ClassifyId"] = lastId
  881. admin := mobileMap[mobile]
  882. if admin != nil {
  883. indexMap["AdminId"] = admin.AdminId
  884. indexMap["AdminName"] = admin.RealName
  885. }
  886. result, err = PostEdbLib(indexMap, method)
  887. if err != nil {
  888. utils.FileLog.Info("初始化指标失败:" + err.Error() + " result:" + string(result))
  889. return
  890. }
  891. indexResp := new(models.EdbInfoResp)
  892. err = json.Unmarshal(result, &indexResp)
  893. if err != nil {
  894. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  895. return
  896. }
  897. if indexResp.Ret != 200 {
  898. if strings.Contains(indexResp.Msg, "新增指标失败") {
  899. continue
  900. } else {
  901. fmt.Println("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  902. utils.FileLog.Info("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  903. return
  904. }
  905. }
  906. fmt.Println("add index success:" + indexCode)
  907. //刷新指标
  908. {
  909. refreshMethod := "smm/refresh"
  910. refreshMap := make(map[string]interface{})
  911. refreshMap["EdbInfoId"] = indexResp.Data.EdbInfoId
  912. refreshMap["EdbCode"] = indexCode
  913. refreshMap["StartDate"] = "1990-01-01"
  914. PostEdbLib(refreshMap, refreshMethod)
  915. }
  916. } else {
  917. fmt.Println("data is empty")
  918. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
  919. }
  920. }
  921. }
  922. }
  923. // InitDataToEdbInfo
  924. // @Description: 添加指标到指标库
  925. // @author: Roc
  926. // @datetime 2024-01-15 14:12:12
  927. // @param filePath string
  928. func InitDataToEdbInfo(filePath string) {
  929. var err error
  930. defer func() {
  931. if err != nil {
  932. fmt.Println("InitDataToEdbInfo Err:" + err.Error())
  933. utils.FileLog.Info("InitDataToEdbInfo Err:" + err.Error())
  934. }
  935. }()
  936. //读取excel
  937. path, err := filepath.Abs(os.Args[0])
  938. if err != nil {
  939. fmt.Println(err)
  940. }
  941. dir := filepath.Dir(path)
  942. fmt.Println("dir:" + dir)
  943. dataPath := dir + filePath
  944. fmt.Println("dataPath:" + dataPath)
  945. f, err := excelize.OpenFile(dataPath)
  946. if err != nil {
  947. fmt.Println(err)
  948. return
  949. }
  950. defer func() {
  951. // Close the spreadsheet.
  952. if err := f.Close(); err != nil {
  953. fmt.Println(err)
  954. }
  955. }()
  956. rows, err := f.GetRows("Sheet1")
  957. if err != nil {
  958. fmt.Println(err)
  959. return
  960. }
  961. fmt.Println("rows len:", len(rows))
  962. // 获取创建人信息
  963. mobileMap := make(map[string]*models.Admin)
  964. if utils.MYSQL_URL_ETA != "" {
  965. admins, e := models.GetSysAdminList(``, make([]interface{}, 0))
  966. if e != nil {
  967. err = fmt.Errorf("GetSysAdminList err: %s", e.Error())
  968. return
  969. }
  970. for _, v := range admins {
  971. if v.Mobile == "" {
  972. continue
  973. }
  974. mobileMap[v.Mobile] = v
  975. }
  976. }
  977. for rk, row := range rows {
  978. if rk > 0 {
  979. var classifyFirst, classifySecond, classifyThree, classifyFourth, classifyFifth, classifySixth, indexCode, indexName, frequency, unit, source, mobile, terminalCode string
  980. for ck, colCell := range row {
  981. colCell = strings.TrimSpace(colCell)
  982. switch ck {
  983. case 0:
  984. classifyFirst = colCell
  985. case 1:
  986. classifySecond = colCell
  987. case 2:
  988. classifyThree = colCell
  989. case 3:
  990. classifyFourth = colCell
  991. case 4:
  992. classifyFifth = colCell
  993. case 5:
  994. classifySixth = colCell
  995. case 6:
  996. indexCode = colCell
  997. case 7:
  998. indexName = colCell
  999. case 8:
  1000. frequency = colCell
  1001. case 9:
  1002. unit = colCell
  1003. case 10:
  1004. source = colCell
  1005. case 11:
  1006. mobile = colCell
  1007. case 12:
  1008. terminalCode = colCell
  1009. }
  1010. }
  1011. // 校验excel文件内容
  1012. {
  1013. errMsgList := make([]string, 0)
  1014. if classifyFirst == "" {
  1015. errMsgList = append(errMsgList, `一级目录不允许为空`)
  1016. }
  1017. if indexCode == "" {
  1018. errMsgList = append(errMsgList, `指标编码不允许为空`)
  1019. }
  1020. if indexName == "" {
  1021. errMsgList = append(errMsgList, `指标名称不允许为空`)
  1022. }
  1023. if frequency == "" {
  1024. errMsgList = append(errMsgList, `频度不允许为空`)
  1025. }
  1026. if unit == "" {
  1027. errMsgList = append(errMsgList, `单位不允许为空`)
  1028. }
  1029. if source == "" {
  1030. errMsgList = append(errMsgList, `指标来源不允许为空`)
  1031. }
  1032. if terminalCode == "" {
  1033. errMsgList = append(errMsgList, `终端号不允许为空`)
  1034. }
  1035. // 如果有错误信息,那么就不继续执行
  1036. if len(errMsgList) > 0 {
  1037. fmt.Println(strings.Join(errMsgList, ";"))
  1038. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source, terminalCode)
  1039. continue
  1040. }
  1041. }
  1042. // 开始入库
  1043. {
  1044. //判断指标是否存在
  1045. switch source {
  1046. case "钢联":
  1047. ok, e := VerifyMysteelIndex(indexCode)
  1048. if e != nil {
  1049. fmt.Println(indexCode, ";判断指标是否存在失败,Err:"+e.Error())
  1050. continue
  1051. }
  1052. if !ok {
  1053. fmt.Println("指标:" + indexCode + ";不存在")
  1054. continue
  1055. }
  1056. case "SMM":
  1057. ok, e := VerifySmmIndex(indexCode)
  1058. if e != nil {
  1059. fmt.Println(indexCode, ";判断指标是否存在失败,Err:"+e.Error())
  1060. continue
  1061. }
  1062. if !ok {
  1063. fmt.Println("指标:" + indexCode + ";不存在")
  1064. continue
  1065. }
  1066. }
  1067. var firstId, secondId, thirdId, fourthId, fifthId, lastId int
  1068. method := "classify/get_or_add"
  1069. classifyFirstMap := make(map[string]interface{})
  1070. classifyFirstMap["ClassifyName"] = classifyFirst
  1071. classifyFirstMap["ParentId"] = 0
  1072. classifyFirstMap["Level"] = 0
  1073. classifyFirstMap["ClassifyType"] = 0
  1074. result, e := PostEdbLib(classifyFirstMap, method)
  1075. if e != nil {
  1076. err = fmt.Errorf("ClassifyFirst PostEdbLib err: %s", e.Error())
  1077. return
  1078. }
  1079. resp := new(models.ClassifyResp)
  1080. if e = json.Unmarshal(result, &resp); e != nil {
  1081. err = fmt.Errorf("ClassifyFirst json unmarshal err: %s", e.Error())
  1082. return
  1083. }
  1084. if resp.Ret != 200 {
  1085. err = fmt.Errorf("ClassifyFirst resp msg: %s; errMsg: %s", resp.Msg, resp.ErrMsg)
  1086. return
  1087. }
  1088. firstId = resp.Data.ClassifyId
  1089. lastId = firstId
  1090. // 二级分类
  1091. if classifySecond != "" {
  1092. classifySecondMap := make(map[string]interface{})
  1093. classifySecondMap["ClassifyName"] = classifySecond
  1094. classifySecondMap["ParentId"] = firstId
  1095. classifySecondMap["Level"] = 1
  1096. classifySecondMap["ClassifyType"] = 0
  1097. res2, e := PostEdbLib(classifySecondMap, method)
  1098. if e != nil {
  1099. err = fmt.Errorf("ClassifySecond PostEdbLib err: %s", e.Error())
  1100. return
  1101. }
  1102. resp2 := new(models.ClassifyResp)
  1103. if e = json.Unmarshal(res2, &resp2); e != nil {
  1104. err = fmt.Errorf("ClassifySecond json unmarshal err: %s", e.Error())
  1105. return
  1106. }
  1107. if resp2.Ret != 200 {
  1108. err = fmt.Errorf("ClassifySecond resp msg: %s; errMsg: %s", resp2.Msg, resp2.ErrMsg)
  1109. return
  1110. }
  1111. secondId = resp2.Data.ClassifyId
  1112. lastId = secondId
  1113. }
  1114. // 三级分类
  1115. if classifyThree != "" {
  1116. classifyThreeMap := make(map[string]interface{})
  1117. classifyThreeMap["ClassifyName"] = classifyThree
  1118. classifyThreeMap["ParentId"] = secondId
  1119. classifyThreeMap["Level"] = 2
  1120. classifyThreeMap["ClassifyType"] = 0
  1121. res3, e := PostEdbLib(classifyThreeMap, method)
  1122. if e != nil {
  1123. err = fmt.Errorf("ClassifyThird PostEdbLib err: %s", e.Error())
  1124. return
  1125. }
  1126. resp3 := new(models.ClassifyResp)
  1127. if e = json.Unmarshal(res3, &resp3); e != nil {
  1128. err = fmt.Errorf("ClassifyThird json unmarshal err: %s", e.Error())
  1129. return
  1130. }
  1131. if resp3.Ret != 200 {
  1132. err = fmt.Errorf("ClassifyThird resp msg: %s; errMsg: %s", resp3.Msg, resp3.ErrMsg)
  1133. return
  1134. }
  1135. thirdId = resp3.Data.ClassifyId
  1136. lastId = thirdId
  1137. }
  1138. // 四级分类
  1139. if classifyFourth != "" {
  1140. classifyFourthMap := make(map[string]interface{})
  1141. classifyFourthMap["ClassifyName"] = classifyFourth
  1142. classifyFourthMap["ParentId"] = thirdId
  1143. classifyFourthMap["Level"] = 3
  1144. classifyFourthMap["ClassifyType"] = 0
  1145. res4, e := PostEdbLib(classifyFourthMap, method)
  1146. if e != nil {
  1147. err = fmt.Errorf("ClassifyFourth PostEdbLib err: %s", e.Error())
  1148. return
  1149. }
  1150. resp4 := new(models.ClassifyResp)
  1151. if e = json.Unmarshal(res4, &resp4); e != nil {
  1152. err = fmt.Errorf("ClassifyFourth json unmarshal err: %s", e.Error())
  1153. return
  1154. }
  1155. if resp4.Ret != 200 {
  1156. err = fmt.Errorf("ClassifyFourth resp msg: %s; errMsg: %s", resp4.Msg, resp4.ErrMsg)
  1157. return
  1158. }
  1159. fourthId = resp4.Data.ClassifyId
  1160. lastId = fourthId
  1161. }
  1162. // 五级分类
  1163. if classifyFifth != "" {
  1164. classifyFifthMap := make(map[string]interface{})
  1165. classifyFifthMap["ClassifyName"] = classifyFifth
  1166. classifyFifthMap["ParentId"] = fourthId
  1167. classifyFifthMap["Level"] = 4
  1168. classifyFifthMap["ClassifyType"] = 0
  1169. res5, e := PostEdbLib(classifyFifthMap, method)
  1170. if e != nil {
  1171. err = fmt.Errorf("ClassifyFifth PostEdbLib err: %s", e.Error())
  1172. return
  1173. }
  1174. resp5 := new(models.ClassifyResp)
  1175. if e = json.Unmarshal(res5, &resp5); e != nil {
  1176. err = fmt.Errorf("ClassifyFifth json unmarshal err: %s", e.Error())
  1177. return
  1178. }
  1179. if resp5.Ret != 200 {
  1180. err = fmt.Errorf("ClassifyFifth resp msg: %s; errMsg: %s", resp5.Msg, resp5.ErrMsg)
  1181. return
  1182. }
  1183. fifthId = resp5.Data.ClassifyId
  1184. lastId = fifthId
  1185. }
  1186. // 六级分类
  1187. if classifySixth != "" {
  1188. classifySixthMap := make(map[string]interface{})
  1189. classifySixthMap["ClassifyName"] = classifySixth
  1190. classifySixthMap["ParentId"] = fifthId
  1191. classifySixthMap["Level"] = 5
  1192. classifySixthMap["ClassifyType"] = 0
  1193. res6, e := PostEdbLib(classifySixthMap, method)
  1194. if e != nil {
  1195. err = fmt.Errorf("ClassifySixth PostEdbLib err: %s", e.Error())
  1196. return
  1197. }
  1198. resp6 := new(models.ClassifyResp)
  1199. if e = json.Unmarshal(res6, &resp6); e != nil {
  1200. err = fmt.Errorf("ClassifySixth json unmarshal err: %s", e.Error())
  1201. return
  1202. }
  1203. if resp6.Ret != 200 {
  1204. err = fmt.Errorf("ClassifySixth resp msg: %s; errMsg: %s", resp6.Msg, resp6.ErrMsg)
  1205. return
  1206. }
  1207. lastId = resp6.Data.ClassifyId
  1208. }
  1209. // 添加指标
  1210. method = "edb_info/add"
  1211. sourceId, ok := IndexSourceMap[source]
  1212. if !ok {
  1213. fmt.Println("source is not defined")
  1214. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
  1215. continue
  1216. }
  1217. indexMap := make(map[string]interface{})
  1218. indexMap["Source"] = sourceId
  1219. indexMap["EdbCode"] = indexCode
  1220. indexMap["EdbName"] = indexName
  1221. indexMap["Frequency"] = frequency
  1222. indexMap["Unit"] = unit
  1223. indexMap["ClassifyId"] = lastId
  1224. indexMap["TerminalCode"] = terminalCode
  1225. admin := mobileMap[mobile]
  1226. if admin != nil {
  1227. indexMap["AdminId"] = admin.AdminId
  1228. indexMap["AdminName"] = admin.RealName
  1229. }
  1230. result, err = PostEdbLib(indexMap, method)
  1231. if err != nil {
  1232. utils.FileLog.Info("初始化指标失败:" + err.Error() + " result:" + string(result))
  1233. return
  1234. }
  1235. indexResp := new(models.EdbInfoResp)
  1236. err = json.Unmarshal(result, &indexResp)
  1237. if err != nil {
  1238. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  1239. return
  1240. }
  1241. if indexResp.Ret != 200 {
  1242. if strings.Contains(indexResp.Msg, "新增指标失败") {
  1243. continue
  1244. } else {
  1245. fmt.Println("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  1246. utils.FileLog.Info("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  1247. return
  1248. }
  1249. }
  1250. fmt.Println("add index success:" + indexCode)
  1251. //刷新指标
  1252. method, ok = IndexSourceRefreshMap[sourceId]
  1253. if !ok {
  1254. fmt.Println("source refresh path is not defined")
  1255. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
  1256. continue
  1257. }
  1258. refreshMap := make(map[string]interface{})
  1259. refreshMap["EdbInfoId"] = indexResp.Data.EdbInfoId
  1260. refreshMap["EdbCode"] = indexCode
  1261. refreshMap["StartDate"] = "1990-01-01"
  1262. PostEdbLib(refreshMap, method)
  1263. }
  1264. }
  1265. }
  1266. }
  1267. // VerifyMysteelIndex
  1268. // @Description: 判断钢联指标编码是否已经入库
  1269. // @author: Roc
  1270. // @datetime 2024-01-15 13:54:48
  1271. // @param indexCode string
  1272. // @return ok bool
  1273. // @return err error
  1274. func VerifyMysteelIndex(indexCode string) (ok bool, err error) {
  1275. //判断指标是否存在
  1276. method := "mysteel_chemical/index_detail"
  1277. indexSearchMap := make(map[string]interface{})
  1278. indexSearchMap["IndexCode"] = indexCode
  1279. indexResult, e := PostEdbLib(indexSearchMap, method)
  1280. if e != nil {
  1281. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  1282. return
  1283. }
  1284. mysteelIndexResp := new(models.MysteelIndexResp)
  1285. e = json.Unmarshal(indexResult, &mysteelIndexResp)
  1286. if e != nil {
  1287. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  1288. return
  1289. }
  1290. if mysteelIndexResp.Ret != 200 {
  1291. err = errors.New("判断指标是否存在失败,Err:" + mysteelIndexResp.ErrMsg)
  1292. return
  1293. }
  1294. if mysteelIndexResp.Data.BaseFromMysteelChemicalIndexId <= 0 {
  1295. fmt.Println("指标:" + indexCode + ";不存在")
  1296. return
  1297. }
  1298. ok = true
  1299. return
  1300. }
  1301. // VerifySmmIndex
  1302. // @Description: 判断有色指标编码是否已经入库
  1303. // @author: Roc
  1304. // @datetime 2024-01-15 13:54:48
  1305. // @param indexCode string
  1306. // @return ok bool
  1307. // @return err error
  1308. func VerifySmmIndex(indexCode string) (ok bool, err error) {
  1309. //判断指标是否存在
  1310. method := "smm/index_detail/from_data_source"
  1311. indexSearchMap := make(map[string]interface{})
  1312. indexSearchMap["IndexCode"] = indexCode
  1313. indexResult, e := PostEdbLib(indexSearchMap, method)
  1314. if e != nil {
  1315. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  1316. return
  1317. }
  1318. smmIndexResp := new(models.BaseFromSmmIndexResp)
  1319. e = json.Unmarshal(indexResult, &smmIndexResp)
  1320. if e != nil {
  1321. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  1322. return
  1323. }
  1324. if smmIndexResp.Ret != 200 {
  1325. err = errors.New("判断指标是否存在失败,Err:" + smmIndexResp.ErrMsg)
  1326. return
  1327. }
  1328. if smmIndexResp.Data.BaseFromSmmIndexId <= 0 {
  1329. //fmt.Println("指标:" + indexCode + ";不存在")
  1330. return
  1331. }
  1332. ok = true
  1333. return
  1334. }