base_from_mysteel_chemical.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. indexObj.IsSupplierStop = item.IsSupplierStop
  331. var isStop int
  332. if strings.Contains(indexItem.IndexName, "停") {
  333. isStop = 1
  334. indexObj.IsSupplierStop = 1
  335. }
  336. indexObj.IsStop = isStop
  337. //修改数据
  338. updateColsArr := make([]string, 0)
  339. updateColsArr = append(updateColsArr, "index_name")
  340. updateColsArr = append(updateColsArr, "unit")
  341. updateColsArr = append(updateColsArr, "source")
  342. updateColsArr = append(updateColsArr, "frequency")
  343. updateColsArr = append(updateColsArr, "start_date")
  344. updateColsArr = append(updateColsArr, "end_date")
  345. updateColsArr = append(updateColsArr, "describe")
  346. updateColsArr = append(updateColsArr, "end_date")
  347. updateColsArr = append(updateColsArr, "is_stop")
  348. updateColsArr = append(updateColsArr, "is_supplier_stop")
  349. updateColsArr = append(updateColsArr, "modify_time")
  350. e := indexObj.Update(updateColsArr)
  351. if e != nil {
  352. fmt.Println("Index Update Err:" + e.Error())
  353. return
  354. }
  355. if item.IndexName != indexItem.IndexName {
  356. var changeRecord models.BaseFromMysteelChemicalRecord
  357. changeRecord.BaseFromMysteelChemicalIndexId = item.BaseFromMysteelChemicalIndexId
  358. changeRecord.OldIndexName = item.IndexName
  359. changeRecord.NewIndexName = indexItem.IndexName
  360. ctime := time.Now()
  361. changeRecord.CreateTime = ctime
  362. changeRecord.Timestamp = ctime.Unix()
  363. e = changeRecord.AddBaseFromMysteelChemicalRecord()
  364. if e != nil {
  365. fmt.Println("mysteel chemical changeRecord Add Err:" + e.Error())
  366. return
  367. }
  368. }
  369. dataObj := new(models.BaseFromMysteelChemicalData)
  370. //获取已存在的所有数据
  371. exitDataList, err := dataObj.GetIndexDataList(indexItem.IndexCode)
  372. if err != nil {
  373. fmt.Println("GetIndexDataList Err:" + err.Error())
  374. return err
  375. }
  376. fmt.Println("exitDataListLen:", len(exitDataList))
  377. for _, v := range exitDataList {
  378. dateStr := v.DataTime.Format(utils.FormatDate)
  379. exitDataMap[dateStr] = v
  380. }
  381. }
  382. dataObj := new(models.BaseFromMysteelChemicalData)
  383. var hasUpdate bool
  384. // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
  385. for date, value := range indexItem.ExcelDataMap {
  386. dateTime, err := utils.DealExcelDate(date)
  387. if err != nil {
  388. fmt.Println("time.ParseInLocation Err:" + err.Error())
  389. return err
  390. }
  391. date = dateTime.Format(utils.FormatDate)
  392. if findData, ok := exitDataMap[date]; !ok {
  393. if !strings.Contains(value, "#N/A") {
  394. dataItem := new(models.BaseFromMysteelChemicalData)
  395. dataItem.BaseFromMysteelChemicalIndexId = indexId
  396. dataItem.IndexCode = indexItem.IndexCode
  397. dataItem.DataTime = dateTime
  398. dataItem.Value = value
  399. dataItem.UpdateDate = indexItem.UpdateDate
  400. dataItem.CreateTime = time.Now()
  401. dataItem.ModifyTime = time.Now()
  402. addDataList = append(addDataList, *dataItem)
  403. }
  404. } else {
  405. if findData != nil && findData.Value != value && !strings.Contains(value, "#N/A") { //修改数据
  406. dataObj.BaseFromMysteelChemicalDataId = findData.BaseFromMysteelChemicalDataId
  407. dataObj.Value = value
  408. dataObj.ModifyTime = time.Now()
  409. updateDataColsArr := make([]string, 0)
  410. updateDataColsArr = append(updateDataColsArr, "value")
  411. updateDataColsArr = append(updateDataColsArr, "modify_time")
  412. dataObj.Update(updateDataColsArr)
  413. hasUpdate = true
  414. }
  415. }
  416. }
  417. if len(addDataList) > 0 {
  418. err = dataObj.Add(addDataList)
  419. if err != nil {
  420. fmt.Println("dataObj.Add() Err:" + err.Error())
  421. return
  422. }
  423. hasUpdate = true
  424. }
  425. //修改最大最小日期
  426. mysteelIndexMaxItem, err := dataObj.GetMysteelIndexInfoMaxAndMinInfo(indexItem.IndexCode)
  427. if err == nil && mysteelIndexMaxItem != nil {
  428. e := dataObj.ModifyMysteelIndexMaxAndMinInfo(indexItem.IndexCode, mysteelIndexMaxItem)
  429. if e != nil {
  430. fmt.Println("ModifyMysteelIndexMaxAndMinInfo Err:" + e.Error())
  431. }
  432. }
  433. // 同步刷新图库钢联的指标
  434. //go func() {
  435. var indexErr error
  436. var lErr error
  437. defer func() {
  438. if indexErr != nil {
  439. tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新异常, 指标编码: %s, err: %s", indexObj.IndexCode, indexErr.Error())
  440. alarm_msg.SendAlarmMsg(tips, 3)
  441. }
  442. if lErr != nil {
  443. tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新统计异常, 指标编码: %s, err: %s", indexObj.IndexCode, lErr.Error())
  444. alarm_msg.SendAlarmMsg(tips, 3)
  445. }
  446. }()
  447. edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexObj.IndexCode)
  448. if e != nil && e.Error() != utils.ErrNoRow() {
  449. indexErr = e
  450. return
  451. }
  452. if edbInfo != nil {
  453. dataUpdateResult := 2
  454. dataUpdateFailedReason := "服务异常"
  455. _, logErrMsg, logErr := logic.RefreshBaseEdbInfo(edbInfo, ``)
  456. if logErr != nil {
  457. lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 2, logErrMsg+logErr.Error(), dataUpdateResult, dataUpdateFailedReason, 1, 0)
  458. return
  459. }
  460. if hasUpdate {
  461. dataUpdateResult = 1
  462. dataUpdateFailedReason = ""
  463. } else {
  464. dataUpdateFailedReason = "未刷新到数据"
  465. }
  466. // 添加刷新成功日志
  467. lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", dataUpdateResult, dataUpdateFailedReason, 1, 0)
  468. if lErr != nil {
  469. return
  470. }
  471. //如果变更了指标名称,则添加指标信息变更日志
  472. if nameChange {
  473. edbInfo.SourceIndexName = indexItem.IndexName
  474. lErr = edbInfo.Update([]string{"SourceIndexName"})
  475. if lErr != nil {
  476. return
  477. }
  478. lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 0, "", 0, 1)
  479. }
  480. }
  481. //}()
  482. return
  483. }
  484. type MySteelChemicalApiDataBody struct {
  485. IndexCodes []string `json:"indexCodes"`
  486. StartTime string `json:"startTime"`
  487. EndTime string `json:"endTime"`
  488. Order string `json:"order"`
  489. }
  490. type MySteelChemicalApiInfoBody struct {
  491. PageNum int `json:"pageNum"`
  492. PageSize int `json:"pageSize"`
  493. IncludeInfo bool `json:"includeInfo"`
  494. }
  495. // GetEdbDataFromMySteelChemical 批量获得钢联化工的指标数据
  496. func GetEdbDataFromMySteelChemical(indexCodes []string, startTime, endTime, order string) (item *models.MySteelChemicalApiResp, err error) {
  497. if utils.MysteelChemicalApiToken == "" {
  498. err = errors.New("钢联接口token未配置")
  499. return
  500. }
  501. m := new(MySteelChemicalApiDataBody)
  502. m.IndexCodes = indexCodes
  503. m.StartTime = startTime
  504. m.EndTime = endTime
  505. m.Order = order
  506. postData, er := json.Marshal(m)
  507. if er != nil {
  508. err = er
  509. return
  510. }
  511. postUrl := `https://mds.mysteel.com/dynamic/order/api/fcAbRA`
  512. body, err := MySteelChemicalPost(postUrl, "data", postData)
  513. if err != nil {
  514. return
  515. }
  516. err = json.Unmarshal(body, &item)
  517. if err != nil {
  518. return
  519. }
  520. return
  521. }
  522. // GetMySteelChemicalIndexNameMap 获取钢联化工的所有指标的信息
  523. func GetMySteelChemicalIndexNameMap() (indexNameMap map[string]*models.MySteelChemicalApiInfoItem, err error) {
  524. if utils.MysteelChemicalApiToken == "" {
  525. err = errors.New("钢联接口token未配置")
  526. return
  527. }
  528. item, err := getPageIndexInfoMap(1, 200, true)
  529. if err != nil {
  530. return
  531. }
  532. indexNameMap = make(map[string]*models.MySteelChemicalApiInfoItem)
  533. for _, v := range item.Data.List {
  534. indexNameMap[v.IndexCode] = v
  535. }
  536. // 如果总条数大于200,则继续获取
  537. if item.Data.Total > 200 || item.Data.Pages > 1 {
  538. for i := 2; i <= item.Data.Pages; i++ {
  539. item, err = getPageIndexInfoMap(i, 200, true)
  540. if err != nil {
  541. return
  542. }
  543. for _, v := range item.Data.List {
  544. indexNameMap[v.IndexCode] = v
  545. }
  546. }
  547. return
  548. }
  549. return
  550. }
  551. func getPageIndexInfoMap(pageNum, pageSize int, includeInfo bool) (item *models.MySteelChemicalApiInfoResp, err error) {
  552. m := new(MySteelChemicalApiInfoBody)
  553. m.PageNum = pageNum
  554. m.PageSize = pageSize
  555. m.IncludeInfo = includeInfo
  556. postData, er := json.Marshal(m)
  557. if er != nil {
  558. err = er
  559. return
  560. }
  561. postUrl := `https://mds.mysteel.com/dynamic/order/api/fcAbRA`
  562. body, er := MySteelChemicalPost(postUrl, "info", postData)
  563. if er != nil {
  564. err = er
  565. return
  566. }
  567. err = json.Unmarshal(body, &item)
  568. if err != nil {
  569. return
  570. }
  571. if !item.Success {
  572. err = errors.New(item.Message)
  573. utils.FileLog.Info("code:" + item.Code + " message:" + item.Message)
  574. return
  575. }
  576. return
  577. }
  578. func MySteelChemicalPost(postUrl, hType string, postData []byte) (body []byte, err error) {
  579. req, er := http.NewRequest(`POST`, postUrl, strings.NewReader(string(postData)))
  580. if er != nil {
  581. err = er
  582. return
  583. }
  584. req.Header.Set(`Content-Type`, `application/json`)
  585. req.Header.Set(`accessTokenSign`, utils.MysteelChemicalApiToken)
  586. req.Header.Set(`infoOrData`, hType)
  587. client := &http.Client{}
  588. resp, er := client.Do(req)
  589. if er != nil {
  590. err = er
  591. return
  592. }
  593. defer resp.Body.Close()
  594. body, err = io.ReadAll(resp.Body)
  595. if err != nil {
  596. return
  597. }
  598. return
  599. }
  600. func RefreshDataFromMysteelChemical(edbCode, startDate, endDate string) (err error) {
  601. indexObj := &models.BaseFromMysteelChemicalIndex{}
  602. tmpIndex, err := indexObj.GetIndexItem(edbCode)
  603. if err != nil {
  604. return
  605. }
  606. terminal, err := GetTerminal(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, tmpIndex.TerminalCode)
  607. if err != nil {
  608. err = fmt.Errorf("获取钢联化工接口配置出错 Err: %s", err)
  609. return
  610. }
  611. if tmpIndex.TerminalCode == "" {
  612. // 设置指标与终端关系的缓存
  613. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + edbCode
  614. _ = utils.Rc.Put(terminalCodeCacheKey, terminal.TerminalCode, utils.GetTodayLastSecond())
  615. }
  616. // 如果配置了api的token, 那么就走api接口
  617. if utils.MysteelChemicalApiToken != "" {
  618. resp, er := GetEdbDataFromMySteelChemical([]string{edbCode}, startDate, endDate, "desc")
  619. if er != nil {
  620. err = er
  621. return
  622. }
  623. if !resp.Success {
  624. err = errors.New(resp.Message)
  625. return
  626. }
  627. dataObj := new(models.BaseFromMysteelChemicalData)
  628. exitDataList, er := dataObj.GetIndexDataList(edbCode)
  629. if er != nil {
  630. err = er
  631. return
  632. }
  633. existDataMap := make(map[string]*models.BaseFromMysteelChemicalData)
  634. for _, v := range exitDataList {
  635. dateStr := v.DataTime.Format(utils.FormatDate)
  636. existDataMap[dateStr] = v
  637. }
  638. mysteelChemicalDatas, er := tranformData(resp)
  639. if er != nil {
  640. err = er
  641. return
  642. }
  643. addItems := make([]*models.BaseFromMysteelChemicalData, 0)
  644. indexObj := &models.BaseFromMysteelChemicalIndex{}
  645. existIndex, er := indexObj.GetIndexItem(edbCode)
  646. if er != nil {
  647. err = er
  648. return
  649. }
  650. if len(mysteelChemicalDatas) == 0 {
  651. err = errors.New("没有获取到数据")
  652. return
  653. }
  654. // 因为只有一个指标,所以取第一个就可以了
  655. items := mysteelChemicalDatas[0]
  656. for _, v := range items {
  657. dateStr := v.DataTime.Format(utils.FormatDate)
  658. if findData, ok := existDataMap[dateStr]; !ok {
  659. v.BaseFromMysteelChemicalIndexId = existIndex.BaseFromMysteelChemicalIndexId
  660. addItems = append(addItems, v)
  661. } else {
  662. if findData != nil && findData.Value != v.Value {
  663. dataObj.BaseFromMysteelChemicalDataId = findData.BaseFromMysteelChemicalDataId
  664. dataObj.Value = v.Value
  665. dataObj.ModifyTime = time.Now()
  666. err = dataObj.Update([]string{"value", "modify_time"})
  667. if err != nil {
  668. return
  669. }
  670. }
  671. }
  672. }
  673. err = dataObj.AddV2(addItems)
  674. if err != nil {
  675. return
  676. }
  677. return
  678. }
  679. return
  680. }