fwh_shutdownList.go 2.0 KB

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