package shop.alien.second.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.second.SecondGoods;
import shop.alien.entity.second.vo.SecondGoodsVo;
import shop.alien.second.service.SecondGoodsService;
import shop.alien.util.common.JwtUtil;

import java.util.List;

/**
 * 二手商品控制器
 */
@Slf4j
@Api(tags = {"二手平台-商品管理-搜索相关"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/secondGoods")
@RequiredArgsConstructor
public class SearchGoodsController {

    private final SecondGoodsService secondGoodsService;


    /**
     * 搜索商品列表（包含商品信息、图片、用户信息），按距离和创建时间倒序
     */
    @PostMapping("/search")
    @ApiOperation("搜索结果-商品列表")
    public R<IPage<SecondGoodsVo>> searchGoodsList(
            @ApiParam("二手商品搜索条件") @RequestBody SecondGoodsVo secondGoodsVo) {
        log.info("SecondGoodsController.searchGoodsList?secondGoodsVo={}", secondGoodsVo.toString());
        R<IPage<SecondGoodsVo>> result = new R<>();
        IPage<SecondGoodsVo> page = new Page<>(secondGoodsVo.getPageNum(), secondGoodsVo.getPageSize());
        JSONObject data = JwtUtil.getCurrentUserInfo();
        if (null != data) {
            int userId = data.getInteger("userId");
            String phone = data.getString("phone");
            secondGoodsVo.setUserPhone(phone);
            result = R.data(secondGoodsService.searchGoodsList(page, userId,secondGoodsVo), "查询成功");
        }
        return result;
    }

    /**
     * 获取商品热卖排行榜（前10名）
     */
    @GetMapping("/getHotSellingRanking")
    @ApiOperation("推荐 - 获取商品点赞热卖排行榜（前10名）")
    public R<List<SecondGoods>> getHotSellingRanking() {
        return R.data(secondGoodsService.getHotSellingRankingTop10(), "获取成功");
    }

    /**
     * 大家都在看（前10名）
     */
    @GetMapping("/getCollectTop10")
    @ApiOperation("大家都在看 - 获取商品收藏排行榜（前10名）")
    public R<List<SecondGoods>> getCollectTop10() {
        return R.data(secondGoodsService.getCollectTop10(), "获取成功");
    }

    /**
     * 查询商品列表
     */
    @GetMapping("/getGoodsListByUserId")
    @ApiOperation("查询商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "goodsStatus", value = "商品状态 0:草稿 1:审核中 2:审核失败 3:已上架 4:已下架 5:已售出", dataType = "Integer", paramType = "query")})
    public R<List<SecondGoods>> getGoodsListByUserId(Integer userId, Integer goodsStatus) {
        return R.data(secondGoodsService.getGoodsListByUserId(userId, goodsStatus), "获取成功");
    }

}