package cn.iocoder.yudao.module.portal.controller.admin.newstype;

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 cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.portal.controller.admin.newstype.vo.*;
import cn.iocoder.yudao.module.portal.dal.dataobject.newstype.NewsTypeDO;
import cn.iocoder.yudao.module.portal.service.newstype.NewsTypeService;

@Tag(name = "管理后台 - 新闻类型")
@RestController
@RequestMapping("/portal/news-type")
@Validated
public class NewsTypeController {

    @Resource
    private NewsTypeService newsTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建新闻类型")
    @PreAuthorize("@ss.hasPermission('portal:news-type:create')")
    public CommonResult<Long> createNewsType(@Valid @RequestBody NewsTypeSaveReqVO createReqVO) {
        return success(newsTypeService.createNewsType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新新闻类型")
    @PreAuthorize("@ss.hasPermission('portal:news-type:update')")
    public CommonResult<Boolean> updateNewsType(@Valid @RequestBody NewsTypeSaveReqVO updateReqVO) {
        newsTypeService.updateNewsType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除新闻类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('portal:news-type:delete')")
    public CommonResult<Boolean> deleteNewsType(@RequestParam("id") Long id) {
        newsTypeService.deleteNewsType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得新闻类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('portal:news-type:query')")
    public CommonResult<NewsTypeRespVO> getNewsType(@RequestParam("id") Long id) {
        NewsTypeDO newsType = newsTypeService.getNewsType(id);
        return success(BeanUtils.toBean(newsType, NewsTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得新闻类型分页")
    @PreAuthorize("@ss.hasPermission('portal:news-type:query')")
    public CommonResult<PageResult<NewsTypeRespVO>> getNewsTypePage(@Valid NewsTypePageReqVO pageReqVO) {
        PageResult<NewsTypeDO> pageResult = newsTypeService.getNewsTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NewsTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出新闻类型 Excel")
    @PreAuthorize("@ss.hasPermission('portal:news-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportNewsTypeExcel(@Valid NewsTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NewsTypeDO> list = newsTypeService.getNewsTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "新闻类型.xls", "数据", NewsTypeRespVO.class,
                        BeanUtils.toBean(list, NewsTypeRespVO.class));
    }

}