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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.portal.controller.admin.newstype.vo.NewsTypePageReqVO;
import cn.iocoder.yudao.module.portal.controller.admin.newstype.vo.NewsTypeRespVO;
import cn.iocoder.yudao.module.portal.controller.admin.newstype.vo.NewsTypeSaveReqVO;
import cn.iocoder.yudao.module.portal.dal.dataobject.newstype.NewsTypeDO;
import cn.iocoder.yudao.module.portal.service.newstype.NewsTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

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

    @Resource
    private NewsTypeService newsTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建新闻类型")
    public CommonResult<Long> createNewsType(@Valid @RequestBody NewsTypeSaveReqVO createReqVO) {
        return success(newsTypeService.createNewsType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新新闻类型")
    public CommonResult<Boolean> updateNewsType(@Valid @RequestBody NewsTypeSaveReqVO updateReqVO) {
        newsTypeService.updateNewsType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除新闻类型")
    @Parameter(name = "id", description = "编号", required = true)
    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")
    public CommonResult<NewsTypeRespVO> getNewsType(@RequestParam("id") Long id) {
        NewsTypeDO newsType = newsTypeService.getNewsType(id);
        return success(BeanUtils.toBean(newsType, NewsTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得新闻类型分页")
    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")
    @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));
    }

}