hsby.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package hsby
  2. import (
  3. "mtp2_if/global/app"
  4. "mtp2_if/global/e"
  5. "mtp2_if/logger"
  6. "mtp2_if/models"
  7. "net/http"
  8. "sort"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // QueryTopGoodsReq 查询热门商品列表请求参数
  12. type QueryTopGoodsReq struct {
  13. app.PageInfo
  14. MarketID int `form:"marketID" binding:"required"`
  15. DescProvinceID int `form:"descProvinceID"`
  16. DescCityID int `form:"descCityID"`
  17. }
  18. // QueryTopGoods 查询热门商品列表
  19. // @Summary 查询热门商品列表
  20. // @Description 说明:查询结果已按Hotindex(景点热度)从大到小排序
  21. // @Produce json
  22. // @Security ApiKeyAuth
  23. // @Param page query int false "页码"
  24. // @Param pagesize query int false "每页条数"
  25. // @Param marketID query int true "市场ID"
  26. // @Param DescProvinceID query int false "目的地(省)ID"
  27. // @Param DescCityID query int false "目的地(市)ID"
  28. // @Success 200 {object} models.TopGoods
  29. // @Failure 500 {object} app.Response
  30. // @Router /HSBY/QueryTopGoods [get]
  31. // @Tags 定制【海商报业】
  32. func QueryTopGoods(c *gin.Context) {
  33. appG := app.Gin{C: c}
  34. // 获取请求参数
  35. var req QueryTopGoodsReq
  36. if err := appG.C.ShouldBindQuery(&req); err != nil {
  37. logger.GetLogger().Errorf("QueryTopGoods failed: %s", err.Error())
  38. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  39. return
  40. }
  41. // 获取数据
  42. topGoodses, err := models.GetHsbyTopGoods(req.MarketID, req.DescProvinceID, req.DescCityID)
  43. if err != nil {
  44. // 查询失败
  45. logger.GetLogger().Errorf("QueryTopGoods failed: %s", err.Error())
  46. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  47. return
  48. }
  49. // 排序
  50. sort.Slice(topGoodses, func(i int, j int) bool {
  51. return topGoodses[i].Hotindex > topGoodses[j].Hotindex
  52. })
  53. // 分页
  54. total := len(topGoodses)
  55. if req.PageSize > 0 {
  56. rstByPage := make([]models.TopGoods, 0)
  57. // 开始上标
  58. start := req.Page * req.PageSize
  59. // 结束下标
  60. end := start + req.PageSize
  61. if start <= len(topGoodses) {
  62. // 判断结束下标是否越界
  63. if end > len(topGoodses) {
  64. end = len(topGoodses)
  65. }
  66. rstByPage = topGoodses[start:end]
  67. } else {
  68. rstByPage = topGoodses[0:0]
  69. }
  70. topGoodses = rstByPage
  71. }
  72. // 查询成功返回
  73. if req.PageSize > 0 {
  74. logger.GetLogger().Debugln("QueryTopGoods successed: %v", topGoodses)
  75. appG.ResponseByPage(http.StatusOK, e.SUCCESS, topGoodses, app.PageInfo{Page: req.Page, PageSize: req.PageSize, Total: total})
  76. } else {
  77. logger.GetLogger().Debugln("QueryTopGoods successed: %v", topGoodses)
  78. appG.Response(http.StatusOK, e.SUCCESS, topGoodses)
  79. }
  80. }