base_from_mysteel_chemical.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. postUrl := utils.MySteelChemicalApiUrl
  513. if postUrl == "" {
  514. err = errors.New("钢联化工接口url未配置")
  515. return
  516. }
  517. body, err := MySteelChemicalPost(postUrl, "data", postData)
  518. if err != nil {
  519. return
  520. }
  521. err = json.Unmarshal(body, &item)
  522. if err != nil {
  523. return
  524. }
  525. return
  526. }
  527. // GetMySteelChemicalIndexNameMap 获取钢联化工的所有指标的信息
  528. func GetMySteelChemicalIndexNameMap() (indexNameMap map[string]*models.MySteelChemicalApiInfoItem, err error) {
  529. if utils.MysteelChemicalApiToken == "" {
  530. err = errors.New("钢联接口token未配置")
  531. return
  532. }
  533. item, err := getPageIndexInfoMap(1, 200, true)
  534. if err != nil {
  535. return
  536. }
  537. indexNameMap = make(map[string]*models.MySteelChemicalApiInfoItem)
  538. for _, v := range item.Data.List {
  539. indexNameMap[v.IndexCode] = v
  540. }
  541. // 如果总条数大于200,则继续获取
  542. if item.Data.Total > 200 || item.Data.Pages > 1 {
  543. for i := 2; i <= item.Data.Pages; i++ {
  544. item, err = getPageIndexInfoMap(i, 200, true)
  545. if err != nil {
  546. return
  547. }
  548. for _, v := range item.Data.List {
  549. indexNameMap[v.IndexCode] = v
  550. }
  551. }
  552. return
  553. }
  554. return
  555. }
  556. func getPageIndexInfoMap(pageNum, pageSize int, includeInfo bool) (item *models.MySteelChemicalApiInfoResp, err error) {
  557. m := new(MySteelChemicalApiInfoBody)
  558. m.PageNum = pageNum
  559. m.PageSize = pageSize
  560. m.IncludeInfo = includeInfo
  561. postData, er := json.Marshal(m)
  562. if er != nil {
  563. err = er
  564. return
  565. }
  566. // postUrl := `https://mds.mysteel.com/dynamic/order/api/fcAbRA`
  567. postUrl := utils.MySteelChemicalApiUrl
  568. if postUrl == "" {
  569. err = errors.New("钢联化工接口url未配置")
  570. return
  571. }
  572. body, er := MySteelChemicalPost(postUrl, "info", postData)
  573. if er != nil {
  574. err = er
  575. return
  576. }
  577. err = json.Unmarshal(body, &item)
  578. if err != nil {
  579. return
  580. }
  581. if !item.Success {
  582. err = errors.New(item.Message)
  583. utils.FileLog.Info("code:" + item.Code + " message:" + item.Message)
  584. return
  585. }
  586. return
  587. }
  588. func MySteelChemicalPost(postUrl, hType string, postData []byte) (body []byte, err error) {
  589. req, er := http.NewRequest(`POST`, postUrl, strings.NewReader(string(postData)))
  590. if er != nil {
  591. err = er
  592. return
  593. }
  594. req.Header.Set(`Content-Type`, `application/json`)
  595. req.Header.Set(`accessTokenSign`, utils.MysteelChemicalApiToken)
  596. req.Header.Set(`infoOrData`, hType)
  597. client := &http.Client{}
  598. resp, er := client.Do(req)
  599. if er != nil {
  600. err = er
  601. return
  602. }
  603. defer resp.Body.Close()
  604. body, err = io.ReadAll(resp.Body)
  605. if err != nil {
  606. return
  607. }
  608. return
  609. }
  610. func RefreshDataFromMysteelChemical(edbCode, startDate, endDate string) (err error) {
  611. indexObj := &models.BaseFromMysteelChemicalIndex{}
  612. tmpIndex, err := indexObj.GetIndexItem(edbCode)
  613. if err != nil {
  614. return
  615. }
  616. terminal, err := GetTerminal(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, tmpIndex.TerminalCode)
  617. if err != nil {
  618. err = fmt.Errorf("获取钢联化工接口配置出错 Err: %s", err)
  619. return
  620. }
  621. if tmpIndex.TerminalCode == "" {
  622. // 设置指标与终端关系的缓存
  623. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + edbCode
  624. _ = utils.Rc.Put(terminalCodeCacheKey, terminal.TerminalCode, utils.GetTodayLastSecond())
  625. }
  626. // 如果配置了api的token, 那么就走api接口
  627. if utils.MysteelChemicalApiToken != "" {
  628. resp, er := GetEdbDataFromMySteelChemical([]string{edbCode}, startDate, endDate, "desc")
  629. if er != nil {
  630. err = er
  631. return
  632. }
  633. if !resp.Success {
  634. err = errors.New(resp.Message)
  635. return
  636. }
  637. dataObj := new(models.BaseFromMysteelChemicalData)
  638. exitDataList, er := dataObj.GetIndexDataList(edbCode)
  639. if er != nil {
  640. err = er
  641. return
  642. }
  643. existDataMap := make(map[string]*models.BaseFromMysteelChemicalData)
  644. for _, v := range exitDataList {
  645. dateStr := v.DataTime.Format(utils.FormatDate)
  646. existDataMap[dateStr] = v
  647. }
  648. mysteelChemicalDatas, er := tranformData(resp)
  649. if er != nil {
  650. err = er
  651. return
  652. }
  653. addItems := make([]*models.BaseFromMysteelChemicalData, 0)
  654. indexObj := &models.BaseFromMysteelChemicalIndex{}
  655. existIndex, er := indexObj.GetIndexItem(edbCode)
  656. if er != nil {
  657. err = er
  658. return
  659. }
  660. if len(mysteelChemicalDatas) == 0 {
  661. err = errors.New("没有获取到数据")
  662. return
  663. }
  664. // 因为只有一个指标,所以取第一个就可以了
  665. items := mysteelChemicalDatas[0]
  666. for _, v := range items {
  667. dateStr := v.DataTime.Format(utils.FormatDate)
  668. if findData, ok := existDataMap[dateStr]; !ok {
  669. v.BaseFromMysteelChemicalIndexId = existIndex.BaseFromMysteelChemicalIndexId
  670. addItems = append(addItems, v)
  671. } else {
  672. if findData != nil && findData.Value != v.Value {
  673. dataObj.BaseFromMysteelChemicalDataId = findData.BaseFromMysteelChemicalDataId
  674. dataObj.Value = v.Value
  675. dataObj.ModifyTime = time.Now()
  676. err = dataObj.Update([]string{"value", "modify_time"})
  677. if err != nil {
  678. return
  679. }
  680. }
  681. }
  682. }
  683. err = dataObj.AddV2(addItems)
  684. if err != nil {
  685. return
  686. }
  687. return
  688. }
  689. return
  690. }