init_base_index.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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. //successIndexList := make([]string, 0)
  978. errorIndexList := make([]string, 0)
  979. for rk, row := range rows {
  980. if rk > 0 {
  981. var classifyFirst, classifySecond, classifyThree, classifyFourth, classifyFifth, classifySixth, indexCode, indexName, frequency, unit, source, mobile, terminalCode string
  982. for ck, colCell := range row {
  983. colCell = strings.TrimSpace(colCell)
  984. switch ck {
  985. case 0:
  986. classifyFirst = colCell
  987. case 1:
  988. classifySecond = colCell
  989. case 2:
  990. classifyThree = colCell
  991. case 3:
  992. classifyFourth = colCell
  993. case 4:
  994. classifyFifth = colCell
  995. case 5:
  996. classifySixth = colCell
  997. case 6:
  998. indexCode = colCell
  999. case 7:
  1000. indexName = colCell
  1001. case 8:
  1002. frequency = colCell
  1003. case 9:
  1004. unit = colCell
  1005. case 10:
  1006. source = colCell
  1007. case 11:
  1008. mobile = colCell
  1009. case 12:
  1010. terminalCode = colCell
  1011. }
  1012. }
  1013. // 校验excel文件内容
  1014. {
  1015. tmpErrMsgList := make([]string, 0)
  1016. if classifyFirst == "" {
  1017. tmpErrMsgList = append(tmpErrMsgList, `一级目录不允许为空`)
  1018. }
  1019. if indexCode == "" {
  1020. tmpErrMsgList = append(tmpErrMsgList, `指标编码不允许为空`)
  1021. }
  1022. if indexName == "" {
  1023. tmpErrMsgList = append(tmpErrMsgList, `指标名称不允许为空`)
  1024. }
  1025. if frequency == "" {
  1026. tmpErrMsgList = append(tmpErrMsgList, `频度不允许为空`)
  1027. }
  1028. if unit == "" {
  1029. tmpErrMsgList = append(tmpErrMsgList, `单位不允许为空`)
  1030. }
  1031. if source == "" {
  1032. tmpErrMsgList = append(tmpErrMsgList, `指标来源不允许为空`)
  1033. }
  1034. if terminalCode == "" {
  1035. tmpErrMsgList = append(tmpErrMsgList, `终端号不允许为空`)
  1036. }
  1037. // 如果有错误信息,那么就不继续执行
  1038. if len(tmpErrMsgList) > 0 {
  1039. fmt.Println(strings.Join(tmpErrMsgList, ";"))
  1040. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source, terminalCode)
  1041. continue
  1042. }
  1043. }
  1044. // 开始入库
  1045. {
  1046. //判断指标是否存在
  1047. switch source {
  1048. case "钢联":
  1049. ok, e := VerifyMysteelIndex(indexCode)
  1050. if e != nil {
  1051. fmt.Println(indexCode, ";判断指标是否存在失败,Err:"+e.Error())
  1052. continue
  1053. }
  1054. if !ok {
  1055. fmt.Println("指标:" + indexCode + ";不存在")
  1056. continue
  1057. }
  1058. case "SMM":
  1059. ok, e := VerifySmmIndex(indexCode)
  1060. if e != nil {
  1061. fmt.Println(indexCode, ";判断指标是否存在失败,Err:"+e.Error())
  1062. continue
  1063. }
  1064. if !ok {
  1065. fmt.Println("指标:" + indexCode + ";不存在")
  1066. continue
  1067. }
  1068. }
  1069. // 开始添加分类
  1070. var firstId, secondId, thirdId, fourthId, fifthId, lastId int
  1071. method := "classify/get_or_add"
  1072. classifyFirstMap := make(map[string]interface{})
  1073. classifyFirstMap["ClassifyName"] = classifyFirst
  1074. classifyFirstMap["ParentId"] = 0
  1075. classifyFirstMap["Level"] = 0
  1076. classifyFirstMap["ClassifyType"] = 0
  1077. result, e := PostEdbLib(classifyFirstMap, method)
  1078. if e != nil {
  1079. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifyFirst PostEdbLib err: %s", indexCode, e.Error()))
  1080. continue
  1081. }
  1082. resp := new(models.ClassifyResp)
  1083. if e = json.Unmarshal(result, &resp); e != nil {
  1084. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifyFirst json unmarshal err: %s", indexCode, e.Error()))
  1085. continue
  1086. }
  1087. if resp.Ret != 200 {
  1088. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifyFirst resp msg: %s; errMsg: %s", indexCode, resp.Msg, resp.ErrMsg))
  1089. continue
  1090. }
  1091. firstId = resp.Data.ClassifyId
  1092. lastId = firstId
  1093. // 二级分类
  1094. if classifySecond != "" {
  1095. classifySecondMap := make(map[string]interface{})
  1096. classifySecondMap["ClassifyName"] = classifySecond
  1097. classifySecondMap["ParentId"] = firstId
  1098. classifySecondMap["Level"] = 1
  1099. classifySecondMap["ClassifyType"] = 0
  1100. res2, e := PostEdbLib(classifySecondMap, method)
  1101. if e != nil {
  1102. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifySecond PostEdbLib err: %s", indexCode, e.Error()))
  1103. continue
  1104. }
  1105. resp2 := new(models.ClassifyResp)
  1106. if e = json.Unmarshal(res2, &resp2); e != nil {
  1107. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifySecond json unmarshal err: %s", indexCode, e.Error()))
  1108. continue
  1109. }
  1110. if resp2.Ret != 200 {
  1111. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifySecond resp msg: %s; errMsg: %s", indexCode, resp2.Msg, resp2.ErrMsg))
  1112. continue
  1113. }
  1114. secondId = resp2.Data.ClassifyId
  1115. lastId = secondId
  1116. }
  1117. // 三级分类
  1118. if classifyThree != "" {
  1119. classifyThreeMap := make(map[string]interface{})
  1120. classifyThreeMap["ClassifyName"] = classifyThree
  1121. classifyThreeMap["ParentId"] = secondId
  1122. classifyThreeMap["Level"] = 2
  1123. classifyThreeMap["ClassifyType"] = 0
  1124. res3, e := PostEdbLib(classifyThreeMap, method)
  1125. if e != nil {
  1126. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifySecond ClassifyThird err: %s", indexCode, e.Error()))
  1127. continue
  1128. }
  1129. resp3 := new(models.ClassifyResp)
  1130. if e = json.Unmarshal(res3, &resp3); e != nil {
  1131. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifyThird json unmarshal err: %s", indexCode, e.Error()))
  1132. continue
  1133. }
  1134. if resp3.Ret != 200 {
  1135. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifyThird resp msg: %s; errMsg: %s", indexCode, resp3.Msg, resp3.ErrMsg))
  1136. continue
  1137. }
  1138. thirdId = resp3.Data.ClassifyId
  1139. lastId = thirdId
  1140. }
  1141. // 四级分类
  1142. if classifyFourth != "" {
  1143. classifyFourthMap := make(map[string]interface{})
  1144. classifyFourthMap["ClassifyName"] = classifyFourth
  1145. classifyFourthMap["ParentId"] = thirdId
  1146. classifyFourthMap["Level"] = 3
  1147. classifyFourthMap["ClassifyType"] = 0
  1148. res4, e := PostEdbLib(classifyFourthMap, method)
  1149. if e != nil {
  1150. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifyFourth ClassifyThird err: %s", indexCode, e.Error()))
  1151. continue
  1152. }
  1153. resp4 := new(models.ClassifyResp)
  1154. if e = json.Unmarshal(res4, &resp4); e != nil {
  1155. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifyFourth json unmarshal err: %s", indexCode, e.Error()))
  1156. continue
  1157. }
  1158. if resp4.Ret != 200 {
  1159. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifyFourth resp msg: %s; errMsg: %s", indexCode, resp4.Msg, resp4.ErrMsg))
  1160. continue
  1161. }
  1162. fourthId = resp4.Data.ClassifyId
  1163. lastId = fourthId
  1164. }
  1165. // 五级分类
  1166. if classifyFifth != "" {
  1167. classifyFifthMap := make(map[string]interface{})
  1168. classifyFifthMap["ClassifyName"] = classifyFifth
  1169. classifyFifthMap["ParentId"] = fourthId
  1170. classifyFifthMap["Level"] = 4
  1171. classifyFifthMap["ClassifyType"] = 0
  1172. res5, e := PostEdbLib(classifyFifthMap, method)
  1173. if e != nil {
  1174. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifyFifth ClassifyThird err: %s", indexCode, e.Error()))
  1175. continue
  1176. }
  1177. resp5 := new(models.ClassifyResp)
  1178. if e = json.Unmarshal(res5, &resp5); e != nil {
  1179. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifyFifth json unmarshal err: %s", indexCode, e.Error()))
  1180. continue
  1181. }
  1182. if resp5.Ret != 200 {
  1183. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifyFifth resp msg: %s; errMsg: %s", indexCode, resp5.Msg, resp5.ErrMsg))
  1184. continue
  1185. }
  1186. fifthId = resp5.Data.ClassifyId
  1187. lastId = fifthId
  1188. }
  1189. // 六级分类
  1190. if classifySixth != "" {
  1191. classifySixthMap := make(map[string]interface{})
  1192. classifySixthMap["ClassifyName"] = classifySixth
  1193. classifySixthMap["ParentId"] = fifthId
  1194. classifySixthMap["Level"] = 5
  1195. classifySixthMap["ClassifyType"] = 0
  1196. res6, e := PostEdbLib(classifySixthMap, method)
  1197. if e != nil {
  1198. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifySixth ClassifyThird err: %s", indexCode, e.Error()))
  1199. continue
  1200. }
  1201. resp6 := new(models.ClassifyResp)
  1202. if e = json.Unmarshal(res6, &resp6); e != nil {
  1203. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifySixth json unmarshal err: %s", indexCode, e.Error()))
  1204. continue
  1205. }
  1206. if resp6.Ret != 200 {
  1207. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,ClassifySixth resp msg: %s; errMsg: %s", indexCode, resp6.Msg, resp6.ErrMsg))
  1208. continue
  1209. }
  1210. lastId = resp6.Data.ClassifyId
  1211. }
  1212. // 添加指标
  1213. method = "edb_info/add"
  1214. sourceId, ok := IndexSourceMap[source]
  1215. if !ok {
  1216. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,来源未定义:%s", indexCode, source))
  1217. continue
  1218. }
  1219. indexMap := make(map[string]interface{})
  1220. indexMap["Source"] = sourceId
  1221. indexMap["EdbCode"] = indexCode
  1222. indexMap["EdbName"] = indexName
  1223. indexMap["Frequency"] = frequency
  1224. indexMap["Unit"] = unit
  1225. indexMap["ClassifyId"] = lastId
  1226. indexMap["TerminalCode"] = terminalCode
  1227. admin := mobileMap[mobile]
  1228. if admin != nil {
  1229. indexMap["AdminId"] = admin.AdminId
  1230. indexMap["AdminName"] = admin.RealName
  1231. }
  1232. result, err = PostEdbLib(indexMap, method)
  1233. if err != nil {
  1234. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,初始化指标失败:%s ;result:%s", indexCode, err.Error(), string(result)))
  1235. utils.FileLog.Info("初始化指标失败:" + err.Error() + " result:" + string(result))
  1236. continue
  1237. }
  1238. indexResp := new(models.EdbInfoResp)
  1239. err = json.Unmarshal(result, &indexResp)
  1240. if err != nil {
  1241. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,初始化指标2失败:%s", indexCode, err.Error()))
  1242. utils.FileLog.Info("初始化指标2失败:" + err.Error())
  1243. continue
  1244. }
  1245. if indexResp.Ret != 200 {
  1246. if strings.Contains(indexResp.Msg, "新增指标失败") {
  1247. continue
  1248. } else {
  1249. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,初始化指标3失败:%s;%s", indexCode, indexResp.Msg, indexResp.ErrMsg))
  1250. utils.FileLog.Info("初始化指标3失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  1251. continue
  1252. }
  1253. }
  1254. fmt.Println("add index success:" + indexCode)
  1255. //刷新指标
  1256. method, ok = IndexSourceRefreshMap[sourceId]
  1257. if !ok {
  1258. errorIndexList = append(errorIndexList, fmt.Sprintf("指标编码:%s,刷新地址未定义:%s;", indexCode, source))
  1259. //fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
  1260. continue
  1261. }
  1262. refreshMap := make(map[string]interface{})
  1263. refreshMap["EdbInfoId"] = indexResp.Data.EdbInfoId
  1264. refreshMap["EdbCode"] = indexCode
  1265. refreshMap["StartDate"] = "1990-01-01"
  1266. PostEdbLib(refreshMap, method)
  1267. }
  1268. }
  1269. }
  1270. if len(errorIndexList) > 0 {
  1271. fmt.Println("初始化指标失败列表:\n" + strings.Join(errorIndexList, "\n"))
  1272. utils.FileLog.Info("初始化指标失败列表:\n" + strings.Join(errorIndexList, "\n"))
  1273. }
  1274. }
  1275. // VerifyMysteelIndex
  1276. // @Description: 判断钢联指标编码是否已经入库
  1277. // @author: Roc
  1278. // @datetime 2024-01-15 13:54:48
  1279. // @param indexCode string
  1280. // @return ok bool
  1281. // @return err error
  1282. func VerifyMysteelIndex(indexCode string) (ok bool, err error) {
  1283. //判断指标是否存在
  1284. method := "mysteel_chemical/index_detail"
  1285. indexSearchMap := make(map[string]interface{})
  1286. indexSearchMap["IndexCode"] = indexCode
  1287. indexResult, e := PostEdbLib(indexSearchMap, method)
  1288. if e != nil {
  1289. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  1290. return
  1291. }
  1292. mysteelIndexResp := new(models.MysteelIndexResp)
  1293. e = json.Unmarshal(indexResult, &mysteelIndexResp)
  1294. if e != nil {
  1295. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  1296. return
  1297. }
  1298. if mysteelIndexResp.Ret != 200 {
  1299. err = errors.New("判断指标是否存在失败,Err:" + mysteelIndexResp.ErrMsg)
  1300. return
  1301. }
  1302. if mysteelIndexResp.Data.BaseFromMysteelChemicalIndexId <= 0 {
  1303. fmt.Println("指标:" + indexCode + ";不存在")
  1304. return
  1305. }
  1306. ok = true
  1307. return
  1308. }
  1309. // VerifySmmIndex
  1310. // @Description: 判断有色指标编码是否已经入库
  1311. // @author: Roc
  1312. // @datetime 2024-01-15 13:54:48
  1313. // @param indexCode string
  1314. // @return ok bool
  1315. // @return err error
  1316. func VerifySmmIndex(indexCode string) (ok bool, err error) {
  1317. //判断指标是否存在
  1318. method := "smm/index_detail/from_data_source"
  1319. indexSearchMap := make(map[string]interface{})
  1320. indexSearchMap["IndexCode"] = indexCode
  1321. indexResult, e := PostEdbLib(indexSearchMap, method)
  1322. if e != nil {
  1323. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  1324. return
  1325. }
  1326. smmIndexResp := new(models.BaseFromSmmIndexResp)
  1327. e = json.Unmarshal(indexResult, &smmIndexResp)
  1328. if e != nil {
  1329. err = fmt.Errorf("判断指标是否存在失败, Err: %s", e.Error())
  1330. return
  1331. }
  1332. if smmIndexResp.Ret != 200 {
  1333. err = errors.New("判断指标是否存在失败,Err:" + smmIndexResp.ErrMsg)
  1334. return
  1335. }
  1336. if smmIndexResp.Data.BaseFromSmmIndexId <= 0 {
  1337. //fmt.Println("指标:" + indexCode + ";不存在")
  1338. return
  1339. }
  1340. ok = true
  1341. return
  1342. }