package com.jojubanking.boot.module.system.controller.admin.carousel;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

import com.jojubanking.boot.framework.excel.core.util.ExcelUtils;

import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import static com.jojubanking.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.jojubanking.boot.module.system.controller.admin.carousel.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.carousel.CarouselDO;
import com.jojubanking.boot.module.system.convert.carousel.CarouselConvert;
import com.jojubanking.boot.module.system.service.carousel.CarouselService;

@Api(tags = "管理后台 - 轮播图")
@RestController
@RequestMapping("/system/carousel")
@Validated
public class CarouselController {

    @Resource
    private CarouselService carouselService;

    @PostMapping("/create")
    @ApiOperation("创建轮播图")
    @PreAuthorize("@ss.hasPermission('system:carousel:create')")
    public CommonResult<Integer> createCarousel(@Valid @RequestBody CarouselCreateReqVO createReqVO) {
        return success(carouselService.createCarousel(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新轮播图")
    @PreAuthorize("@ss.hasPermission('system:carousel:update')")
    public CommonResult<Boolean> updateCarousel(@Valid @RequestBody CarouselUpdateReqVO updateReqVO) {
        carouselService.updateCarousel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除轮播图")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:carousel:delete')")
    public CommonResult<Boolean> deleteCarousel(@RequestParam("id") Integer id) {
        carouselService.deleteCarousel(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得轮播图")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:carousel:query')")
    public CommonResult<CarouselRespVO> getCarousel(@RequestParam("id") Integer id) {
        CarouselDO carousel = carouselService.getCarousel(id);
        return success(CarouselConvert.INSTANCE.convert(carousel));
    }

    @GetMapping("/list")
    @ApiOperation("获得轮播图列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:carousel:query')")
    public CommonResult<List<CarouselRespVO>> getCarouselList(@RequestParam("ids") Collection<Integer> ids) {
        List<CarouselDO> list = carouselService.getCarouselList(ids);
        return success(CarouselConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得轮播图分页")
    @PreAuthorize("@ss.hasPermission('system:carousel:query')")
    public CommonResult<PageResult<CarouselRespVO>> getCarouselPage(@Valid CarouselPageReqVO pageVO) {
        PageResult<CarouselDO> pageResult = carouselService.getCarouselPage(pageVO);
        return success(CarouselConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出轮播图 Excel")
    @PreAuthorize("@ss.hasPermission('system:carousel:export')")
    @OperateLog(type = EXPORT)
    public void exportCarouselExcel(@Valid CarouselExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CarouselDO> list = carouselService.getCarouselList(exportReqVO);
        // 导出 Excel
        List<CarouselExcelVO> datas = CarouselConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "轮播图.xls", "数据", CarouselExcelVO.class, datas);
    }

}
