package com.weavi.controller;

import com.weavi.entity.Goods;
import com.weavi.service.GoodsService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
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 java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @RequestMapping("/page")
    public List<Goods> sortPage(@RequestParam(name="index") int pageIndex){
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable page = new PageRequest(pageIndex-1,2,sort);
        Page<Goods> goodsDatas = goodsService.findAll(page);
        System.out.println("查询总页数："+goodsDatas.getTotalPages());
        System.out.println("查询总记录数："+goodsDatas.getTotalElements());
        System.out.println("查询当前第几页："+goodsDatas.getNumber()+1);
        System.out.println("查询当前页的记录数："+goodsDatas.getNumberOfElements());
        List<Goods> goods = goodsDatas.getContent();
        return goods;
    }
}
