package com.cy.guoyan.admin.module.guoyan.controller.admin.banner;

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.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

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

import com.cy.guoyan.admin.framework.common.pojo.PageParam;
import com.cy.guoyan.admin.framework.common.pojo.PageResult;
import com.cy.guoyan.admin.framework.common.pojo.CommonResult;
import com.cy.guoyan.admin.framework.common.util.object.BeanUtils;
import static com.cy.guoyan.admin.framework.common.pojo.CommonResult.success;

import com.cy.guoyan.admin.framework.excel.core.util.ExcelUtils;

import com.cy.guoyan.admin.framework.apilog.core.annotation.ApiAccessLog;
import static com.cy.guoyan.admin.framework.apilog.core.enums.OperateTypeEnum.*;

import com.cy.guoyan.admin.module.guoyan.controller.admin.banner.vo.*;
import com.cy.guoyan.admin.module.guoyan.dal.dataobject.banner.BannerDO;
import com.cy.guoyan.admin.module.guoyan.service.banner.BannerService;

@Tag(name = "管理后台 - 横幅")
@RestController
@RequestMapping("/guoyan/banner")
@Validated
public class BannerController {

    @Resource
    private BannerService bannerService;

    @PostMapping("/create")
    @Operation(summary = "创建横幅")
    @PreAuthorize("@ss.hasPermission('guoyan:banner:create')")
    public CommonResult<Long> createBanner(@Valid @RequestBody BannerSaveReqVO createReqVO) {
        return success(bannerService.createBanner(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新横幅")
    @PreAuthorize("@ss.hasPermission('guoyan:banner:update')")
    public CommonResult<Boolean> updateBanner(@Valid @RequestBody BannerSaveReqVO updateReqVO) {
        bannerService.updateBanner(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除横幅")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('guoyan:banner:delete')")
    public CommonResult<Boolean> deleteBanner(@RequestParam("id") Long id) {
        bannerService.deleteBanner(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除横幅")
                @PreAuthorize("@ss.hasPermission('guoyan:banner:delete')")
    public CommonResult<Boolean> deleteBannerList(@RequestParam("ids") List<Long> ids) {
        bannerService.deleteBannerListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得横幅")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('guoyan:banner:query')")
    public CommonResult<BannerRespVO> getBanner(@RequestParam("id") Long id) {
        BannerDO banner = bannerService.getBanner(id);
        return success(BeanUtils.toBean(banner, BannerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得横幅分页")
    @PreAuthorize("@ss.hasPermission('guoyan:banner:query')")
    public CommonResult<PageResult<BannerRespVO>> getBannerPage(@Valid BannerPageReqVO pageReqVO) {
        PageResult<BannerDO> pageResult = bannerService.getBannerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BannerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出横幅 Excel")
    @PreAuthorize("@ss.hasPermission('guoyan:banner:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBannerExcel(@Valid BannerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BannerDO> list = bannerService.getBannerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "横幅.xls", "数据", BannerRespVO.class,
                        BeanUtils.toBean(list, BannerRespVO.class));
    }

}