fwh_shutdownList.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package fwh_pro
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "fwh-toolbox-server/config"
  6. "fwh-toolbox-server/domain"
  7. "net/url"
  8. "strconv"
  9. )
  10. type ShutdownListResponse struct {
  11. Code int `json:"code"`
  12. Msg string `json:"msg"`
  13. Data ShutdownListData `json:"data"`
  14. }
  15. type ShutdownListData struct {
  16. Current int `json:"current"` // 当前页
  17. Pages int `json:"pages"` // 总页数
  18. Size int `json:"size"` // 每页最大条数
  19. Total int64 `json:"total"` // 总条数
  20. Records []domain.ShutdownRecord `json:"records"` // 记录列表
  21. }
  22. // 查询Shutdown站点列表 one page
  23. func querySiteShutdownListOnePage(date string, country int, page int) ([]byte, error) {
  24. // 构造 query 参数
  25. params := url.Values{}
  26. if country <= 0 {
  27. params.Add("countryId", "") // 0-ALL
  28. } else {
  29. params.Add("countryId", strconv.Itoa(country)) // 2-美国 3-澳大利亚 5-加拿大 6-新西兰 51-巴巴多斯
  30. }
  31. params.Add("date", date)
  32. params.Add("timeType", strconv.Itoa(1)) // 0-通讯时间;1-设备时间
  33. params.Add("pageSize", strconv.FormatUint(REQ_PAGE_SIZE, 10))
  34. params.Add("pageNum", strconv.Itoa(page))
  35. params.Add("land", "ZH_CN")
  36. reqUrl := fmt.Sprintf("https://%s/api-inspect/fault/listShutdownGateway", config.FWH_ENERGY)
  37. response, err := FwhHttpGet(reqUrl, params)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return response.Body, nil
  42. }
  43. // 查询Shutdown站点列表
  44. func QuerySiteShutdownList(date string, country int) ([]domain.ShutdownRecord, error) {
  45. var total int64 = 0
  46. var page int = 0
  47. var records []domain.ShutdownRecord = []domain.ShutdownRecord{}
  48. for total == 0 || REQ_PAGE_SIZE*uint64(page) < uint64(total) {
  49. page++
  50. resByte, err := querySiteShutdownListOnePage(date, country, page)
  51. if err != nil {
  52. return nil, err
  53. }
  54. var shutdownListRes ShutdownListResponse
  55. // 反序列化(unmarshal)
  56. jsonErr := json.Unmarshal(resByte, &shutdownListRes)
  57. if jsonErr != nil {
  58. return nil, jsonErr
  59. }
  60. total = int64(shutdownListRes.Data.Total)
  61. records = append(records, shutdownListRes.Data.Records...)
  62. }
  63. return records, nil
  64. }