init_base_index.go 44 KB

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