package com.miaxis.pc.controller.carousel;

import com.miaxis.carousel.domain.CarouselChartInfo;
import com.miaxis.carousel.service.ICarouselChartInfoService;
import com.miaxis.carousel.vo.CarouselChartInfoVo;
import com.miaxis.common.annotation.Log;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 【轮播图】Controller
 *
 * @author miaxis
 * @date 2021-03-11
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/pc/carousel")
@Api(tags={"【pc-轮播图】"})
public class CarouselChartInfoController extends BaseController{

    private final ICarouselChartInfoService carouselChartInfoService;

    /**
     * 查询轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('carousel:carousel:list')")
    @GetMapping("/list")
    @ApiOperation("查询轮播图列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<CarouselChartInfoVo> list(@ModelAttribute CarouselChartInfo carouselChartInfo){
        startPage();
        List<CarouselChartInfoVo> list = carouselChartInfoService.selectCarouselChartInfoList(carouselChartInfo);
        return toResponsePageInfo(list);
    }


    /**
     * 新增轮播图
     */
    @PreAuthorize("@ss.hasPermi('carousel:carousel:add')")
    @Log(title = "轮播图", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation("新增轮播图")
    public Response<Integer> add(@RequestBody List<CarouselChartInfo> carouselChartInfo){
        List<CarouselChartInfo> collect = carouselChartInfo.stream().filter(c -> c.getId() == null).collect(Collectors.toList());
        return toResponse(carouselChartInfoService.saveBatch(collect) ? 1 : 0);
    }


    /**
     * 删除轮播图(伪删除)
     */
    @PreAuthorize("@ss.hasPermi('carousel:carousel:remove')")
    @Log(title = "轮播图", businessType = BusinessTypeEnum.UPDATE)
	@PutMapping("/{ids}")
    @ApiOperation("删除轮播图")
    public  Response<Integer> remove(
            @ApiParam(name = "ids", value = "轮播图ids参数", required = true)
            @PathVariable Long[] ids
    ){
        return carouselChartInfoService.removeCarouselByIds(ids);
    }
}
