package com.amms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.amms.domain.vo.Result;
import com.amms.domain.Carousel;
import com.amms.service.ICarouselService;

import java.util.List;

/**
 * 轮播图Controller
 */
@RestController
@RequestMapping("/carousel")
public class CarouselController {

    @Autowired
    private ICarouselService carouselService;

    /**
     * 查询轮播图列表
     */
    @GetMapping("/list")
    public PageInfo list(Carousel carousel, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Carousel> carousels = carouselService.selectCarouselList(carousel);
        return new PageInfo(carousels);
    }

    /**
     * 查询全部轮播图列表
     */
    @GetMapping("/listAll")
    public List<Carousel> listAll(Carousel carousel) {
        return carouselService.selectCarouselList(carousel);
    }

    /**
     * 获取轮播图详细信息
     */
    @GetMapping(value = "/info/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return Result.success(carouselService.selectCarouselById(id));
    }

    /**
     * 新增轮播图
     */
    @PostMapping("/add")
    public Result add(@RequestBody Carousel carousel) {
        return carouselService.insertCarousel(carousel) > 0 ? Result.success("新增成功") : Result.error("新增失败");
    }

    /**
     * 修改轮播图
     */
    @PutMapping
    public Result update(@RequestBody Carousel carousel) {
        return carouselService.updateCarousel(carousel) > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 删除轮播图
     */
	@DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        return carouselService.deleteCarouselById(id) > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }
}
