base_from_mysteel_chemical.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_index_lib/logic"
  6. "eta/eta_index_lib/models"
  7. "eta/eta_index_lib/services/alarm_msg"
  8. "eta/eta_index_lib/utils"
  9. "fmt"
  10. "io"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. func HandleMysteelIndex(req *models.HandleMysteelIndexResp) (err error) {
  17. for _, v := range req.List {
  18. if v.IndexName == "" || v.IndexCode == "" {
  19. continue
  20. }
  21. err = handleIndex(v)
  22. if err != nil {
  23. return
  24. }
  25. }
  26. _ = SetMysteelChemicalEdbInfoUpdateStat(false)
  27. _ = SetEdbSourceStat(false)
  28. return
  29. }
  30. func HandleApiMysteelIndex(req *models.HandleMysteelIndexResp) (errMsg string, err error) {
  31. addIndexCodeList := make([]string, 0)
  32. for _, v := range req.List {
  33. if v.IndexCode == "" {
  34. continue
  35. }
  36. addIndexCodeList = append(addIndexCodeList, v.IndexCode)
  37. }
  38. errMsg, err = HandleApiIndex(addIndexCodeList)
  39. if err != nil {
  40. return
  41. }
  42. _ = SetMysteelChemicalEdbInfoUpdateStat(false)
  43. _ = SetEdbSourceStat(false)
  44. return
  45. }
  46. func ApiCheck() (ok bool, err error) {
  47. item, err := getPageIndexInfoMap(1, 1, false)
  48. if err != nil {
  49. if err.Error() == "406" {
  50. return false, nil
  51. }
  52. if item != nil && item.Code == "100006" {
  53. return false, nil
  54. }
  55. return
  56. }
  57. if item != nil && item.Code == "100006" {
  58. return false, nil
  59. }
  60. return true, nil
  61. }
  62. func HandleApiIndex(indexCodes []string) (errMsg string, err error) {
  63. if len(indexCodes) == 0 {
  64. return
  65. }
  66. resp, err := GetEdbDataFromMySteelChemical(indexCodes, utils.GetEdbRefreshStartDate(""), utils.BASE_END_DATE, "desc")
  67. if err != nil {
  68. return
  69. }
  70. if !resp.Success {
  71. errMsg = "获取数据失败"
  72. err = errors.New(resp.Message)
  73. return
  74. }
  75. indexInfoMap, err := GetMySteelChemicalIndexNameMap()
  76. if err != nil {
  77. errMsg = "获取指标数据失败"
  78. return
  79. }
  80. indexObj := &models.BaseFromMysteelChemicalIndex{}
  81. existIndexs, err := indexObj.GetBatchIndexItem(indexCodes)
  82. if err != nil {
  83. errMsg = "获取指标数据失败"
  84. return
  85. }
  86. //获取已存在的所有数据
  87. existDataMap := make(map[string]*models.BaseFromMysteelChemicalData)
  88. existIndexMap := make(map[string]*models.BaseFromMysteelChemicalIndex)
  89. updateDataObj := new(models.BaseFromMysteelChemicalData)
  90. for _, v := range existIndexs {
  91. // 更新指标的名称,单位和频度等信息
  92. if info, ok := indexInfoMap[v.IndexCode]; ok {
  93. v.IndexName = info.IndexName
  94. v.Unit = info.UnitName
  95. v.Frequency = info.FrequencyName
  96. v.ModifyTime = time.Now()
  97. err = v.Update([]string{"index_name", "unit", "frequency", "modify_time"})
  98. if err != nil {
  99. errMsg = "更新指标失败"
  100. return
  101. }
  102. }
  103. if err != nil {
  104. errMsg = "添加指标失败"
  105. return
  106. }
  107. existIndexMap[v.IndexCode] = v
  108. exitDataList, er := updateDataObj.GetIndexDataList(v.IndexCode)
  109. if er != nil {
  110. errMsg = "获取指标数据失败"
  111. err = er
  112. return
  113. }
  114. fmt.Println("exitDataListLen:", len(exitDataList))
  115. for _, v := range exitDataList {
  116. dateStr := v.DataTime.Format(utils.FormatDate)
  117. existDataMap[dateStr] = v
  118. }
  119. }
  120. mysteelChemicalDatas, err := tranformData(resp)
  121. if err != nil {
  122. errMsg = "转换数据失败"
  123. return
  124. }
  125. var indexErr error
  126. var lErr error
  127. defer func() {
  128. if indexErr != nil {
  129. tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新异常, 指标编码: %s, err: %s", indexObj.IndexCode, indexErr.Error())
  130. alarm_msg.SendAlarmMsg(tips, 3)
  131. }
  132. if lErr != nil {
  133. tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新统计异常, 指标编码: %s, err: %s", indexObj.IndexCode, lErr.Error())
  134. alarm_msg.SendAlarmMsg(tips, 3)
  135. }
  136. }()
  137. var hasUpdate bool
  138. dataObj := new(models.BaseFromMysteelChemicalData)
  139. for _, items := range mysteelChemicalDatas {
  140. addItems := make([]*models.BaseFromMysteelChemicalData, 0)
  141. for _, v := range items {
  142. dateStr := v.DataTime.Format(utils.FormatDate)
  143. if findData, ok := existDataMap[dateStr]; !ok {
  144. index, ok := existIndexMap[v.IndexCode]
  145. if !ok {
  146. continue
  147. }
  148. v.BaseFromMysteelChemicalIndexId = index.BaseFromMysteelChemicalIndexId
  149. addItems = append(addItems, v)
  150. } else {
  151. if findData != nil && findData.Value != v.Value {
  152. dataObj.BaseFromMysteelChemicalDataId = findData.BaseFromMysteelChemicalDataId
  153. dataObj.Value = v.Value
  154. dataObj.ModifyTime = time.Now()
  155. err = dataObj.Update([]string{"value", "modify_time"})
  156. if err != nil {
  157. errMsg = "更新数据失败"
  158. return
  159. }
  160. hasUpdate = true
  161. }
  162. }
  163. }
  164. err = dataObj.AddV2(addItems)
  165. if err != nil {
  166. return
  167. }
  168. //修改最大最小日期
  169. if len(items) <= 0 {
  170. continue
  171. }
  172. mysteelIndexMaxItem, er := dataObj.GetMysteelIndexInfoMaxAndMinInfo(items[0].IndexCode)
  173. if er == nil && mysteelIndexMaxItem != nil {
  174. e := dataObj.ModifyMysteelIndexMaxAndMinInfo(items[0].IndexCode, mysteelIndexMaxItem)
  175. if e != nil {
  176. fmt.Println("ModifyMysteelIndexMaxAndMinInfo Err:" + e.Error())
  177. utils.FileLog.Info("修改钢联化工的最大最小日期失败,Err:" + e.Error())
  178. }
  179. }
  180. edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexObj.IndexCode)
  181. if e != nil && e.Error() != utils.ErrNoRow() {
  182. indexErr = e
  183. return
  184. }
  185. if edbInfo != nil {
  186. dataUpdateResult := 2
  187. dataUpdateFailedReason := "服务异常"
  188. _, logErrMsg, logErr := logic.RefreshBaseEdbInfo(edbInfo, ``)
  189. if logErr != nil {
  190. lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 2, logErrMsg+logErr.Error(), dataUpdateResult, dataUpdateFailedReason, 0, 0)
  191. return
  192. }
  193. if hasUpdate {
  194. dataUpdateResult = 1
  195. dataUpdateFailedReason = ""
  196. } else {
  197. dataUpdateFailedReason = "未刷新到数据"
  198. }
  199. // 添加刷新成功日志
  200. lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", dataUpdateResult, dataUpdateFailedReason, 0, 0)
  201. if lErr != nil {
  202. return
  203. }
  204. }
  205. }
  206. return
  207. }
  208. func tranformData(dataResp *models.MySteelChemicalApiResp) (items [][]*models.BaseFromMysteelChemicalData, err error) {
  209. for _, v := range dataResp.Data {
  210. tmpNewDataMap := make(map[string]int64)
  211. tmpDateDataMap := make(map[string]*models.BaseFromMysteelChemicalData)
  212. tmpDataItems := make([]*models.BaseFromMysteelChemicalData, 0)
  213. for _, vv := range v.DataList {
  214. tmpData := new(models.BaseFromMysteelChemicalData)
  215. tmpData.IndexCode = vv.IndexCode
  216. // 如果存在多条数据,则取发布时间最新的数据
  217. pub, ok := tmpNewDataMap[vv.DataDate]
  218. if !ok {
  219. tmpNewDataMap[vv.DataDate] = vv.PublishTime
  220. tmpData.Value = strconv.FormatFloat(vv.DataValue, 'f', -1, 64)
  221. } else {
  222. if pub < vv.PublishTime {
  223. tmpNewDataMap[vv.DataDate] = vv.PublishTime
  224. tmpData = tmpDateDataMap[vv.DataDate]
  225. tmpData.Value = strconv.FormatFloat(vv.DataValue, 'f', -1, 64)
  226. }
  227. continue
  228. }
  229. dataDate, er := time.Parse(utils.FormatDate, vv.DataDate)
  230. if er != nil {
  231. err = er
  232. return
  233. }
  234. tmpData.DataTime = dataDate
  235. tmpData.CreateTime = time.Now()
  236. tmpData.ModifyTime = time.Now()
  237. tmpDataItems = append(tmpDataItems, tmpData)
  238. tmpDateDataMap[vv.DataDate] = tmpData
  239. }
  240. items = append(items, tmpDataItems)
  241. }
  242. return
  243. }
  244. func handleIndex(indexItem *models.HandleMysteelIndex) (err error) {
  245. defer func() {
  246. if err != nil {
  247. // 添加刷新失败日志
  248. dataUpdateResult := 2
  249. dataUpdateFailedReason := "服务异常"
  250. edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexItem.IndexCode)
  251. if e == nil {
  252. //查询指标存在,才添加刷新日志
  253. _ = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 2, err.Error(), dataUpdateResult, dataUpdateFailedReason, 1, 0)
  254. }
  255. }
  256. }()
  257. indexObj := new(models.BaseFromMysteelChemicalIndex)
  258. var indexId int64
  259. addDataList := make([]models.BaseFromMysteelChemicalData, 0)
  260. exitDataMap := make(map[string]*models.BaseFromMysteelChemicalData)
  261. //判断指标是否存在
  262. var isAdd int
  263. item, err := indexObj.GetIndexItem(indexItem.IndexCode)
  264. if err != nil {
  265. if err.Error() == utils.ErrNoRow() {
  266. isAdd = 1
  267. } else {
  268. isAdd = -1
  269. return
  270. }
  271. }
  272. nameChange := false
  273. if item != nil && item.BaseFromMysteelChemicalIndexId > 0 {
  274. isAdd = 2
  275. if item.IndexName != indexItem.IndexName {
  276. nameChange = true
  277. }
  278. } else {
  279. isAdd = 1
  280. }
  281. fmt.Println("isAdd:", isAdd)
  282. if !strings.Contains(indexItem.Frequency, "度") {
  283. indexItem.Frequency = indexItem.Frequency + "度"
  284. }
  285. if isAdd == 1 {
  286. indexObj.IndexCode = indexItem.IndexCode
  287. indexObj.IndexName = indexItem.IndexName
  288. indexObj.Unit = indexItem.Unit
  289. indexObj.Source = indexItem.Source
  290. indexObj.Describe = indexItem.Describe
  291. indexObj.StartDate, err = utils.DealExcelDate(indexItem.StartDate)
  292. if err != nil {
  293. fmt.Println("utils.DealExcelDate err:" + indexItem.StartDate + err.Error())
  294. return
  295. }
  296. indexObj.EndDate, err = utils.DealExcelDate(indexItem.EndDate)
  297. if err != nil {
  298. fmt.Println("utils.DealExcelDate err:" + indexItem.EndDate + err.Error())
  299. return
  300. }
  301. indexObj.Frequency = indexItem.Frequency
  302. //indexObj.CreateTime = time.Now().Local()
  303. //indexObj.ModifyTime = time.Now().Local()
  304. err = indexObj.Add()
  305. if err != nil {
  306. fmt.Println("add err:" + err.Error())
  307. return
  308. }
  309. indexId = indexObj.BaseFromMysteelChemicalIndexId
  310. } else if isAdd == 2 {
  311. indexObj.BaseFromMysteelChemicalIndexId = item.BaseFromMysteelChemicalIndexId
  312. indexObj.IndexCode = indexItem.IndexCode
  313. indexObj.IndexName = indexItem.IndexName
  314. indexObj.Unit = indexItem.Unit
  315. indexObj.Source = indexItem.Source
  316. indexObj.Describe = indexItem.Describe
  317. indexObj.StartDate, err = utils.DealExcelDate(indexItem.StartDate)
  318. if err != nil {
  319. fmt.Println("utils.DealExcelDate err:" + indexItem.StartDate + err.Error())
  320. return
  321. }
  322. indexObj.EndDate, err = utils.DealExcelDate(indexItem.EndDate)
  323. if err != nil {
  324. fmt.Println("utils.DealExcelDate err:" + indexItem.EndDate + err.Error())
  325. return
  326. }
  327. indexObj.Frequency = indexItem.Frequency
  328. indexObj.ModifyTime = time.Now()
  329. indexId = item.BaseFromMysteelChemicalIndexId
  330. var isStop int
  331. if strings.Contains(indexItem.IndexName, "停") {
  332. isStop = 1
  333. }
  334. indexObj.IsStop = isStop
  335. //修改数据
  336. updateColsArr := make([]string, 0)
  337. updateColsArr = append(updateColsArr, "index_name")
  338. updateColsArr = append(updateColsArr, "unit")
  339. updateColsArr = append(updateColsArr, "source")
  340. updateColsArr = append(updateColsArr, "frequency")
  341. updateColsArr = append(updateColsArr, "start_date")
  342. updateColsArr = append(updateColsArr, "end_date")
  343. updateColsArr = append(updateColsArr, "describe")
  344. updateColsArr = append(updateColsArr, "end_date")
  345. updateColsArr = append(updateColsArr, "is_stop")
  346. updateColsArr = append(updateColsArr, "modify_time")
  347. e := indexObj.Update(updateColsArr)
  348. if e != nil {
  349. fmt.Println("Index Update Err:" + e.Error())
  350. return
  351. }
  352. if item.IndexName != indexItem.IndexName {
  353. var changeRecord models.BaseFromMysteelChemicalRecord
  354. changeRecord.BaseFromMysteelChemicalIndexId = item.BaseFromMysteelChemicalIndexId
  355. changeRecord.OldIndexName = item.IndexName
  356. changeRecord.NewIndexName = indexItem.IndexName
  357. ctime := time.Now()
  358. changeRecord.CreateTime = ctime
  359. changeRecord.Timestamp = ctime.Unix()
  360. e = changeRecord.AddBaseFromMysteelChemicalRecord()
  361. if e != nil {
  362. fmt.Println("mysteel chemical changeRecord Add Err:" + e.Error())
  363. return
  364. }
  365. }
  366. dataObj := new(models.BaseFromMysteelChemicalData)
  367. //获取已存在的所有数据
  368. exitDataList, err := dataObj.GetIndexDataList(indexItem.IndexCode)
  369. if err != nil {
  370. fmt.Println("GetIndexDataList Err:" + err.Error())
  371. return err
  372. }
  373. fmt.Println("exitDataListLen:", len(exitDataList))
  374. for _, v := range exitDataList {
  375. dateStr := v.DataTime.Format(utils.FormatDate)
  376. exitDataMap[dateStr] = v
  377. }
  378. }
  379. dataObj := new(models.BaseFromMysteelChemicalData)
  380. var hasUpdate bool
  381. // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
  382. for date, value := range indexItem.ExcelDataMap {
  383. dateTime, err := utils.DealExcelDate(date)
  384. if err != nil {
  385. fmt.Println("time.ParseInLocation Err:" + err.Error())
  386. return err
  387. }
  388. date = dateTime.Format(utils.FormatDate)
  389. if findData, ok := exitDataMap[date]; !ok {
  390. if !strings.Contains(value, "#N/A") {
  391. dataItem := new(models.BaseFromMysteelChemicalData)
  392. dataItem.BaseFromMysteelChemicalIndexId = indexId
  393. dataItem.IndexCode = indexItem.IndexCode
  394. dataItem.DataTime = dateTime
  395. dataItem.Value = value
  396. dataItem.UpdateDate = indexItem.UpdateDate
  397. dataItem.CreateTime = time.Now()
  398. dataItem.ModifyTime = time.Now()
  399. addDataList = append(addDataList, *dataItem)
  400. }
  401. } else {
  402. if findData != nil && findData.Value != value && !strings.Contains(value, "#N/A") { //修改数据
  403. dataObj.BaseFromMysteelChemicalDataId = findData.BaseFromMysteelChemicalDataId
  404. dataObj.Value = value
  405. dataObj.ModifyTime = time.Now()
  406. updateDataColsArr := make([]string, 0)
  407. updateDataColsArr = append(updateDataColsArr, "value")
  408. updateDataColsArr = append(updateDataColsArr, "modify_time")
  409. dataObj.Update(updateDataColsArr)
  410. hasUpdate = true
  411. }
  412. }
  413. }
  414. if len(addDataList) > 0 {
  415. err = dataObj.Add(addDataList)
  416. if err != nil {
  417. fmt.Println("dataObj.Add() Err:" + err.Error())
  418. return
  419. }
  420. hasUpdate = true
  421. }
  422. //修改最大最小日期
  423. mysteelIndexMaxItem, err := dataObj.GetMysteelIndexInfoMaxAndMinInfo(indexItem.IndexCode)
  424. if err == nil && mysteelIndexMaxItem != nil {
  425. e := dataObj.ModifyMysteelIndexMaxAndMinInfo(indexItem.IndexCode, mysteelIndexMaxItem)
  426. if e != nil {
  427. fmt.Println("ModifyMysteelIndexMaxAndMinInfo Err:" + e.Error())
  428. }
  429. }
  430. // 同步刷新图库钢联的指标
  431. //go func() {
  432. var indexErr error
  433. var lErr error
  434. defer func() {
  435. if indexErr != nil {
  436. tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新异常, 指标编码: %s, err: %s", indexObj.IndexCode, indexErr.Error())
  437. alarm_msg.SendAlarmMsg(tips, 3)
  438. }
  439. if lErr != nil {
  440. tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新统计异常, 指标编码: %s, err: %s", indexObj.IndexCode, lErr.Error())
  441. alarm_msg.SendAlarmMsg(tips, 3)
  442. }
  443. }()
  444. edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexObj.IndexCode)
  445. if e != nil && e.Error() != utils.ErrNoRow() {
  446. indexErr = e
  447. return
  448. }
  449. if edbInfo != nil {
  450. dataUpdateResult := 2
  451. dataUpdateFailedReason := "服务异常"
  452. _, logErrMsg, logErr := logic.RefreshBaseEdbInfo(edbInfo, ``)
  453. if logErr != nil {
  454. lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 2, logErrMsg+logErr.Error(), dataUpdateResult, dataUpdateFailedReason, 1, 0)
  455. return
  456. }
  457. if hasUpdate {
  458. dataUpdateResult = 1
  459. dataUpdateFailedReason = ""
  460. } else {
  461. dataUpdateFailedReason = "未刷新到数据"
  462. }
  463. // 添加刷新成功日志
  464. lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", dataUpdateResult, dataUpdateFailedReason, 1, 0)
  465. if lErr != nil {
  466. return
  467. }
  468. //如果变更了指标名称,则添加指标信息变更日志
  469. if nameChange {
  470. edbInfo.SourceIndexName = indexItem.IndexName
  471. lErr = edbInfo.Update([]string{"SourceIndexName"})
  472. if lErr != nil {
  473. return
  474. }
  475. lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 0, "", 0, 1)
  476. }
  477. }
  478. //}()
  479. return
  480. }
  481. type MySteelChemicalApiDataBody struct {
  482. IndexCodes []string `json:"indexCodes"`
  483. StartTime string `json:"startTime"`
  484. EndTime string `json:"endTime"`
  485. Order string `json:"order"`
  486. }
  487. type MySteelChemicalApiInfoBody struct {
  488. PageNum int `json:"pageNum"`
  489. PageSize int `json:"pageSize"`
  490. IncludeInfo bool `json:"includeInfo"`
  491. }
  492. // GetEdbDataFromMySteelChemical 批量获得钢联化工的指标数据
  493. func GetEdbDataFromMySteelChemical(indexCodes []string, startTime, endTime, order string) (item *models.MySteelChemicalApiResp, err error) {
  494. if utils.MysteelChemicalApiToken == "" {
  495. err = errors.New("钢联接口token未配置")
  496. return
  497. }
  498. m := new(MySteelChemicalApiDataBody)
  499. m.IndexCodes = indexCodes
  500. m.StartTime = startTime
  501. m.EndTime = endTime
  502. m.Order = order
  503. postData, er := json.Marshal(m)
  504. if er != nil {
  505. err = er
  506. return
  507. }
  508. postUrl := `https://mds.mysteel.com/dynamic/order/api/fcAbRA`
  509. body, err := MySteelChemicalPost(postUrl, "data", postData)
  510. if err != nil {
  511. return
  512. }
  513. err = json.Unmarshal(body, &item)
  514. if err != nil {
  515. return
  516. }
  517. return
  518. }
  519. // GetMySteelChemicalIndexNameMap 获取钢联化工的所有指标的信息
  520. func GetMySteelChemicalIndexNameMap() (indexNameMap map[string]*models.MySteelChemicalApiInfoItem, err error) {
  521. if utils.MysteelChemicalApiToken == "" {
  522. err = errors.New("钢联接口token未配置")
  523. return
  524. }
  525. item, err := getPageIndexInfoMap(1, 200, true)
  526. if err != nil {
  527. return
  528. }
  529. indexNameMap = make(map[string]*models.MySteelChemicalApiInfoItem)
  530. for _, v := range item.Data.List {
  531. indexNameMap[v.IndexCode] = v
  532. }
  533. // 如果总条数大于200,则继续获取
  534. if item.Data.Total > 200 || item.Data.Pages > 1 {
  535. for i := 2; i <= item.Data.Pages; i++ {
  536. item, err = getPageIndexInfoMap(i, 200, true)
  537. if err != nil {
  538. return
  539. }
  540. for _, v := range item.Data.List {
  541. indexNameMap[v.IndexCode] = v
  542. }
  543. }
  544. return
  545. }
  546. return
  547. }
  548. func getPageIndexInfoMap(pageNum, pageSize int, includeInfo bool) (item *models.MySteelChemicalApiInfoResp, err error) {
  549. m := new(MySteelChemicalApiInfoBody)
  550. m.PageNum = pageNum
  551. m.PageSize = pageSize
  552. m.IncludeInfo = includeInfo
  553. postData, er := json.Marshal(m)
  554. if er != nil {
  555. err = er
  556. return
  557. }
  558. postUrl := `https://mds.mysteel.com/dynamic/order/api/fcAbRA`
  559. body, er := MySteelChemicalPost(postUrl, "info", postData)
  560. if er != nil {
  561. err = er
  562. return
  563. }
  564. err = json.Unmarshal(body, &item)
  565. if err != nil {
  566. return
  567. }
  568. if !item.Success {
  569. err = errors.New(item.Message)
  570. utils.FileLog.Info("code:" + item.Code + " message:" + item.Message)
  571. return
  572. }
  573. return
  574. }
  575. func MySteelChemicalPost(postUrl, hType string, postData []byte) (body []byte, err error) {
  576. req, er := http.NewRequest(`POST`, postUrl, strings.NewReader(string(postData)))
  577. if er != nil {
  578. err = er
  579. return
  580. }
  581. req.Header.Set(`Content-Type`, `application/json`)
  582. req.Header.Set(`accessTokenSign`, utils.MysteelChemicalApiToken)
  583. req.Header.Set(`infoOrData`, hType)
  584. client := &http.Client{}
  585. resp, er := client.Do(req)
  586. if er != nil {
  587. err = er
  588. return
  589. }
  590. defer resp.Body.Close()
  591. body, err = io.ReadAll(resp.Body)
  592. if err != nil {
  593. return
  594. }
  595. return
  596. }
  597. func RefreshDataFromMysteelChemical(edbCode, startDate, endDate string) (err error) {
  598. indexObj := &models.BaseFromMysteelChemicalIndex{}
  599. tmpIndex, err := indexObj.GetIndexItem(edbCode)
  600. if err != nil {
  601. return
  602. }
  603. terminal, err := GetTerminal(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, tmpIndex.TerminalCode)
  604. if err != nil {
  605. err = fmt.Errorf("获取钢联化工接口配置出错 Err: %s", err)
  606. return
  607. }
  608. if tmpIndex.TerminalCode == "" {
  609. // 设置指标与终端关系的缓存
  610. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + edbCode
  611. _ = utils.Rc.Put(terminalCodeCacheKey, terminal.TerminalCode, utils.GetTodayLastSecond())
  612. }
  613. // 如果配置了api的token, 那么就走api接口
  614. if utils.MysteelChemicalApiToken != "" {
  615. resp, er := GetEdbDataFromMySteelChemical([]string{edbCode}, startDate, endDate, "desc")
  616. if er != nil {
  617. err = er
  618. return
  619. }
  620. if !resp.Success {
  621. err = errors.New(resp.Message)
  622. return
  623. }
  624. dataObj := new(models.BaseFromMysteelChemicalData)
  625. exitDataList, er := dataObj.GetIndexDataList(edbCode)
  626. if er != nil {
  627. err = er
  628. return
  629. }
  630. existDataMap := make(map[string]*models.BaseFromMysteelChemicalData)
  631. for _, v := range exitDataList {
  632. dateStr := v.DataTime.Format(utils.FormatDate)
  633. existDataMap[dateStr] = v
  634. }
  635. mysteelChemicalDatas, er := tranformData(resp)
  636. if er != nil {
  637. err = er
  638. return
  639. }
  640. addItems := make([]*models.BaseFromMysteelChemicalData, 0)
  641. indexObj := &models.BaseFromMysteelChemicalIndex{}
  642. existIndex, er := indexObj.GetIndexItem(edbCode)
  643. if er != nil {
  644. err = er
  645. return
  646. }
  647. if len(mysteelChemicalDatas) == 0 {
  648. err = errors.New("没有获取到数据")
  649. return
  650. }
  651. // 因为只有一个指标,所以取第一个就可以了
  652. items := mysteelChemicalDatas[0]
  653. for _, v := range items {
  654. dateStr := v.DataTime.Format(utils.FormatDate)
  655. if findData, ok := existDataMap[dateStr]; !ok {
  656. v.BaseFromMysteelChemicalIndexId = existIndex.BaseFromMysteelChemicalIndexId
  657. addItems = append(addItems, v)
  658. } else {
  659. if findData != nil && findData.Value != v.Value {
  660. dataObj.BaseFromMysteelChemicalDataId = findData.BaseFromMysteelChemicalDataId
  661. dataObj.Value = v.Value
  662. dataObj.ModifyTime = time.Now()
  663. err = dataObj.Update([]string{"value", "modify_time"})
  664. if err != nil {
  665. return
  666. }
  667. }
  668. }
  669. }
  670. err = dataObj.AddV2(addItems)
  671. if err != nil {
  672. return
  673. }
  674. return
  675. }
  676. return
  677. }