package cn.pluss.platform.controller;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.banner.BannerService;
import cn.pluss.platform.entity.Banner;
import cn.pluss.platform.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author DJH
 */
@RestController
@RequestMapping("/banner")
@Api(value = "Banner控制类", tags = {"Banner接口"})
public class BannerController {

    @Resource
    private BannerService bannerService;

    @PostMapping
    @ApiOperation(value = "添加数据", notes = "添加新的数据", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Banner", value = "待添加的Banner实例", paramType = "body", dataType = "Banner", required = true)})
    public Result<Object> add(@RequestBody Banner banner) {
        bannerService.saveBanner(banner);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/queryBannerList")
    @ApiOperation(tags = {"页面-首页"}, value = "查询所有的轮播图", notes = "查询所有的轮播图", httpMethod = "POST")
    public Result<List<Banner>> queryBannerList(@RequestBody Banner banner) {
        if (banner == null) {
            return ResultGenerator.genFailResult("参数错误！");
        }
        if (banner.getType() == null || StringUtil.isEmpty(banner.getType())) {
            return ResultGenerator.genFailResult("查询参数错误！");
        }

        Banner queryBanner = new Banner();
        queryBanner.setType(banner.getType());
        List<Banner> banners = bannerService.queryBannerList(queryBanner);
        return ResultGenerator.genSuccessResult(banners);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据", notes = "根据id删除数据", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询的id实例", paramType = "path", dataType = "Integer", required = true)})
    public Result<Object> delete(@PathVariable Integer id) {
        Banner banner = new Banner();
        banner.setId(id);
        bannerService.deleteBanner(banner);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    @ApiOperation(value = "更新数据", notes = "根据内容更新数据", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Banner", value = "更新的Banner实例", paramType = "body", dataType = "Banner", required = true)})
    public Result<Object> update(@RequestBody Banner banner) {
        bannerService.updateBanner(banner);
        return ResultGenerator.genSuccessResult();
    }

}
