| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package hsby
- import (
- "mtp2_if/global/app"
- "mtp2_if/global/e"
- "mtp2_if/logger"
- "mtp2_if/models"
- "net/http"
- "sort"
- "github.com/gin-gonic/gin"
- )
- // QueryTopGoodsReq 查询热门商品列表请求参数
- type QueryTopGoodsReq struct {
- app.PageInfo
- MarketID int `form:"marketID" binding:"required"`
- DescProvinceID int `form:"descProvinceID"`
- DescCityID int `form:"descCityID"`
- }
- // QueryTopGoods 查询热门商品列表
- // @Summary 查询热门商品列表
- // @Description 说明:查询结果已按Hotindex(景点热度)从大到小排序
- // @Produce json
- // @Security ApiKeyAuth
- // @Param page query int false "页码"
- // @Param pagesize query int false "每页条数"
- // @Param marketID query int true "市场ID"
- // @Param DescProvinceID query int false "目的地(省)ID"
- // @Param DescCityID query int false "目的地(市)ID"
- // @Success 200 {object} models.TopGoods
- // @Failure 500 {object} app.Response
- // @Router /HSBY/QueryTopGoods [get]
- // @Tags 定制【海商报业】
- func QueryTopGoods(c *gin.Context) {
- appG := app.Gin{C: c}
- // 获取请求参数
- var req QueryTopGoodsReq
- if err := appG.C.ShouldBindQuery(&req); err != nil {
- logger.GetLogger().Errorf("QueryTopGoods failed: %s", err.Error())
- appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
- return
- }
- // 获取数据
- topGoodses, err := models.GetHsbyTopGoods(req.MarketID, req.DescProvinceID, req.DescCityID)
- if err != nil {
- // 查询失败
- logger.GetLogger().Errorf("QueryTopGoods failed: %s", err.Error())
- appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
- return
- }
- // 排序
- sort.Slice(topGoodses, func(i int, j int) bool {
- return topGoodses[i].Hotindex > topGoodses[j].Hotindex
- })
- // 分页
- total := len(topGoodses)
- if req.PageSize > 0 {
- rstByPage := make([]models.TopGoods, 0)
- // 开始上标
- start := req.Page * req.PageSize
- // 结束下标
- end := start + req.PageSize
- if start <= len(topGoodses) {
- // 判断结束下标是否越界
- if end > len(topGoodses) {
- end = len(topGoodses)
- }
- rstByPage = topGoodses[start:end]
- } else {
- rstByPage = topGoodses[0:0]
- }
- topGoodses = rstByPage
- }
- // 查询成功返回
- if req.PageSize > 0 {
- logger.GetLogger().Debugln("QueryTopGoods successed: %v", topGoodses)
- appG.ResponseByPage(http.StatusOK, e.SUCCESS, topGoodses, app.PageInfo{Page: req.Page, PageSize: req.PageSize, Total: total})
- } else {
- logger.GetLogger().Debugln("QueryTopGoods successed: %v", topGoodses)
- appG.Response(http.StatusOK, e.SUCCESS, topGoodses)
- }
- }
|