package com.slodon.b2b2c.controller.goods.front;


import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.goods.example.GoodsSearchWordsExample;
import com.slodon.b2b2c.goods.pojo.GoodsSearchWords;
import com.slodon.b2b2c.model.goods.GoodsSearchWordsModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "front-高频搜索词")
@RestController
@Slf4j
@RequestMapping("/v3/goods/front/goods/searchWords")
public class GoodsSearchWordsController extends BaseController {
    @Resource
    private GoodsSearchWordsModel goodsSearchWordsModel;

    @ApiOperation("高频搜索词列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyWord", value = "搜索词", required = true, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "最多展示条数", defaultValue = "10", paramType = "query"),
    })
    @GetMapping("list")
    public JsonResult<List<GoodsSearchWords>> list(HttpServletRequest request,
                                                   @RequestParam(value = "keyWord") String keyWord,
                                                   @RequestParam(value = "size",required = false,defaultValue = "10") Integer size) {
        String webSite = WebUtil.getWebSite(request);
        PagerInfo pager = new PagerInfo(size,1);
        GoodsSearchWordsExample example = new GoodsSearchWordsExample();
        example.setWebSite(webSite);
        example.setWordsContentOrPyLike(keyWord);
        example.setSearchGoodsNumGte(1);
        example.setOrderBy("search_frequency desc,search_goods_num desc,create_time desc");
        List<GoodsSearchWords> list = goodsSearchWordsModel.getGoodsSearchWordsList(example, pager);

        return SldResponse.success(list);
    }

}
