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

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.researchtopiccategory.vo.*;
import com.cy.guoyan.admin.module.guoyan.dal.dataobject.researchtopiccategory.ResearchTopicCategoryDO;
import com.cy.guoyan.admin.module.guoyan.service.researchtopiccategory.ResearchTopicCategoryService;

@Tag(name = "管理后台 - 国资研究专题分类")
@RestController
@RequestMapping("/guoyan/research-topic-category")
@Validated
public class ResearchTopicCategoryController {

    @Resource
    private ResearchTopicCategoryService researchTopicCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建国资研究专题分类")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-category:create')")
    public CommonResult<Long> createResearchTopicCategory(@Valid @RequestBody ResearchTopicCategorySaveReqVO createReqVO) {
        return success(researchTopicCategoryService.createResearchTopicCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新国资研究专题分类")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-category:update')")
    public CommonResult<Boolean> updateResearchTopicCategory(@Valid @RequestBody ResearchTopicCategorySaveReqVO updateReqVO) {
        researchTopicCategoryService.updateResearchTopicCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除国资研究专题分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-category:delete')")
    public CommonResult<Boolean> deleteResearchTopicCategory(@RequestParam("id") Long id) {
        researchTopicCategoryService.deleteResearchTopicCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除国资研究专题分类")
                @PreAuthorize("@ss.hasPermission('guoyan:research-topic-category:delete')")
    public CommonResult<Boolean> deleteResearchTopicCategoryList(@RequestParam("ids") List<Long> ids) {
        researchTopicCategoryService.deleteResearchTopicCategoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得国资研究专题分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-category:query')")
    public CommonResult<ResearchTopicCategoryRespVO> getResearchTopicCategory(@RequestParam("id") Long id) {
        ResearchTopicCategoryDO researchTopicCategory = researchTopicCategoryService.getResearchTopicCategory(id);
        return success(BeanUtils.toBean(researchTopicCategory, ResearchTopicCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得国资研究专题分类分页")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-category:query')")
    public CommonResult<PageResult<ResearchTopicCategoryRespVO>> getResearchTopicCategoryPage(@Valid ResearchTopicCategoryPageReqVO pageReqVO) {
        PageResult<ResearchTopicCategoryDO> pageResult = researchTopicCategoryService.getResearchTopicCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResearchTopicCategoryRespVO.class));
    }

    @GetMapping("/tree")
    @Operation(summary = "获得国资研究专题分类树")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-category:query')")
    public CommonResult<List<ResearchTopicCategoryTreeRespVO>> getResearchTopicCategoryTree(@Valid ResearchTopicCategoryPageReqVO pageReqVO) {
        List<ResearchTopicCategoryDO> pageResult = researchTopicCategoryService.getResearchTopicCategoryTree(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResearchTopicCategoryTreeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出国资研究专题分类 Excel")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportResearchTopicCategoryExcel(@Valid ResearchTopicCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResearchTopicCategoryDO> list = researchTopicCategoryService.getResearchTopicCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "国资研究专题分类.xls", "数据", ResearchTopicCategoryRespVO.class,
                        BeanUtils.toBean(list, ResearchTopicCategoryRespVO.class));
    }

}